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