xref: /sqlite-3.40.0/src/expr.c (revision 290c1948)
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*290c1948Sdrh ** $Id: expr.c,v 1.157 2004/08/21 17:54:45 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 );
22971c697efSdrh   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 /*
1083*290c1948Sdrh ** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1084*290c1948Sdrh **
1085*290c1948Sdrh ** This routine is provided as a convenience since it is very common
1086*290c1948Sdrh ** to call ResolveIds() and Check() back to back.
1087*290c1948Sdrh */
1088*290c1948Sdrh int sqlite3ExprResolveAndCheck(
1089*290c1948Sdrh   Parse *pParse,     /* The parser context */
1090*290c1948Sdrh   SrcList *pSrcList, /* List of tables used to resolve column names */
1091*290c1948Sdrh   ExprList *pEList,  /* List of expressions used to resolve "AS" */
1092*290c1948Sdrh   Expr *pExpr,       /* The expression to be analyzed. */
1093*290c1948Sdrh   int allowAgg,      /* True to allow aggregate expressions */
1094*290c1948Sdrh   int *pIsAgg        /* Set to TRUE if aggregates are found */
1095*290c1948Sdrh ){
1096*290c1948Sdrh   if( pExpr==0 ) return 0;
1097*290c1948Sdrh   if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1098*290c1948Sdrh     return 1;
1099*290c1948Sdrh   }
1100*290c1948Sdrh   return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1101*290c1948Sdrh }
1102*290c1948Sdrh 
1103*290c1948Sdrh /*
1104fec19aadSdrh ** Generate an instruction that will put the integer describe by
1105fec19aadSdrh ** text z[0..n-1] on the stack.
1106fec19aadSdrh */
1107fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){
1108fec19aadSdrh   int i;
11096fec0762Sdrh   if( sqlite3GetInt32(z, &i) ){
11106fec0762Sdrh     sqlite3VdbeAddOp(v, OP_Integer, i, 0);
11116fec0762Sdrh   }else if( sqlite3FitsIn64Bits(z) ){
11126fec0762Sdrh     sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
1113fec19aadSdrh   }else{
1114fec19aadSdrh     sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1115fec19aadSdrh   }
1116fec19aadSdrh }
1117fec19aadSdrh 
1118fec19aadSdrh /*
1119cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given
11201ccde15dSdrh ** expression and leave the result on the top of stack.
1121cce7d176Sdrh */
11224adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1123cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1124cce7d176Sdrh   int op;
1125daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1126cce7d176Sdrh   switch( pExpr->op ){
1127cce7d176Sdrh     case TK_PLUS:     op = OP_Add;      break;
1128cce7d176Sdrh     case TK_MINUS:    op = OP_Subtract; break;
1129cce7d176Sdrh     case TK_STAR:     op = OP_Multiply; break;
1130cce7d176Sdrh     case TK_SLASH:    op = OP_Divide;   break;
1131cce7d176Sdrh     case TK_AND:      op = OP_And;      break;
1132cce7d176Sdrh     case TK_OR:       op = OP_Or;       break;
1133cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1134cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1135cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1136cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1137cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1138cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1139cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1140cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1141cce7d176Sdrh     case TK_NOT:      op = OP_Not;      break;
1142cce7d176Sdrh     case TK_UMINUS:   op = OP_Negative; break;
1143bf4133cbSdrh     case TK_BITAND:   op = OP_BitAnd;   break;
1144bf4133cbSdrh     case TK_BITOR:    op = OP_BitOr;    break;
1145bf4133cbSdrh     case TK_BITNOT:   op = OP_BitNot;   break;
1146bf4133cbSdrh     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
1147bf4133cbSdrh     case TK_RSHIFT:   op = OP_ShiftRight; break;
1148bf4133cbSdrh     case TK_REM:      op = OP_Remainder;  break;
1149fec19aadSdrh     case TK_FLOAT:    op = OP_Real;       break;
11500f69c1e3Sdanielk1977     case TK_STRING:   op = OP_String8;     break;
1151c572ef7fSdanielk1977     case TK_BLOB:     op = OP_HexBlob;    break;
1152cfe9a69fSdanielk1977     default: op = 0; break;
1153cce7d176Sdrh   }
1154cce7d176Sdrh   switch( pExpr->op ){
1155967e8b73Sdrh     case TK_COLUMN: {
11562282792aSdrh       if( pParse->useAgg ){
11574adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1158c4a3c779Sdrh       }else if( pExpr->iColumn>=0 ){
11594adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1160c4a3c779Sdrh       }else{
11614adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
11622282792aSdrh       }
1163cce7d176Sdrh       break;
1164cce7d176Sdrh     }
1165cce7d176Sdrh     case TK_INTEGER: {
1166fec19aadSdrh       codeInteger(v, pExpr->token.z, pExpr->token.n);
1167fec19aadSdrh       break;
116851e9a445Sdrh     }
1169fec19aadSdrh     case TK_FLOAT:
1170fec19aadSdrh     case TK_STRING: {
1171fec19aadSdrh       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
11724adee20fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1173cce7d176Sdrh       break;
1174cce7d176Sdrh     }
1175c572ef7fSdanielk1977     case TK_BLOB: {
1176c572ef7fSdanielk1977       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1177c572ef7fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1178c572ef7fSdanielk1977       break;
1179c572ef7fSdanielk1977     }
1180cce7d176Sdrh     case TK_NULL: {
11810f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1182cce7d176Sdrh       break;
1183cce7d176Sdrh     }
118450457896Sdrh     case TK_VARIABLE: {
11854adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
1186895d7472Sdrh       if( pExpr->token.n>1 ){
1187895d7472Sdrh         sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1188895d7472Sdrh       }
118950457896Sdrh       break;
119050457896Sdrh     }
1191c9b84a1fSdrh     case TK_LT:
1192c9b84a1fSdrh     case TK_LE:
1193c9b84a1fSdrh     case TK_GT:
1194c9b84a1fSdrh     case TK_GE:
1195c9b84a1fSdrh     case TK_NE:
1196c9b84a1fSdrh     case TK_EQ: {
1197a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1198a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1199be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
1200a37cdde0Sdanielk1977       break;
1201c9b84a1fSdrh     }
1202cce7d176Sdrh     case TK_AND:
1203cce7d176Sdrh     case TK_OR:
1204cce7d176Sdrh     case TK_PLUS:
1205cce7d176Sdrh     case TK_STAR:
1206cce7d176Sdrh     case TK_MINUS:
1207bf4133cbSdrh     case TK_REM:
1208bf4133cbSdrh     case TK_BITAND:
1209bf4133cbSdrh     case TK_BITOR:
121017c40294Sdrh     case TK_SLASH:
1211bf4133cbSdrh     case TK_LSHIFT:
1212bf4133cbSdrh     case TK_RSHIFT: {
12134adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
121417c40294Sdrh       sqlite3ExprCode(pParse, pExpr->pRight);
12154adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1216bf4133cbSdrh       break;
1217bf4133cbSdrh     }
12180040077dSdrh     case TK_CONCAT: {
12194adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12204adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
122172c952a1Sdanielk1977       sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
12220040077dSdrh       break;
12230040077dSdrh     }
1224cce7d176Sdrh     case TK_UMINUS: {
1225fec19aadSdrh       Expr *pLeft = pExpr->pLeft;
1226fec19aadSdrh       assert( pLeft );
1227fec19aadSdrh       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1228fec19aadSdrh         Token *p = &pLeft->token;
12296e142f54Sdrh         char *z = sqliteMalloc( p->n + 2 );
12306e142f54Sdrh         sprintf(z, "-%.*s", p->n, p->z);
1231fec19aadSdrh         if( pLeft->op==TK_FLOAT ){
1232fec19aadSdrh           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1233e6840900Sdrh         }else{
1234fec19aadSdrh           codeInteger(v, z, p->n+1);
1235e6840900Sdrh         }
12366e142f54Sdrh         sqliteFree(z);
12376e142f54Sdrh         break;
12386e142f54Sdrh       }
12391ccde15dSdrh       /* Fall through into TK_NOT */
12406e142f54Sdrh     }
1241bf4133cbSdrh     case TK_BITNOT:
12426e142f54Sdrh     case TK_NOT: {
12434adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12444adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1245cce7d176Sdrh       break;
1246cce7d176Sdrh     }
1247cce7d176Sdrh     case TK_ISNULL:
1248cce7d176Sdrh     case TK_NOTNULL: {
1249cce7d176Sdrh       int dest;
12504adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
12514adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12524adee20fSdanielk1977       dest = sqlite3VdbeCurrentAddr(v) + 2;
12534adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
12544adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1255cce7d176Sdrh     }
1256a37cdde0Sdanielk1977     break;
12572282792aSdrh     case TK_AGG_FUNCTION: {
12584adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
12592282792aSdrh       break;
12602282792aSdrh     }
12614b59ab5eSdrh     case TK_GLOB:
12624b59ab5eSdrh     case TK_LIKE:
1263cce7d176Sdrh     case TK_FUNCTION: {
1264cce7d176Sdrh       ExprList *pList = pExpr->pList;
126589425d5eSdrh       int nExpr = pList ? pList->nExpr : 0;
12660bce8354Sdrh       FuncDef *pDef;
12674b59ab5eSdrh       int nId;
12684b59ab5eSdrh       const char *zId;
1269682f68b0Sdanielk1977       int p2 = 0;
1270682f68b0Sdanielk1977       int i;
1271d8123366Sdanielk1977       u8 enc = pParse->db->enc;
1272dc1bdc4fSdanielk1977       CollSeq *pColl = 0;
12734b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1274d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
12750bce8354Sdrh       assert( pDef!=0 );
1276f9b596ebSdrh       nExpr = sqlite3ExprCodeExprList(pParse, pList);
1277682f68b0Sdanielk1977       for(i=0; i<nExpr && i<32; i++){
1278d02eb1fdSdanielk1977         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1279d02eb1fdSdanielk1977           p2 |= (1<<i);
1280d02eb1fdSdanielk1977         }
1281dc1bdc4fSdanielk1977         if( pDef->needCollSeq && !pColl ){
1282dc1bdc4fSdanielk1977           pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1283dc1bdc4fSdanielk1977         }
1284dc1bdc4fSdanielk1977       }
1285dc1bdc4fSdanielk1977       if( pDef->needCollSeq ){
1286dc1bdc4fSdanielk1977         if( !pColl ) pColl = pParse->db->pDfltColl;
1287d8123366Sdanielk1977         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
1288682f68b0Sdanielk1977       }
1289682f68b0Sdanielk1977       sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
12906ec2733bSdrh       break;
12916ec2733bSdrh     }
129219a775c2Sdrh     case TK_SELECT: {
12934adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
129419a775c2Sdrh       break;
129519a775c2Sdrh     }
1296fef5208cSdrh     case TK_IN: {
1297fef5208cSdrh       int addr;
1298e014a838Sdanielk1977       char const *affStr;
1299e014a838Sdanielk1977 
1300e014a838Sdanielk1977       /* Figure out the affinity to use to create a key from the results
1301e014a838Sdanielk1977       ** of the expression. affinityStr stores a static string suitable for
1302ededfd5eSdanielk1977       ** P3 of OP_MakeRecord.
1303e014a838Sdanielk1977       */
1304e014a838Sdanielk1977       affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1305e014a838Sdanielk1977 
13064adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1307e014a838Sdanielk1977 
1308e014a838Sdanielk1977       /* Code the <expr> from "<expr> IN (...)". The temporary table
1309e014a838Sdanielk1977       ** pExpr->iTable contains the values that make up the (...) set.
1310e014a838Sdanielk1977       */
13114adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
13124adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1313e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */
13144adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
13150f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1316e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1317ededfd5eSdanielk1977       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1318e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1319e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */
1320e014a838Sdanielk1977 
1321fef5208cSdrh       break;
1322fef5208cSdrh     }
1323fef5208cSdrh     case TK_BETWEEN: {
1324be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1325be5c89acSdrh       struct ExprList_item *pLItem = pExpr->pList->a;
1326be5c89acSdrh       Expr *pRight = pLItem->pExpr;
1327be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
13284adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1329be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1330be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
13314adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1332be5c89acSdrh       pLItem++;
1333be5c89acSdrh       pRight = pLItem->pExpr;
1334be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1335be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
13364adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_And, 0, 0);
1337fef5208cSdrh       break;
1338fef5208cSdrh     }
133951e9a445Sdrh     case TK_UPLUS:
1340a2e00042Sdrh     case TK_AS: {
13414adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1342a2e00042Sdrh       break;
1343a2e00042Sdrh     }
134417a7f8ddSdrh     case TK_CASE: {
134517a7f8ddSdrh       int expr_end_label;
1346f5905aa7Sdrh       int jumpInst;
1347f5905aa7Sdrh       int addr;
1348f5905aa7Sdrh       int nExpr;
134917a7f8ddSdrh       int i;
1350be5c89acSdrh       ExprList *pEList;
1351be5c89acSdrh       struct ExprList_item *aListelem;
135217a7f8ddSdrh 
135317a7f8ddSdrh       assert(pExpr->pList);
135417a7f8ddSdrh       assert((pExpr->pList->nExpr % 2) == 0);
135517a7f8ddSdrh       assert(pExpr->pList->nExpr > 0);
1356be5c89acSdrh       pEList = pExpr->pList;
1357be5c89acSdrh       aListelem = pEList->a;
1358be5c89acSdrh       nExpr = pEList->nExpr;
13594adee20fSdanielk1977       expr_end_label = sqlite3VdbeMakeLabel(v);
136017a7f8ddSdrh       if( pExpr->pLeft ){
13614adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pLeft);
1362cce7d176Sdrh       }
1363f5905aa7Sdrh       for(i=0; i<nExpr; i=i+2){
1364be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i].pExpr);
136517a7f8ddSdrh         if( pExpr->pLeft ){
13664adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1367be5c89acSdrh           jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1368be5c89acSdrh                                  OP_Ne, 0, 1);
13694adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1370f5905aa7Sdrh         }else{
13714adee20fSdanielk1977           jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
137217a7f8ddSdrh         }
1373be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
13744adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
13754adee20fSdanielk1977         addr = sqlite3VdbeCurrentAddr(v);
13764adee20fSdanielk1977         sqlite3VdbeChangeP2(v, jumpInst, addr);
137717a7f8ddSdrh       }
1378f570f011Sdrh       if( pExpr->pLeft ){
13794adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1380f570f011Sdrh       }
138117a7f8ddSdrh       if( pExpr->pRight ){
13824adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pRight);
138317a7f8ddSdrh       }else{
13840f69c1e3Sdanielk1977         sqlite3VdbeAddOp(v, OP_String8, 0, 0);
138517a7f8ddSdrh       }
13864adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, expr_end_label);
13876f34903eSdanielk1977       break;
13886f34903eSdanielk1977     }
13896f34903eSdanielk1977     case TK_RAISE: {
13906f34903eSdanielk1977       if( !pParse->trigStack ){
13914adee20fSdanielk1977         sqlite3ErrorMsg(pParse,
1392da93d238Sdrh                        "RAISE() may only be used within a trigger-program");
13936f34903eSdanielk1977         pParse->nErr++;
13946f34903eSdanielk1977 	return;
13956f34903eSdanielk1977       }
13966f34903eSdanielk1977       if( pExpr->iColumn == OE_Rollback ||
13976f34903eSdanielk1977 	  pExpr->iColumn == OE_Abort ||
13986f34903eSdanielk1977 	  pExpr->iColumn == OE_Fail ){
13994adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1400701a0aebSdrh                            pExpr->token.z, pExpr->token.n);
14014adee20fSdanielk1977 	  sqlite3VdbeDequoteP3(v, -1);
14026f34903eSdanielk1977       } else {
14036f34903eSdanielk1977 	  assert( pExpr->iColumn == OE_Ignore );
14044adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1405701a0aebSdrh                            "(IGNORE jump)", 0);
14066f34903eSdanielk1977       }
140717a7f8ddSdrh     }
140817a7f8ddSdrh     break;
140917a7f8ddSdrh   }
1410cce7d176Sdrh }
1411cce7d176Sdrh 
1412cce7d176Sdrh /*
1413268380caSdrh ** Generate code that pushes the value of every element of the given
1414f9b596ebSdrh ** expression list onto the stack.
1415268380caSdrh **
1416268380caSdrh ** Return the number of elements pushed onto the stack.
1417268380caSdrh */
14184adee20fSdanielk1977 int sqlite3ExprCodeExprList(
1419268380caSdrh   Parse *pParse,     /* Parsing context */
1420f9b596ebSdrh   ExprList *pList    /* The expression list to be coded */
1421268380caSdrh ){
1422268380caSdrh   struct ExprList_item *pItem;
1423268380caSdrh   int i, n;
1424268380caSdrh   Vdbe *v;
1425268380caSdrh   if( pList==0 ) return 0;
14264adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1427268380caSdrh   n = pList->nExpr;
1428268380caSdrh   for(pItem=pList->a, i=0; i<n; i++, pItem++){
14294adee20fSdanielk1977     sqlite3ExprCode(pParse, pItem->pExpr);
1430268380caSdrh   }
1431f9b596ebSdrh   return n;
1432268380caSdrh }
1433268380caSdrh 
1434268380caSdrh /*
1435cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made
1436cce7d176Sdrh ** to the label "dest" if the expression is true but execution
1437cce7d176Sdrh ** continues straight thru if the expression is false.
1438f5905aa7Sdrh **
1439f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then
1440f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true.
1441cce7d176Sdrh */
14424adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1443cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1444cce7d176Sdrh   int op = 0;
1445daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1446cce7d176Sdrh   switch( pExpr->op ){
1447cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1448cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1449cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1450cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1451cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1452cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1453cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1454cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1455cce7d176Sdrh     default:  break;
1456cce7d176Sdrh   }
1457cce7d176Sdrh   switch( pExpr->op ){
1458cce7d176Sdrh     case TK_AND: {
14594adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
14604adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
14614adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
14624adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1463cce7d176Sdrh       break;
1464cce7d176Sdrh     }
1465cce7d176Sdrh     case TK_OR: {
14664adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
14674adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1468cce7d176Sdrh       break;
1469cce7d176Sdrh     }
1470cce7d176Sdrh     case TK_NOT: {
14714adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1472cce7d176Sdrh       break;
1473cce7d176Sdrh     }
1474cce7d176Sdrh     case TK_LT:
1475cce7d176Sdrh     case TK_LE:
1476cce7d176Sdrh     case TK_GT:
1477cce7d176Sdrh     case TK_GE:
1478cce7d176Sdrh     case TK_NE:
14790ac65892Sdrh     case TK_EQ: {
14804adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14814adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1482be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1483cce7d176Sdrh       break;
1484cce7d176Sdrh     }
1485cce7d176Sdrh     case TK_ISNULL:
1486cce7d176Sdrh     case TK_NOTNULL: {
14874adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14884adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1489cce7d176Sdrh       break;
1490cce7d176Sdrh     }
1491fef5208cSdrh     case TK_BETWEEN: {
14920202b29eSdanielk1977       /* The expression "x BETWEEN y AND z" is implemented as:
14930202b29eSdanielk1977       **
14940202b29eSdanielk1977       ** 1 IF (x < y) GOTO 3
14950202b29eSdanielk1977       ** 2 IF (x <= z) GOTO <dest>
14960202b29eSdanielk1977       ** 3 ...
14970202b29eSdanielk1977       */
1498f5905aa7Sdrh       int addr;
1499be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1500be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1501be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
15024adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1503be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1504be5c89acSdrh       addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
15050202b29eSdanielk1977 
1506be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1507be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1508be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
15090202b29eSdanielk1977 
15104adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
15114adee20fSdanielk1977       sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
15124adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1513fef5208cSdrh       break;
1514fef5208cSdrh     }
1515cce7d176Sdrh     default: {
15164adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
15174adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
1518cce7d176Sdrh       break;
1519cce7d176Sdrh     }
1520cce7d176Sdrh   }
1521cce7d176Sdrh }
1522cce7d176Sdrh 
1523cce7d176Sdrh /*
152466b89c8fSdrh ** Generate code for a boolean expression such that a jump is made
1525cce7d176Sdrh ** to the label "dest" if the expression is false but execution
1526cce7d176Sdrh ** continues straight thru if the expression is true.
1527f5905aa7Sdrh **
1528f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then
1529f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1530cce7d176Sdrh */
15314adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1532cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1533cce7d176Sdrh   int op = 0;
1534daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1535cce7d176Sdrh   switch( pExpr->op ){
1536cce7d176Sdrh     case TK_LT:       op = OP_Ge;       break;
1537cce7d176Sdrh     case TK_LE:       op = OP_Gt;       break;
1538cce7d176Sdrh     case TK_GT:       op = OP_Le;       break;
1539cce7d176Sdrh     case TK_GE:       op = OP_Lt;       break;
1540cce7d176Sdrh     case TK_NE:       op = OP_Eq;       break;
1541cce7d176Sdrh     case TK_EQ:       op = OP_Ne;       break;
1542cce7d176Sdrh     case TK_ISNULL:   op = OP_NotNull;  break;
1543cce7d176Sdrh     case TK_NOTNULL:  op = OP_IsNull;   break;
1544cce7d176Sdrh     default:  break;
1545cce7d176Sdrh   }
1546cce7d176Sdrh   switch( pExpr->op ){
1547cce7d176Sdrh     case TK_AND: {
15484adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
15494adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1550cce7d176Sdrh       break;
1551cce7d176Sdrh     }
1552cce7d176Sdrh     case TK_OR: {
15534adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
15544adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
15554adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
15564adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1557cce7d176Sdrh       break;
1558cce7d176Sdrh     }
1559cce7d176Sdrh     case TK_NOT: {
15604adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1561cce7d176Sdrh       break;
1562cce7d176Sdrh     }
1563cce7d176Sdrh     case TK_LT:
1564cce7d176Sdrh     case TK_LE:
1565cce7d176Sdrh     case TK_GT:
1566cce7d176Sdrh     case TK_GE:
1567cce7d176Sdrh     case TK_NE:
1568cce7d176Sdrh     case TK_EQ: {
15694adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15704adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1571be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1572cce7d176Sdrh       break;
1573cce7d176Sdrh     }
1574cce7d176Sdrh     case TK_ISNULL:
1575cce7d176Sdrh     case TK_NOTNULL: {
15764adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15774adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1578cce7d176Sdrh       break;
1579cce7d176Sdrh     }
1580fef5208cSdrh     case TK_BETWEEN: {
15810202b29eSdanielk1977       /* The expression is "x BETWEEN y AND z". It is implemented as:
15820202b29eSdanielk1977       **
15830202b29eSdanielk1977       ** 1 IF (x >= y) GOTO 3
15840202b29eSdanielk1977       ** 2 GOTO <dest>
15850202b29eSdanielk1977       ** 3 IF (x > z) GOTO <dest>
15860202b29eSdanielk1977       */
1587fef5208cSdrh       int addr;
1588be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1589be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1590be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
15914adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1592be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
15934adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1594be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1595be5c89acSdrh 
15964adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
15974adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1598be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1599be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1600be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
1601fef5208cSdrh       break;
1602fef5208cSdrh     }
1603cce7d176Sdrh     default: {
16044adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
16054adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1606cce7d176Sdrh       break;
1607cce7d176Sdrh     }
1608cce7d176Sdrh   }
1609cce7d176Sdrh }
16102282792aSdrh 
16112282792aSdrh /*
16122282792aSdrh ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
16132282792aSdrh ** if they are identical and return FALSE if they differ in any way.
16142282792aSdrh */
16154adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){
16162282792aSdrh   int i;
16172282792aSdrh   if( pA==0 ){
16182282792aSdrh     return pB==0;
16192282792aSdrh   }else if( pB==0 ){
16202282792aSdrh     return 0;
16212282792aSdrh   }
16222282792aSdrh   if( pA->op!=pB->op ) return 0;
16234adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
16244adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
16252282792aSdrh   if( pA->pList ){
16262282792aSdrh     if( pB->pList==0 ) return 0;
16272282792aSdrh     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
16282282792aSdrh     for(i=0; i<pA->pList->nExpr; i++){
16294adee20fSdanielk1977       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
16302282792aSdrh         return 0;
16312282792aSdrh       }
16322282792aSdrh     }
16332282792aSdrh   }else if( pB->pList ){
16342282792aSdrh     return 0;
16352282792aSdrh   }
16362282792aSdrh   if( pA->pSelect || pB->pSelect ) return 0;
16372f2c01e5Sdrh   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
16382282792aSdrh   if( pA->token.z ){
16392282792aSdrh     if( pB->token.z==0 ) return 0;
16406977fea8Sdrh     if( pB->token.n!=pA->token.n ) return 0;
16414adee20fSdanielk1977     if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
16422282792aSdrh   }
16432282792aSdrh   return 1;
16442282792aSdrh }
16452282792aSdrh 
16462282792aSdrh /*
16472282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index.
16482282792aSdrh */
16492282792aSdrh static int appendAggInfo(Parse *pParse){
16502282792aSdrh   if( (pParse->nAgg & 0x7)==0 ){
16512282792aSdrh     int amt = pParse->nAgg + 8;
16526d4abfbeSdrh     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
16536d4abfbeSdrh     if( aAgg==0 ){
16542282792aSdrh       return -1;
16552282792aSdrh     }
16566d4abfbeSdrh     pParse->aAgg = aAgg;
16572282792aSdrh   }
16582282792aSdrh   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
16592282792aSdrh   return pParse->nAgg++;
16602282792aSdrh }
16612282792aSdrh 
16622282792aSdrh /*
16632282792aSdrh ** Analyze the given expression looking for aggregate functions and
16642282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array.
16652282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary.
16662282792aSdrh **
16672282792aSdrh ** This routine should only be called after the expression has been
16684adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
16692282792aSdrh **
16702282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return
16712282792aSdrh ** the number of errors.
16722282792aSdrh */
16734adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
16742282792aSdrh   int i;
16752282792aSdrh   AggExpr *aAgg;
16762282792aSdrh   int nErr = 0;
16772282792aSdrh 
16782282792aSdrh   if( pExpr==0 ) return 0;
16792282792aSdrh   switch( pExpr->op ){
1680967e8b73Sdrh     case TK_COLUMN: {
16812282792aSdrh       aAgg = pParse->aAgg;
16822282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16832282792aSdrh         if( aAgg[i].isAgg ) continue;
16842282792aSdrh         if( aAgg[i].pExpr->iTable==pExpr->iTable
1685967e8b73Sdrh          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
16862282792aSdrh           break;
16872282792aSdrh         }
16882282792aSdrh       }
16892282792aSdrh       if( i>=pParse->nAgg ){
16902282792aSdrh         i = appendAggInfo(pParse);
16912282792aSdrh         if( i<0 ) return 1;
16922282792aSdrh         pParse->aAgg[i].isAgg = 0;
16932282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16942282792aSdrh       }
1695aaf88729Sdrh       pExpr->iAgg = i;
16962282792aSdrh       break;
16972282792aSdrh     }
16982282792aSdrh     case TK_AGG_FUNCTION: {
16992282792aSdrh       aAgg = pParse->aAgg;
17002282792aSdrh       for(i=0; i<pParse->nAgg; i++){
17012282792aSdrh         if( !aAgg[i].isAgg ) continue;
17024adee20fSdanielk1977         if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
17032282792aSdrh           break;
17042282792aSdrh         }
17052282792aSdrh       }
17062282792aSdrh       if( i>=pParse->nAgg ){
1707d8123366Sdanielk1977         u8 enc = pParse->db->enc;
17082282792aSdrh         i = appendAggInfo(pParse);
17092282792aSdrh         if( i<0 ) return 1;
17102282792aSdrh         pParse->aAgg[i].isAgg = 1;
17112282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
17124adee20fSdanielk1977         pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
17136977fea8Sdrh              pExpr->token.z, pExpr->token.n,
1714d8123366Sdanielk1977              pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
17152282792aSdrh       }
17162282792aSdrh       pExpr->iAgg = i;
17172282792aSdrh       break;
17182282792aSdrh     }
17192282792aSdrh     default: {
17202282792aSdrh       if( pExpr->pLeft ){
17214adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
17222282792aSdrh       }
17232282792aSdrh       if( nErr==0 && pExpr->pRight ){
17244adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
17252282792aSdrh       }
17262282792aSdrh       if( nErr==0 && pExpr->pList ){
17272282792aSdrh         int n = pExpr->pList->nExpr;
17282282792aSdrh         int i;
17292282792aSdrh         for(i=0; nErr==0 && i<n; i++){
17304adee20fSdanielk1977           nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
17312282792aSdrh         }
17322282792aSdrh       }
17332282792aSdrh       break;
17342282792aSdrh     }
17352282792aSdrh   }
17362282792aSdrh   return nErr;
17372282792aSdrh }
17388e0a2f90Sdrh 
17398e0a2f90Sdrh /*
1740d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag
1741d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
1742d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return
1743d02eb1fdSdanielk1977 ** NULL if the function does not exist.
17448e0a2f90Sdrh **
17450bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef
17468e0a2f90Sdrh ** structure is created and liked into the "db" structure if a
17478e0a2f90Sdrh ** no matching function previously existed.  When createFlag is true
17488e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts
17498e0a2f90Sdrh ** any number of arguments will be returned.
17508e0a2f90Sdrh **
17518e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid
17528e0a2f90Sdrh ** function found is returned.  A function is valid if either xFunc
17538e0a2f90Sdrh ** or xStep is non-zero.
1754d02eb1fdSdanielk1977 **
1755d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and
1756d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not
1757d02eb1fdSdanielk1977 ** match that requested.
17588e0a2f90Sdrh */
17594adee20fSdanielk1977 FuncDef *sqlite3FindFunction(
17608e0a2f90Sdrh   sqlite *db,        /* An open database */
17618e0a2f90Sdrh   const char *zName, /* Name of the function.  Not null-terminated */
17628e0a2f90Sdrh   int nName,         /* Number of characters in the name */
17638e0a2f90Sdrh   int nArg,          /* Number of arguments.  -1 means any number */
1764d8123366Sdanielk1977   u8 enc,            /* Preferred text encoding */
17658e0a2f90Sdrh   int createFlag     /* Create new entry if true and does not otherwise exist */
17668e0a2f90Sdrh ){
1767d02eb1fdSdanielk1977   FuncDef *p;         /* Iterator variable */
1768d02eb1fdSdanielk1977   FuncDef *pFirst;    /* First function with this name */
1769d02eb1fdSdanielk1977   FuncDef *pBest = 0; /* Best match found so far */
1770d8123366Sdanielk1977   int bestmatch = 0;
1771d02eb1fdSdanielk1977 
1772d8123366Sdanielk1977 
1773d8123366Sdanielk1977   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
1774d02eb1fdSdanielk1977   if( nArg<-1 ) nArg = -1;
1775d02eb1fdSdanielk1977 
1776d02eb1fdSdanielk1977   pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1777d02eb1fdSdanielk1977   for(p=pFirst; p; p=p->pNext){
1778d8123366Sdanielk1977     /* During the search for the best function definition, bestmatch is set
1779d8123366Sdanielk1977     ** as follows to indicate the quality of the match with the definition
1780d8123366Sdanielk1977     ** pointed to by pBest:
1781d8123366Sdanielk1977     **
1782d8123366Sdanielk1977     ** 0: pBest is NULL. No match has been found.
1783d8123366Sdanielk1977     ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1784d8123366Sdanielk1977     **    encoding is requested, or vice versa.
1785d8123366Sdanielk1977     ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1786d8123366Sdanielk1977     **    requested, or vice versa.
1787d8123366Sdanielk1977     ** 3: A variable arguments function using the same text encoding.
1788d8123366Sdanielk1977     ** 4: A function with the exact number of arguments requested that
1789d8123366Sdanielk1977     **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1790d8123366Sdanielk1977     ** 5: A function with the exact number of arguments requested that
1791d8123366Sdanielk1977     **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1792d8123366Sdanielk1977     ** 6: An exact match.
1793d8123366Sdanielk1977     **
1794d8123366Sdanielk1977     ** A larger value of 'matchqual' indicates a more desirable match.
1795d8123366Sdanielk1977     */
1796e12c17baSdanielk1977     if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
1797d8123366Sdanielk1977       int match = 1;          /* Quality of this match */
1798d8123366Sdanielk1977       if( p->nArg==nArg || nArg==-1 ){
1799d8123366Sdanielk1977         match = 4;
18008e0a2f90Sdrh       }
1801d8123366Sdanielk1977       if( enc==p->iPrefEnc ){
1802d8123366Sdanielk1977         match += 2;
18038e0a2f90Sdrh       }
1804d8123366Sdanielk1977       else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1805d8123366Sdanielk1977                (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1806d8123366Sdanielk1977         match += 1;
1807d02eb1fdSdanielk1977       }
1808d8123366Sdanielk1977 
1809d8123366Sdanielk1977       if( match>bestmatch ){
1810d02eb1fdSdanielk1977         pBest = p;
1811d8123366Sdanielk1977         bestmatch = match;
1812d02eb1fdSdanielk1977       }
1813d02eb1fdSdanielk1977     }
1814d02eb1fdSdanielk1977   }
1815d02eb1fdSdanielk1977 
1816d8123366Sdanielk1977   /* If the createFlag parameter is true, and the seach did not reveal an
1817d8123366Sdanielk1977   ** exact match for the name, number of arguments and encoding, then add a
1818d8123366Sdanielk1977   ** new entry to the hash table and return it.
1819d8123366Sdanielk1977   */
1820d8123366Sdanielk1977   if( createFlag && bestmatch<6 &&
1821d02eb1fdSdanielk1977       (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1822d02eb1fdSdanielk1977     pBest->nArg = nArg;
1823d02eb1fdSdanielk1977     pBest->pNext = pFirst;
1824d02eb1fdSdanielk1977     pBest->zName = (char*)&pBest[1];
1825d8123366Sdanielk1977     pBest->iPrefEnc = enc;
1826d02eb1fdSdanielk1977     memcpy(pBest->zName, zName, nName);
1827d02eb1fdSdanielk1977     pBest->zName[nName] = 0;
1828d02eb1fdSdanielk1977     sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
1829d02eb1fdSdanielk1977   }
1830d02eb1fdSdanielk1977 
1831d02eb1fdSdanielk1977   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1832d02eb1fdSdanielk1977     return pBest;
1833d02eb1fdSdanielk1977   }
18348e0a2f90Sdrh   return 0;
18358e0a2f90Sdrh }
1836