xref: /sqlite-3.40.0/src/expr.c (revision d02eb1fd)
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*d02eb1fdSdanielk1977 ** $Id: expr.c,v 1.135 2004/06/06 09:44:04 danielk1977 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   }
29e014a838Sdanielk1977 }
30e014a838Sdanielk1977 
31e014a838Sdanielk1977 
32e014a838Sdanielk1977 /*
33e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any.
34e014a838Sdanielk1977 **
35e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias,
36e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the
37e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned,
38e014a838Sdanielk1977 ** indicating no affinity for the expression.
39e014a838Sdanielk1977 **
40e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all
41e014a838Sdanielk1977 ** have an affinity:
42e014a838Sdanielk1977 **
43e014a838Sdanielk1977 ** CREATE TABLE t1(a);
44e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a;
45e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b;
46e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1);
47e014a838Sdanielk1977 */
48bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){
49a37cdde0Sdanielk1977   if( pExpr->op==TK_AS ){
50bf3b721fSdanielk1977     return sqlite3ExprAffinity(pExpr->pLeft);
51a37cdde0Sdanielk1977   }
52a37cdde0Sdanielk1977   if( pExpr->op==TK_SELECT ){
53bf3b721fSdanielk1977     return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
54a37cdde0Sdanielk1977   }
55a37cdde0Sdanielk1977   return pExpr->affinity;
56a37cdde0Sdanielk1977 }
57a37cdde0Sdanielk1977 
5853db1458Sdrh /*
5953db1458Sdrh ** pExpr is the left operand of a comparison operator.  aff2 is the
6053db1458Sdrh ** type affinity of the right operand.  This routine returns the
6153db1458Sdrh ** type affinity that should be used for the comparison operator.
6253db1458Sdrh */
63e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){
64bf3b721fSdanielk1977   char aff1 = sqlite3ExprAffinity(pExpr);
65e014a838Sdanielk1977   if( aff1 && aff2 ){
66e014a838Sdanielk1977     /* Both sides of the comparison are columns. If one has numeric or
67e014a838Sdanielk1977     ** integer affinity, use that. Otherwise use no affinity.
68e014a838Sdanielk1977     */
69e014a838Sdanielk1977     if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
70e014a838Sdanielk1977       return SQLITE_AFF_INTEGER;
71e014a838Sdanielk1977     }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
72e014a838Sdanielk1977       return SQLITE_AFF_NUMERIC;
73e014a838Sdanielk1977     }else{
74e014a838Sdanielk1977       return SQLITE_AFF_NONE;
75e014a838Sdanielk1977     }
76e014a838Sdanielk1977   }else if( !aff1 && !aff2 ){
77e014a838Sdanielk1977     /* Neither side of the comparison is a column. Use numeric affinity
78e014a838Sdanielk1977     ** for the comparison.
79e014a838Sdanielk1977     */
80e014a838Sdanielk1977     return SQLITE_AFF_NUMERIC;
81e014a838Sdanielk1977   }else{
82e014a838Sdanielk1977     /* One side is a column, the other is not. Use the columns affinity. */
83e014a838Sdanielk1977     return (aff1 + aff2);
84e014a838Sdanielk1977   }
85e014a838Sdanielk1977 }
86e014a838Sdanielk1977 
8753db1458Sdrh /*
8853db1458Sdrh ** pExpr is a comparison operator.  Return the type affinity that should
8953db1458Sdrh ** be applied to both operands prior to doing the comparison.
9053db1458Sdrh */
91e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){
92e014a838Sdanielk1977   char aff;
93e014a838Sdanielk1977   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
94e014a838Sdanielk1977           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
95e014a838Sdanielk1977           pExpr->op==TK_NE );
96e014a838Sdanielk1977   assert( pExpr->pLeft );
97bf3b721fSdanielk1977   aff = sqlite3ExprAffinity(pExpr->pLeft);
98e014a838Sdanielk1977   if( pExpr->pRight ){
99e014a838Sdanielk1977     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
100e014a838Sdanielk1977   }
101e014a838Sdanielk1977   else if( pExpr->pSelect ){
102e014a838Sdanielk1977     aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
103e014a838Sdanielk1977   }
104e014a838Sdanielk1977   else if( !aff ){
105e014a838Sdanielk1977     aff = SQLITE_AFF_NUMERIC;
106e014a838Sdanielk1977   }
107e014a838Sdanielk1977   return aff;
108e014a838Sdanielk1977 }
109e014a838Sdanielk1977 
110e014a838Sdanielk1977 /*
111e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
112e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true
113e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement
114e014a838Sdanielk1977 ** the comparison in pExpr.
115e014a838Sdanielk1977 */
116e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
117e014a838Sdanielk1977   char aff = comparisonAffinity(pExpr);
118e014a838Sdanielk1977   return
119e014a838Sdanielk1977     (aff==SQLITE_AFF_NONE) ||
120e014a838Sdanielk1977     (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
121e014a838Sdanielk1977     (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
122e014a838Sdanielk1977     (aff==idx_affinity);
123e014a838Sdanielk1977 }
124e014a838Sdanielk1977 
125a37cdde0Sdanielk1977 /*
126a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison
127a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
128a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned
129a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression
130a37cdde0Sdanielk1977 ** evaluates to NULL.
131a37cdde0Sdanielk1977 */
132e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
133bf3b721fSdanielk1977   char aff = sqlite3ExprAffinity(pExpr2);
134e014a838Sdanielk1977   return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
135a37cdde0Sdanielk1977 }
136a37cdde0Sdanielk1977 
137a2e00042Sdrh /*
138a76b5dfcSdrh ** Construct a new expression node and return a pointer to it.  Memory
139a76b5dfcSdrh ** for this node is obtained from sqliteMalloc().  The calling function
140a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed.
141a76b5dfcSdrh */
1424adee20fSdanielk1977 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
143a76b5dfcSdrh   Expr *pNew;
144a76b5dfcSdrh   pNew = sqliteMalloc( sizeof(Expr) );
145a76b5dfcSdrh   if( pNew==0 ){
1464efc4754Sdrh     /* When malloc fails, we leak memory from pLeft and pRight */
147a76b5dfcSdrh     return 0;
148a76b5dfcSdrh   }
149a76b5dfcSdrh   pNew->op = op;
150a76b5dfcSdrh   pNew->pLeft = pLeft;
151a76b5dfcSdrh   pNew->pRight = pRight;
152a76b5dfcSdrh   if( pToken ){
1534b59ab5eSdrh     assert( pToken->dyn==0 );
154a76b5dfcSdrh     pNew->token = *pToken;
1556977fea8Sdrh     pNew->span = *pToken;
156a76b5dfcSdrh   }else{
1574efc4754Sdrh     assert( pNew->token.dyn==0 );
1584efc4754Sdrh     assert( pNew->token.z==0 );
1594efc4754Sdrh     assert( pNew->token.n==0 );
1606977fea8Sdrh     if( pLeft && pRight ){
1614adee20fSdanielk1977       sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
1626977fea8Sdrh     }else{
1636977fea8Sdrh       pNew->span = pNew->token;
1646977fea8Sdrh     }
165a76b5dfcSdrh   }
166a76b5dfcSdrh   return pNew;
167a76b5dfcSdrh }
168a76b5dfcSdrh 
169a76b5dfcSdrh /*
1706977fea8Sdrh ** Set the Expr.span field of the given expression to span all
171a76b5dfcSdrh ** text between the two given tokens.
172a76b5dfcSdrh */
1734adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
1744efc4754Sdrh   assert( pRight!=0 );
1754efc4754Sdrh   assert( pLeft!=0 );
1764efc4754Sdrh   /* Note: pExpr might be NULL due to a prior malloc failure */
1774efc4754Sdrh   if( pExpr && pRight->z && pLeft->z ){
1784b59ab5eSdrh     if( pLeft->dyn==0 && pRight->dyn==0 ){
1796977fea8Sdrh       pExpr->span.z = pLeft->z;
1806977fea8Sdrh       pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
1814b59ab5eSdrh     }else{
1826977fea8Sdrh       pExpr->span.z = 0;
1834b59ab5eSdrh     }
184a76b5dfcSdrh   }
185a76b5dfcSdrh }
186a76b5dfcSdrh 
187a76b5dfcSdrh /*
188a76b5dfcSdrh ** Construct a new expression node for a function with multiple
189a76b5dfcSdrh ** arguments.
190a76b5dfcSdrh */
1914adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
192a76b5dfcSdrh   Expr *pNew;
193a76b5dfcSdrh   pNew = sqliteMalloc( sizeof(Expr) );
194a76b5dfcSdrh   if( pNew==0 ){
1954adee20fSdanielk1977     /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
196a76b5dfcSdrh     return 0;
197a76b5dfcSdrh   }
198a76b5dfcSdrh   pNew->op = TK_FUNCTION;
199a76b5dfcSdrh   pNew->pList = pList;
200a76b5dfcSdrh   if( pToken ){
2014b59ab5eSdrh     assert( pToken->dyn==0 );
202a76b5dfcSdrh     pNew->token = *pToken;
203a76b5dfcSdrh   }else{
204a76b5dfcSdrh     pNew->token.z = 0;
205a76b5dfcSdrh   }
2066977fea8Sdrh   pNew->span = pNew->token;
207a76b5dfcSdrh   return pNew;
208a76b5dfcSdrh }
209a76b5dfcSdrh 
210a76b5dfcSdrh /*
211a2e00042Sdrh ** Recursively delete an expression tree.
212a2e00042Sdrh */
2134adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){
214a2e00042Sdrh   if( p==0 ) return;
2154efc4754Sdrh   if( p->span.dyn ) sqliteFree((char*)p->span.z);
2164efc4754Sdrh   if( p->token.dyn ) sqliteFree((char*)p->token.z);
2174adee20fSdanielk1977   sqlite3ExprDelete(p->pLeft);
2184adee20fSdanielk1977   sqlite3ExprDelete(p->pRight);
2194adee20fSdanielk1977   sqlite3ExprListDelete(p->pList);
2204adee20fSdanielk1977   sqlite3SelectDelete(p->pSelect);
221a2e00042Sdrh   sqliteFree(p);
222a2e00042Sdrh }
223a2e00042Sdrh 
224a76b5dfcSdrh 
225a76b5dfcSdrh /*
226ff78bd2fSdrh ** The following group of routines make deep copies of expressions,
227ff78bd2fSdrh ** expression lists, ID lists, and select statements.  The copies can
228ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines)
229ff78bd2fSdrh ** without effecting the originals.
230ff78bd2fSdrh **
2314adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
2324adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
233ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines.
234ff78bd2fSdrh **
235ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated.
236ff78bd2fSdrh */
2374adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){
238ff78bd2fSdrh   Expr *pNew;
239ff78bd2fSdrh   if( p==0 ) return 0;
240fcb78a49Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
241ff78bd2fSdrh   if( pNew==0 ) return 0;
2423b167c75Sdrh   memcpy(pNew, p, sizeof(*pNew));
2436977fea8Sdrh   if( p->token.z!=0 ){
2444b59ab5eSdrh     pNew->token.z = sqliteStrDup(p->token.z);
2454b59ab5eSdrh     pNew->token.dyn = 1;
2464b59ab5eSdrh   }else{
2474efc4754Sdrh     assert( pNew->token.z==0 );
2484b59ab5eSdrh   }
2496977fea8Sdrh   pNew->span.z = 0;
2504adee20fSdanielk1977   pNew->pLeft = sqlite3ExprDup(p->pLeft);
2514adee20fSdanielk1977   pNew->pRight = sqlite3ExprDup(p->pRight);
2524adee20fSdanielk1977   pNew->pList = sqlite3ExprListDup(p->pList);
2534adee20fSdanielk1977   pNew->pSelect = sqlite3SelectDup(p->pSelect);
254ff78bd2fSdrh   return pNew;
255ff78bd2fSdrh }
2564adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){
2574b59ab5eSdrh   if( pTo->dyn ) sqliteFree((char*)pTo->z);
2584b59ab5eSdrh   if( pFrom->z ){
2594b59ab5eSdrh     pTo->n = pFrom->n;
2604b59ab5eSdrh     pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
2614b59ab5eSdrh     pTo->dyn = 1;
2624b59ab5eSdrh   }else{
2634b59ab5eSdrh     pTo->z = 0;
2644b59ab5eSdrh   }
2654b59ab5eSdrh }
2664adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){
267ff78bd2fSdrh   ExprList *pNew;
2683e7bc9caSdrh   struct ExprList_item *pItem;
269ff78bd2fSdrh   int i;
270ff78bd2fSdrh   if( p==0 ) return 0;
271ff78bd2fSdrh   pNew = sqliteMalloc( sizeof(*pNew) );
272ff78bd2fSdrh   if( pNew==0 ) return 0;
2734305d103Sdrh   pNew->nExpr = pNew->nAlloc = p->nExpr;
2743e7bc9caSdrh   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
2751bdd9b57Sdrh   if( pItem==0 ) return 0;  /* Leaks memory after a malloc failure */
2761bdd9b57Sdrh   for(i=0; i<p->nExpr; i++, pItem++){
2774b59ab5eSdrh     Expr *pNewExpr, *pOldExpr;
2784adee20fSdanielk1977     pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
2796977fea8Sdrh     if( pOldExpr->span.z!=0 && pNewExpr ){
2806977fea8Sdrh       /* Always make a copy of the span for top-level expressions in the
2814b59ab5eSdrh       ** expression list.  The logic in SELECT processing that determines
2824b59ab5eSdrh       ** the names of columns in the result set needs this information */
2834adee20fSdanielk1977       sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
2844b59ab5eSdrh     }
2851f3e905cSdrh     assert( pNewExpr==0 || pNewExpr->span.z!=0
28624b03fd0Sdanielk1977             || pOldExpr->span.z==0 || sqlite3_malloc_failed );
2873e7bc9caSdrh     pItem->zName = sqliteStrDup(p->a[i].zName);
2883e7bc9caSdrh     pItem->sortOrder = p->a[i].sortOrder;
2893e7bc9caSdrh     pItem->isAgg = p->a[i].isAgg;
2903e7bc9caSdrh     pItem->done = 0;
291ff78bd2fSdrh   }
292ff78bd2fSdrh   return pNew;
293ff78bd2fSdrh }
2944adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){
295ad3cab52Sdrh   SrcList *pNew;
296ad3cab52Sdrh   int i;
297113088ecSdrh   int nByte;
298ad3cab52Sdrh   if( p==0 ) return 0;
299113088ecSdrh   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
3004efc4754Sdrh   pNew = sqliteMallocRaw( nByte );
301ad3cab52Sdrh   if( pNew==0 ) return 0;
3024305d103Sdrh   pNew->nSrc = pNew->nAlloc = p->nSrc;
303ad3cab52Sdrh   for(i=0; i<p->nSrc; i++){
3044efc4754Sdrh     struct SrcList_item *pNewItem = &pNew->a[i];
3054efc4754Sdrh     struct SrcList_item *pOldItem = &p->a[i];
3064efc4754Sdrh     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
3074efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
3084efc4754Sdrh     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
3094efc4754Sdrh     pNewItem->jointype = pOldItem->jointype;
3104efc4754Sdrh     pNewItem->iCursor = pOldItem->iCursor;
3114efc4754Sdrh     pNewItem->pTab = 0;
3124adee20fSdanielk1977     pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
3134adee20fSdanielk1977     pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
3144adee20fSdanielk1977     pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
315ad3cab52Sdrh   }
316ad3cab52Sdrh   return pNew;
317ad3cab52Sdrh }
3184adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){
319ff78bd2fSdrh   IdList *pNew;
320ff78bd2fSdrh   int i;
321ff78bd2fSdrh   if( p==0 ) return 0;
3224efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*pNew) );
323ff78bd2fSdrh   if( pNew==0 ) return 0;
3244305d103Sdrh   pNew->nId = pNew->nAlloc = p->nId;
3254efc4754Sdrh   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
326e4697f5eSdrh   if( pNew->a==0 ) return 0;
327ff78bd2fSdrh   for(i=0; i<p->nId; i++){
3284efc4754Sdrh     struct IdList_item *pNewItem = &pNew->a[i];
3294efc4754Sdrh     struct IdList_item *pOldItem = &p->a[i];
3304efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
3314efc4754Sdrh     pNewItem->idx = pOldItem->idx;
332ff78bd2fSdrh   }
333ff78bd2fSdrh   return pNew;
334ff78bd2fSdrh }
3354adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){
336ff78bd2fSdrh   Select *pNew;
337ff78bd2fSdrh   if( p==0 ) return 0;
3384efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
339ff78bd2fSdrh   if( pNew==0 ) return 0;
340ff78bd2fSdrh   pNew->isDistinct = p->isDistinct;
3414adee20fSdanielk1977   pNew->pEList = sqlite3ExprListDup(p->pEList);
3424adee20fSdanielk1977   pNew->pSrc = sqlite3SrcListDup(p->pSrc);
3434adee20fSdanielk1977   pNew->pWhere = sqlite3ExprDup(p->pWhere);
3444adee20fSdanielk1977   pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
3454adee20fSdanielk1977   pNew->pHaving = sqlite3ExprDup(p->pHaving);
3464adee20fSdanielk1977   pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
347ff78bd2fSdrh   pNew->op = p->op;
3484adee20fSdanielk1977   pNew->pPrior = sqlite3SelectDup(p->pPrior);
349ff78bd2fSdrh   pNew->nLimit = p->nLimit;
350ff78bd2fSdrh   pNew->nOffset = p->nOffset;
351ff78bd2fSdrh   pNew->zSelect = 0;
3527b58daeaSdrh   pNew->iLimit = -1;
3537b58daeaSdrh   pNew->iOffset = -1;
354ff78bd2fSdrh   return pNew;
355ff78bd2fSdrh }
356ff78bd2fSdrh 
357ff78bd2fSdrh 
358ff78bd2fSdrh /*
359a76b5dfcSdrh ** Add a new element to the end of an expression list.  If pList is
360a76b5dfcSdrh ** initially NULL, then create a new expression list.
361a76b5dfcSdrh */
3624adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
363a76b5dfcSdrh   if( pList==0 ){
364a76b5dfcSdrh     pList = sqliteMalloc( sizeof(ExprList) );
365a76b5dfcSdrh     if( pList==0 ){
3664adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
367a76b5dfcSdrh       return 0;
368a76b5dfcSdrh     }
3694efc4754Sdrh     assert( pList->nAlloc==0 );
370a76b5dfcSdrh   }
3714305d103Sdrh   if( pList->nAlloc<=pList->nExpr ){
3724305d103Sdrh     pList->nAlloc = pList->nAlloc*2 + 4;
3734efc4754Sdrh     pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
3744efc4754Sdrh     if( pList->a==0 ){
3754adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
3764efc4754Sdrh       pList->nExpr = pList->nAlloc = 0;
377a76b5dfcSdrh       return pList;
378a76b5dfcSdrh     }
379a76b5dfcSdrh   }
3804efc4754Sdrh   assert( pList->a!=0 );
3814efc4754Sdrh   if( pExpr || pName ){
3824efc4754Sdrh     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
3834efc4754Sdrh     memset(pItem, 0, sizeof(*pItem));
3844efc4754Sdrh     pItem->pExpr = pExpr;
385a76b5dfcSdrh     if( pName ){
3864adee20fSdanielk1977       sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
3874adee20fSdanielk1977       sqlite3Dequote(pItem->zName);
388a76b5dfcSdrh     }
389a76b5dfcSdrh   }
390a76b5dfcSdrh   return pList;
391a76b5dfcSdrh }
392a76b5dfcSdrh 
393a76b5dfcSdrh /*
394a76b5dfcSdrh ** Delete an entire expression list.
395a76b5dfcSdrh */
3964adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){
397a76b5dfcSdrh   int i;
398a76b5dfcSdrh   if( pList==0 ) return;
3991bdd9b57Sdrh   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
4001bdd9b57Sdrh   assert( pList->nExpr<=pList->nAlloc );
401a76b5dfcSdrh   for(i=0; i<pList->nExpr; i++){
4024adee20fSdanielk1977     sqlite3ExprDelete(pList->a[i].pExpr);
403a76b5dfcSdrh     sqliteFree(pList->a[i].zName);
404a76b5dfcSdrh   }
405a76b5dfcSdrh   sqliteFree(pList->a);
406a76b5dfcSdrh   sqliteFree(pList);
407a76b5dfcSdrh }
408a76b5dfcSdrh 
409a76b5dfcSdrh /*
410fef5208cSdrh ** Walk an expression tree.  Return 1 if the expression is constant
411fef5208cSdrh ** and 0 if it involves variables.
4122398937bSdrh **
4132398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc")
4142398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is
4152398937bSdrh ** a constant.
416fef5208cSdrh */
4174adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){
418fef5208cSdrh   switch( p->op ){
419fef5208cSdrh     case TK_ID:
420967e8b73Sdrh     case TK_COLUMN:
421fef5208cSdrh     case TK_DOT:
4227bdc0c1dSdrh     case TK_FUNCTION:
423fef5208cSdrh       return 0;
4247bdc0c1dSdrh     case TK_NULL:
4252398937bSdrh     case TK_STRING:
426c572ef7fSdanielk1977     case TK_BLOB:
4279208643dSdrh     case TK_INTEGER:
4289208643dSdrh     case TK_FLOAT:
42950457896Sdrh     case TK_VARIABLE:
4309208643dSdrh       return 1;
431fef5208cSdrh     default: {
4324adee20fSdanielk1977       if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
4334adee20fSdanielk1977       if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
434fef5208cSdrh       if( p->pList ){
435fef5208cSdrh         int i;
436fef5208cSdrh         for(i=0; i<p->pList->nExpr; i++){
4374adee20fSdanielk1977           if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
438fef5208cSdrh         }
439fef5208cSdrh       }
4409208643dSdrh       return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
441fef5208cSdrh     }
442fef5208cSdrh   }
4439208643dSdrh   return 0;
444fef5208cSdrh }
445fef5208cSdrh 
446fef5208cSdrh /*
447202b2df7Sdrh ** If the given expression codes a constant integer that is small enough
448202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer
449202b2df7Sdrh ** in *pValue.  If the expression is not an integer or if it is too big
450202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
451e4de1febSdrh */
4524adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){
453e4de1febSdrh   switch( p->op ){
454e4de1febSdrh     case TK_INTEGER: {
455fec19aadSdrh       if( sqlite3GetInt32(p->token.z, pValue) ){
456e4de1febSdrh         return 1;
457e4de1febSdrh       }
458202b2df7Sdrh       break;
459202b2df7Sdrh     }
460e4de1febSdrh     case TK_STRING: {
461bd790ee3Sdrh       const char *z = p->token.z;
462e4de1febSdrh       int n = p->token.n;
463bd790ee3Sdrh       if( n>0 && z[0]=='-' ){ z++; n--; }
464e4de1febSdrh       while( n>0 && *z && isdigit(*z) ){ z++; n--; }
465fec19aadSdrh       if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
466e4de1febSdrh         return 1;
467e4de1febSdrh       }
468e4de1febSdrh       break;
469e4de1febSdrh     }
4704b59ab5eSdrh     case TK_UPLUS: {
4714adee20fSdanielk1977       return sqlite3ExprIsInteger(p->pLeft, pValue);
4724b59ab5eSdrh     }
473e4de1febSdrh     case TK_UMINUS: {
474e4de1febSdrh       int v;
4754adee20fSdanielk1977       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
476e4de1febSdrh         *pValue = -v;
477e4de1febSdrh         return 1;
478e4de1febSdrh       }
479e4de1febSdrh       break;
480e4de1febSdrh     }
481e4de1febSdrh     default: break;
482e4de1febSdrh   }
483e4de1febSdrh   return 0;
484e4de1febSdrh }
485e4de1febSdrh 
486e4de1febSdrh /*
487c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name.
488c4a3c779Sdrh */
4894adee20fSdanielk1977 int sqlite3IsRowid(const char *z){
4904adee20fSdanielk1977   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
4914adee20fSdanielk1977   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
4924adee20fSdanielk1977   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
493c4a3c779Sdrh   return 0;
494c4a3c779Sdrh }
495c4a3c779Sdrh 
496c4a3c779Sdrh /*
4978141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
4988141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr
4998141f61eSdrh ** expression node refer back to that source column.  The following changes
5008141f61eSdrh ** are made to pExpr:
5018141f61eSdrh **
5028141f61eSdrh **    pExpr->iDb           Set the index in db->aDb[] of the database holding
5038141f61eSdrh **                         the table.
5048141f61eSdrh **    pExpr->iTable        Set to the cursor number for the table obtained
5058141f61eSdrh **                         from pSrcList.
5068141f61eSdrh **    pExpr->iColumn       Set to the column number within the table.
5078141f61eSdrh **    pExpr->op            Set to TK_COLUMN.
5088141f61eSdrh **    pExpr->pLeft         Any expression this points to is deleted
5098141f61eSdrh **    pExpr->pRight        Any expression this points to is deleted.
5108141f61eSdrh **
5118141f61eSdrh ** The pDbToken is the name of the database (the "X").  This value may be
5128141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z.  Any available database
5138141f61eSdrh ** can be used.  The pTableToken is the name of the table (the "Y").  This
5148141f61eSdrh ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
5158141f61eSdrh ** means that the form of the name is Z and that columns from any table
5168141f61eSdrh ** can be used.
5178141f61eSdrh **
5188141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message
5198141f61eSdrh ** in pParse and return non-zero.  Return zero on success.
5208141f61eSdrh */
5218141f61eSdrh static int lookupName(
5228141f61eSdrh   Parse *pParse,      /* The parsing context */
5238141f61eSdrh   Token *pDbToken,     /* Name of the database containing table, or NULL */
5248141f61eSdrh   Token *pTableToken,  /* Name of table containing column, or NULL */
5258141f61eSdrh   Token *pColumnToken, /* Name of the column. */
5268141f61eSdrh   SrcList *pSrcList,   /* List of tables used to resolve column names */
5278141f61eSdrh   ExprList *pEList,    /* List of expressions used to resolve "AS" */
5288141f61eSdrh   Expr *pExpr          /* Make this EXPR node point to the selected column */
5298141f61eSdrh ){
5308141f61eSdrh   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
5318141f61eSdrh   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
5328141f61eSdrh   char *zCol = 0;      /* Name of the column.  The "Z" */
5338141f61eSdrh   int i, j;            /* Loop counters */
5348141f61eSdrh   int cnt = 0;         /* Number of matching column names */
5358141f61eSdrh   int cntTab = 0;      /* Number of matching table names */
5367e26d750Sdrh   sqlite *db = pParse->db;  /* The database */
5378141f61eSdrh 
5388141f61eSdrh   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
5398141f61eSdrh   if( pDbToken && pDbToken->z ){
5408141f61eSdrh     zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
5414adee20fSdanielk1977     sqlite3Dequote(zDb);
5428141f61eSdrh   }else{
5438141f61eSdrh     zDb = 0;
5448141f61eSdrh   }
5458141f61eSdrh   if( pTableToken && pTableToken->z ){
5468141f61eSdrh     zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
5474adee20fSdanielk1977     sqlite3Dequote(zTab);
5488141f61eSdrh   }else{
5498141f61eSdrh     assert( zDb==0 );
5508141f61eSdrh     zTab = 0;
5518141f61eSdrh   }
5528141f61eSdrh   zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
5534adee20fSdanielk1977   sqlite3Dequote(zCol);
55424b03fd0Sdanielk1977   if( sqlite3_malloc_failed ){
5558141f61eSdrh     return 1;  /* Leak memory (zDb and zTab) if malloc fails */
5568141f61eSdrh   }
5578141f61eSdrh   assert( zTab==0 || pEList==0 );
5588141f61eSdrh 
5598141f61eSdrh   pExpr->iTable = -1;
5608141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
5618141f61eSdrh     struct SrcList_item *pItem = &pSrcList->a[i];
5628141f61eSdrh     Table *pTab = pItem->pTab;
5638141f61eSdrh     Column *pCol;
5648141f61eSdrh 
5658141f61eSdrh     if( pTab==0 ) continue;
5668141f61eSdrh     assert( pTab->nCol>0 );
5678141f61eSdrh     if( zTab ){
5688141f61eSdrh       if( pItem->zAlias ){
5698141f61eSdrh         char *zTabName = pItem->zAlias;
5704adee20fSdanielk1977         if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
5718141f61eSdrh       }else{
5728141f61eSdrh         char *zTabName = pTab->zName;
5734adee20fSdanielk1977         if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
5744adee20fSdanielk1977         if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
5758141f61eSdrh           continue;
5768141f61eSdrh         }
5778141f61eSdrh       }
5788141f61eSdrh     }
5798141f61eSdrh     if( 0==(cntTab++) ){
5808141f61eSdrh       pExpr->iTable = pItem->iCursor;
5818141f61eSdrh       pExpr->iDb = pTab->iDb;
5828141f61eSdrh     }
5838141f61eSdrh     for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
5844adee20fSdanielk1977       if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
5858141f61eSdrh         cnt++;
5868141f61eSdrh         pExpr->iTable = pItem->iCursor;
5878141f61eSdrh         pExpr->iDb = pTab->iDb;
5888141f61eSdrh         /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
5898141f61eSdrh         pExpr->iColumn = j==pTab->iPKey ? -1 : j;
590a37cdde0Sdanielk1977         pExpr->affinity = pTab->aCol[j].affinity;
5918141f61eSdrh         break;
5928141f61eSdrh       }
5938141f61eSdrh     }
5948141f61eSdrh   }
5958141f61eSdrh 
5968141f61eSdrh   /* If we have not already resolved the name, then maybe
5978141f61eSdrh   ** it is a new.* or old.* trigger argument reference
5988141f61eSdrh   */
5998141f61eSdrh   if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
6008141f61eSdrh     TriggerStack *pTriggerStack = pParse->trigStack;
6018141f61eSdrh     Table *pTab = 0;
6024adee20fSdanielk1977     if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
6038141f61eSdrh       pExpr->iTable = pTriggerStack->newIdx;
6048141f61eSdrh       assert( pTriggerStack->pTab );
6058141f61eSdrh       pTab = pTriggerStack->pTab;
6064adee20fSdanielk1977     }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
6078141f61eSdrh       pExpr->iTable = pTriggerStack->oldIdx;
6088141f61eSdrh       assert( pTriggerStack->pTab );
6098141f61eSdrh       pTab = pTriggerStack->pTab;
6108141f61eSdrh     }
6118141f61eSdrh 
6128141f61eSdrh     if( pTab ){
6138141f61eSdrh       int j;
6148141f61eSdrh       Column *pCol = pTab->aCol;
6158141f61eSdrh 
6168141f61eSdrh       pExpr->iDb = pTab->iDb;
6178141f61eSdrh       cntTab++;
6188141f61eSdrh       for(j=0; j < pTab->nCol; j++, pCol++) {
6194adee20fSdanielk1977         if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
6208141f61eSdrh           cnt++;
6218141f61eSdrh           pExpr->iColumn = j==pTab->iPKey ? -1 : j;
622a37cdde0Sdanielk1977           pExpr->affinity = pTab->aCol[j].affinity;
6238141f61eSdrh           break;
6248141f61eSdrh         }
6258141f61eSdrh       }
6268141f61eSdrh     }
6278141f61eSdrh   }
6288141f61eSdrh 
6298141f61eSdrh   /*
6308141f61eSdrh   ** Perhaps the name is a reference to the ROWID
6318141f61eSdrh   */
6324adee20fSdanielk1977   if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
6338141f61eSdrh     cnt = 1;
6348141f61eSdrh     pExpr->iColumn = -1;
635a37cdde0Sdanielk1977     pExpr->affinity = SQLITE_AFF_INTEGER;
6368141f61eSdrh   }
6378141f61eSdrh 
6388141f61eSdrh   /*
6398141f61eSdrh   ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
6408141f61eSdrh   ** might refer to an result-set alias.  This happens, for example, when
6418141f61eSdrh   ** we are resolving names in the WHERE clause of the following command:
6428141f61eSdrh   **
6438141f61eSdrh   **     SELECT a+b AS x FROM table WHERE x<10;
6448141f61eSdrh   **
6458141f61eSdrh   ** In cases like this, replace pExpr with a copy of the expression that
6468141f61eSdrh   ** forms the result set entry ("a+b" in the example) and return immediately.
6478141f61eSdrh   ** Note that the expression in the result set should have already been
6488141f61eSdrh   ** resolved by the time the WHERE clause is resolved.
6498141f61eSdrh   */
6508141f61eSdrh   if( cnt==0 && pEList!=0 ){
6518141f61eSdrh     for(j=0; j<pEList->nExpr; j++){
6528141f61eSdrh       char *zAs = pEList->a[j].zName;
6534adee20fSdanielk1977       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
6548141f61eSdrh         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
6558141f61eSdrh         pExpr->op = TK_AS;
6568141f61eSdrh         pExpr->iColumn = j;
6574adee20fSdanielk1977         pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
6588141f61eSdrh         sqliteFree(zCol);
6598141f61eSdrh         assert( zTab==0 && zDb==0 );
6608141f61eSdrh         return 0;
6618141f61eSdrh       }
6628141f61eSdrh     }
6638141f61eSdrh   }
6648141f61eSdrh 
6658141f61eSdrh   /*
6668141f61eSdrh   ** If X and Y are NULL (in other words if only the column name Z is
6678141f61eSdrh   ** supplied) and the value of Z is enclosed in double-quotes, then
6688141f61eSdrh   ** Z is a string literal if it doesn't match any column names.  In that
6698141f61eSdrh   ** case, we need to return right away and not make any changes to
6708141f61eSdrh   ** pExpr.
6718141f61eSdrh   */
6728141f61eSdrh   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
6738141f61eSdrh     sqliteFree(zCol);
6748141f61eSdrh     return 0;
6758141f61eSdrh   }
6768141f61eSdrh 
6778141f61eSdrh   /*
6788141f61eSdrh   ** cnt==0 means there was not match.  cnt>1 means there were two or
6798141f61eSdrh   ** more matches.  Either way, we have an error.
6808141f61eSdrh   */
6818141f61eSdrh   if( cnt!=1 ){
6828141f61eSdrh     char *z = 0;
6838141f61eSdrh     char *zErr;
6848141f61eSdrh     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
6858141f61eSdrh     if( zDb ){
6864adee20fSdanielk1977       sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
6878141f61eSdrh     }else if( zTab ){
6884adee20fSdanielk1977       sqlite3SetString(&z, zTab, ".", zCol, 0);
6898141f61eSdrh     }else{
6908141f61eSdrh       z = sqliteStrDup(zCol);
6918141f61eSdrh     }
6924adee20fSdanielk1977     sqlite3ErrorMsg(pParse, zErr, z);
6938141f61eSdrh     sqliteFree(z);
6948141f61eSdrh   }
6958141f61eSdrh 
6968141f61eSdrh   /* Clean up and return
6978141f61eSdrh   */
6988141f61eSdrh   sqliteFree(zDb);
6998141f61eSdrh   sqliteFree(zTab);
7008141f61eSdrh   sqliteFree(zCol);
7014adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pLeft);
7028141f61eSdrh   pExpr->pLeft = 0;
7034adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pRight);
7048141f61eSdrh   pExpr->pRight = 0;
7058141f61eSdrh   pExpr->op = TK_COLUMN;
7064adee20fSdanielk1977   sqlite3AuthRead(pParse, pExpr, pSrcList);
7078141f61eSdrh   return cnt!=1;
7088141f61eSdrh }
7098141f61eSdrh 
7108141f61eSdrh /*
711cce7d176Sdrh ** This routine walks an expression tree and resolves references to
712967e8b73Sdrh ** table columns.  Nodes of the form ID.ID or ID resolve into an
713aacc543eSdrh ** index to the table in the table list and a column offset.  The
714aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
715aacc543eSdrh ** value is changed to the index of the referenced table in pTabList
716832508b7Sdrh ** plus the "base" value.  The base value will ultimately become the
717aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced
718aacc543eSdrh ** table.  The Expr.iColumn value is changed to the index of the column
719aacc543eSdrh ** of the referenced table.  The Expr.iColumn value for the special
720aacc543eSdrh ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
721aacc543eSdrh ** alias for ROWID.
72219a775c2Sdrh **
723fef5208cSdrh ** We also check for instances of the IN operator.  IN comes in two
724fef5208cSdrh ** forms:
725fef5208cSdrh **
726fef5208cSdrh **           expr IN (exprlist)
727fef5208cSdrh ** and
728fef5208cSdrh **           expr IN (SELECT ...)
729fef5208cSdrh **
730fef5208cSdrh ** The first form is handled by creating a set holding the list
731fef5208cSdrh ** of allowed values.  The second form causes the SELECT to generate
732fef5208cSdrh ** a temporary table.
733fef5208cSdrh **
734fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression.
73519a775c2Sdrh ** If it finds any, it generates code to write the value of that select
73619a775c2Sdrh ** into a memory cell.
737cce7d176Sdrh **
738967e8b73Sdrh ** Unknown columns or tables provoke an error.  The function returns
739cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg.
740cce7d176Sdrh */
7414adee20fSdanielk1977 int sqlite3ExprResolveIds(
742a2e00042Sdrh   Parse *pParse,     /* The parser context */
7438141f61eSdrh   SrcList *pSrcList, /* List of tables used to resolve column names */
744a2e00042Sdrh   ExprList *pEList,  /* List of expressions used to resolve "AS" */
745a2e00042Sdrh   Expr *pExpr        /* The expression to be analyzed. */
746a2e00042Sdrh ){
7476a3ea0e6Sdrh   int i;
7486a3ea0e6Sdrh 
7498141f61eSdrh   if( pExpr==0 || pSrcList==0 ) return 0;
7508141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
7518141f61eSdrh     assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
7526a3ea0e6Sdrh   }
753cce7d176Sdrh   switch( pExpr->op ){
7542398937bSdrh     /* Double-quoted strings (ex: "abc") are used as identifiers if
7552398937bSdrh     ** possible.  Otherwise they remain as strings.  Single-quoted
7562398937bSdrh     ** strings (ex: 'abc') are always string literals.
7572398937bSdrh     */
7582398937bSdrh     case TK_STRING: {
7592398937bSdrh       if( pExpr->token.z[0]=='\'' ) break;
7602398937bSdrh       /* Fall thru into the TK_ID case if this is a double-quoted string */
7612398937bSdrh     }
7628141f61eSdrh     /* A lone identifier is the name of a columnd.
763a2e00042Sdrh     */
764cce7d176Sdrh     case TK_ID: {
7658141f61eSdrh       if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
766cce7d176Sdrh         return 1;
767ed6c8671Sdrh       }
768cce7d176Sdrh       break;
769cce7d176Sdrh     }
770cce7d176Sdrh 
771d24cc427Sdrh     /* A table name and column name:     ID.ID
772d24cc427Sdrh     ** Or a database, table and column:  ID.ID.ID
773d24cc427Sdrh     */
774cce7d176Sdrh     case TK_DOT: {
7758141f61eSdrh       Token *pColumn;
7768141f61eSdrh       Token *pTable;
7778141f61eSdrh       Token *pDb;
7788141f61eSdrh       Expr *pRight;
779cce7d176Sdrh 
780cce7d176Sdrh       pRight = pExpr->pRight;
781d24cc427Sdrh       if( pRight->op==TK_ID ){
7828141f61eSdrh         pDb = 0;
7838141f61eSdrh         pTable = &pExpr->pLeft->token;
7848141f61eSdrh         pColumn = &pRight->token;
785d24cc427Sdrh       }else{
7868141f61eSdrh         assert( pRight->op==TK_DOT );
7878141f61eSdrh         pDb = &pExpr->pLeft->token;
7888141f61eSdrh         pTable = &pRight->pLeft->token;
7898141f61eSdrh         pColumn = &pRight->pRight->token;
790d24cc427Sdrh       }
7918141f61eSdrh       if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
792daffd0e5Sdrh         return 1;
793daffd0e5Sdrh       }
794cce7d176Sdrh       break;
795cce7d176Sdrh     }
796cce7d176Sdrh 
797fef5208cSdrh     case TK_IN: {
798e014a838Sdanielk1977       char affinity;
7994adee20fSdanielk1977       Vdbe *v = sqlite3GetVdbe(pParse);
800d3d39e93Sdrh       KeyInfo keyInfo;
801d3d39e93Sdrh 
802fef5208cSdrh       if( v==0 ) return 1;
8034adee20fSdanielk1977       if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
804cfab11bcSdrh         return 1;
805cfab11bcSdrh       }
806bf3b721fSdanielk1977       affinity = sqlite3ExprAffinity(pExpr->pLeft);
807e014a838Sdanielk1977 
808e014a838Sdanielk1977       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
809e014a838Sdanielk1977       ** expression it is handled the same way. A temporary table is
810e014a838Sdanielk1977       ** filled with single-field index keys representing the results
811e014a838Sdanielk1977       ** from the SELECT or the <exprlist>.
812fef5208cSdrh       **
813e014a838Sdanielk1977       ** If the 'x' expression is a column value, or the SELECT...
814e014a838Sdanielk1977       ** statement returns a column value, then the affinity of that
815e014a838Sdanielk1977       ** column is used to build the index keys. If both 'x' and the
816e014a838Sdanielk1977       ** SELECT... statement are columns, then numeric affinity is used
817e014a838Sdanielk1977       ** if either column has NUMERIC or INTEGER affinity. If neither
818e014a838Sdanielk1977       ** 'x' nor the SELECT... statement are columns, then numeric affinity
819e014a838Sdanielk1977       ** is used.
820fef5208cSdrh       */
821832508b7Sdrh       pExpr->iTable = pParse->nTab++;
822d3d39e93Sdrh       memset(&keyInfo, 0, sizeof(keyInfo));
823d3d39e93Sdrh       keyInfo.nField = 1;
824d3d39e93Sdrh       keyInfo.aColl[0] = pParse->db->pDfltColl;
825d3d39e93Sdrh       sqlite3VdbeOp3(v, OP_OpenTemp, pExpr->iTable, 0, \
826d3d39e93Sdrh            (char*)&keyInfo, P3_KEYINFO);
827f3218feaSdrh       sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
828e014a838Sdanielk1977 
829e014a838Sdanielk1977       if( pExpr->pSelect ){
830e014a838Sdanielk1977         /* Case 1:     expr IN (SELECT ...)
831e014a838Sdanielk1977         **
832e014a838Sdanielk1977         ** Generate code to write the results of the select into the temporary
833e014a838Sdanielk1977         ** table allocated and opened above.
834e014a838Sdanielk1977         */
835e014a838Sdanielk1977         int iParm = pExpr->iTable +  (((int)affinity)<<16);
836e014a838Sdanielk1977         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
837bf3b721fSdanielk1977         sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
838fef5208cSdrh       }else if( pExpr->pList ){
839fef5208cSdrh         /* Case 2:     expr IN (exprlist)
840fef5208cSdrh         **
841e014a838Sdanielk1977 	** For each expression, build an index key from the evaluation and
842e014a838Sdanielk1977         ** store it in the temporary table. If <expr> is a column, then use
843e014a838Sdanielk1977         ** that columns affinity when building index keys. If <expr> is not
844e014a838Sdanielk1977         ** a column, use numeric affinity.
845fef5208cSdrh         */
846e014a838Sdanielk1977         int i;
847e014a838Sdanielk1977         char const *affStr;
848e014a838Sdanielk1977         if( !affinity ){
849e014a838Sdanielk1977           affinity = SQLITE_AFF_NUMERIC;
850e014a838Sdanielk1977         }
851e014a838Sdanielk1977         affStr = sqlite3AffinityString(affinity);
852e014a838Sdanielk1977 
853e014a838Sdanielk1977         /* Loop through each expression in <exprlist>. */
854fef5208cSdrh         for(i=0; i<pExpr->pList->nExpr; i++){
855fef5208cSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
856e014a838Sdanielk1977 
857e014a838Sdanielk1977           /* Check that the expression is constant and valid. */
8584adee20fSdanielk1977           if( !sqlite3ExprIsConstant(pE2) ){
8594adee20fSdanielk1977             sqlite3ErrorMsg(pParse,
860da93d238Sdrh               "right-hand side of IN operator must be constant");
861fef5208cSdrh             return 1;
862fef5208cSdrh           }
8634adee20fSdanielk1977           if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
8644794b980Sdrh             return 1;
8654794b980Sdrh           }
866e014a838Sdanielk1977 
867e014a838Sdanielk1977           /* Evaluate the expression and insert it into the temp table */
8684adee20fSdanielk1977           sqlite3ExprCode(pParse, pE2);
869e014a838Sdanielk1977           sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
8700f69c1e3Sdanielk1977           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
871e014a838Sdanielk1977           sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
872fef5208cSdrh         }
873fef5208cSdrh       }
874cfab11bcSdrh       break;
875fef5208cSdrh     }
876fef5208cSdrh 
87719a775c2Sdrh     case TK_SELECT: {
878fef5208cSdrh       /* This has to be a scalar SELECT.  Generate code to put the
879fef5208cSdrh       ** value of this select in a memory cell and record the number
880967e8b73Sdrh       ** of the memory cell in iColumn.
881fef5208cSdrh       */
882967e8b73Sdrh       pExpr->iColumn = pParse->nMem++;
883bf3b721fSdanielk1977       if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
88419a775c2Sdrh         return 1;
88519a775c2Sdrh       }
88619a775c2Sdrh       break;
88719a775c2Sdrh     }
88819a775c2Sdrh 
889cce7d176Sdrh     /* For all else, just recursively walk the tree */
890cce7d176Sdrh     default: {
891cce7d176Sdrh       if( pExpr->pLeft
8924adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
893cce7d176Sdrh         return 1;
894cce7d176Sdrh       }
895cce7d176Sdrh       if( pExpr->pRight
8964adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
897cce7d176Sdrh         return 1;
898cce7d176Sdrh       }
899cce7d176Sdrh       if( pExpr->pList ){
900cce7d176Sdrh         int i;
901cce7d176Sdrh         ExprList *pList = pExpr->pList;
902cce7d176Sdrh         for(i=0; i<pList->nExpr; i++){
903832508b7Sdrh           Expr *pArg = pList->a[i].pExpr;
9044adee20fSdanielk1977           if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
905cce7d176Sdrh             return 1;
906cce7d176Sdrh           }
907cce7d176Sdrh         }
908cce7d176Sdrh       }
909cce7d176Sdrh     }
910cce7d176Sdrh   }
911cce7d176Sdrh   return 0;
912cce7d176Sdrh }
913cce7d176Sdrh 
914cce7d176Sdrh /*
9154b59ab5eSdrh ** pExpr is a node that defines a function of some kind.  It might
9164b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function
9174b59ab5eSdrh ** that implements an operator, like "a LIKE b".
9184b59ab5eSdrh **
9194b59ab5eSdrh ** This routine makes *pzName point to the name of the function and
9204b59ab5eSdrh ** *pnName hold the number of characters in the function name.
9214b59ab5eSdrh */
9224b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
9234b59ab5eSdrh   switch( pExpr->op ){
9244b59ab5eSdrh     case TK_FUNCTION: {
9254b59ab5eSdrh       *pzName = pExpr->token.z;
9266977fea8Sdrh       *pnName = pExpr->token.n;
9274b59ab5eSdrh       break;
9284b59ab5eSdrh     }
9294b59ab5eSdrh     case TK_LIKE: {
9304b59ab5eSdrh       *pzName = "like";
9314b59ab5eSdrh       *pnName = 4;
9324b59ab5eSdrh       break;
9334b59ab5eSdrh     }
9344b59ab5eSdrh     case TK_GLOB: {
9354b59ab5eSdrh       *pzName = "glob";
9364b59ab5eSdrh       *pnName = 4;
9374b59ab5eSdrh       break;
9384b59ab5eSdrh     }
9394b59ab5eSdrh     default: {
9404b59ab5eSdrh       *pzName = "can't happen";
9414b59ab5eSdrh       *pnName = 12;
9424b59ab5eSdrh       break;
9434b59ab5eSdrh     }
9444b59ab5eSdrh   }
9454b59ab5eSdrh }
9464b59ab5eSdrh 
9474b59ab5eSdrh /*
948cce7d176Sdrh ** Error check the functions in an expression.  Make sure all
949cce7d176Sdrh ** function names are recognized and all functions have the correct
950cce7d176Sdrh ** number of arguments.  Leave an error message in pParse->zErrMsg
951cce7d176Sdrh ** if anything is amiss.  Return the number of errors.
952cce7d176Sdrh **
953cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function
954cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
955cce7d176Sdrh */
9564adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
957cce7d176Sdrh   int nErr = 0;
958cce7d176Sdrh   if( pExpr==0 ) return 0;
959cce7d176Sdrh   switch( pExpr->op ){
9604b59ab5eSdrh     case TK_GLOB:
9614b59ab5eSdrh     case TK_LIKE:
962cce7d176Sdrh     case TK_FUNCTION: {
963c9b84a1fSdrh       int n = pExpr->pList ? pExpr->pList->nExpr : 0;  /* Number of arguments */
964c9b84a1fSdrh       int no_such_func = 0;       /* True if no such function exists */
965c9b84a1fSdrh       int wrong_num_args = 0;     /* True if wrong number of arguments */
966c9b84a1fSdrh       int is_agg = 0;             /* True if is an aggregate function */
967cce7d176Sdrh       int i;
9684b59ab5eSdrh       int nId;                    /* Number of characters in function name */
9694b59ab5eSdrh       const char *zId;            /* The function name. */
9700bce8354Sdrh       FuncDef *pDef;
971*d02eb1fdSdanielk1977       int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
9720bce8354Sdrh 
9734b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
974*d02eb1fdSdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, iPrefEnc, 0);
9750bce8354Sdrh       if( pDef==0 ){
976*d02eb1fdSdanielk1977         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, iPrefEnc, 0);
9770bce8354Sdrh         if( pDef==0 ){
978cce7d176Sdrh           no_such_func = 1;
9798e0a2f90Sdrh         }else{
9808e0a2f90Sdrh           wrong_num_args = 1;
9818e0a2f90Sdrh         }
9828e0a2f90Sdrh       }else{
9830bce8354Sdrh         is_agg = pDef->xFunc==0;
984cce7d176Sdrh       }
9858e0a2f90Sdrh       if( is_agg && !allowAgg ){
9864adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
9878e0a2f90Sdrh         nErr++;
9888e0a2f90Sdrh         is_agg = 0;
9898e0a2f90Sdrh       }else if( no_such_func ){
9904adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
991cce7d176Sdrh         nErr++;
9928e0a2f90Sdrh       }else if( wrong_num_args ){
9934adee20fSdanielk1977         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
994f7a9e1acSdrh              nId, zId);
9958e0a2f90Sdrh         nErr++;
996cce7d176Sdrh       }
997f7a9e1acSdrh       if( is_agg ){
998f7a9e1acSdrh         pExpr->op = TK_AGG_FUNCTION;
999f7a9e1acSdrh         if( pIsAgg ) *pIsAgg = 1;
1000f7a9e1acSdrh       }
1001cce7d176Sdrh       for(i=0; nErr==0 && i<n; i++){
10024adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
10034cfa7934Sdrh                                allowAgg && !is_agg, pIsAgg);
1004cce7d176Sdrh       }
1005d3d39e93Sdrh       /** TODO:  Compute pExpr->affinity based on the expected return
1006d3d39e93Sdrh       ** type of the function */
1007cce7d176Sdrh     }
1008cce7d176Sdrh     default: {
1009cce7d176Sdrh       if( pExpr->pLeft ){
10104adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
1011cce7d176Sdrh       }
1012cce7d176Sdrh       if( nErr==0 && pExpr->pRight ){
10134adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
1014cce7d176Sdrh       }
1015fef5208cSdrh       if( nErr==0 && pExpr->pList ){
1016fef5208cSdrh         int n = pExpr->pList->nExpr;
1017fef5208cSdrh         int i;
1018fef5208cSdrh         for(i=0; nErr==0 && i<n; i++){
10192282792aSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
10204adee20fSdanielk1977           nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
1021fef5208cSdrh         }
1022fef5208cSdrh       }
1023cce7d176Sdrh       break;
1024cce7d176Sdrh     }
1025cce7d176Sdrh   }
1026cce7d176Sdrh   return nErr;
1027cce7d176Sdrh }
1028cce7d176Sdrh 
1029cce7d176Sdrh /*
1030d3d39e93Sdrh ** Return one of the SQLITE_AFF_* affinity types that indicates the likely
1031d3d39e93Sdrh ** data type of the result of the given expression.
1032d3d39e93Sdrh **
1033d3d39e93Sdrh ** Not every expression has a fixed type.  If the type cannot be determined
1034d3d39e93Sdrh ** at compile-time, then try to return the type affinity if the expression
1035d3d39e93Sdrh ** is a column.  Otherwise just return SQLITE_AFF_NONE.
1036c9b84a1fSdrh **
10374adee20fSdanielk1977 ** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
1038c9b84a1fSdrh ** both been called on the expression before it is passed to this routine.
1039c9b84a1fSdrh */
10404adee20fSdanielk1977 int sqlite3ExprType(Expr *p){
1041d3d39e93Sdrh   if( p==0 ) return SQLITE_AFF_NONE;
1042c9b84a1fSdrh   while( p ) switch( p->op ){
1043c9b84a1fSdrh     case TK_CONCAT:
1044736c22b8Sdrh     case TK_STRING:
1045c572ef7fSdanielk1977     case TK_BLOB:
1046d3d39e93Sdrh       return SQLITE_AFF_TEXT;
1047c9b84a1fSdrh 
1048c9b84a1fSdrh     case TK_AS:
1049c9b84a1fSdrh       p = p->pLeft;
1050c9b84a1fSdrh       break;
1051c9b84a1fSdrh 
1052736c22b8Sdrh     case TK_VARIABLE:
1053d3d39e93Sdrh     case TK_NULL:
1054d3d39e93Sdrh       return SQLITE_AFF_NONE;
1055c9b84a1fSdrh 
1056d3d39e93Sdrh     case TK_SELECT:   /*** FIX ME ****/
1057d3d39e93Sdrh     case TK_COLUMN:   /*** FIX ME ****/
1058d3d39e93Sdrh     case TK_CASE:     /*** FIX ME ****/
1059b1363206Sdrh 
1060c9b84a1fSdrh     default:
1061d3d39e93Sdrh       return SQLITE_AFF_NUMERIC;
1062c9b84a1fSdrh   }
1063d3d39e93Sdrh   return SQLITE_AFF_NONE;
1064c9b84a1fSdrh }
1065c9b84a1fSdrh 
1066c9b84a1fSdrh /*
1067fec19aadSdrh ** Generate an instruction that will put the integer describe by
1068fec19aadSdrh ** text z[0..n-1] on the stack.
1069fec19aadSdrh */
1070fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){
1071fec19aadSdrh   int i;
10726fec0762Sdrh   if( sqlite3GetInt32(z, &i) ){
10736fec0762Sdrh     sqlite3VdbeAddOp(v, OP_Integer, i, 0);
10746fec0762Sdrh   }else if( sqlite3FitsIn64Bits(z) ){
10756fec0762Sdrh     sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
1076fec19aadSdrh   }else{
1077fec19aadSdrh     sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1078fec19aadSdrh   }
1079fec19aadSdrh }
1080fec19aadSdrh 
1081fec19aadSdrh /*
1082cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given
10831ccde15dSdrh ** expression and leave the result on the top of stack.
1084cce7d176Sdrh */
10854adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1086cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1087cce7d176Sdrh   int op;
1088daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1089cce7d176Sdrh   switch( pExpr->op ){
1090cce7d176Sdrh     case TK_PLUS:     op = OP_Add;      break;
1091cce7d176Sdrh     case TK_MINUS:    op = OP_Subtract; break;
1092cce7d176Sdrh     case TK_STAR:     op = OP_Multiply; break;
1093cce7d176Sdrh     case TK_SLASH:    op = OP_Divide;   break;
1094cce7d176Sdrh     case TK_AND:      op = OP_And;      break;
1095cce7d176Sdrh     case TK_OR:       op = OP_Or;       break;
1096cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1097cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1098cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1099cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1100cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1101cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1102cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1103cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1104cce7d176Sdrh     case TK_NOT:      op = OP_Not;      break;
1105cce7d176Sdrh     case TK_UMINUS:   op = OP_Negative; break;
1106bf4133cbSdrh     case TK_BITAND:   op = OP_BitAnd;   break;
1107bf4133cbSdrh     case TK_BITOR:    op = OP_BitOr;    break;
1108bf4133cbSdrh     case TK_BITNOT:   op = OP_BitNot;   break;
1109bf4133cbSdrh     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
1110bf4133cbSdrh     case TK_RSHIFT:   op = OP_ShiftRight; break;
1111bf4133cbSdrh     case TK_REM:      op = OP_Remainder;  break;
1112fec19aadSdrh     case TK_FLOAT:    op = OP_Real;       break;
11130f69c1e3Sdanielk1977     case TK_STRING:   op = OP_String8;     break;
1114c572ef7fSdanielk1977     case TK_BLOB:     op = OP_HexBlob;    break;
1115cce7d176Sdrh     default: break;
1116cce7d176Sdrh   }
1117cce7d176Sdrh   switch( pExpr->op ){
1118967e8b73Sdrh     case TK_COLUMN: {
11192282792aSdrh       if( pParse->useAgg ){
11204adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1121c4a3c779Sdrh       }else if( pExpr->iColumn>=0 ){
11224adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1123c4a3c779Sdrh       }else{
11244adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
11252282792aSdrh       }
1126cce7d176Sdrh       break;
1127cce7d176Sdrh     }
1128cce7d176Sdrh     case TK_INTEGER: {
1129fec19aadSdrh       codeInteger(v, pExpr->token.z, pExpr->token.n);
1130fec19aadSdrh       break;
113151e9a445Sdrh     }
1132fec19aadSdrh     case TK_FLOAT:
1133fec19aadSdrh     case TK_STRING: {
1134fec19aadSdrh       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
11354adee20fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1136cce7d176Sdrh       break;
1137cce7d176Sdrh     }
1138c572ef7fSdanielk1977     case TK_BLOB: {
1139c572ef7fSdanielk1977       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1140c572ef7fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1141c572ef7fSdanielk1977       break;
1142c572ef7fSdanielk1977     }
1143cce7d176Sdrh     case TK_NULL: {
11440f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1145cce7d176Sdrh       break;
1146cce7d176Sdrh     }
114750457896Sdrh     case TK_VARIABLE: {
11484adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
114950457896Sdrh       break;
115050457896Sdrh     }
1151c9b84a1fSdrh     case TK_LT:
1152c9b84a1fSdrh     case TK_LE:
1153c9b84a1fSdrh     case TK_GT:
1154c9b84a1fSdrh     case TK_GE:
1155c9b84a1fSdrh     case TK_NE:
1156c9b84a1fSdrh     case TK_EQ: {
1157a37cdde0Sdanielk1977       int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
1158a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1159a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1160a37cdde0Sdanielk1977       sqlite3VdbeAddOp(v, op, p1, 0);
1161a37cdde0Sdanielk1977       break;
1162c9b84a1fSdrh     }
1163cce7d176Sdrh     case TK_AND:
1164cce7d176Sdrh     case TK_OR:
1165cce7d176Sdrh     case TK_PLUS:
1166cce7d176Sdrh     case TK_STAR:
1167cce7d176Sdrh     case TK_MINUS:
1168bf4133cbSdrh     case TK_REM:
1169bf4133cbSdrh     case TK_BITAND:
1170bf4133cbSdrh     case TK_BITOR:
1171c9b84a1fSdrh     case TK_SLASH: {
11724adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
11734adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
11744adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1175cce7d176Sdrh       break;
1176cce7d176Sdrh     }
1177bf4133cbSdrh     case TK_LSHIFT:
1178bf4133cbSdrh     case TK_RSHIFT: {
11794adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
11804adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
11814adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1182bf4133cbSdrh       break;
1183bf4133cbSdrh     }
11840040077dSdrh     case TK_CONCAT: {
11854adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
11864adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
11874adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
11880040077dSdrh       break;
11890040077dSdrh     }
1190cce7d176Sdrh     case TK_UMINUS: {
1191fec19aadSdrh       Expr *pLeft = pExpr->pLeft;
1192fec19aadSdrh       assert( pLeft );
1193fec19aadSdrh       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1194fec19aadSdrh         Token *p = &pLeft->token;
11956e142f54Sdrh         char *z = sqliteMalloc( p->n + 2 );
11966e142f54Sdrh         sprintf(z, "-%.*s", p->n, p->z);
1197fec19aadSdrh         if( pLeft->op==TK_FLOAT ){
1198fec19aadSdrh           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1199e6840900Sdrh         }else{
1200fec19aadSdrh           codeInteger(v, z, p->n+1);
1201e6840900Sdrh         }
12026e142f54Sdrh         sqliteFree(z);
12036e142f54Sdrh         break;
12046e142f54Sdrh       }
12051ccde15dSdrh       /* Fall through into TK_NOT */
12066e142f54Sdrh     }
1207bf4133cbSdrh     case TK_BITNOT:
12086e142f54Sdrh     case TK_NOT: {
12094adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12104adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1211cce7d176Sdrh       break;
1212cce7d176Sdrh     }
1213cce7d176Sdrh     case TK_ISNULL:
1214cce7d176Sdrh     case TK_NOTNULL: {
1215cce7d176Sdrh       int dest;
12164adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
12174adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12184adee20fSdanielk1977       dest = sqlite3VdbeCurrentAddr(v) + 2;
12194adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
12204adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1221cce7d176Sdrh     }
1222a37cdde0Sdanielk1977     break;
12232282792aSdrh     case TK_AGG_FUNCTION: {
12244adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
12252282792aSdrh       break;
12262282792aSdrh     }
12274b59ab5eSdrh     case TK_GLOB:
12284b59ab5eSdrh     case TK_LIKE:
1229cce7d176Sdrh     case TK_FUNCTION: {
1230cce7d176Sdrh       ExprList *pList = pExpr->pList;
123189425d5eSdrh       int nExpr = pList ? pList->nExpr : 0;
12320bce8354Sdrh       FuncDef *pDef;
12334b59ab5eSdrh       int nId;
12344b59ab5eSdrh       const char *zId;
1235682f68b0Sdanielk1977       int p2 = 0;
1236682f68b0Sdanielk1977       int i;
1237*d02eb1fdSdanielk1977       int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
12384b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1239*d02eb1fdSdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, iPrefEnc, 0);
12400bce8354Sdrh       assert( pDef!=0 );
1241f9b596ebSdrh       nExpr = sqlite3ExprCodeExprList(pParse, pList);
1242682f68b0Sdanielk1977       for(i=0; i<nExpr && i<32; i++){
1243*d02eb1fdSdanielk1977         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1244*d02eb1fdSdanielk1977           p2 |= (1<<i);
1245*d02eb1fdSdanielk1977         }
1246682f68b0Sdanielk1977       }
1247682f68b0Sdanielk1977       sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
12486ec2733bSdrh       break;
12496ec2733bSdrh     }
125019a775c2Sdrh     case TK_SELECT: {
12514adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
125219a775c2Sdrh       break;
125319a775c2Sdrh     }
1254fef5208cSdrh     case TK_IN: {
1255fef5208cSdrh       int addr;
1256e014a838Sdanielk1977       char const *affStr;
1257e014a838Sdanielk1977 
1258e014a838Sdanielk1977       /* Figure out the affinity to use to create a key from the results
1259e014a838Sdanielk1977       ** of the expression. affinityStr stores a static string suitable for
1260e014a838Sdanielk1977       ** P3 of OP_MakeKey.
1261e014a838Sdanielk1977       */
1262e014a838Sdanielk1977       affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1263e014a838Sdanielk1977 
12644adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1265e014a838Sdanielk1977 
1266e014a838Sdanielk1977       /* Code the <expr> from "<expr> IN (...)". The temporary table
1267e014a838Sdanielk1977       ** pExpr->iTable contains the values that make up the (...) set.
1268e014a838Sdanielk1977       */
12694adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12704adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1271e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */
12724adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
12730f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1274e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1275e014a838Sdanielk1977       sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1276e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1277e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */
1278e014a838Sdanielk1977 
1279fef5208cSdrh       break;
1280fef5208cSdrh     }
1281fef5208cSdrh     case TK_BETWEEN: {
12824adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12834adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
12844adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
12854adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Ge, 0, 0);
12864adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
12874adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
12884adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Le, 0, 0);
12894adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_And, 0, 0);
1290fef5208cSdrh       break;
1291fef5208cSdrh     }
129251e9a445Sdrh     case TK_UPLUS:
1293a2e00042Sdrh     case TK_AS: {
12944adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1295a2e00042Sdrh       break;
1296a2e00042Sdrh     }
129717a7f8ddSdrh     case TK_CASE: {
129817a7f8ddSdrh       int expr_end_label;
1299f5905aa7Sdrh       int jumpInst;
1300f5905aa7Sdrh       int addr;
1301f5905aa7Sdrh       int nExpr;
130217a7f8ddSdrh       int i;
130317a7f8ddSdrh 
130417a7f8ddSdrh       assert(pExpr->pList);
130517a7f8ddSdrh       assert((pExpr->pList->nExpr % 2) == 0);
130617a7f8ddSdrh       assert(pExpr->pList->nExpr > 0);
1307f5905aa7Sdrh       nExpr = pExpr->pList->nExpr;
13084adee20fSdanielk1977       expr_end_label = sqlite3VdbeMakeLabel(v);
130917a7f8ddSdrh       if( pExpr->pLeft ){
13104adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pLeft);
1311cce7d176Sdrh       }
1312f5905aa7Sdrh       for(i=0; i<nExpr; i=i+2){
13134adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
131417a7f8ddSdrh         if( pExpr->pLeft ){
13154adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
13164adee20fSdanielk1977           jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0);
13174adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1318f5905aa7Sdrh         }else{
13194adee20fSdanielk1977           jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
132017a7f8ddSdrh         }
13214adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
13224adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
13234adee20fSdanielk1977         addr = sqlite3VdbeCurrentAddr(v);
13244adee20fSdanielk1977         sqlite3VdbeChangeP2(v, jumpInst, addr);
132517a7f8ddSdrh       }
1326f570f011Sdrh       if( pExpr->pLeft ){
13274adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1328f570f011Sdrh       }
132917a7f8ddSdrh       if( pExpr->pRight ){
13304adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pRight);
133117a7f8ddSdrh       }else{
13320f69c1e3Sdanielk1977         sqlite3VdbeAddOp(v, OP_String8, 0, 0);
133317a7f8ddSdrh       }
13344adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, expr_end_label);
13356f34903eSdanielk1977       break;
13366f34903eSdanielk1977     }
13376f34903eSdanielk1977     case TK_RAISE: {
13386f34903eSdanielk1977       if( !pParse->trigStack ){
13394adee20fSdanielk1977         sqlite3ErrorMsg(pParse,
1340da93d238Sdrh                        "RAISE() may only be used within a trigger-program");
13416f34903eSdanielk1977         pParse->nErr++;
13426f34903eSdanielk1977 	return;
13436f34903eSdanielk1977       }
13446f34903eSdanielk1977       if( pExpr->iColumn == OE_Rollback ||
13456f34903eSdanielk1977 	  pExpr->iColumn == OE_Abort ||
13466f34903eSdanielk1977 	  pExpr->iColumn == OE_Fail ){
13474adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1348701a0aebSdrh                            pExpr->token.z, pExpr->token.n);
13494adee20fSdanielk1977 	  sqlite3VdbeDequoteP3(v, -1);
13506f34903eSdanielk1977       } else {
13516f34903eSdanielk1977 	  assert( pExpr->iColumn == OE_Ignore );
13524adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1353701a0aebSdrh                            "(IGNORE jump)", 0);
13546f34903eSdanielk1977       }
135517a7f8ddSdrh     }
135617a7f8ddSdrh     break;
135717a7f8ddSdrh   }
1358cce7d176Sdrh }
1359cce7d176Sdrh 
1360cce7d176Sdrh /*
1361268380caSdrh ** Generate code that pushes the value of every element of the given
1362f9b596ebSdrh ** expression list onto the stack.
1363268380caSdrh **
1364268380caSdrh ** Return the number of elements pushed onto the stack.
1365268380caSdrh */
13664adee20fSdanielk1977 int sqlite3ExprCodeExprList(
1367268380caSdrh   Parse *pParse,     /* Parsing context */
1368f9b596ebSdrh   ExprList *pList    /* The expression list to be coded */
1369268380caSdrh ){
1370268380caSdrh   struct ExprList_item *pItem;
1371268380caSdrh   int i, n;
1372268380caSdrh   Vdbe *v;
1373268380caSdrh   if( pList==0 ) return 0;
13744adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1375268380caSdrh   n = pList->nExpr;
1376268380caSdrh   for(pItem=pList->a, i=0; i<n; i++, pItem++){
13774adee20fSdanielk1977     sqlite3ExprCode(pParse, pItem->pExpr);
1378268380caSdrh   }
1379f9b596ebSdrh   return n;
1380268380caSdrh }
1381268380caSdrh 
1382268380caSdrh /*
1383cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made
1384cce7d176Sdrh ** to the label "dest" if the expression is true but execution
1385cce7d176Sdrh ** continues straight thru if the expression is false.
1386f5905aa7Sdrh **
1387f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then
1388f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true.
1389cce7d176Sdrh */
13904adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1391cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1392cce7d176Sdrh   int op = 0;
1393daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1394cce7d176Sdrh   switch( pExpr->op ){
1395cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1396cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1397cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1398cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1399cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1400cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1401cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1402cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1403cce7d176Sdrh     default:  break;
1404cce7d176Sdrh   }
1405cce7d176Sdrh   switch( pExpr->op ){
1406cce7d176Sdrh     case TK_AND: {
14074adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
14084adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
14094adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
14104adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1411cce7d176Sdrh       break;
1412cce7d176Sdrh     }
1413cce7d176Sdrh     case TK_OR: {
14144adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
14154adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1416cce7d176Sdrh       break;
1417cce7d176Sdrh     }
1418cce7d176Sdrh     case TK_NOT: {
14194adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1420cce7d176Sdrh       break;
1421cce7d176Sdrh     }
1422cce7d176Sdrh     case TK_LT:
1423cce7d176Sdrh     case TK_LE:
1424cce7d176Sdrh     case TK_GT:
1425cce7d176Sdrh     case TK_GE:
1426cce7d176Sdrh     case TK_NE:
14270ac65892Sdrh     case TK_EQ: {
1428a37cdde0Sdanielk1977       int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
14294adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14304adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1431a37cdde0Sdanielk1977       sqlite3VdbeAddOp(v, op, p1, dest);
1432cce7d176Sdrh       break;
1433cce7d176Sdrh     }
1434cce7d176Sdrh     case TK_ISNULL:
1435cce7d176Sdrh     case TK_NOTNULL: {
14364adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14374adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1438cce7d176Sdrh       break;
1439cce7d176Sdrh     }
1440fef5208cSdrh     case TK_BETWEEN: {
1441f5905aa7Sdrh       int addr;
14424adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
14434adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
14444adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
14454adee20fSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
14464adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
14474adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest);
14484adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
14494adee20fSdanielk1977       sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
14504adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1451fef5208cSdrh       break;
1452fef5208cSdrh     }
1453cce7d176Sdrh     default: {
14544adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
14554adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
1456cce7d176Sdrh       break;
1457cce7d176Sdrh     }
1458cce7d176Sdrh   }
1459cce7d176Sdrh }
1460cce7d176Sdrh 
1461cce7d176Sdrh /*
146266b89c8fSdrh ** Generate code for a boolean expression such that a jump is made
1463cce7d176Sdrh ** to the label "dest" if the expression is false but execution
1464cce7d176Sdrh ** continues straight thru if the expression is true.
1465f5905aa7Sdrh **
1466f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then
1467f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1468cce7d176Sdrh */
14694adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1470cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1471cce7d176Sdrh   int op = 0;
1472daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1473cce7d176Sdrh   switch( pExpr->op ){
1474cce7d176Sdrh     case TK_LT:       op = OP_Ge;       break;
1475cce7d176Sdrh     case TK_LE:       op = OP_Gt;       break;
1476cce7d176Sdrh     case TK_GT:       op = OP_Le;       break;
1477cce7d176Sdrh     case TK_GE:       op = OP_Lt;       break;
1478cce7d176Sdrh     case TK_NE:       op = OP_Eq;       break;
1479cce7d176Sdrh     case TK_EQ:       op = OP_Ne;       break;
1480cce7d176Sdrh     case TK_ISNULL:   op = OP_NotNull;  break;
1481cce7d176Sdrh     case TK_NOTNULL:  op = OP_IsNull;   break;
1482cce7d176Sdrh     default:  break;
1483cce7d176Sdrh   }
1484cce7d176Sdrh   switch( pExpr->op ){
1485cce7d176Sdrh     case TK_AND: {
14864adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
14874adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1488cce7d176Sdrh       break;
1489cce7d176Sdrh     }
1490cce7d176Sdrh     case TK_OR: {
14914adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
14924adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
14934adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
14944adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1495cce7d176Sdrh       break;
1496cce7d176Sdrh     }
1497cce7d176Sdrh     case TK_NOT: {
14984adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1499cce7d176Sdrh       break;
1500cce7d176Sdrh     }
1501cce7d176Sdrh     case TK_LT:
1502cce7d176Sdrh     case TK_LE:
1503cce7d176Sdrh     case TK_GT:
1504cce7d176Sdrh     case TK_GE:
1505cce7d176Sdrh     case TK_NE:
1506cce7d176Sdrh     case TK_EQ: {
1507a37cdde0Sdanielk1977       int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
15084adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15094adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1510a37cdde0Sdanielk1977       sqlite3VdbeAddOp(v, op, p1, dest);
1511cce7d176Sdrh       break;
1512cce7d176Sdrh     }
1513cce7d176Sdrh     case TK_ISNULL:
1514cce7d176Sdrh     case TK_NOTNULL: {
15154adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15164adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1517cce7d176Sdrh       break;
1518cce7d176Sdrh     }
1519e014a838Sdanielk1977 #if 0
1520fef5208cSdrh     case TK_IN: {
1521f5905aa7Sdrh       int addr;
15224adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15234adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
15244adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
15254adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
15264adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
1527fef5208cSdrh       if( pExpr->pSelect ){
15284adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
1529fef5208cSdrh       }else{
15304adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
1531fef5208cSdrh       }
1532fef5208cSdrh       break;
1533fef5208cSdrh     }
1534e014a838Sdanielk1977 #endif
1535fef5208cSdrh     case TK_BETWEEN: {
1536fef5208cSdrh       int addr;
15374adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15384adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
15394adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
15404adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
15414adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
15424adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
15434adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
15444adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
15454adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest);
1546fef5208cSdrh       break;
1547fef5208cSdrh     }
1548cce7d176Sdrh     default: {
15494adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
15504adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1551cce7d176Sdrh       break;
1552cce7d176Sdrh     }
1553cce7d176Sdrh   }
1554cce7d176Sdrh }
15552282792aSdrh 
15562282792aSdrh /*
15572282792aSdrh ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
15582282792aSdrh ** if they are identical and return FALSE if they differ in any way.
15592282792aSdrh */
15604adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){
15612282792aSdrh   int i;
15622282792aSdrh   if( pA==0 ){
15632282792aSdrh     return pB==0;
15642282792aSdrh   }else if( pB==0 ){
15652282792aSdrh     return 0;
15662282792aSdrh   }
15672282792aSdrh   if( pA->op!=pB->op ) return 0;
15684adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
15694adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
15702282792aSdrh   if( pA->pList ){
15712282792aSdrh     if( pB->pList==0 ) return 0;
15722282792aSdrh     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
15732282792aSdrh     for(i=0; i<pA->pList->nExpr; i++){
15744adee20fSdanielk1977       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
15752282792aSdrh         return 0;
15762282792aSdrh       }
15772282792aSdrh     }
15782282792aSdrh   }else if( pB->pList ){
15792282792aSdrh     return 0;
15802282792aSdrh   }
15812282792aSdrh   if( pA->pSelect || pB->pSelect ) return 0;
15822f2c01e5Sdrh   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
15832282792aSdrh   if( pA->token.z ){
15842282792aSdrh     if( pB->token.z==0 ) return 0;
15856977fea8Sdrh     if( pB->token.n!=pA->token.n ) return 0;
15864adee20fSdanielk1977     if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
15872282792aSdrh   }
15882282792aSdrh   return 1;
15892282792aSdrh }
15902282792aSdrh 
15912282792aSdrh /*
15922282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index.
15932282792aSdrh */
15942282792aSdrh static int appendAggInfo(Parse *pParse){
15952282792aSdrh   if( (pParse->nAgg & 0x7)==0 ){
15962282792aSdrh     int amt = pParse->nAgg + 8;
15976d4abfbeSdrh     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
15986d4abfbeSdrh     if( aAgg==0 ){
15992282792aSdrh       return -1;
16002282792aSdrh     }
16016d4abfbeSdrh     pParse->aAgg = aAgg;
16022282792aSdrh   }
16032282792aSdrh   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
16042282792aSdrh   return pParse->nAgg++;
16052282792aSdrh }
16062282792aSdrh 
16072282792aSdrh /*
16082282792aSdrh ** Analyze the given expression looking for aggregate functions and
16092282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array.
16102282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary.
16112282792aSdrh **
16122282792aSdrh ** This routine should only be called after the expression has been
16134adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
16142282792aSdrh **
16152282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return
16162282792aSdrh ** the number of errors.
16172282792aSdrh */
16184adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
16192282792aSdrh   int i;
16202282792aSdrh   AggExpr *aAgg;
16212282792aSdrh   int nErr = 0;
16222282792aSdrh 
16232282792aSdrh   if( pExpr==0 ) return 0;
16242282792aSdrh   switch( pExpr->op ){
1625967e8b73Sdrh     case TK_COLUMN: {
16262282792aSdrh       aAgg = pParse->aAgg;
16272282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16282282792aSdrh         if( aAgg[i].isAgg ) continue;
16292282792aSdrh         if( aAgg[i].pExpr->iTable==pExpr->iTable
1630967e8b73Sdrh          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
16312282792aSdrh           break;
16322282792aSdrh         }
16332282792aSdrh       }
16342282792aSdrh       if( i>=pParse->nAgg ){
16352282792aSdrh         i = appendAggInfo(pParse);
16362282792aSdrh         if( i<0 ) return 1;
16372282792aSdrh         pParse->aAgg[i].isAgg = 0;
16382282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16392282792aSdrh       }
1640aaf88729Sdrh       pExpr->iAgg = i;
16412282792aSdrh       break;
16422282792aSdrh     }
16432282792aSdrh     case TK_AGG_FUNCTION: {
16442282792aSdrh       aAgg = pParse->aAgg;
16452282792aSdrh       for(i=0; i<pParse->nAgg; i++){
16462282792aSdrh         if( !aAgg[i].isAgg ) continue;
16474adee20fSdanielk1977         if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
16482282792aSdrh           break;
16492282792aSdrh         }
16502282792aSdrh       }
16512282792aSdrh       if( i>=pParse->nAgg ){
1652*d02eb1fdSdanielk1977         int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
16532282792aSdrh         i = appendAggInfo(pParse);
16542282792aSdrh         if( i<0 ) return 1;
16552282792aSdrh         pParse->aAgg[i].isAgg = 1;
16562282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
16574adee20fSdanielk1977         pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
16586977fea8Sdrh              pExpr->token.z, pExpr->token.n,
1659*d02eb1fdSdanielk1977              pExpr->pList ? pExpr->pList->nExpr : 0, iPrefEnc, 0);
16602282792aSdrh       }
16612282792aSdrh       pExpr->iAgg = i;
16622282792aSdrh       break;
16632282792aSdrh     }
16642282792aSdrh     default: {
16652282792aSdrh       if( pExpr->pLeft ){
16664adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
16672282792aSdrh       }
16682282792aSdrh       if( nErr==0 && pExpr->pRight ){
16694adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
16702282792aSdrh       }
16712282792aSdrh       if( nErr==0 && pExpr->pList ){
16722282792aSdrh         int n = pExpr->pList->nExpr;
16732282792aSdrh         int i;
16742282792aSdrh         for(i=0; nErr==0 && i<n; i++){
16754adee20fSdanielk1977           nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
16762282792aSdrh         }
16772282792aSdrh       }
16782282792aSdrh       break;
16792282792aSdrh     }
16802282792aSdrh   }
16812282792aSdrh   return nErr;
16822282792aSdrh }
16838e0a2f90Sdrh 
16848e0a2f90Sdrh /*
1685*d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag
1686*d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
1687*d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return
1688*d02eb1fdSdanielk1977 ** NULL if the function does not exist.
16898e0a2f90Sdrh **
16900bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef
16918e0a2f90Sdrh ** structure is created and liked into the "db" structure if a
16928e0a2f90Sdrh ** no matching function previously existed.  When createFlag is true
16938e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts
16948e0a2f90Sdrh ** any number of arguments will be returned.
16958e0a2f90Sdrh **
16968e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid
16978e0a2f90Sdrh ** function found is returned.  A function is valid if either xFunc
16988e0a2f90Sdrh ** or xStep is non-zero.
1699*d02eb1fdSdanielk1977 **
1700*d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and
1701*d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not
1702*d02eb1fdSdanielk1977 ** match that requested.
17038e0a2f90Sdrh */
17044adee20fSdanielk1977 FuncDef *sqlite3FindFunction(
17058e0a2f90Sdrh   sqlite *db,        /* An open database */
17068e0a2f90Sdrh   const char *zName, /* Name of the function.  Not null-terminated */
17078e0a2f90Sdrh   int nName,         /* Number of characters in the name */
17088e0a2f90Sdrh   int nArg,          /* Number of arguments.  -1 means any number */
1709*d02eb1fdSdanielk1977   int eTextRep,      /* True to retrieve UTF-16 versions. */
17108e0a2f90Sdrh   int createFlag     /* Create new entry if true and does not otherwise exist */
17118e0a2f90Sdrh ){
1712*d02eb1fdSdanielk1977   FuncDef *p;         /* Iterator variable */
1713*d02eb1fdSdanielk1977   FuncDef *pFirst;    /* First function with this name */
1714*d02eb1fdSdanielk1977   FuncDef *pBest = 0; /* Best match found so far */
1715*d02eb1fdSdanielk1977   int matchqual = 0;
1716*d02eb1fdSdanielk1977 
1717*d02eb1fdSdanielk1977   /* Normalize argument values to simplify comparisons below. */
1718*d02eb1fdSdanielk1977   if( eTextRep ) eTextRep = 1;
1719*d02eb1fdSdanielk1977   if( nArg<-1 ) nArg = -1;
1720*d02eb1fdSdanielk1977 
1721*d02eb1fdSdanielk1977   pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1722*d02eb1fdSdanielk1977   for(p=pFirst; p; p=p->pNext){
1723*d02eb1fdSdanielk1977     if( 1 || p->xFunc || p->xStep ){
1724*d02eb1fdSdanielk1977       if( p->nArg==nArg && p->iPrefEnc==eTextRep ){
1725*d02eb1fdSdanielk1977         /* A perfect match. */
1726*d02eb1fdSdanielk1977         pBest = p;
1727*d02eb1fdSdanielk1977         matchqual = 4;
1728*d02eb1fdSdanielk1977         break;
17298e0a2f90Sdrh       }
1730*d02eb1fdSdanielk1977       if( p->nArg==nArg ){
1731*d02eb1fdSdanielk1977         /* Number of arguments matches, but not the text encoding */
1732*d02eb1fdSdanielk1977         pBest = p;
1733*d02eb1fdSdanielk1977         matchqual = 3;
17348e0a2f90Sdrh       }
1735*d02eb1fdSdanielk1977       else if( (p->nArg<0) || (nArg<0) ){
1736*d02eb1fdSdanielk1977         if( matchqual<2 && p->iPrefEnc==eTextRep ){
1737*d02eb1fdSdanielk1977           /* Matched a varargs function with correct text encoding */
1738*d02eb1fdSdanielk1977           pBest = p;
1739*d02eb1fdSdanielk1977           matchqual = 2;
1740*d02eb1fdSdanielk1977         }
1741*d02eb1fdSdanielk1977         if( matchqual<1 ){
1742*d02eb1fdSdanielk1977           /* Matched a varargs function with incorrect text encoding */
1743*d02eb1fdSdanielk1977           pBest = p;
1744*d02eb1fdSdanielk1977           matchqual = 1;
1745*d02eb1fdSdanielk1977         }
1746*d02eb1fdSdanielk1977       }
1747*d02eb1fdSdanielk1977     }
1748*d02eb1fdSdanielk1977   }
1749*d02eb1fdSdanielk1977 
1750*d02eb1fdSdanielk1977   if( createFlag && matchqual<4 &&
1751*d02eb1fdSdanielk1977       (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1752*d02eb1fdSdanielk1977     pBest->nArg = nArg;
1753*d02eb1fdSdanielk1977     pBest->pNext = pFirst;
1754*d02eb1fdSdanielk1977     pBest->zName = (char*)&pBest[1];
1755*d02eb1fdSdanielk1977     memcpy(pBest->zName, zName, nName);
1756*d02eb1fdSdanielk1977     pBest->zName[nName] = 0;
1757*d02eb1fdSdanielk1977     sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
1758*d02eb1fdSdanielk1977   }
1759*d02eb1fdSdanielk1977 
1760*d02eb1fdSdanielk1977   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1761*d02eb1fdSdanielk1977     return pBest;
1762*d02eb1fdSdanielk1977   }
17638e0a2f90Sdrh   return 0;
17648e0a2f90Sdrh }
1765*d02eb1fdSdanielk1977 
1766