xref: /sqlite-3.40.0/src/expr.c (revision 71c697ef)
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*71c697efSdrh ** $Id: expr.c,v 1.155 2004/08/08 23:39:19 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 );
229*71c697efSdrh   if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
2304b59ab5eSdrh     if( pLeft->dyn==0 && pRight->dyn==0 ){
2316977fea8Sdrh       pExpr->span.z = pLeft->z;
2326977fea8Sdrh       pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
2334b59ab5eSdrh     }else{
2346977fea8Sdrh       pExpr->span.z = 0;
2354b59ab5eSdrh     }
236a76b5dfcSdrh   }
237a76b5dfcSdrh }
238a76b5dfcSdrh 
239a76b5dfcSdrh /*
240a76b5dfcSdrh ** Construct a new expression node for a function with multiple
241a76b5dfcSdrh ** arguments.
242a76b5dfcSdrh */
2434adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
244a76b5dfcSdrh   Expr *pNew;
245a76b5dfcSdrh   pNew = sqliteMalloc( sizeof(Expr) );
246a76b5dfcSdrh   if( pNew==0 ){
2474adee20fSdanielk1977     /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
248a76b5dfcSdrh     return 0;
249a76b5dfcSdrh   }
250a76b5dfcSdrh   pNew->op = TK_FUNCTION;
251a76b5dfcSdrh   pNew->pList = pList;
252a76b5dfcSdrh   if( pToken ){
2534b59ab5eSdrh     assert( pToken->dyn==0 );
254a76b5dfcSdrh     pNew->token = *pToken;
255a76b5dfcSdrh   }else{
256a76b5dfcSdrh     pNew->token.z = 0;
257a76b5dfcSdrh   }
2586977fea8Sdrh   pNew->span = pNew->token;
259a76b5dfcSdrh   return pNew;
260a76b5dfcSdrh }
261a76b5dfcSdrh 
262a76b5dfcSdrh /*
263a2e00042Sdrh ** Recursively delete an expression tree.
264a2e00042Sdrh */
2654adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){
266a2e00042Sdrh   if( p==0 ) return;
2674efc4754Sdrh   if( p->span.dyn ) sqliteFree((char*)p->span.z);
2684efc4754Sdrh   if( p->token.dyn ) sqliteFree((char*)p->token.z);
2694adee20fSdanielk1977   sqlite3ExprDelete(p->pLeft);
2704adee20fSdanielk1977   sqlite3ExprDelete(p->pRight);
2714adee20fSdanielk1977   sqlite3ExprListDelete(p->pList);
2724adee20fSdanielk1977   sqlite3SelectDelete(p->pSelect);
273a2e00042Sdrh   sqliteFree(p);
274a2e00042Sdrh }
275a2e00042Sdrh 
276a76b5dfcSdrh 
277a76b5dfcSdrh /*
278ff78bd2fSdrh ** The following group of routines make deep copies of expressions,
279ff78bd2fSdrh ** expression lists, ID lists, and select statements.  The copies can
280ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines)
281ff78bd2fSdrh ** without effecting the originals.
282ff78bd2fSdrh **
2834adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
2844adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
285ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines.
286ff78bd2fSdrh **
287ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated.
288ff78bd2fSdrh */
2894adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){
290ff78bd2fSdrh   Expr *pNew;
291ff78bd2fSdrh   if( p==0 ) return 0;
292fcb78a49Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
293ff78bd2fSdrh   if( pNew==0 ) return 0;
2943b167c75Sdrh   memcpy(pNew, p, sizeof(*pNew));
2956977fea8Sdrh   if( p->token.z!=0 ){
2964b59ab5eSdrh     pNew->token.z = sqliteStrDup(p->token.z);
2974b59ab5eSdrh     pNew->token.dyn = 1;
2984b59ab5eSdrh   }else{
2994efc4754Sdrh     assert( pNew->token.z==0 );
3004b59ab5eSdrh   }
3016977fea8Sdrh   pNew->span.z = 0;
3024adee20fSdanielk1977   pNew->pLeft = sqlite3ExprDup(p->pLeft);
3034adee20fSdanielk1977   pNew->pRight = sqlite3ExprDup(p->pRight);
3044adee20fSdanielk1977   pNew->pList = sqlite3ExprListDup(p->pList);
3054adee20fSdanielk1977   pNew->pSelect = sqlite3SelectDup(p->pSelect);
306ff78bd2fSdrh   return pNew;
307ff78bd2fSdrh }
3084adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){
3094b59ab5eSdrh   if( pTo->dyn ) sqliteFree((char*)pTo->z);
3104b59ab5eSdrh   if( pFrom->z ){
3114b59ab5eSdrh     pTo->n = pFrom->n;
3124b59ab5eSdrh     pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
3134b59ab5eSdrh     pTo->dyn = 1;
3144b59ab5eSdrh   }else{
3154b59ab5eSdrh     pTo->z = 0;
3164b59ab5eSdrh   }
3174b59ab5eSdrh }
3184adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){
319ff78bd2fSdrh   ExprList *pNew;
3203e7bc9caSdrh   struct ExprList_item *pItem;
321ff78bd2fSdrh   int i;
322ff78bd2fSdrh   if( p==0 ) return 0;
323ff78bd2fSdrh   pNew = sqliteMalloc( sizeof(*pNew) );
324ff78bd2fSdrh   if( pNew==0 ) return 0;
3254305d103Sdrh   pNew->nExpr = pNew->nAlloc = p->nExpr;
3263e7bc9caSdrh   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
327e0048400Sdanielk1977   if( pItem==0 ){
328e0048400Sdanielk1977     sqliteFree(pNew);
329e0048400Sdanielk1977     return 0;
330e0048400Sdanielk1977   }
3311bdd9b57Sdrh   for(i=0; i<p->nExpr; i++, pItem++){
3324b59ab5eSdrh     Expr *pNewExpr, *pOldExpr;
3334adee20fSdanielk1977     pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
3346977fea8Sdrh     if( pOldExpr->span.z!=0 && pNewExpr ){
3356977fea8Sdrh       /* Always make a copy of the span for top-level expressions in the
3364b59ab5eSdrh       ** expression list.  The logic in SELECT processing that determines
3374b59ab5eSdrh       ** the names of columns in the result set needs this information */
3384adee20fSdanielk1977       sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
3394b59ab5eSdrh     }
3401f3e905cSdrh     assert( pNewExpr==0 || pNewExpr->span.z!=0
34124b03fd0Sdanielk1977             || pOldExpr->span.z==0 || sqlite3_malloc_failed );
3423e7bc9caSdrh     pItem->zName = sqliteStrDup(p->a[i].zName);
3433e7bc9caSdrh     pItem->sortOrder = p->a[i].sortOrder;
3443e7bc9caSdrh     pItem->isAgg = p->a[i].isAgg;
3453e7bc9caSdrh     pItem->done = 0;
346ff78bd2fSdrh   }
347ff78bd2fSdrh   return pNew;
348ff78bd2fSdrh }
3494adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){
350ad3cab52Sdrh   SrcList *pNew;
351ad3cab52Sdrh   int i;
352113088ecSdrh   int nByte;
353ad3cab52Sdrh   if( p==0 ) return 0;
354113088ecSdrh   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
3554efc4754Sdrh   pNew = sqliteMallocRaw( nByte );
356ad3cab52Sdrh   if( pNew==0 ) return 0;
3574305d103Sdrh   pNew->nSrc = pNew->nAlloc = p->nSrc;
358ad3cab52Sdrh   for(i=0; i<p->nSrc; i++){
3594efc4754Sdrh     struct SrcList_item *pNewItem = &pNew->a[i];
3604efc4754Sdrh     struct SrcList_item *pOldItem = &p->a[i];
3614efc4754Sdrh     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
3624efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
3634efc4754Sdrh     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
3644efc4754Sdrh     pNewItem->jointype = pOldItem->jointype;
3654efc4754Sdrh     pNewItem->iCursor = pOldItem->iCursor;
3664efc4754Sdrh     pNewItem->pTab = 0;
3674adee20fSdanielk1977     pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
3684adee20fSdanielk1977     pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
3694adee20fSdanielk1977     pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
370ad3cab52Sdrh   }
371ad3cab52Sdrh   return pNew;
372ad3cab52Sdrh }
3734adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){
374ff78bd2fSdrh   IdList *pNew;
375ff78bd2fSdrh   int i;
376ff78bd2fSdrh   if( p==0 ) return 0;
3774efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*pNew) );
378ff78bd2fSdrh   if( pNew==0 ) return 0;
3794305d103Sdrh   pNew->nId = pNew->nAlloc = p->nId;
3804efc4754Sdrh   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
381e4697f5eSdrh   if( pNew->a==0 ) return 0;
382ff78bd2fSdrh   for(i=0; i<p->nId; i++){
3834efc4754Sdrh     struct IdList_item *pNewItem = &pNew->a[i];
3844efc4754Sdrh     struct IdList_item *pOldItem = &p->a[i];
3854efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
3864efc4754Sdrh     pNewItem->idx = pOldItem->idx;
387ff78bd2fSdrh   }
388ff78bd2fSdrh   return pNew;
389ff78bd2fSdrh }
3904adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){
391ff78bd2fSdrh   Select *pNew;
392ff78bd2fSdrh   if( p==0 ) return 0;
3934efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
394ff78bd2fSdrh   if( pNew==0 ) return 0;
395ff78bd2fSdrh   pNew->isDistinct = p->isDistinct;
3964adee20fSdanielk1977   pNew->pEList = sqlite3ExprListDup(p->pEList);
3974adee20fSdanielk1977   pNew->pSrc = sqlite3SrcListDup(p->pSrc);
3984adee20fSdanielk1977   pNew->pWhere = sqlite3ExprDup(p->pWhere);
3994adee20fSdanielk1977   pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
4004adee20fSdanielk1977   pNew->pHaving = sqlite3ExprDup(p->pHaving);
4014adee20fSdanielk1977   pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
402ff78bd2fSdrh   pNew->op = p->op;
4034adee20fSdanielk1977   pNew->pPrior = sqlite3SelectDup(p->pPrior);
404ff78bd2fSdrh   pNew->nLimit = p->nLimit;
405ff78bd2fSdrh   pNew->nOffset = p->nOffset;
406ff78bd2fSdrh   pNew->zSelect = 0;
4077b58daeaSdrh   pNew->iLimit = -1;
4087b58daeaSdrh   pNew->iOffset = -1;
409dc1bdc4fSdanielk1977   pNew->ppOpenTemp = 0;
410ff78bd2fSdrh   return pNew;
411ff78bd2fSdrh }
412ff78bd2fSdrh 
413ff78bd2fSdrh 
414ff78bd2fSdrh /*
415a76b5dfcSdrh ** Add a new element to the end of an expression list.  If pList is
416a76b5dfcSdrh ** initially NULL, then create a new expression list.
417a76b5dfcSdrh */
4184adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
419a76b5dfcSdrh   if( pList==0 ){
420a76b5dfcSdrh     pList = sqliteMalloc( sizeof(ExprList) );
421a76b5dfcSdrh     if( pList==0 ){
4224adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
423a76b5dfcSdrh       return 0;
424a76b5dfcSdrh     }
4254efc4754Sdrh     assert( pList->nAlloc==0 );
426a76b5dfcSdrh   }
4274305d103Sdrh   if( pList->nAlloc<=pList->nExpr ){
4284305d103Sdrh     pList->nAlloc = pList->nAlloc*2 + 4;
4294efc4754Sdrh     pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
4304efc4754Sdrh     if( pList->a==0 ){
4314adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
4324efc4754Sdrh       pList->nExpr = pList->nAlloc = 0;
433a76b5dfcSdrh       return pList;
434a76b5dfcSdrh     }
435a76b5dfcSdrh   }
4364efc4754Sdrh   assert( pList->a!=0 );
4374efc4754Sdrh   if( pExpr || pName ){
4384efc4754Sdrh     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
4394efc4754Sdrh     memset(pItem, 0, sizeof(*pItem));
4404efc4754Sdrh     pItem->pExpr = pExpr;
441a99db3b6Sdrh     pItem->zName = sqlite3NameFromToken(pName);
442a76b5dfcSdrh   }
443a76b5dfcSdrh   return pList;
444a76b5dfcSdrh }
445a76b5dfcSdrh 
446a76b5dfcSdrh /*
447a76b5dfcSdrh ** Delete an entire expression list.
448a76b5dfcSdrh */
4494adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){
450a76b5dfcSdrh   int i;
451be5c89acSdrh   struct ExprList_item *pItem;
452a76b5dfcSdrh   if( pList==0 ) return;
4531bdd9b57Sdrh   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
4541bdd9b57Sdrh   assert( pList->nExpr<=pList->nAlloc );
455be5c89acSdrh   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
456be5c89acSdrh     sqlite3ExprDelete(pItem->pExpr);
457be5c89acSdrh     sqliteFree(pItem->zName);
458a76b5dfcSdrh   }
459a76b5dfcSdrh   sqliteFree(pList->a);
460a76b5dfcSdrh   sqliteFree(pList);
461a76b5dfcSdrh }
462a76b5dfcSdrh 
463a76b5dfcSdrh /*
464fef5208cSdrh ** Walk an expression tree.  Return 1 if the expression is constant
465fef5208cSdrh ** and 0 if it involves variables.
4662398937bSdrh **
4672398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc")
4682398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is
4692398937bSdrh ** a constant.
470fef5208cSdrh */
4714adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){
472fef5208cSdrh   switch( p->op ){
473fef5208cSdrh     case TK_ID:
474967e8b73Sdrh     case TK_COLUMN:
475fef5208cSdrh     case TK_DOT:
4767bdc0c1dSdrh     case TK_FUNCTION:
477fef5208cSdrh       return 0;
4787bdc0c1dSdrh     case TK_NULL:
4792398937bSdrh     case TK_STRING:
480c572ef7fSdanielk1977     case TK_BLOB:
4819208643dSdrh     case TK_INTEGER:
4829208643dSdrh     case TK_FLOAT:
48350457896Sdrh     case TK_VARIABLE:
4849208643dSdrh       return 1;
485fef5208cSdrh     default: {
4864adee20fSdanielk1977       if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
4874adee20fSdanielk1977       if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
488fef5208cSdrh       if( p->pList ){
489fef5208cSdrh         int i;
490fef5208cSdrh         for(i=0; i<p->pList->nExpr; i++){
4914adee20fSdanielk1977           if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
492fef5208cSdrh         }
493fef5208cSdrh       }
4949208643dSdrh       return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
495fef5208cSdrh     }
496fef5208cSdrh   }
4979208643dSdrh   return 0;
498fef5208cSdrh }
499fef5208cSdrh 
500fef5208cSdrh /*
501202b2df7Sdrh ** If the given expression codes a constant integer that is small enough
502202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer
503202b2df7Sdrh ** in *pValue.  If the expression is not an integer or if it is too big
504202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
505e4de1febSdrh */
5064adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){
507e4de1febSdrh   switch( p->op ){
508e4de1febSdrh     case TK_INTEGER: {
509fec19aadSdrh       if( sqlite3GetInt32(p->token.z, pValue) ){
510e4de1febSdrh         return 1;
511e4de1febSdrh       }
512202b2df7Sdrh       break;
513202b2df7Sdrh     }
514e4de1febSdrh     case TK_STRING: {
5154c755c0fSdrh       const u8 *z = (u8*)p->token.z;
516e4de1febSdrh       int n = p->token.n;
517bd790ee3Sdrh       if( n>0 && z[0]=='-' ){ z++; n--; }
518e4de1febSdrh       while( n>0 && *z && isdigit(*z) ){ z++; n--; }
519fec19aadSdrh       if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
520e4de1febSdrh         return 1;
521e4de1febSdrh       }
522e4de1febSdrh       break;
523e4de1febSdrh     }
5244b59ab5eSdrh     case TK_UPLUS: {
5254adee20fSdanielk1977       return sqlite3ExprIsInteger(p->pLeft, pValue);
5264b59ab5eSdrh     }
527e4de1febSdrh     case TK_UMINUS: {
528e4de1febSdrh       int v;
5294adee20fSdanielk1977       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
530e4de1febSdrh         *pValue = -v;
531e4de1febSdrh         return 1;
532e4de1febSdrh       }
533e4de1febSdrh       break;
534e4de1febSdrh     }
535e4de1febSdrh     default: break;
536e4de1febSdrh   }
537e4de1febSdrh   return 0;
538e4de1febSdrh }
539e4de1febSdrh 
540e4de1febSdrh /*
541c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name.
542c4a3c779Sdrh */
5434adee20fSdanielk1977 int sqlite3IsRowid(const char *z){
5444adee20fSdanielk1977   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
5454adee20fSdanielk1977   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
5464adee20fSdanielk1977   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
547c4a3c779Sdrh   return 0;
548c4a3c779Sdrh }
549c4a3c779Sdrh 
550c4a3c779Sdrh /*
5518141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
5528141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr
5538141f61eSdrh ** expression node refer back to that source column.  The following changes
5548141f61eSdrh ** are made to pExpr:
5558141f61eSdrh **
5568141f61eSdrh **    pExpr->iDb           Set the index in db->aDb[] of the database holding
5578141f61eSdrh **                         the table.
5588141f61eSdrh **    pExpr->iTable        Set to the cursor number for the table obtained
5598141f61eSdrh **                         from pSrcList.
5608141f61eSdrh **    pExpr->iColumn       Set to the column number within the table.
5618141f61eSdrh **    pExpr->op            Set to TK_COLUMN.
5628141f61eSdrh **    pExpr->pLeft         Any expression this points to is deleted
5638141f61eSdrh **    pExpr->pRight        Any expression this points to is deleted.
5648141f61eSdrh **
5658141f61eSdrh ** The pDbToken is the name of the database (the "X").  This value may be
5668141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z.  Any available database
5678141f61eSdrh ** can be used.  The pTableToken is the name of the table (the "Y").  This
5688141f61eSdrh ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
5698141f61eSdrh ** means that the form of the name is Z and that columns from any table
5708141f61eSdrh ** can be used.
5718141f61eSdrh **
5728141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message
5738141f61eSdrh ** in pParse and return non-zero.  Return zero on success.
5748141f61eSdrh */
5758141f61eSdrh static int lookupName(
5768141f61eSdrh   Parse *pParse,      /* The parsing context */
5778141f61eSdrh   Token *pDbToken,     /* Name of the database containing table, or NULL */
5788141f61eSdrh   Token *pTableToken,  /* Name of table containing column, or NULL */
5798141f61eSdrh   Token *pColumnToken, /* Name of the column. */
5808141f61eSdrh   SrcList *pSrcList,   /* List of tables used to resolve column names */
5818141f61eSdrh   ExprList *pEList,    /* List of expressions used to resolve "AS" */
5828141f61eSdrh   Expr *pExpr          /* Make this EXPR node point to the selected column */
5838141f61eSdrh ){
5848141f61eSdrh   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
5858141f61eSdrh   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
5868141f61eSdrh   char *zCol = 0;      /* Name of the column.  The "Z" */
5878141f61eSdrh   int i, j;            /* Loop counters */
5888141f61eSdrh   int cnt = 0;         /* Number of matching column names */
5898141f61eSdrh   int cntTab = 0;      /* Number of matching table names */
5907e26d750Sdrh   sqlite *db = pParse->db;  /* The database */
5918141f61eSdrh 
5928141f61eSdrh   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
593a99db3b6Sdrh   zDb = sqlite3NameFromToken(pDbToken);
594a99db3b6Sdrh   zTab = sqlite3NameFromToken(pTableToken);
595a99db3b6Sdrh   zCol = sqlite3NameFromToken(pColumnToken);
59624b03fd0Sdanielk1977   if( sqlite3_malloc_failed ){
5978141f61eSdrh     return 1;  /* Leak memory (zDb and zTab) if malloc fails */
5988141f61eSdrh   }
5998141f61eSdrh   assert( zTab==0 || pEList==0 );
6008141f61eSdrh 
6018141f61eSdrh   pExpr->iTable = -1;
6028141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
6038141f61eSdrh     struct SrcList_item *pItem = &pSrcList->a[i];
6048141f61eSdrh     Table *pTab = pItem->pTab;
6058141f61eSdrh     Column *pCol;
6068141f61eSdrh 
6078141f61eSdrh     if( pTab==0 ) continue;
6088141f61eSdrh     assert( pTab->nCol>0 );
6098141f61eSdrh     if( zTab ){
6108141f61eSdrh       if( pItem->zAlias ){
6118141f61eSdrh         char *zTabName = pItem->zAlias;
6124adee20fSdanielk1977         if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
6138141f61eSdrh       }else{
6148141f61eSdrh         char *zTabName = pTab->zName;
6154adee20fSdanielk1977         if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
6164adee20fSdanielk1977         if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
6178141f61eSdrh           continue;
6188141f61eSdrh         }
6198141f61eSdrh       }
6208141f61eSdrh     }
6218141f61eSdrh     if( 0==(cntTab++) ){
6228141f61eSdrh       pExpr->iTable = pItem->iCursor;
6238141f61eSdrh       pExpr->iDb = pTab->iDb;
6248141f61eSdrh     }
6258141f61eSdrh     for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
6264adee20fSdanielk1977       if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
6278141f61eSdrh         cnt++;
6288141f61eSdrh         pExpr->iTable = pItem->iCursor;
6298141f61eSdrh         pExpr->iDb = pTab->iDb;
6308141f61eSdrh         /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
6318141f61eSdrh         pExpr->iColumn = j==pTab->iPKey ? -1 : j;
632a37cdde0Sdanielk1977         pExpr->affinity = pTab->aCol[j].affinity;
6330202b29eSdanielk1977         pExpr->pColl = pTab->aCol[j].pColl;
6348141f61eSdrh         break;
6358141f61eSdrh       }
6368141f61eSdrh     }
6378141f61eSdrh   }
6388141f61eSdrh 
6398141f61eSdrh   /* If we have not already resolved the name, then maybe
6408141f61eSdrh   ** it is a new.* or old.* trigger argument reference
6418141f61eSdrh   */
6428141f61eSdrh   if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
6438141f61eSdrh     TriggerStack *pTriggerStack = pParse->trigStack;
6448141f61eSdrh     Table *pTab = 0;
6454adee20fSdanielk1977     if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
6468141f61eSdrh       pExpr->iTable = pTriggerStack->newIdx;
6478141f61eSdrh       assert( pTriggerStack->pTab );
6488141f61eSdrh       pTab = pTriggerStack->pTab;
6494adee20fSdanielk1977     }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
6508141f61eSdrh       pExpr->iTable = pTriggerStack->oldIdx;
6518141f61eSdrh       assert( pTriggerStack->pTab );
6528141f61eSdrh       pTab = pTriggerStack->pTab;
6538141f61eSdrh     }
6548141f61eSdrh 
6558141f61eSdrh     if( pTab ){
6568141f61eSdrh       int j;
6578141f61eSdrh       Column *pCol = pTab->aCol;
6588141f61eSdrh 
6598141f61eSdrh       pExpr->iDb = pTab->iDb;
6608141f61eSdrh       cntTab++;
6618141f61eSdrh       for(j=0; j < pTab->nCol; j++, pCol++) {
6624adee20fSdanielk1977         if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
6638141f61eSdrh           cnt++;
6648141f61eSdrh           pExpr->iColumn = j==pTab->iPKey ? -1 : j;
665a37cdde0Sdanielk1977           pExpr->affinity = pTab->aCol[j].affinity;
6660202b29eSdanielk1977           pExpr->pColl = pTab->aCol[j].pColl;
6678141f61eSdrh           break;
6688141f61eSdrh         }
6698141f61eSdrh       }
6708141f61eSdrh     }
6718141f61eSdrh   }
6728141f61eSdrh 
6738141f61eSdrh   /*
6748141f61eSdrh   ** Perhaps the name is a reference to the ROWID
6758141f61eSdrh   */
6764adee20fSdanielk1977   if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
6778141f61eSdrh     cnt = 1;
6788141f61eSdrh     pExpr->iColumn = -1;
679a37cdde0Sdanielk1977     pExpr->affinity = SQLITE_AFF_INTEGER;
6808141f61eSdrh   }
6818141f61eSdrh 
6828141f61eSdrh   /*
6838141f61eSdrh   ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
6848141f61eSdrh   ** might refer to an result-set alias.  This happens, for example, when
6858141f61eSdrh   ** we are resolving names in the WHERE clause of the following command:
6868141f61eSdrh   **
6878141f61eSdrh   **     SELECT a+b AS x FROM table WHERE x<10;
6888141f61eSdrh   **
6898141f61eSdrh   ** In cases like this, replace pExpr with a copy of the expression that
6908141f61eSdrh   ** forms the result set entry ("a+b" in the example) and return immediately.
6918141f61eSdrh   ** Note that the expression in the result set should have already been
6928141f61eSdrh   ** resolved by the time the WHERE clause is resolved.
6938141f61eSdrh   */
6948141f61eSdrh   if( cnt==0 && pEList!=0 ){
6958141f61eSdrh     for(j=0; j<pEList->nExpr; j++){
6968141f61eSdrh       char *zAs = pEList->a[j].zName;
6974adee20fSdanielk1977       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
6988141f61eSdrh         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
6998141f61eSdrh         pExpr->op = TK_AS;
7008141f61eSdrh         pExpr->iColumn = j;
7014adee20fSdanielk1977         pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
7028141f61eSdrh         sqliteFree(zCol);
7038141f61eSdrh         assert( zTab==0 && zDb==0 );
7048141f61eSdrh         return 0;
7058141f61eSdrh       }
7068141f61eSdrh     }
7078141f61eSdrh   }
7088141f61eSdrh 
7098141f61eSdrh   /*
7108141f61eSdrh   ** If X and Y are NULL (in other words if only the column name Z is
7118141f61eSdrh   ** supplied) and the value of Z is enclosed in double-quotes, then
7128141f61eSdrh   ** Z is a string literal if it doesn't match any column names.  In that
7138141f61eSdrh   ** case, we need to return right away and not make any changes to
7148141f61eSdrh   ** pExpr.
7158141f61eSdrh   */
7168141f61eSdrh   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
7178141f61eSdrh     sqliteFree(zCol);
7188141f61eSdrh     return 0;
7198141f61eSdrh   }
7208141f61eSdrh 
7218141f61eSdrh   /*
7228141f61eSdrh   ** cnt==0 means there was not match.  cnt>1 means there were two or
7238141f61eSdrh   ** more matches.  Either way, we have an error.
7248141f61eSdrh   */
7258141f61eSdrh   if( cnt!=1 ){
7268141f61eSdrh     char *z = 0;
7278141f61eSdrh     char *zErr;
7288141f61eSdrh     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
7298141f61eSdrh     if( zDb ){
7304adee20fSdanielk1977       sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
7318141f61eSdrh     }else if( zTab ){
7324adee20fSdanielk1977       sqlite3SetString(&z, zTab, ".", zCol, 0);
7338141f61eSdrh     }else{
7348141f61eSdrh       z = sqliteStrDup(zCol);
7358141f61eSdrh     }
7364adee20fSdanielk1977     sqlite3ErrorMsg(pParse, zErr, z);
7378141f61eSdrh     sqliteFree(z);
7388141f61eSdrh   }
7398141f61eSdrh 
7408141f61eSdrh   /* Clean up and return
7418141f61eSdrh   */
7428141f61eSdrh   sqliteFree(zDb);
7438141f61eSdrh   sqliteFree(zTab);
7448141f61eSdrh   sqliteFree(zCol);
7454adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pLeft);
7468141f61eSdrh   pExpr->pLeft = 0;
7474adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pRight);
7488141f61eSdrh   pExpr->pRight = 0;
7498141f61eSdrh   pExpr->op = TK_COLUMN;
7504adee20fSdanielk1977   sqlite3AuthRead(pParse, pExpr, pSrcList);
7518141f61eSdrh   return cnt!=1;
7528141f61eSdrh }
7538141f61eSdrh 
7548141f61eSdrh /*
755cce7d176Sdrh ** This routine walks an expression tree and resolves references to
756967e8b73Sdrh ** table columns.  Nodes of the form ID.ID or ID resolve into an
757aacc543eSdrh ** index to the table in the table list and a column offset.  The
758aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
759aacc543eSdrh ** value is changed to the index of the referenced table in pTabList
760832508b7Sdrh ** plus the "base" value.  The base value will ultimately become the
761aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced
762aacc543eSdrh ** table.  The Expr.iColumn value is changed to the index of the column
763aacc543eSdrh ** of the referenced table.  The Expr.iColumn value for the special
764aacc543eSdrh ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
765aacc543eSdrh ** alias for ROWID.
76619a775c2Sdrh **
767fef5208cSdrh ** We also check for instances of the IN operator.  IN comes in two
768fef5208cSdrh ** forms:
769fef5208cSdrh **
770fef5208cSdrh **           expr IN (exprlist)
771fef5208cSdrh ** and
772fef5208cSdrh **           expr IN (SELECT ...)
773fef5208cSdrh **
774fef5208cSdrh ** The first form is handled by creating a set holding the list
775fef5208cSdrh ** of allowed values.  The second form causes the SELECT to generate
776fef5208cSdrh ** a temporary table.
777fef5208cSdrh **
778fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression.
77919a775c2Sdrh ** If it finds any, it generates code to write the value of that select
78019a775c2Sdrh ** into a memory cell.
781cce7d176Sdrh **
782967e8b73Sdrh ** Unknown columns or tables provoke an error.  The function returns
783cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg.
784cce7d176Sdrh */
7854adee20fSdanielk1977 int sqlite3ExprResolveIds(
786a2e00042Sdrh   Parse *pParse,     /* The parser context */
7878141f61eSdrh   SrcList *pSrcList, /* List of tables used to resolve column names */
788a2e00042Sdrh   ExprList *pEList,  /* List of expressions used to resolve "AS" */
789a2e00042Sdrh   Expr *pExpr        /* The expression to be analyzed. */
790a2e00042Sdrh ){
7916a3ea0e6Sdrh   int i;
7926a3ea0e6Sdrh 
7938141f61eSdrh   if( pExpr==0 || pSrcList==0 ) return 0;
7948141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
7958141f61eSdrh     assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
7966a3ea0e6Sdrh   }
797cce7d176Sdrh   switch( pExpr->op ){
7982398937bSdrh     /* Double-quoted strings (ex: "abc") are used as identifiers if
7992398937bSdrh     ** possible.  Otherwise they remain as strings.  Single-quoted
8002398937bSdrh     ** strings (ex: 'abc') are always string literals.
8012398937bSdrh     */
8022398937bSdrh     case TK_STRING: {
8032398937bSdrh       if( pExpr->token.z[0]=='\'' ) break;
8042398937bSdrh       /* Fall thru into the TK_ID case if this is a double-quoted string */
8052398937bSdrh     }
8068141f61eSdrh     /* A lone identifier is the name of a columnd.
807a2e00042Sdrh     */
808cce7d176Sdrh     case TK_ID: {
8098141f61eSdrh       if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
810cce7d176Sdrh         return 1;
811ed6c8671Sdrh       }
812cce7d176Sdrh       break;
813cce7d176Sdrh     }
814cce7d176Sdrh 
815d24cc427Sdrh     /* A table name and column name:     ID.ID
816d24cc427Sdrh     ** Or a database, table and column:  ID.ID.ID
817d24cc427Sdrh     */
818cce7d176Sdrh     case TK_DOT: {
8198141f61eSdrh       Token *pColumn;
8208141f61eSdrh       Token *pTable;
8218141f61eSdrh       Token *pDb;
8228141f61eSdrh       Expr *pRight;
823cce7d176Sdrh 
824cce7d176Sdrh       pRight = pExpr->pRight;
825d24cc427Sdrh       if( pRight->op==TK_ID ){
8268141f61eSdrh         pDb = 0;
8278141f61eSdrh         pTable = &pExpr->pLeft->token;
8288141f61eSdrh         pColumn = &pRight->token;
829d24cc427Sdrh       }else{
8308141f61eSdrh         assert( pRight->op==TK_DOT );
8318141f61eSdrh         pDb = &pExpr->pLeft->token;
8328141f61eSdrh         pTable = &pRight->pLeft->token;
8338141f61eSdrh         pColumn = &pRight->pRight->token;
834d24cc427Sdrh       }
8358141f61eSdrh       if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
836daffd0e5Sdrh         return 1;
837daffd0e5Sdrh       }
838cce7d176Sdrh       break;
839cce7d176Sdrh     }
840cce7d176Sdrh 
841fef5208cSdrh     case TK_IN: {
842e014a838Sdanielk1977       char affinity;
8434adee20fSdanielk1977       Vdbe *v = sqlite3GetVdbe(pParse);
844d3d39e93Sdrh       KeyInfo keyInfo;
8450202b29eSdanielk1977       int addr;        /* Address of OP_OpenTemp instruction */
846d3d39e93Sdrh 
847fef5208cSdrh       if( v==0 ) return 1;
8484adee20fSdanielk1977       if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
849cfab11bcSdrh         return 1;
850cfab11bcSdrh       }
851bf3b721fSdanielk1977       affinity = sqlite3ExprAffinity(pExpr->pLeft);
852e014a838Sdanielk1977 
853e014a838Sdanielk1977       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
854e014a838Sdanielk1977       ** expression it is handled the same way. A temporary table is
855e014a838Sdanielk1977       ** filled with single-field index keys representing the results
856e014a838Sdanielk1977       ** from the SELECT or the <exprlist>.
857fef5208cSdrh       **
858e014a838Sdanielk1977       ** If the 'x' expression is a column value, or the SELECT...
859e014a838Sdanielk1977       ** statement returns a column value, then the affinity of that
860e014a838Sdanielk1977       ** column is used to build the index keys. If both 'x' and the
861e014a838Sdanielk1977       ** SELECT... statement are columns, then numeric affinity is used
862e014a838Sdanielk1977       ** if either column has NUMERIC or INTEGER affinity. If neither
863e014a838Sdanielk1977       ** 'x' nor the SELECT... statement are columns, then numeric affinity
864e014a838Sdanielk1977       ** is used.
865fef5208cSdrh       */
866832508b7Sdrh       pExpr->iTable = pParse->nTab++;
8670202b29eSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
868d3d39e93Sdrh       memset(&keyInfo, 0, sizeof(keyInfo));
869d3d39e93Sdrh       keyInfo.nField = 1;
870f3218feaSdrh       sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
871e014a838Sdanielk1977 
872e014a838Sdanielk1977       if( pExpr->pSelect ){
873e014a838Sdanielk1977         /* Case 1:     expr IN (SELECT ...)
874e014a838Sdanielk1977         **
875e014a838Sdanielk1977         ** Generate code to write the results of the select into the temporary
876e014a838Sdanielk1977         ** table allocated and opened above.
877e014a838Sdanielk1977         */
878e014a838Sdanielk1977         int iParm = pExpr->iTable +  (((int)affinity)<<16);
879be5c89acSdrh         ExprList *pEList;
880e014a838Sdanielk1977         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
881bf3b721fSdanielk1977         sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
882be5c89acSdrh         pEList = pExpr->pSelect->pEList;
883be5c89acSdrh         if( pEList && pEList->nExpr>0 ){
8847cedc8d4Sdanielk1977           keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
885be5c89acSdrh               pEList->a[0].pExpr);
8860202b29eSdanielk1977         }
887fef5208cSdrh       }else if( pExpr->pList ){
888fef5208cSdrh         /* Case 2:     expr IN (exprlist)
889fef5208cSdrh         **
890e014a838Sdanielk1977 	** For each expression, build an index key from the evaluation and
891e014a838Sdanielk1977         ** store it in the temporary table. If <expr> is a column, then use
892e014a838Sdanielk1977         ** that columns affinity when building index keys. If <expr> is not
893e014a838Sdanielk1977         ** a column, use numeric affinity.
894fef5208cSdrh         */
895e014a838Sdanielk1977         int i;
896e014a838Sdanielk1977         char const *affStr;
897e014a838Sdanielk1977         if( !affinity ){
898e014a838Sdanielk1977           affinity = SQLITE_AFF_NUMERIC;
899e014a838Sdanielk1977         }
900e014a838Sdanielk1977         affStr = sqlite3AffinityString(affinity);
9010202b29eSdanielk1977         keyInfo.aColl[0] = pExpr->pLeft->pColl;
902e014a838Sdanielk1977 
903e014a838Sdanielk1977         /* Loop through each expression in <exprlist>. */
904fef5208cSdrh         for(i=0; i<pExpr->pList->nExpr; i++){
905fef5208cSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
906e014a838Sdanielk1977 
907e014a838Sdanielk1977           /* Check that the expression is constant and valid. */
9084adee20fSdanielk1977           if( !sqlite3ExprIsConstant(pE2) ){
9094adee20fSdanielk1977             sqlite3ErrorMsg(pParse,
910da93d238Sdrh               "right-hand side of IN operator must be constant");
911fef5208cSdrh             return 1;
912fef5208cSdrh           }
9134adee20fSdanielk1977           if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
9144794b980Sdrh             return 1;
9154794b980Sdrh           }
916e014a838Sdanielk1977 
917e014a838Sdanielk1977           /* Evaluate the expression and insert it into the temp table */
9184adee20fSdanielk1977           sqlite3ExprCode(pParse, pE2);
919ededfd5eSdanielk1977           sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
9200f69c1e3Sdanielk1977           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
921e014a838Sdanielk1977           sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
922fef5208cSdrh         }
923fef5208cSdrh       }
9240202b29eSdanielk1977       sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
9250202b29eSdanielk1977 
926cfab11bcSdrh       break;
927fef5208cSdrh     }
928fef5208cSdrh 
92919a775c2Sdrh     case TK_SELECT: {
930fef5208cSdrh       /* This has to be a scalar SELECT.  Generate code to put the
931fef5208cSdrh       ** value of this select in a memory cell and record the number
932967e8b73Sdrh       ** of the memory cell in iColumn.
933fef5208cSdrh       */
934967e8b73Sdrh       pExpr->iColumn = pParse->nMem++;
935bf3b721fSdanielk1977       if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
93619a775c2Sdrh         return 1;
93719a775c2Sdrh       }
93819a775c2Sdrh       break;
93919a775c2Sdrh     }
94019a775c2Sdrh 
941cce7d176Sdrh     /* For all else, just recursively walk the tree */
942cce7d176Sdrh     default: {
943cce7d176Sdrh       if( pExpr->pLeft
9444adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
945cce7d176Sdrh         return 1;
946cce7d176Sdrh       }
947cce7d176Sdrh       if( pExpr->pRight
9484adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
949cce7d176Sdrh         return 1;
950cce7d176Sdrh       }
951cce7d176Sdrh       if( pExpr->pList ){
952cce7d176Sdrh         int i;
953cce7d176Sdrh         ExprList *pList = pExpr->pList;
954cce7d176Sdrh         for(i=0; i<pList->nExpr; i++){
955832508b7Sdrh           Expr *pArg = pList->a[i].pExpr;
9564adee20fSdanielk1977           if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
957cce7d176Sdrh             return 1;
958cce7d176Sdrh           }
959cce7d176Sdrh         }
960cce7d176Sdrh       }
961cce7d176Sdrh     }
962cce7d176Sdrh   }
963cce7d176Sdrh   return 0;
964cce7d176Sdrh }
965cce7d176Sdrh 
966cce7d176Sdrh /*
9674b59ab5eSdrh ** pExpr is a node that defines a function of some kind.  It might
9684b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function
9694b59ab5eSdrh ** that implements an operator, like "a LIKE b".
9704b59ab5eSdrh **
9714b59ab5eSdrh ** This routine makes *pzName point to the name of the function and
9724b59ab5eSdrh ** *pnName hold the number of characters in the function name.
9734b59ab5eSdrh */
9744b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
9754b59ab5eSdrh   switch( pExpr->op ){
9764b59ab5eSdrh     case TK_FUNCTION: {
9774b59ab5eSdrh       *pzName = pExpr->token.z;
9786977fea8Sdrh       *pnName = pExpr->token.n;
9794b59ab5eSdrh       break;
9804b59ab5eSdrh     }
9814b59ab5eSdrh     case TK_LIKE: {
9824b59ab5eSdrh       *pzName = "like";
9834b59ab5eSdrh       *pnName = 4;
9844b59ab5eSdrh       break;
9854b59ab5eSdrh     }
9864b59ab5eSdrh     case TK_GLOB: {
9874b59ab5eSdrh       *pzName = "glob";
9884b59ab5eSdrh       *pnName = 4;
9894b59ab5eSdrh       break;
9904b59ab5eSdrh     }
9914b59ab5eSdrh     default: {
9924b59ab5eSdrh       *pzName = "can't happen";
9934b59ab5eSdrh       *pnName = 12;
9944b59ab5eSdrh       break;
9954b59ab5eSdrh     }
9964b59ab5eSdrh   }
9974b59ab5eSdrh }
9984b59ab5eSdrh 
9994b59ab5eSdrh /*
1000cce7d176Sdrh ** Error check the functions in an expression.  Make sure all
1001cce7d176Sdrh ** function names are recognized and all functions have the correct
1002cce7d176Sdrh ** number of arguments.  Leave an error message in pParse->zErrMsg
1003cce7d176Sdrh ** if anything is amiss.  Return the number of errors.
1004cce7d176Sdrh **
1005cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function
1006cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1007cce7d176Sdrh */
10084adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
1009cce7d176Sdrh   int nErr = 0;
1010cce7d176Sdrh   if( pExpr==0 ) return 0;
1011cce7d176Sdrh   switch( pExpr->op ){
10124b59ab5eSdrh     case TK_GLOB:
10134b59ab5eSdrh     case TK_LIKE:
1014cce7d176Sdrh     case TK_FUNCTION: {
1015c9b84a1fSdrh       int n = pExpr->pList ? pExpr->pList->nExpr : 0;  /* Number of arguments */
1016c9b84a1fSdrh       int no_such_func = 0;       /* True if no such function exists */
1017c9b84a1fSdrh       int wrong_num_args = 0;     /* True if wrong number of arguments */
1018c9b84a1fSdrh       int is_agg = 0;             /* True if is an aggregate function */
1019cce7d176Sdrh       int i;
10204b59ab5eSdrh       int nId;                    /* Number of characters in function name */
10214b59ab5eSdrh       const char *zId;            /* The function name. */
10220bce8354Sdrh       FuncDef *pDef;
1023d8123366Sdanielk1977       int enc = pParse->db->enc;
10240bce8354Sdrh 
10254b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1026d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
10270bce8354Sdrh       if( pDef==0 ){
1028d8123366Sdanielk1977         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
10290bce8354Sdrh         if( pDef==0 ){
1030cce7d176Sdrh           no_such_func = 1;
10318e0a2f90Sdrh         }else{
10328e0a2f90Sdrh           wrong_num_args = 1;
10338e0a2f90Sdrh         }
10348e0a2f90Sdrh       }else{
10350bce8354Sdrh         is_agg = pDef->xFunc==0;
1036cce7d176Sdrh       }
10378e0a2f90Sdrh       if( is_agg && !allowAgg ){
10384adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
10398e0a2f90Sdrh         nErr++;
10408e0a2f90Sdrh         is_agg = 0;
10418e0a2f90Sdrh       }else if( no_such_func ){
10424adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1043cce7d176Sdrh         nErr++;
10448e0a2f90Sdrh       }else if( wrong_num_args ){
10454adee20fSdanielk1977         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1046f7a9e1acSdrh              nId, zId);
10478e0a2f90Sdrh         nErr++;
1048cce7d176Sdrh       }
1049f7a9e1acSdrh       if( is_agg ){
1050f7a9e1acSdrh         pExpr->op = TK_AGG_FUNCTION;
1051f7a9e1acSdrh         if( pIsAgg ) *pIsAgg = 1;
1052f7a9e1acSdrh       }
1053cce7d176Sdrh       for(i=0; nErr==0 && i<n; i++){
10544adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
10554cfa7934Sdrh                                allowAgg && !is_agg, pIsAgg);
1056cce7d176Sdrh       }
10570202b29eSdanielk1977       /* FIX ME:  Compute pExpr->affinity based on the expected return
10580202b29eSdanielk1977       ** type of the function
10590202b29eSdanielk1977       */
1060cce7d176Sdrh     }
1061cce7d176Sdrh     default: {
1062cce7d176Sdrh       if( pExpr->pLeft ){
10634adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
1064cce7d176Sdrh       }
1065cce7d176Sdrh       if( nErr==0 && pExpr->pRight ){
10664adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
1067cce7d176Sdrh       }
1068fef5208cSdrh       if( nErr==0 && pExpr->pList ){
1069fef5208cSdrh         int n = pExpr->pList->nExpr;
1070fef5208cSdrh         int i;
1071fef5208cSdrh         for(i=0; nErr==0 && i<n; i++){
10722282792aSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
10734adee20fSdanielk1977           nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
1074fef5208cSdrh         }
1075fef5208cSdrh       }
1076cce7d176Sdrh       break;
1077cce7d176Sdrh     }
1078cce7d176Sdrh   }
1079cce7d176Sdrh   return nErr;
1080cce7d176Sdrh }
1081cce7d176Sdrh 
1082cce7d176Sdrh /*
1083fec19aadSdrh ** Generate an instruction that will put the integer describe by
1084fec19aadSdrh ** text z[0..n-1] on the stack.
1085fec19aadSdrh */
1086fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){
1087fec19aadSdrh   int i;
10886fec0762Sdrh   if( sqlite3GetInt32(z, &i) ){
10896fec0762Sdrh     sqlite3VdbeAddOp(v, OP_Integer, i, 0);
10906fec0762Sdrh   }else if( sqlite3FitsIn64Bits(z) ){
10916fec0762Sdrh     sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
1092fec19aadSdrh   }else{
1093fec19aadSdrh     sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1094fec19aadSdrh   }
1095fec19aadSdrh }
1096fec19aadSdrh 
1097fec19aadSdrh /*
1098cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given
10991ccde15dSdrh ** expression and leave the result on the top of stack.
1100cce7d176Sdrh */
11014adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1102cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1103cce7d176Sdrh   int op;
1104daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1105cce7d176Sdrh   switch( pExpr->op ){
1106cce7d176Sdrh     case TK_PLUS:     op = OP_Add;      break;
1107cce7d176Sdrh     case TK_MINUS:    op = OP_Subtract; break;
1108cce7d176Sdrh     case TK_STAR:     op = OP_Multiply; break;
1109cce7d176Sdrh     case TK_SLASH:    op = OP_Divide;   break;
1110cce7d176Sdrh     case TK_AND:      op = OP_And;      break;
1111cce7d176Sdrh     case TK_OR:       op = OP_Or;       break;
1112cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1113cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1114cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1115cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1116cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1117cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1118cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1119cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1120cce7d176Sdrh     case TK_NOT:      op = OP_Not;      break;
1121cce7d176Sdrh     case TK_UMINUS:   op = OP_Negative; break;
1122bf4133cbSdrh     case TK_BITAND:   op = OP_BitAnd;   break;
1123bf4133cbSdrh     case TK_BITOR:    op = OP_BitOr;    break;
1124bf4133cbSdrh     case TK_BITNOT:   op = OP_BitNot;   break;
1125bf4133cbSdrh     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
1126bf4133cbSdrh     case TK_RSHIFT:   op = OP_ShiftRight; break;
1127bf4133cbSdrh     case TK_REM:      op = OP_Remainder;  break;
1128fec19aadSdrh     case TK_FLOAT:    op = OP_Real;       break;
11290f69c1e3Sdanielk1977     case TK_STRING:   op = OP_String8;     break;
1130c572ef7fSdanielk1977     case TK_BLOB:     op = OP_HexBlob;    break;
1131cfe9a69fSdanielk1977     default: op = 0; break;
1132cce7d176Sdrh   }
1133cce7d176Sdrh   switch( pExpr->op ){
1134967e8b73Sdrh     case TK_COLUMN: {
11352282792aSdrh       if( pParse->useAgg ){
11364adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1137c4a3c779Sdrh       }else if( pExpr->iColumn>=0 ){
11384adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1139c4a3c779Sdrh       }else{
11404adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
11412282792aSdrh       }
1142cce7d176Sdrh       break;
1143cce7d176Sdrh     }
1144cce7d176Sdrh     case TK_INTEGER: {
1145fec19aadSdrh       codeInteger(v, pExpr->token.z, pExpr->token.n);
1146fec19aadSdrh       break;
114751e9a445Sdrh     }
1148fec19aadSdrh     case TK_FLOAT:
1149fec19aadSdrh     case TK_STRING: {
1150fec19aadSdrh       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
11514adee20fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1152cce7d176Sdrh       break;
1153cce7d176Sdrh     }
1154c572ef7fSdanielk1977     case TK_BLOB: {
1155c572ef7fSdanielk1977       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1156c572ef7fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1157c572ef7fSdanielk1977       break;
1158c572ef7fSdanielk1977     }
1159cce7d176Sdrh     case TK_NULL: {
11600f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1161cce7d176Sdrh       break;
1162cce7d176Sdrh     }
116350457896Sdrh     case TK_VARIABLE: {
11644adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
116550457896Sdrh       break;
116650457896Sdrh     }
1167c9b84a1fSdrh     case TK_LT:
1168c9b84a1fSdrh     case TK_LE:
1169c9b84a1fSdrh     case TK_GT:
1170c9b84a1fSdrh     case TK_GE:
1171c9b84a1fSdrh     case TK_NE:
1172c9b84a1fSdrh     case TK_EQ: {
1173a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1174a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1175be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
1176a37cdde0Sdanielk1977       break;
1177c9b84a1fSdrh     }
1178cce7d176Sdrh     case TK_AND:
1179cce7d176Sdrh     case TK_OR:
1180cce7d176Sdrh     case TK_PLUS:
1181cce7d176Sdrh     case TK_STAR:
1182cce7d176Sdrh     case TK_MINUS:
1183bf4133cbSdrh     case TK_REM:
1184bf4133cbSdrh     case TK_BITAND:
1185bf4133cbSdrh     case TK_BITOR:
118617c40294Sdrh     case TK_SLASH:
1187bf4133cbSdrh     case TK_LSHIFT:
1188bf4133cbSdrh     case TK_RSHIFT: {
11894adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
119017c40294Sdrh       sqlite3ExprCode(pParse, pExpr->pRight);
11914adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1192bf4133cbSdrh       break;
1193bf4133cbSdrh     }
11940040077dSdrh     case TK_CONCAT: {
11954adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
11964adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
119772c952a1Sdanielk1977       sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
11980040077dSdrh       break;
11990040077dSdrh     }
1200cce7d176Sdrh     case TK_UMINUS: {
1201fec19aadSdrh       Expr *pLeft = pExpr->pLeft;
1202fec19aadSdrh       assert( pLeft );
1203fec19aadSdrh       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1204fec19aadSdrh         Token *p = &pLeft->token;
12056e142f54Sdrh         char *z = sqliteMalloc( p->n + 2 );
12066e142f54Sdrh         sprintf(z, "-%.*s", p->n, p->z);
1207fec19aadSdrh         if( pLeft->op==TK_FLOAT ){
1208fec19aadSdrh           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1209e6840900Sdrh         }else{
1210fec19aadSdrh           codeInteger(v, z, p->n+1);
1211e6840900Sdrh         }
12126e142f54Sdrh         sqliteFree(z);
12136e142f54Sdrh         break;
12146e142f54Sdrh       }
12151ccde15dSdrh       /* Fall through into TK_NOT */
12166e142f54Sdrh     }
1217bf4133cbSdrh     case TK_BITNOT:
12186e142f54Sdrh     case TK_NOT: {
12194adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12204adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1221cce7d176Sdrh       break;
1222cce7d176Sdrh     }
1223cce7d176Sdrh     case TK_ISNULL:
1224cce7d176Sdrh     case TK_NOTNULL: {
1225cce7d176Sdrh       int dest;
12264adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
12274adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12284adee20fSdanielk1977       dest = sqlite3VdbeCurrentAddr(v) + 2;
12294adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
12304adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1231cce7d176Sdrh     }
1232a37cdde0Sdanielk1977     break;
12332282792aSdrh     case TK_AGG_FUNCTION: {
12344adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
12352282792aSdrh       break;
12362282792aSdrh     }
12374b59ab5eSdrh     case TK_GLOB:
12384b59ab5eSdrh     case TK_LIKE:
1239cce7d176Sdrh     case TK_FUNCTION: {
1240cce7d176Sdrh       ExprList *pList = pExpr->pList;
124189425d5eSdrh       int nExpr = pList ? pList->nExpr : 0;
12420bce8354Sdrh       FuncDef *pDef;
12434b59ab5eSdrh       int nId;
12444b59ab5eSdrh       const char *zId;
1245682f68b0Sdanielk1977       int p2 = 0;
1246682f68b0Sdanielk1977       int i;
1247d8123366Sdanielk1977       u8 enc = pParse->db->enc;
1248dc1bdc4fSdanielk1977       CollSeq *pColl = 0;
12494b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1250d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
12510bce8354Sdrh       assert( pDef!=0 );
1252f9b596ebSdrh       nExpr = sqlite3ExprCodeExprList(pParse, pList);
1253682f68b0Sdanielk1977       for(i=0; i<nExpr && i<32; i++){
1254d02eb1fdSdanielk1977         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1255d02eb1fdSdanielk1977           p2 |= (1<<i);
1256d02eb1fdSdanielk1977         }
1257dc1bdc4fSdanielk1977         if( pDef->needCollSeq && !pColl ){
1258dc1bdc4fSdanielk1977           pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1259dc1bdc4fSdanielk1977         }
1260dc1bdc4fSdanielk1977       }
1261dc1bdc4fSdanielk1977       if( pDef->needCollSeq ){
1262dc1bdc4fSdanielk1977         if( !pColl ) pColl = pParse->db->pDfltColl;
1263d8123366Sdanielk1977         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
1264682f68b0Sdanielk1977       }
1265682f68b0Sdanielk1977       sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
12666ec2733bSdrh       break;
12676ec2733bSdrh     }
126819a775c2Sdrh     case TK_SELECT: {
12694adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
127019a775c2Sdrh       break;
127119a775c2Sdrh     }
1272fef5208cSdrh     case TK_IN: {
1273fef5208cSdrh       int addr;
1274e014a838Sdanielk1977       char const *affStr;
1275e014a838Sdanielk1977 
1276e014a838Sdanielk1977       /* Figure out the affinity to use to create a key from the results
1277e014a838Sdanielk1977       ** of the expression. affinityStr stores a static string suitable for
1278ededfd5eSdanielk1977       ** P3 of OP_MakeRecord.
1279e014a838Sdanielk1977       */
1280e014a838Sdanielk1977       affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1281e014a838Sdanielk1977 
12824adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1283e014a838Sdanielk1977 
1284e014a838Sdanielk1977       /* Code the <expr> from "<expr> IN (...)". The temporary table
1285e014a838Sdanielk1977       ** pExpr->iTable contains the values that make up the (...) set.
1286e014a838Sdanielk1977       */
12874adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12884adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1289e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */
12904adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
12910f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1292e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1293ededfd5eSdanielk1977       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1294e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1295e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */
1296e014a838Sdanielk1977 
1297fef5208cSdrh       break;
1298fef5208cSdrh     }
1299fef5208cSdrh     case TK_BETWEEN: {
1300be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1301be5c89acSdrh       struct ExprList_item *pLItem = pExpr->pList->a;
1302be5c89acSdrh       Expr *pRight = pLItem->pExpr;
1303be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
13044adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1305be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1306be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
13074adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1308be5c89acSdrh       pLItem++;
1309be5c89acSdrh       pRight = pLItem->pExpr;
1310be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1311be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
13124adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_And, 0, 0);
1313fef5208cSdrh       break;
1314fef5208cSdrh     }
131551e9a445Sdrh     case TK_UPLUS:
1316a2e00042Sdrh     case TK_AS: {
13174adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1318a2e00042Sdrh       break;
1319a2e00042Sdrh     }
132017a7f8ddSdrh     case TK_CASE: {
132117a7f8ddSdrh       int expr_end_label;
1322f5905aa7Sdrh       int jumpInst;
1323f5905aa7Sdrh       int addr;
1324f5905aa7Sdrh       int nExpr;
132517a7f8ddSdrh       int i;
1326be5c89acSdrh       ExprList *pEList;
1327be5c89acSdrh       struct ExprList_item *aListelem;
132817a7f8ddSdrh 
132917a7f8ddSdrh       assert(pExpr->pList);
133017a7f8ddSdrh       assert((pExpr->pList->nExpr % 2) == 0);
133117a7f8ddSdrh       assert(pExpr->pList->nExpr > 0);
1332be5c89acSdrh       pEList = pExpr->pList;
1333be5c89acSdrh       aListelem = pEList->a;
1334be5c89acSdrh       nExpr = pEList->nExpr;
13354adee20fSdanielk1977       expr_end_label = sqlite3VdbeMakeLabel(v);
133617a7f8ddSdrh       if( pExpr->pLeft ){
13374adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pLeft);
1338cce7d176Sdrh       }
1339f5905aa7Sdrh       for(i=0; i<nExpr; i=i+2){
1340be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i].pExpr);
134117a7f8ddSdrh         if( pExpr->pLeft ){
13424adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1343be5c89acSdrh           jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1344be5c89acSdrh                                  OP_Ne, 0, 1);
13454adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1346f5905aa7Sdrh         }else{
13474adee20fSdanielk1977           jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
134817a7f8ddSdrh         }
1349be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
13504adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
13514adee20fSdanielk1977         addr = sqlite3VdbeCurrentAddr(v);
13524adee20fSdanielk1977         sqlite3VdbeChangeP2(v, jumpInst, addr);
135317a7f8ddSdrh       }
1354f570f011Sdrh       if( pExpr->pLeft ){
13554adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1356f570f011Sdrh       }
135717a7f8ddSdrh       if( pExpr->pRight ){
13584adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pRight);
135917a7f8ddSdrh       }else{
13600f69c1e3Sdanielk1977         sqlite3VdbeAddOp(v, OP_String8, 0, 0);
136117a7f8ddSdrh       }
13624adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, expr_end_label);
13636f34903eSdanielk1977       break;
13646f34903eSdanielk1977     }
13656f34903eSdanielk1977     case TK_RAISE: {
13666f34903eSdanielk1977       if( !pParse->trigStack ){
13674adee20fSdanielk1977         sqlite3ErrorMsg(pParse,
1368da93d238Sdrh                        "RAISE() may only be used within a trigger-program");
13696f34903eSdanielk1977         pParse->nErr++;
13706f34903eSdanielk1977 	return;
13716f34903eSdanielk1977       }
13726f34903eSdanielk1977       if( pExpr->iColumn == OE_Rollback ||
13736f34903eSdanielk1977 	  pExpr->iColumn == OE_Abort ||
13746f34903eSdanielk1977 	  pExpr->iColumn == OE_Fail ){
13754adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1376701a0aebSdrh                            pExpr->token.z, pExpr->token.n);
13774adee20fSdanielk1977 	  sqlite3VdbeDequoteP3(v, -1);
13786f34903eSdanielk1977       } else {
13796f34903eSdanielk1977 	  assert( pExpr->iColumn == OE_Ignore );
13804adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1381701a0aebSdrh                            "(IGNORE jump)", 0);
13826f34903eSdanielk1977       }
138317a7f8ddSdrh     }
138417a7f8ddSdrh     break;
138517a7f8ddSdrh   }
1386cce7d176Sdrh }
1387cce7d176Sdrh 
1388cce7d176Sdrh /*
1389268380caSdrh ** Generate code that pushes the value of every element of the given
1390f9b596ebSdrh ** expression list onto the stack.
1391268380caSdrh **
1392268380caSdrh ** Return the number of elements pushed onto the stack.
1393268380caSdrh */
13944adee20fSdanielk1977 int sqlite3ExprCodeExprList(
1395268380caSdrh   Parse *pParse,     /* Parsing context */
1396f9b596ebSdrh   ExprList *pList    /* The expression list to be coded */
1397268380caSdrh ){
1398268380caSdrh   struct ExprList_item *pItem;
1399268380caSdrh   int i, n;
1400268380caSdrh   Vdbe *v;
1401268380caSdrh   if( pList==0 ) return 0;
14024adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1403268380caSdrh   n = pList->nExpr;
1404268380caSdrh   for(pItem=pList->a, i=0; i<n; i++, pItem++){
14054adee20fSdanielk1977     sqlite3ExprCode(pParse, pItem->pExpr);
1406268380caSdrh   }
1407f9b596ebSdrh   return n;
1408268380caSdrh }
1409268380caSdrh 
1410268380caSdrh /*
1411cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made
1412cce7d176Sdrh ** to the label "dest" if the expression is true but execution
1413cce7d176Sdrh ** continues straight thru if the expression is false.
1414f5905aa7Sdrh **
1415f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then
1416f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true.
1417cce7d176Sdrh */
14184adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1419cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1420cce7d176Sdrh   int op = 0;
1421daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1422cce7d176Sdrh   switch( pExpr->op ){
1423cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1424cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1425cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1426cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1427cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1428cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1429cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1430cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1431cce7d176Sdrh     default:  break;
1432cce7d176Sdrh   }
1433cce7d176Sdrh   switch( pExpr->op ){
1434cce7d176Sdrh     case TK_AND: {
14354adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
14364adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
14374adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
14384adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1439cce7d176Sdrh       break;
1440cce7d176Sdrh     }
1441cce7d176Sdrh     case TK_OR: {
14424adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
14434adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1444cce7d176Sdrh       break;
1445cce7d176Sdrh     }
1446cce7d176Sdrh     case TK_NOT: {
14474adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1448cce7d176Sdrh       break;
1449cce7d176Sdrh     }
1450cce7d176Sdrh     case TK_LT:
1451cce7d176Sdrh     case TK_LE:
1452cce7d176Sdrh     case TK_GT:
1453cce7d176Sdrh     case TK_GE:
1454cce7d176Sdrh     case TK_NE:
14550ac65892Sdrh     case TK_EQ: {
14564adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14574adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1458be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1459cce7d176Sdrh       break;
1460cce7d176Sdrh     }
1461cce7d176Sdrh     case TK_ISNULL:
1462cce7d176Sdrh     case TK_NOTNULL: {
14634adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14644adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1465cce7d176Sdrh       break;
1466cce7d176Sdrh     }
1467fef5208cSdrh     case TK_BETWEEN: {
14680202b29eSdanielk1977       /* The expression "x BETWEEN y AND z" is implemented as:
14690202b29eSdanielk1977       **
14700202b29eSdanielk1977       ** 1 IF (x < y) GOTO 3
14710202b29eSdanielk1977       ** 2 IF (x <= z) GOTO <dest>
14720202b29eSdanielk1977       ** 3 ...
14730202b29eSdanielk1977       */
1474f5905aa7Sdrh       int addr;
1475be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1476be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1477be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
14784adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1479be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1480be5c89acSdrh       addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
14810202b29eSdanielk1977 
1482be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1483be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1484be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
14850202b29eSdanielk1977 
14864adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
14874adee20fSdanielk1977       sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
14884adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1489fef5208cSdrh       break;
1490fef5208cSdrh     }
1491cce7d176Sdrh     default: {
14924adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
14934adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
1494cce7d176Sdrh       break;
1495cce7d176Sdrh     }
1496cce7d176Sdrh   }
1497cce7d176Sdrh }
1498cce7d176Sdrh 
1499cce7d176Sdrh /*
150066b89c8fSdrh ** Generate code for a boolean expression such that a jump is made
1501cce7d176Sdrh ** to the label "dest" if the expression is false but execution
1502cce7d176Sdrh ** continues straight thru if the expression is true.
1503f5905aa7Sdrh **
1504f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then
1505f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1506cce7d176Sdrh */
15074adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1508cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1509cce7d176Sdrh   int op = 0;
1510daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1511cce7d176Sdrh   switch( pExpr->op ){
1512cce7d176Sdrh     case TK_LT:       op = OP_Ge;       break;
1513cce7d176Sdrh     case TK_LE:       op = OP_Gt;       break;
1514cce7d176Sdrh     case TK_GT:       op = OP_Le;       break;
1515cce7d176Sdrh     case TK_GE:       op = OP_Lt;       break;
1516cce7d176Sdrh     case TK_NE:       op = OP_Eq;       break;
1517cce7d176Sdrh     case TK_EQ:       op = OP_Ne;       break;
1518cce7d176Sdrh     case TK_ISNULL:   op = OP_NotNull;  break;
1519cce7d176Sdrh     case TK_NOTNULL:  op = OP_IsNull;   break;
1520cce7d176Sdrh     default:  break;
1521cce7d176Sdrh   }
1522cce7d176Sdrh   switch( pExpr->op ){
1523cce7d176Sdrh     case TK_AND: {
15244adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
15254adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1526cce7d176Sdrh       break;
1527cce7d176Sdrh     }
1528cce7d176Sdrh     case TK_OR: {
15294adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
15304adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
15314adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
15324adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1533cce7d176Sdrh       break;
1534cce7d176Sdrh     }
1535cce7d176Sdrh     case TK_NOT: {
15364adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1537cce7d176Sdrh       break;
1538cce7d176Sdrh     }
1539cce7d176Sdrh     case TK_LT:
1540cce7d176Sdrh     case TK_LE:
1541cce7d176Sdrh     case TK_GT:
1542cce7d176Sdrh     case TK_GE:
1543cce7d176Sdrh     case TK_NE:
1544cce7d176Sdrh     case TK_EQ: {
15454adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15464adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1547be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1548cce7d176Sdrh       break;
1549cce7d176Sdrh     }
1550cce7d176Sdrh     case TK_ISNULL:
1551cce7d176Sdrh     case TK_NOTNULL: {
15524adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15534adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1554cce7d176Sdrh       break;
1555cce7d176Sdrh     }
1556fef5208cSdrh     case TK_BETWEEN: {
15570202b29eSdanielk1977       /* The expression is "x BETWEEN y AND z". It is implemented as:
15580202b29eSdanielk1977       **
15590202b29eSdanielk1977       ** 1 IF (x >= y) GOTO 3
15600202b29eSdanielk1977       ** 2 GOTO <dest>
15610202b29eSdanielk1977       ** 3 IF (x > z) GOTO <dest>
15620202b29eSdanielk1977       */
1563fef5208cSdrh       int addr;
1564be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1565be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1566be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
15674adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1568be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
15694adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1570be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1571be5c89acSdrh 
15724adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
15734adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1574be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1575be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1576be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
1577fef5208cSdrh       break;
1578fef5208cSdrh     }
1579cce7d176Sdrh     default: {
15804adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
15814adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1582cce7d176Sdrh       break;
1583cce7d176Sdrh     }
1584cce7d176Sdrh   }
1585cce7d176Sdrh }
15862282792aSdrh 
15872282792aSdrh /*
15882282792aSdrh ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
15892282792aSdrh ** if they are identical and return FALSE if they differ in any way.
15902282792aSdrh */
15914adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){
15922282792aSdrh   int i;
15932282792aSdrh   if( pA==0 ){
15942282792aSdrh     return pB==0;
15952282792aSdrh   }else if( pB==0 ){
15962282792aSdrh     return 0;
15972282792aSdrh   }
15982282792aSdrh   if( pA->op!=pB->op ) return 0;
15994adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
16004adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
16012282792aSdrh   if( pA->pList ){
16022282792aSdrh     if( pB->pList==0 ) return 0;
16032282792aSdrh     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
16042282792aSdrh     for(i=0; i<pA->pList->nExpr; i++){
16054adee20fSdanielk1977       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
16062282792aSdrh         return 0;
16072282792aSdrh       }
16082282792aSdrh     }
16092282792aSdrh   }else if( pB->pList ){
16102282792aSdrh     return 0;
16112282792aSdrh   }
16122282792aSdrh   if( pA->pSelect || pB->pSelect ) return 0;
16132f2c01e5Sdrh   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
16142282792aSdrh   if( pA->token.z ){
16152282792aSdrh     if( pB->token.z==0 ) return 0;
16166977fea8Sdrh     if( pB->token.n!=pA->token.n ) return 0;
16174adee20fSdanielk1977     if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
16182282792aSdrh   }
16192282792aSdrh   return 1;
16202282792aSdrh }
16212282792aSdrh 
16222282792aSdrh /*
16232282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index.
16242282792aSdrh */
16252282792aSdrh static int appendAggInfo(Parse *pParse){
16262282792aSdrh   if( (pParse->nAgg & 0x7)==0 ){
16272282792aSdrh     int amt = pParse->nAgg + 8;
16286d4abfbeSdrh     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
16296d4abfbeSdrh     if( aAgg==0 ){
16302282792aSdrh       return -1;
16312282792aSdrh     }
16326d4abfbeSdrh     pParse->aAgg = aAgg;
16332282792aSdrh   }
16342282792aSdrh   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
16352282792aSdrh   return pParse->nAgg++;
16362282792aSdrh }
16372282792aSdrh 
16382282792aSdrh /*
16392282792aSdrh ** Analyze the given expression looking for aggregate functions and
16402282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array.
16412282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary.
16422282792aSdrh **
16432282792aSdrh ** This routine should only be called after the expression has been
16444adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
16452282792aSdrh **
16462282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return
16472282792aSdrh ** the number of errors.
16482282792aSdrh */
16494adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
16502282792aSdrh   int i;
16512282792aSdrh   AggExpr *aAgg;
16522282792aSdrh   int nErr = 0;
16532282792aSdrh 
16542282792aSdrh   if( pExpr==0 ) return 0;
16552282792aSdrh   switch( pExpr->op ){
1656967e8b73Sdrh     case TK_COLUMN: {
16572282792aSdrh       aAgg = pParse->aAgg;
16582282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16592282792aSdrh         if( aAgg[i].isAgg ) continue;
16602282792aSdrh         if( aAgg[i].pExpr->iTable==pExpr->iTable
1661967e8b73Sdrh          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
16622282792aSdrh           break;
16632282792aSdrh         }
16642282792aSdrh       }
16652282792aSdrh       if( i>=pParse->nAgg ){
16662282792aSdrh         i = appendAggInfo(pParse);
16672282792aSdrh         if( i<0 ) return 1;
16682282792aSdrh         pParse->aAgg[i].isAgg = 0;
16692282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16702282792aSdrh       }
1671aaf88729Sdrh       pExpr->iAgg = i;
16722282792aSdrh       break;
16732282792aSdrh     }
16742282792aSdrh     case TK_AGG_FUNCTION: {
16752282792aSdrh       aAgg = pParse->aAgg;
16762282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16772282792aSdrh         if( !aAgg[i].isAgg ) continue;
16784adee20fSdanielk1977         if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
16792282792aSdrh           break;
16802282792aSdrh         }
16812282792aSdrh       }
16822282792aSdrh       if( i>=pParse->nAgg ){
1683d8123366Sdanielk1977         u8 enc = pParse->db->enc;
16842282792aSdrh         i = appendAggInfo(pParse);
16852282792aSdrh         if( i<0 ) return 1;
16862282792aSdrh         pParse->aAgg[i].isAgg = 1;
16872282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16884adee20fSdanielk1977         pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
16896977fea8Sdrh              pExpr->token.z, pExpr->token.n,
1690d8123366Sdanielk1977              pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
16912282792aSdrh       }
16922282792aSdrh       pExpr->iAgg = i;
16932282792aSdrh       break;
16942282792aSdrh     }
16952282792aSdrh     default: {
16962282792aSdrh       if( pExpr->pLeft ){
16974adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
16982282792aSdrh       }
16992282792aSdrh       if( nErr==0 && pExpr->pRight ){
17004adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
17012282792aSdrh       }
17022282792aSdrh       if( nErr==0 && pExpr->pList ){
17032282792aSdrh         int n = pExpr->pList->nExpr;
17042282792aSdrh         int i;
17052282792aSdrh         for(i=0; nErr==0 && i<n; i++){
17064adee20fSdanielk1977           nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
17072282792aSdrh         }
17082282792aSdrh       }
17092282792aSdrh       break;
17102282792aSdrh     }
17112282792aSdrh   }
17122282792aSdrh   return nErr;
17132282792aSdrh }
17148e0a2f90Sdrh 
17158e0a2f90Sdrh /*
1716d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag
1717d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
1718d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return
1719d02eb1fdSdanielk1977 ** NULL if the function does not exist.
17208e0a2f90Sdrh **
17210bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef
17228e0a2f90Sdrh ** structure is created and liked into the "db" structure if a
17238e0a2f90Sdrh ** no matching function previously existed.  When createFlag is true
17248e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts
17258e0a2f90Sdrh ** any number of arguments will be returned.
17268e0a2f90Sdrh **
17278e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid
17288e0a2f90Sdrh ** function found is returned.  A function is valid if either xFunc
17298e0a2f90Sdrh ** or xStep is non-zero.
1730d02eb1fdSdanielk1977 **
1731d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and
1732d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not
1733d02eb1fdSdanielk1977 ** match that requested.
17348e0a2f90Sdrh */
17354adee20fSdanielk1977 FuncDef *sqlite3FindFunction(
17368e0a2f90Sdrh   sqlite *db,        /* An open database */
17378e0a2f90Sdrh   const char *zName, /* Name of the function.  Not null-terminated */
17388e0a2f90Sdrh   int nName,         /* Number of characters in the name */
17398e0a2f90Sdrh   int nArg,          /* Number of arguments.  -1 means any number */
1740d8123366Sdanielk1977   u8 enc,            /* Preferred text encoding */
17418e0a2f90Sdrh   int createFlag     /* Create new entry if true and does not otherwise exist */
17428e0a2f90Sdrh ){
1743d02eb1fdSdanielk1977   FuncDef *p;         /* Iterator variable */
1744d02eb1fdSdanielk1977   FuncDef *pFirst;    /* First function with this name */
1745d02eb1fdSdanielk1977   FuncDef *pBest = 0; /* Best match found so far */
1746d8123366Sdanielk1977   int bestmatch = 0;
1747d02eb1fdSdanielk1977 
1748d8123366Sdanielk1977 
1749d8123366Sdanielk1977   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
1750d02eb1fdSdanielk1977   if( nArg<-1 ) nArg = -1;
1751d02eb1fdSdanielk1977 
1752d02eb1fdSdanielk1977   pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1753d02eb1fdSdanielk1977   for(p=pFirst; p; p=p->pNext){
1754d8123366Sdanielk1977     /* During the search for the best function definition, bestmatch is set
1755d8123366Sdanielk1977     ** as follows to indicate the quality of the match with the definition
1756d8123366Sdanielk1977     ** pointed to by pBest:
1757d8123366Sdanielk1977     **
1758d8123366Sdanielk1977     ** 0: pBest is NULL. No match has been found.
1759d8123366Sdanielk1977     ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1760d8123366Sdanielk1977     **    encoding is requested, or vice versa.
1761d8123366Sdanielk1977     ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1762d8123366Sdanielk1977     **    requested, or vice versa.
1763d8123366Sdanielk1977     ** 3: A variable arguments function using the same text encoding.
1764d8123366Sdanielk1977     ** 4: A function with the exact number of arguments requested that
1765d8123366Sdanielk1977     **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1766d8123366Sdanielk1977     ** 5: A function with the exact number of arguments requested that
1767d8123366Sdanielk1977     **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1768d8123366Sdanielk1977     ** 6: An exact match.
1769d8123366Sdanielk1977     **
1770d8123366Sdanielk1977     ** A larger value of 'matchqual' indicates a more desirable match.
1771d8123366Sdanielk1977     */
1772e12c17baSdanielk1977     if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
1773d8123366Sdanielk1977       int match = 1;          /* Quality of this match */
1774d8123366Sdanielk1977       if( p->nArg==nArg || nArg==-1 ){
1775d8123366Sdanielk1977         match = 4;
17768e0a2f90Sdrh       }
1777d8123366Sdanielk1977       if( enc==p->iPrefEnc ){
1778d8123366Sdanielk1977         match += 2;
17798e0a2f90Sdrh       }
1780d8123366Sdanielk1977       else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1781d8123366Sdanielk1977                (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1782d8123366Sdanielk1977         match += 1;
1783d02eb1fdSdanielk1977       }
1784d8123366Sdanielk1977 
1785d8123366Sdanielk1977       if( match>bestmatch ){
1786d02eb1fdSdanielk1977         pBest = p;
1787d8123366Sdanielk1977         bestmatch = match;
1788d02eb1fdSdanielk1977       }
1789d02eb1fdSdanielk1977     }
1790d02eb1fdSdanielk1977   }
1791d02eb1fdSdanielk1977 
1792d8123366Sdanielk1977   /* If the createFlag parameter is true, and the seach did not reveal an
1793d8123366Sdanielk1977   ** exact match for the name, number of arguments and encoding, then add a
1794d8123366Sdanielk1977   ** new entry to the hash table and return it.
1795d8123366Sdanielk1977   */
1796d8123366Sdanielk1977   if( createFlag && bestmatch<6 &&
1797d02eb1fdSdanielk1977       (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1798d02eb1fdSdanielk1977     pBest->nArg = nArg;
1799d02eb1fdSdanielk1977     pBest->pNext = pFirst;
1800d02eb1fdSdanielk1977     pBest->zName = (char*)&pBest[1];
1801d8123366Sdanielk1977     pBest->iPrefEnc = enc;
1802d02eb1fdSdanielk1977     memcpy(pBest->zName, zName, nName);
1803d02eb1fdSdanielk1977     pBest->zName[nName] = 0;
1804d02eb1fdSdanielk1977     sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
1805d02eb1fdSdanielk1977   }
1806d02eb1fdSdanielk1977 
1807d02eb1fdSdanielk1977   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1808d02eb1fdSdanielk1977     return pBest;
1809d02eb1fdSdanielk1977   }
18108e0a2f90Sdrh   return 0;
18118e0a2f90Sdrh }
1812