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