xref: /sqlite-3.40.0/src/expr.c (revision 8a29dfde)
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.366 2008/04/11 15:36:03 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_SELECT ){
39     return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
40   }
41 #ifndef SQLITE_OMIT_CAST
42   if( op==TK_CAST ){
43     return sqlite3AffinityType(&pExpr->token);
44   }
45 #endif
46   return pExpr->affinity;
47 }
48 
49 /*
50 ** Set the collating sequence for expression pExpr to be the collating
51 ** sequence named by pToken.   Return a pointer to the revised expression.
52 ** The collating sequence is marked as "explicit" using the EP_ExpCollate
53 ** flag.  An explicit collating sequence will override implicit
54 ** collating sequences.
55 */
56 Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
57   char *zColl = 0;            /* Dequoted name of collation sequence */
58   CollSeq *pColl;
59   zColl = sqlite3NameFromToken(pParse->db, pName);
60   if( pExpr && zColl ){
61     pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
62     if( pColl ){
63       pExpr->pColl = pColl;
64       pExpr->flags |= EP_ExpCollate;
65     }
66   }
67   sqlite3_free(zColl);
68   return pExpr;
69 }
70 
71 /*
72 ** Return the default collation sequence for the expression pExpr. If
73 ** there is no default collation type, return 0.
74 */
75 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76   CollSeq *pColl = 0;
77   if( pExpr ){
78     int op;
79     pColl = pExpr->pColl;
80     op = pExpr->op;
81     if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
82       return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
83     }
84   }
85   if( sqlite3CheckCollSeq(pParse, pColl) ){
86     pColl = 0;
87   }
88   return pColl;
89 }
90 
91 /*
92 ** pExpr is an operand of a comparison operator.  aff2 is the
93 ** type affinity of the other operand.  This routine returns the
94 ** type affinity that should be used for the comparison operator.
95 */
96 char sqlite3CompareAffinity(Expr *pExpr, char aff2){
97   char aff1 = sqlite3ExprAffinity(pExpr);
98   if( aff1 && aff2 ){
99     /* Both sides of the comparison are columns. If one has numeric
100     ** affinity, use that. Otherwise use no affinity.
101     */
102     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
103       return SQLITE_AFF_NUMERIC;
104     }else{
105       return SQLITE_AFF_NONE;
106     }
107   }else if( !aff1 && !aff2 ){
108     /* Neither side of the comparison is a column.  Compare the
109     ** results directly.
110     */
111     return SQLITE_AFF_NONE;
112   }else{
113     /* One side is a column, the other is not. Use the columns affinity. */
114     assert( aff1==0 || aff2==0 );
115     return (aff1 + aff2);
116   }
117 }
118 
119 /*
120 ** pExpr is a comparison operator.  Return the type affinity that should
121 ** be applied to both operands prior to doing the comparison.
122 */
123 static char comparisonAffinity(Expr *pExpr){
124   char aff;
125   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
126           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
127           pExpr->op==TK_NE );
128   assert( pExpr->pLeft );
129   aff = sqlite3ExprAffinity(pExpr->pLeft);
130   if( pExpr->pRight ){
131     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
132   }
133   else if( pExpr->pSelect ){
134     aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
135   }
136   else if( !aff ){
137     aff = SQLITE_AFF_NONE;
138   }
139   return aff;
140 }
141 
142 /*
143 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
144 ** idx_affinity is the affinity of an indexed column. Return true
145 ** if the index with affinity idx_affinity may be used to implement
146 ** the comparison in pExpr.
147 */
148 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
149   char aff = comparisonAffinity(pExpr);
150   switch( aff ){
151     case SQLITE_AFF_NONE:
152       return 1;
153     case SQLITE_AFF_TEXT:
154       return idx_affinity==SQLITE_AFF_TEXT;
155     default:
156       return sqlite3IsNumericAffinity(idx_affinity);
157   }
158 }
159 
160 /*
161 ** Return the P5 value that should be used for a binary comparison
162 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
163 */
164 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
165   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
166   aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
167   return aff;
168 }
169 
170 /*
171 ** Return a pointer to the collation sequence that should be used by
172 ** a binary comparison operator comparing pLeft and pRight.
173 **
174 ** If the left hand expression has a collating sequence type, then it is
175 ** used. Otherwise the collation sequence for the right hand expression
176 ** is used, or the default (BINARY) if neither expression has a collating
177 ** type.
178 **
179 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
180 ** it is not considered.
181 */
182 CollSeq *sqlite3BinaryCompareCollSeq(
183   Parse *pParse,
184   Expr *pLeft,
185   Expr *pRight
186 ){
187   CollSeq *pColl;
188   assert( pLeft );
189   if( pLeft->flags & EP_ExpCollate ){
190     assert( pLeft->pColl );
191     pColl = pLeft->pColl;
192   }else if( pRight && pRight->flags & EP_ExpCollate ){
193     assert( pRight->pColl );
194     pColl = pRight->pColl;
195   }else{
196     pColl = sqlite3ExprCollSeq(pParse, pLeft);
197     if( !pColl ){
198       pColl = sqlite3ExprCollSeq(pParse, pRight);
199     }
200   }
201   return pColl;
202 }
203 
204 /*
205 ** Generate the operands for a comparison operation.  Before
206 ** generating the code for each operand, set the EP_AnyAff
207 ** flag on the expression so that it will be able to used a
208 ** cached column value that has previously undergone an
209 ** affinity change.
210 */
211 static void codeCompareOperands(
212   Parse *pParse,    /* Parsing and code generating context */
213   Expr *pLeft,      /* The left operand */
214   int *pRegLeft,    /* Register where left operand is stored */
215   int *pFreeLeft,   /* Free this register when done */
216   Expr *pRight,     /* The right operand */
217   int *pRegRight,   /* Register where right operand is stored */
218   int *pFreeRight   /* Write temp register for right operand there */
219 ){
220   while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
221   pLeft->flags |= EP_AnyAff;
222   *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
223   while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
224   pRight->flags |= EP_AnyAff;
225   *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
226 }
227 
228 /*
229 ** Generate code for a comparison operator.
230 */
231 static int codeCompare(
232   Parse *pParse,    /* The parsing (and code generating) context */
233   Expr *pLeft,      /* The left operand */
234   Expr *pRight,     /* The right operand */
235   int opcode,       /* The comparison opcode */
236   int in1, int in2, /* Register holding operands */
237   int dest,         /* Jump here if true.  */
238   int jumpIfNull    /* If true, jump if either operand is NULL */
239 ){
240   int p5;
241   int addr;
242   CollSeq *p4;
243 
244   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
245   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
246   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
247                            (void*)p4, P4_COLLSEQ);
248   sqlite3VdbeChangeP5(pParse->pVdbe, p5);
249   if( p5 & SQLITE_AFF_MASK ){
250     sqlite3ExprCacheAffinityChange(pParse, in1, 1);
251     sqlite3ExprCacheAffinityChange(pParse, in2, 1);
252   }
253   return addr;
254 }
255 
256 /*
257 ** Construct a new expression node and return a pointer to it.  Memory
258 ** for this node is obtained from sqlite3_malloc().  The calling function
259 ** is responsible for making sure the node eventually gets freed.
260 */
261 Expr *sqlite3Expr(
262   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
263   int op,                 /* Expression opcode */
264   Expr *pLeft,            /* Left operand */
265   Expr *pRight,           /* Right operand */
266   const Token *pToken     /* Argument token */
267 ){
268   Expr *pNew;
269   static const Expr zeroExpr;
270   pNew = sqlite3DbMallocRaw(db, sizeof(Expr));
271   if( pNew==0 ){
272     /* When malloc fails, delete pLeft and pRight. Expressions passed to
273     ** this function must always be allocated with sqlite3Expr() for this
274     ** reason.
275     */
276     sqlite3ExprDelete(pLeft);
277     sqlite3ExprDelete(pRight);
278     return 0;
279   }
280   *pNew = zeroExpr;
281   pNew->op = op;
282   pNew->pLeft = pLeft;
283   pNew->pRight = pRight;
284   pNew->iAgg = -1;
285   if( pToken ){
286     assert( pToken->dyn==0 );
287     pNew->span = pNew->token = *pToken;
288   }else if( pLeft ){
289     if( pRight ){
290       sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
291       if( pRight->flags & EP_ExpCollate ){
292         pNew->flags |= EP_ExpCollate;
293         pNew->pColl = pRight->pColl;
294       }
295     }
296     if( pLeft->flags & EP_ExpCollate ){
297       pNew->flags |= EP_ExpCollate;
298       pNew->pColl = pLeft->pColl;
299     }
300   }
301 
302   sqlite3ExprSetHeight(pNew);
303   return pNew;
304 }
305 
306 /*
307 ** Works like sqlite3Expr() except that it takes an extra Parse*
308 ** argument and notifies the associated connection object if malloc fails.
309 */
310 Expr *sqlite3PExpr(
311   Parse *pParse,          /* Parsing context */
312   int op,                 /* Expression opcode */
313   Expr *pLeft,            /* Left operand */
314   Expr *pRight,           /* Right operand */
315   const Token *pToken     /* Argument token */
316 ){
317   return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
318 }
319 
320 /*
321 ** When doing a nested parse, you can include terms in an expression
322 ** that look like this:   #1 #2 ...  These terms refer to registers
323 ** in the virtual machine.  #N is the N-th register.
324 **
325 ** This routine is called by the parser to deal with on of those terms.
326 ** It immediately generates code to store the value in a memory location.
327 ** The returns an expression that will code to extract the value from
328 ** that memory location as needed.
329 */
330 Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
331   Vdbe *v = pParse->pVdbe;
332   Expr *p;
333   if( pParse->nested==0 ){
334     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
335     return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
336   }
337   if( v==0 ) return 0;
338   p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
339   if( p==0 ){
340     return 0;  /* Malloc failed */
341   }
342   p->iTable = atoi((char*)&pToken->z[1]);
343   return p;
344 }
345 
346 /*
347 ** Join two expressions using an AND operator.  If either expression is
348 ** NULL, then just return the other expression.
349 */
350 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
351   if( pLeft==0 ){
352     return pRight;
353   }else if( pRight==0 ){
354     return pLeft;
355   }else{
356     return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
357   }
358 }
359 
360 /*
361 ** Set the Expr.span field of the given expression to span all
362 ** text between the two given tokens.
363 */
364 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
365   assert( pRight!=0 );
366   assert( pLeft!=0 );
367   if( pExpr && pRight->z && pLeft->z ){
368     assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
369     if( pLeft->dyn==0 && pRight->dyn==0 ){
370       pExpr->span.z = pLeft->z;
371       pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
372     }else{
373       pExpr->span.z = 0;
374     }
375   }
376 }
377 
378 /*
379 ** Construct a new expression node for a function with multiple
380 ** arguments.
381 */
382 Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
383   Expr *pNew;
384   assert( pToken );
385   pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
386   if( pNew==0 ){
387     sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
388     return 0;
389   }
390   pNew->op = TK_FUNCTION;
391   pNew->pList = pList;
392   assert( pToken->dyn==0 );
393   pNew->token = *pToken;
394   pNew->span = pNew->token;
395 
396   sqlite3ExprSetHeight(pNew);
397   return pNew;
398 }
399 
400 /*
401 ** Assign a variable number to an expression that encodes a wildcard
402 ** in the original SQL statement.
403 **
404 ** Wildcards consisting of a single "?" are assigned the next sequential
405 ** variable number.
406 **
407 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
408 ** sure "nnn" is not too be to avoid a denial of service attack when
409 ** the SQL statement comes from an external source.
410 **
411 ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
412 ** as the previous instance of the same wildcard.  Or if this is the first
413 ** instance of the wildcard, the next sequenial variable number is
414 ** assigned.
415 */
416 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
417   Token *pToken;
418   sqlite3 *db = pParse->db;
419 
420   if( pExpr==0 ) return;
421   pToken = &pExpr->token;
422   assert( pToken->n>=1 );
423   assert( pToken->z!=0 );
424   assert( pToken->z[0]!=0 );
425   if( pToken->n==1 ){
426     /* Wildcard of the form "?".  Assign the next variable number */
427     pExpr->iTable = ++pParse->nVar;
428   }else if( pToken->z[0]=='?' ){
429     /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
430     ** use it as the variable number */
431     int i;
432     pExpr->iTable = i = atoi((char*)&pToken->z[1]);
433     testcase( i==0 );
434     testcase( i==1 );
435     testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
436     testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
437     if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
438       sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
439           db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
440     }
441     if( i>pParse->nVar ){
442       pParse->nVar = i;
443     }
444   }else{
445     /* Wildcards of the form ":aaa" or "$aaa".  Reuse the same variable
446     ** number as the prior appearance of the same name, or if the name
447     ** has never appeared before, reuse the same variable number
448     */
449     int i, n;
450     n = pToken->n;
451     for(i=0; i<pParse->nVarExpr; i++){
452       Expr *pE;
453       if( (pE = pParse->apVarExpr[i])!=0
454           && pE->token.n==n
455           && memcmp(pE->token.z, pToken->z, n)==0 ){
456         pExpr->iTable = pE->iTable;
457         break;
458       }
459     }
460     if( i>=pParse->nVarExpr ){
461       pExpr->iTable = ++pParse->nVar;
462       if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
463         pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
464         pParse->apVarExpr =
465             sqlite3DbReallocOrFree(
466               db,
467               pParse->apVarExpr,
468               pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
469             );
470       }
471       if( !db->mallocFailed ){
472         assert( pParse->apVarExpr!=0 );
473         pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
474       }
475     }
476   }
477   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
478     sqlite3ErrorMsg(pParse, "too many SQL variables");
479   }
480 }
481 
482 /*
483 ** Recursively delete an expression tree.
484 */
485 void sqlite3ExprDelete(Expr *p){
486   if( p==0 ) return;
487   if( p->span.dyn ) sqlite3_free((char*)p->span.z);
488   if( p->token.dyn ) sqlite3_free((char*)p->token.z);
489   sqlite3ExprDelete(p->pLeft);
490   sqlite3ExprDelete(p->pRight);
491   sqlite3ExprListDelete(p->pList);
492   sqlite3SelectDelete(p->pSelect);
493   sqlite3_free(p);
494 }
495 
496 /*
497 ** The Expr.token field might be a string literal that is quoted.
498 ** If so, remove the quotation marks.
499 */
500 void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
501   if( ExprHasAnyProperty(p, EP_Dequoted) ){
502     return;
503   }
504   ExprSetProperty(p, EP_Dequoted);
505   if( p->token.dyn==0 ){
506     sqlite3TokenCopy(db, &p->token, &p->token);
507   }
508   sqlite3Dequote((char*)p->token.z);
509 }
510 
511 
512 /*
513 ** The following group of routines make deep copies of expressions,
514 ** expression lists, ID lists, and select statements.  The copies can
515 ** be deleted (by being passed to their respective ...Delete() routines)
516 ** without effecting the originals.
517 **
518 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
519 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
520 ** by subsequent calls to sqlite*ListAppend() routines.
521 **
522 ** Any tables that the SrcList might point to are not duplicated.
523 */
524 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
525   Expr *pNew;
526   if( p==0 ) return 0;
527   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
528   if( pNew==0 ) return 0;
529   memcpy(pNew, p, sizeof(*pNew));
530   if( p->token.z!=0 ){
531     pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
532     pNew->token.dyn = 1;
533   }else{
534     assert( pNew->token.z==0 );
535   }
536   pNew->span.z = 0;
537   pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
538   pNew->pRight = sqlite3ExprDup(db, p->pRight);
539   pNew->pList = sqlite3ExprListDup(db, p->pList);
540   pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
541   return pNew;
542 }
543 void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
544   if( pTo->dyn ) sqlite3_free((char*)pTo->z);
545   if( pFrom->z ){
546     pTo->n = pFrom->n;
547     pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
548     pTo->dyn = 1;
549   }else{
550     pTo->z = 0;
551   }
552 }
553 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
554   ExprList *pNew;
555   struct ExprList_item *pItem, *pOldItem;
556   int i;
557   if( p==0 ) return 0;
558   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
559   if( pNew==0 ) return 0;
560   pNew->iECursor = 0;
561   pNew->nExpr = pNew->nAlloc = p->nExpr;
562   pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
563   if( pItem==0 ){
564     sqlite3_free(pNew);
565     return 0;
566   }
567   pOldItem = p->a;
568   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
569     Expr *pNewExpr, *pOldExpr;
570     pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
571     if( pOldExpr->span.z!=0 && pNewExpr ){
572       /* Always make a copy of the span for top-level expressions in the
573       ** expression list.  The logic in SELECT processing that determines
574       ** the names of columns in the result set needs this information */
575       sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
576     }
577     assert( pNewExpr==0 || pNewExpr->span.z!=0
578             || pOldExpr->span.z==0
579             || db->mallocFailed );
580     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
581     pItem->sortOrder = pOldItem->sortOrder;
582     pItem->isAgg = pOldItem->isAgg;
583     pItem->done = 0;
584   }
585   return pNew;
586 }
587 
588 /*
589 ** If cursors, triggers, views and subqueries are all omitted from
590 ** the build, then none of the following routines, except for
591 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
592 ** called with a NULL argument.
593 */
594 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
595  || !defined(SQLITE_OMIT_SUBQUERY)
596 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
597   SrcList *pNew;
598   int i;
599   int nByte;
600   if( p==0 ) return 0;
601   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
602   pNew = sqlite3DbMallocRaw(db, nByte );
603   if( pNew==0 ) return 0;
604   pNew->nSrc = pNew->nAlloc = p->nSrc;
605   for(i=0; i<p->nSrc; i++){
606     struct SrcList_item *pNewItem = &pNew->a[i];
607     struct SrcList_item *pOldItem = &p->a[i];
608     Table *pTab;
609     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
610     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
611     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
612     pNewItem->jointype = pOldItem->jointype;
613     pNewItem->iCursor = pOldItem->iCursor;
614     pNewItem->isPopulated = pOldItem->isPopulated;
615     pTab = pNewItem->pTab = pOldItem->pTab;
616     if( pTab ){
617       pTab->nRef++;
618     }
619     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
620     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
621     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
622     pNewItem->colUsed = pOldItem->colUsed;
623   }
624   return pNew;
625 }
626 IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
627   IdList *pNew;
628   int i;
629   if( p==0 ) return 0;
630   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
631   if( pNew==0 ) return 0;
632   pNew->nId = pNew->nAlloc = p->nId;
633   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
634   if( pNew->a==0 ){
635     sqlite3_free(pNew);
636     return 0;
637   }
638   for(i=0; i<p->nId; i++){
639     struct IdList_item *pNewItem = &pNew->a[i];
640     struct IdList_item *pOldItem = &p->a[i];
641     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
642     pNewItem->idx = pOldItem->idx;
643   }
644   return pNew;
645 }
646 Select *sqlite3SelectDup(sqlite3 *db, Select *p){
647   Select *pNew;
648   if( p==0 ) return 0;
649   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
650   if( pNew==0 ) return 0;
651   pNew->isDistinct = p->isDistinct;
652   pNew->pEList = sqlite3ExprListDup(db, p->pEList);
653   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
654   pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
655   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
656   pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
657   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
658   pNew->op = p->op;
659   pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
660   pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
661   pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
662   pNew->iLimit = -1;
663   pNew->iOffset = -1;
664   pNew->isResolved = p->isResolved;
665   pNew->isAgg = p->isAgg;
666   pNew->usesEphm = 0;
667   pNew->disallowOrderBy = 0;
668   pNew->pRightmost = 0;
669   pNew->addrOpenEphm[0] = -1;
670   pNew->addrOpenEphm[1] = -1;
671   pNew->addrOpenEphm[2] = -1;
672   return pNew;
673 }
674 #else
675 Select *sqlite3SelectDup(sqlite3 *db, Select *p){
676   assert( p==0 );
677   return 0;
678 }
679 #endif
680 
681 
682 /*
683 ** Add a new element to the end of an expression list.  If pList is
684 ** initially NULL, then create a new expression list.
685 */
686 ExprList *sqlite3ExprListAppend(
687   Parse *pParse,          /* Parsing context */
688   ExprList *pList,        /* List to which to append. Might be NULL */
689   Expr *pExpr,            /* Expression to be appended */
690   Token *pName            /* AS keyword for the expression */
691 ){
692   sqlite3 *db = pParse->db;
693   if( pList==0 ){
694     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
695     if( pList==0 ){
696       goto no_mem;
697     }
698     assert( pList->nAlloc==0 );
699   }
700   if( pList->nAlloc<=pList->nExpr ){
701     struct ExprList_item *a;
702     int n = pList->nAlloc*2 + 4;
703     a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
704     if( a==0 ){
705       goto no_mem;
706     }
707     pList->a = a;
708     pList->nAlloc = n;
709   }
710   assert( pList->a!=0 );
711   if( pExpr || pName ){
712     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
713     memset(pItem, 0, sizeof(*pItem));
714     pItem->zName = sqlite3NameFromToken(db, pName);
715     pItem->pExpr = pExpr;
716   }
717   return pList;
718 
719 no_mem:
720   /* Avoid leaking memory if malloc has failed. */
721   sqlite3ExprDelete(pExpr);
722   sqlite3ExprListDelete(pList);
723   return 0;
724 }
725 
726 /*
727 ** If the expression list pEList contains more than iLimit elements,
728 ** leave an error message in pParse.
729 */
730 void sqlite3ExprListCheckLength(
731   Parse *pParse,
732   ExprList *pEList,
733   const char *zObject
734 ){
735   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
736   testcase( pEList && pEList->nExpr==mx );
737   testcase( pEList && pEList->nExpr==mx+1 );
738   if( pEList && pEList->nExpr>mx ){
739     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
740   }
741 }
742 
743 
744 /* The following three functions, heightOfExpr(), heightOfExprList()
745 ** and heightOfSelect(), are used to determine the maximum height
746 ** of any expression tree referenced by the structure passed as the
747 ** first argument.
748 **
749 ** If this maximum height is greater than the current value pointed
750 ** to by pnHeight, the second parameter, then set *pnHeight to that
751 ** value.
752 */
753 static void heightOfExpr(Expr *p, int *pnHeight){
754   if( p ){
755     if( p->nHeight>*pnHeight ){
756       *pnHeight = p->nHeight;
757     }
758   }
759 }
760 static void heightOfExprList(ExprList *p, int *pnHeight){
761   if( p ){
762     int i;
763     for(i=0; i<p->nExpr; i++){
764       heightOfExpr(p->a[i].pExpr, pnHeight);
765     }
766   }
767 }
768 static void heightOfSelect(Select *p, int *pnHeight){
769   if( p ){
770     heightOfExpr(p->pWhere, pnHeight);
771     heightOfExpr(p->pHaving, pnHeight);
772     heightOfExpr(p->pLimit, pnHeight);
773     heightOfExpr(p->pOffset, pnHeight);
774     heightOfExprList(p->pEList, pnHeight);
775     heightOfExprList(p->pGroupBy, pnHeight);
776     heightOfExprList(p->pOrderBy, pnHeight);
777     heightOfSelect(p->pPrior, pnHeight);
778   }
779 }
780 
781 /*
782 ** Set the Expr.nHeight variable in the structure passed as an
783 ** argument. An expression with no children, Expr.pList or
784 ** Expr.pSelect member has a height of 1. Any other expression
785 ** has a height equal to the maximum height of any other
786 ** referenced Expr plus one.
787 */
788 void sqlite3ExprSetHeight(Expr *p){
789   int nHeight = 0;
790   heightOfExpr(p->pLeft, &nHeight);
791   heightOfExpr(p->pRight, &nHeight);
792   heightOfExprList(p->pList, &nHeight);
793   heightOfSelect(p->pSelect, &nHeight);
794   p->nHeight = nHeight + 1;
795 }
796 
797 /*
798 ** Return the maximum height of any expression tree referenced
799 ** by the select statement passed as an argument.
800 */
801 int sqlite3SelectExprHeight(Select *p){
802   int nHeight = 0;
803   heightOfSelect(p, &nHeight);
804   return nHeight;
805 }
806 
807 /*
808 ** Delete an entire expression list.
809 */
810 void sqlite3ExprListDelete(ExprList *pList){
811   int i;
812   struct ExprList_item *pItem;
813   if( pList==0 ) return;
814   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
815   assert( pList->nExpr<=pList->nAlloc );
816   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
817     sqlite3ExprDelete(pItem->pExpr);
818     sqlite3_free(pItem->zName);
819   }
820   sqlite3_free(pList->a);
821   sqlite3_free(pList);
822 }
823 
824 /*
825 ** Walk an expression tree.  Call xFunc for each node visited.  xFunc
826 ** is called on the node before xFunc is called on the nodes children.
827 **
828 ** The return value from xFunc determines whether the tree walk continues.
829 ** 0 means continue walking the tree.  1 means do not walk children
830 ** of the current node but continue with siblings.  2 means abandon
831 ** the tree walk completely.
832 **
833 ** The return value from this routine is 1 to abandon the tree walk
834 ** and 0 to continue.
835 **
836 ** NOTICE:  This routine does *not* descend into subqueries.
837 */
838 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
839 static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
840   int rc;
841   if( pExpr==0 ) return 0;
842   rc = (*xFunc)(pArg, pExpr);
843   if( rc==0 ){
844     if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
845     if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
846     if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
847   }
848   return rc>1;
849 }
850 
851 /*
852 ** Call walkExprTree() for every expression in list p.
853 */
854 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
855   int i;
856   struct ExprList_item *pItem;
857   if( !p ) return 0;
858   for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
859     if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
860   }
861   return 0;
862 }
863 
864 /*
865 ** Call walkExprTree() for every expression in Select p, not including
866 ** expressions that are part of sub-selects in any FROM clause or the LIMIT
867 ** or OFFSET expressions..
868 */
869 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
870   walkExprList(p->pEList, xFunc, pArg);
871   walkExprTree(p->pWhere, xFunc, pArg);
872   walkExprList(p->pGroupBy, xFunc, pArg);
873   walkExprTree(p->pHaving, xFunc, pArg);
874   walkExprList(p->pOrderBy, xFunc, pArg);
875   if( p->pPrior ){
876     walkSelectExpr(p->pPrior, xFunc, pArg);
877   }
878   return 0;
879 }
880 
881 
882 /*
883 ** This routine is designed as an xFunc for walkExprTree().
884 **
885 ** pArg is really a pointer to an integer.  If we can tell by looking
886 ** at pExpr that the expression that contains pExpr is not a constant
887 ** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
888 ** If pExpr does does not disqualify the expression from being a constant
889 ** then do nothing.
890 **
891 ** After walking the whole tree, if no nodes are found that disqualify
892 ** the expression as constant, then we assume the whole expression
893 ** is constant.  See sqlite3ExprIsConstant() for additional information.
894 */
895 static int exprNodeIsConstant(void *pArg, Expr *pExpr){
896   int *pN = (int*)pArg;
897 
898   /* If *pArg is 3 then any term of the expression that comes from
899   ** the ON or USING clauses of a join disqualifies the expression
900   ** from being considered constant. */
901   if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
902     *pN = 0;
903     return 2;
904   }
905 
906   switch( pExpr->op ){
907     /* Consider functions to be constant if all their arguments are constant
908     ** and *pArg==2 */
909     case TK_FUNCTION:
910       if( (*pN)==2 ) return 0;
911       /* Fall through */
912     case TK_ID:
913     case TK_COLUMN:
914     case TK_DOT:
915     case TK_AGG_FUNCTION:
916     case TK_AGG_COLUMN:
917 #ifndef SQLITE_OMIT_SUBQUERY
918     case TK_SELECT:
919     case TK_EXISTS:
920       testcase( pExpr->op==TK_SELECT );
921       testcase( pExpr->op==TK_EXISTS );
922 #endif
923       testcase( pExpr->op==TK_ID );
924       testcase( pExpr->op==TK_COLUMN );
925       testcase( pExpr->op==TK_DOT );
926       testcase( pExpr->op==TK_AGG_FUNCTION );
927       testcase( pExpr->op==TK_AGG_COLUMN );
928       *pN = 0;
929       return 2;
930     case TK_IN:
931       if( pExpr->pSelect ){
932         *pN = 0;
933         return 2;
934       }
935     default:
936       return 0;
937   }
938 }
939 
940 /*
941 ** Walk an expression tree.  Return 1 if the expression is constant
942 ** and 0 if it involves variables or function calls.
943 **
944 ** For the purposes of this function, a double-quoted string (ex: "abc")
945 ** is considered a variable but a single-quoted string (ex: 'abc') is
946 ** a constant.
947 */
948 int sqlite3ExprIsConstant(Expr *p){
949   int isConst = 1;
950   walkExprTree(p, exprNodeIsConstant, &isConst);
951   return isConst;
952 }
953 
954 /*
955 ** Walk an expression tree.  Return 1 if the expression is constant
956 ** that does no originate from the ON or USING clauses of a join.
957 ** Return 0 if it involves variables or function calls or terms from
958 ** an ON or USING clause.
959 */
960 int sqlite3ExprIsConstantNotJoin(Expr *p){
961   int isConst = 3;
962   walkExprTree(p, exprNodeIsConstant, &isConst);
963   return isConst!=0;
964 }
965 
966 /*
967 ** Walk an expression tree.  Return 1 if the expression is constant
968 ** or a function call with constant arguments.  Return and 0 if there
969 ** are any variables.
970 **
971 ** For the purposes of this function, a double-quoted string (ex: "abc")
972 ** is considered a variable but a single-quoted string (ex: 'abc') is
973 ** a constant.
974 */
975 int sqlite3ExprIsConstantOrFunction(Expr *p){
976   int isConst = 2;
977   walkExprTree(p, exprNodeIsConstant, &isConst);
978   return isConst!=0;
979 }
980 
981 /*
982 ** If the expression p codes a constant integer that is small enough
983 ** to fit in a 32-bit integer, return 1 and put the value of the integer
984 ** in *pValue.  If the expression is not an integer or if it is too big
985 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
986 */
987 int sqlite3ExprIsInteger(Expr *p, int *pValue){
988   switch( p->op ){
989     case TK_INTEGER: {
990       if( sqlite3GetInt32((char*)p->token.z, pValue) ){
991         return 1;
992       }
993       break;
994     }
995     case TK_UPLUS: {
996       return sqlite3ExprIsInteger(p->pLeft, pValue);
997     }
998     case TK_UMINUS: {
999       int v;
1000       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
1001         *pValue = -v;
1002         return 1;
1003       }
1004       break;
1005     }
1006     default: break;
1007   }
1008   return 0;
1009 }
1010 
1011 /*
1012 ** Return TRUE if the given string is a row-id column name.
1013 */
1014 int sqlite3IsRowid(const char *z){
1015   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1016   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1017   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
1018   return 0;
1019 }
1020 
1021 /*
1022 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1023 ** that name in the set of source tables in pSrcList and make the pExpr
1024 ** expression node refer back to that source column.  The following changes
1025 ** are made to pExpr:
1026 **
1027 **    pExpr->iDb           Set the index in db->aDb[] of the database holding
1028 **                         the table.
1029 **    pExpr->iTable        Set to the cursor number for the table obtained
1030 **                         from pSrcList.
1031 **    pExpr->iColumn       Set to the column number within the table.
1032 **    pExpr->op            Set to TK_COLUMN.
1033 **    pExpr->pLeft         Any expression this points to is deleted
1034 **    pExpr->pRight        Any expression this points to is deleted.
1035 **
1036 ** The pDbToken is the name of the database (the "X").  This value may be
1037 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
1038 ** can be used.  The pTableToken is the name of the table (the "Y").  This
1039 ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
1040 ** means that the form of the name is Z and that columns from any table
1041 ** can be used.
1042 **
1043 ** If the name cannot be resolved unambiguously, leave an error message
1044 ** in pParse and return non-zero.  Return zero on success.
1045 */
1046 static int lookupName(
1047   Parse *pParse,       /* The parsing context */
1048   Token *pDbToken,     /* Name of the database containing table, or NULL */
1049   Token *pTableToken,  /* Name of table containing column, or NULL */
1050   Token *pColumnToken, /* Name of the column. */
1051   NameContext *pNC,    /* The name context used to resolve the name */
1052   Expr *pExpr          /* Make this EXPR node point to the selected column */
1053 ){
1054   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
1055   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
1056   char *zCol = 0;      /* Name of the column.  The "Z" */
1057   int i, j;            /* Loop counters */
1058   int cnt = 0;         /* Number of matching column names */
1059   int cntTab = 0;      /* Number of matching table names */
1060   sqlite3 *db = pParse->db;  /* The database */
1061   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
1062   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
1063   NameContext *pTopNC = pNC;        /* First namecontext in the list */
1064   Schema *pSchema = 0;              /* Schema of the expression */
1065 
1066   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
1067   zDb = sqlite3NameFromToken(db, pDbToken);
1068   zTab = sqlite3NameFromToken(db, pTableToken);
1069   zCol = sqlite3NameFromToken(db, pColumnToken);
1070   if( db->mallocFailed ){
1071     goto lookupname_end;
1072   }
1073 
1074   pExpr->iTable = -1;
1075   while( pNC && cnt==0 ){
1076     ExprList *pEList;
1077     SrcList *pSrcList = pNC->pSrcList;
1078 
1079     if( pSrcList ){
1080       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
1081         Table *pTab;
1082         int iDb;
1083         Column *pCol;
1084 
1085         pTab = pItem->pTab;
1086         assert( pTab!=0 );
1087         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1088         assert( pTab->nCol>0 );
1089         if( zTab ){
1090           if( pItem->zAlias ){
1091             char *zTabName = pItem->zAlias;
1092             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1093           }else{
1094             char *zTabName = pTab->zName;
1095             if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1096             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
1097               continue;
1098             }
1099           }
1100         }
1101         if( 0==(cntTab++) ){
1102           pExpr->iTable = pItem->iCursor;
1103           pSchema = pTab->pSchema;
1104           pMatch = pItem;
1105         }
1106         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1107           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
1108             const char *zColl = pTab->aCol[j].zColl;
1109             IdList *pUsing;
1110             cnt++;
1111             pExpr->iTable = pItem->iCursor;
1112             pMatch = pItem;
1113             pSchema = pTab->pSchema;
1114             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1115             pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1116             pExpr->affinity = pTab->aCol[j].affinity;
1117             if( (pExpr->flags & EP_ExpCollate)==0 ){
1118               pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1119             }
1120             if( i<pSrcList->nSrc-1 ){
1121               if( pItem[1].jointype & JT_NATURAL ){
1122                 /* If this match occurred in the left table of a natural join,
1123                 ** then skip the right table to avoid a duplicate match */
1124                 pItem++;
1125                 i++;
1126               }else if( (pUsing = pItem[1].pUsing)!=0 ){
1127                 /* If this match occurs on a column that is in the USING clause
1128                 ** of a join, skip the search of the right table of the join
1129                 ** to avoid a duplicate match there. */
1130                 int k;
1131                 for(k=0; k<pUsing->nId; k++){
1132                   if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1133                     pItem++;
1134                     i++;
1135                     break;
1136                   }
1137                 }
1138               }
1139             }
1140             break;
1141           }
1142         }
1143       }
1144     }
1145 
1146 #ifndef SQLITE_OMIT_TRIGGER
1147     /* If we have not already resolved the name, then maybe
1148     ** it is a new.* or old.* trigger argument reference
1149     */
1150     if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1151       TriggerStack *pTriggerStack = pParse->trigStack;
1152       Table *pTab = 0;
1153       u32 *piColMask;
1154       if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1155         pExpr->iTable = pTriggerStack->newIdx;
1156         assert( pTriggerStack->pTab );
1157         pTab = pTriggerStack->pTab;
1158         piColMask = &(pTriggerStack->newColMask);
1159       }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1160         pExpr->iTable = pTriggerStack->oldIdx;
1161         assert( pTriggerStack->pTab );
1162         pTab = pTriggerStack->pTab;
1163         piColMask = &(pTriggerStack->oldColMask);
1164       }
1165 
1166       if( pTab ){
1167         int iCol;
1168         Column *pCol = pTab->aCol;
1169 
1170         pSchema = pTab->pSchema;
1171         cntTab++;
1172         for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
1173           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
1174             const char *zColl = pTab->aCol[iCol].zColl;
1175             cnt++;
1176             pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1177             pExpr->affinity = pTab->aCol[iCol].affinity;
1178             if( (pExpr->flags & EP_ExpCollate)==0 ){
1179               pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1180             }
1181             pExpr->pTab = pTab;
1182             if( iCol>=0 ){
1183               testcase( iCol==31 );
1184               testcase( iCol==32 );
1185               *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1186             }
1187             break;
1188           }
1189         }
1190       }
1191     }
1192 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
1193 
1194     /*
1195     ** Perhaps the name is a reference to the ROWID
1196     */
1197     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1198       cnt = 1;
1199       pExpr->iColumn = -1;
1200       pExpr->affinity = SQLITE_AFF_INTEGER;
1201     }
1202 
1203     /*
1204     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1205     ** might refer to an result-set alias.  This happens, for example, when
1206     ** we are resolving names in the WHERE clause of the following command:
1207     **
1208     **     SELECT a+b AS x FROM table WHERE x<10;
1209     **
1210     ** In cases like this, replace pExpr with a copy of the expression that
1211     ** forms the result set entry ("a+b" in the example) and return immediately.
1212     ** Note that the expression in the result set should have already been
1213     ** resolved by the time the WHERE clause is resolved.
1214     */
1215     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
1216       for(j=0; j<pEList->nExpr; j++){
1217         char *zAs = pEList->a[j].zName;
1218         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1219           Expr *pDup, *pOrig;
1220           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1221           assert( pExpr->pList==0 );
1222           assert( pExpr->pSelect==0 );
1223           pOrig = pEList->a[j].pExpr;
1224           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1225             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
1226             sqlite3_free(zCol);
1227             return 2;
1228           }
1229           pDup = sqlite3ExprDup(db, pOrig);
1230           if( pExpr->flags & EP_ExpCollate ){
1231             pDup->pColl = pExpr->pColl;
1232             pDup->flags |= EP_ExpCollate;
1233           }
1234           if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1235           if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
1236           memcpy(pExpr, pDup, sizeof(*pExpr));
1237           sqlite3_free(pDup);
1238           cnt = 1;
1239           pMatch = 0;
1240           assert( zTab==0 && zDb==0 );
1241           goto lookupname_end_2;
1242         }
1243       }
1244     }
1245 
1246     /* Advance to the next name context.  The loop will exit when either
1247     ** we have a match (cnt>0) or when we run out of name contexts.
1248     */
1249     if( cnt==0 ){
1250       pNC = pNC->pNext;
1251     }
1252   }
1253 
1254   /*
1255   ** If X and Y are NULL (in other words if only the column name Z is
1256   ** supplied) and the value of Z is enclosed in double-quotes, then
1257   ** Z is a string literal if it doesn't match any column names.  In that
1258   ** case, we need to return right away and not make any changes to
1259   ** pExpr.
1260   **
1261   ** Because no reference was made to outer contexts, the pNC->nRef
1262   ** fields are not changed in any context.
1263   */
1264   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1265     sqlite3_free(zCol);
1266     return 0;
1267   }
1268 
1269   /*
1270   ** cnt==0 means there was not match.  cnt>1 means there were two or
1271   ** more matches.  Either way, we have an error.
1272   */
1273   if( cnt!=1 ){
1274     const char *zErr;
1275     zErr = cnt==0 ? "no such column" : "ambiguous column name";
1276     if( zDb ){
1277       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
1278     }else if( zTab ){
1279       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
1280     }else{
1281       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
1282     }
1283     pTopNC->nErr++;
1284   }
1285 
1286   /* If a column from a table in pSrcList is referenced, then record
1287   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
1288   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
1289   ** column number is greater than the number of bits in the bitmask
1290   ** then set the high-order bit of the bitmask.
1291   */
1292   if( pExpr->iColumn>=0 && pMatch!=0 ){
1293     int n = pExpr->iColumn;
1294     testcase( n==sizeof(Bitmask)*8-1 );
1295     if( n>=sizeof(Bitmask)*8 ){
1296       n = sizeof(Bitmask)*8-1;
1297     }
1298     assert( pMatch->iCursor==pExpr->iTable );
1299     pMatch->colUsed |= ((Bitmask)1)<<n;
1300   }
1301 
1302 lookupname_end:
1303   /* Clean up and return
1304   */
1305   sqlite3_free(zDb);
1306   sqlite3_free(zTab);
1307   sqlite3ExprDelete(pExpr->pLeft);
1308   pExpr->pLeft = 0;
1309   sqlite3ExprDelete(pExpr->pRight);
1310   pExpr->pRight = 0;
1311   pExpr->op = TK_COLUMN;
1312 lookupname_end_2:
1313   sqlite3_free(zCol);
1314   if( cnt==1 ){
1315     assert( pNC!=0 );
1316     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
1317     if( pMatch && !pMatch->pSelect ){
1318       pExpr->pTab = pMatch->pTab;
1319     }
1320     /* Increment the nRef value on all name contexts from TopNC up to
1321     ** the point where the name matched. */
1322     for(;;){
1323       assert( pTopNC!=0 );
1324       pTopNC->nRef++;
1325       if( pTopNC==pNC ) break;
1326       pTopNC = pTopNC->pNext;
1327     }
1328     return 0;
1329   } else {
1330     return 1;
1331   }
1332 }
1333 
1334 /*
1335 ** This routine is designed as an xFunc for walkExprTree().
1336 **
1337 ** Resolve symbolic names into TK_COLUMN operators for the current
1338 ** node in the expression tree.  Return 0 to continue the search down
1339 ** the tree or 2 to abort the tree walk.
1340 **
1341 ** This routine also does error checking and name resolution for
1342 ** function names.  The operator for aggregate functions is changed
1343 ** to TK_AGG_FUNCTION.
1344 */
1345 static int nameResolverStep(void *pArg, Expr *pExpr){
1346   NameContext *pNC = (NameContext*)pArg;
1347   Parse *pParse;
1348 
1349   if( pExpr==0 ) return 1;
1350   assert( pNC!=0 );
1351   pParse = pNC->pParse;
1352 
1353   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1354   ExprSetProperty(pExpr, EP_Resolved);
1355 #ifndef NDEBUG
1356   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1357     SrcList *pSrcList = pNC->pSrcList;
1358     int i;
1359     for(i=0; i<pNC->pSrcList->nSrc; i++){
1360       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1361     }
1362   }
1363 #endif
1364   switch( pExpr->op ){
1365     /* Double-quoted strings (ex: "abc") are used as identifiers if
1366     ** possible.  Otherwise they remain as strings.  Single-quoted
1367     ** strings (ex: 'abc') are always string literals.
1368     */
1369     case TK_STRING: {
1370       if( pExpr->token.z[0]=='\'' ) break;
1371       /* Fall thru into the TK_ID case if this is a double-quoted string */
1372     }
1373     /* A lone identifier is the name of a column.
1374     */
1375     case TK_ID: {
1376       lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1377       return 1;
1378     }
1379 
1380     /* A table name and column name:     ID.ID
1381     ** Or a database, table and column:  ID.ID.ID
1382     */
1383     case TK_DOT: {
1384       Token *pColumn;
1385       Token *pTable;
1386       Token *pDb;
1387       Expr *pRight;
1388 
1389       /* if( pSrcList==0 ) break; */
1390       pRight = pExpr->pRight;
1391       if( pRight->op==TK_ID ){
1392         pDb = 0;
1393         pTable = &pExpr->pLeft->token;
1394         pColumn = &pRight->token;
1395       }else{
1396         assert( pRight->op==TK_DOT );
1397         pDb = &pExpr->pLeft->token;
1398         pTable = &pRight->pLeft->token;
1399         pColumn = &pRight->pRight->token;
1400       }
1401       lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1402       return 1;
1403     }
1404 
1405     /* Resolve function names
1406     */
1407     case TK_CONST_FUNC:
1408     case TK_FUNCTION: {
1409       ExprList *pList = pExpr->pList;    /* The argument list */
1410       int n = pList ? pList->nExpr : 0;  /* Number of arguments */
1411       int no_such_func = 0;       /* True if no such function exists */
1412       int wrong_num_args = 0;     /* True if wrong number of arguments */
1413       int is_agg = 0;             /* True if is an aggregate function */
1414       int i;
1415       int auth;                   /* Authorization to use the function */
1416       int nId;                    /* Number of characters in function name */
1417       const char *zId;            /* The function name. */
1418       FuncDef *pDef;              /* Information about the function */
1419       int enc = ENC(pParse->db);  /* The database encoding */
1420 
1421       zId = (char*)pExpr->token.z;
1422       nId = pExpr->token.n;
1423       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1424       if( pDef==0 ){
1425         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1426         if( pDef==0 ){
1427           no_such_func = 1;
1428         }else{
1429           wrong_num_args = 1;
1430         }
1431       }else{
1432         is_agg = pDef->xFunc==0;
1433       }
1434 #ifndef SQLITE_OMIT_AUTHORIZATION
1435       if( pDef ){
1436         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1437         if( auth!=SQLITE_OK ){
1438           if( auth==SQLITE_DENY ){
1439             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1440                                     pDef->zName);
1441             pNC->nErr++;
1442           }
1443           pExpr->op = TK_NULL;
1444           return 1;
1445         }
1446       }
1447 #endif
1448       if( is_agg && !pNC->allowAgg ){
1449         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1450         pNC->nErr++;
1451         is_agg = 0;
1452       }else if( no_such_func ){
1453         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1454         pNC->nErr++;
1455       }else if( wrong_num_args ){
1456         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1457              nId, zId);
1458         pNC->nErr++;
1459       }
1460       if( is_agg ){
1461         pExpr->op = TK_AGG_FUNCTION;
1462         pNC->hasAgg = 1;
1463       }
1464       if( is_agg ) pNC->allowAgg = 0;
1465       for(i=0; pNC->nErr==0 && i<n; i++){
1466         walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
1467       }
1468       if( is_agg ) pNC->allowAgg = 1;
1469       /* FIX ME:  Compute pExpr->affinity based on the expected return
1470       ** type of the function
1471       */
1472       return is_agg;
1473     }
1474 #ifndef SQLITE_OMIT_SUBQUERY
1475     case TK_SELECT:
1476     case TK_EXISTS:
1477 #endif
1478     case TK_IN: {
1479       if( pExpr->pSelect ){
1480         int nRef = pNC->nRef;
1481 #ifndef SQLITE_OMIT_CHECK
1482         if( pNC->isCheck ){
1483           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1484         }
1485 #endif
1486         sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1487         assert( pNC->nRef>=nRef );
1488         if( nRef!=pNC->nRef ){
1489           ExprSetProperty(pExpr, EP_VarSelect);
1490         }
1491       }
1492       break;
1493     }
1494 #ifndef SQLITE_OMIT_CHECK
1495     case TK_VARIABLE: {
1496       if( pNC->isCheck ){
1497         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1498       }
1499       break;
1500     }
1501 #endif
1502   }
1503   return 0;
1504 }
1505 
1506 /*
1507 ** This routine walks an expression tree and resolves references to
1508 ** table columns.  Nodes of the form ID.ID or ID resolve into an
1509 ** index to the table in the table list and a column offset.  The
1510 ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
1511 ** value is changed to the index of the referenced table in pTabList
1512 ** plus the "base" value.  The base value will ultimately become the
1513 ** VDBE cursor number for a cursor that is pointing into the referenced
1514 ** table.  The Expr.iColumn value is changed to the index of the column
1515 ** of the referenced table.  The Expr.iColumn value for the special
1516 ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
1517 ** alias for ROWID.
1518 **
1519 ** Also resolve function names and check the functions for proper
1520 ** usage.  Make sure all function names are recognized and all functions
1521 ** have the correct number of arguments.  Leave an error message
1522 ** in pParse->zErrMsg if anything is amiss.  Return the number of errors.
1523 **
1524 ** If the expression contains aggregate functions then set the EP_Agg
1525 ** property on the expression.
1526 */
1527 int sqlite3ExprResolveNames(
1528   NameContext *pNC,       /* Namespace to resolve expressions in. */
1529   Expr *pExpr             /* The expression to be analyzed. */
1530 ){
1531   int savedHasAgg;
1532 
1533   if( pExpr==0 ) return 0;
1534 #if SQLITE_MAX_EXPR_DEPTH>0
1535   {
1536     int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
1537     if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){
1538       sqlite3ErrorMsg(pNC->pParse,
1539          "Expression tree is too large (maximum depth %d)", mxDepth
1540       );
1541       return 1;
1542     }
1543     pNC->pParse->nHeight += pExpr->nHeight;
1544   }
1545 #endif
1546   savedHasAgg = pNC->hasAgg;
1547   pNC->hasAgg = 0;
1548   walkExprTree(pExpr, nameResolverStep, pNC);
1549 #if SQLITE_MAX_EXPR_DEPTH>0
1550   pNC->pParse->nHeight -= pExpr->nHeight;
1551 #endif
1552   if( pNC->nErr>0 ){
1553     ExprSetProperty(pExpr, EP_Error);
1554   }
1555   if( pNC->hasAgg ){
1556     ExprSetProperty(pExpr, EP_Agg);
1557   }else if( savedHasAgg ){
1558     pNC->hasAgg = 1;
1559   }
1560   return ExprHasProperty(pExpr, EP_Error);
1561 }
1562 
1563 /*
1564 ** A pointer instance of this structure is used to pass information
1565 ** through walkExprTree into codeSubqueryStep().
1566 */
1567 typedef struct QueryCoder QueryCoder;
1568 struct QueryCoder {
1569   Parse *pParse;       /* The parsing context */
1570   NameContext *pNC;    /* Namespace of first enclosing query */
1571 };
1572 
1573 #ifdef SQLITE_TEST
1574   int sqlite3_enable_in_opt = 1;
1575 #else
1576   #define sqlite3_enable_in_opt 1
1577 #endif
1578 
1579 /*
1580 ** This function is used by the implementation of the IN (...) operator.
1581 ** It's job is to find or create a b-tree structure that may be used
1582 ** either to test for membership of the (...) set or to iterate through
1583 ** its members, skipping duplicates.
1584 **
1585 ** The cursor opened on the structure (database table, database index
1586 ** or ephermal table) is stored in pX->iTable before this function returns.
1587 ** The returned value indicates the structure type, as follows:
1588 **
1589 **   IN_INDEX_ROWID - The cursor was opened on a database table.
1590 **   IN_INDEX_INDEX - The cursor was opened on a database index.
1591 **   IN_INDEX_EPH -   The cursor was opened on a specially created and
1592 **                    populated epheremal table.
1593 **
1594 ** An existing structure may only be used if the SELECT is of the simple
1595 ** form:
1596 **
1597 **     SELECT <column> FROM <table>
1598 **
1599 ** If the mustBeUnique parameter is false, the structure will be used
1600 ** for fast set membership tests. In this case an epheremal table must
1601 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1602 ** be found with <column> as its left-most column.
1603 **
1604 ** If mustBeUnique is true, then the structure will be used to iterate
1605 ** through the set members, skipping any duplicates. In this case an
1606 ** epheremal table must be used unless the selected <column> is guaranteed
1607 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
1608 ** is unique by virtue of a constraint or implicit index.
1609 */
1610 #ifndef SQLITE_OMIT_SUBQUERY
1611 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1612   Select *p;
1613   int eType = 0;
1614   int iTab = pParse->nTab++;
1615 
1616   /* The follwing if(...) expression is true if the SELECT is of the
1617   ** simple form:
1618   **
1619   **     SELECT <column> FROM <table>
1620   **
1621   ** If this is the case, it may be possible to use an existing table
1622   ** or index instead of generating an epheremal table.
1623   */
1624   if( sqlite3_enable_in_opt
1625    && (p=pX->pSelect)!=0 && !p->pPrior
1626    && !p->isDistinct && !p->isAgg && !p->pGroupBy
1627    && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
1628    && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect
1629    && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1630    && !p->pLimit && !p->pOffset && !p->pWhere
1631   ){
1632     sqlite3 *db = pParse->db;
1633     Index *pIdx;
1634     Expr *pExpr = p->pEList->a[0].pExpr;
1635     int iCol = pExpr->iColumn;
1636     Vdbe *v = sqlite3GetVdbe(pParse);
1637 
1638     /* This function is only called from two places. In both cases the vdbe
1639     ** has already been allocated. So assume sqlite3GetVdbe() is always
1640     ** successful here.
1641     */
1642     assert(v);
1643     if( iCol<0 ){
1644       int iMem = ++pParse->nMem;
1645       int iAddr;
1646       Table *pTab = p->pSrc->a[0].pTab;
1647       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1648       sqlite3VdbeUsesBtree(v, iDb);
1649 
1650       iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
1651       sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
1652 
1653       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1654       eType = IN_INDEX_ROWID;
1655 
1656       sqlite3VdbeJumpHere(v, iAddr);
1657     }else{
1658       /* The collation sequence used by the comparison. If an index is to
1659       ** be used in place of a temp-table, it must be ordered according
1660       ** to this collation sequence.
1661       */
1662       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1663 
1664       /* Check that the affinity that will be used to perform the
1665       ** comparison is the same as the affinity of the column. If
1666       ** it is not, it is not possible to use any index.
1667       */
1668       Table *pTab = p->pSrc->a[0].pTab;
1669       char aff = comparisonAffinity(pX);
1670       int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1671 
1672       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1673         if( (pIdx->aiColumn[0]==iCol)
1674          && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1675          && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1676         ){
1677           int iDb;
1678           int iMem = ++pParse->nMem;
1679           int iAddr;
1680           char *pKey;
1681 
1682           pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1683           iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1684           sqlite3VdbeUsesBtree(v, iDb);
1685 
1686           iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
1687           sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
1688 
1689           sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
1690           sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
1691                                pKey,P4_KEYINFO_HANDOFF);
1692           VdbeComment((v, "%s", pIdx->zName));
1693           eType = IN_INDEX_INDEX;
1694 
1695           sqlite3VdbeJumpHere(v, iAddr);
1696         }
1697       }
1698     }
1699   }
1700 
1701   if( eType==0 ){
1702     sqlite3CodeSubselect(pParse, pX);
1703     eType = IN_INDEX_EPH;
1704   }else{
1705     pX->iTable = iTab;
1706   }
1707   return eType;
1708 }
1709 #endif
1710 
1711 /*
1712 ** Generate code for scalar subqueries used as an expression
1713 ** and IN operators.  Examples:
1714 **
1715 **     (SELECT a FROM b)          -- subquery
1716 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
1717 **     x IN (4,5,11)              -- IN operator with list on right-hand side
1718 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
1719 **
1720 ** The pExpr parameter describes the expression that contains the IN
1721 ** operator or subquery.
1722 */
1723 #ifndef SQLITE_OMIT_SUBQUERY
1724 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1725   int testAddr = 0;                       /* One-time test address */
1726   Vdbe *v = sqlite3GetVdbe(pParse);
1727   if( v==0 ) return;
1728 
1729 
1730   /* This code must be run in its entirety every time it is encountered
1731   ** if any of the following is true:
1732   **
1733   **    *  The right-hand side is a correlated subquery
1734   **    *  The right-hand side is an expression list containing variables
1735   **    *  We are inside a trigger
1736   **
1737   ** If all of the above are false, then we can run this code just once
1738   ** save the results, and reuse the same result on subsequent invocations.
1739   */
1740   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1741     int mem = ++pParse->nMem;
1742     sqlite3VdbeAddOp1(v, OP_If, mem);
1743     testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
1744     assert( testAddr>0 || pParse->db->mallocFailed );
1745   }
1746 
1747   switch( pExpr->op ){
1748     case TK_IN: {
1749       char affinity;
1750       KeyInfo keyInfo;
1751       int addr;        /* Address of OP_OpenEphemeral instruction */
1752 
1753       affinity = sqlite3ExprAffinity(pExpr->pLeft);
1754 
1755       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1756       ** expression it is handled the same way. A virtual table is
1757       ** filled with single-field index keys representing the results
1758       ** from the SELECT or the <exprlist>.
1759       **
1760       ** If the 'x' expression is a column value, or the SELECT...
1761       ** statement returns a column value, then the affinity of that
1762       ** column is used to build the index keys. If both 'x' and the
1763       ** SELECT... statement are columns, then numeric affinity is used
1764       ** if either column has NUMERIC or INTEGER affinity. If neither
1765       ** 'x' nor the SELECT... statement are columns, then numeric affinity
1766       ** is used.
1767       */
1768       pExpr->iTable = pParse->nTab++;
1769       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
1770       memset(&keyInfo, 0, sizeof(keyInfo));
1771       keyInfo.nField = 1;
1772 
1773       if( pExpr->pSelect ){
1774         /* Case 1:     expr IN (SELECT ...)
1775         **
1776         ** Generate code to write the results of the select into the temporary
1777         ** table allocated and opened above.
1778         */
1779         SelectDest dest;
1780         ExprList *pEList;
1781 
1782         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1783         dest.affinity = (int)affinity;
1784         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
1785         if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
1786           return;
1787         }
1788         pEList = pExpr->pSelect->pEList;
1789         if( pEList && pEList->nExpr>0 ){
1790           keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
1791               pEList->a[0].pExpr);
1792         }
1793       }else if( pExpr->pList ){
1794         /* Case 2:     expr IN (exprlist)
1795         **
1796         ** For each expression, build an index key from the evaluation and
1797         ** store it in the temporary table. If <expr> is a column, then use
1798         ** that columns affinity when building index keys. If <expr> is not
1799         ** a column, use numeric affinity.
1800         */
1801         int i;
1802         ExprList *pList = pExpr->pList;
1803         struct ExprList_item *pItem;
1804         int r1, r2;
1805 
1806         if( !affinity ){
1807           affinity = SQLITE_AFF_NONE;
1808         }
1809         keyInfo.aColl[0] = pExpr->pLeft->pColl;
1810 
1811         /* Loop through each expression in <exprlist>. */
1812         r1 = sqlite3GetTempReg(pParse);
1813         r2 = sqlite3GetTempReg(pParse);
1814         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1815           Expr *pE2 = pItem->pExpr;
1816 
1817           /* If the expression is not constant then we will need to
1818           ** disable the test that was generated above that makes sure
1819           ** this code only executes once.  Because for a non-constant
1820           ** expression we need to rerun this code each time.
1821           */
1822           if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1823             sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
1824             testAddr = 0;
1825           }
1826 
1827           /* Evaluate the expression and insert it into the temp table */
1828           pParse->disableColCache++;
1829           sqlite3ExprCode(pParse, pE2, r1);
1830           assert( pParse->disableColCache>0 );
1831           pParse->disableColCache--;
1832           sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
1833           sqlite3ExprCacheAffinityChange(pParse, r1, 1);
1834           sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
1835         }
1836         sqlite3ReleaseTempReg(pParse, r1);
1837         sqlite3ReleaseTempReg(pParse, r2);
1838       }
1839       sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
1840       break;
1841     }
1842 
1843     case TK_EXISTS:
1844     case TK_SELECT: {
1845       /* This has to be a scalar SELECT.  Generate code to put the
1846       ** value of this select in a memory cell and record the number
1847       ** of the memory cell in iColumn.
1848       */
1849       static const Token one = { (u8*)"1", 0, 1 };
1850       Select *pSel;
1851       SelectDest dest;
1852 
1853       pSel = pExpr->pSelect;
1854       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
1855       if( pExpr->op==TK_SELECT ){
1856         dest.eDest = SRT_Mem;
1857         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
1858         VdbeComment((v, "Init subquery result"));
1859       }else{
1860         dest.eDest = SRT_Exists;
1861         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
1862         VdbeComment((v, "Init EXISTS result"));
1863       }
1864       sqlite3ExprDelete(pSel->pLimit);
1865       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
1866       if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
1867         return;
1868       }
1869       pExpr->iColumn = dest.iParm;
1870       break;
1871     }
1872   }
1873 
1874   if( testAddr ){
1875     sqlite3VdbeJumpHere(v, testAddr-1);
1876   }
1877 
1878   return;
1879 }
1880 #endif /* SQLITE_OMIT_SUBQUERY */
1881 
1882 /*
1883 ** Duplicate an 8-byte value
1884 */
1885 static char *dup8bytes(Vdbe *v, const char *in){
1886   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1887   if( out ){
1888     memcpy(out, in, 8);
1889   }
1890   return out;
1891 }
1892 
1893 /*
1894 ** Generate an instruction that will put the floating point
1895 ** value described by z[0..n-1] into register iMem.
1896 **
1897 ** The z[] string will probably not be zero-terminated.  But the
1898 ** z[n] character is guaranteed to be something that does not look
1899 ** like the continuation of the number.
1900 */
1901 static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
1902   assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1903   if( z ){
1904     double value;
1905     char *zV;
1906     assert( !isdigit(z[n]) );
1907     sqlite3AtoF(z, &value);
1908     if( negateFlag ) value = -value;
1909     zV = dup8bytes(v, (char*)&value);
1910     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
1911   }
1912 }
1913 
1914 
1915 /*
1916 ** Generate an instruction that will put the integer describe by
1917 ** text z[0..n-1] into register iMem.
1918 **
1919 ** The z[] string will probably not be zero-terminated.  But the
1920 ** z[n] character is guaranteed to be something that does not look
1921 ** like the continuation of the number.
1922 */
1923 static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
1924   assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1925   if( z ){
1926     int i;
1927     assert( !isdigit(z[n]) );
1928     if( sqlite3GetInt32(z, &i) ){
1929       if( negFlag ) i = -i;
1930       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1931     }else if( sqlite3FitsIn64Bits(z, negFlag) ){
1932       i64 value;
1933       char *zV;
1934       sqlite3Atoi64(z, &value);
1935       if( negFlag ) value = -value;
1936       zV = dup8bytes(v, (char*)&value);
1937       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
1938     }else{
1939       codeReal(v, z, n, negFlag, iMem);
1940     }
1941   }
1942 }
1943 
1944 
1945 /*
1946 ** Generate code that will extract the iColumn-th column from
1947 ** table pTab and store the column value in a register.  An effort
1948 ** is made to store the column value in register iReg, but this is
1949 ** not guaranteed.  The location of the column value is returned.
1950 **
1951 ** There must be an open cursor to pTab in iTable when this routine
1952 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
1953 **
1954 ** This routine might attempt to reuse the value of the column that
1955 ** has already been loaded into a register.  The value will always
1956 ** be used if it has not undergone any affinity changes.  But if
1957 ** an affinity change has occurred, then the cached value will only be
1958 ** used if allowAffChng is true.
1959 */
1960 int sqlite3ExprCodeGetColumn(
1961   Parse *pParse,   /* Parsing and code generating context */
1962   Table *pTab,     /* Description of the table we are reading from */
1963   int iColumn,     /* Index of the table column */
1964   int iTable,      /* The cursor pointing to the table */
1965   int iReg,        /* Store results here */
1966   int allowAffChng /* True if prior affinity changes are OK */
1967 ){
1968   Vdbe *v = pParse->pVdbe;
1969   int i;
1970   struct yColCache *p;
1971 
1972   for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
1973     if( p->iTable==iTable && p->iColumn==iColumn
1974            && (!p->affChange || allowAffChng) ){
1975 #if 0
1976       sqlite3VdbeAddOp0(v, OP_Noop);
1977       VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
1978 #endif
1979       return p->iReg;
1980     }
1981   }
1982   assert( v!=0 );
1983   if( iColumn<0 ){
1984     int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1985     sqlite3VdbeAddOp2(v, op, iTable, iReg);
1986   }else if( pTab==0 ){
1987     sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
1988   }else{
1989     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1990     sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
1991     sqlite3ColumnDefault(v, pTab, iColumn);
1992 #ifndef SQLITE_OMIT_FLOATING_POINT
1993     if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1994       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
1995     }
1996 #endif
1997   }
1998   if( pParse->disableColCache==0 ){
1999     i = pParse->iColCache;
2000     p = &pParse->aColCache[i];
2001     p->iTable = iTable;
2002     p->iColumn = iColumn;
2003     p->iReg = iReg;
2004     p->affChange = 0;
2005     i++;
2006     if( i>=ArraySize(pParse->aColCache) ) i = 0;
2007     if( i>pParse->nColCache ) pParse->nColCache = i;
2008     pParse->iColCache = i;
2009   }
2010   return iReg;
2011 }
2012 
2013 /*
2014 ** Clear all column cache entries associated with the vdbe
2015 ** cursor with cursor number iTable.
2016 */
2017 void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
2018   if( iTable<0 ){
2019     pParse->nColCache = 0;
2020     pParse->iColCache = 0;
2021   }else{
2022     int i;
2023     for(i=0; i<pParse->nColCache; i++){
2024       if( pParse->aColCache[i].iTable==iTable ){
2025         testcase( i==pParse->nColCache-1 );
2026         pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2027         pParse->iColCache = pParse->nColCache;
2028       }
2029     }
2030   }
2031 }
2032 
2033 /*
2034 ** Record the fact that an affinity change has occurred on iCount
2035 ** registers starting with iStart.
2036 */
2037 void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
2038   int iEnd = iStart + iCount - 1;
2039   int i;
2040   for(i=0; i<pParse->nColCache; i++){
2041     int r = pParse->aColCache[i].iReg;
2042     if( r>=iStart && r<=iEnd ){
2043       pParse->aColCache[i].affChange = 1;
2044     }
2045   }
2046 }
2047 
2048 /*
2049 ** Generate code to moves content from one register to another.
2050 ** Keep the column cache up-to-date.
2051 */
2052 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){
2053   int i;
2054   if( iFrom==iTo ) return;
2055   sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo);
2056   for(i=0; i<pParse->nColCache; i++){
2057     if( pParse->aColCache[i].iReg==iFrom ){
2058       pParse->aColCache[i].iReg = iTo;
2059     }
2060   }
2061 }
2062 
2063 /*
2064 ** Return true if any register in the range iFrom..iTo (inclusive)
2065 ** is used as part of the column cache.
2066 */
2067 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2068   int i;
2069   for(i=0; i<pParse->nColCache; i++){
2070     int r = pParse->aColCache[i].iReg;
2071     if( r>=iFrom && r<=iTo ) return 1;
2072   }
2073   return 0;
2074 }
2075 
2076 /*
2077 ** Theres is a value in register iCurrent.  We ultimately want
2078 ** the value to be in register iTarget.  It might be that
2079 ** iCurrent and iTarget are the same register.
2080 **
2081 ** We are going to modify the value, so we need to make sure it
2082 ** is not a cached register.  If iCurrent is a cached register,
2083 ** then try to move the value over to iTarget.  If iTarget is a
2084 ** cached register, then clear the corresponding cache line.
2085 **
2086 ** Return the register that the value ends up in.
2087 */
2088 int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
2089   int i;
2090   assert( pParse->pVdbe!=0 );
2091   if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2092     return iCurrent;
2093   }
2094   if( iCurrent!=iTarget ){
2095     sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2096   }
2097   for(i=0; i<pParse->nColCache; i++){
2098     if( pParse->aColCache[i].iReg==iTarget ){
2099       pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2100       pParse->iColCache = pParse->nColCache;
2101     }
2102   }
2103   return iTarget;
2104 }
2105 
2106 /*
2107 ** Generate code into the current Vdbe to evaluate the given
2108 ** expression.  Attempt to store the results in register "target".
2109 ** Return the register where results are stored.
2110 **
2111 ** With this routine, there is no guaranteed that results will
2112 ** be stored in target.  The result might be stored in some other
2113 ** register if it is convenient to do so.  The calling function
2114 ** must check the return code and move the results to the desired
2115 ** register.
2116 */
2117 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
2118   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
2119   int op;                   /* The opcode being coded */
2120   int inReg = target;       /* Results stored in register inReg */
2121   int regFree1 = 0;         /* If non-zero free this temporary register */
2122   int regFree2 = 0;         /* If non-zero free this temporary register */
2123   int r1, r2, r3, r4;       /* Various register numbers */
2124 
2125   assert( v!=0 || pParse->db->mallocFailed );
2126   assert( target>0 && target<=pParse->nMem );
2127   if( v==0 ) return 0;
2128 
2129   if( pExpr==0 ){
2130     op = TK_NULL;
2131   }else{
2132     op = pExpr->op;
2133   }
2134   switch( op ){
2135     case TK_AGG_COLUMN: {
2136       AggInfo *pAggInfo = pExpr->pAggInfo;
2137       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2138       if( !pAggInfo->directMode ){
2139         assert( pCol->iMem>0 );
2140         inReg = pCol->iMem;
2141         break;
2142       }else if( pAggInfo->useSortingIdx ){
2143         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2144                               pCol->iSorterColumn, target);
2145         break;
2146       }
2147       /* Otherwise, fall thru into the TK_COLUMN case */
2148     }
2149     case TK_COLUMN: {
2150       if( pExpr->iTable<0 ){
2151         /* This only happens when coding check constraints */
2152         assert( pParse->ckBase>0 );
2153         inReg = pExpr->iColumn + pParse->ckBase;
2154       }else{
2155         testcase( (pExpr->flags & EP_AnyAff)!=0 );
2156         inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
2157                                  pExpr->iColumn, pExpr->iTable, target,
2158                                  pExpr->flags & EP_AnyAff);
2159       }
2160       break;
2161     }
2162     case TK_INTEGER: {
2163       codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
2164       break;
2165     }
2166     case TK_FLOAT: {
2167       codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
2168       break;
2169     }
2170     case TK_STRING: {
2171       sqlite3DequoteExpr(pParse->db, pExpr);
2172       sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
2173                         (char*)pExpr->token.z, pExpr->token.n);
2174       break;
2175     }
2176     case TK_NULL: {
2177       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2178       break;
2179     }
2180 #ifndef SQLITE_OMIT_BLOB_LITERAL
2181     case TK_BLOB: {
2182       int n;
2183       const char *z;
2184       char *zBlob;
2185       assert( pExpr->token.n>=3 );
2186       assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2187       assert( pExpr->token.z[1]=='\'' );
2188       assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
2189       n = pExpr->token.n - 3;
2190       z = (char*)pExpr->token.z + 2;
2191       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2192       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
2193       break;
2194     }
2195 #endif
2196     case TK_VARIABLE: {
2197       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
2198       if( pExpr->token.n>1 ){
2199         sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
2200       }
2201       break;
2202     }
2203     case TK_REGISTER: {
2204       inReg = pExpr->iTable;
2205       break;
2206     }
2207 #ifndef SQLITE_OMIT_CAST
2208     case TK_CAST: {
2209       /* Expressions of the form:   CAST(pLeft AS token) */
2210       int aff, to_op;
2211       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2212       aff = sqlite3AffinityType(&pExpr->token);
2213       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2214       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
2215       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
2216       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2217       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
2218       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
2219       testcase( to_op==OP_ToText );
2220       testcase( to_op==OP_ToBlob );
2221       testcase( to_op==OP_ToNumeric );
2222       testcase( to_op==OP_ToInt );
2223       testcase( to_op==OP_ToReal );
2224       sqlite3VdbeAddOp1(v, to_op, inReg);
2225       testcase( usedAsColumnCache(pParse, inReg, inReg) );
2226       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
2227       break;
2228     }
2229 #endif /* SQLITE_OMIT_CAST */
2230     case TK_LT:
2231     case TK_LE:
2232     case TK_GT:
2233     case TK_GE:
2234     case TK_NE:
2235     case TK_EQ: {
2236       assert( TK_LT==OP_Lt );
2237       assert( TK_LE==OP_Le );
2238       assert( TK_GT==OP_Gt );
2239       assert( TK_GE==OP_Ge );
2240       assert( TK_EQ==OP_Eq );
2241       assert( TK_NE==OP_Ne );
2242       testcase( op==TK_LT );
2243       testcase( op==TK_LE );
2244       testcase( op==TK_GT );
2245       testcase( op==TK_GE );
2246       testcase( op==TK_EQ );
2247       testcase( op==TK_NE );
2248       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2249                                   pExpr->pRight, &r2, &regFree2);
2250       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2251                   r1, r2, inReg, SQLITE_STOREP2);
2252       testcase( regFree1==0 );
2253       testcase( regFree2==0 );
2254       break;
2255     }
2256     case TK_AND:
2257     case TK_OR:
2258     case TK_PLUS:
2259     case TK_STAR:
2260     case TK_MINUS:
2261     case TK_REM:
2262     case TK_BITAND:
2263     case TK_BITOR:
2264     case TK_SLASH:
2265     case TK_LSHIFT:
2266     case TK_RSHIFT:
2267     case TK_CONCAT: {
2268       assert( TK_AND==OP_And );
2269       assert( TK_OR==OP_Or );
2270       assert( TK_PLUS==OP_Add );
2271       assert( TK_MINUS==OP_Subtract );
2272       assert( TK_REM==OP_Remainder );
2273       assert( TK_BITAND==OP_BitAnd );
2274       assert( TK_BITOR==OP_BitOr );
2275       assert( TK_SLASH==OP_Divide );
2276       assert( TK_LSHIFT==OP_ShiftLeft );
2277       assert( TK_RSHIFT==OP_ShiftRight );
2278       assert( TK_CONCAT==OP_Concat );
2279       testcase( op==TK_AND );
2280       testcase( op==TK_OR );
2281       testcase( op==TK_PLUS );
2282       testcase( op==TK_MINUS );
2283       testcase( op==TK_REM );
2284       testcase( op==TK_BITAND );
2285       testcase( op==TK_BITOR );
2286       testcase( op==TK_SLASH );
2287       testcase( op==TK_LSHIFT );
2288       testcase( op==TK_RSHIFT );
2289       testcase( op==TK_CONCAT );
2290       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2291       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
2292       sqlite3VdbeAddOp3(v, op, r2, r1, target);
2293       testcase( regFree1==0 );
2294       testcase( regFree2==0 );
2295       break;
2296     }
2297     case TK_UMINUS: {
2298       Expr *pLeft = pExpr->pLeft;
2299       assert( pLeft );
2300       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2301         Token *p = &pLeft->token;
2302         if( pLeft->op==TK_FLOAT ){
2303           codeReal(v, (char*)p->z, p->n, 1, target);
2304         }else{
2305           codeInteger(v, (char*)p->z, p->n, 1, target);
2306         }
2307       }else{
2308         regFree1 = r1 = sqlite3GetTempReg(pParse);
2309         sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
2310         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
2311         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
2312         testcase( regFree2==0 );
2313       }
2314       inReg = target;
2315       break;
2316     }
2317     case TK_BITNOT:
2318     case TK_NOT: {
2319       assert( TK_BITNOT==OP_BitNot );
2320       assert( TK_NOT==OP_Not );
2321       testcase( op==TK_BITNOT );
2322       testcase( op==TK_NOT );
2323       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2324       testcase( inReg==target );
2325       testcase( usedAsColumnCache(pParse, inReg, inReg) );
2326       inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
2327       sqlite3VdbeAddOp1(v, op, inReg);
2328       break;
2329     }
2330     case TK_ISNULL:
2331     case TK_NOTNULL: {
2332       int addr;
2333       assert( TK_ISNULL==OP_IsNull );
2334       assert( TK_NOTNULL==OP_NotNull );
2335       testcase( op==TK_ISNULL );
2336       testcase( op==TK_NOTNULL );
2337       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
2338       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2339       testcase( regFree1==0 );
2340       addr = sqlite3VdbeAddOp1(v, op, r1);
2341       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
2342       sqlite3VdbeJumpHere(v, addr);
2343       break;
2344     }
2345     case TK_AGG_FUNCTION: {
2346       AggInfo *pInfo = pExpr->pAggInfo;
2347       if( pInfo==0 ){
2348         sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2349             &pExpr->span);
2350       }else{
2351         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
2352       }
2353       break;
2354     }
2355     case TK_CONST_FUNC:
2356     case TK_FUNCTION: {
2357       ExprList *pList = pExpr->pList;
2358       int nExpr = pList ? pList->nExpr : 0;
2359       FuncDef *pDef;
2360       int nId;
2361       const char *zId;
2362       int constMask = 0;
2363       int i;
2364       sqlite3 *db = pParse->db;
2365       u8 enc = ENC(db);
2366       CollSeq *pColl = 0;
2367 
2368       testcase( op==TK_CONST_FUNC );
2369       testcase( op==TK_FUNCTION );
2370       zId = (char*)pExpr->token.z;
2371       nId = pExpr->token.n;
2372       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
2373       assert( pDef!=0 );
2374       if( pList ){
2375         nExpr = pList->nExpr;
2376         r1 = sqlite3GetTempRange(pParse, nExpr);
2377         sqlite3ExprCodeExprList(pParse, pList, r1);
2378       }else{
2379         nExpr = r1 = 0;
2380       }
2381 #ifndef SQLITE_OMIT_VIRTUALTABLE
2382       /* Possibly overload the function if the first argument is
2383       ** a virtual table column.
2384       **
2385       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2386       ** second argument, not the first, as the argument to test to
2387       ** see if it is a column in a virtual table.  This is done because
2388       ** the left operand of infix functions (the operand we want to
2389       ** control overloading) ends up as the second argument to the
2390       ** function.  The expression "A glob B" is equivalent to
2391       ** "glob(B,A).  We want to use the A in "A glob B" to test
2392       ** for function overloading.  But we use the B term in "glob(B,A)".
2393       */
2394       if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
2395         pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
2396       }else if( nExpr>0 ){
2397         pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
2398       }
2399 #endif
2400       for(i=0; i<nExpr && i<32; i++){
2401         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
2402           constMask |= (1<<i);
2403         }
2404         if( pDef->needCollSeq && !pColl ){
2405           pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2406         }
2407       }
2408       if( pDef->needCollSeq ){
2409         if( !pColl ) pColl = pParse->db->pDfltColl;
2410         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
2411       }
2412       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
2413                         (char*)pDef, P4_FUNCDEF);
2414       sqlite3VdbeChangeP5(v, nExpr);
2415       if( nExpr ){
2416         sqlite3ReleaseTempRange(pParse, r1, nExpr);
2417       }
2418       sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
2419       break;
2420     }
2421 #ifndef SQLITE_OMIT_SUBQUERY
2422     case TK_EXISTS:
2423     case TK_SELECT: {
2424       testcase( op==TK_EXISTS );
2425       testcase( op==TK_SELECT );
2426       if( pExpr->iColumn==0 ){
2427         sqlite3CodeSubselect(pParse, pExpr);
2428       }
2429       inReg = pExpr->iColumn;
2430       break;
2431     }
2432     case TK_IN: {
2433       int j1, j2, j3, j4, j5;
2434       char affinity;
2435       int eType;
2436 
2437       eType = sqlite3FindInIndex(pParse, pExpr, 0);
2438 
2439       /* Figure out the affinity to use to create a key from the results
2440       ** of the expression. affinityStr stores a static string suitable for
2441       ** P4 of OP_MakeRecord.
2442       */
2443       affinity = comparisonAffinity(pExpr);
2444 
2445       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
2446 
2447       /* Code the <expr> from "<expr> IN (...)". The temporary table
2448       ** pExpr->iTable contains the values that make up the (...) set.
2449       */
2450       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2451       testcase( regFree1==0 );
2452       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2453       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2454       j2  = sqlite3VdbeAddOp0(v, OP_Goto);
2455       sqlite3VdbeJumpHere(v, j1);
2456       if( eType==IN_INDEX_ROWID ){
2457         j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1);
2458         j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
2459         j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2460         sqlite3VdbeJumpHere(v, j3);
2461         sqlite3VdbeJumpHere(v, j4);
2462       }else{
2463         r2 = regFree2 = sqlite3GetTempReg(pParse);
2464         sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
2465         sqlite3ExprCacheAffinityChange(pParse, r1, 1);
2466         j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
2467       }
2468       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
2469       sqlite3VdbeJumpHere(v, j2);
2470       sqlite3VdbeJumpHere(v, j5);
2471       break;
2472     }
2473 #endif
2474     /*
2475     **    x BETWEEN y AND z
2476     **
2477     ** This is equivalent to
2478     **
2479     **    x>=y AND x<=z
2480     **
2481     ** X is stored in pExpr->pLeft.
2482     ** Y is stored in pExpr->pList->a[0].pExpr.
2483     ** Z is stored in pExpr->pList->a[1].pExpr.
2484     */
2485     case TK_BETWEEN: {
2486       Expr *pLeft = pExpr->pLeft;
2487       struct ExprList_item *pLItem = pExpr->pList->a;
2488       Expr *pRight = pLItem->pExpr;
2489 
2490       codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2491                                   pRight, &r2, &regFree2);
2492       testcase( regFree1==0 );
2493       testcase( regFree2==0 );
2494       r3 = sqlite3GetTempReg(pParse);
2495       r4 = sqlite3GetTempReg(pParse);
2496       codeCompare(pParse, pLeft, pRight, OP_Ge,
2497                   r1, r2, r3, SQLITE_STOREP2);
2498       pLItem++;
2499       pRight = pLItem->pExpr;
2500       sqlite3ReleaseTempReg(pParse, regFree2);
2501       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
2502       testcase( regFree2==0 );
2503       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2504       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
2505       sqlite3ReleaseTempReg(pParse, r3);
2506       sqlite3ReleaseTempReg(pParse, r4);
2507       break;
2508     }
2509     case TK_UPLUS: {
2510       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2511       break;
2512     }
2513 
2514     /*
2515     ** Form A:
2516     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2517     **
2518     ** Form B:
2519     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2520     **
2521     ** Form A is can be transformed into the equivalent form B as follows:
2522     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2523     **        WHEN x=eN THEN rN ELSE y END
2524     **
2525     ** X (if it exists) is in pExpr->pLeft.
2526     ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
2527     ** ELSE clause and no other term matches, then the result of the
2528     ** exprssion is NULL.
2529     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2530     **
2531     ** The result of the expression is the Ri for the first matching Ei,
2532     ** or if there is no matching Ei, the ELSE term Y, or if there is
2533     ** no ELSE term, NULL.
2534     */
2535     case TK_CASE: {
2536       int endLabel;                     /* GOTO label for end of CASE stmt */
2537       int nextCase;                     /* GOTO label for next WHEN clause */
2538       int nExpr;                        /* 2x number of WHEN terms */
2539       int i;                            /* Loop counter */
2540       ExprList *pEList;                 /* List of WHEN terms */
2541       struct ExprList_item *aListelem;  /* Array of WHEN terms */
2542       Expr opCompare;                   /* The X==Ei expression */
2543       Expr cacheX;                      /* Cached expression X */
2544       Expr *pX;                         /* The X expression */
2545       Expr *pTest;                      /* X==Ei (form A) or just Ei (form B) */
2546 
2547       assert(pExpr->pList);
2548       assert((pExpr->pList->nExpr % 2) == 0);
2549       assert(pExpr->pList->nExpr > 0);
2550       pEList = pExpr->pList;
2551       aListelem = pEList->a;
2552       nExpr = pEList->nExpr;
2553       endLabel = sqlite3VdbeMakeLabel(v);
2554       if( (pX = pExpr->pLeft)!=0 ){
2555         cacheX = *pX;
2556         testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
2557         cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
2558         testcase( regFree1==0 );
2559         cacheX.op = TK_REGISTER;
2560         cacheX.iColumn = 0;
2561         opCompare.op = TK_EQ;
2562         opCompare.pLeft = &cacheX;
2563         pTest = &opCompare;
2564       }
2565       pParse->disableColCache++;
2566       for(i=0; i<nExpr; i=i+2){
2567         if( pX ){
2568           opCompare.pRight = aListelem[i].pExpr;
2569         }else{
2570           pTest = aListelem[i].pExpr;
2571         }
2572         nextCase = sqlite3VdbeMakeLabel(v);
2573         testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
2574         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
2575         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2576         testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
2577         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
2578         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2579         sqlite3VdbeResolveLabel(v, nextCase);
2580       }
2581       if( pExpr->pRight ){
2582         sqlite3ExprCode(pParse, pExpr->pRight, target);
2583       }else{
2584         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2585       }
2586       sqlite3VdbeResolveLabel(v, endLabel);
2587       assert( pParse->disableColCache>0 );
2588       pParse->disableColCache--;
2589       break;
2590     }
2591 #ifndef SQLITE_OMIT_TRIGGER
2592     case TK_RAISE: {
2593       if( !pParse->trigStack ){
2594         sqlite3ErrorMsg(pParse,
2595                        "RAISE() may only be used within a trigger-program");
2596         return 0;
2597       }
2598       if( pExpr->iColumn!=OE_Ignore ){
2599          assert( pExpr->iColumn==OE_Rollback ||
2600                  pExpr->iColumn == OE_Abort ||
2601                  pExpr->iColumn == OE_Fail );
2602          sqlite3DequoteExpr(pParse->db, pExpr);
2603          sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
2604                         (char*)pExpr->token.z, pExpr->token.n);
2605       } else {
2606          assert( pExpr->iColumn == OE_Ignore );
2607          sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2608          sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2609          VdbeComment((v, "raise(IGNORE)"));
2610       }
2611       break;
2612     }
2613 #endif
2614   }
2615   sqlite3ReleaseTempReg(pParse, regFree1);
2616   sqlite3ReleaseTempReg(pParse, regFree2);
2617   return inReg;
2618 }
2619 
2620 /*
2621 ** Generate code to evaluate an expression and store the results
2622 ** into a register.  Return the register number where the results
2623 ** are stored.
2624 **
2625 ** If the register is a temporary register that can be deallocated,
2626 ** then write its number into *pReg.  If the result register is not
2627 ** a temporary, then set *pReg to zero.
2628 */
2629 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2630   int r1 = sqlite3GetTempReg(pParse);
2631   int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2632   if( r2==r1 ){
2633     *pReg = r1;
2634   }else{
2635     sqlite3ReleaseTempReg(pParse, r1);
2636     *pReg = 0;
2637   }
2638   return r2;
2639 }
2640 
2641 /*
2642 ** Generate code that will evaluate expression pExpr and store the
2643 ** results in register target.  The results are guaranteed to appear
2644 ** in register target.
2645 */
2646 int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
2647   int inReg;
2648 
2649   assert( target>0 && target<=pParse->nMem );
2650   inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
2651   assert( pParse->pVdbe || pParse->db->mallocFailed );
2652   if( inReg!=target && pParse->pVdbe ){
2653     sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
2654   }
2655   return target;
2656 }
2657 
2658 /*
2659 ** Generate code that evalutes the given expression and puts the result
2660 ** in register target.
2661 **
2662 ** Also make a copy of the expression results into another "cache" register
2663 ** and modify the expression so that the next time it is evaluated,
2664 ** the result is a copy of the cache register.
2665 **
2666 ** This routine is used for expressions that are used multiple
2667 ** times.  They are evaluated once and the results of the expression
2668 ** are reused.
2669 */
2670 int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
2671   Vdbe *v = pParse->pVdbe;
2672   int inReg;
2673   inReg = sqlite3ExprCode(pParse, pExpr, target);
2674   assert( target>0 );
2675   if( pExpr->op!=TK_REGISTER ){
2676     int iMem;
2677     iMem = ++pParse->nMem;
2678     sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
2679     pExpr->iTable = iMem;
2680     pExpr->iColumn = pExpr->op;
2681     pExpr->op = TK_REGISTER;
2682   }
2683   return inReg;
2684 }
2685 
2686 /*
2687 ** Return TRUE if pExpr is an constant expression that is appropriate
2688 ** for factoring out of a loop.  Appropriate expressions are:
2689 **
2690 **    *  Any expression that evaluates to two or more opcodes.
2691 **
2692 **    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2693 **       or OP_Variable that does not need to be placed in a
2694 **       specific register.
2695 **
2696 ** There is no point in factoring out single-instruction constant
2697 ** expressions that need to be placed in a particular register.
2698 ** We could factor them out, but then we would end up adding an
2699 ** OP_SCopy instruction to move the value into the correct register
2700 ** later.  We might as well just use the original instruction and
2701 ** avoid the OP_SCopy.
2702 */
2703 static int isAppropriateForFactoring(Expr *p){
2704   if( !sqlite3ExprIsConstantNotJoin(p) ){
2705     return 0;  /* Only constant expressions are appropriate for factoring */
2706   }
2707   if( (p->flags & EP_FixedDest)==0 ){
2708     return 1;  /* Any constant without a fixed destination is appropriate */
2709   }
2710   while( p->op==TK_UPLUS ) p = p->pLeft;
2711   switch( p->op ){
2712 #ifndef SQLITE_OMIT_BLOB_LITERAL
2713     case TK_BLOB:
2714 #endif
2715     case TK_VARIABLE:
2716     case TK_INTEGER:
2717     case TK_FLOAT:
2718     case TK_NULL:
2719     case TK_STRING: {
2720       testcase( p->op==TK_BLOB );
2721       testcase( p->op==TK_VARIABLE );
2722       testcase( p->op==TK_INTEGER );
2723       testcase( p->op==TK_FLOAT );
2724       testcase( p->op==TK_NULL );
2725       testcase( p->op==TK_STRING );
2726       /* Single-instruction constants with a fixed destination are
2727       ** better done in-line.  If we factor them, they will just end
2728       ** up generating an OP_SCopy to move the value to the destination
2729       ** register. */
2730       return 0;
2731     }
2732     case TK_UMINUS: {
2733        if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2734          return 0;
2735        }
2736        break;
2737     }
2738     default: {
2739       break;
2740     }
2741   }
2742   return 1;
2743 }
2744 
2745 /*
2746 ** If pExpr is a constant expression that is appropriate for
2747 ** factoring out of a loop, then evaluate the expression
2748 ** into a register and convert the expression into a TK_REGISTER
2749 ** expression.
2750 */
2751 static int evalConstExpr(void *pArg, Expr *pExpr){
2752   Parse *pParse = (Parse*)pArg;
2753   switch( pExpr->op ){
2754     case TK_REGISTER: {
2755       return 1;
2756     }
2757     case TK_FUNCTION:
2758     case TK_AGG_FUNCTION:
2759     case TK_CONST_FUNC: {
2760       /* The arguments to a function have a fixed destination.
2761       ** Mark them this way to avoid generated unneeded OP_SCopy
2762       ** instructions.
2763       */
2764       ExprList *pList = pExpr->pList;
2765       if( pList ){
2766         int i = pList->nExpr;
2767         struct ExprList_item *pItem = pList->a;
2768         for(; i>0; i--, pItem++){
2769           if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2770         }
2771       }
2772       break;
2773     }
2774   }
2775   if( isAppropriateForFactoring(pExpr) ){
2776     int r1 = ++pParse->nMem;
2777     int r2;
2778     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2779     if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
2780     pExpr->iColumn = pExpr->op;
2781     pExpr->op = TK_REGISTER;
2782     pExpr->iTable = r2;
2783     return 1;
2784   }
2785   return 0;
2786 }
2787 
2788 /*
2789 ** Preevaluate constant subexpressions within pExpr and store the
2790 ** results in registers.  Modify pExpr so that the constant subexpresions
2791 ** are TK_REGISTER opcodes that refer to the precomputed values.
2792 */
2793 void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2794    walkExprTree(pExpr, evalConstExpr, pParse);
2795 }
2796 
2797 
2798 /*
2799 ** Generate code that pushes the value of every element of the given
2800 ** expression list into a sequence of registers beginning at target.
2801 **
2802 ** Return the number of elements evaluated.
2803 */
2804 int sqlite3ExprCodeExprList(
2805   Parse *pParse,     /* Parsing context */
2806   ExprList *pList,   /* The expression list to be coded */
2807   int target         /* Where to write results */
2808 ){
2809   struct ExprList_item *pItem;
2810   int i, n;
2811   assert( pList!=0 || pParse->db->mallocFailed );
2812   if( pList==0 ){
2813     return 0;
2814   }
2815   assert( target>0 );
2816   n = pList->nExpr;
2817   for(pItem=pList->a, i=n; i>0; i--, pItem++){
2818     sqlite3ExprCode(pParse, pItem->pExpr, target);
2819     target++;
2820   }
2821   return n;
2822 }
2823 
2824 /*
2825 ** Generate code for a boolean expression such that a jump is made
2826 ** to the label "dest" if the expression is true but execution
2827 ** continues straight thru if the expression is false.
2828 **
2829 ** If the expression evaluates to NULL (neither true nor false), then
2830 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
2831 **
2832 ** This code depends on the fact that certain token values (ex: TK_EQ)
2833 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2834 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
2835 ** the make process cause these values to align.  Assert()s in the code
2836 ** below verify that the numbers are aligned correctly.
2837 */
2838 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
2839   Vdbe *v = pParse->pVdbe;
2840   int op = 0;
2841   int regFree1 = 0;
2842   int regFree2 = 0;
2843   int r1, r2;
2844 
2845   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
2846   if( v==0 || pExpr==0 ) return;
2847   op = pExpr->op;
2848   switch( op ){
2849     case TK_AND: {
2850       int d2 = sqlite3VdbeMakeLabel(v);
2851       testcase( jumpIfNull==0 );
2852       testcase( pParse->disableColCache==0 );
2853       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
2854       pParse->disableColCache++;
2855       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2856       assert( pParse->disableColCache>0 );
2857       pParse->disableColCache--;
2858       sqlite3VdbeResolveLabel(v, d2);
2859       break;
2860     }
2861     case TK_OR: {
2862       testcase( jumpIfNull==0 );
2863       testcase( pParse->disableColCache==0 );
2864       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2865       pParse->disableColCache++;
2866       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2867       assert( pParse->disableColCache>0 );
2868       pParse->disableColCache--;
2869       break;
2870     }
2871     case TK_NOT: {
2872       testcase( jumpIfNull==0 );
2873       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2874       break;
2875     }
2876     case TK_LT:
2877     case TK_LE:
2878     case TK_GT:
2879     case TK_GE:
2880     case TK_NE:
2881     case TK_EQ: {
2882       assert( TK_LT==OP_Lt );
2883       assert( TK_LE==OP_Le );
2884       assert( TK_GT==OP_Gt );
2885       assert( TK_GE==OP_Ge );
2886       assert( TK_EQ==OP_Eq );
2887       assert( TK_NE==OP_Ne );
2888       testcase( op==TK_LT );
2889       testcase( op==TK_LE );
2890       testcase( op==TK_GT );
2891       testcase( op==TK_GE );
2892       testcase( op==TK_EQ );
2893       testcase( op==TK_NE );
2894       testcase( jumpIfNull==0 );
2895       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2896                                   pExpr->pRight, &r2, &regFree2);
2897       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2898                   r1, r2, dest, jumpIfNull);
2899       testcase( regFree1==0 );
2900       testcase( regFree2==0 );
2901       break;
2902     }
2903     case TK_ISNULL:
2904     case TK_NOTNULL: {
2905       assert( TK_ISNULL==OP_IsNull );
2906       assert( TK_NOTNULL==OP_NotNull );
2907       testcase( op==TK_ISNULL );
2908       testcase( op==TK_NOTNULL );
2909       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2910       sqlite3VdbeAddOp2(v, op, r1, dest);
2911       testcase( regFree1==0 );
2912       break;
2913     }
2914     case TK_BETWEEN: {
2915       /*    x BETWEEN y AND z
2916       **
2917       ** Is equivalent to
2918       **
2919       **    x>=y AND x<=z
2920       **
2921       ** Code it as such, taking care to do the common subexpression
2922       ** elementation of x.
2923       */
2924       Expr exprAnd;
2925       Expr compLeft;
2926       Expr compRight;
2927       Expr exprX;
2928 
2929       exprX = *pExpr->pLeft;
2930       exprAnd.op = TK_AND;
2931       exprAnd.pLeft = &compLeft;
2932       exprAnd.pRight = &compRight;
2933       compLeft.op = TK_GE;
2934       compLeft.pLeft = &exprX;
2935       compLeft.pRight = pExpr->pList->a[0].pExpr;
2936       compRight.op = TK_LE;
2937       compRight.pLeft = &exprX;
2938       compRight.pRight = pExpr->pList->a[1].pExpr;
2939       exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2940       testcase( regFree1==0 );
2941       exprX.op = TK_REGISTER;
2942       testcase( jumpIfNull==0 );
2943       sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
2944       break;
2945     }
2946     default: {
2947       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2948       sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
2949       testcase( regFree1==0 );
2950       testcase( jumpIfNull==0 );
2951       break;
2952     }
2953   }
2954   sqlite3ReleaseTempReg(pParse, regFree1);
2955   sqlite3ReleaseTempReg(pParse, regFree2);
2956 }
2957 
2958 /*
2959 ** Generate code for a boolean expression such that a jump is made
2960 ** to the label "dest" if the expression is false but execution
2961 ** continues straight thru if the expression is true.
2962 **
2963 ** If the expression evaluates to NULL (neither true nor false) then
2964 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2965 ** is 0.
2966 */
2967 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
2968   Vdbe *v = pParse->pVdbe;
2969   int op = 0;
2970   int regFree1 = 0;
2971   int regFree2 = 0;
2972   int r1, r2;
2973 
2974   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
2975   if( v==0 || pExpr==0 ) return;
2976 
2977   /* The value of pExpr->op and op are related as follows:
2978   **
2979   **       pExpr->op            op
2980   **       ---------          ----------
2981   **       TK_ISNULL          OP_NotNull
2982   **       TK_NOTNULL         OP_IsNull
2983   **       TK_NE              OP_Eq
2984   **       TK_EQ              OP_Ne
2985   **       TK_GT              OP_Le
2986   **       TK_LE              OP_Gt
2987   **       TK_GE              OP_Lt
2988   **       TK_LT              OP_Ge
2989   **
2990   ** For other values of pExpr->op, op is undefined and unused.
2991   ** The value of TK_ and OP_ constants are arranged such that we
2992   ** can compute the mapping above using the following expression.
2993   ** Assert()s verify that the computation is correct.
2994   */
2995   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2996 
2997   /* Verify correct alignment of TK_ and OP_ constants
2998   */
2999   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
3000   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
3001   assert( pExpr->op!=TK_NE || op==OP_Eq );
3002   assert( pExpr->op!=TK_EQ || op==OP_Ne );
3003   assert( pExpr->op!=TK_LT || op==OP_Ge );
3004   assert( pExpr->op!=TK_LE || op==OP_Gt );
3005   assert( pExpr->op!=TK_GT || op==OP_Le );
3006   assert( pExpr->op!=TK_GE || op==OP_Lt );
3007 
3008   switch( pExpr->op ){
3009     case TK_AND: {
3010       testcase( jumpIfNull==0 );
3011       testcase( pParse->disableColCache==0 );
3012       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
3013       pParse->disableColCache++;
3014       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
3015       assert( pParse->disableColCache>0 );
3016       pParse->disableColCache--;
3017       break;
3018     }
3019     case TK_OR: {
3020       int d2 = sqlite3VdbeMakeLabel(v);
3021       testcase( jumpIfNull==0 );
3022       testcase( pParse->disableColCache==0 );
3023       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
3024       pParse->disableColCache++;
3025       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
3026       assert( pParse->disableColCache>0 );
3027       pParse->disableColCache--;
3028       sqlite3VdbeResolveLabel(v, d2);
3029       break;
3030     }
3031     case TK_NOT: {
3032       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
3033       break;
3034     }
3035     case TK_LT:
3036     case TK_LE:
3037     case TK_GT:
3038     case TK_GE:
3039     case TK_NE:
3040     case TK_EQ: {
3041       testcase( op==TK_LT );
3042       testcase( op==TK_LE );
3043       testcase( op==TK_GT );
3044       testcase( op==TK_GE );
3045       testcase( op==TK_EQ );
3046       testcase( op==TK_NE );
3047       testcase( jumpIfNull==0 );
3048       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
3049                                   pExpr->pRight, &r2, &regFree2);
3050       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
3051                   r1, r2, dest, jumpIfNull);
3052       testcase( regFree1==0 );
3053       testcase( regFree2==0 );
3054       break;
3055     }
3056     case TK_ISNULL:
3057     case TK_NOTNULL: {
3058       testcase( op==TK_ISNULL );
3059       testcase( op==TK_NOTNULL );
3060       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3061       sqlite3VdbeAddOp2(v, op, r1, dest);
3062       testcase( regFree1==0 );
3063       break;
3064     }
3065     case TK_BETWEEN: {
3066       /*    x BETWEEN y AND z
3067       **
3068       ** Is equivalent to
3069       **
3070       **    x>=y AND x<=z
3071       **
3072       ** Code it as such, taking care to do the common subexpression
3073       ** elementation of x.
3074       */
3075       Expr exprAnd;
3076       Expr compLeft;
3077       Expr compRight;
3078       Expr exprX;
3079 
3080       exprX = *pExpr->pLeft;
3081       exprAnd.op = TK_AND;
3082       exprAnd.pLeft = &compLeft;
3083       exprAnd.pRight = &compRight;
3084       compLeft.op = TK_GE;
3085       compLeft.pLeft = &exprX;
3086       compLeft.pRight = pExpr->pList->a[0].pExpr;
3087       compRight.op = TK_LE;
3088       compRight.pLeft = &exprX;
3089       compRight.pRight = pExpr->pList->a[1].pExpr;
3090       exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
3091       testcase( regFree1==0 );
3092       exprX.op = TK_REGISTER;
3093       testcase( jumpIfNull==0 );
3094       sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
3095       break;
3096     }
3097     default: {
3098       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3099       sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
3100       testcase( regFree1==0 );
3101       testcase( jumpIfNull==0 );
3102       break;
3103     }
3104   }
3105   sqlite3ReleaseTempReg(pParse, regFree1);
3106   sqlite3ReleaseTempReg(pParse, regFree2);
3107 }
3108 
3109 /*
3110 ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
3111 ** if they are identical and return FALSE if they differ in any way.
3112 **
3113 ** Sometimes this routine will return FALSE even if the two expressions
3114 ** really are equivalent.  If we cannot prove that the expressions are
3115 ** identical, we return FALSE just to be safe.  So if this routine
3116 ** returns false, then you do not really know for certain if the two
3117 ** expressions are the same.  But if you get a TRUE return, then you
3118 ** can be sure the expressions are the same.  In the places where
3119 ** this routine is used, it does not hurt to get an extra FALSE - that
3120 ** just might result in some slightly slower code.  But returning
3121 ** an incorrect TRUE could lead to a malfunction.
3122 */
3123 int sqlite3ExprCompare(Expr *pA, Expr *pB){
3124   int i;
3125   if( pA==0||pB==0 ){
3126     return pB==pA;
3127   }
3128   if( pA->op!=pB->op ) return 0;
3129   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
3130   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
3131   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
3132   if( pA->pList ){
3133     if( pB->pList==0 ) return 0;
3134     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
3135     for(i=0; i<pA->pList->nExpr; i++){
3136       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
3137         return 0;
3138       }
3139     }
3140   }else if( pB->pList ){
3141     return 0;
3142   }
3143   if( pA->pSelect || pB->pSelect ) return 0;
3144   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
3145   if( pA->op!=TK_COLUMN && pA->token.z ){
3146     if( pB->token.z==0 ) return 0;
3147     if( pB->token.n!=pA->token.n ) return 0;
3148     if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
3149       return 0;
3150     }
3151   }
3152   return 1;
3153 }
3154 
3155 
3156 /*
3157 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
3158 ** the new element.  Return a negative number if malloc fails.
3159 */
3160 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
3161   int i;
3162   pInfo->aCol = sqlite3ArrayAllocate(
3163        db,
3164        pInfo->aCol,
3165        sizeof(pInfo->aCol[0]),
3166        3,
3167        &pInfo->nColumn,
3168        &pInfo->nColumnAlloc,
3169        &i
3170   );
3171   return i;
3172 }
3173 
3174 /*
3175 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
3176 ** the new element.  Return a negative number if malloc fails.
3177 */
3178 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
3179   int i;
3180   pInfo->aFunc = sqlite3ArrayAllocate(
3181        db,
3182        pInfo->aFunc,
3183        sizeof(pInfo->aFunc[0]),
3184        3,
3185        &pInfo->nFunc,
3186        &pInfo->nFuncAlloc,
3187        &i
3188   );
3189   return i;
3190 }
3191 
3192 /*
3193 ** This is an xFunc for walkExprTree() used to implement
3194 ** sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
3195 ** for additional information.
3196 **
3197 ** This routine analyzes the aggregate function at pExpr.
3198 */
3199 static int analyzeAggregate(void *pArg, Expr *pExpr){
3200   int i;
3201   NameContext *pNC = (NameContext *)pArg;
3202   Parse *pParse = pNC->pParse;
3203   SrcList *pSrcList = pNC->pSrcList;
3204   AggInfo *pAggInfo = pNC->pAggInfo;
3205 
3206   switch( pExpr->op ){
3207     case TK_AGG_COLUMN:
3208     case TK_COLUMN: {
3209       /* Check to see if the column is in one of the tables in the FROM
3210       ** clause of the aggregate query */
3211       if( pSrcList ){
3212         struct SrcList_item *pItem = pSrcList->a;
3213         for(i=0; i<pSrcList->nSrc; i++, pItem++){
3214           struct AggInfo_col *pCol;
3215           if( pExpr->iTable==pItem->iCursor ){
3216             /* If we reach this point, it means that pExpr refers to a table
3217             ** that is in the FROM clause of the aggregate query.
3218             **
3219             ** Make an entry for the column in pAggInfo->aCol[] if there
3220             ** is not an entry there already.
3221             */
3222             int k;
3223             pCol = pAggInfo->aCol;
3224             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
3225               if( pCol->iTable==pExpr->iTable &&
3226                   pCol->iColumn==pExpr->iColumn ){
3227                 break;
3228               }
3229             }
3230             if( (k>=pAggInfo->nColumn)
3231              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3232             ){
3233               pCol = &pAggInfo->aCol[k];
3234               pCol->pTab = pExpr->pTab;
3235               pCol->iTable = pExpr->iTable;
3236               pCol->iColumn = pExpr->iColumn;
3237               pCol->iMem = ++pParse->nMem;
3238               pCol->iSorterColumn = -1;
3239               pCol->pExpr = pExpr;
3240               if( pAggInfo->pGroupBy ){
3241                 int j, n;
3242                 ExprList *pGB = pAggInfo->pGroupBy;
3243                 struct ExprList_item *pTerm = pGB->a;
3244                 n = pGB->nExpr;
3245                 for(j=0; j<n; j++, pTerm++){
3246                   Expr *pE = pTerm->pExpr;
3247                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3248                       pE->iColumn==pExpr->iColumn ){
3249                     pCol->iSorterColumn = j;
3250                     break;
3251                   }
3252                 }
3253               }
3254               if( pCol->iSorterColumn<0 ){
3255                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3256               }
3257             }
3258             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3259             ** because it was there before or because we just created it).
3260             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3261             ** pAggInfo->aCol[] entry.
3262             */
3263             pExpr->pAggInfo = pAggInfo;
3264             pExpr->op = TK_AGG_COLUMN;
3265             pExpr->iAgg = k;
3266             break;
3267           } /* endif pExpr->iTable==pItem->iCursor */
3268         } /* end loop over pSrcList */
3269       }
3270       return 1;
3271     }
3272     case TK_AGG_FUNCTION: {
3273       /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3274       ** to be ignored */
3275       if( pNC->nDepth==0 ){
3276         /* Check to see if pExpr is a duplicate of another aggregate
3277         ** function that is already in the pAggInfo structure
3278         */
3279         struct AggInfo_func *pItem = pAggInfo->aFunc;
3280         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3281           if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
3282             break;
3283           }
3284         }
3285         if( i>=pAggInfo->nFunc ){
3286           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
3287           */
3288           u8 enc = ENC(pParse->db);
3289           i = addAggInfoFunc(pParse->db, pAggInfo);
3290           if( i>=0 ){
3291             pItem = &pAggInfo->aFunc[i];
3292             pItem->pExpr = pExpr;
3293             pItem->iMem = ++pParse->nMem;
3294             pItem->pFunc = sqlite3FindFunction(pParse->db,
3295                    (char*)pExpr->token.z, pExpr->token.n,
3296                    pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
3297             if( pExpr->flags & EP_Distinct ){
3298               pItem->iDistinct = pParse->nTab++;
3299             }else{
3300               pItem->iDistinct = -1;
3301             }
3302           }
3303         }
3304         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3305         */
3306         pExpr->iAgg = i;
3307         pExpr->pAggInfo = pAggInfo;
3308         return 1;
3309       }
3310     }
3311   }
3312 
3313   /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3314   ** to be changed to TK_AGG_COLUMN.  But increment nDepth so that
3315   ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3316   */
3317   if( pExpr->pSelect ){
3318     pNC->nDepth++;
3319     walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3320     pNC->nDepth--;
3321   }
3322   return 0;
3323 }
3324 
3325 /*
3326 ** Analyze the given expression looking for aggregate functions and
3327 ** for variables that need to be added to the pParse->aAgg[] array.
3328 ** Make additional entries to the pParse->aAgg[] array as necessary.
3329 **
3330 ** This routine should only be called after the expression has been
3331 ** analyzed by sqlite3ExprResolveNames().
3332 */
3333 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
3334   walkExprTree(pExpr, analyzeAggregate, pNC);
3335 }
3336 
3337 /*
3338 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3339 ** expression list.  Return the number of errors.
3340 **
3341 ** If an error is found, the analysis is cut short.
3342 */
3343 void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
3344   struct ExprList_item *pItem;
3345   int i;
3346   if( pList ){
3347     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3348       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
3349     }
3350   }
3351 }
3352 
3353 /*
3354 ** Allocate or deallocate temporary use registers during code generation.
3355 */
3356 int sqlite3GetTempReg(Parse *pParse){
3357   int i, r;
3358   if( pParse->nTempReg==0 ){
3359     return ++pParse->nMem;
3360   }
3361   for(i=0; i<pParse->nTempReg; i++){
3362     r = pParse->aTempReg[i];
3363     if( usedAsColumnCache(pParse, r, r) ) continue;
3364   }
3365   if( i>=pParse->nTempReg ){
3366     return ++pParse->nMem;
3367   }
3368   while( i<pParse->nTempReg-1 ){
3369     pParse->aTempReg[i] = pParse->aTempReg[i+1];
3370   }
3371   pParse->nTempReg--;
3372   return r;
3373 }
3374 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
3375   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
3376     pParse->aTempReg[pParse->nTempReg++] = iReg;
3377   }
3378 }
3379 
3380 /*
3381 ** Allocate or deallocate a block of nReg consecutive registers
3382 */
3383 int sqlite3GetTempRange(Parse *pParse, int nReg){
3384   int i, n;
3385   i = pParse->iRangeReg;
3386   n = pParse->nRangeReg;
3387   if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
3388     pParse->iRangeReg += nReg;
3389     pParse->nRangeReg -= nReg;
3390   }else{
3391     i = pParse->nMem+1;
3392     pParse->nMem += nReg;
3393   }
3394   return i;
3395 }
3396 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3397   if( nReg>pParse->nRangeReg ){
3398     pParse->nRangeReg = nReg;
3399     pParse->iRangeReg = iReg;
3400   }
3401 }
3402