PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - pdo_sqlite/sqlite/src - expr.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 1015
Code covered: 48.5 % Executed lines: 492
Legend: not executed executed

       1                 : /*
       2                 : ** 2001 September 15
       3                 : **
       4                 : ** The author disclaims copyright to this source code.  In place of
       5                 : ** a legal notice, here is a blessing:
       6                 : **
       7                 : **    May you do good and not evil.
       8                 : **    May you find forgiveness for yourself and forgive others.
       9                 : **    May you share freely, never taking more than you give.
      10                 : **
      11                 : *************************************************************************
      12                 : ** This file contains routines used for analyzing expressions and
      13                 : ** for generating VDBE code that evaluates expressions in SQLite.
      14                 : **
      15                 : ** $Id$
      16                 : */
      17                 : #include "sqliteInt.h"
      18                 : #include <ctype.h>
      19                 : 
      20                 : /*
      21                 : ** Return the 'affinity' of the expression pExpr if any.
      22                 : **
      23                 : ** If pExpr is a column, a reference to a column via an 'AS' alias,
      24                 : ** or a sub-select with a column as the return value, then the 
      25                 : ** affinity of that column is returned. Otherwise, 0x00 is returned,
      26                 : ** indicating no affinity for the expression.
      27                 : **
      28                 : ** i.e. the WHERE clause expresssions in the following statements all
      29                 : ** have an affinity:
      30                 : **
      31                 : ** CREATE TABLE t1(a);
      32                 : ** SELECT * FROM t1 WHERE a;
      33                 : ** SELECT a AS b FROM t1 WHERE b;
      34                 : ** SELECT * FROM t1 WHERE (select a from t1);
      35                 : */
      36             194 : char sqlite3ExprAffinity(Expr *pExpr){
      37             194 :   int op = pExpr->op;
      38             194 :   if( op==TK_AS ){
      39               0 :     return sqlite3ExprAffinity(pExpr->pLeft);
      40                 :   }
      41             194 :   if( op==TK_SELECT ){
      42               0 :     return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
      43                 :   }
      44                 : #ifndef SQLITE_OMIT_CAST
      45             194 :   if( op==TK_CAST ){
      46               0 :     return sqlite3AffinityType(&pExpr->token);
      47                 :   }
      48                 : #endif
      49             194 :   return pExpr->affinity;
      50                 : }
      51                 : 
      52                 : /*
      53                 : ** Set the collating sequence for expression pExpr to be the collating
      54                 : ** sequence named by pToken.   Return a pointer to the revised expression.
      55                 : ** The collating sequence is marked as "explicit" using the EP_ExpCollate
      56                 : ** flag.  An explicit collating sequence will override implicit
      57                 : ** collating sequences.
      58                 : */
      59               0 : Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
      60                 :   CollSeq *pColl;
      61               0 :   if( pExpr==0 ) return 0;
      62               0 :   pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
      63               0 :   if( pColl ){
      64               0 :     pExpr->pColl = pColl;
      65               0 :     pExpr->flags |= EP_ExpCollate;
      66                 :   }
      67               0 :   return pExpr;
      68                 : }
      69                 : 
      70                 : /*
      71                 : ** Return the default collation sequence for the expression pExpr. If
      72                 : ** there is no default collation type, return 0.
      73                 : */
      74             106 : CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
      75             106 :   CollSeq *pColl = 0;
      76             106 :   if( pExpr ){
      77             106 :     pColl = pExpr->pColl;
      78             106 :     if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
      79               0 :       return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
      80                 :     }
      81                 :   }
      82             106 :   if( sqlite3CheckCollSeq(pParse, pColl) ){ 
      83               0 :     pColl = 0;
      84                 :   }
      85             106 :   return pColl;
      86                 : }
      87                 : 
      88                 : /*
      89                 : ** pExpr is an operand of a comparison operator.  aff2 is the
      90                 : ** type affinity of the other operand.  This routine returns the
      91                 : ** type affinity that should be used for the comparison operator.
      92                 : */
      93              97 : char sqlite3CompareAffinity(Expr *pExpr, char aff2){
      94              97 :   char aff1 = sqlite3ExprAffinity(pExpr);
      95              97 :   if( aff1 && aff2 ){
      96                 :     /* Both sides of the comparison are columns. If one has numeric
      97                 :     ** affinity, use that. Otherwise use no affinity.
      98                 :     */
      99              10 :     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
     100              10 :       return SQLITE_AFF_NUMERIC;
     101                 :     }else{
     102               0 :       return SQLITE_AFF_NONE;
     103                 :     }
     104              87 :   }else if( !aff1 && !aff2 ){
     105                 :     /* Neither side of the comparison is a column.  Compare the
     106                 :     ** results directly.
     107                 :     */
     108               0 :     return SQLITE_AFF_NONE;
     109                 :   }else{
     110                 :     /* One side is a column, the other is not. Use the columns affinity. */
     111                 :     assert( aff1==0 || aff2==0 );
     112              87 :     return (aff1 + aff2);
     113                 :   }
     114                 : }
     115                 : 
     116                 : /*
     117                 : ** pExpr is a comparison operator.  Return the type affinity that should
     118                 : ** be applied to both operands prior to doing the comparison.
     119                 : */
     120              18 : static char comparisonAffinity(Expr *pExpr){
     121                 :   char aff;
     122                 :   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
     123                 :           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
     124                 :           pExpr->op==TK_NE );
     125                 :   assert( pExpr->pLeft );
     126              18 :   aff = sqlite3ExprAffinity(pExpr->pLeft);
     127              18 :   if( pExpr->pRight ){
     128              18 :     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
     129                 :   }
     130               0 :   else if( pExpr->pSelect ){
     131               0 :     aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
     132                 :   }
     133               0 :   else if( !aff ){
     134               0 :     aff = SQLITE_AFF_NONE;
     135                 :   }
     136              18 :   return aff;
     137                 : }
     138                 : 
     139                 : /*
     140                 : ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
     141                 : ** idx_affinity is the affinity of an indexed column. Return true
     142                 : ** if the index with affinity idx_affinity may be used to implement
     143                 : ** the comparison in pExpr.
     144                 : */
     145              18 : int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
     146              18 :   char aff = comparisonAffinity(pExpr);
     147              18 :   switch( aff ){
     148                 :     case SQLITE_AFF_NONE:
     149               0 :       return 1;
     150                 :     case SQLITE_AFF_TEXT:
     151               2 :       return idx_affinity==SQLITE_AFF_TEXT;
     152                 :     default:
     153              16 :       return sqlite3IsNumericAffinity(idx_affinity);
     154                 :   }
     155                 : }
     156                 : 
     157                 : /*
     158                 : ** Return the P1 value that should be used for a binary comparison
     159                 : ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
     160                 : ** If jumpIfNull is true, then set the low byte of the returned
     161                 : ** P1 value to tell the opcode to jump if either expression
     162                 : ** evaluates to NULL.
     163                 : */
     164              79 : static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
     165              79 :   char aff = sqlite3ExprAffinity(pExpr2);
     166              79 :   return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
     167                 : }
     168                 : 
     169                 : /*
     170                 : ** Return a pointer to the collation sequence that should be used by
     171                 : ** a binary comparison operator comparing pLeft and pRight.
     172                 : **
     173                 : ** If the left hand expression has a collating sequence type, then it is
     174                 : ** used. Otherwise the collation sequence for the right hand expression
     175                 : ** is used, or the default (BINARY) if neither expression has a collating
     176                 : ** type.
     177                 : */
     178              79 : static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
     179                 :   CollSeq *pColl;
     180                 :   assert( pLeft );
     181                 :   assert( pRight );
     182              79 :   if( pLeft->flags & EP_ExpCollate ){
     183                 :     assert( pLeft->pColl );
     184               0 :     pColl = pLeft->pColl;
     185              79 :   }else if( pRight->flags & EP_ExpCollate ){
     186                 :     assert( pRight->pColl );
     187               0 :     pColl = pRight->pColl;
     188                 :   }else{
     189              79 :     pColl = sqlite3ExprCollSeq(pParse, pLeft);
     190              79 :     if( !pColl ){
     191               0 :       pColl = sqlite3ExprCollSeq(pParse, pRight);
     192                 :     }
     193                 :   }
     194              79 :   return pColl;
     195                 : }
     196                 : 
     197                 : /*
     198                 : ** Generate code for a comparison operator.
     199                 : */
     200                 : static int codeCompare(
     201                 :   Parse *pParse,    /* The parsing (and code generating) context */
     202                 :   Expr *pLeft,      /* The left operand */
     203                 :   Expr *pRight,     /* The right operand */
     204                 :   int opcode,       /* The comparison opcode */
     205                 :   int dest,         /* Jump here if true.  */
     206                 :   int jumpIfNull    /* If true, jump if either operand is NULL */
     207              79 : ){
     208              79 :   int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
     209              79 :   CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
     210              79 :   return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
     211                 : }
     212                 : 
     213                 : /*
     214                 : ** Construct a new expression node and return a pointer to it.  Memory
     215                 : ** for this node is obtained from sqliteMalloc().  The calling function
     216                 : ** is responsible for making sure the node eventually gets freed.
     217                 : */
     218            1660 : Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
     219                 :   Expr *pNew;
     220            1660 :   pNew = sqliteMalloc( sizeof(Expr) );
     221            1660 :   if( pNew==0 ){
     222                 :     /* When malloc fails, delete pLeft and pRight. Expressions passed to 
     223                 :     ** this function must always be allocated with sqlite3Expr() for this 
     224                 :     ** reason. 
     225                 :     */
     226               0 :     sqlite3ExprDelete(pLeft);
     227               0 :     sqlite3ExprDelete(pRight);
     228               0 :     return 0;
     229                 :   }
     230            1660 :   pNew->op = op;
     231            1660 :   pNew->pLeft = pLeft;
     232            1660 :   pNew->pRight = pRight;
     233            1660 :   pNew->iAgg = -1;
     234            1660 :   if( pToken ){
     235                 :     assert( pToken->dyn==0 );
     236            1385 :     pNew->span = pNew->token = *pToken;
     237             275 :   }else if( pLeft ){
     238             188 :     if( pRight ){
     239             186 :       sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
     240             186 :       if( pRight->flags & EP_ExpCollate ){
     241               0 :         pNew->flags |= EP_ExpCollate;
     242               0 :         pNew->pColl = pRight->pColl;
     243                 :       }
     244                 :     }
     245             188 :     if( pLeft->flags & EP_ExpCollate ){
     246               0 :       pNew->flags |= EP_ExpCollate;
     247               0 :       pNew->pColl = pLeft->pColl;
     248                 :     }
     249                 :   }
     250            1660 :   return pNew;
     251                 : }
     252                 : 
     253                 : /*
     254                 : ** Works like sqlite3Expr() but frees its pLeft and pRight arguments
     255                 : ** if it fails due to a malloc problem.
     256                 : */
     257               0 : Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
     258               0 :   Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
     259               0 :   if( pNew==0 ){
     260               0 :     sqlite3ExprDelete(pLeft);
     261               0 :     sqlite3ExprDelete(pRight);
     262                 :   }
     263               0 :   return pNew;
     264                 : }
     265                 : 
     266                 : /*
     267                 : ** When doing a nested parse, you can include terms in an expression
     268                 : ** that look like this:   #0 #1 #2 ...  These terms refer to elements
     269                 : ** on the stack.  "#0" means the top of the stack.
     270                 : ** "#1" means the next down on the stack.  And so forth.
     271                 : **
     272                 : ** This routine is called by the parser to deal with on of those terms.
     273                 : ** It immediately generates code to store the value in a memory location.
     274                 : ** The returns an expression that will code to extract the value from
     275                 : ** that memory location as needed.
     276                 : */
     277             163 : Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
     278             163 :   Vdbe *v = pParse->pVdbe;
     279                 :   Expr *p;
     280                 :   int depth;
     281             163 :   if( pParse->nested==0 ){
     282               0 :     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
     283               0 :     return 0;
     284                 :   }
     285             163 :   if( v==0 ) return 0;
     286             163 :   p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
     287             163 :   if( p==0 ){
     288               0 :     return 0;  /* Malloc failed */
     289                 :   }
     290             163 :   depth = atoi((char*)&pToken->z[1]);
     291             163 :   p->iTable = pParse->nMem++;
     292             163 :   sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
     293             163 :   sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
     294             163 :   return p;
     295                 : }
     296                 : 
     297                 : /*
     298                 : ** Join two expressions using an AND operator.  If either expression is
     299                 : ** NULL, then just return the other expression.
     300                 : */
     301               5 : Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
     302               5 :   if( pLeft==0 ){
     303               3 :     return pRight;
     304               2 :   }else if( pRight==0 ){
     305               0 :     return pLeft;
     306                 :   }else{
     307               2 :     return sqlite3Expr(TK_AND, pLeft, pRight, 0);
     308                 :   }
     309                 : }
     310                 : 
     311                 : /*
     312                 : ** Set the Expr.span field of the given expression to span all
     313                 : ** text between the two given tokens.
     314                 : */
     315             205 : void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
     316                 :   assert( pRight!=0 );
     317                 :   assert( pLeft!=0 );
     318             205 :   if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
     319                 :     assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
     320             410 :     if( pLeft->dyn==0 && pRight->dyn==0 ){
     321             205 :       pExpr->span.z = pLeft->z;
     322             205 :       pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
     323                 :     }else{
     324               0 :       pExpr->span.z = 0;
     325                 :     }
     326                 :   }
     327             205 : }
     328                 : 
     329                 : /*
     330                 : ** Construct a new expression node for a function with multiple
     331                 : ** arguments.
     332                 : */
     333              15 : Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
     334                 :   Expr *pNew;
     335                 :   assert( pToken );
     336              15 :   pNew = sqliteMalloc( sizeof(Expr) );
     337              15 :   if( pNew==0 ){
     338               0 :     sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
     339               0 :     return 0;
     340                 :   }
     341              15 :   pNew->op = TK_FUNCTION;
     342              15 :   pNew->pList = pList;
     343                 :   assert( pToken->dyn==0 );
     344              15 :   pNew->token = *pToken;
     345              15 :   pNew->span = pNew->token;
     346              15 :   return pNew;
     347                 : }
     348                 : 
     349                 : /*
     350                 : ** Assign a variable number to an expression that encodes a wildcard
     351                 : ** in the original SQL statement.  
     352                 : **
     353                 : ** Wildcards consisting of a single "?" are assigned the next sequential
     354                 : ** variable number.
     355                 : **
     356                 : ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
     357                 : ** sure "nnn" is not too be to avoid a denial of service attack when
     358                 : ** the SQL statement comes from an external source.
     359                 : **
     360                 : ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
     361                 : ** as the previous instance of the same wildcard.  Or if this is the first
     362                 : ** instance of the wildcard, the next sequenial variable number is
     363                 : ** assigned.
     364                 : */
     365              50 : void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
     366                 :   Token *pToken;
     367              50 :   if( pExpr==0 ) return;
     368              50 :   pToken = &pExpr->token;
     369                 :   assert( pToken->n>=1 );
     370                 :   assert( pToken->z!=0 );
     371                 :   assert( pToken->z[0]!=0 );
     372              50 :   if( pToken->n==1 ){
     373                 :     /* Wildcard of the form "?".  Assign the next variable number */
     374              25 :     pExpr->iTable = ++pParse->nVar;
     375              25 :   }else if( pToken->z[0]=='?' ){
     376                 :     /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
     377                 :     ** use it as the variable number */
     378                 :     int i;
     379               0 :     pExpr->iTable = i = atoi((char*)&pToken->z[1]);
     380               0 :     if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
     381               0 :       sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
     382                 :           SQLITE_MAX_VARIABLE_NUMBER);
     383                 :     }
     384               0 :     if( i>pParse->nVar ){
     385               0 :       pParse->nVar = i;
     386                 :     }
     387                 :   }else{
     388                 :     /* Wildcards of the form ":aaa" or "$aaa".  Reuse the same variable
     389                 :     ** number as the prior appearance of the same name, or if the name
     390                 :     ** has never appeared before, reuse the same variable number
     391                 :     */
     392                 :     int i, n;
     393              25 :     n = pToken->n;
     394              38 :     for(i=0; i<pParse->nVarExpr; i++){
     395                 :       Expr *pE;
     396              13 :       if( (pE = pParse->apVarExpr[i])!=0
     397                 :           && pE->token.n==n
     398                 :           && memcmp(pE->token.z, pToken->z, n)==0 ){
     399               0 :         pExpr->iTable = pE->iTable;
     400               0 :         break;
     401                 :       }
     402                 :     }
     403              25 :     if( i>=pParse->nVarExpr ){
     404              25 :       pExpr->iTable = ++pParse->nVar;
     405              25 :       if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
     406              16 :         pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
     407              16 :         pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
     408                 :                        pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
     409                 :       }
     410              25 :       if( !sqlite3MallocFailed() ){
     411                 :         assert( pParse->apVarExpr!=0 );
     412              25 :         pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
     413                 :       }
     414                 :     }
     415                 :   } 
     416                 : }
     417                 : 
     418                 : /*
     419                 : ** Recursively delete an expression tree.
     420                 : */
     421            8622 : void sqlite3ExprDelete(Expr *p){
     422            8622 :   if( p==0 ) return;
     423            1690 :   if( p->span.dyn ) sqliteFree((char*)p->span.z);
     424            1690 :   if( p->token.dyn ) sqliteFree((char*)p->token.z);
     425            1690 :   sqlite3ExprDelete(p->pLeft);
     426            1690 :   sqlite3ExprDelete(p->pRight);
     427            1690 :   sqlite3ExprListDelete(p->pList);
     428            1690 :   sqlite3SelectDelete(p->pSelect);
     429            1690 :   sqliteFree(p);
     430                 : }
     431                 : 
     432                 : /*
     433                 : ** The Expr.token field might be a string literal that is quoted.
     434                 : ** If so, remove the quotation marks.
     435                 : */
     436             569 : void sqlite3DequoteExpr(Expr *p){
     437             569 :   if( ExprHasAnyProperty(p, EP_Dequoted) ){
     438               0 :     return;
     439                 :   }
     440             569 :   ExprSetProperty(p, EP_Dequoted);
     441             569 :   if( p->token.dyn==0 ){
     442             569 :     sqlite3TokenCopy(&p->token, &p->token);
     443                 :   }
     444             569 :   sqlite3Dequote((char*)p->token.z);
     445                 : }
     446                 : 
     447                 : 
     448                 : /*
     449                 : ** The following group of routines make deep copies of expressions,
     450                 : ** expression lists, ID lists, and select statements.  The copies can
     451                 : ** be deleted (by being passed to their respective ...Delete() routines)
     452                 : ** without effecting the originals.
     453                 : **
     454                 : ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
     455                 : ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
     456                 : ** by subsequent calls to sqlite*ListAppend() routines.
     457                 : **
     458                 : ** Any tables that the SrcList might point to are not duplicated.
     459                 : */
     460              35 : Expr *sqlite3ExprDup(Expr *p){
     461                 :   Expr *pNew;
     462              35 :   if( p==0 ) return 0;
     463              15 :   pNew = sqliteMallocRaw( sizeof(*p) );
     464              15 :   if( pNew==0 ) return 0;
     465              15 :   memcpy(pNew, p, sizeof(*pNew));
     466              15 :   if( p->token.z!=0 ){
     467               0 :     pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
     468               0 :     pNew->token.dyn = 1;
     469                 :   }else{
     470                 :     assert( pNew->token.z==0 );
     471                 :   }
     472              15 :   pNew->span.z = 0;
     473              15 :   pNew->pLeft = sqlite3ExprDup(p->pLeft);
     474              15 :   pNew->pRight = sqlite3ExprDup(p->pRight);
     475              15 :   pNew->pList = sqlite3ExprListDup(p->pList);
     476              15 :   pNew->pSelect = sqlite3SelectDup(p->pSelect);
     477              15 :   pNew->pTab = p->pTab;
     478              15 :   return pNew;
     479                 : }
     480             569 : void sqlite3TokenCopy(Token *pTo, Token *pFrom){
     481             569 :   if( pTo->dyn ) sqliteFree((char*)pTo->z);
     482             569 :   if( pFrom->z ){
     483             569 :     pTo->n = pFrom->n;
     484             569 :     pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
     485             569 :     pTo->dyn = 1;
     486                 :   }else{
     487               0 :     pTo->z = 0;
     488                 :   }
     489             569 : }
     490              15 : ExprList *sqlite3ExprListDup(ExprList *p){
     491                 :   ExprList *pNew;
     492                 :   struct ExprList_item *pItem, *pOldItem;
     493                 :   int i;
     494              15 :   if( p==0 ) return 0;
     495               0 :   pNew = sqliteMalloc( sizeof(*pNew) );
     496               0 :   if( pNew==0 ) return 0;
     497               0 :   pNew->nExpr = pNew->nAlloc = p->nExpr;
     498               0 :   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
     499               0 :   if( pItem==0 ){
     500               0 :     sqliteFree(pNew);
     501               0 :     return 0;
     502                 :   } 
     503               0 :   pOldItem = p->a;
     504               0 :   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
     505                 :     Expr *pNewExpr, *pOldExpr;
     506               0 :     pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
     507               0 :     if( pOldExpr->span.z!=0 && pNewExpr ){
     508                 :       /* Always make a copy of the span for top-level expressions in the
     509                 :       ** expression list.  The logic in SELECT processing that determines
     510                 :       ** the names of columns in the result set needs this information */
     511               0 :       sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
     512                 :     }
     513                 :     assert( pNewExpr==0 || pNewExpr->span.z!=0 
     514                 :             || pOldExpr->span.z==0
     515                 :             || sqlite3MallocFailed() );
     516               0 :     pItem->zName = sqliteStrDup(pOldItem->zName);
     517               0 :     pItem->sortOrder = pOldItem->sortOrder;
     518               0 :     pItem->isAgg = pOldItem->isAgg;
     519               0 :     pItem->done = 0;
     520                 :   }
     521               0 :   return pNew;
     522                 : }
     523                 : 
     524                 : /*
     525                 : ** If cursors, triggers, views and subqueries are all omitted from
     526                 : ** the build, then none of the following routines, except for 
     527                 : ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
     528                 : ** called with a NULL argument.
     529                 : */
     530                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
     531                 :  || !defined(SQLITE_OMIT_SUBQUERY)
     532               0 : SrcList *sqlite3SrcListDup(SrcList *p){
     533                 :   SrcList *pNew;
     534                 :   int i;
     535                 :   int nByte;
     536               0 :   if( p==0 ) return 0;
     537               0 :   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
     538               0 :   pNew = sqliteMallocRaw( nByte );
     539               0 :   if( pNew==0 ) return 0;
     540               0 :   pNew->nSrc = pNew->nAlloc = p->nSrc;
     541               0 :   for(i=0; i<p->nSrc; i++){
     542               0 :     struct SrcList_item *pNewItem = &pNew->a[i];
     543               0 :     struct SrcList_item *pOldItem = &p->a[i];
     544                 :     Table *pTab;
     545               0 :     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
     546               0 :     pNewItem->zName = sqliteStrDup(pOldItem->zName);
     547               0 :     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
     548               0 :     pNewItem->jointype = pOldItem->jointype;
     549               0 :     pNewItem->iCursor = pOldItem->iCursor;
     550               0 :     pNewItem->isPopulated = pOldItem->isPopulated;
     551               0 :     pTab = pNewItem->pTab = pOldItem->pTab;
     552               0 :     if( pTab ){
     553               0 :       pTab->nRef++;
     554                 :     }
     555               0 :     pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
     556               0 :     pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
     557               0 :     pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
     558               0 :     pNewItem->colUsed = pOldItem->colUsed;
     559                 :   }
     560               0 :   return pNew;
     561                 : }
     562               0 : IdList *sqlite3IdListDup(IdList *p){
     563                 :   IdList *pNew;
     564                 :   int i;
     565               0 :   if( p==0 ) return 0;
     566               0 :   pNew = sqliteMallocRaw( sizeof(*pNew) );
     567               0 :   if( pNew==0 ) return 0;
     568               0 :   pNew->nId = pNew->nAlloc = p->nId;
     569               0 :   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
     570               0 :   if( pNew->a==0 ){
     571               0 :     sqliteFree(pNew);
     572               0 :     return 0;
     573                 :   }
     574               0 :   for(i=0; i<p->nId; i++){
     575               0 :     struct IdList_item *pNewItem = &pNew->a[i];
     576               0 :     struct IdList_item *pOldItem = &p->a[i];
     577               0 :     pNewItem->zName = sqliteStrDup(pOldItem->zName);
     578               0 :     pNewItem->idx = pOldItem->idx;
     579                 :   }
     580               0 :   return pNew;
     581                 : }
     582              15 : Select *sqlite3SelectDup(Select *p){
     583                 :   Select *pNew;
     584              15 :   if( p==0 ) return 0;
     585               0 :   pNew = sqliteMallocRaw( sizeof(*p) );
     586               0 :   if( pNew==0 ) return 0;
     587               0 :   pNew->isDistinct = p->isDistinct;
     588               0 :   pNew->pEList = sqlite3ExprListDup(p->pEList);
     589               0 :   pNew->pSrc = sqlite3SrcListDup(p->pSrc);
     590               0 :   pNew->pWhere = sqlite3ExprDup(p->pWhere);
     591               0 :   pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
     592               0 :   pNew->pHaving = sqlite3ExprDup(p->pHaving);
     593               0 :   pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
     594               0 :   pNew->op = p->op;
     595               0 :   pNew->pPrior = sqlite3SelectDup(p->pPrior);
     596               0 :   pNew->pLimit = sqlite3ExprDup(p->pLimit);
     597               0 :   pNew->pOffset = sqlite3ExprDup(p->pOffset);
     598               0 :   pNew->iLimit = -1;
     599               0 :   pNew->iOffset = -1;
     600               0 :   pNew->isResolved = p->isResolved;
     601               0 :   pNew->isAgg = p->isAgg;
     602               0 :   pNew->usesEphm = 0;
     603               0 :   pNew->disallowOrderBy = 0;
     604               0 :   pNew->pRightmost = 0;
     605               0 :   pNew->addrOpenEphm[0] = -1;
     606               0 :   pNew->addrOpenEphm[1] = -1;
     607               0 :   pNew->addrOpenEphm[2] = -1;
     608               0 :   return pNew;
     609                 : }
     610                 : #else
     611                 : Select *sqlite3SelectDup(Select *p){
     612                 :   assert( p==0 );
     613                 :   return 0;
     614                 : }
     615                 : #endif
     616                 : 
     617                 : 
     618                 : /*
     619                 : ** Add a new element to the end of an expression list.  If pList is
     620                 : ** initially NULL, then create a new expression list.
     621                 : */
     622            1240 : ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
     623            1240 :   if( pList==0 ){
     624             490 :     pList = sqliteMalloc( sizeof(ExprList) );
     625             490 :     if( pList==0 ){
     626               0 :       goto no_mem;
     627                 :     }
     628                 :     assert( pList->nAlloc==0 );
     629                 :   }
     630            1240 :   if( pList->nAlloc<=pList->nExpr ){
     631                 :     struct ExprList_item *a;
     632             586 :     int n = pList->nAlloc*2 + 4;
     633             586 :     a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
     634             586 :     if( a==0 ){
     635               0 :       goto no_mem;
     636                 :     }
     637             586 :     pList->a = a;
     638             586 :     pList->nAlloc = n;
     639                 :   }
     640                 :   assert( pList->a!=0 );
     641            1240 :   if( pExpr || pName ){
     642            1240 :     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
     643            1240 :     memset(pItem, 0, sizeof(*pItem));
     644            1240 :     pItem->zName = sqlite3NameFromToken(pName);
     645            1240 :     pItem->pExpr = pExpr;
     646                 :   }
     647            1240 :   return pList;
     648                 : 
     649               0 : no_mem:     
     650                 :   /* Avoid leaking memory if malloc has failed. */
     651               0 :   sqlite3ExprDelete(pExpr);
     652               0 :   sqlite3ExprListDelete(pList);
     653               0 :   return 0;
     654                 : }
     655                 : 
     656                 : /*
     657                 : ** Delete an entire expression list.
     658                 : */
     659            2557 : void sqlite3ExprListDelete(ExprList *pList){
     660                 :   int i;
     661                 :   struct ExprList_item *pItem;
     662            2557 :   if( pList==0 ) return;
     663                 :   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
     664                 :   assert( pList->nExpr<=pList->nAlloc );
     665            1730 :   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
     666            1240 :     sqlite3ExprDelete(pItem->pExpr);
     667            1240 :     sqliteFree(pItem->zName);
     668                 :   }
     669             490 :   sqliteFree(pList->a);
     670             490 :   sqliteFree(pList);
     671                 : }
     672                 : 
     673                 : /*
     674                 : ** Walk an expression tree.  Call xFunc for each node visited.
     675                 : **
     676                 : ** The return value from xFunc determines whether the tree walk continues.
     677                 : ** 0 means continue walking the tree.  1 means do not walk children
     678                 : ** of the current node but continue with siblings.  2 means abandon
     679                 : ** the tree walk completely.
     680                 : **
     681                 : ** The return value from this routine is 1 to abandon the tree walk
     682                 : ** and 0 to continue.
     683                 : **
     684                 : ** NOTICE:  This routine does *not* descend into subqueries.
     685                 : */
     686                 : static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
     687            3774 : static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
     688                 :   int rc;
     689            3774 :   if( pExpr==0 ) return 0;
     690            1912 :   rc = (*xFunc)(pArg, pExpr);
     691            1912 :   if( rc==0 ){
     692            1245 :     if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
     693            1097 :     if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
     694            1092 :     if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
     695                 :   }
     696            1759 :   return rc>1;
     697                 : }
     698                 : 
     699                 : /*
     700                 : ** Call walkExprTree() for every expression in list p.
     701                 : */
     702            1092 : static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
     703                 :   int i;
     704                 :   struct ExprList_item *pItem;
     705            1092 :   if( !p ) return 0;
     706               2 :   for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
     707               1 :     if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
     708                 :   }
     709               1 :   return 0;
     710                 : }
     711                 : 
     712                 : /*
     713                 : ** Call walkExprTree() for every expression in Select p, not including
     714                 : ** expressions that are part of sub-selects in any FROM clause or the LIMIT
     715                 : ** or OFFSET expressions..
     716                 : */
     717               0 : static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
     718               0 :   walkExprList(p->pEList, xFunc, pArg);
     719               0 :   walkExprTree(p->pWhere, xFunc, pArg);
     720               0 :   walkExprList(p->pGroupBy, xFunc, pArg);
     721               0 :   walkExprTree(p->pHaving, xFunc, pArg);
     722               0 :   walkExprList(p->pOrderBy, xFunc, pArg);
     723               0 :   return 0;
     724                 : }
     725                 : 
     726                 : 
     727                 : /*
     728                 : ** This routine is designed as an xFunc for walkExprTree().
     729                 : **
     730                 : ** pArg is really a pointer to an integer.  If we can tell by looking
     731                 : ** at pExpr that the expression that contains pExpr is not a constant
     732                 : ** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
     733                 : ** If pExpr does does not disqualify the expression from being a constant
     734                 : ** then do nothing.
     735                 : **
     736                 : ** After walking the whole tree, if no nodes are found that disqualify
     737                 : ** the expression as constant, then we assume the whole expression
     738                 : ** is constant.  See sqlite3ExprIsConstant() for additional information.
     739                 : */
     740             298 : static int exprNodeIsConstant(void *pArg, Expr *pExpr){
     741             298 :   switch( pExpr->op ){
     742                 :     /* Consider functions to be constant if all their arguments are constant
     743                 :     ** and *pArg==2 */
     744                 :     case TK_FUNCTION:
     745               0 :       if( *((int*)pArg)==2 ) return 0;
     746                 :       /* Fall through */
     747                 :     case TK_ID:
     748                 :     case TK_COLUMN:
     749                 :     case TK_DOT:
     750                 :     case TK_AGG_FUNCTION:
     751                 :     case TK_AGG_COLUMN:
     752                 : #ifndef SQLITE_OMIT_SUBQUERY
     753                 :     case TK_SELECT:
     754                 :     case TK_EXISTS:
     755                 : #endif
     756             140 :       *((int*)pArg) = 0;
     757             140 :       return 2;
     758                 :     case TK_IN:
     759               0 :       if( pExpr->pSelect ){
     760               0 :         *((int*)pArg) = 0;
     761               0 :         return 2;
     762                 :       }
     763                 :     default:
     764             158 :       return 0;
     765                 :   }
     766                 : }
     767                 : 
     768                 : /*
     769                 : ** Walk an expression tree.  Return 1 if the expression is constant
     770                 : ** and 0 if it involves variables or function calls.
     771                 : **
     772                 : ** For the purposes of this function, a double-quoted string (ex: "abc")
     773                 : ** is considered a variable but a single-quoted string (ex: 'abc') is
     774                 : ** a constant.
     775                 : */
     776             140 : int sqlite3ExprIsConstant(Expr *p){
     777             140 :   int isConst = 1;
     778             140 :   walkExprTree(p, exprNodeIsConstant, &isConst);
     779             140 :   return isConst;
     780                 : }
     781                 : 
     782                 : /*
     783                 : ** Walk an expression tree.  Return 1 if the expression is constant
     784                 : ** or a function call with constant arguments.  Return and 0 if there
     785                 : ** are any variables.
     786                 : **
     787                 : ** For the purposes of this function, a double-quoted string (ex: "abc")
     788                 : ** is considered a variable but a single-quoted string (ex: 'abc') is
     789                 : ** a constant.
     790                 : */
     791               0 : int sqlite3ExprIsConstantOrFunction(Expr *p){
     792               0 :   int isConst = 2;
     793               0 :   walkExprTree(p, exprNodeIsConstant, &isConst);
     794               0 :   return isConst!=0;
     795                 : }
     796                 : 
     797                 : /*
     798                 : ** If the expression p codes a constant integer that is small enough
     799                 : ** to fit in a 32-bit integer, return 1 and put the value of the integer
     800                 : ** in *pValue.  If the expression is not an integer or if it is too big
     801                 : ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
     802                 : */
     803               4 : int sqlite3ExprIsInteger(Expr *p, int *pValue){
     804               4 :   switch( p->op ){
     805                 :     case TK_INTEGER: {
     806               0 :       if( sqlite3GetInt32((char*)p->token.z, pValue) ){
     807               0 :         return 1;
     808                 :       }
     809               0 :       break;
     810                 :     }
     811                 :     case TK_UPLUS: {
     812               0 :       return sqlite3ExprIsInteger(p->pLeft, pValue);
     813                 :     }
     814                 :     case TK_UMINUS: {
     815                 :       int v;
     816               0 :       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
     817               0 :         *pValue = -v;
     818               0 :         return 1;
     819                 :       }
     820                 :       break;
     821                 :     }
     822                 :     default: break;
     823                 :   }
     824               4 :   return 0;
     825                 : }
     826                 : 
     827                 : /*
     828                 : ** Return TRUE if the given string is a row-id column name.
     829                 : */
     830              58 : int sqlite3IsRowid(const char *z){
     831              58 :   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
     832              58 :   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
     833               1 :   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
     834               1 :   return 0;
     835                 : }
     836                 : 
     837                 : /*
     838                 : ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
     839                 : ** that name in the set of source tables in pSrcList and make the pExpr 
     840                 : ** expression node refer back to that source column.  The following changes
     841                 : ** are made to pExpr:
     842                 : **
     843                 : **    pExpr->iDb           Set the index in db->aDb[] of the database holding
     844                 : **                         the table.
     845                 : **    pExpr->iTable        Set to the cursor number for the table obtained
     846                 : **                         from pSrcList.
     847                 : **    pExpr->iColumn       Set to the column number within the table.
     848                 : **    pExpr->op            Set to TK_COLUMN.
     849                 : **    pExpr->pLeft         Any expression this points to is deleted
     850                 : **    pExpr->pRight        Any expression this points to is deleted.
     851                 : **
     852                 : ** The pDbToken is the name of the database (the "X").  This value may be
     853                 : ** NULL meaning that name is of the form Y.Z or Z.  Any available database
     854                 : ** can be used.  The pTableToken is the name of the table (the "Y").  This
     855                 : ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
     856                 : ** means that the form of the name is Z and that columns from any table
     857                 : ** can be used.
     858                 : **
     859                 : ** If the name cannot be resolved unambiguously, leave an error message
     860                 : ** in pParse and return non-zero.  Return zero on success.
     861                 : */
     862                 : static int lookupName(
     863                 :   Parse *pParse,       /* The parsing context */
     864                 :   Token *pDbToken,     /* Name of the database containing table, or NULL */
     865                 :   Token *pTableToken,  /* Name of table containing column, or NULL */
     866                 :   Token *pColumnToken, /* Name of the column. */
     867                 :   NameContext *pNC,    /* The name context used to resolve the name */
     868                 :   Expr *pExpr          /* Make this EXPR node point to the selected column */
     869             494 : ){
     870             494 :   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
     871             494 :   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
     872             494 :   char *zCol = 0;      /* Name of the column.  The "Z" */
     873                 :   int i, j;            /* Loop counters */
     874             494 :   int cnt = 0;         /* Number of matching column names */
     875             494 :   int cntTab = 0;      /* Number of matching table names */
     876             494 :   sqlite3 *db = pParse->db;  /* The database */
     877                 :   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
     878             494 :   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
     879             494 :   NameContext *pTopNC = pNC;        /* First namecontext in the list */
     880                 : 
     881                 :   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
     882             494 :   zDb = sqlite3NameFromToken(pDbToken);
     883             494 :   zTab = sqlite3NameFromToken(pTableToken);
     884             494 :   zCol = sqlite3NameFromToken(pColumnToken);
     885             494 :   if( sqlite3MallocFailed() ){
     886               0 :     goto lookupname_end;
     887                 :   }
     888                 : 
     889             494 :   pExpr->iTable = -1;
     890            1482 :   while( pNC && cnt==0 ){
     891                 :     ExprList *pEList;
     892             494 :     SrcList *pSrcList = pNC->pSrcList;
     893                 : 
     894             494 :     if( pSrcList ){
     895             993 :       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
     896                 :         Table *pTab;
     897                 :         int iDb;
     898                 :         Column *pCol;
     899                 :   
     900             509 :         pTab = pItem->pTab;
     901                 :         assert( pTab!=0 );
     902             509 :         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
     903                 :         assert( pTab->nCol>0 );
     904             509 :         if( zTab ){
     905              51 :           if( pItem->zAlias ){
     906               0 :             char *zTabName = pItem->zAlias;
     907               0 :             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
     908                 :           }else{
     909              51 :             char *zTabName = pTab->zName;
     910              51 :             if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
     911              26 :             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
     912               0 :               continue;
     913                 :             }
     914                 :           }
     915                 :         }
     916             484 :         if( 0==(cntTab++) ){
     917             484 :           pExpr->iTable = pItem->iCursor;
     918             484 :           pExpr->pSchema = pTab->pSchema;
     919             484 :           pMatch = pItem;
     920                 :         }
     921            1524 :         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
     922            1466 :           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
     923             426 :             const char *zColl = pTab->aCol[j].zColl;
     924                 :             IdList *pUsing;
     925             426 :             cnt++;
     926             426 :             pExpr->iTable = pItem->iCursor;
     927             426 :             pMatch = pItem;
     928             426 :             pExpr->pSchema = pTab->pSchema;
     929                 :             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
     930             426 :             pExpr->iColumn = j==pTab->iPKey ? -1 : j;
     931             426 :             pExpr->affinity = pTab->aCol[j].affinity;
     932             426 :             if( (pExpr->flags & EP_ExpCollate)==0 ){
     933             426 :               pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
     934                 :             }
     935             426 :             if( i<pSrcList->nSrc-1 ){
     936              12 :               if( pItem[1].jointype & JT_NATURAL ){
     937                 :                 /* If this match occurred in the left table of a natural join,
     938                 :                 ** then skip the right table to avoid a duplicate match */
     939               0 :                 pItem++;
     940               0 :                 i++;
     941              12 :               }else if( (pUsing = pItem[1].pUsing)!=0 ){
     942                 :                 /* If this match occurs on a column that is in the USING clause
     943                 :                 ** of a join, skip the search of the right table of the join
     944                 :                 ** to avoid a duplicate match there. */
     945                 :                 int k;
     946               0 :                 for(k=0; k<pUsing->nId; k++){
     947               0 :                   if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
     948               0 :                     pItem++;
     949               0 :                     i++;
     950               0 :                     break;
     951                 :                   }
     952                 :                 }
     953                 :               }
     954                 :             }
     955             426 :             break;
     956                 :           }
     957                 :         }
     958                 :       }
     959                 :     }
     960                 : 
     961                 : #ifndef SQLITE_OMIT_TRIGGER
     962                 :     /* If we have not already resolved the name, then maybe 
     963                 :     ** it is a new.* or old.* trigger argument reference
     964                 :     */
     965             494 :     if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
     966               0 :       TriggerStack *pTriggerStack = pParse->trigStack;
     967               0 :       Table *pTab = 0;
     968               0 :       if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
     969               0 :         pExpr->iTable = pTriggerStack->newIdx;
     970                 :         assert( pTriggerStack->pTab );
     971               0 :         pTab = pTriggerStack->pTab;
     972               0 :       }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
     973               0 :         pExpr->iTable = pTriggerStack->oldIdx;
     974                 :         assert( pTriggerStack->pTab );
     975               0 :         pTab = pTriggerStack->pTab;
     976                 :       }
     977                 : 
     978               0 :       if( pTab ){ 
     979                 :         int iCol;
     980               0 :         Column *pCol = pTab->aCol;
     981                 : 
     982               0 :         pExpr->pSchema = pTab->pSchema;
     983               0 :         cntTab++;
     984               0 :         for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
     985               0 :           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
     986               0 :             const char *zColl = pTab->aCol[iCol].zColl;
     987               0 :             cnt++;
     988               0 :             pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
     989               0 :             pExpr->affinity = pTab->aCol[iCol].affinity;
     990               0 :             if( (pExpr->flags & EP_ExpCollate)==0 ){
     991               0 :               pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
     992                 :             }
     993               0 :             pExpr->pTab = pTab;
     994               0 :             break;
     995                 :           }
     996                 :         }
     997                 :       }
     998                 :     }
     999                 : #endif /* !defined(SQLITE_OMIT_TRIGGER) */
    1000                 : 
    1001                 :     /*
    1002                 :     ** Perhaps the name is a reference to the ROWID
    1003                 :     */
    1004             494 :     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
    1005              57 :       cnt = 1;
    1006              57 :       pExpr->iColumn = -1;
    1007              57 :       pExpr->affinity = SQLITE_AFF_INTEGER;
    1008                 :     }
    1009                 : 
    1010                 :     /*
    1011                 :     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
    1012                 :     ** might refer to an result-set alias.  This happens, for example, when
    1013                 :     ** we are resolving names in the WHERE clause of the following command:
    1014                 :     **
    1015                 :     **     SELECT a+b AS x FROM table WHERE x<10;
    1016                 :     **
    1017                 :     ** In cases like this, replace pExpr with a copy of the expression that
    1018                 :     ** forms the result set entry ("a+b" in the example) and return immediately.
    1019                 :     ** Note that the expression in the result set should have already been
    1020                 :     ** resolved by the time the WHERE clause is resolved.
    1021                 :     */
    1022             494 :     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
    1023               3 :       for(j=0; j<pEList->nExpr; j++){
    1024               2 :         char *zAs = pEList->a[j].zName;
    1025               2 :         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
    1026                 :           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
    1027               0 :           pExpr->op = TK_AS;
    1028               0 :           pExpr->iColumn = j;
    1029               0 :           pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
    1030               0 :           cnt = 1;
    1031                 :           assert( zTab==0 && zDb==0 );
    1032               0 :           goto lookupname_end_2;
    1033                 :         }
    1034                 :       } 
    1035                 :     }
    1036                 : 
    1037                 :     /* Advance to the next name context.  The loop will exit when either
    1038                 :     ** we have a match (cnt>0) or when we run out of name contexts.
    1039                 :     */
    1040             494 :     if( cnt==0 ){
    1041              11 :       pNC = pNC->pNext;
    1042                 :     }
    1043                 :   }
    1044                 : 
    1045                 :   /*
    1046                 :   ** If X and Y are NULL (in other words if only the column name Z is
    1047                 :   ** supplied) and the value of Z is enclosed in double-quotes, then
    1048                 :   ** Z is a string literal if it doesn't match any column names.  In that
    1049                 :   ** case, we need to return right away and not make any changes to
    1050                 :   ** pExpr.
    1051                 :   **
    1052                 :   ** Because no reference was made to outer contexts, the pNC->nRef
    1053                 :   ** fields are not changed in any context.
    1054                 :   */
    1055             494 :   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
    1056              11 :     sqliteFree(zCol);
    1057              11 :     return 0;
    1058                 :   }
    1059                 : 
    1060                 :   /*
    1061                 :   ** cnt==0 means there was not match.  cnt>1 means there were two or
    1062                 :   ** more matches.  Either way, we have an error.
    1063                 :   */
    1064             483 :   if( cnt!=1 ){
    1065               0 :     char *z = 0;
    1066                 :     char *zErr;
    1067               0 :     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
    1068               0 :     if( zDb ){
    1069               0 :       sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
    1070               0 :     }else if( zTab ){
    1071               0 :       sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
    1072                 :     }else{
    1073               0 :       z = sqliteStrDup(zCol);
    1074                 :     }
    1075               0 :     sqlite3ErrorMsg(pParse, zErr, z);
    1076               0 :     sqliteFree(z);
    1077               0 :     pTopNC->nErr++;
    1078                 :   }
    1079                 : 
    1080                 :   /* If a column from a table in pSrcList is referenced, then record
    1081                 :   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
    1082                 :   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
    1083                 :   ** column number is greater than the number of bits in the bitmask
    1084                 :   ** then set the high-order bit of the bitmask.
    1085                 :   */
    1086             483 :   if( pExpr->iColumn>=0 && pMatch!=0 ){
    1087             421 :     int n = pExpr->iColumn;
    1088             421 :     if( n>=sizeof(Bitmask)*8 ){
    1089               0 :       n = sizeof(Bitmask)*8-1;
    1090                 :     }
    1091                 :     assert( pMatch->iCursor==pExpr->iTable );
    1092             421 :     pMatch->colUsed |= ((Bitmask)1)<<n;
    1093                 :   }
    1094                 : 
    1095             483 : lookupname_end:
    1096                 :   /* Clean up and return
    1097                 :   */
    1098             483 :   sqliteFree(zDb);
    1099             483 :   sqliteFree(zTab);
    1100             483 :   sqlite3ExprDelete(pExpr->pLeft);
    1101             483 :   pExpr->pLeft = 0;
    1102             483 :   sqlite3ExprDelete(pExpr->pRight);
    1103             483 :   pExpr->pRight = 0;
    1104             483 :   pExpr->op = TK_COLUMN;
    1105             483 : lookupname_end_2:
    1106             483 :   sqliteFree(zCol);
    1107             483 :   if( cnt==1 ){
    1108                 :     assert( pNC!=0 );
    1109             483 :     sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
    1110             483 :     if( pMatch && !pMatch->pSelect ){
    1111             483 :       pExpr->pTab = pMatch->pTab;
    1112                 :     }
    1113                 :     /* Increment the nRef value on all name contexts from TopNC up to
    1114                 :     ** the point where the name matched. */
    1115                 :     for(;;){
    1116                 :       assert( pTopNC!=0 );
    1117             483 :       pTopNC->nRef++;
    1118             483 :       if( pTopNC==pNC ) break;
    1119               0 :       pTopNC = pTopNC->pNext;
    1120               0 :     }
    1121             483 :     return 0;
    1122                 :   } else {
    1123               0 :     return 1;
    1124                 :   }
    1125                 : }
    1126                 : 
    1127                 : /*
    1128                 : ** This routine is designed as an xFunc for walkExprTree().
    1129                 : **
    1130                 : ** Resolve symbolic names into TK_COLUMN operators for the current
    1131                 : ** node in the expression tree.  Return 0 to continue the search down
    1132                 : ** the tree or 2 to abort the tree walk.
    1133                 : **
    1134                 : ** This routine also does error checking and name resolution for
    1135                 : ** function names.  The operator for aggregate functions is changed
    1136                 : ** to TK_AGG_FUNCTION.
    1137                 : */
    1138            1596 : static int nameResolverStep(void *pArg, Expr *pExpr){
    1139            1596 :   NameContext *pNC = (NameContext*)pArg;
    1140                 :   Parse *pParse;
    1141                 : 
    1142            1596 :   if( pExpr==0 ) return 1;
    1143                 :   assert( pNC!=0 );
    1144            1596 :   pParse = pNC->pParse;
    1145                 : 
    1146            1596 :   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
    1147            1595 :   ExprSetProperty(pExpr, EP_Resolved);
    1148                 : #ifndef NDEBUG
    1149                 :   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
    1150                 :     SrcList *pSrcList = pNC->pSrcList;
    1151                 :     int i;
    1152                 :     for(i=0; i<pNC->pSrcList->nSrc; i++){
    1153                 :       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
    1154                 :     }
    1155                 :   }
    1156                 : #endif
    1157            1595 :   switch( pExpr->op ){
    1158                 :     /* Double-quoted strings (ex: "abc") are used as identifiers if
    1159                 :     ** possible.  Otherwise they remain as strings.  Single-quoted
    1160                 :     ** strings (ex: 'abc') are always string literals.
    1161                 :     */
    1162                 :     case TK_STRING: {
    1163             569 :       if( pExpr->token.z[0]=='\'' ) break;
    1164                 :       /* Fall thru into the TK_ID case if this is a double-quoted string */
    1165                 :     }
    1166                 :     /* A lone identifier is the name of a column.
    1167                 :     */
    1168                 :     case TK_ID: {
    1169             468 :       lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
    1170             468 :       return 1;
    1171                 :     }
    1172                 :   
    1173                 :     /* A table name and column name:     ID.ID
    1174                 :     ** Or a database, table and column:  ID.ID.ID
    1175                 :     */
    1176                 :     case TK_DOT: {
    1177                 :       Token *pColumn;
    1178                 :       Token *pTable;
    1179                 :       Token *pDb;
    1180                 :       Expr *pRight;
    1181                 : 
    1182                 :       /* if( pSrcList==0 ) break; */
    1183              26 :       pRight = pExpr->pRight;
    1184              26 :       if( pRight->op==TK_ID ){
    1185              26 :         pDb = 0;
    1186              26 :         pTable = &pExpr->pLeft->token;
    1187              26 :         pColumn = &pRight->token;
    1188                 :       }else{
    1189                 :         assert( pRight->op==TK_DOT );
    1190               0 :         pDb = &pExpr->pLeft->token;
    1191               0 :         pTable = &pRight->pLeft->token;
    1192               0 :         pColumn = &pRight->pRight->token;
    1193                 :       }
    1194              26 :       lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
    1195              26 :       return 1;
    1196                 :     }
    1197                 : 
    1198                 :     /* Resolve function names
    1199                 :     */
    1200                 :     case TK_CONST_FUNC:
    1201                 :     case TK_FUNCTION: {
    1202              15 :       ExprList *pList = pExpr->pList;    /* The argument list */
    1203              15 :       int n = pList ? pList->nExpr : 0;  /* Number of arguments */
    1204              15 :       int no_such_func = 0;       /* True if no such function exists */
    1205              15 :       int wrong_num_args = 0;     /* True if wrong number of arguments */
    1206              15 :       int is_agg = 0;             /* True if is an aggregate function */
    1207                 :       int i;
    1208                 :       int auth;                   /* Authorization to use the function */
    1209                 :       int nId;                    /* Number of characters in function name */
    1210                 :       const char *zId;            /* The function name. */
    1211                 :       FuncDef *pDef;              /* Information about the function */
    1212              15 :       int enc = ENC(pParse->db);  /* The database encoding */
    1213                 : 
    1214              15 :       zId = (char*)pExpr->token.z;
    1215              15 :       nId = pExpr->token.n;
    1216              15 :       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
    1217              15 :       if( pDef==0 ){
    1218               0 :         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
    1219               0 :         if( pDef==0 ){
    1220               0 :           no_such_func = 1;
    1221                 :         }else{
    1222               0 :           wrong_num_args = 1;
    1223                 :         }
    1224                 :       }else{
    1225              15 :         is_agg = pDef->xFunc==0;
    1226                 :       }
    1227                 : #ifndef SQLITE_OMIT_AUTHORIZATION
    1228              15 :       if( pDef ){
    1229              15 :         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
    1230              15 :         if( auth!=SQLITE_OK ){
    1231               0 :           if( auth==SQLITE_DENY ){
    1232               0 :             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
    1233                 :                                     pDef->zName);
    1234               0 :             pNC->nErr++;
    1235                 :           }
    1236               0 :           pExpr->op = TK_NULL;
    1237               0 :           return 1;
    1238                 :         }
    1239                 :       }
    1240                 : #endif
    1241              15 :       if( is_agg && !pNC->allowAgg ){
    1242               0 :         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
    1243               0 :         pNC->nErr++;
    1244               0 :         is_agg = 0;
    1245              15 :       }else if( no_such_func ){
    1246               0 :         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
    1247               0 :         pNC->nErr++;
    1248              15 :       }else if( wrong_num_args ){
    1249               0 :         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
    1250                 :              nId, zId);
    1251               0 :         pNC->nErr++;
    1252                 :       }
    1253              15 :       if( is_agg ){
    1254              14 :         pExpr->op = TK_AGG_FUNCTION;
    1255              14 :         pNC->hasAgg = 1;
    1256                 :       }
    1257              15 :       if( is_agg ) pNC->allowAgg = 0;
    1258              20 :       for(i=0; pNC->nErr==0 && i<n; i++){
    1259               5 :         walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
    1260                 :       }
    1261              15 :       if( is_agg ) pNC->allowAgg = 1;
    1262                 :       /* FIX ME:  Compute pExpr->affinity based on the expected return
    1263                 :       ** type of the function 
    1264                 :       */
    1265              15 :       return is_agg;
    1266                 :     }
    1267                 : #ifndef SQLITE_OMIT_SUBQUERY
    1268                 :     case TK_SELECT:
    1269                 :     case TK_EXISTS:
    1270                 : #endif
    1271                 :     case TK_IN: {
    1272               0 :       if( pExpr->pSelect ){
    1273               0 :         int nRef = pNC->nRef;
    1274                 : #ifndef SQLITE_OMIT_CHECK
    1275               0 :         if( pNC->isCheck ){
    1276               0 :           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
    1277                 :         }
    1278                 : #endif
    1279               0 :         sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
    1280                 :         assert( pNC->nRef>=nRef );
    1281               0 :         if( nRef!=pNC->nRef ){
    1282               0 :           ExprSetProperty(pExpr, EP_VarSelect);
    1283                 :         }
    1284                 :       }
    1285               0 :       break;
    1286                 :     }
    1287                 : #ifndef SQLITE_OMIT_CHECK
    1288                 :     case TK_VARIABLE: {
    1289              50 :       if( pNC->isCheck ){
    1290               0 :         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
    1291                 :       }
    1292                 :       break;
    1293                 :     }
    1294                 : #endif
    1295                 :   }
    1296            1086 :   return 0;
    1297                 : }
    1298                 : 
    1299                 : /*
    1300                 : ** This routine walks an expression tree and resolves references to
    1301                 : ** table columns.  Nodes of the form ID.ID or ID resolve into an
    1302                 : ** index to the table in the table list and a column offset.  The 
    1303                 : ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
    1304                 : ** value is changed to the index of the referenced table in pTabList
    1305                 : ** plus the "base" value.  The base value will ultimately become the
    1306                 : ** VDBE cursor number for a cursor that is pointing into the referenced
    1307                 : ** table.  The Expr.iColumn value is changed to the index of the column 
    1308                 : ** of the referenced table.  The Expr.iColumn value for the special
    1309                 : ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
    1310                 : ** alias for ROWID.
    1311                 : **
    1312                 : ** Also resolve function names and check the functions for proper
    1313                 : ** usage.  Make sure all function names are recognized and all functions
    1314                 : ** have the correct number of arguments.  Leave an error message
    1315                 : ** in pParse->zErrMsg if anything is amiss.  Return the number of errors.
    1316                 : **
    1317                 : ** If the expression contains aggregate functions then set the EP_Agg
    1318                 : ** property on the expression.
    1319                 : */
    1320                 : int sqlite3ExprResolveNames(
    1321                 :   NameContext *pNC,       /* Namespace to resolve expressions in. */
    1322                 :   Expr *pExpr             /* The expression to be analyzed. */
    1323            1810 : ){
    1324                 :   int savedHasAgg;
    1325            1810 :   if( pExpr==0 ) return 0;
    1326            1268 :   savedHasAgg = pNC->hasAgg;
    1327            1268 :   pNC->hasAgg = 0;
    1328            1268 :   walkExprTree(pExpr, nameResolverStep, pNC);
    1329            1268 :   if( pNC->nErr>0 ){
    1330               0 :     ExprSetProperty(pExpr, EP_Error);
    1331                 :   }
    1332            1268 :   if( pNC->hasAgg ){
    1333              14 :     ExprSetProperty(pExpr, EP_Agg);
    1334            1254 :   }else if( savedHasAgg ){
    1335               1 :     pNC->hasAgg = 1;
    1336                 :   }
    1337            1268 :   return ExprHasProperty(pExpr, EP_Error);
    1338                 : }
    1339                 : 
    1340                 : /*
    1341                 : ** A pointer instance of this structure is used to pass information
    1342                 : ** through walkExprTree into codeSubqueryStep().
    1343                 : */
    1344                 : typedef struct QueryCoder QueryCoder;
    1345                 : struct QueryCoder {
    1346                 :   Parse *pParse;       /* The parsing context */
    1347                 :   NameContext *pNC;    /* Namespace of first enclosing query */
    1348                 : };
    1349                 : 
    1350                 : 
    1351                 : /*
    1352                 : ** Generate code for scalar subqueries used as an expression
    1353                 : ** and IN operators.  Examples:
    1354                 : **
    1355                 : **     (SELECT a FROM b)          -- subquery
    1356                 : **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
    1357                 : **     x IN (4,5,11)              -- IN operator with list on right-hand side
    1358                 : **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
    1359                 : **
    1360                 : ** The pExpr parameter describes the expression that contains the IN
    1361                 : ** operator or subquery.
    1362                 : */
    1363                 : #ifndef SQLITE_OMIT_SUBQUERY
    1364               0 : void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
    1365               0 :   int testAddr = 0;                       /* One-time test address */
    1366               0 :   Vdbe *v = sqlite3GetVdbe(pParse);
    1367               0 :   if( v==0 ) return;
    1368                 : 
    1369                 :   /* This code must be run in its entirety every time it is encountered
    1370                 :   ** if any of the following is true:
    1371                 :   **
    1372                 :   **    *  The right-hand side is a correlated subquery
    1373                 :   **    *  The right-hand side is an expression list containing variables
    1374                 :   **    *  We are inside a trigger
    1375                 :   **
    1376                 :   ** If all of the above are false, then we can run this code just once
    1377                 :   ** save the results, and reuse the same result on subsequent invocations.
    1378                 :   */
    1379               0 :   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
    1380               0 :     int mem = pParse->nMem++;
    1381               0 :     sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
    1382               0 :     testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
    1383                 :     assert( testAddr>0 || sqlite3MallocFailed() );
    1384               0 :     sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
    1385                 :   }
    1386                 : 
    1387               0 :   switch( pExpr->op ){
    1388                 :     case TK_IN: {
    1389                 :       char affinity;
    1390                 :       KeyInfo keyInfo;
    1391                 :       int addr;        /* Address of OP_OpenEphemeral instruction */
    1392                 : 
    1393               0 :       affinity = sqlite3ExprAffinity(pExpr->pLeft);
    1394                 : 
    1395                 :       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
    1396                 :       ** expression it is handled the same way. A virtual table is 
    1397                 :       ** filled with single-field index keys representing the results
    1398                 :       ** from the SELECT or the <exprlist>.
    1399                 :       **
    1400                 :       ** If the 'x' expression is a column value, or the SELECT...
    1401                 :       ** statement returns a column value, then the affinity of that
    1402                 :       ** column is used to build the index keys. If both 'x' and the
    1403                 :       ** SELECT... statement are columns, then numeric affinity is used
    1404                 :       ** if either column has NUMERIC or INTEGER affinity. If neither
    1405                 :       ** 'x' nor the SELECT... statement are columns, then numeric affinity
    1406                 :       ** is used.
    1407                 :       */
    1408               0 :       pExpr->iTable = pParse->nTab++;
    1409               0 :       addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
    1410               0 :       memset(&keyInfo, 0, sizeof(keyInfo));
    1411               0 :       keyInfo.nField = 1;
    1412               0 :       sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
    1413                 : 
    1414               0 :       if( pExpr->pSelect ){
    1415                 :         /* Case 1:     expr IN (SELECT ...)
    1416                 :         **
    1417                 :         ** Generate code to write the results of the select into the temporary
    1418                 :         ** table allocated and opened above.
    1419                 :         */
    1420               0 :         int iParm = pExpr->iTable +  (((int)affinity)<<16);
    1421                 :         ExprList *pEList;
    1422                 :         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
    1423               0 :         if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
    1424               0 :           return;
    1425                 :         }
    1426               0 :         pEList = pExpr->pSelect->pEList;
    1427               0 :         if( pEList && pEList->nExpr>0 ){ 
    1428               0 :           keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
    1429                 :               pEList->a[0].pExpr);
    1430                 :         }
    1431               0 :       }else if( pExpr->pList ){
    1432                 :         /* Case 2:     expr IN (exprlist)
    1433                 :         **
    1434                 :         ** For each expression, build an index key from the evaluation and
    1435                 :         ** store it in the temporary table. If <expr> is a column, then use
    1436                 :         ** that columns affinity when building index keys. If <expr> is not
    1437                 :         ** a column, use numeric affinity.
    1438                 :         */
    1439                 :         int i;
    1440               0 :         ExprList *pList = pExpr->pList;
    1441                 :         struct ExprList_item *pItem;
    1442                 : 
    1443               0 :         if( !affinity ){
    1444               0 :           affinity = SQLITE_AFF_NONE;
    1445                 :         }
    1446               0 :         keyInfo.aColl[0] = pExpr->pLeft->pColl;
    1447                 : 
    1448                 :         /* Loop through each expression in <exprlist>. */
    1449               0 :         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
    1450               0 :           Expr *pE2 = pItem->pExpr;
    1451                 : 
    1452                 :           /* If the expression is not constant then we will need to
    1453                 :           ** disable the test that was generated above that makes sure
    1454                 :           ** this code only executes once.  Because for a non-constant
    1455                 :           ** expression we need to rerun this code each time.
    1456                 :           */
    1457               0 :           if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
    1458               0 :             sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
    1459               0 :             testAddr = 0;
    1460                 :           }
    1461                 : 
    1462                 :           /* Evaluate the expression and insert it into the temp table */
    1463               0 :           sqlite3ExprCode(pParse, pE2);
    1464               0 :           sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
    1465               0 :           sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
    1466                 :         }
    1467                 :       }
    1468               0 :       sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
    1469               0 :       break;
    1470                 :     }
    1471                 : 
    1472                 :     case TK_EXISTS:
    1473                 :     case TK_SELECT: {
    1474                 :       /* This has to be a scalar SELECT.  Generate code to put the
    1475                 :       ** value of this select in a memory cell and record the number
    1476                 :       ** of the memory cell in iColumn.
    1477                 :       */
    1478                 :       static const Token one = { (u8*)"1", 0, 1 };
    1479                 :       Select *pSel;
    1480                 :       int iMem;
    1481                 :       int sop;
    1482                 : 
    1483               0 :       pExpr->iColumn = iMem = pParse->nMem++;
    1484               0 :       pSel = pExpr->pSelect;
    1485               0 :       if( pExpr->op==TK_SELECT ){
    1486               0 :         sop = SRT_Mem;
    1487               0 :         sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
    1488                 :         VdbeComment((v, "# Init subquery result"));
    1489                 :       }else{
    1490               0 :         sop = SRT_Exists;
    1491               0 :         sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
    1492                 :         VdbeComment((v, "# Init EXISTS result"));
    1493                 :       }
    1494               0 :       sqlite3ExprDelete(pSel->pLimit);
    1495               0 :       pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
    1496               0 :       if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
    1497               0 :         return;
    1498                 :       }
    1499                 :       break;
    1500                 :     }
    1501                 :   }
    1502                 : 
    1503               0 :   if( testAddr ){
    1504               0 :     sqlite3VdbeJumpHere(v, testAddr);
    1505                 :   }
    1506               0 :   return;
    1507                 : }
    1508                 : #endif /* SQLITE_OMIT_SUBQUERY */
    1509                 : 
    1510                 : /*
    1511                 : ** Generate an instruction that will put the integer describe by
    1512                 : ** text z[0..n-1] on the stack.
    1513                 : */
    1514             106 : static void codeInteger(Vdbe *v, const char *z, int n){
    1515                 :   int i;
    1516             106 :   if( sqlite3GetInt32(z, &i) ){
    1517             106 :     sqlite3VdbeAddOp(v, OP_Integer, i, 0);
    1518               0 :   }else if( sqlite3FitsIn64Bits(z) ){
    1519               0 :     sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
    1520                 :   }else{
    1521               0 :     sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
    1522                 :   }
    1523             106 : }
    1524                 : 
    1525                 : 
    1526                 : /*
    1527                 : ** Generate code that will extract the iColumn-th column from
    1528                 : ** table pTab and push that column value on the stack.  There
    1529                 : ** is an open cursor to pTab in iTable.  If iColumn<0 then
    1530                 : ** code is generated that extracts the rowid.
    1531                 : */
    1532             412 : void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
    1533             412 :   if( iColumn<0 ){
    1534               4 :     int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
    1535               4 :     sqlite3VdbeAddOp(v, op, iTable, 0);
    1536             408 :   }else if( pTab==0 ){
    1537               0 :     sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
    1538                 :   }else{
    1539             408 :     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
    1540             408 :     sqlite3VdbeAddOp(v, op, iTable, iColumn);
    1541             408 :     sqlite3ColumnDefault(v, pTab, iColumn);
    1542                 : #ifndef SQLITE_OMIT_FLOATING_POINT
    1543             408 :     if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
    1544               0 :       sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
    1545                 :     }
    1546                 : #endif
    1547                 :   }
    1548             412 : }
    1549                 : 
    1550                 : /*
    1551                 : ** Generate code into the current Vdbe to evaluate the given
    1552                 : ** expression and leave the result on the top of stack.
    1553                 : **
    1554                 : ** This code depends on the fact that certain token values (ex: TK_EQ)
    1555                 : ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
    1556                 : ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
    1557                 : ** the make process cause these values to align.  Assert()s in the code
    1558                 : ** below verify that the numbers are aligned correctly.
    1559                 : */
    1560            1362 : void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
    1561            1362 :   Vdbe *v = pParse->pVdbe;
    1562                 :   int op;
    1563            1362 :   int stackChng = 1;    /* Amount of change to stack depth */
    1564                 : 
    1565            1362 :   if( v==0 ) return;
    1566            1362 :   if( pExpr==0 ){
    1567               0 :     sqlite3VdbeAddOp(v, OP_Null, 0, 0);
    1568               0 :     return;
    1569                 :   }
    1570            1362 :   op = pExpr->op;
    1571            1362 :   switch( op ){
    1572                 :     case TK_AGG_COLUMN: {
    1573               4 :       AggInfo *pAggInfo = pExpr->pAggInfo;
    1574               4 :       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
    1575               4 :       if( !pAggInfo->directMode ){
    1576               0 :         sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
    1577               0 :         break;
    1578               4 :       }else if( pAggInfo->useSortingIdx ){
    1579               0 :         sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
    1580                 :                               pCol->iSorterColumn);
    1581               0 :         break;
    1582                 :       }
    1583                 :       /* Otherwise, fall thru into the TK_COLUMN case */
    1584                 :     }
    1585                 :     case TK_COLUMN: {
    1586             412 :       if( pExpr->iTable<0 ){
    1587                 :         /* This only happens when coding check constraints */
    1588                 :         assert( pParse->ckOffset>0 );
    1589               0 :         sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
    1590                 :       }else{
    1591             412 :         sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
    1592                 :       }
    1593             412 :       break;
    1594                 :     }
    1595                 :     case TK_INTEGER: {
    1596             106 :       codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
    1597             106 :       break;
    1598                 :     }
    1599                 :     case TK_FLOAT:
    1600                 :     case TK_STRING: {
    1601                 :       assert( TK_FLOAT==OP_Real );
    1602                 :       assert( TK_STRING==OP_String8 );
    1603             569 :       sqlite3DequoteExpr(pExpr);
    1604             569 :       sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
    1605             569 :       break;
    1606                 :     }
    1607                 :     case TK_NULL: {
    1608              47 :       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
    1609              47 :       break;
    1610                 :     }
    1611                 : #ifndef SQLITE_OMIT_BLOB_LITERAL
    1612                 :     case TK_BLOB: {
    1613                 :       int n;
    1614                 :       const char *z;
    1615                 :       assert( TK_BLOB==OP_HexBlob );
    1616               0 :       n = pExpr->token.n - 3;
    1617               0 :       z = (char*)pExpr->token.z + 2;
    1618                 :       assert( n>=0 );
    1619               0 :       if( n==0 ){
    1620               0 :         z = "";
    1621                 :       }
    1622               0 :       sqlite3VdbeOp3(v, op, 0, 0, z, n);
    1623               0 :       break;
    1624                 :     }
    1625                 : #endif
    1626                 :     case TK_VARIABLE: {
    1627              50 :       sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
    1628              50 :       if( pExpr->token.n>1 ){
    1629              25 :         sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
    1630                 :       }
    1631              50 :       break;
    1632                 :     }
    1633                 :     case TK_REGISTER: {
    1634             163 :       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
    1635             163 :       break;
    1636                 :     }
    1637                 : #ifndef SQLITE_OMIT_CAST
    1638                 :     case TK_CAST: {
    1639                 :       /* Expressions of the form:   CAST(pLeft AS token) */
    1640                 :       int aff, to_op;
    1641               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1642               0 :       aff = sqlite3AffinityType(&pExpr->token);
    1643               0 :       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
    1644                 :       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
    1645                 :       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
    1646                 :       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
    1647                 :       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
    1648                 :       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
    1649               0 :       sqlite3VdbeAddOp(v, to_op, 0, 0);
    1650               0 :       stackChng = 0;
    1651               0 :       break;
    1652                 :     }
    1653                 : #endif /* SQLITE_OMIT_CAST */
    1654                 :     case TK_LT:
    1655                 :     case TK_LE:
    1656                 :     case TK_GT:
    1657                 :     case TK_GE:
    1658                 :     case TK_NE:
    1659                 :     case TK_EQ: {
    1660                 :       assert( TK_LT==OP_Lt );
    1661                 :       assert( TK_LE==OP_Le );
    1662                 :       assert( TK_GT==OP_Gt );
    1663                 :       assert( TK_GE==OP_Ge );
    1664                 :       assert( TK_EQ==OP_Eq );
    1665                 :       assert( TK_NE==OP_Ne );
    1666               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1667               0 :       sqlite3ExprCode(pParse, pExpr->pRight);
    1668               0 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
    1669               0 :       stackChng = -1;
    1670               0 :       break;
    1671                 :     }
    1672                 :     case TK_AND:
    1673                 :     case TK_OR:
    1674                 :     case TK_PLUS:
    1675                 :     case TK_STAR:
    1676                 :     case TK_MINUS:
    1677                 :     case TK_REM:
    1678                 :     case TK_BITAND:
    1679                 :     case TK_BITOR:
    1680                 :     case TK_SLASH:
    1681                 :     case TK_LSHIFT:
    1682                 :     case TK_RSHIFT: 
    1683                 :     case TK_CONCAT: {
    1684                 :       assert( TK_AND==OP_And );
    1685                 :       assert( TK_OR==OP_Or );
    1686                 :       assert( TK_PLUS==OP_Add );
    1687                 :       assert( TK_MINUS==OP_Subtract );
    1688                 :       assert( TK_REM==OP_Remainder );
    1689                 :       assert( TK_BITAND==OP_BitAnd );
    1690                 :       assert( TK_BITOR==OP_BitOr );
    1691                 :       assert( TK_SLASH==OP_Divide );
    1692                 :       assert( TK_LSHIFT==OP_ShiftLeft );
    1693                 :       assert( TK_RSHIFT==OP_ShiftRight );
    1694                 :       assert( TK_CONCAT==OP_Concat );
    1695               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1696               0 :       sqlite3ExprCode(pParse, pExpr->pRight);
    1697               0 :       sqlite3VdbeAddOp(v, op, 0, 0);
    1698               0 :       stackChng = -1;
    1699               0 :       break;
    1700                 :     }
    1701                 :     case TK_UMINUS: {
    1702               0 :       Expr *pLeft = pExpr->pLeft;
    1703                 :       assert( pLeft );
    1704               0 :       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
    1705               0 :         Token *p = &pLeft->token;
    1706               0 :         char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
    1707               0 :         if( pLeft->op==TK_FLOAT ){
    1708               0 :           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
    1709                 :         }else{
    1710               0 :           codeInteger(v, z, p->n+1);
    1711                 :         }
    1712               0 :         sqliteFree(z);
    1713               0 :         break;
    1714                 :       }
    1715                 :       /* Fall through into TK_NOT */
    1716                 :     }
    1717                 :     case TK_BITNOT:
    1718                 :     case TK_NOT: {
    1719                 :       assert( TK_BITNOT==OP_BitNot );
    1720                 :       assert( TK_NOT==OP_Not );
    1721               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1722               0 :       sqlite3VdbeAddOp(v, op, 0, 0);
    1723               0 :       stackChng = 0;
    1724               0 :       break;
    1725                 :     }
    1726                 :     case TK_ISNULL:
    1727                 :     case TK_NOTNULL: {
    1728                 :       int dest;
    1729                 :       assert( TK_ISNULL==OP_IsNull );
    1730                 :       assert( TK_NOTNULL==OP_NotNull );
    1731               0 :       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
    1732               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1733               0 :       dest = sqlite3VdbeCurrentAddr(v) + 2;
    1734               0 :       sqlite3VdbeAddOp(v, op, 1, dest);
    1735               0 :       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
    1736               0 :       stackChng = 0;
    1737               0 :       break;
    1738                 :     }
    1739                 :     case TK_AGG_FUNCTION: {
    1740              14 :       AggInfo *pInfo = pExpr->pAggInfo;
    1741              14 :       if( pInfo==0 ){
    1742               0 :         sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
    1743                 :             &pExpr->span);
    1744                 :       }else{
    1745              14 :         sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
    1746                 :       }
    1747              14 :       break;
    1748                 :     }
    1749                 :     case TK_CONST_FUNC:
    1750                 :     case TK_FUNCTION: {
    1751               1 :       ExprList *pList = pExpr->pList;
    1752               1 :       int nExpr = pList ? pList->nExpr : 0;
    1753                 :       FuncDef *pDef;
    1754                 :       int nId;
    1755                 :       const char *zId;
    1756               1 :       int constMask = 0;
    1757                 :       int i;
    1758               1 :       u8 enc = ENC(pParse->db);
    1759               1 :       CollSeq *pColl = 0;
    1760               1 :       zId = (char*)pExpr->token.z;
    1761               1 :       nId = pExpr->token.n;
    1762               1 :       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
    1763                 :       assert( pDef!=0 );
    1764               1 :       nExpr = sqlite3ExprCodeExprList(pParse, pList);
    1765                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
    1766                 :       /* Possibly overload the function if the first argument is
    1767                 :       ** a virtual table column.
    1768                 :       **
    1769                 :       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
    1770                 :       ** second argument, not the first, as the argument to test to
    1771                 :       ** see if it is a column in a virtual table.  This is done because
    1772                 :       ** the left operand of infix functions (the operand we want to
    1773                 :       ** control overloading) ends up as the second argument to the
    1774                 :       ** function.  The expression "A glob B" is equivalent to 
    1775                 :       ** "glob(B,A).  We want to use the A in "A glob B" to test
    1776                 :       ** for function overloading.  But we use the B term in "glob(B,A)".
    1777                 :       */
    1778               1 :       if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
    1779               0 :         pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
    1780               1 :       }else if( nExpr>0 ){
    1781               1 :         pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
    1782                 :       }
    1783                 : #endif
    1784               2 :       for(i=0; i<nExpr && i<32; i++){
    1785               1 :         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
    1786               0 :           constMask |= (1<<i);
    1787                 :         }
    1788               1 :         if( pDef->needCollSeq && !pColl ){
    1789               0 :           pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
    1790                 :         }
    1791                 :       }
    1792               1 :       if( pDef->needCollSeq ){
    1793               0 :         if( !pColl ) pColl = pParse->db->pDfltColl; 
    1794               0 :         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
    1795                 :       }
    1796               1 :       sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
    1797               1 :       stackChng = 1-nExpr;
    1798               1 :       break;
    1799                 :     }
    1800                 : #ifndef SQLITE_OMIT_SUBQUERY
    1801                 :     case TK_EXISTS:
    1802                 :     case TK_SELECT: {
    1803               0 :       if( pExpr->iColumn==0 ){
    1804               0 :         sqlite3CodeSubselect(pParse, pExpr);
    1805                 :       }
    1806               0 :       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
    1807                 :       VdbeComment((v, "# load subquery result"));
    1808               0 :       break;
    1809                 :     }
    1810                 :     case TK_IN: {
    1811                 :       int addr;
    1812                 :       char affinity;
    1813               0 :       int ckOffset = pParse->ckOffset;
    1814               0 :       sqlite3CodeSubselect(pParse, pExpr);
    1815                 : 
    1816                 :       /* Figure out the affinity to use to create a key from the results
    1817                 :       ** of the expression. affinityStr stores a static string suitable for
    1818                 :       ** P3 of OP_MakeRecord.
    1819                 :       */
    1820               0 :       affinity = comparisonAffinity(pExpr);
    1821                 : 
    1822               0 :       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
    1823               0 :       pParse->ckOffset = ckOffset+1;
    1824                 : 
    1825                 :       /* Code the <expr> from "<expr> IN (...)". The temporary table
    1826                 :       ** pExpr->iTable contains the values that make up the (...) set.
    1827                 :       */
    1828               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1829               0 :       addr = sqlite3VdbeCurrentAddr(v);
    1830               0 :       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */
    1831               0 :       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
    1832               0 :       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
    1833               0 :       sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
    1834               0 :       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);   /* addr + 4 */
    1835               0 :       sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
    1836               0 :       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */
    1837                 : 
    1838               0 :       break;
    1839                 :     }
    1840                 : #endif
    1841                 :     case TK_BETWEEN: {
    1842               0 :       Expr *pLeft = pExpr->pLeft;
    1843               0 :       struct ExprList_item *pLItem = pExpr->pList->a;
    1844               0 :       Expr *pRight = pLItem->pExpr;
    1845               0 :       sqlite3ExprCode(pParse, pLeft);
    1846               0 :       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
    1847               0 :       sqlite3ExprCode(pParse, pRight);
    1848               0 :       codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
    1849               0 :       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
    1850               0 :       pLItem++;
    1851               0 :       pRight = pLItem->pExpr;
    1852               0 :       sqlite3ExprCode(pParse, pRight);
    1853               0 :       codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
    1854               0 :       sqlite3VdbeAddOp(v, OP_And, 0, 0);
    1855               0 :       break;
    1856                 :     }
    1857                 :     case TK_UPLUS:
    1858                 :     case TK_AS: {
    1859               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    1860               0 :       stackChng = 0;
    1861               0 :       break;
    1862                 :     }
    1863                 :     case TK_CASE: {
    1864                 :       int expr_end_label;
    1865                 :       int jumpInst;
    1866                 :       int nExpr;
    1867                 :       int i;
    1868                 :       ExprList *pEList;
    1869                 :       struct ExprList_item *aListelem;
    1870                 : 
    1871                 :       assert(pExpr->pList);
    1872                 :       assert((pExpr->pList->nExpr % 2) == 0);
    1873                 :       assert(pExpr->pList->nExpr > 0);
    1874               0 :       pEList = pExpr->pList;
    1875               0 :       aListelem = pEList->a;
    1876               0 :       nExpr = pEList->nExpr;
    1877               0 :       expr_end_label = sqlite3VdbeMakeLabel(v);
    1878               0 :       if( pExpr->pLeft ){
    1879               0 :         sqlite3ExprCode(pParse, pExpr->pLeft);
    1880                 :       }
    1881               0 :       for(i=0; i<nExpr; i=i+2){
    1882               0 :         sqlite3ExprCode(pParse, aListelem[i].pExpr);
    1883               0 :         if( pExpr->pLeft ){
    1884               0 :           sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
    1885               0 :           jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
    1886                 :                                  OP_Ne, 0, 1);
    1887               0 :           sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
    1888                 :         }else{
    1889               0 :           jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
    1890                 :         }
    1891               0 :         sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
    1892               0 :         sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
    1893               0 :         sqlite3VdbeJumpHere(v, jumpInst);
    1894                 :       }
    1895               0 :       if( pExpr->pLeft ){
    1896               0 :         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
    1897                 :       }
    1898               0 :       if( pExpr->pRight ){
    1899               0 :         sqlite3ExprCode(pParse, pExpr->pRight);
    1900                 :       }else{
    1901               0 :         sqlite3VdbeAddOp(v, OP_Null, 0, 0);
    1902                 :       }
    1903               0 :       sqlite3VdbeResolveLabel(v, expr_end_label);
    1904               0 :       break;
    1905                 :     }
    1906                 : #ifndef SQLITE_OMIT_TRIGGER
    1907                 :     case TK_RAISE: {
    1908               0 :       if( !pParse->trigStack ){
    1909               0 :         sqlite3ErrorMsg(pParse,
    1910                 :                        "RAISE() may only be used within a trigger-program");
    1911               0 :         return;
    1912                 :       }
    1913               0 :       if( pExpr->iColumn!=OE_Ignore ){
    1914                 :          assert( pExpr->iColumn==OE_Rollback ||
    1915                 :                  pExpr->iColumn == OE_Abort ||
    1916                 :                  pExpr->iColumn == OE_Fail );
    1917               0 :          sqlite3DequoteExpr(pExpr);
    1918               0 :          sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
    1919                 :                         (char*)pExpr->token.z, pExpr->token.n);
    1920                 :       } else {
    1921                 :          assert( pExpr->iColumn == OE_Ignore );
    1922               0 :          sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
    1923               0 :          sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
    1924                 :          VdbeComment((v, "# raise(IGNORE)"));
    1925                 :       }
    1926               0 :       stackChng = 0;
    1927                 :       break;
    1928                 :     }
    1929                 : #endif
    1930                 :   }
    1931                 : 
    1932            1362 :   if( pParse->ckOffset ){
    1933               0 :     pParse->ckOffset += stackChng;
    1934                 :     assert( pParse->ckOffset );
    1935                 :   }
    1936                 : }
    1937                 : 
    1938                 : #ifndef SQLITE_OMIT_TRIGGER
    1939                 : /*
    1940                 : ** Generate code that evalutes the given expression and leaves the result
    1941                 : ** on the stack.  See also sqlite3ExprCode().
    1942                 : **
    1943                 : ** This routine might also cache the result and modify the pExpr tree
    1944                 : ** so that it will make use of the cached result on subsequent evaluations
    1945                 : ** rather than evaluate the whole expression again.  Trivial expressions are
    1946                 : ** not cached.  If the expression is cached, its result is stored in a 
    1947                 : ** memory location.
    1948                 : */
    1949               0 : void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
    1950               0 :   Vdbe *v = pParse->pVdbe;
    1951                 :   int iMem;
    1952                 :   int addr1, addr2;
    1953               0 :   if( v==0 ) return;
    1954               0 :   addr1 = sqlite3VdbeCurrentAddr(v);
    1955               0 :   sqlite3ExprCode(pParse, pExpr);
    1956               0 :   addr2 = sqlite3VdbeCurrentAddr(v);
    1957               0 :   if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
    1958               0 :     iMem = pExpr->iTable = pParse->nMem++;
    1959               0 :     sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
    1960               0 :     pExpr->op = TK_REGISTER;
    1961                 :   }
    1962                 : }
    1963                 : #endif
    1964                 : 
    1965                 : /*
    1966                 : ** Generate code that pushes the value of every element of the given
    1967                 : ** expression list onto the stack.
    1968                 : **
    1969                 : ** Return the number of elements pushed onto the stack.
    1970                 : */
    1971                 : int sqlite3ExprCodeExprList(
    1972                 :   Parse *pParse,     /* Parsing context */
    1973                 :   ExprList *pList    /* The expression list to be coded */
    1974             158 : ){
    1975                 :   struct ExprList_item *pItem;
    1976                 :   int i, n;
    1977             158 :   if( pList==0 ) return 0;
    1978             158 :   n = pList->nExpr;
    1979             516 :   for(pItem=pList->a, i=n; i>0; i--, pItem++){
    1980             358 :     sqlite3ExprCode(pParse, pItem->pExpr);
    1981                 :   }
    1982             158 :   return n;
    1983                 : }
    1984                 : 
    1985                 : /*
    1986                 : ** Generate code for a boolean expression such that a jump is made
    1987                 : ** to the label "dest" if the expression is true but execution
    1988                 : ** continues straight thru if the expression is false.
    1989                 : **
    1990                 : ** If the expression evaluates to NULL (neither true nor false), then
    1991                 : ** take the jump if the jumpIfNull flag is true.
    1992                 : **
    1993                 : ** This code depends on the fact that certain token values (ex: TK_EQ)
    1994                 : ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
    1995                 : ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
    1996                 : ** the make process cause these values to align.  Assert()s in the code
    1997                 : ** below verify that the numbers are aligned correctly.
    1998                 : */
    1999               2 : void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
    2000               2 :   Vdbe *v = pParse->pVdbe;
    2001               2 :   int op = 0;
    2002               2 :   int ckOffset = pParse->ckOffset;
    2003               2 :   if( v==0 || pExpr==0 ) return;
    2004               2 :   op = pExpr->op;
    2005               2 :   switch( op ){
    2006                 :     case TK_AND: {
    2007               0 :       int d2 = sqlite3VdbeMakeLabel(v);
    2008               0 :       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
    2009               0 :       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
    2010               0 :       sqlite3VdbeResolveLabel(v, d2);
    2011               0 :       break;
    2012                 :     }
    2013                 :     case TK_OR: {
    2014               0 :       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
    2015               0 :       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
    2016               0 :       break;
    2017                 :     }
    2018                 :     case TK_NOT: {
    2019               0 :       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
    2020               0 :       break;
    2021                 :     }
    2022                 :     case TK_LT:
    2023                 :     case TK_LE:
    2024                 :     case TK_GT:
    2025                 :     case TK_GE:
    2026                 :     case TK_NE:
    2027                 :     case TK_EQ: {
    2028                 :       assert( TK_LT==OP_Lt );
    2029                 :       assert( TK_LE==OP_Le );
    2030                 :       assert( TK_GT==OP_Gt );
    2031                 :       assert( TK_GE==OP_Ge );
    2032                 :       assert( TK_EQ==OP_Eq );
    2033                 :       assert( TK_NE==OP_Ne );
    2034               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    2035               0 :       sqlite3ExprCode(pParse, pExpr->pRight);
    2036               0 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
    2037               0 :       break;
    2038                 :     }
    2039                 :     case TK_ISNULL:
    2040                 :     case TK_NOTNULL: {
    2041                 :       assert( TK_ISNULL==OP_IsNull );
    2042                 :       assert( TK_NOTNULL==OP_NotNull );
    2043               2 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    2044               2 :       sqlite3VdbeAddOp(v, op, 1, dest);
    2045               2 :       break;
    2046                 :     }
    2047                 :     case TK_BETWEEN: {
    2048                 :       /* The expression "x BETWEEN y AND z" is implemented as:
    2049                 :       **
    2050                 :       ** 1 IF (x < y) GOTO 3
    2051                 :       ** 2 IF (x <= z) GOTO <dest>
    2052                 :       ** 3 ...
    2053                 :       */
    2054                 :       int addr;
    2055               0 :       Expr *pLeft = pExpr->pLeft;
    2056               0 :       Expr *pRight = pExpr->pList->a[0].pExpr;
    2057               0 :       sqlite3ExprCode(pParse, pLeft);
    2058               0 :       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
    2059               0 :       sqlite3ExprCode(pParse, pRight);
    2060               0 :       addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
    2061                 : 
    2062               0 :       pRight = pExpr->pList->a[1].pExpr;
    2063               0 :       sqlite3ExprCode(pParse, pRight);
    2064               0 :       codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
    2065                 : 
    2066               0 :       sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
    2067               0 :       sqlite3VdbeJumpHere(v, addr);
    2068               0 :       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
    2069               0 :       break;
    2070                 :     }
    2071                 :     default: {
    2072               0 :       sqlite3ExprCode(pParse, pExpr);
    2073               0 :       sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
    2074                 :       break;
    2075                 :     }
    2076                 :   }
    2077               2 :   pParse->ckOffset = ckOffset;
    2078                 : }
    2079                 : 
    2080                 : /*
    2081                 : ** Generate code for a boolean expression such that a jump is made
    2082                 : ** to the label "dest" if the expression is false but execution
    2083                 : ** continues straight thru if the expression is true.
    2084                 : **
    2085                 : ** If the expression evaluates to NULL (neither true nor false) then
    2086                 : ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
    2087                 : */
    2088              86 : void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
    2089              86 :   Vdbe *v = pParse->pVdbe;
    2090              86 :   int op = 0;
    2091              86 :   int ckOffset = pParse->ckOffset;
    2092              86 :   if( v==0 || pExpr==0 ) return;
    2093                 : 
    2094                 :   /* The value of pExpr->op and op are related as follows:
    2095                 :   **
    2096                 :   **       pExpr->op            op
    2097                 :   **       ---------          ----------
    2098                 :   **       TK_ISNULL          OP_NotNull
    2099                 :   **       TK_NOTNULL         OP_IsNull
    2100                 :   **       TK_NE              OP_Eq
    2101                 :   **       TK_EQ              OP_Ne
    2102                 :   **       TK_GT              OP_Le
    2103                 :   **       TK_LE              OP_Gt
    2104                 :   **       TK_GE              OP_Lt
    2105                 :   **       TK_LT              OP_Ge
    2106                 :   **
    2107                 :   ** For other values of pExpr->op, op is undefined and unused.
    2108                 :   ** The value of TK_ and OP_ constants are arranged such that we
    2109                 :   ** can compute the mapping above using the following expression.
    2110                 :   ** Assert()s verify that the computation is correct.
    2111                 :   */
    2112              86 :   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
    2113                 : 
    2114                 :   /* Verify correct alignment of TK_ and OP_ constants
    2115                 :   */
    2116                 :   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
    2117                 :   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
    2118                 :   assert( pExpr->op!=TK_NE || op==OP_Eq );
    2119                 :   assert( pExpr->op!=TK_EQ || op==OP_Ne );
    2120                 :   assert( pExpr->op!=TK_LT || op==OP_Ge );
    2121                 :   assert( pExpr->op!=TK_LE || op==OP_Gt );
    2122                 :   assert( pExpr->op!=TK_GT || op==OP_Le );
    2123                 :   assert( pExpr->op!=TK_GE || op==OP_Lt );
    2124                 : 
    2125              86 :   switch( pExpr->op ){
    2126                 :     case TK_AND: {
    2127               0 :       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
    2128               0 :       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
    2129               0 :       break;
    2130                 :     }
    2131                 :     case TK_OR: {
    2132               2 :       int d2 = sqlite3VdbeMakeLabel(v);
    2133               2 :       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
    2134               2 :       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
    2135               2 :       sqlite3VdbeResolveLabel(v, d2);
    2136               2 :       break;
    2137                 :     }
    2138                 :     case TK_NOT: {
    2139               0 :       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
    2140               0 :       break;
    2141                 :     }
    2142                 :     case TK_LT:
    2143                 :     case TK_LE:
    2144                 :     case TK_GT:
    2145                 :     case TK_GE:
    2146                 :     case TK_NE:
    2147                 :     case TK_EQ: {
    2148              79 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    2149              79 :       sqlite3ExprCode(pParse, pExpr->pRight);
    2150              79 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
    2151              79 :       break;
    2152                 :     }
    2153                 :     case TK_ISNULL:
    2154                 :     case TK_NOTNULL: {
    2155               0 :       sqlite3ExprCode(pParse, pExpr->pLeft);
    2156               0 :       sqlite3VdbeAddOp(v, op, 1, dest);
    2157               0 :       break;
    2158                 :     }
    2159                 :     case TK_BETWEEN: {
    2160                 :       /* The expression is "x BETWEEN y AND z". It is implemented as:
    2161                 :       **
    2162                 :       ** 1 IF (x >= y) GOTO 3
    2163                 :       ** 2 GOTO <dest>
    2164                 :       ** 3 IF (x > z) GOTO <dest>
    2165                 :       */
    2166                 :       int addr;
    2167               0 :       Expr *pLeft = pExpr->pLeft;
    2168               0 :       Expr *pRight = pExpr->pList->a[0].pExpr;
    2169               0 :       sqlite3ExprCode(pParse, pLeft);
    2170               0 :       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
    2171               0 :       sqlite3ExprCode(pParse, pRight);
    2172               0 :       addr = sqlite3VdbeCurrentAddr(v);
    2173               0 :       codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
    2174                 : 
    2175               0 :       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
    2176               0 :       sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
    2177               0 :       pRight = pExpr->pList->a[1].pExpr;
    2178               0 :       sqlite3ExprCode(pParse, pRight);
    2179               0 :       codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
    2180               0 :       break;
    2181                 :     }
    2182                 :     default: {
    2183               5 :       sqlite3ExprCode(pParse, pExpr);
    2184               5 :       sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
    2185                 :       break;
    2186                 :     }
    2187                 :   }
    2188              86 :   pParse->ckOffset = ckOffset;
    2189                 : }
    2190                 : 
    2191                 : /*
    2192                 : ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
    2193                 : ** if they are identical and return FALSE if they differ in any way.
    2194                 : **
    2195                 : ** Sometimes this routine will return FALSE even if the two expressions
    2196                 : ** really are equivalent.  If we cannot prove that the expressions are
    2197                 : ** identical, we return FALSE just to be safe.  So if this routine
    2198                 : ** returns false, then you do not really know for certain if the two
    2199                 : ** expressions are the same.  But if you get a TRUE return, then you
    2200                 : ** can be sure the expressions are the same.  In the places where
    2201                 : ** this routine is used, it does not hurt to get an extra FALSE - that
    2202                 : ** just might result in some slightly slower code.  But returning
    2203                 : ** an incorrect TRUE could lead to a malfunction.
    2204                 : */
    2205               0 : int sqlite3ExprCompare(Expr *pA, Expr *pB){
    2206                 :   int i;
    2207               0 :   if( pA==0||pB==0 ){
    2208               0 :     return pB==pA;
    2209                 :   }
    2210               0 :   if( pA->op!=pB->op ) return 0;
    2211               0 :   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
    2212               0 :   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
    2213               0 :   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
    2214               0 :   if( pA->pList ){
    2215               0 :     if( pB->pList==0 ) return 0;
    2216               0 :     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
    2217               0 :     for(i=0; i<pA->pList->nExpr; i++){
    2218               0 :       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
    2219               0 :         return 0;
    2220                 :       }
    2221                 :     }
    2222               0 :   }else if( pB->pList ){
    2223               0 :     return 0;
    2224                 :   }
    2225               0 :   if( pA->pSelect || pB->pSelect ) return 0;
    2226               0 :   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
    2227               0 :   if( pA->op!=TK_COLUMN && pA->token.z ){
    2228               0 :     if( pB->token.z==0 ) return 0;
    2229               0 :     if( pB->token.n!=pA->token.n ) return 0;
    2230               0 :     if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
    2231               0 :       return 0;
    2232                 :     }
    2233                 :   }
    2234               0 :   return 1;
    2235                 : }
    2236                 : 
    2237                 : 
    2238                 : /*
    2239                 : ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
    2240                 : ** the new element.  Return a negative number if malloc fails.
    2241                 : */
    2242               4 : static int addAggInfoColumn(AggInfo *pInfo){
    2243                 :   int i;
    2244               4 :   pInfo->aCol = sqlite3ArrayAllocate(
    2245                 :        pInfo->aCol,
    2246                 :        sizeof(pInfo->aCol[0]),
    2247                 :        3,
    2248                 :        &pInfo->nColumn,
    2249                 :        &pInfo->nColumnAlloc,
    2250                 :        &i
    2251                 :   );
    2252               4 :   return i;
    2253                 : }    
    2254                 : 
    2255                 : /*
    2256                 : ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
    2257                 : ** the new element.  Return a negative number if malloc fails.
    2258                 : */
    2259              14 : static int addAggInfoFunc(AggInfo *pInfo){
    2260                 :   int i;
    2261              14 :   pInfo->aFunc = sqlite3ArrayAllocate(
    2262                 :        pInfo->aFunc,
    2263                 :        sizeof(pInfo->aFunc[0]),
    2264                 :        3,
    2265                 :        &pInfo->nFunc,
    2266                 :        &pInfo->nFuncAlloc,
    2267                 :        &i
    2268                 :   );
    2269              14 :   return i;
    2270                 : }    
    2271                 : 
    2272                 : /*
    2273                 : ** This is an xFunc for walkExprTree() used to implement 
    2274                 : ** sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
    2275                 : ** for additional information.
    2276                 : **
    2277                 : ** This routine analyzes the aggregate function at pExpr.
    2278                 : */
    2279              18 : static int analyzeAggregate(void *pArg, Expr *pExpr){
    2280                 :   int i;
    2281              18 :   NameContext *pNC = (NameContext *)pArg;
    2282              18 :   Parse *pParse = pNC->pParse;
    2283              18 :   SrcList *pSrcList = pNC->pSrcList;
    2284              18 :   AggInfo *pAggInfo = pNC->pAggInfo;
    2285                 :   
    2286                 : 
    2287              18 :   switch( pExpr->op ){
    2288                 :     case TK_AGG_COLUMN:
    2289                 :     case TK_COLUMN: {
    2290                 :       /* Check to see if the column is in one of the tables in the FROM
    2291                 :       ** clause of the aggregate query */
    2292               4 :       if( pSrcList ){
    2293               4 :         struct SrcList_item *pItem = pSrcList->a;
    2294               4 :         for(i=0; i<pSrcList->nSrc; i++, pItem++){
    2295                 :           struct AggInfo_col *pCol;
    2296               4 :           if( pExpr->iTable==pItem->iCursor ){
    2297                 :             /* If we reach this point, it means that pExpr refers to a table
    2298                 :             ** that is in the FROM clause of the aggregate query.  
    2299                 :             **
    2300                 :             ** Make an entry for the column in pAggInfo->aCol[] if there
    2301                 :             ** is not an entry there already.
    2302                 :             */
    2303                 :             int k;
    2304               4 :             pCol = pAggInfo->aCol;
    2305               4 :             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
    2306               0 :               if( pCol->iTable==pExpr->iTable &&
    2307                 :                   pCol->iColumn==pExpr->iColumn ){
    2308               0 :                 break;
    2309                 :               }
    2310                 :             }
    2311               4 :             if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
    2312               4 :               pCol = &pAggInfo->aCol[k];
    2313               4 :               pCol->pTab = pExpr->pTab;
    2314               4 :               pCol->iTable = pExpr->iTable;
    2315               4 :               pCol->iColumn = pExpr->iColumn;
    2316               4 :               pCol->iMem = pParse->nMem++;
    2317               4 :               pCol->iSorterColumn = -1;
    2318               4 :               pCol->pExpr = pExpr;
    2319               4 :               if( pAggInfo->pGroupBy ){
    2320                 :                 int j, n;
    2321               0 :                 ExprList *pGB = pAggInfo->pGroupBy;
    2322               0 :                 struct ExprList_item *pTerm = pGB->a;
    2323               0 :                 n = pGB->nExpr;
    2324               0 :                 for(j=0; j<n; j++, pTerm++){
    2325               0 :                   Expr *pE = pTerm->pExpr;
    2326               0 :                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
    2327                 :                       pE->iColumn==pExpr->iColumn ){
    2328               0 :                     pCol->iSorterColumn = j;
    2329               0 :                     break;
    2330                 :                   }
    2331                 :                 }
    2332                 :               }
    2333               4 :               if( pCol->iSorterColumn<0 ){
    2334               4 :                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
    2335                 :               }
    2336                 :             }
    2337                 :             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
    2338                 :             ** because it was there before or because we just created it).
    2339                 :             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
    2340                 :             ** pAggInfo->aCol[] entry.
    2341                 :             */
    2342               4 :             pExpr->pAggInfo = pAggInfo;
    2343               4 :             pExpr->op = TK_AGG_COLUMN;
    2344               4 :             pExpr->iAgg = k;
    2345               4 :             break;
    2346                 :           } /* endif pExpr->iTable==pItem->iCursor */
    2347                 :         } /* end loop over pSrcList */
    2348                 :       }
    2349               4 :       return 1;
    2350                 :     }
    2351                 :     case TK_AGG_FUNCTION: {
    2352                 :       /* The pNC->nDepth==0 test causes aggregate functions in subqueries
    2353                 :       ** to be ignored */
    2354              14 :       if( pNC->nDepth==0 ){
    2355                 :         /* Check to see if pExpr is a duplicate of another aggregate 
    2356                 :         ** function that is already in the pAggInfo structure
    2357                 :         */
    2358              14 :         struct AggInfo_func *pItem = pAggInfo->aFunc;
    2359              14 :         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
    2360               0 :           if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
    2361               0 :             break;
    2362                 :           }
    2363                 :         }
    2364              14 :         if( i>=pAggInfo->nFunc ){
    2365                 :           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
    2366                 :           */
    2367              14 :           u8 enc = ENC(pParse->db);
    2368              14 :           i = addAggInfoFunc(pAggInfo);
    2369              14 :           if( i>=0 ){
    2370              14 :             pItem = &pAggInfo->aFunc[i];
    2371              14 :             pItem->pExpr = pExpr;
    2372              14 :             pItem->iMem = pParse->nMem++;
    2373              14 :             pItem->pFunc = sqlite3FindFunction(pParse->db,
    2374                 :                    (char*)pExpr->token.z, pExpr->token.n,
    2375                 :                    pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
    2376              14 :             if( pExpr->flags & EP_Distinct ){
    2377               0 :               pItem->iDistinct = pParse->nTab++;
    2378                 :             }else{
    2379              14 :               pItem->iDistinct = -1;
    2380                 :             }
    2381                 :           }
    2382                 :         }
    2383                 :         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
    2384                 :         */
    2385              14 :         pExpr->iAgg = i;
    2386              14 :         pExpr->pAggInfo = pAggInfo;
    2387              14 :         return 1;
    2388                 :       }
    2389                 :     }
    2390                 :   }
    2391                 : 
    2392                 :   /* Recursively walk subqueries looking for TK_COLUMN nodes that need
    2393                 :   ** to be changed to TK_AGG_COLUMN.  But increment nDepth so that
    2394                 :   ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
    2395                 :   */
    2396               0 :   if( pExpr->pSelect ){
    2397               0 :     pNC->nDepth++;
    2398               0 :     walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
    2399               0 :     pNC->nDepth--;
    2400                 :   }
    2401               0 :   return 0;
    2402                 : }
    2403                 : 
    2404                 : /*
    2405                 : ** Analyze the given expression looking for aggregate functions and
    2406                 : ** for variables that need to be added to the pParse->aAgg[] array.
    2407                 : ** Make additional entries to the pParse->aAgg[] array as necessary.
    2408                 : **
    2409                 : ** This routine should only be called after the expression has been
    2410                 : ** analyzed by sqlite3ExprResolveNames().
    2411                 : **
    2412                 : ** If errors are seen, leave an error message in zErrMsg and return
    2413                 : ** the number of errors.
    2414                 : */
    2415              18 : int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
    2416              18 :   int nErr = pNC->pParse->nErr;
    2417              18 :   walkExprTree(pExpr, analyzeAggregate, pNC);
    2418              18 :   return pNC->pParse->nErr - nErr;
    2419                 : }
    2420                 : 
    2421                 : /*
    2422                 : ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
    2423                 : ** expression list.  Return the number of errors.
    2424                 : **
    2425                 : ** If an error is found, the analysis is cut short.
    2426                 : */
    2427              42 : int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
    2428                 :   struct ExprList_item *pItem;
    2429                 :   int i;
    2430              42 :   int nErr = 0;
    2431              42 :   if( pList ){
    2432              36 :     for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
    2433              18 :       nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
    2434                 :     }
    2435                 :   }
    2436              42 :   return nErr;
    2437                 : }

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:16 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.