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