xref: /sqlite-3.40.0/src/expr.c (revision 4c755c0f)
1cce7d176Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
3cce7d176Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
6cce7d176Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
10cce7d176Sdrh **
11cce7d176Sdrh *************************************************************************
121ccde15dSdrh ** This file contains routines used for analyzing expressions and
13b19a2bc6Sdrh ** for generating VDBE code that evaluates expressions in SQLite.
14cce7d176Sdrh **
15*4c755c0fSdrh ** $Id: expr.c,v 1.154 2004/08/08 20:22:17 drh Exp $
16cce7d176Sdrh */
17cce7d176Sdrh #include "sqliteInt.h"
1804738cb9Sdrh #include <ctype.h>
19a2e00042Sdrh 
20e014a838Sdanielk1977 char const *sqlite3AffinityString(char affinity){
21e014a838Sdanielk1977   switch( affinity ){
22e014a838Sdanielk1977     case SQLITE_AFF_INTEGER: return "i";
23e014a838Sdanielk1977     case SQLITE_AFF_NUMERIC: return "n";
24e014a838Sdanielk1977     case SQLITE_AFF_TEXT:    return "t";
25e014a838Sdanielk1977     case SQLITE_AFF_NONE:    return "o";
26e014a838Sdanielk1977     default:
27e014a838Sdanielk1977       assert(0);
28e014a838Sdanielk1977   }
29e0d4b060Sdanielk1977   return 0;
30e014a838Sdanielk1977 }
31e014a838Sdanielk1977 
32e014a838Sdanielk1977 
33e014a838Sdanielk1977 /*
34e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any.
35e014a838Sdanielk1977 **
36e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias,
37e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the
38e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned,
39e014a838Sdanielk1977 ** indicating no affinity for the expression.
40e014a838Sdanielk1977 **
41e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all
42e014a838Sdanielk1977 ** have an affinity:
43e014a838Sdanielk1977 **
44e014a838Sdanielk1977 ** CREATE TABLE t1(a);
45e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a;
46e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b;
47e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1);
48e014a838Sdanielk1977 */
49bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){
50a37cdde0Sdanielk1977   if( pExpr->op==TK_AS ){
51bf3b721fSdanielk1977     return sqlite3ExprAffinity(pExpr->pLeft);
52a37cdde0Sdanielk1977   }
53a37cdde0Sdanielk1977   if( pExpr->op==TK_SELECT ){
54bf3b721fSdanielk1977     return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
55a37cdde0Sdanielk1977   }
56a37cdde0Sdanielk1977   return pExpr->affinity;
57a37cdde0Sdanielk1977 }
58a37cdde0Sdanielk1977 
5953db1458Sdrh /*
600202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If
610202b29eSdanielk1977 ** there is no default collation type, return 0.
620202b29eSdanielk1977 */
637cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
647cedc8d4Sdanielk1977   CollSeq *pColl = 0;
650202b29eSdanielk1977   if( pExpr ){
667cedc8d4Sdanielk1977     pColl = pExpr->pColl;
677cedc8d4Sdanielk1977     if( pExpr->op==TK_AS && !pColl ){
687cedc8d4Sdanielk1977       return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
690202b29eSdanielk1977     }
700202b29eSdanielk1977   }
717cedc8d4Sdanielk1977   if( sqlite3CheckCollSeq(pParse, pColl) ){
727cedc8d4Sdanielk1977     pColl = 0;
737cedc8d4Sdanielk1977   }
747cedc8d4Sdanielk1977   return pColl;
750202b29eSdanielk1977 }
760202b29eSdanielk1977 
770202b29eSdanielk1977 /*
7853db1458Sdrh ** pExpr is the left operand of a comparison operator.  aff2 is the
7953db1458Sdrh ** type affinity of the right operand.  This routine returns the
8053db1458Sdrh ** type affinity that should be used for the comparison operator.
8153db1458Sdrh */
82e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){
83bf3b721fSdanielk1977   char aff1 = sqlite3ExprAffinity(pExpr);
84e014a838Sdanielk1977   if( aff1 && aff2 ){
85e014a838Sdanielk1977     /* Both sides of the comparison are columns. If one has numeric or
86e014a838Sdanielk1977     ** integer affinity, use that. Otherwise use no affinity.
87e014a838Sdanielk1977     */
88e014a838Sdanielk1977     if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
89e014a838Sdanielk1977       return SQLITE_AFF_INTEGER;
90e014a838Sdanielk1977     }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
91e014a838Sdanielk1977       return SQLITE_AFF_NUMERIC;
92e014a838Sdanielk1977     }else{
93e014a838Sdanielk1977       return SQLITE_AFF_NONE;
94e014a838Sdanielk1977     }
95e014a838Sdanielk1977   }else if( !aff1 && !aff2 ){
965f6a87b3Sdrh     /* Neither side of the comparison is a column.  Compare the
975f6a87b3Sdrh     ** results directly.
98e014a838Sdanielk1977     */
995f6a87b3Sdrh     /* return SQLITE_AFF_NUMERIC;  // Ticket #805 */
1005f6a87b3Sdrh     return SQLITE_AFF_NONE;
101e014a838Sdanielk1977   }else{
102e014a838Sdanielk1977     /* One side is a column, the other is not. Use the columns affinity. */
103e014a838Sdanielk1977     return (aff1 + aff2);
104e014a838Sdanielk1977   }
105e014a838Sdanielk1977 }
106e014a838Sdanielk1977 
10753db1458Sdrh /*
10853db1458Sdrh ** pExpr is a comparison operator.  Return the type affinity that should
10953db1458Sdrh ** be applied to both operands prior to doing the comparison.
11053db1458Sdrh */
111e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){
112e014a838Sdanielk1977   char aff;
113e014a838Sdanielk1977   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
114e014a838Sdanielk1977           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
115e014a838Sdanielk1977           pExpr->op==TK_NE );
116e014a838Sdanielk1977   assert( pExpr->pLeft );
117bf3b721fSdanielk1977   aff = sqlite3ExprAffinity(pExpr->pLeft);
118e014a838Sdanielk1977   if( pExpr->pRight ){
119e014a838Sdanielk1977     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
120e014a838Sdanielk1977   }
121e014a838Sdanielk1977   else if( pExpr->pSelect ){
122e014a838Sdanielk1977     aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
123e014a838Sdanielk1977   }
124e014a838Sdanielk1977   else if( !aff ){
125e014a838Sdanielk1977     aff = SQLITE_AFF_NUMERIC;
126e014a838Sdanielk1977   }
127e014a838Sdanielk1977   return aff;
128e014a838Sdanielk1977 }
129e014a838Sdanielk1977 
130e014a838Sdanielk1977 /*
131e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
132e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true
133e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement
134e014a838Sdanielk1977 ** the comparison in pExpr.
135e014a838Sdanielk1977 */
136e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
137e014a838Sdanielk1977   char aff = comparisonAffinity(pExpr);
138e014a838Sdanielk1977   return
139e014a838Sdanielk1977     (aff==SQLITE_AFF_NONE) ||
140e014a838Sdanielk1977     (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
141e014a838Sdanielk1977     (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
142e014a838Sdanielk1977     (aff==idx_affinity);
143e014a838Sdanielk1977 }
144e014a838Sdanielk1977 
145a37cdde0Sdanielk1977 /*
146a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison
147a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
148a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned
149a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression
150a37cdde0Sdanielk1977 ** evaluates to NULL.
151a37cdde0Sdanielk1977 */
152e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
153bf3b721fSdanielk1977   char aff = sqlite3ExprAffinity(pExpr2);
154e014a838Sdanielk1977   return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
155a37cdde0Sdanielk1977 }
156a37cdde0Sdanielk1977 
157a2e00042Sdrh /*
1580202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by
1590202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight.
1600202b29eSdanielk1977 **
1610202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is
1620202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression
1630202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating
1640202b29eSdanielk1977 ** type.
1650202b29eSdanielk1977 */
1667cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
1677cedc8d4Sdanielk1977   CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
1680202b29eSdanielk1977   if( !pColl ){
1697cedc8d4Sdanielk1977     pColl = sqlite3ExprCollSeq(pParse, pRight);
1700202b29eSdanielk1977   }
1710202b29eSdanielk1977   return pColl;
1720202b29eSdanielk1977 }
1730202b29eSdanielk1977 
1740202b29eSdanielk1977 /*
175be5c89acSdrh ** Generate code for a comparison operator.
176be5c89acSdrh */
177be5c89acSdrh static int codeCompare(
178be5c89acSdrh   Parse *pParse,    /* The parsing (and code generating) context */
179be5c89acSdrh   Expr *pLeft,      /* The left operand */
180be5c89acSdrh   Expr *pRight,     /* The right operand */
181be5c89acSdrh   int opcode,       /* The comparison opcode */
182be5c89acSdrh   int dest,         /* Jump here if true.  */
183be5c89acSdrh   int jumpIfNull    /* If true, jump if either operand is NULL */
184be5c89acSdrh ){
185be5c89acSdrh   int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
186be5c89acSdrh   CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
187be5c89acSdrh   return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void *)p3, P3_COLLSEQ);
188be5c89acSdrh }
189be5c89acSdrh 
190be5c89acSdrh /*
191a76b5dfcSdrh ** Construct a new expression node and return a pointer to it.  Memory
192a76b5dfcSdrh ** for this node is obtained from sqliteMalloc().  The calling function
193a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed.
194a76b5dfcSdrh */
1954adee20fSdanielk1977 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
196a76b5dfcSdrh   Expr *pNew;
197a76b5dfcSdrh   pNew = sqliteMalloc( sizeof(Expr) );
198a76b5dfcSdrh   if( pNew==0 ){
1994efc4754Sdrh     /* When malloc fails, we leak memory from pLeft and pRight */
200a76b5dfcSdrh     return 0;
201a76b5dfcSdrh   }
202a76b5dfcSdrh   pNew->op = op;
203a76b5dfcSdrh   pNew->pLeft = pLeft;
204a76b5dfcSdrh   pNew->pRight = pRight;
205a76b5dfcSdrh   if( pToken ){
2064b59ab5eSdrh     assert( pToken->dyn==0 );
207a76b5dfcSdrh     pNew->token = *pToken;
2086977fea8Sdrh     pNew->span = *pToken;
209a76b5dfcSdrh   }else{
2104efc4754Sdrh     assert( pNew->token.dyn==0 );
2114efc4754Sdrh     assert( pNew->token.z==0 );
2124efc4754Sdrh     assert( pNew->token.n==0 );
2136977fea8Sdrh     if( pLeft && pRight ){
2144adee20fSdanielk1977       sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
2156977fea8Sdrh     }else{
2166977fea8Sdrh       pNew->span = pNew->token;
2176977fea8Sdrh     }
218a76b5dfcSdrh   }
219a76b5dfcSdrh   return pNew;
220a76b5dfcSdrh }
221a76b5dfcSdrh 
222a76b5dfcSdrh /*
2236977fea8Sdrh ** Set the Expr.span field of the given expression to span all
224a76b5dfcSdrh ** text between the two given tokens.
225a76b5dfcSdrh */
2264adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
2274efc4754Sdrh   assert( pRight!=0 );
2284efc4754Sdrh   assert( pLeft!=0 );
2294efc4754Sdrh   /* Note: pExpr might be NULL due to a prior malloc failure */
2304efc4754Sdrh   if( pExpr && pRight->z && pLeft->z ){
2314b59ab5eSdrh     if( pLeft->dyn==0 && pRight->dyn==0 ){
2326977fea8Sdrh       pExpr->span.z = pLeft->z;
2336977fea8Sdrh       pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
2344b59ab5eSdrh     }else{
2356977fea8Sdrh       pExpr->span.z = 0;
2364b59ab5eSdrh     }
237a76b5dfcSdrh   }
238a76b5dfcSdrh }
239a76b5dfcSdrh 
240a76b5dfcSdrh /*
241a76b5dfcSdrh ** Construct a new expression node for a function with multiple
242a76b5dfcSdrh ** arguments.
243a76b5dfcSdrh */
2444adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
245a76b5dfcSdrh   Expr *pNew;
246a76b5dfcSdrh   pNew = sqliteMalloc( sizeof(Expr) );
247a76b5dfcSdrh   if( pNew==0 ){
2484adee20fSdanielk1977     /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
249a76b5dfcSdrh     return 0;
250a76b5dfcSdrh   }
251a76b5dfcSdrh   pNew->op = TK_FUNCTION;
252a76b5dfcSdrh   pNew->pList = pList;
253a76b5dfcSdrh   if( pToken ){
2544b59ab5eSdrh     assert( pToken->dyn==0 );
255a76b5dfcSdrh     pNew->token = *pToken;
256a76b5dfcSdrh   }else{
257a76b5dfcSdrh     pNew->token.z = 0;
258a76b5dfcSdrh   }
2596977fea8Sdrh   pNew->span = pNew->token;
260a76b5dfcSdrh   return pNew;
261a76b5dfcSdrh }
262a76b5dfcSdrh 
263a76b5dfcSdrh /*
264a2e00042Sdrh ** Recursively delete an expression tree.
265a2e00042Sdrh */
2664adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){
267a2e00042Sdrh   if( p==0 ) return;
2684efc4754Sdrh   if( p->span.dyn ) sqliteFree((char*)p->span.z);
2694efc4754Sdrh   if( p->token.dyn ) sqliteFree((char*)p->token.z);
2704adee20fSdanielk1977   sqlite3ExprDelete(p->pLeft);
2714adee20fSdanielk1977   sqlite3ExprDelete(p->pRight);
2724adee20fSdanielk1977   sqlite3ExprListDelete(p->pList);
2734adee20fSdanielk1977   sqlite3SelectDelete(p->pSelect);
274a2e00042Sdrh   sqliteFree(p);
275a2e00042Sdrh }
276a2e00042Sdrh 
277a76b5dfcSdrh 
278a76b5dfcSdrh /*
279ff78bd2fSdrh ** The following group of routines make deep copies of expressions,
280ff78bd2fSdrh ** expression lists, ID lists, and select statements.  The copies can
281ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines)
282ff78bd2fSdrh ** without effecting the originals.
283ff78bd2fSdrh **
2844adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
2854adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
286ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines.
287ff78bd2fSdrh **
288ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated.
289ff78bd2fSdrh */
2904adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){
291ff78bd2fSdrh   Expr *pNew;
292ff78bd2fSdrh   if( p==0 ) return 0;
293fcb78a49Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
294ff78bd2fSdrh   if( pNew==0 ) return 0;
2953b167c75Sdrh   memcpy(pNew, p, sizeof(*pNew));
2966977fea8Sdrh   if( p->token.z!=0 ){
2974b59ab5eSdrh     pNew->token.z = sqliteStrDup(p->token.z);
2984b59ab5eSdrh     pNew->token.dyn = 1;
2994b59ab5eSdrh   }else{
3004efc4754Sdrh     assert( pNew->token.z==0 );
3014b59ab5eSdrh   }
3026977fea8Sdrh   pNew->span.z = 0;
3034adee20fSdanielk1977   pNew->pLeft = sqlite3ExprDup(p->pLeft);
3044adee20fSdanielk1977   pNew->pRight = sqlite3ExprDup(p->pRight);
3054adee20fSdanielk1977   pNew->pList = sqlite3ExprListDup(p->pList);
3064adee20fSdanielk1977   pNew->pSelect = sqlite3SelectDup(p->pSelect);
307ff78bd2fSdrh   return pNew;
308ff78bd2fSdrh }
3094adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){
3104b59ab5eSdrh   if( pTo->dyn ) sqliteFree((char*)pTo->z);
3114b59ab5eSdrh   if( pFrom->z ){
3124b59ab5eSdrh     pTo->n = pFrom->n;
3134b59ab5eSdrh     pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
3144b59ab5eSdrh     pTo->dyn = 1;
3154b59ab5eSdrh   }else{
3164b59ab5eSdrh     pTo->z = 0;
3174b59ab5eSdrh   }
3184b59ab5eSdrh }
3194adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){
320ff78bd2fSdrh   ExprList *pNew;
3213e7bc9caSdrh   struct ExprList_item *pItem;
322ff78bd2fSdrh   int i;
323ff78bd2fSdrh   if( p==0 ) return 0;
324ff78bd2fSdrh   pNew = sqliteMalloc( sizeof(*pNew) );
325ff78bd2fSdrh   if( pNew==0 ) return 0;
3264305d103Sdrh   pNew->nExpr = pNew->nAlloc = p->nExpr;
3273e7bc9caSdrh   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
328e0048400Sdanielk1977   if( pItem==0 ){
329e0048400Sdanielk1977     sqliteFree(pNew);
330e0048400Sdanielk1977     return 0;
331e0048400Sdanielk1977   }
3321bdd9b57Sdrh   for(i=0; i<p->nExpr; i++, pItem++){
3334b59ab5eSdrh     Expr *pNewExpr, *pOldExpr;
3344adee20fSdanielk1977     pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
3356977fea8Sdrh     if( pOldExpr->span.z!=0 && pNewExpr ){
3366977fea8Sdrh       /* Always make a copy of the span for top-level expressions in the
3374b59ab5eSdrh       ** expression list.  The logic in SELECT processing that determines
3384b59ab5eSdrh       ** the names of columns in the result set needs this information */
3394adee20fSdanielk1977       sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
3404b59ab5eSdrh     }
3411f3e905cSdrh     assert( pNewExpr==0 || pNewExpr->span.z!=0
34224b03fd0Sdanielk1977             || pOldExpr->span.z==0 || sqlite3_malloc_failed );
3433e7bc9caSdrh     pItem->zName = sqliteStrDup(p->a[i].zName);
3443e7bc9caSdrh     pItem->sortOrder = p->a[i].sortOrder;
3453e7bc9caSdrh     pItem->isAgg = p->a[i].isAgg;
3463e7bc9caSdrh     pItem->done = 0;
347ff78bd2fSdrh   }
348ff78bd2fSdrh   return pNew;
349ff78bd2fSdrh }
3504adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){
351ad3cab52Sdrh   SrcList *pNew;
352ad3cab52Sdrh   int i;
353113088ecSdrh   int nByte;
354ad3cab52Sdrh   if( p==0 ) return 0;
355113088ecSdrh   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
3564efc4754Sdrh   pNew = sqliteMallocRaw( nByte );
357ad3cab52Sdrh   if( pNew==0 ) return 0;
3584305d103Sdrh   pNew->nSrc = pNew->nAlloc = p->nSrc;
359ad3cab52Sdrh   for(i=0; i<p->nSrc; i++){
3604efc4754Sdrh     struct SrcList_item *pNewItem = &pNew->a[i];
3614efc4754Sdrh     struct SrcList_item *pOldItem = &p->a[i];
3624efc4754Sdrh     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
3634efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
3644efc4754Sdrh     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
3654efc4754Sdrh     pNewItem->jointype = pOldItem->jointype;
3664efc4754Sdrh     pNewItem->iCursor = pOldItem->iCursor;
3674efc4754Sdrh     pNewItem->pTab = 0;
3684adee20fSdanielk1977     pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
3694adee20fSdanielk1977     pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
3704adee20fSdanielk1977     pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
371ad3cab52Sdrh   }
372ad3cab52Sdrh   return pNew;
373ad3cab52Sdrh }
3744adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){
375ff78bd2fSdrh   IdList *pNew;
376ff78bd2fSdrh   int i;
377ff78bd2fSdrh   if( p==0 ) return 0;
3784efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*pNew) );
379ff78bd2fSdrh   if( pNew==0 ) return 0;
3804305d103Sdrh   pNew->nId = pNew->nAlloc = p->nId;
3814efc4754Sdrh   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
382e4697f5eSdrh   if( pNew->a==0 ) return 0;
383ff78bd2fSdrh   for(i=0; i<p->nId; i++){
3844efc4754Sdrh     struct IdList_item *pNewItem = &pNew->a[i];
3854efc4754Sdrh     struct IdList_item *pOldItem = &p->a[i];
3864efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
3874efc4754Sdrh     pNewItem->idx = pOldItem->idx;
388ff78bd2fSdrh   }
389ff78bd2fSdrh   return pNew;
390ff78bd2fSdrh }
3914adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){
392ff78bd2fSdrh   Select *pNew;
393ff78bd2fSdrh   if( p==0 ) return 0;
3944efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
395ff78bd2fSdrh   if( pNew==0 ) return 0;
396ff78bd2fSdrh   pNew->isDistinct = p->isDistinct;
3974adee20fSdanielk1977   pNew->pEList = sqlite3ExprListDup(p->pEList);
3984adee20fSdanielk1977   pNew->pSrc = sqlite3SrcListDup(p->pSrc);
3994adee20fSdanielk1977   pNew->pWhere = sqlite3ExprDup(p->pWhere);
4004adee20fSdanielk1977   pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
4014adee20fSdanielk1977   pNew->pHaving = sqlite3ExprDup(p->pHaving);
4024adee20fSdanielk1977   pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
403ff78bd2fSdrh   pNew->op = p->op;
4044adee20fSdanielk1977   pNew->pPrior = sqlite3SelectDup(p->pPrior);
405ff78bd2fSdrh   pNew->nLimit = p->nLimit;
406ff78bd2fSdrh   pNew->nOffset = p->nOffset;
407ff78bd2fSdrh   pNew->zSelect = 0;
4087b58daeaSdrh   pNew->iLimit = -1;
4097b58daeaSdrh   pNew->iOffset = -1;
410dc1bdc4fSdanielk1977   pNew->ppOpenTemp = 0;
411ff78bd2fSdrh   return pNew;
412ff78bd2fSdrh }
413ff78bd2fSdrh 
414ff78bd2fSdrh 
415ff78bd2fSdrh /*
416a76b5dfcSdrh ** Add a new element to the end of an expression list.  If pList is
417a76b5dfcSdrh ** initially NULL, then create a new expression list.
418a76b5dfcSdrh */
4194adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
420a76b5dfcSdrh   if( pList==0 ){
421a76b5dfcSdrh     pList = sqliteMalloc( sizeof(ExprList) );
422a76b5dfcSdrh     if( pList==0 ){
4234adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
424a76b5dfcSdrh       return 0;
425a76b5dfcSdrh     }
4264efc4754Sdrh     assert( pList->nAlloc==0 );
427a76b5dfcSdrh   }
4284305d103Sdrh   if( pList->nAlloc<=pList->nExpr ){
4294305d103Sdrh     pList->nAlloc = pList->nAlloc*2 + 4;
4304efc4754Sdrh     pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
4314efc4754Sdrh     if( pList->a==0 ){
4324adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
4334efc4754Sdrh       pList->nExpr = pList->nAlloc = 0;
434a76b5dfcSdrh       return pList;
435a76b5dfcSdrh     }
436a76b5dfcSdrh   }
4374efc4754Sdrh   assert( pList->a!=0 );
4384efc4754Sdrh   if( pExpr || pName ){
4394efc4754Sdrh     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
4404efc4754Sdrh     memset(pItem, 0, sizeof(*pItem));
4414efc4754Sdrh     pItem->pExpr = pExpr;
442a99db3b6Sdrh     pItem->zName = sqlite3NameFromToken(pName);
443a76b5dfcSdrh   }
444a76b5dfcSdrh   return pList;
445a76b5dfcSdrh }
446a76b5dfcSdrh 
447a76b5dfcSdrh /*
448a76b5dfcSdrh ** Delete an entire expression list.
449a76b5dfcSdrh */
4504adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){
451a76b5dfcSdrh   int i;
452be5c89acSdrh   struct ExprList_item *pItem;
453a76b5dfcSdrh   if( pList==0 ) return;
4541bdd9b57Sdrh   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
4551bdd9b57Sdrh   assert( pList->nExpr<=pList->nAlloc );
456be5c89acSdrh   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
457be5c89acSdrh     sqlite3ExprDelete(pItem->pExpr);
458be5c89acSdrh     sqliteFree(pItem->zName);
459a76b5dfcSdrh   }
460a76b5dfcSdrh   sqliteFree(pList->a);
461a76b5dfcSdrh   sqliteFree(pList);
462a76b5dfcSdrh }
463a76b5dfcSdrh 
464a76b5dfcSdrh /*
465fef5208cSdrh ** Walk an expression tree.  Return 1 if the expression is constant
466fef5208cSdrh ** and 0 if it involves variables.
4672398937bSdrh **
4682398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc")
4692398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is
4702398937bSdrh ** a constant.
471fef5208cSdrh */
4724adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){
473fef5208cSdrh   switch( p->op ){
474fef5208cSdrh     case TK_ID:
475967e8b73Sdrh     case TK_COLUMN:
476fef5208cSdrh     case TK_DOT:
4777bdc0c1dSdrh     case TK_FUNCTION:
478fef5208cSdrh       return 0;
4797bdc0c1dSdrh     case TK_NULL:
4802398937bSdrh     case TK_STRING:
481c572ef7fSdanielk1977     case TK_BLOB:
4829208643dSdrh     case TK_INTEGER:
4839208643dSdrh     case TK_FLOAT:
48450457896Sdrh     case TK_VARIABLE:
4859208643dSdrh       return 1;
486fef5208cSdrh     default: {
4874adee20fSdanielk1977       if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
4884adee20fSdanielk1977       if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
489fef5208cSdrh       if( p->pList ){
490fef5208cSdrh         int i;
491fef5208cSdrh         for(i=0; i<p->pList->nExpr; i++){
4924adee20fSdanielk1977           if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
493fef5208cSdrh         }
494fef5208cSdrh       }
4959208643dSdrh       return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
496fef5208cSdrh     }
497fef5208cSdrh   }
4989208643dSdrh   return 0;
499fef5208cSdrh }
500fef5208cSdrh 
501fef5208cSdrh /*
502202b2df7Sdrh ** If the given expression codes a constant integer that is small enough
503202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer
504202b2df7Sdrh ** in *pValue.  If the expression is not an integer or if it is too big
505202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
506e4de1febSdrh */
5074adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){
508e4de1febSdrh   switch( p->op ){
509e4de1febSdrh     case TK_INTEGER: {
510fec19aadSdrh       if( sqlite3GetInt32(p->token.z, pValue) ){
511e4de1febSdrh         return 1;
512e4de1febSdrh       }
513202b2df7Sdrh       break;
514202b2df7Sdrh     }
515e4de1febSdrh     case TK_STRING: {
516*4c755c0fSdrh       const u8 *z = (u8*)p->token.z;
517e4de1febSdrh       int n = p->token.n;
518bd790ee3Sdrh       if( n>0 && z[0]=='-' ){ z++; n--; }
519e4de1febSdrh       while( n>0 && *z && isdigit(*z) ){ z++; n--; }
520fec19aadSdrh       if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
521e4de1febSdrh         return 1;
522e4de1febSdrh       }
523e4de1febSdrh       break;
524e4de1febSdrh     }
5254b59ab5eSdrh     case TK_UPLUS: {
5264adee20fSdanielk1977       return sqlite3ExprIsInteger(p->pLeft, pValue);
5274b59ab5eSdrh     }
528e4de1febSdrh     case TK_UMINUS: {
529e4de1febSdrh       int v;
5304adee20fSdanielk1977       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
531e4de1febSdrh         *pValue = -v;
532e4de1febSdrh         return 1;
533e4de1febSdrh       }
534e4de1febSdrh       break;
535e4de1febSdrh     }
536e4de1febSdrh     default: break;
537e4de1febSdrh   }
538e4de1febSdrh   return 0;
539e4de1febSdrh }
540e4de1febSdrh 
541e4de1febSdrh /*
542c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name.
543c4a3c779Sdrh */
5444adee20fSdanielk1977 int sqlite3IsRowid(const char *z){
5454adee20fSdanielk1977   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
5464adee20fSdanielk1977   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
5474adee20fSdanielk1977   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
548c4a3c779Sdrh   return 0;
549c4a3c779Sdrh }
550c4a3c779Sdrh 
551c4a3c779Sdrh /*
5528141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
5538141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr
5548141f61eSdrh ** expression node refer back to that source column.  The following changes
5558141f61eSdrh ** are made to pExpr:
5568141f61eSdrh **
5578141f61eSdrh **    pExpr->iDb           Set the index in db->aDb[] of the database holding
5588141f61eSdrh **                         the table.
5598141f61eSdrh **    pExpr->iTable        Set to the cursor number for the table obtained
5608141f61eSdrh **                         from pSrcList.
5618141f61eSdrh **    pExpr->iColumn       Set to the column number within the table.
5628141f61eSdrh **    pExpr->op            Set to TK_COLUMN.
5638141f61eSdrh **    pExpr->pLeft         Any expression this points to is deleted
5648141f61eSdrh **    pExpr->pRight        Any expression this points to is deleted.
5658141f61eSdrh **
5668141f61eSdrh ** The pDbToken is the name of the database (the "X").  This value may be
5678141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z.  Any available database
5688141f61eSdrh ** can be used.  The pTableToken is the name of the table (the "Y").  This
5698141f61eSdrh ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
5708141f61eSdrh ** means that the form of the name is Z and that columns from any table
5718141f61eSdrh ** can be used.
5728141f61eSdrh **
5738141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message
5748141f61eSdrh ** in pParse and return non-zero.  Return zero on success.
5758141f61eSdrh */
5768141f61eSdrh static int lookupName(
5778141f61eSdrh   Parse *pParse,      /* The parsing context */
5788141f61eSdrh   Token *pDbToken,     /* Name of the database containing table, or NULL */
5798141f61eSdrh   Token *pTableToken,  /* Name of table containing column, or NULL */
5808141f61eSdrh   Token *pColumnToken, /* Name of the column. */
5818141f61eSdrh   SrcList *pSrcList,   /* List of tables used to resolve column names */
5828141f61eSdrh   ExprList *pEList,    /* List of expressions used to resolve "AS" */
5838141f61eSdrh   Expr *pExpr          /* Make this EXPR node point to the selected column */
5848141f61eSdrh ){
5858141f61eSdrh   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
5868141f61eSdrh   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
5878141f61eSdrh   char *zCol = 0;      /* Name of the column.  The "Z" */
5888141f61eSdrh   int i, j;            /* Loop counters */
5898141f61eSdrh   int cnt = 0;         /* Number of matching column names */
5908141f61eSdrh   int cntTab = 0;      /* Number of matching table names */
5917e26d750Sdrh   sqlite *db = pParse->db;  /* The database */
5928141f61eSdrh 
5938141f61eSdrh   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
594a99db3b6Sdrh   zDb = sqlite3NameFromToken(pDbToken);
595a99db3b6Sdrh   zTab = sqlite3NameFromToken(pTableToken);
596a99db3b6Sdrh   zCol = sqlite3NameFromToken(pColumnToken);
59724b03fd0Sdanielk1977   if( sqlite3_malloc_failed ){
5988141f61eSdrh     return 1;  /* Leak memory (zDb and zTab) if malloc fails */
5998141f61eSdrh   }
6008141f61eSdrh   assert( zTab==0 || pEList==0 );
6018141f61eSdrh 
6028141f61eSdrh   pExpr->iTable = -1;
6038141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
6048141f61eSdrh     struct SrcList_item *pItem = &pSrcList->a[i];
6058141f61eSdrh     Table *pTab = pItem->pTab;
6068141f61eSdrh     Column *pCol;
6078141f61eSdrh 
6088141f61eSdrh     if( pTab==0 ) continue;
6098141f61eSdrh     assert( pTab->nCol>0 );
6108141f61eSdrh     if( zTab ){
6118141f61eSdrh       if( pItem->zAlias ){
6128141f61eSdrh         char *zTabName = pItem->zAlias;
6134adee20fSdanielk1977         if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
6148141f61eSdrh       }else{
6158141f61eSdrh         char *zTabName = pTab->zName;
6164adee20fSdanielk1977         if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
6174adee20fSdanielk1977         if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
6188141f61eSdrh           continue;
6198141f61eSdrh         }
6208141f61eSdrh       }
6218141f61eSdrh     }
6228141f61eSdrh     if( 0==(cntTab++) ){
6238141f61eSdrh       pExpr->iTable = pItem->iCursor;
6248141f61eSdrh       pExpr->iDb = pTab->iDb;
6258141f61eSdrh     }
6268141f61eSdrh     for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
6274adee20fSdanielk1977       if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
6288141f61eSdrh         cnt++;
6298141f61eSdrh         pExpr->iTable = pItem->iCursor;
6308141f61eSdrh         pExpr->iDb = pTab->iDb;
6318141f61eSdrh         /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
6328141f61eSdrh         pExpr->iColumn = j==pTab->iPKey ? -1 : j;
633a37cdde0Sdanielk1977         pExpr->affinity = pTab->aCol[j].affinity;
6340202b29eSdanielk1977         pExpr->pColl = pTab->aCol[j].pColl;
6358141f61eSdrh         break;
6368141f61eSdrh       }
6378141f61eSdrh     }
6388141f61eSdrh   }
6398141f61eSdrh 
6408141f61eSdrh   /* If we have not already resolved the name, then maybe
6418141f61eSdrh   ** it is a new.* or old.* trigger argument reference
6428141f61eSdrh   */
6438141f61eSdrh   if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
6448141f61eSdrh     TriggerStack *pTriggerStack = pParse->trigStack;
6458141f61eSdrh     Table *pTab = 0;
6464adee20fSdanielk1977     if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
6478141f61eSdrh       pExpr->iTable = pTriggerStack->newIdx;
6488141f61eSdrh       assert( pTriggerStack->pTab );
6498141f61eSdrh       pTab = pTriggerStack->pTab;
6504adee20fSdanielk1977     }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
6518141f61eSdrh       pExpr->iTable = pTriggerStack->oldIdx;
6528141f61eSdrh       assert( pTriggerStack->pTab );
6538141f61eSdrh       pTab = pTriggerStack->pTab;
6548141f61eSdrh     }
6558141f61eSdrh 
6568141f61eSdrh     if( pTab ){
6578141f61eSdrh       int j;
6588141f61eSdrh       Column *pCol = pTab->aCol;
6598141f61eSdrh 
6608141f61eSdrh       pExpr->iDb = pTab->iDb;
6618141f61eSdrh       cntTab++;
6628141f61eSdrh       for(j=0; j < pTab->nCol; j++, pCol++) {
6634adee20fSdanielk1977         if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
6648141f61eSdrh           cnt++;
6658141f61eSdrh           pExpr->iColumn = j==pTab->iPKey ? -1 : j;
666a37cdde0Sdanielk1977           pExpr->affinity = pTab->aCol[j].affinity;
6670202b29eSdanielk1977           pExpr->pColl = pTab->aCol[j].pColl;
6688141f61eSdrh           break;
6698141f61eSdrh         }
6708141f61eSdrh       }
6718141f61eSdrh     }
6728141f61eSdrh   }
6738141f61eSdrh 
6748141f61eSdrh   /*
6758141f61eSdrh   ** Perhaps the name is a reference to the ROWID
6768141f61eSdrh   */
6774adee20fSdanielk1977   if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
6788141f61eSdrh     cnt = 1;
6798141f61eSdrh     pExpr->iColumn = -1;
680a37cdde0Sdanielk1977     pExpr->affinity = SQLITE_AFF_INTEGER;
6818141f61eSdrh   }
6828141f61eSdrh 
6838141f61eSdrh   /*
6848141f61eSdrh   ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
6858141f61eSdrh   ** might refer to an result-set alias.  This happens, for example, when
6868141f61eSdrh   ** we are resolving names in the WHERE clause of the following command:
6878141f61eSdrh   **
6888141f61eSdrh   **     SELECT a+b AS x FROM table WHERE x<10;
6898141f61eSdrh   **
6908141f61eSdrh   ** In cases like this, replace pExpr with a copy of the expression that
6918141f61eSdrh   ** forms the result set entry ("a+b" in the example) and return immediately.
6928141f61eSdrh   ** Note that the expression in the result set should have already been
6938141f61eSdrh   ** resolved by the time the WHERE clause is resolved.
6948141f61eSdrh   */
6958141f61eSdrh   if( cnt==0 && pEList!=0 ){
6968141f61eSdrh     for(j=0; j<pEList->nExpr; j++){
6978141f61eSdrh       char *zAs = pEList->a[j].zName;
6984adee20fSdanielk1977       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
6998141f61eSdrh         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
7008141f61eSdrh         pExpr->op = TK_AS;
7018141f61eSdrh         pExpr->iColumn = j;
7024adee20fSdanielk1977         pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
7038141f61eSdrh         sqliteFree(zCol);
7048141f61eSdrh         assert( zTab==0 && zDb==0 );
7058141f61eSdrh         return 0;
7068141f61eSdrh       }
7078141f61eSdrh     }
7088141f61eSdrh   }
7098141f61eSdrh 
7108141f61eSdrh   /*
7118141f61eSdrh   ** If X and Y are NULL (in other words if only the column name Z is
7128141f61eSdrh   ** supplied) and the value of Z is enclosed in double-quotes, then
7138141f61eSdrh   ** Z is a string literal if it doesn't match any column names.  In that
7148141f61eSdrh   ** case, we need to return right away and not make any changes to
7158141f61eSdrh   ** pExpr.
7168141f61eSdrh   */
7178141f61eSdrh   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
7188141f61eSdrh     sqliteFree(zCol);
7198141f61eSdrh     return 0;
7208141f61eSdrh   }
7218141f61eSdrh 
7228141f61eSdrh   /*
7238141f61eSdrh   ** cnt==0 means there was not match.  cnt>1 means there were two or
7248141f61eSdrh   ** more matches.  Either way, we have an error.
7258141f61eSdrh   */
7268141f61eSdrh   if( cnt!=1 ){
7278141f61eSdrh     char *z = 0;
7288141f61eSdrh     char *zErr;
7298141f61eSdrh     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
7308141f61eSdrh     if( zDb ){
7314adee20fSdanielk1977       sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
7328141f61eSdrh     }else if( zTab ){
7334adee20fSdanielk1977       sqlite3SetString(&z, zTab, ".", zCol, 0);
7348141f61eSdrh     }else{
7358141f61eSdrh       z = sqliteStrDup(zCol);
7368141f61eSdrh     }
7374adee20fSdanielk1977     sqlite3ErrorMsg(pParse, zErr, z);
7388141f61eSdrh     sqliteFree(z);
7398141f61eSdrh   }
7408141f61eSdrh 
7418141f61eSdrh   /* Clean up and return
7428141f61eSdrh   */
7438141f61eSdrh   sqliteFree(zDb);
7448141f61eSdrh   sqliteFree(zTab);
7458141f61eSdrh   sqliteFree(zCol);
7464adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pLeft);
7478141f61eSdrh   pExpr->pLeft = 0;
7484adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pRight);
7498141f61eSdrh   pExpr->pRight = 0;
7508141f61eSdrh   pExpr->op = TK_COLUMN;
7514adee20fSdanielk1977   sqlite3AuthRead(pParse, pExpr, pSrcList);
7528141f61eSdrh   return cnt!=1;
7538141f61eSdrh }
7548141f61eSdrh 
7558141f61eSdrh /*
756cce7d176Sdrh ** This routine walks an expression tree and resolves references to
757967e8b73Sdrh ** table columns.  Nodes of the form ID.ID or ID resolve into an
758aacc543eSdrh ** index to the table in the table list and a column offset.  The
759aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
760aacc543eSdrh ** value is changed to the index of the referenced table in pTabList
761832508b7Sdrh ** plus the "base" value.  The base value will ultimately become the
762aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced
763aacc543eSdrh ** table.  The Expr.iColumn value is changed to the index of the column
764aacc543eSdrh ** of the referenced table.  The Expr.iColumn value for the special
765aacc543eSdrh ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
766aacc543eSdrh ** alias for ROWID.
76719a775c2Sdrh **
768fef5208cSdrh ** We also check for instances of the IN operator.  IN comes in two
769fef5208cSdrh ** forms:
770fef5208cSdrh **
771fef5208cSdrh **           expr IN (exprlist)
772fef5208cSdrh ** and
773fef5208cSdrh **           expr IN (SELECT ...)
774fef5208cSdrh **
775fef5208cSdrh ** The first form is handled by creating a set holding the list
776fef5208cSdrh ** of allowed values.  The second form causes the SELECT to generate
777fef5208cSdrh ** a temporary table.
778fef5208cSdrh **
779fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression.
78019a775c2Sdrh ** If it finds any, it generates code to write the value of that select
78119a775c2Sdrh ** into a memory cell.
782cce7d176Sdrh **
783967e8b73Sdrh ** Unknown columns or tables provoke an error.  The function returns
784cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg.
785cce7d176Sdrh */
7864adee20fSdanielk1977 int sqlite3ExprResolveIds(
787a2e00042Sdrh   Parse *pParse,     /* The parser context */
7888141f61eSdrh   SrcList *pSrcList, /* List of tables used to resolve column names */
789a2e00042Sdrh   ExprList *pEList,  /* List of expressions used to resolve "AS" */
790a2e00042Sdrh   Expr *pExpr        /* The expression to be analyzed. */
791a2e00042Sdrh ){
7926a3ea0e6Sdrh   int i;
7936a3ea0e6Sdrh 
7948141f61eSdrh   if( pExpr==0 || pSrcList==0 ) return 0;
7958141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
7968141f61eSdrh     assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
7976a3ea0e6Sdrh   }
798cce7d176Sdrh   switch( pExpr->op ){
7992398937bSdrh     /* Double-quoted strings (ex: "abc") are used as identifiers if
8002398937bSdrh     ** possible.  Otherwise they remain as strings.  Single-quoted
8012398937bSdrh     ** strings (ex: 'abc') are always string literals.
8022398937bSdrh     */
8032398937bSdrh     case TK_STRING: {
8042398937bSdrh       if( pExpr->token.z[0]=='\'' ) break;
8052398937bSdrh       /* Fall thru into the TK_ID case if this is a double-quoted string */
8062398937bSdrh     }
8078141f61eSdrh     /* A lone identifier is the name of a columnd.
808a2e00042Sdrh     */
809cce7d176Sdrh     case TK_ID: {
8108141f61eSdrh       if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
811cce7d176Sdrh         return 1;
812ed6c8671Sdrh       }
813cce7d176Sdrh       break;
814cce7d176Sdrh     }
815cce7d176Sdrh 
816d24cc427Sdrh     /* A table name and column name:     ID.ID
817d24cc427Sdrh     ** Or a database, table and column:  ID.ID.ID
818d24cc427Sdrh     */
819cce7d176Sdrh     case TK_DOT: {
8208141f61eSdrh       Token *pColumn;
8218141f61eSdrh       Token *pTable;
8228141f61eSdrh       Token *pDb;
8238141f61eSdrh       Expr *pRight;
824cce7d176Sdrh 
825cce7d176Sdrh       pRight = pExpr->pRight;
826d24cc427Sdrh       if( pRight->op==TK_ID ){
8278141f61eSdrh         pDb = 0;
8288141f61eSdrh         pTable = &pExpr->pLeft->token;
8298141f61eSdrh         pColumn = &pRight->token;
830d24cc427Sdrh       }else{
8318141f61eSdrh         assert( pRight->op==TK_DOT );
8328141f61eSdrh         pDb = &pExpr->pLeft->token;
8338141f61eSdrh         pTable = &pRight->pLeft->token;
8348141f61eSdrh         pColumn = &pRight->pRight->token;
835d24cc427Sdrh       }
8368141f61eSdrh       if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
837daffd0e5Sdrh         return 1;
838daffd0e5Sdrh       }
839cce7d176Sdrh       break;
840cce7d176Sdrh     }
841cce7d176Sdrh 
842fef5208cSdrh     case TK_IN: {
843e014a838Sdanielk1977       char affinity;
8444adee20fSdanielk1977       Vdbe *v = sqlite3GetVdbe(pParse);
845d3d39e93Sdrh       KeyInfo keyInfo;
8460202b29eSdanielk1977       int addr;        /* Address of OP_OpenTemp instruction */
847d3d39e93Sdrh 
848fef5208cSdrh       if( v==0 ) return 1;
8494adee20fSdanielk1977       if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
850cfab11bcSdrh         return 1;
851cfab11bcSdrh       }
852bf3b721fSdanielk1977       affinity = sqlite3ExprAffinity(pExpr->pLeft);
853e014a838Sdanielk1977 
854e014a838Sdanielk1977       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
855e014a838Sdanielk1977       ** expression it is handled the same way. A temporary table is
856e014a838Sdanielk1977       ** filled with single-field index keys representing the results
857e014a838Sdanielk1977       ** from the SELECT or the <exprlist>.
858fef5208cSdrh       **
859e014a838Sdanielk1977       ** If the 'x' expression is a column value, or the SELECT...
860e014a838Sdanielk1977       ** statement returns a column value, then the affinity of that
861e014a838Sdanielk1977       ** column is used to build the index keys. If both 'x' and the
862e014a838Sdanielk1977       ** SELECT... statement are columns, then numeric affinity is used
863e014a838Sdanielk1977       ** if either column has NUMERIC or INTEGER affinity. If neither
864e014a838Sdanielk1977       ** 'x' nor the SELECT... statement are columns, then numeric affinity
865e014a838Sdanielk1977       ** is used.
866fef5208cSdrh       */
867832508b7Sdrh       pExpr->iTable = pParse->nTab++;
8680202b29eSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
869d3d39e93Sdrh       memset(&keyInfo, 0, sizeof(keyInfo));
870d3d39e93Sdrh       keyInfo.nField = 1;
871f3218feaSdrh       sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
872e014a838Sdanielk1977 
873e014a838Sdanielk1977       if( pExpr->pSelect ){
874e014a838Sdanielk1977         /* Case 1:     expr IN (SELECT ...)
875e014a838Sdanielk1977         **
876e014a838Sdanielk1977         ** Generate code to write the results of the select into the temporary
877e014a838Sdanielk1977         ** table allocated and opened above.
878e014a838Sdanielk1977         */
879e014a838Sdanielk1977         int iParm = pExpr->iTable +  (((int)affinity)<<16);
880be5c89acSdrh         ExprList *pEList;
881e014a838Sdanielk1977         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
882bf3b721fSdanielk1977         sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
883be5c89acSdrh         pEList = pExpr->pSelect->pEList;
884be5c89acSdrh         if( pEList && pEList->nExpr>0 ){
8857cedc8d4Sdanielk1977           keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
886be5c89acSdrh               pEList->a[0].pExpr);
8870202b29eSdanielk1977         }
888fef5208cSdrh       }else if( pExpr->pList ){
889fef5208cSdrh         /* Case 2:     expr IN (exprlist)
890fef5208cSdrh         **
891e014a838Sdanielk1977 	** For each expression, build an index key from the evaluation and
892e014a838Sdanielk1977         ** store it in the temporary table. If <expr> is a column, then use
893e014a838Sdanielk1977         ** that columns affinity when building index keys. If <expr> is not
894e014a838Sdanielk1977         ** a column, use numeric affinity.
895fef5208cSdrh         */
896e014a838Sdanielk1977         int i;
897e014a838Sdanielk1977         char const *affStr;
898e014a838Sdanielk1977         if( !affinity ){
899e014a838Sdanielk1977           affinity = SQLITE_AFF_NUMERIC;
900e014a838Sdanielk1977         }
901e014a838Sdanielk1977         affStr = sqlite3AffinityString(affinity);
9020202b29eSdanielk1977         keyInfo.aColl[0] = pExpr->pLeft->pColl;
903e014a838Sdanielk1977 
904e014a838Sdanielk1977         /* Loop through each expression in <exprlist>. */
905fef5208cSdrh         for(i=0; i<pExpr->pList->nExpr; i++){
906fef5208cSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
907e014a838Sdanielk1977 
908e014a838Sdanielk1977           /* Check that the expression is constant and valid. */
9094adee20fSdanielk1977           if( !sqlite3ExprIsConstant(pE2) ){
9104adee20fSdanielk1977             sqlite3ErrorMsg(pParse,
911da93d238Sdrh               "right-hand side of IN operator must be constant");
912fef5208cSdrh             return 1;
913fef5208cSdrh           }
9144adee20fSdanielk1977           if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
9154794b980Sdrh             return 1;
9164794b980Sdrh           }
917e014a838Sdanielk1977 
918e014a838Sdanielk1977           /* Evaluate the expression and insert it into the temp table */
9194adee20fSdanielk1977           sqlite3ExprCode(pParse, pE2);
920ededfd5eSdanielk1977           sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
9210f69c1e3Sdanielk1977           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
922e014a838Sdanielk1977           sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
923fef5208cSdrh         }
924fef5208cSdrh       }
9250202b29eSdanielk1977       sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
9260202b29eSdanielk1977 
927cfab11bcSdrh       break;
928fef5208cSdrh     }
929fef5208cSdrh 
93019a775c2Sdrh     case TK_SELECT: {
931fef5208cSdrh       /* This has to be a scalar SELECT.  Generate code to put the
932fef5208cSdrh       ** value of this select in a memory cell and record the number
933967e8b73Sdrh       ** of the memory cell in iColumn.
934fef5208cSdrh       */
935967e8b73Sdrh       pExpr->iColumn = pParse->nMem++;
936bf3b721fSdanielk1977       if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
93719a775c2Sdrh         return 1;
93819a775c2Sdrh       }
93919a775c2Sdrh       break;
94019a775c2Sdrh     }
94119a775c2Sdrh 
942cce7d176Sdrh     /* For all else, just recursively walk the tree */
943cce7d176Sdrh     default: {
944cce7d176Sdrh       if( pExpr->pLeft
9454adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
946cce7d176Sdrh         return 1;
947cce7d176Sdrh       }
948cce7d176Sdrh       if( pExpr->pRight
9494adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
950cce7d176Sdrh         return 1;
951cce7d176Sdrh       }
952cce7d176Sdrh       if( pExpr->pList ){
953cce7d176Sdrh         int i;
954cce7d176Sdrh         ExprList *pList = pExpr->pList;
955cce7d176Sdrh         for(i=0; i<pList->nExpr; i++){
956832508b7Sdrh           Expr *pArg = pList->a[i].pExpr;
9574adee20fSdanielk1977           if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
958cce7d176Sdrh             return 1;
959cce7d176Sdrh           }
960cce7d176Sdrh         }
961cce7d176Sdrh       }
962cce7d176Sdrh     }
963cce7d176Sdrh   }
964cce7d176Sdrh   return 0;
965cce7d176Sdrh }
966cce7d176Sdrh 
967cce7d176Sdrh /*
9684b59ab5eSdrh ** pExpr is a node that defines a function of some kind.  It might
9694b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function
9704b59ab5eSdrh ** that implements an operator, like "a LIKE b".
9714b59ab5eSdrh **
9724b59ab5eSdrh ** This routine makes *pzName point to the name of the function and
9734b59ab5eSdrh ** *pnName hold the number of characters in the function name.
9744b59ab5eSdrh */
9754b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
9764b59ab5eSdrh   switch( pExpr->op ){
9774b59ab5eSdrh     case TK_FUNCTION: {
9784b59ab5eSdrh       *pzName = pExpr->token.z;
9796977fea8Sdrh       *pnName = pExpr->token.n;
9804b59ab5eSdrh       break;
9814b59ab5eSdrh     }
9824b59ab5eSdrh     case TK_LIKE: {
9834b59ab5eSdrh       *pzName = "like";
9844b59ab5eSdrh       *pnName = 4;
9854b59ab5eSdrh       break;
9864b59ab5eSdrh     }
9874b59ab5eSdrh     case TK_GLOB: {
9884b59ab5eSdrh       *pzName = "glob";
9894b59ab5eSdrh       *pnName = 4;
9904b59ab5eSdrh       break;
9914b59ab5eSdrh     }
9924b59ab5eSdrh     default: {
9934b59ab5eSdrh       *pzName = "can't happen";
9944b59ab5eSdrh       *pnName = 12;
9954b59ab5eSdrh       break;
9964b59ab5eSdrh     }
9974b59ab5eSdrh   }
9984b59ab5eSdrh }
9994b59ab5eSdrh 
10004b59ab5eSdrh /*
1001cce7d176Sdrh ** Error check the functions in an expression.  Make sure all
1002cce7d176Sdrh ** function names are recognized and all functions have the correct
1003cce7d176Sdrh ** number of arguments.  Leave an error message in pParse->zErrMsg
1004cce7d176Sdrh ** if anything is amiss.  Return the number of errors.
1005cce7d176Sdrh **
1006cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function
1007cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1008cce7d176Sdrh */
10094adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
1010cce7d176Sdrh   int nErr = 0;
1011cce7d176Sdrh   if( pExpr==0 ) return 0;
1012cce7d176Sdrh   switch( pExpr->op ){
10134b59ab5eSdrh     case TK_GLOB:
10144b59ab5eSdrh     case TK_LIKE:
1015cce7d176Sdrh     case TK_FUNCTION: {
1016c9b84a1fSdrh       int n = pExpr->pList ? pExpr->pList->nExpr : 0;  /* Number of arguments */
1017c9b84a1fSdrh       int no_such_func = 0;       /* True if no such function exists */
1018c9b84a1fSdrh       int wrong_num_args = 0;     /* True if wrong number of arguments */
1019c9b84a1fSdrh       int is_agg = 0;             /* True if is an aggregate function */
1020cce7d176Sdrh       int i;
10214b59ab5eSdrh       int nId;                    /* Number of characters in function name */
10224b59ab5eSdrh       const char *zId;            /* The function name. */
10230bce8354Sdrh       FuncDef *pDef;
1024d8123366Sdanielk1977       int enc = pParse->db->enc;
10250bce8354Sdrh 
10264b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1027d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
10280bce8354Sdrh       if( pDef==0 ){
1029d8123366Sdanielk1977         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
10300bce8354Sdrh         if( pDef==0 ){
1031cce7d176Sdrh           no_such_func = 1;
10328e0a2f90Sdrh         }else{
10338e0a2f90Sdrh           wrong_num_args = 1;
10348e0a2f90Sdrh         }
10358e0a2f90Sdrh       }else{
10360bce8354Sdrh         is_agg = pDef->xFunc==0;
1037cce7d176Sdrh       }
10388e0a2f90Sdrh       if( is_agg && !allowAgg ){
10394adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
10408e0a2f90Sdrh         nErr++;
10418e0a2f90Sdrh         is_agg = 0;
10428e0a2f90Sdrh       }else if( no_such_func ){
10434adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1044cce7d176Sdrh         nErr++;
10458e0a2f90Sdrh       }else if( wrong_num_args ){
10464adee20fSdanielk1977         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1047f7a9e1acSdrh              nId, zId);
10488e0a2f90Sdrh         nErr++;
1049cce7d176Sdrh       }
1050f7a9e1acSdrh       if( is_agg ){
1051f7a9e1acSdrh         pExpr->op = TK_AGG_FUNCTION;
1052f7a9e1acSdrh         if( pIsAgg ) *pIsAgg = 1;
1053f7a9e1acSdrh       }
1054cce7d176Sdrh       for(i=0; nErr==0 && i<n; i++){
10554adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
10564cfa7934Sdrh                                allowAgg && !is_agg, pIsAgg);
1057cce7d176Sdrh       }
10580202b29eSdanielk1977       /* FIX ME:  Compute pExpr->affinity based on the expected return
10590202b29eSdanielk1977       ** type of the function
10600202b29eSdanielk1977       */
1061cce7d176Sdrh     }
1062cce7d176Sdrh     default: {
1063cce7d176Sdrh       if( pExpr->pLeft ){
10644adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
1065cce7d176Sdrh       }
1066cce7d176Sdrh       if( nErr==0 && pExpr->pRight ){
10674adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
1068cce7d176Sdrh       }
1069fef5208cSdrh       if( nErr==0 && pExpr->pList ){
1070fef5208cSdrh         int n = pExpr->pList->nExpr;
1071fef5208cSdrh         int i;
1072fef5208cSdrh         for(i=0; nErr==0 && i<n; i++){
10732282792aSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
10744adee20fSdanielk1977           nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
1075fef5208cSdrh         }
1076fef5208cSdrh       }
1077cce7d176Sdrh       break;
1078cce7d176Sdrh     }
1079cce7d176Sdrh   }
1080cce7d176Sdrh   return nErr;
1081cce7d176Sdrh }
1082cce7d176Sdrh 
1083cce7d176Sdrh /*
1084fec19aadSdrh ** Generate an instruction that will put the integer describe by
1085fec19aadSdrh ** text z[0..n-1] on the stack.
1086fec19aadSdrh */
1087fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){
1088fec19aadSdrh   int i;
10896fec0762Sdrh   if( sqlite3GetInt32(z, &i) ){
10906fec0762Sdrh     sqlite3VdbeAddOp(v, OP_Integer, i, 0);
10916fec0762Sdrh   }else if( sqlite3FitsIn64Bits(z) ){
10926fec0762Sdrh     sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
1093fec19aadSdrh   }else{
1094fec19aadSdrh     sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1095fec19aadSdrh   }
1096fec19aadSdrh }
1097fec19aadSdrh 
1098fec19aadSdrh /*
1099cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given
11001ccde15dSdrh ** expression and leave the result on the top of stack.
1101cce7d176Sdrh */
11024adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1103cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1104cce7d176Sdrh   int op;
1105daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1106cce7d176Sdrh   switch( pExpr->op ){
1107cce7d176Sdrh     case TK_PLUS:     op = OP_Add;      break;
1108cce7d176Sdrh     case TK_MINUS:    op = OP_Subtract; break;
1109cce7d176Sdrh     case TK_STAR:     op = OP_Multiply; break;
1110cce7d176Sdrh     case TK_SLASH:    op = OP_Divide;   break;
1111cce7d176Sdrh     case TK_AND:      op = OP_And;      break;
1112cce7d176Sdrh     case TK_OR:       op = OP_Or;       break;
1113cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1114cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1115cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1116cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1117cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1118cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1119cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1120cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1121cce7d176Sdrh     case TK_NOT:      op = OP_Not;      break;
1122cce7d176Sdrh     case TK_UMINUS:   op = OP_Negative; break;
1123bf4133cbSdrh     case TK_BITAND:   op = OP_BitAnd;   break;
1124bf4133cbSdrh     case TK_BITOR:    op = OP_BitOr;    break;
1125bf4133cbSdrh     case TK_BITNOT:   op = OP_BitNot;   break;
1126bf4133cbSdrh     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
1127bf4133cbSdrh     case TK_RSHIFT:   op = OP_ShiftRight; break;
1128bf4133cbSdrh     case TK_REM:      op = OP_Remainder;  break;
1129fec19aadSdrh     case TK_FLOAT:    op = OP_Real;       break;
11300f69c1e3Sdanielk1977     case TK_STRING:   op = OP_String8;     break;
1131c572ef7fSdanielk1977     case TK_BLOB:     op = OP_HexBlob;    break;
1132cfe9a69fSdanielk1977     default: op = 0; break;
1133cce7d176Sdrh   }
1134cce7d176Sdrh   switch( pExpr->op ){
1135967e8b73Sdrh     case TK_COLUMN: {
11362282792aSdrh       if( pParse->useAgg ){
11374adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1138c4a3c779Sdrh       }else if( pExpr->iColumn>=0 ){
11394adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1140c4a3c779Sdrh       }else{
11414adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
11422282792aSdrh       }
1143cce7d176Sdrh       break;
1144cce7d176Sdrh     }
1145cce7d176Sdrh     case TK_INTEGER: {
1146fec19aadSdrh       codeInteger(v, pExpr->token.z, pExpr->token.n);
1147fec19aadSdrh       break;
114851e9a445Sdrh     }
1149fec19aadSdrh     case TK_FLOAT:
1150fec19aadSdrh     case TK_STRING: {
1151fec19aadSdrh       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
11524adee20fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1153cce7d176Sdrh       break;
1154cce7d176Sdrh     }
1155c572ef7fSdanielk1977     case TK_BLOB: {
1156c572ef7fSdanielk1977       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1157c572ef7fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1158c572ef7fSdanielk1977       break;
1159c572ef7fSdanielk1977     }
1160cce7d176Sdrh     case TK_NULL: {
11610f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1162cce7d176Sdrh       break;
1163cce7d176Sdrh     }
116450457896Sdrh     case TK_VARIABLE: {
11654adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
116650457896Sdrh       break;
116750457896Sdrh     }
1168c9b84a1fSdrh     case TK_LT:
1169c9b84a1fSdrh     case TK_LE:
1170c9b84a1fSdrh     case TK_GT:
1171c9b84a1fSdrh     case TK_GE:
1172c9b84a1fSdrh     case TK_NE:
1173c9b84a1fSdrh     case TK_EQ: {
1174a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1175a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1176be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
1177a37cdde0Sdanielk1977       break;
1178c9b84a1fSdrh     }
1179cce7d176Sdrh     case TK_AND:
1180cce7d176Sdrh     case TK_OR:
1181cce7d176Sdrh     case TK_PLUS:
1182cce7d176Sdrh     case TK_STAR:
1183cce7d176Sdrh     case TK_MINUS:
1184bf4133cbSdrh     case TK_REM:
1185bf4133cbSdrh     case TK_BITAND:
1186bf4133cbSdrh     case TK_BITOR:
118717c40294Sdrh     case TK_SLASH:
1188bf4133cbSdrh     case TK_LSHIFT:
1189bf4133cbSdrh     case TK_RSHIFT: {
11904adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
119117c40294Sdrh       sqlite3ExprCode(pParse, pExpr->pRight);
11924adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1193bf4133cbSdrh       break;
1194bf4133cbSdrh     }
11950040077dSdrh     case TK_CONCAT: {
11964adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
11974adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
119872c952a1Sdanielk1977       sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
11990040077dSdrh       break;
12000040077dSdrh     }
1201cce7d176Sdrh     case TK_UMINUS: {
1202fec19aadSdrh       Expr *pLeft = pExpr->pLeft;
1203fec19aadSdrh       assert( pLeft );
1204fec19aadSdrh       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1205fec19aadSdrh         Token *p = &pLeft->token;
12066e142f54Sdrh         char *z = sqliteMalloc( p->n + 2 );
12076e142f54Sdrh         sprintf(z, "-%.*s", p->n, p->z);
1208fec19aadSdrh         if( pLeft->op==TK_FLOAT ){
1209fec19aadSdrh           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1210e6840900Sdrh         }else{
1211fec19aadSdrh           codeInteger(v, z, p->n+1);
1212e6840900Sdrh         }
12136e142f54Sdrh         sqliteFree(z);
12146e142f54Sdrh         break;
12156e142f54Sdrh       }
12161ccde15dSdrh       /* Fall through into TK_NOT */
12176e142f54Sdrh     }
1218bf4133cbSdrh     case TK_BITNOT:
12196e142f54Sdrh     case TK_NOT: {
12204adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12214adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1222cce7d176Sdrh       break;
1223cce7d176Sdrh     }
1224cce7d176Sdrh     case TK_ISNULL:
1225cce7d176Sdrh     case TK_NOTNULL: {
1226cce7d176Sdrh       int dest;
12274adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
12284adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12294adee20fSdanielk1977       dest = sqlite3VdbeCurrentAddr(v) + 2;
12304adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
12314adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1232cce7d176Sdrh     }
1233a37cdde0Sdanielk1977     break;
12342282792aSdrh     case TK_AGG_FUNCTION: {
12354adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
12362282792aSdrh       break;
12372282792aSdrh     }
12384b59ab5eSdrh     case TK_GLOB:
12394b59ab5eSdrh     case TK_LIKE:
1240cce7d176Sdrh     case TK_FUNCTION: {
1241cce7d176Sdrh       ExprList *pList = pExpr->pList;
124289425d5eSdrh       int nExpr = pList ? pList->nExpr : 0;
12430bce8354Sdrh       FuncDef *pDef;
12444b59ab5eSdrh       int nId;
12454b59ab5eSdrh       const char *zId;
1246682f68b0Sdanielk1977       int p2 = 0;
1247682f68b0Sdanielk1977       int i;
1248d8123366Sdanielk1977       u8 enc = pParse->db->enc;
1249dc1bdc4fSdanielk1977       CollSeq *pColl = 0;
12504b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1251d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
12520bce8354Sdrh       assert( pDef!=0 );
1253f9b596ebSdrh       nExpr = sqlite3ExprCodeExprList(pParse, pList);
1254682f68b0Sdanielk1977       for(i=0; i<nExpr && i<32; i++){
1255d02eb1fdSdanielk1977         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1256d02eb1fdSdanielk1977           p2 |= (1<<i);
1257d02eb1fdSdanielk1977         }
1258dc1bdc4fSdanielk1977         if( pDef->needCollSeq && !pColl ){
1259dc1bdc4fSdanielk1977           pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1260dc1bdc4fSdanielk1977         }
1261dc1bdc4fSdanielk1977       }
1262dc1bdc4fSdanielk1977       if( pDef->needCollSeq ){
1263dc1bdc4fSdanielk1977         if( !pColl ) pColl = pParse->db->pDfltColl;
1264d8123366Sdanielk1977         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
1265682f68b0Sdanielk1977       }
1266682f68b0Sdanielk1977       sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
12676ec2733bSdrh       break;
12686ec2733bSdrh     }
126919a775c2Sdrh     case TK_SELECT: {
12704adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
127119a775c2Sdrh       break;
127219a775c2Sdrh     }
1273fef5208cSdrh     case TK_IN: {
1274fef5208cSdrh       int addr;
1275e014a838Sdanielk1977       char const *affStr;
1276e014a838Sdanielk1977 
1277e014a838Sdanielk1977       /* Figure out the affinity to use to create a key from the results
1278e014a838Sdanielk1977       ** of the expression. affinityStr stores a static string suitable for
1279ededfd5eSdanielk1977       ** P3 of OP_MakeRecord.
1280e014a838Sdanielk1977       */
1281e014a838Sdanielk1977       affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1282e014a838Sdanielk1977 
12834adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1284e014a838Sdanielk1977 
1285e014a838Sdanielk1977       /* Code the <expr> from "<expr> IN (...)". The temporary table
1286e014a838Sdanielk1977       ** pExpr->iTable contains the values that make up the (...) set.
1287e014a838Sdanielk1977       */
12884adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12894adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1290e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */
12914adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
12920f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1293e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1294ededfd5eSdanielk1977       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1295e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1296e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */
1297e014a838Sdanielk1977 
1298fef5208cSdrh       break;
1299fef5208cSdrh     }
1300fef5208cSdrh     case TK_BETWEEN: {
1301be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1302be5c89acSdrh       struct ExprList_item *pLItem = pExpr->pList->a;
1303be5c89acSdrh       Expr *pRight = pLItem->pExpr;
1304be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
13054adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1306be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1307be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
13084adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1309be5c89acSdrh       pLItem++;
1310be5c89acSdrh       pRight = pLItem->pExpr;
1311be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1312be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
13134adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_And, 0, 0);
1314fef5208cSdrh       break;
1315fef5208cSdrh     }
131651e9a445Sdrh     case TK_UPLUS:
1317a2e00042Sdrh     case TK_AS: {
13184adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1319a2e00042Sdrh       break;
1320a2e00042Sdrh     }
132117a7f8ddSdrh     case TK_CASE: {
132217a7f8ddSdrh       int expr_end_label;
1323f5905aa7Sdrh       int jumpInst;
1324f5905aa7Sdrh       int addr;
1325f5905aa7Sdrh       int nExpr;
132617a7f8ddSdrh       int i;
1327be5c89acSdrh       ExprList *pEList;
1328be5c89acSdrh       struct ExprList_item *aListelem;
132917a7f8ddSdrh 
133017a7f8ddSdrh       assert(pExpr->pList);
133117a7f8ddSdrh       assert((pExpr->pList->nExpr % 2) == 0);
133217a7f8ddSdrh       assert(pExpr->pList->nExpr > 0);
1333be5c89acSdrh       pEList = pExpr->pList;
1334be5c89acSdrh       aListelem = pEList->a;
1335be5c89acSdrh       nExpr = pEList->nExpr;
13364adee20fSdanielk1977       expr_end_label = sqlite3VdbeMakeLabel(v);
133717a7f8ddSdrh       if( pExpr->pLeft ){
13384adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pLeft);
1339cce7d176Sdrh       }
1340f5905aa7Sdrh       for(i=0; i<nExpr; i=i+2){
1341be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i].pExpr);
134217a7f8ddSdrh         if( pExpr->pLeft ){
13434adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1344be5c89acSdrh           jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1345be5c89acSdrh                                  OP_Ne, 0, 1);
13464adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1347f5905aa7Sdrh         }else{
13484adee20fSdanielk1977           jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
134917a7f8ddSdrh         }
1350be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
13514adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
13524adee20fSdanielk1977         addr = sqlite3VdbeCurrentAddr(v);
13534adee20fSdanielk1977         sqlite3VdbeChangeP2(v, jumpInst, addr);
135417a7f8ddSdrh       }
1355f570f011Sdrh       if( pExpr->pLeft ){
13564adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1357f570f011Sdrh       }
135817a7f8ddSdrh       if( pExpr->pRight ){
13594adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pRight);
136017a7f8ddSdrh       }else{
13610f69c1e3Sdanielk1977         sqlite3VdbeAddOp(v, OP_String8, 0, 0);
136217a7f8ddSdrh       }
13634adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, expr_end_label);
13646f34903eSdanielk1977       break;
13656f34903eSdanielk1977     }
13666f34903eSdanielk1977     case TK_RAISE: {
13676f34903eSdanielk1977       if( !pParse->trigStack ){
13684adee20fSdanielk1977         sqlite3ErrorMsg(pParse,
1369da93d238Sdrh                        "RAISE() may only be used within a trigger-program");
13706f34903eSdanielk1977         pParse->nErr++;
13716f34903eSdanielk1977 	return;
13726f34903eSdanielk1977       }
13736f34903eSdanielk1977       if( pExpr->iColumn == OE_Rollback ||
13746f34903eSdanielk1977 	  pExpr->iColumn == OE_Abort ||
13756f34903eSdanielk1977 	  pExpr->iColumn == OE_Fail ){
13764adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1377701a0aebSdrh                            pExpr->token.z, pExpr->token.n);
13784adee20fSdanielk1977 	  sqlite3VdbeDequoteP3(v, -1);
13796f34903eSdanielk1977       } else {
13806f34903eSdanielk1977 	  assert( pExpr->iColumn == OE_Ignore );
13814adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1382701a0aebSdrh                            "(IGNORE jump)", 0);
13836f34903eSdanielk1977       }
138417a7f8ddSdrh     }
138517a7f8ddSdrh     break;
138617a7f8ddSdrh   }
1387cce7d176Sdrh }
1388cce7d176Sdrh 
1389cce7d176Sdrh /*
1390268380caSdrh ** Generate code that pushes the value of every element of the given
1391f9b596ebSdrh ** expression list onto the stack.
1392268380caSdrh **
1393268380caSdrh ** Return the number of elements pushed onto the stack.
1394268380caSdrh */
13954adee20fSdanielk1977 int sqlite3ExprCodeExprList(
1396268380caSdrh   Parse *pParse,     /* Parsing context */
1397f9b596ebSdrh   ExprList *pList    /* The expression list to be coded */
1398268380caSdrh ){
1399268380caSdrh   struct ExprList_item *pItem;
1400268380caSdrh   int i, n;
1401268380caSdrh   Vdbe *v;
1402268380caSdrh   if( pList==0 ) return 0;
14034adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1404268380caSdrh   n = pList->nExpr;
1405268380caSdrh   for(pItem=pList->a, i=0; i<n; i++, pItem++){
14064adee20fSdanielk1977     sqlite3ExprCode(pParse, pItem->pExpr);
1407268380caSdrh   }
1408f9b596ebSdrh   return n;
1409268380caSdrh }
1410268380caSdrh 
1411268380caSdrh /*
1412cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made
1413cce7d176Sdrh ** to the label "dest" if the expression is true but execution
1414cce7d176Sdrh ** continues straight thru if the expression is false.
1415f5905aa7Sdrh **
1416f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then
1417f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true.
1418cce7d176Sdrh */
14194adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1420cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1421cce7d176Sdrh   int op = 0;
1422daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1423cce7d176Sdrh   switch( pExpr->op ){
1424cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1425cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1426cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1427cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1428cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1429cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1430cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1431cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1432cce7d176Sdrh     default:  break;
1433cce7d176Sdrh   }
1434cce7d176Sdrh   switch( pExpr->op ){
1435cce7d176Sdrh     case TK_AND: {
14364adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
14374adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
14384adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
14394adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1440cce7d176Sdrh       break;
1441cce7d176Sdrh     }
1442cce7d176Sdrh     case TK_OR: {
14434adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
14444adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1445cce7d176Sdrh       break;
1446cce7d176Sdrh     }
1447cce7d176Sdrh     case TK_NOT: {
14484adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1449cce7d176Sdrh       break;
1450cce7d176Sdrh     }
1451cce7d176Sdrh     case TK_LT:
1452cce7d176Sdrh     case TK_LE:
1453cce7d176Sdrh     case TK_GT:
1454cce7d176Sdrh     case TK_GE:
1455cce7d176Sdrh     case TK_NE:
14560ac65892Sdrh     case TK_EQ: {
14574adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14584adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1459be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1460cce7d176Sdrh       break;
1461cce7d176Sdrh     }
1462cce7d176Sdrh     case TK_ISNULL:
1463cce7d176Sdrh     case TK_NOTNULL: {
14644adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14654adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1466cce7d176Sdrh       break;
1467cce7d176Sdrh     }
1468fef5208cSdrh     case TK_BETWEEN: {
14690202b29eSdanielk1977       /* The expression "x BETWEEN y AND z" is implemented as:
14700202b29eSdanielk1977       **
14710202b29eSdanielk1977       ** 1 IF (x < y) GOTO 3
14720202b29eSdanielk1977       ** 2 IF (x <= z) GOTO <dest>
14730202b29eSdanielk1977       ** 3 ...
14740202b29eSdanielk1977       */
1475f5905aa7Sdrh       int addr;
1476be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1477be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1478be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
14794adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1480be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1481be5c89acSdrh       addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
14820202b29eSdanielk1977 
1483be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1484be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1485be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
14860202b29eSdanielk1977 
14874adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
14884adee20fSdanielk1977       sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
14894adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1490fef5208cSdrh       break;
1491fef5208cSdrh     }
1492cce7d176Sdrh     default: {
14934adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
14944adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
1495cce7d176Sdrh       break;
1496cce7d176Sdrh     }
1497cce7d176Sdrh   }
1498cce7d176Sdrh }
1499cce7d176Sdrh 
1500cce7d176Sdrh /*
150166b89c8fSdrh ** Generate code for a boolean expression such that a jump is made
1502cce7d176Sdrh ** to the label "dest" if the expression is false but execution
1503cce7d176Sdrh ** continues straight thru if the expression is true.
1504f5905aa7Sdrh **
1505f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then
1506f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1507cce7d176Sdrh */
15084adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1509cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1510cce7d176Sdrh   int op = 0;
1511daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1512cce7d176Sdrh   switch( pExpr->op ){
1513cce7d176Sdrh     case TK_LT:       op = OP_Ge;       break;
1514cce7d176Sdrh     case TK_LE:       op = OP_Gt;       break;
1515cce7d176Sdrh     case TK_GT:       op = OP_Le;       break;
1516cce7d176Sdrh     case TK_GE:       op = OP_Lt;       break;
1517cce7d176Sdrh     case TK_NE:       op = OP_Eq;       break;
1518cce7d176Sdrh     case TK_EQ:       op = OP_Ne;       break;
1519cce7d176Sdrh     case TK_ISNULL:   op = OP_NotNull;  break;
1520cce7d176Sdrh     case TK_NOTNULL:  op = OP_IsNull;   break;
1521cce7d176Sdrh     default:  break;
1522cce7d176Sdrh   }
1523cce7d176Sdrh   switch( pExpr->op ){
1524cce7d176Sdrh     case TK_AND: {
15254adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
15264adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1527cce7d176Sdrh       break;
1528cce7d176Sdrh     }
1529cce7d176Sdrh     case TK_OR: {
15304adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
15314adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
15324adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
15334adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1534cce7d176Sdrh       break;
1535cce7d176Sdrh     }
1536cce7d176Sdrh     case TK_NOT: {
15374adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1538cce7d176Sdrh       break;
1539cce7d176Sdrh     }
1540cce7d176Sdrh     case TK_LT:
1541cce7d176Sdrh     case TK_LE:
1542cce7d176Sdrh     case TK_GT:
1543cce7d176Sdrh     case TK_GE:
1544cce7d176Sdrh     case TK_NE:
1545cce7d176Sdrh     case TK_EQ: {
15464adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15474adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1548be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1549cce7d176Sdrh       break;
1550cce7d176Sdrh     }
1551cce7d176Sdrh     case TK_ISNULL:
1552cce7d176Sdrh     case TK_NOTNULL: {
15534adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15544adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1555cce7d176Sdrh       break;
1556cce7d176Sdrh     }
1557fef5208cSdrh     case TK_BETWEEN: {
15580202b29eSdanielk1977       /* The expression is "x BETWEEN y AND z". It is implemented as:
15590202b29eSdanielk1977       **
15600202b29eSdanielk1977       ** 1 IF (x >= y) GOTO 3
15610202b29eSdanielk1977       ** 2 GOTO <dest>
15620202b29eSdanielk1977       ** 3 IF (x > z) GOTO <dest>
15630202b29eSdanielk1977       */
1564fef5208cSdrh       int addr;
1565be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1566be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1567be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
15684adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1569be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
15704adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1571be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1572be5c89acSdrh 
15734adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
15744adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1575be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1576be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1577be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
1578fef5208cSdrh       break;
1579fef5208cSdrh     }
1580cce7d176Sdrh     default: {
15814adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
15824adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1583cce7d176Sdrh       break;
1584cce7d176Sdrh     }
1585cce7d176Sdrh   }
1586cce7d176Sdrh }
15872282792aSdrh 
15882282792aSdrh /*
15892282792aSdrh ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
15902282792aSdrh ** if they are identical and return FALSE if they differ in any way.
15912282792aSdrh */
15924adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){
15932282792aSdrh   int i;
15942282792aSdrh   if( pA==0 ){
15952282792aSdrh     return pB==0;
15962282792aSdrh   }else if( pB==0 ){
15972282792aSdrh     return 0;
15982282792aSdrh   }
15992282792aSdrh   if( pA->op!=pB->op ) return 0;
16004adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
16014adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
16022282792aSdrh   if( pA->pList ){
16032282792aSdrh     if( pB->pList==0 ) return 0;
16042282792aSdrh     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
16052282792aSdrh     for(i=0; i<pA->pList->nExpr; i++){
16064adee20fSdanielk1977       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
16072282792aSdrh         return 0;
16082282792aSdrh       }
16092282792aSdrh     }
16102282792aSdrh   }else if( pB->pList ){
16112282792aSdrh     return 0;
16122282792aSdrh   }
16132282792aSdrh   if( pA->pSelect || pB->pSelect ) return 0;
16142f2c01e5Sdrh   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
16152282792aSdrh   if( pA->token.z ){
16162282792aSdrh     if( pB->token.z==0 ) return 0;
16176977fea8Sdrh     if( pB->token.n!=pA->token.n ) return 0;
16184adee20fSdanielk1977     if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
16192282792aSdrh   }
16202282792aSdrh   return 1;
16212282792aSdrh }
16222282792aSdrh 
16232282792aSdrh /*
16242282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index.
16252282792aSdrh */
16262282792aSdrh static int appendAggInfo(Parse *pParse){
16272282792aSdrh   if( (pParse->nAgg & 0x7)==0 ){
16282282792aSdrh     int amt = pParse->nAgg + 8;
16296d4abfbeSdrh     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
16306d4abfbeSdrh     if( aAgg==0 ){
16312282792aSdrh       return -1;
16322282792aSdrh     }
16336d4abfbeSdrh     pParse->aAgg = aAgg;
16342282792aSdrh   }
16352282792aSdrh   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
16362282792aSdrh   return pParse->nAgg++;
16372282792aSdrh }
16382282792aSdrh 
16392282792aSdrh /*
16402282792aSdrh ** Analyze the given expression looking for aggregate functions and
16412282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array.
16422282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary.
16432282792aSdrh **
16442282792aSdrh ** This routine should only be called after the expression has been
16454adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
16462282792aSdrh **
16472282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return
16482282792aSdrh ** the number of errors.
16492282792aSdrh */
16504adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
16512282792aSdrh   int i;
16522282792aSdrh   AggExpr *aAgg;
16532282792aSdrh   int nErr = 0;
16542282792aSdrh 
16552282792aSdrh   if( pExpr==0 ) return 0;
16562282792aSdrh   switch( pExpr->op ){
1657967e8b73Sdrh     case TK_COLUMN: {
16582282792aSdrh       aAgg = pParse->aAgg;
16592282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16602282792aSdrh         if( aAgg[i].isAgg ) continue;
16612282792aSdrh         if( aAgg[i].pExpr->iTable==pExpr->iTable
1662967e8b73Sdrh          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
16632282792aSdrh           break;
16642282792aSdrh         }
16652282792aSdrh       }
16662282792aSdrh       if( i>=pParse->nAgg ){
16672282792aSdrh         i = appendAggInfo(pParse);
16682282792aSdrh         if( i<0 ) return 1;
16692282792aSdrh         pParse->aAgg[i].isAgg = 0;
16702282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16712282792aSdrh       }
1672aaf88729Sdrh       pExpr->iAgg = i;
16732282792aSdrh       break;
16742282792aSdrh     }
16752282792aSdrh     case TK_AGG_FUNCTION: {
16762282792aSdrh       aAgg = pParse->aAgg;
16772282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16782282792aSdrh         if( !aAgg[i].isAgg ) continue;
16794adee20fSdanielk1977         if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
16802282792aSdrh           break;
16812282792aSdrh         }
16822282792aSdrh       }
16832282792aSdrh       if( i>=pParse->nAgg ){
1684d8123366Sdanielk1977         u8 enc = pParse->db->enc;
16852282792aSdrh         i = appendAggInfo(pParse);
16862282792aSdrh         if( i<0 ) return 1;
16872282792aSdrh         pParse->aAgg[i].isAgg = 1;
16882282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16894adee20fSdanielk1977         pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
16906977fea8Sdrh              pExpr->token.z, pExpr->token.n,
1691d8123366Sdanielk1977              pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
16922282792aSdrh       }
16932282792aSdrh       pExpr->iAgg = i;
16942282792aSdrh       break;
16952282792aSdrh     }
16962282792aSdrh     default: {
16972282792aSdrh       if( pExpr->pLeft ){
16984adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
16992282792aSdrh       }
17002282792aSdrh       if( nErr==0 && pExpr->pRight ){
17014adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
17022282792aSdrh       }
17032282792aSdrh       if( nErr==0 && pExpr->pList ){
17042282792aSdrh         int n = pExpr->pList->nExpr;
17052282792aSdrh         int i;
17062282792aSdrh         for(i=0; nErr==0 && i<n; i++){
17074adee20fSdanielk1977           nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
17082282792aSdrh         }
17092282792aSdrh       }
17102282792aSdrh       break;
17112282792aSdrh     }
17122282792aSdrh   }
17132282792aSdrh   return nErr;
17142282792aSdrh }
17158e0a2f90Sdrh 
17168e0a2f90Sdrh /*
1717d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag
1718d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
1719d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return
1720d02eb1fdSdanielk1977 ** NULL if the function does not exist.
17218e0a2f90Sdrh **
17220bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef
17238e0a2f90Sdrh ** structure is created and liked into the "db" structure if a
17248e0a2f90Sdrh ** no matching function previously existed.  When createFlag is true
17258e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts
17268e0a2f90Sdrh ** any number of arguments will be returned.
17278e0a2f90Sdrh **
17288e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid
17298e0a2f90Sdrh ** function found is returned.  A function is valid if either xFunc
17308e0a2f90Sdrh ** or xStep is non-zero.
1731d02eb1fdSdanielk1977 **
1732d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and
1733d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not
1734d02eb1fdSdanielk1977 ** match that requested.
17358e0a2f90Sdrh */
17364adee20fSdanielk1977 FuncDef *sqlite3FindFunction(
17378e0a2f90Sdrh   sqlite *db,        /* An open database */
17388e0a2f90Sdrh   const char *zName, /* Name of the function.  Not null-terminated */
17398e0a2f90Sdrh   int nName,         /* Number of characters in the name */
17408e0a2f90Sdrh   int nArg,          /* Number of arguments.  -1 means any number */
1741d8123366Sdanielk1977   u8 enc,            /* Preferred text encoding */
17428e0a2f90Sdrh   int createFlag     /* Create new entry if true and does not otherwise exist */
17438e0a2f90Sdrh ){
1744d02eb1fdSdanielk1977   FuncDef *p;         /* Iterator variable */
1745d02eb1fdSdanielk1977   FuncDef *pFirst;    /* First function with this name */
1746d02eb1fdSdanielk1977   FuncDef *pBest = 0; /* Best match found so far */
1747d8123366Sdanielk1977   int bestmatch = 0;
1748d02eb1fdSdanielk1977 
1749d8123366Sdanielk1977 
1750d8123366Sdanielk1977   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
1751d02eb1fdSdanielk1977   if( nArg<-1 ) nArg = -1;
1752d02eb1fdSdanielk1977 
1753d02eb1fdSdanielk1977   pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1754d02eb1fdSdanielk1977   for(p=pFirst; p; p=p->pNext){
1755d8123366Sdanielk1977     /* During the search for the best function definition, bestmatch is set
1756d8123366Sdanielk1977     ** as follows to indicate the quality of the match with the definition
1757d8123366Sdanielk1977     ** pointed to by pBest:
1758d8123366Sdanielk1977     **
1759d8123366Sdanielk1977     ** 0: pBest is NULL. No match has been found.
1760d8123366Sdanielk1977     ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1761d8123366Sdanielk1977     **    encoding is requested, or vice versa.
1762d8123366Sdanielk1977     ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1763d8123366Sdanielk1977     **    requested, or vice versa.
1764d8123366Sdanielk1977     ** 3: A variable arguments function using the same text encoding.
1765d8123366Sdanielk1977     ** 4: A function with the exact number of arguments requested that
1766d8123366Sdanielk1977     **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1767d8123366Sdanielk1977     ** 5: A function with the exact number of arguments requested that
1768d8123366Sdanielk1977     **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1769d8123366Sdanielk1977     ** 6: An exact match.
1770d8123366Sdanielk1977     **
1771d8123366Sdanielk1977     ** A larger value of 'matchqual' indicates a more desirable match.
1772d8123366Sdanielk1977     */
1773e12c17baSdanielk1977     if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
1774d8123366Sdanielk1977       int match = 1;          /* Quality of this match */
1775d8123366Sdanielk1977       if( p->nArg==nArg || nArg==-1 ){
1776d8123366Sdanielk1977         match = 4;
17778e0a2f90Sdrh       }
1778d8123366Sdanielk1977       if( enc==p->iPrefEnc ){
1779d8123366Sdanielk1977         match += 2;
17808e0a2f90Sdrh       }
1781d8123366Sdanielk1977       else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1782d8123366Sdanielk1977                (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1783d8123366Sdanielk1977         match += 1;
1784d02eb1fdSdanielk1977       }
1785d8123366Sdanielk1977 
1786d8123366Sdanielk1977       if( match>bestmatch ){
1787d02eb1fdSdanielk1977         pBest = p;
1788d8123366Sdanielk1977         bestmatch = match;
1789d02eb1fdSdanielk1977       }
1790d02eb1fdSdanielk1977     }
1791d02eb1fdSdanielk1977   }
1792d02eb1fdSdanielk1977 
1793d8123366Sdanielk1977   /* If the createFlag parameter is true, and the seach did not reveal an
1794d8123366Sdanielk1977   ** exact match for the name, number of arguments and encoding, then add a
1795d8123366Sdanielk1977   ** new entry to the hash table and return it.
1796d8123366Sdanielk1977   */
1797d8123366Sdanielk1977   if( createFlag && bestmatch<6 &&
1798d02eb1fdSdanielk1977       (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1799d02eb1fdSdanielk1977     pBest->nArg = nArg;
1800d02eb1fdSdanielk1977     pBest->pNext = pFirst;
1801d02eb1fdSdanielk1977     pBest->zName = (char*)&pBest[1];
1802d8123366Sdanielk1977     pBest->iPrefEnc = enc;
1803d02eb1fdSdanielk1977     memcpy(pBest->zName, zName, nName);
1804d02eb1fdSdanielk1977     pBest->zName[nName] = 0;
1805d02eb1fdSdanielk1977     sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
1806d02eb1fdSdanielk1977   }
1807d02eb1fdSdanielk1977 
1808d02eb1fdSdanielk1977   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1809d02eb1fdSdanielk1977     return pBest;
1810d02eb1fdSdanielk1977   }
18118e0a2f90Sdrh   return 0;
18128e0a2f90Sdrh }
1813