xref: /sqlite-3.40.0/src/expr.c (revision fa6bc000)
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*fa6bc000Sdrh ** $Id: expr.c,v 1.161 2004/09/07 16:19:53 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 /*
22391bb0eedSdrh ** Join two expressions using an AND operator.  If either expression is
22491bb0eedSdrh ** NULL, then just return the other expression.
22591bb0eedSdrh */
22691bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
22791bb0eedSdrh   if( pLeft==0 ){
22891bb0eedSdrh     return pRight;
22991bb0eedSdrh   }else if( pRight==0 ){
23091bb0eedSdrh     return pLeft;
23191bb0eedSdrh   }else{
23291bb0eedSdrh     return sqlite3Expr(TK_AND, pLeft, pRight, 0);
23391bb0eedSdrh   }
23491bb0eedSdrh }
23591bb0eedSdrh 
23691bb0eedSdrh /*
2376977fea8Sdrh ** Set the Expr.span field of the given expression to span all
238a76b5dfcSdrh ** text between the two given tokens.
239a76b5dfcSdrh */
2404adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
2414efc4754Sdrh   assert( pRight!=0 );
2424efc4754Sdrh   assert( pLeft!=0 );
24371c697efSdrh   if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
2444b59ab5eSdrh     if( pLeft->dyn==0 && pRight->dyn==0 ){
2456977fea8Sdrh       pExpr->span.z = pLeft->z;
2466977fea8Sdrh       pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
2474b59ab5eSdrh     }else{
2486977fea8Sdrh       pExpr->span.z = 0;
2494b59ab5eSdrh     }
250a76b5dfcSdrh   }
251a76b5dfcSdrh }
252a76b5dfcSdrh 
253a76b5dfcSdrh /*
254a76b5dfcSdrh ** Construct a new expression node for a function with multiple
255a76b5dfcSdrh ** arguments.
256a76b5dfcSdrh */
2574adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
258a76b5dfcSdrh   Expr *pNew;
259a76b5dfcSdrh   pNew = sqliteMalloc( sizeof(Expr) );
260a76b5dfcSdrh   if( pNew==0 ){
2614adee20fSdanielk1977     /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
262a76b5dfcSdrh     return 0;
263a76b5dfcSdrh   }
264a76b5dfcSdrh   pNew->op = TK_FUNCTION;
265a76b5dfcSdrh   pNew->pList = pList;
266a76b5dfcSdrh   if( pToken ){
2674b59ab5eSdrh     assert( pToken->dyn==0 );
268a76b5dfcSdrh     pNew->token = *pToken;
269a76b5dfcSdrh   }else{
270a76b5dfcSdrh     pNew->token.z = 0;
271a76b5dfcSdrh   }
2726977fea8Sdrh   pNew->span = pNew->token;
273a76b5dfcSdrh   return pNew;
274a76b5dfcSdrh }
275a76b5dfcSdrh 
276a76b5dfcSdrh /*
277*fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard
278*fa6bc000Sdrh ** in the original SQL statement.
279*fa6bc000Sdrh **
280*fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential
281*fa6bc000Sdrh ** variable number.
282*fa6bc000Sdrh **
283*fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
284*fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when
285*fa6bc000Sdrh ** the SQL statement comes from an external source.
286*fa6bc000Sdrh **
287*fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
288*fa6bc000Sdrh ** as the previous instance of the same wildcard.  Or if this is the first
289*fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is
290*fa6bc000Sdrh ** assigned.
291*fa6bc000Sdrh */
292*fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
293*fa6bc000Sdrh   Token *pToken;
294*fa6bc000Sdrh   if( pExpr==0 ) return;
295*fa6bc000Sdrh   pToken = &pExpr->token;
296*fa6bc000Sdrh   assert( pToken->n>=1 );
297*fa6bc000Sdrh   assert( pToken->z!=0 );
298*fa6bc000Sdrh   assert( pToken->z[0]!=0 );
299*fa6bc000Sdrh   if( pToken->n==1 ){
300*fa6bc000Sdrh     /* Wildcard of the form "?".  Assign the next variable number */
301*fa6bc000Sdrh     pExpr->iTable = ++pParse->nVar;
302*fa6bc000Sdrh   }else if( pToken->z[0]=='?' ){
303*fa6bc000Sdrh     /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
304*fa6bc000Sdrh     ** use it as the variable number */
305*fa6bc000Sdrh     int i;
306*fa6bc000Sdrh     pExpr->iTable = i = atoi(&pToken->z[1]);
307*fa6bc000Sdrh     if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
308*fa6bc000Sdrh       sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
309*fa6bc000Sdrh           SQLITE_MAX_VARIABLE_NUMBER);
310*fa6bc000Sdrh     }
311*fa6bc000Sdrh     if( i>pParse->nVar ){
312*fa6bc000Sdrh       pParse->nVar = i;
313*fa6bc000Sdrh     }
314*fa6bc000Sdrh   }else{
315*fa6bc000Sdrh     /* Wildcards of the form ":aaa" or "$aaa".  Reuse the same variable
316*fa6bc000Sdrh     ** number as the prior appearance of the same name, or if the name
317*fa6bc000Sdrh     ** has never appeared before, reuse the same variable number
318*fa6bc000Sdrh     */
319*fa6bc000Sdrh     int i, n;
320*fa6bc000Sdrh     n = pToken->n;
321*fa6bc000Sdrh     for(i=0; i<pParse->nVarExpr; i++){
322*fa6bc000Sdrh       Expr *pE;
323*fa6bc000Sdrh       if( (pE = pParse->apVarExpr[i])!=0
324*fa6bc000Sdrh           && pE->token.n==n
325*fa6bc000Sdrh           && memcmp(pE->token.z, pToken->z, n)==0 ){
326*fa6bc000Sdrh         pExpr->iTable = pE->iTable;
327*fa6bc000Sdrh         break;
328*fa6bc000Sdrh       }
329*fa6bc000Sdrh     }
330*fa6bc000Sdrh     if( i>=pParse->nVarExpr ){
331*fa6bc000Sdrh       pExpr->iTable = ++pParse->nVar;
332*fa6bc000Sdrh       if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
333*fa6bc000Sdrh         pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
334*fa6bc000Sdrh         pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
335*fa6bc000Sdrh                        pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
336*fa6bc000Sdrh       }
337*fa6bc000Sdrh       if( !sqlite3_malloc_failed ){
338*fa6bc000Sdrh         assert( pParse->apVarExpr!=0 );
339*fa6bc000Sdrh         pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
340*fa6bc000Sdrh       }
341*fa6bc000Sdrh     }
342*fa6bc000Sdrh   }
343*fa6bc000Sdrh }
344*fa6bc000Sdrh 
345*fa6bc000Sdrh /*
346a2e00042Sdrh ** Recursively delete an expression tree.
347a2e00042Sdrh */
3484adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){
349a2e00042Sdrh   if( p==0 ) return;
3504efc4754Sdrh   if( p->span.dyn ) sqliteFree((char*)p->span.z);
3514efc4754Sdrh   if( p->token.dyn ) sqliteFree((char*)p->token.z);
3524adee20fSdanielk1977   sqlite3ExprDelete(p->pLeft);
3534adee20fSdanielk1977   sqlite3ExprDelete(p->pRight);
3544adee20fSdanielk1977   sqlite3ExprListDelete(p->pList);
3554adee20fSdanielk1977   sqlite3SelectDelete(p->pSelect);
356a2e00042Sdrh   sqliteFree(p);
357a2e00042Sdrh }
358a2e00042Sdrh 
359a76b5dfcSdrh 
360a76b5dfcSdrh /*
361ff78bd2fSdrh ** The following group of routines make deep copies of expressions,
362ff78bd2fSdrh ** expression lists, ID lists, and select statements.  The copies can
363ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines)
364ff78bd2fSdrh ** without effecting the originals.
365ff78bd2fSdrh **
3664adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
3674adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
368ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines.
369ff78bd2fSdrh **
370ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated.
371ff78bd2fSdrh */
3724adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){
373ff78bd2fSdrh   Expr *pNew;
374ff78bd2fSdrh   if( p==0 ) return 0;
375fcb78a49Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
376ff78bd2fSdrh   if( pNew==0 ) return 0;
3773b167c75Sdrh   memcpy(pNew, p, sizeof(*pNew));
3786977fea8Sdrh   if( p->token.z!=0 ){
3794b59ab5eSdrh     pNew->token.z = sqliteStrDup(p->token.z);
3804b59ab5eSdrh     pNew->token.dyn = 1;
3814b59ab5eSdrh   }else{
3824efc4754Sdrh     assert( pNew->token.z==0 );
3834b59ab5eSdrh   }
3846977fea8Sdrh   pNew->span.z = 0;
3854adee20fSdanielk1977   pNew->pLeft = sqlite3ExprDup(p->pLeft);
3864adee20fSdanielk1977   pNew->pRight = sqlite3ExprDup(p->pRight);
3874adee20fSdanielk1977   pNew->pList = sqlite3ExprListDup(p->pList);
3884adee20fSdanielk1977   pNew->pSelect = sqlite3SelectDup(p->pSelect);
389ff78bd2fSdrh   return pNew;
390ff78bd2fSdrh }
3914adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){
3924b59ab5eSdrh   if( pTo->dyn ) sqliteFree((char*)pTo->z);
3934b59ab5eSdrh   if( pFrom->z ){
3944b59ab5eSdrh     pTo->n = pFrom->n;
3954b59ab5eSdrh     pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
3964b59ab5eSdrh     pTo->dyn = 1;
3974b59ab5eSdrh   }else{
3984b59ab5eSdrh     pTo->z = 0;
3994b59ab5eSdrh   }
4004b59ab5eSdrh }
4014adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){
402ff78bd2fSdrh   ExprList *pNew;
4033e7bc9caSdrh   struct ExprList_item *pItem;
404ff78bd2fSdrh   int i;
405ff78bd2fSdrh   if( p==0 ) return 0;
406ff78bd2fSdrh   pNew = sqliteMalloc( sizeof(*pNew) );
407ff78bd2fSdrh   if( pNew==0 ) return 0;
4084305d103Sdrh   pNew->nExpr = pNew->nAlloc = p->nExpr;
4093e7bc9caSdrh   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
410e0048400Sdanielk1977   if( pItem==0 ){
411e0048400Sdanielk1977     sqliteFree(pNew);
412e0048400Sdanielk1977     return 0;
413e0048400Sdanielk1977   }
4141bdd9b57Sdrh   for(i=0; i<p->nExpr; i++, pItem++){
4154b59ab5eSdrh     Expr *pNewExpr, *pOldExpr;
4164adee20fSdanielk1977     pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
4176977fea8Sdrh     if( pOldExpr->span.z!=0 && pNewExpr ){
4186977fea8Sdrh       /* Always make a copy of the span for top-level expressions in the
4194b59ab5eSdrh       ** expression list.  The logic in SELECT processing that determines
4204b59ab5eSdrh       ** the names of columns in the result set needs this information */
4214adee20fSdanielk1977       sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
4224b59ab5eSdrh     }
4231f3e905cSdrh     assert( pNewExpr==0 || pNewExpr->span.z!=0
42424b03fd0Sdanielk1977             || pOldExpr->span.z==0 || sqlite3_malloc_failed );
4253e7bc9caSdrh     pItem->zName = sqliteStrDup(p->a[i].zName);
4263e7bc9caSdrh     pItem->sortOrder = p->a[i].sortOrder;
4273e7bc9caSdrh     pItem->isAgg = p->a[i].isAgg;
4283e7bc9caSdrh     pItem->done = 0;
429ff78bd2fSdrh   }
430ff78bd2fSdrh   return pNew;
431ff78bd2fSdrh }
4324adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){
433ad3cab52Sdrh   SrcList *pNew;
434ad3cab52Sdrh   int i;
435113088ecSdrh   int nByte;
436ad3cab52Sdrh   if( p==0 ) return 0;
437113088ecSdrh   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
4384efc4754Sdrh   pNew = sqliteMallocRaw( nByte );
439ad3cab52Sdrh   if( pNew==0 ) return 0;
4404305d103Sdrh   pNew->nSrc = pNew->nAlloc = p->nSrc;
441ad3cab52Sdrh   for(i=0; i<p->nSrc; i++){
4424efc4754Sdrh     struct SrcList_item *pNewItem = &pNew->a[i];
4434efc4754Sdrh     struct SrcList_item *pOldItem = &p->a[i];
4444efc4754Sdrh     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
4454efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
4464efc4754Sdrh     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
4474efc4754Sdrh     pNewItem->jointype = pOldItem->jointype;
4484efc4754Sdrh     pNewItem->iCursor = pOldItem->iCursor;
4494efc4754Sdrh     pNewItem->pTab = 0;
4504adee20fSdanielk1977     pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
4514adee20fSdanielk1977     pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
4524adee20fSdanielk1977     pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
453ad3cab52Sdrh   }
454ad3cab52Sdrh   return pNew;
455ad3cab52Sdrh }
4564adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){
457ff78bd2fSdrh   IdList *pNew;
458ff78bd2fSdrh   int i;
459ff78bd2fSdrh   if( p==0 ) return 0;
4604efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*pNew) );
461ff78bd2fSdrh   if( pNew==0 ) return 0;
4624305d103Sdrh   pNew->nId = pNew->nAlloc = p->nId;
4634efc4754Sdrh   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
464e4697f5eSdrh   if( pNew->a==0 ) return 0;
465ff78bd2fSdrh   for(i=0; i<p->nId; i++){
4664efc4754Sdrh     struct IdList_item *pNewItem = &pNew->a[i];
4674efc4754Sdrh     struct IdList_item *pOldItem = &p->a[i];
4684efc4754Sdrh     pNewItem->zName = sqliteStrDup(pOldItem->zName);
4694efc4754Sdrh     pNewItem->idx = pOldItem->idx;
470ff78bd2fSdrh   }
471ff78bd2fSdrh   return pNew;
472ff78bd2fSdrh }
4734adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){
474ff78bd2fSdrh   Select *pNew;
475ff78bd2fSdrh   if( p==0 ) return 0;
4764efc4754Sdrh   pNew = sqliteMallocRaw( sizeof(*p) );
477ff78bd2fSdrh   if( pNew==0 ) return 0;
478ff78bd2fSdrh   pNew->isDistinct = p->isDistinct;
4794adee20fSdanielk1977   pNew->pEList = sqlite3ExprListDup(p->pEList);
4804adee20fSdanielk1977   pNew->pSrc = sqlite3SrcListDup(p->pSrc);
4814adee20fSdanielk1977   pNew->pWhere = sqlite3ExprDup(p->pWhere);
4824adee20fSdanielk1977   pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
4834adee20fSdanielk1977   pNew->pHaving = sqlite3ExprDup(p->pHaving);
4844adee20fSdanielk1977   pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
485ff78bd2fSdrh   pNew->op = p->op;
4864adee20fSdanielk1977   pNew->pPrior = sqlite3SelectDup(p->pPrior);
487ff78bd2fSdrh   pNew->nLimit = p->nLimit;
488ff78bd2fSdrh   pNew->nOffset = p->nOffset;
489ff78bd2fSdrh   pNew->zSelect = 0;
4907b58daeaSdrh   pNew->iLimit = -1;
4917b58daeaSdrh   pNew->iOffset = -1;
492dc1bdc4fSdanielk1977   pNew->ppOpenTemp = 0;
493ff78bd2fSdrh   return pNew;
494ff78bd2fSdrh }
495ff78bd2fSdrh 
496ff78bd2fSdrh 
497ff78bd2fSdrh /*
498a76b5dfcSdrh ** Add a new element to the end of an expression list.  If pList is
499a76b5dfcSdrh ** initially NULL, then create a new expression list.
500a76b5dfcSdrh */
5014adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
502a76b5dfcSdrh   if( pList==0 ){
503a76b5dfcSdrh     pList = sqliteMalloc( sizeof(ExprList) );
504a76b5dfcSdrh     if( pList==0 ){
5054adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
506a76b5dfcSdrh       return 0;
507a76b5dfcSdrh     }
5084efc4754Sdrh     assert( pList->nAlloc==0 );
509a76b5dfcSdrh   }
5104305d103Sdrh   if( pList->nAlloc<=pList->nExpr ){
5114305d103Sdrh     pList->nAlloc = pList->nAlloc*2 + 4;
5124efc4754Sdrh     pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
5134efc4754Sdrh     if( pList->a==0 ){
5144adee20fSdanielk1977       /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
5154efc4754Sdrh       pList->nExpr = pList->nAlloc = 0;
516a76b5dfcSdrh       return pList;
517a76b5dfcSdrh     }
518a76b5dfcSdrh   }
5194efc4754Sdrh   assert( pList->a!=0 );
5204efc4754Sdrh   if( pExpr || pName ){
5214efc4754Sdrh     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
5224efc4754Sdrh     memset(pItem, 0, sizeof(*pItem));
5234efc4754Sdrh     pItem->pExpr = pExpr;
524a99db3b6Sdrh     pItem->zName = sqlite3NameFromToken(pName);
525a76b5dfcSdrh   }
526a76b5dfcSdrh   return pList;
527a76b5dfcSdrh }
528a76b5dfcSdrh 
529a76b5dfcSdrh /*
530a76b5dfcSdrh ** Delete an entire expression list.
531a76b5dfcSdrh */
5324adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){
533a76b5dfcSdrh   int i;
534be5c89acSdrh   struct ExprList_item *pItem;
535a76b5dfcSdrh   if( pList==0 ) return;
5361bdd9b57Sdrh   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
5371bdd9b57Sdrh   assert( pList->nExpr<=pList->nAlloc );
538be5c89acSdrh   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
539be5c89acSdrh     sqlite3ExprDelete(pItem->pExpr);
540be5c89acSdrh     sqliteFree(pItem->zName);
541a76b5dfcSdrh   }
542a76b5dfcSdrh   sqliteFree(pList->a);
543a76b5dfcSdrh   sqliteFree(pList);
544a76b5dfcSdrh }
545a76b5dfcSdrh 
546a76b5dfcSdrh /*
547fef5208cSdrh ** Walk an expression tree.  Return 1 if the expression is constant
548fef5208cSdrh ** and 0 if it involves variables.
5492398937bSdrh **
5502398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc")
5512398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is
5522398937bSdrh ** a constant.
553fef5208cSdrh */
5544adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){
555fef5208cSdrh   switch( p->op ){
556fef5208cSdrh     case TK_ID:
557967e8b73Sdrh     case TK_COLUMN:
558fef5208cSdrh     case TK_DOT:
5597bdc0c1dSdrh     case TK_FUNCTION:
560fef5208cSdrh       return 0;
5617bdc0c1dSdrh     case TK_NULL:
5622398937bSdrh     case TK_STRING:
563c572ef7fSdanielk1977     case TK_BLOB:
5649208643dSdrh     case TK_INTEGER:
5659208643dSdrh     case TK_FLOAT:
56650457896Sdrh     case TK_VARIABLE:
5679208643dSdrh       return 1;
568fef5208cSdrh     default: {
5694adee20fSdanielk1977       if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
5704adee20fSdanielk1977       if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
571fef5208cSdrh       if( p->pList ){
572fef5208cSdrh         int i;
573fef5208cSdrh         for(i=0; i<p->pList->nExpr; i++){
5744adee20fSdanielk1977           if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
575fef5208cSdrh         }
576fef5208cSdrh       }
5779208643dSdrh       return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
578fef5208cSdrh     }
579fef5208cSdrh   }
5809208643dSdrh   return 0;
581fef5208cSdrh }
582fef5208cSdrh 
583fef5208cSdrh /*
584202b2df7Sdrh ** If the given expression codes a constant integer that is small enough
585202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer
586202b2df7Sdrh ** in *pValue.  If the expression is not an integer or if it is too big
587202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
588e4de1febSdrh */
5894adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){
590e4de1febSdrh   switch( p->op ){
591e4de1febSdrh     case TK_INTEGER: {
592fec19aadSdrh       if( sqlite3GetInt32(p->token.z, pValue) ){
593e4de1febSdrh         return 1;
594e4de1febSdrh       }
595202b2df7Sdrh       break;
596202b2df7Sdrh     }
597e4de1febSdrh     case TK_STRING: {
5984c755c0fSdrh       const u8 *z = (u8*)p->token.z;
599e4de1febSdrh       int n = p->token.n;
600bd790ee3Sdrh       if( n>0 && z[0]=='-' ){ z++; n--; }
601e4de1febSdrh       while( n>0 && *z && isdigit(*z) ){ z++; n--; }
602fec19aadSdrh       if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
603e4de1febSdrh         return 1;
604e4de1febSdrh       }
605e4de1febSdrh       break;
606e4de1febSdrh     }
6074b59ab5eSdrh     case TK_UPLUS: {
6084adee20fSdanielk1977       return sqlite3ExprIsInteger(p->pLeft, pValue);
6094b59ab5eSdrh     }
610e4de1febSdrh     case TK_UMINUS: {
611e4de1febSdrh       int v;
6124adee20fSdanielk1977       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
613e4de1febSdrh         *pValue = -v;
614e4de1febSdrh         return 1;
615e4de1febSdrh       }
616e4de1febSdrh       break;
617e4de1febSdrh     }
618e4de1febSdrh     default: break;
619e4de1febSdrh   }
620e4de1febSdrh   return 0;
621e4de1febSdrh }
622e4de1febSdrh 
623e4de1febSdrh /*
624c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name.
625c4a3c779Sdrh */
6264adee20fSdanielk1977 int sqlite3IsRowid(const char *z){
6274adee20fSdanielk1977   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
6284adee20fSdanielk1977   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
6294adee20fSdanielk1977   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
630c4a3c779Sdrh   return 0;
631c4a3c779Sdrh }
632c4a3c779Sdrh 
633c4a3c779Sdrh /*
6348141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
6358141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr
6368141f61eSdrh ** expression node refer back to that source column.  The following changes
6378141f61eSdrh ** are made to pExpr:
6388141f61eSdrh **
6398141f61eSdrh **    pExpr->iDb           Set the index in db->aDb[] of the database holding
6408141f61eSdrh **                         the table.
6418141f61eSdrh **    pExpr->iTable        Set to the cursor number for the table obtained
6428141f61eSdrh **                         from pSrcList.
6438141f61eSdrh **    pExpr->iColumn       Set to the column number within the table.
6448141f61eSdrh **    pExpr->op            Set to TK_COLUMN.
6458141f61eSdrh **    pExpr->pLeft         Any expression this points to is deleted
6468141f61eSdrh **    pExpr->pRight        Any expression this points to is deleted.
6478141f61eSdrh **
6488141f61eSdrh ** The pDbToken is the name of the database (the "X").  This value may be
6498141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z.  Any available database
6508141f61eSdrh ** can be used.  The pTableToken is the name of the table (the "Y").  This
6518141f61eSdrh ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
6528141f61eSdrh ** means that the form of the name is Z and that columns from any table
6538141f61eSdrh ** can be used.
6548141f61eSdrh **
6558141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message
6568141f61eSdrh ** in pParse and return non-zero.  Return zero on success.
6578141f61eSdrh */
6588141f61eSdrh static int lookupName(
6598141f61eSdrh   Parse *pParse,      /* The parsing context */
6608141f61eSdrh   Token *pDbToken,     /* Name of the database containing table, or NULL */
6618141f61eSdrh   Token *pTableToken,  /* Name of table containing column, or NULL */
6628141f61eSdrh   Token *pColumnToken, /* Name of the column. */
6638141f61eSdrh   SrcList *pSrcList,   /* List of tables used to resolve column names */
6648141f61eSdrh   ExprList *pEList,    /* List of expressions used to resolve "AS" */
6658141f61eSdrh   Expr *pExpr          /* Make this EXPR node point to the selected column */
6668141f61eSdrh ){
6678141f61eSdrh   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
6688141f61eSdrh   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
6698141f61eSdrh   char *zCol = 0;      /* Name of the column.  The "Z" */
6708141f61eSdrh   int i, j;            /* Loop counters */
6718141f61eSdrh   int cnt = 0;         /* Number of matching column names */
6728141f61eSdrh   int cntTab = 0;      /* Number of matching table names */
6739bb575fdSdrh   sqlite3 *db = pParse->db;  /* The database */
6748141f61eSdrh 
6758141f61eSdrh   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
676a99db3b6Sdrh   zDb = sqlite3NameFromToken(pDbToken);
677a99db3b6Sdrh   zTab = sqlite3NameFromToken(pTableToken);
678a99db3b6Sdrh   zCol = sqlite3NameFromToken(pColumnToken);
67924b03fd0Sdanielk1977   if( sqlite3_malloc_failed ){
6808141f61eSdrh     return 1;  /* Leak memory (zDb and zTab) if malloc fails */
6818141f61eSdrh   }
6828141f61eSdrh   assert( zTab==0 || pEList==0 );
6838141f61eSdrh 
6848141f61eSdrh   pExpr->iTable = -1;
6858141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
6868141f61eSdrh     struct SrcList_item *pItem = &pSrcList->a[i];
6878141f61eSdrh     Table *pTab = pItem->pTab;
6888141f61eSdrh     Column *pCol;
6898141f61eSdrh 
6908141f61eSdrh     if( pTab==0 ) continue;
6918141f61eSdrh     assert( pTab->nCol>0 );
6928141f61eSdrh     if( zTab ){
6938141f61eSdrh       if( pItem->zAlias ){
6948141f61eSdrh         char *zTabName = pItem->zAlias;
6954adee20fSdanielk1977         if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
6968141f61eSdrh       }else{
6978141f61eSdrh         char *zTabName = pTab->zName;
6984adee20fSdanielk1977         if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
6994adee20fSdanielk1977         if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
7008141f61eSdrh           continue;
7018141f61eSdrh         }
7028141f61eSdrh       }
7038141f61eSdrh     }
7048141f61eSdrh     if( 0==(cntTab++) ){
7058141f61eSdrh       pExpr->iTable = pItem->iCursor;
7068141f61eSdrh       pExpr->iDb = pTab->iDb;
7078141f61eSdrh     }
7088141f61eSdrh     for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
7094adee20fSdanielk1977       if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
7108141f61eSdrh         cnt++;
7118141f61eSdrh         pExpr->iTable = pItem->iCursor;
7128141f61eSdrh         pExpr->iDb = pTab->iDb;
7138141f61eSdrh         /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
7148141f61eSdrh         pExpr->iColumn = j==pTab->iPKey ? -1 : j;
715a37cdde0Sdanielk1977         pExpr->affinity = pTab->aCol[j].affinity;
7160202b29eSdanielk1977         pExpr->pColl = pTab->aCol[j].pColl;
7178141f61eSdrh         break;
7188141f61eSdrh       }
7198141f61eSdrh     }
7208141f61eSdrh   }
7218141f61eSdrh 
7228141f61eSdrh   /* If we have not already resolved the name, then maybe
7238141f61eSdrh   ** it is a new.* or old.* trigger argument reference
7248141f61eSdrh   */
7258141f61eSdrh   if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
7268141f61eSdrh     TriggerStack *pTriggerStack = pParse->trigStack;
7278141f61eSdrh     Table *pTab = 0;
7284adee20fSdanielk1977     if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
7298141f61eSdrh       pExpr->iTable = pTriggerStack->newIdx;
7308141f61eSdrh       assert( pTriggerStack->pTab );
7318141f61eSdrh       pTab = pTriggerStack->pTab;
7324adee20fSdanielk1977     }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
7338141f61eSdrh       pExpr->iTable = pTriggerStack->oldIdx;
7348141f61eSdrh       assert( pTriggerStack->pTab );
7358141f61eSdrh       pTab = pTriggerStack->pTab;
7368141f61eSdrh     }
7378141f61eSdrh 
7388141f61eSdrh     if( pTab ){
7398141f61eSdrh       int j;
7408141f61eSdrh       Column *pCol = pTab->aCol;
7418141f61eSdrh 
7428141f61eSdrh       pExpr->iDb = pTab->iDb;
7438141f61eSdrh       cntTab++;
7448141f61eSdrh       for(j=0; j < pTab->nCol; j++, pCol++) {
7454adee20fSdanielk1977         if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
7468141f61eSdrh           cnt++;
7478141f61eSdrh           pExpr->iColumn = j==pTab->iPKey ? -1 : j;
748a37cdde0Sdanielk1977           pExpr->affinity = pTab->aCol[j].affinity;
7490202b29eSdanielk1977           pExpr->pColl = pTab->aCol[j].pColl;
7508141f61eSdrh           break;
7518141f61eSdrh         }
7528141f61eSdrh       }
7538141f61eSdrh     }
7548141f61eSdrh   }
7558141f61eSdrh 
7568141f61eSdrh   /*
7578141f61eSdrh   ** Perhaps the name is a reference to the ROWID
7588141f61eSdrh   */
7594adee20fSdanielk1977   if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
7608141f61eSdrh     cnt = 1;
7618141f61eSdrh     pExpr->iColumn = -1;
762a37cdde0Sdanielk1977     pExpr->affinity = SQLITE_AFF_INTEGER;
7638141f61eSdrh   }
7648141f61eSdrh 
7658141f61eSdrh   /*
7668141f61eSdrh   ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
7678141f61eSdrh   ** might refer to an result-set alias.  This happens, for example, when
7688141f61eSdrh   ** we are resolving names in the WHERE clause of the following command:
7698141f61eSdrh   **
7708141f61eSdrh   **     SELECT a+b AS x FROM table WHERE x<10;
7718141f61eSdrh   **
7728141f61eSdrh   ** In cases like this, replace pExpr with a copy of the expression that
7738141f61eSdrh   ** forms the result set entry ("a+b" in the example) and return immediately.
7748141f61eSdrh   ** Note that the expression in the result set should have already been
7758141f61eSdrh   ** resolved by the time the WHERE clause is resolved.
7768141f61eSdrh   */
7778141f61eSdrh   if( cnt==0 && pEList!=0 ){
7788141f61eSdrh     for(j=0; j<pEList->nExpr; j++){
7798141f61eSdrh       char *zAs = pEList->a[j].zName;
7804adee20fSdanielk1977       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
7818141f61eSdrh         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
7828141f61eSdrh         pExpr->op = TK_AS;
7838141f61eSdrh         pExpr->iColumn = j;
7844adee20fSdanielk1977         pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
7858141f61eSdrh         sqliteFree(zCol);
7868141f61eSdrh         assert( zTab==0 && zDb==0 );
7878141f61eSdrh         return 0;
7888141f61eSdrh       }
7898141f61eSdrh     }
7908141f61eSdrh   }
7918141f61eSdrh 
7928141f61eSdrh   /*
7938141f61eSdrh   ** If X and Y are NULL (in other words if only the column name Z is
7948141f61eSdrh   ** supplied) and the value of Z is enclosed in double-quotes, then
7958141f61eSdrh   ** Z is a string literal if it doesn't match any column names.  In that
7968141f61eSdrh   ** case, we need to return right away and not make any changes to
7978141f61eSdrh   ** pExpr.
7988141f61eSdrh   */
7998141f61eSdrh   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
8008141f61eSdrh     sqliteFree(zCol);
8018141f61eSdrh     return 0;
8028141f61eSdrh   }
8038141f61eSdrh 
8048141f61eSdrh   /*
8058141f61eSdrh   ** cnt==0 means there was not match.  cnt>1 means there were two or
8068141f61eSdrh   ** more matches.  Either way, we have an error.
8078141f61eSdrh   */
8088141f61eSdrh   if( cnt!=1 ){
8098141f61eSdrh     char *z = 0;
8108141f61eSdrh     char *zErr;
8118141f61eSdrh     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
8128141f61eSdrh     if( zDb ){
8134adee20fSdanielk1977       sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
8148141f61eSdrh     }else if( zTab ){
8154adee20fSdanielk1977       sqlite3SetString(&z, zTab, ".", zCol, 0);
8168141f61eSdrh     }else{
8178141f61eSdrh       z = sqliteStrDup(zCol);
8188141f61eSdrh     }
8194adee20fSdanielk1977     sqlite3ErrorMsg(pParse, zErr, z);
8208141f61eSdrh     sqliteFree(z);
8218141f61eSdrh   }
8228141f61eSdrh 
8238141f61eSdrh   /* Clean up and return
8248141f61eSdrh   */
8258141f61eSdrh   sqliteFree(zDb);
8268141f61eSdrh   sqliteFree(zTab);
8278141f61eSdrh   sqliteFree(zCol);
8284adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pLeft);
8298141f61eSdrh   pExpr->pLeft = 0;
8304adee20fSdanielk1977   sqlite3ExprDelete(pExpr->pRight);
8318141f61eSdrh   pExpr->pRight = 0;
8328141f61eSdrh   pExpr->op = TK_COLUMN;
8334adee20fSdanielk1977   sqlite3AuthRead(pParse, pExpr, pSrcList);
8348141f61eSdrh   return cnt!=1;
8358141f61eSdrh }
8368141f61eSdrh 
8378141f61eSdrh /*
838cce7d176Sdrh ** This routine walks an expression tree and resolves references to
839967e8b73Sdrh ** table columns.  Nodes of the form ID.ID or ID resolve into an
840aacc543eSdrh ** index to the table in the table list and a column offset.  The
841aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
842aacc543eSdrh ** value is changed to the index of the referenced table in pTabList
843832508b7Sdrh ** plus the "base" value.  The base value will ultimately become the
844aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced
845aacc543eSdrh ** table.  The Expr.iColumn value is changed to the index of the column
846aacc543eSdrh ** of the referenced table.  The Expr.iColumn value for the special
847aacc543eSdrh ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
848aacc543eSdrh ** alias for ROWID.
84919a775c2Sdrh **
850fef5208cSdrh ** We also check for instances of the IN operator.  IN comes in two
851fef5208cSdrh ** forms:
852fef5208cSdrh **
853fef5208cSdrh **           expr IN (exprlist)
854fef5208cSdrh ** and
855fef5208cSdrh **           expr IN (SELECT ...)
856fef5208cSdrh **
857fef5208cSdrh ** The first form is handled by creating a set holding the list
858fef5208cSdrh ** of allowed values.  The second form causes the SELECT to generate
859fef5208cSdrh ** a temporary table.
860fef5208cSdrh **
861fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression.
86219a775c2Sdrh ** If it finds any, it generates code to write the value of that select
86319a775c2Sdrh ** into a memory cell.
864cce7d176Sdrh **
865967e8b73Sdrh ** Unknown columns or tables provoke an error.  The function returns
866cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg.
867cce7d176Sdrh */
8684adee20fSdanielk1977 int sqlite3ExprResolveIds(
869a2e00042Sdrh   Parse *pParse,     /* The parser context */
8708141f61eSdrh   SrcList *pSrcList, /* List of tables used to resolve column names */
871a2e00042Sdrh   ExprList *pEList,  /* List of expressions used to resolve "AS" */
872a2e00042Sdrh   Expr *pExpr        /* The expression to be analyzed. */
873a2e00042Sdrh ){
8746a3ea0e6Sdrh   int i;
8756a3ea0e6Sdrh 
8768141f61eSdrh   if( pExpr==0 || pSrcList==0 ) return 0;
8778141f61eSdrh   for(i=0; i<pSrcList->nSrc; i++){
8788141f61eSdrh     assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
8796a3ea0e6Sdrh   }
880cce7d176Sdrh   switch( pExpr->op ){
8812398937bSdrh     /* Double-quoted strings (ex: "abc") are used as identifiers if
8822398937bSdrh     ** possible.  Otherwise they remain as strings.  Single-quoted
8832398937bSdrh     ** strings (ex: 'abc') are always string literals.
8842398937bSdrh     */
8852398937bSdrh     case TK_STRING: {
8862398937bSdrh       if( pExpr->token.z[0]=='\'' ) break;
8872398937bSdrh       /* Fall thru into the TK_ID case if this is a double-quoted string */
8882398937bSdrh     }
8898141f61eSdrh     /* A lone identifier is the name of a columnd.
890a2e00042Sdrh     */
891cce7d176Sdrh     case TK_ID: {
8928141f61eSdrh       if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
893cce7d176Sdrh         return 1;
894ed6c8671Sdrh       }
895cce7d176Sdrh       break;
896cce7d176Sdrh     }
897cce7d176Sdrh 
898d24cc427Sdrh     /* A table name and column name:     ID.ID
899d24cc427Sdrh     ** Or a database, table and column:  ID.ID.ID
900d24cc427Sdrh     */
901cce7d176Sdrh     case TK_DOT: {
9028141f61eSdrh       Token *pColumn;
9038141f61eSdrh       Token *pTable;
9048141f61eSdrh       Token *pDb;
9058141f61eSdrh       Expr *pRight;
906cce7d176Sdrh 
907cce7d176Sdrh       pRight = pExpr->pRight;
908d24cc427Sdrh       if( pRight->op==TK_ID ){
9098141f61eSdrh         pDb = 0;
9108141f61eSdrh         pTable = &pExpr->pLeft->token;
9118141f61eSdrh         pColumn = &pRight->token;
912d24cc427Sdrh       }else{
9138141f61eSdrh         assert( pRight->op==TK_DOT );
9148141f61eSdrh         pDb = &pExpr->pLeft->token;
9158141f61eSdrh         pTable = &pRight->pLeft->token;
9168141f61eSdrh         pColumn = &pRight->pRight->token;
917d24cc427Sdrh       }
9188141f61eSdrh       if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
919daffd0e5Sdrh         return 1;
920daffd0e5Sdrh       }
921cce7d176Sdrh       break;
922cce7d176Sdrh     }
923cce7d176Sdrh 
924fef5208cSdrh     case TK_IN: {
925e014a838Sdanielk1977       char affinity;
9264adee20fSdanielk1977       Vdbe *v = sqlite3GetVdbe(pParse);
927d3d39e93Sdrh       KeyInfo keyInfo;
9280202b29eSdanielk1977       int addr;        /* Address of OP_OpenTemp instruction */
929d3d39e93Sdrh 
930fef5208cSdrh       if( v==0 ) return 1;
9314adee20fSdanielk1977       if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
932cfab11bcSdrh         return 1;
933cfab11bcSdrh       }
934bf3b721fSdanielk1977       affinity = sqlite3ExprAffinity(pExpr->pLeft);
935e014a838Sdanielk1977 
936e014a838Sdanielk1977       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
937e014a838Sdanielk1977       ** expression it is handled the same way. A temporary table is
938e014a838Sdanielk1977       ** filled with single-field index keys representing the results
939e014a838Sdanielk1977       ** from the SELECT or the <exprlist>.
940fef5208cSdrh       **
941e014a838Sdanielk1977       ** If the 'x' expression is a column value, or the SELECT...
942e014a838Sdanielk1977       ** statement returns a column value, then the affinity of that
943e014a838Sdanielk1977       ** column is used to build the index keys. If both 'x' and the
944e014a838Sdanielk1977       ** SELECT... statement are columns, then numeric affinity is used
945e014a838Sdanielk1977       ** if either column has NUMERIC or INTEGER affinity. If neither
946e014a838Sdanielk1977       ** 'x' nor the SELECT... statement are columns, then numeric affinity
947e014a838Sdanielk1977       ** is used.
948fef5208cSdrh       */
949832508b7Sdrh       pExpr->iTable = pParse->nTab++;
9500202b29eSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
951d3d39e93Sdrh       memset(&keyInfo, 0, sizeof(keyInfo));
952d3d39e93Sdrh       keyInfo.nField = 1;
953f3218feaSdrh       sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
954e014a838Sdanielk1977 
955e014a838Sdanielk1977       if( pExpr->pSelect ){
956e014a838Sdanielk1977         /* Case 1:     expr IN (SELECT ...)
957e014a838Sdanielk1977         **
958e014a838Sdanielk1977         ** Generate code to write the results of the select into the temporary
959e014a838Sdanielk1977         ** table allocated and opened above.
960e014a838Sdanielk1977         */
961e014a838Sdanielk1977         int iParm = pExpr->iTable +  (((int)affinity)<<16);
962be5c89acSdrh         ExprList *pEList;
963e014a838Sdanielk1977         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
964bf3b721fSdanielk1977         sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
965be5c89acSdrh         pEList = pExpr->pSelect->pEList;
966be5c89acSdrh         if( pEList && pEList->nExpr>0 ){
9677cedc8d4Sdanielk1977           keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
968be5c89acSdrh               pEList->a[0].pExpr);
9690202b29eSdanielk1977         }
970fef5208cSdrh       }else if( pExpr->pList ){
971fef5208cSdrh         /* Case 2:     expr IN (exprlist)
972fef5208cSdrh         **
973e014a838Sdanielk1977 	** For each expression, build an index key from the evaluation and
974e014a838Sdanielk1977         ** store it in the temporary table. If <expr> is a column, then use
975e014a838Sdanielk1977         ** that columns affinity when building index keys. If <expr> is not
976e014a838Sdanielk1977         ** a column, use numeric affinity.
977fef5208cSdrh         */
978e014a838Sdanielk1977         int i;
979e014a838Sdanielk1977         char const *affStr;
980e014a838Sdanielk1977         if( !affinity ){
981e014a838Sdanielk1977           affinity = SQLITE_AFF_NUMERIC;
982e014a838Sdanielk1977         }
983e014a838Sdanielk1977         affStr = sqlite3AffinityString(affinity);
9840202b29eSdanielk1977         keyInfo.aColl[0] = pExpr->pLeft->pColl;
985e014a838Sdanielk1977 
986e014a838Sdanielk1977         /* Loop through each expression in <exprlist>. */
987fef5208cSdrh         for(i=0; i<pExpr->pList->nExpr; i++){
988fef5208cSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
989e014a838Sdanielk1977 
990e014a838Sdanielk1977           /* Check that the expression is constant and valid. */
9914adee20fSdanielk1977           if( !sqlite3ExprIsConstant(pE2) ){
9924adee20fSdanielk1977             sqlite3ErrorMsg(pParse,
993da93d238Sdrh               "right-hand side of IN operator must be constant");
994fef5208cSdrh             return 1;
995fef5208cSdrh           }
9964adee20fSdanielk1977           if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
9974794b980Sdrh             return 1;
9984794b980Sdrh           }
999e014a838Sdanielk1977 
1000e014a838Sdanielk1977           /* Evaluate the expression and insert it into the temp table */
10014adee20fSdanielk1977           sqlite3ExprCode(pParse, pE2);
1002ededfd5eSdanielk1977           sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
10030f69c1e3Sdanielk1977           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1004e014a838Sdanielk1977           sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
1005fef5208cSdrh         }
1006fef5208cSdrh       }
10070202b29eSdanielk1977       sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
10080202b29eSdanielk1977 
1009cfab11bcSdrh       break;
1010fef5208cSdrh     }
1011fef5208cSdrh 
101219a775c2Sdrh     case TK_SELECT: {
1013fef5208cSdrh       /* This has to be a scalar SELECT.  Generate code to put the
1014fef5208cSdrh       ** value of this select in a memory cell and record the number
1015967e8b73Sdrh       ** of the memory cell in iColumn.
1016fef5208cSdrh       */
1017967e8b73Sdrh       pExpr->iColumn = pParse->nMem++;
1018bf3b721fSdanielk1977       if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
101919a775c2Sdrh         return 1;
102019a775c2Sdrh       }
102119a775c2Sdrh       break;
102219a775c2Sdrh     }
102319a775c2Sdrh 
1024cce7d176Sdrh     /* For all else, just recursively walk the tree */
1025cce7d176Sdrh     default: {
1026cce7d176Sdrh       if( pExpr->pLeft
10274adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
1028cce7d176Sdrh         return 1;
1029cce7d176Sdrh       }
1030cce7d176Sdrh       if( pExpr->pRight
10314adee20fSdanielk1977       && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
1032cce7d176Sdrh         return 1;
1033cce7d176Sdrh       }
1034cce7d176Sdrh       if( pExpr->pList ){
1035cce7d176Sdrh         int i;
1036cce7d176Sdrh         ExprList *pList = pExpr->pList;
1037cce7d176Sdrh         for(i=0; i<pList->nExpr; i++){
1038832508b7Sdrh           Expr *pArg = pList->a[i].pExpr;
10394adee20fSdanielk1977           if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
1040cce7d176Sdrh             return 1;
1041cce7d176Sdrh           }
1042cce7d176Sdrh         }
1043cce7d176Sdrh       }
1044cce7d176Sdrh     }
1045cce7d176Sdrh   }
1046cce7d176Sdrh   return 0;
1047cce7d176Sdrh }
1048cce7d176Sdrh 
1049cce7d176Sdrh /*
10504b59ab5eSdrh ** pExpr is a node that defines a function of some kind.  It might
10514b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function
10524b59ab5eSdrh ** that implements an operator, like "a LIKE b".
10534b59ab5eSdrh **
10544b59ab5eSdrh ** This routine makes *pzName point to the name of the function and
10554b59ab5eSdrh ** *pnName hold the number of characters in the function name.
10564b59ab5eSdrh */
10574b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
10584b59ab5eSdrh   switch( pExpr->op ){
10594b59ab5eSdrh     case TK_FUNCTION: {
10604b59ab5eSdrh       *pzName = pExpr->token.z;
10616977fea8Sdrh       *pnName = pExpr->token.n;
10624b59ab5eSdrh       break;
10634b59ab5eSdrh     }
10644b59ab5eSdrh     case TK_LIKE: {
10654b59ab5eSdrh       *pzName = "like";
10664b59ab5eSdrh       *pnName = 4;
10674b59ab5eSdrh       break;
10684b59ab5eSdrh     }
10694b59ab5eSdrh     case TK_GLOB: {
10704b59ab5eSdrh       *pzName = "glob";
10714b59ab5eSdrh       *pnName = 4;
10724b59ab5eSdrh       break;
10734b59ab5eSdrh     }
10744b59ab5eSdrh     default: {
10754b59ab5eSdrh       *pzName = "can't happen";
10764b59ab5eSdrh       *pnName = 12;
10774b59ab5eSdrh       break;
10784b59ab5eSdrh     }
10794b59ab5eSdrh   }
10804b59ab5eSdrh }
10814b59ab5eSdrh 
10824b59ab5eSdrh /*
1083cce7d176Sdrh ** Error check the functions in an expression.  Make sure all
1084cce7d176Sdrh ** function names are recognized and all functions have the correct
1085cce7d176Sdrh ** number of arguments.  Leave an error message in pParse->zErrMsg
1086cce7d176Sdrh ** if anything is amiss.  Return the number of errors.
1087cce7d176Sdrh **
1088cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function
1089cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1090cce7d176Sdrh */
10914adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
1092cce7d176Sdrh   int nErr = 0;
1093cce7d176Sdrh   if( pExpr==0 ) return 0;
1094cce7d176Sdrh   switch( pExpr->op ){
10954b59ab5eSdrh     case TK_GLOB:
10964b59ab5eSdrh     case TK_LIKE:
1097cce7d176Sdrh     case TK_FUNCTION: {
1098c9b84a1fSdrh       int n = pExpr->pList ? pExpr->pList->nExpr : 0;  /* Number of arguments */
1099c9b84a1fSdrh       int no_such_func = 0;       /* True if no such function exists */
1100c9b84a1fSdrh       int wrong_num_args = 0;     /* True if wrong number of arguments */
1101c9b84a1fSdrh       int is_agg = 0;             /* True if is an aggregate function */
1102cce7d176Sdrh       int i;
11034b59ab5eSdrh       int nId;                    /* Number of characters in function name */
11044b59ab5eSdrh       const char *zId;            /* The function name. */
11050bce8354Sdrh       FuncDef *pDef;
1106d8123366Sdanielk1977       int enc = pParse->db->enc;
11070bce8354Sdrh 
11084b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1109d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
11100bce8354Sdrh       if( pDef==0 ){
1111d8123366Sdanielk1977         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
11120bce8354Sdrh         if( pDef==0 ){
1113cce7d176Sdrh           no_such_func = 1;
11148e0a2f90Sdrh         }else{
11158e0a2f90Sdrh           wrong_num_args = 1;
11168e0a2f90Sdrh         }
11178e0a2f90Sdrh       }else{
11180bce8354Sdrh         is_agg = pDef->xFunc==0;
1119cce7d176Sdrh       }
11208e0a2f90Sdrh       if( is_agg && !allowAgg ){
11214adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
11228e0a2f90Sdrh         nErr++;
11238e0a2f90Sdrh         is_agg = 0;
11248e0a2f90Sdrh       }else if( no_such_func ){
11254adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1126cce7d176Sdrh         nErr++;
11278e0a2f90Sdrh       }else if( wrong_num_args ){
11284adee20fSdanielk1977         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1129f7a9e1acSdrh              nId, zId);
11308e0a2f90Sdrh         nErr++;
1131cce7d176Sdrh       }
1132f7a9e1acSdrh       if( is_agg ){
1133f7a9e1acSdrh         pExpr->op = TK_AGG_FUNCTION;
1134f7a9e1acSdrh         if( pIsAgg ) *pIsAgg = 1;
1135f7a9e1acSdrh       }
1136cce7d176Sdrh       for(i=0; nErr==0 && i<n; i++){
11374adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
11384cfa7934Sdrh                                allowAgg && !is_agg, pIsAgg);
1139cce7d176Sdrh       }
11400202b29eSdanielk1977       /* FIX ME:  Compute pExpr->affinity based on the expected return
11410202b29eSdanielk1977       ** type of the function
11420202b29eSdanielk1977       */
1143cce7d176Sdrh     }
1144cce7d176Sdrh     default: {
1145cce7d176Sdrh       if( pExpr->pLeft ){
11464adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
1147cce7d176Sdrh       }
1148cce7d176Sdrh       if( nErr==0 && pExpr->pRight ){
11494adee20fSdanielk1977         nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
1150cce7d176Sdrh       }
1151fef5208cSdrh       if( nErr==0 && pExpr->pList ){
1152fef5208cSdrh         int n = pExpr->pList->nExpr;
1153fef5208cSdrh         int i;
1154fef5208cSdrh         for(i=0; nErr==0 && i<n; i++){
11552282792aSdrh           Expr *pE2 = pExpr->pList->a[i].pExpr;
11564adee20fSdanielk1977           nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
1157fef5208cSdrh         }
1158fef5208cSdrh       }
1159cce7d176Sdrh       break;
1160cce7d176Sdrh     }
1161cce7d176Sdrh   }
1162cce7d176Sdrh   return nErr;
1163cce7d176Sdrh }
1164cce7d176Sdrh 
1165cce7d176Sdrh /*
1166290c1948Sdrh ** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1167290c1948Sdrh **
1168290c1948Sdrh ** This routine is provided as a convenience since it is very common
1169290c1948Sdrh ** to call ResolveIds() and Check() back to back.
1170290c1948Sdrh */
1171290c1948Sdrh int sqlite3ExprResolveAndCheck(
1172290c1948Sdrh   Parse *pParse,     /* The parser context */
1173290c1948Sdrh   SrcList *pSrcList, /* List of tables used to resolve column names */
1174290c1948Sdrh   ExprList *pEList,  /* List of expressions used to resolve "AS" */
1175290c1948Sdrh   Expr *pExpr,       /* The expression to be analyzed. */
1176290c1948Sdrh   int allowAgg,      /* True to allow aggregate expressions */
1177290c1948Sdrh   int *pIsAgg        /* Set to TRUE if aggregates are found */
1178290c1948Sdrh ){
1179290c1948Sdrh   if( pExpr==0 ) return 0;
1180290c1948Sdrh   if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1181290c1948Sdrh     return 1;
1182290c1948Sdrh   }
1183290c1948Sdrh   return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1184290c1948Sdrh }
1185290c1948Sdrh 
1186290c1948Sdrh /*
1187fec19aadSdrh ** Generate an instruction that will put the integer describe by
1188fec19aadSdrh ** text z[0..n-1] on the stack.
1189fec19aadSdrh */
1190fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){
1191fec19aadSdrh   int i;
11926fec0762Sdrh   if( sqlite3GetInt32(z, &i) ){
11936fec0762Sdrh     sqlite3VdbeAddOp(v, OP_Integer, i, 0);
11946fec0762Sdrh   }else if( sqlite3FitsIn64Bits(z) ){
11956fec0762Sdrh     sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
1196fec19aadSdrh   }else{
1197fec19aadSdrh     sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1198fec19aadSdrh   }
1199fec19aadSdrh }
1200fec19aadSdrh 
1201fec19aadSdrh /*
1202cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given
12031ccde15dSdrh ** expression and leave the result on the top of stack.
1204cce7d176Sdrh */
12054adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1206cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1207cce7d176Sdrh   int op;
1208daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1209cce7d176Sdrh   switch( pExpr->op ){
1210cce7d176Sdrh     case TK_PLUS:     op = OP_Add;      break;
1211cce7d176Sdrh     case TK_MINUS:    op = OP_Subtract; break;
1212cce7d176Sdrh     case TK_STAR:     op = OP_Multiply; break;
1213cce7d176Sdrh     case TK_SLASH:    op = OP_Divide;   break;
1214cce7d176Sdrh     case TK_AND:      op = OP_And;      break;
1215cce7d176Sdrh     case TK_OR:       op = OP_Or;       break;
1216cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1217cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1218cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1219cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1220cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1221cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1222cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1223cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1224cce7d176Sdrh     case TK_NOT:      op = OP_Not;      break;
1225cce7d176Sdrh     case TK_UMINUS:   op = OP_Negative; break;
1226bf4133cbSdrh     case TK_BITAND:   op = OP_BitAnd;   break;
1227bf4133cbSdrh     case TK_BITOR:    op = OP_BitOr;    break;
1228bf4133cbSdrh     case TK_BITNOT:   op = OP_BitNot;   break;
1229bf4133cbSdrh     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
1230bf4133cbSdrh     case TK_RSHIFT:   op = OP_ShiftRight; break;
1231bf4133cbSdrh     case TK_REM:      op = OP_Remainder;  break;
1232fec19aadSdrh     case TK_FLOAT:    op = OP_Real;       break;
12330f69c1e3Sdanielk1977     case TK_STRING:   op = OP_String8;    break;
1234c572ef7fSdanielk1977     case TK_BLOB:     op = OP_HexBlob;    break;
1235855eb1cfSdrh     case TK_CONCAT:   op = OP_Concat;     break;
1236cfe9a69fSdanielk1977     default: op = 0; break;
1237cce7d176Sdrh   }
1238cce7d176Sdrh   switch( pExpr->op ){
1239967e8b73Sdrh     case TK_COLUMN: {
12402282792aSdrh       if( pParse->useAgg ){
12414adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1242c4a3c779Sdrh       }else if( pExpr->iColumn>=0 ){
12434adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1244c4a3c779Sdrh       }else{
12454adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
12462282792aSdrh       }
1247cce7d176Sdrh       break;
1248cce7d176Sdrh     }
1249cce7d176Sdrh     case TK_INTEGER: {
1250fec19aadSdrh       codeInteger(v, pExpr->token.z, pExpr->token.n);
1251fec19aadSdrh       break;
125251e9a445Sdrh     }
1253fec19aadSdrh     case TK_FLOAT:
1254fec19aadSdrh     case TK_STRING: {
1255fec19aadSdrh       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
12564adee20fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1257cce7d176Sdrh       break;
1258cce7d176Sdrh     }
1259c572ef7fSdanielk1977     case TK_BLOB: {
1260c572ef7fSdanielk1977       sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1261c572ef7fSdanielk1977       sqlite3VdbeDequoteP3(v, -1);
1262c572ef7fSdanielk1977       break;
1263c572ef7fSdanielk1977     }
1264cce7d176Sdrh     case TK_NULL: {
12650f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1266cce7d176Sdrh       break;
1267cce7d176Sdrh     }
126850457896Sdrh     case TK_VARIABLE: {
12694adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
1270895d7472Sdrh       if( pExpr->token.n>1 ){
1271895d7472Sdrh         sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1272895d7472Sdrh       }
127350457896Sdrh       break;
127450457896Sdrh     }
1275c9b84a1fSdrh     case TK_LT:
1276c9b84a1fSdrh     case TK_LE:
1277c9b84a1fSdrh     case TK_GT:
1278c9b84a1fSdrh     case TK_GE:
1279c9b84a1fSdrh     case TK_NE:
1280c9b84a1fSdrh     case TK_EQ: {
1281a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1282a37cdde0Sdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1283be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
1284a37cdde0Sdanielk1977       break;
1285c9b84a1fSdrh     }
1286cce7d176Sdrh     case TK_AND:
1287cce7d176Sdrh     case TK_OR:
1288cce7d176Sdrh     case TK_PLUS:
1289cce7d176Sdrh     case TK_STAR:
1290cce7d176Sdrh     case TK_MINUS:
1291bf4133cbSdrh     case TK_REM:
1292bf4133cbSdrh     case TK_BITAND:
1293bf4133cbSdrh     case TK_BITOR:
129417c40294Sdrh     case TK_SLASH:
1295bf4133cbSdrh     case TK_LSHIFT:
1296855eb1cfSdrh     case TK_RSHIFT:
12970040077dSdrh     case TK_CONCAT: {
12984adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
12994adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1300855eb1cfSdrh       sqlite3VdbeAddOp(v, op, 0, 0);
13010040077dSdrh       break;
13020040077dSdrh     }
1303cce7d176Sdrh     case TK_UMINUS: {
1304fec19aadSdrh       Expr *pLeft = pExpr->pLeft;
1305fec19aadSdrh       assert( pLeft );
1306fec19aadSdrh       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1307fec19aadSdrh         Token *p = &pLeft->token;
13086e142f54Sdrh         char *z = sqliteMalloc( p->n + 2 );
13096e142f54Sdrh         sprintf(z, "-%.*s", p->n, p->z);
1310fec19aadSdrh         if( pLeft->op==TK_FLOAT ){
1311fec19aadSdrh           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1312e6840900Sdrh         }else{
1313fec19aadSdrh           codeInteger(v, z, p->n+1);
1314e6840900Sdrh         }
13156e142f54Sdrh         sqliteFree(z);
13166e142f54Sdrh         break;
13176e142f54Sdrh       }
13181ccde15dSdrh       /* Fall through into TK_NOT */
13196e142f54Sdrh     }
1320bf4133cbSdrh     case TK_BITNOT:
13216e142f54Sdrh     case TK_NOT: {
13224adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
13234adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 0, 0);
1324cce7d176Sdrh       break;
1325cce7d176Sdrh     }
1326cce7d176Sdrh     case TK_ISNULL:
1327cce7d176Sdrh     case TK_NOTNULL: {
1328cce7d176Sdrh       int dest;
13294adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
13304adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
13314adee20fSdanielk1977       dest = sqlite3VdbeCurrentAddr(v) + 2;
13324adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
13334adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1334cce7d176Sdrh     }
1335a37cdde0Sdanielk1977     break;
13362282792aSdrh     case TK_AGG_FUNCTION: {
13374adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
13382282792aSdrh       break;
13392282792aSdrh     }
13404b59ab5eSdrh     case TK_GLOB:
13414b59ab5eSdrh     case TK_LIKE:
1342cce7d176Sdrh     case TK_FUNCTION: {
1343cce7d176Sdrh       ExprList *pList = pExpr->pList;
134489425d5eSdrh       int nExpr = pList ? pList->nExpr : 0;
13450bce8354Sdrh       FuncDef *pDef;
13464b59ab5eSdrh       int nId;
13474b59ab5eSdrh       const char *zId;
1348682f68b0Sdanielk1977       int p2 = 0;
1349682f68b0Sdanielk1977       int i;
1350d8123366Sdanielk1977       u8 enc = pParse->db->enc;
1351dc1bdc4fSdanielk1977       CollSeq *pColl = 0;
13524b59ab5eSdrh       getFunctionName(pExpr, &zId, &nId);
1353d8123366Sdanielk1977       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
13540bce8354Sdrh       assert( pDef!=0 );
1355f9b596ebSdrh       nExpr = sqlite3ExprCodeExprList(pParse, pList);
1356682f68b0Sdanielk1977       for(i=0; i<nExpr && i<32; i++){
1357d02eb1fdSdanielk1977         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1358d02eb1fdSdanielk1977           p2 |= (1<<i);
1359d02eb1fdSdanielk1977         }
1360dc1bdc4fSdanielk1977         if( pDef->needCollSeq && !pColl ){
1361dc1bdc4fSdanielk1977           pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1362dc1bdc4fSdanielk1977         }
1363dc1bdc4fSdanielk1977       }
1364dc1bdc4fSdanielk1977       if( pDef->needCollSeq ){
1365dc1bdc4fSdanielk1977         if( !pColl ) pColl = pParse->db->pDfltColl;
1366d8123366Sdanielk1977         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
1367682f68b0Sdanielk1977       }
1368682f68b0Sdanielk1977       sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
13696ec2733bSdrh       break;
13706ec2733bSdrh     }
137119a775c2Sdrh     case TK_SELECT: {
13724adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
137319a775c2Sdrh       break;
137419a775c2Sdrh     }
1375fef5208cSdrh     case TK_IN: {
1376fef5208cSdrh       int addr;
1377e014a838Sdanielk1977       char const *affStr;
1378e014a838Sdanielk1977 
1379e014a838Sdanielk1977       /* Figure out the affinity to use to create a key from the results
1380e014a838Sdanielk1977       ** of the expression. affinityStr stores a static string suitable for
1381ededfd5eSdanielk1977       ** P3 of OP_MakeRecord.
1382e014a838Sdanielk1977       */
1383e014a838Sdanielk1977       affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1384e014a838Sdanielk1977 
13854adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1386e014a838Sdanielk1977 
1387e014a838Sdanielk1977       /* Code the <expr> from "<expr> IN (...)". The temporary table
1388e014a838Sdanielk1977       ** pExpr->iTable contains the values that make up the (...) set.
1389e014a838Sdanielk1977       */
13904adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
13914adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1392e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */
13934adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
13940f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
1395e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1396ededfd5eSdanielk1977       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1397e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1398e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */
1399e014a838Sdanielk1977 
1400fef5208cSdrh       break;
1401fef5208cSdrh     }
1402fef5208cSdrh     case TK_BETWEEN: {
1403be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1404be5c89acSdrh       struct ExprList_item *pLItem = pExpr->pList->a;
1405be5c89acSdrh       Expr *pRight = pLItem->pExpr;
1406be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
14074adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1408be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1409be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
14104adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1411be5c89acSdrh       pLItem++;
1412be5c89acSdrh       pRight = pLItem->pExpr;
1413be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1414be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
14154adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_And, 0, 0);
1416fef5208cSdrh       break;
1417fef5208cSdrh     }
141851e9a445Sdrh     case TK_UPLUS:
1419a2e00042Sdrh     case TK_AS: {
14204adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
1421a2e00042Sdrh       break;
1422a2e00042Sdrh     }
142317a7f8ddSdrh     case TK_CASE: {
142417a7f8ddSdrh       int expr_end_label;
1425f5905aa7Sdrh       int jumpInst;
1426f5905aa7Sdrh       int addr;
1427f5905aa7Sdrh       int nExpr;
142817a7f8ddSdrh       int i;
1429be5c89acSdrh       ExprList *pEList;
1430be5c89acSdrh       struct ExprList_item *aListelem;
143117a7f8ddSdrh 
143217a7f8ddSdrh       assert(pExpr->pList);
143317a7f8ddSdrh       assert((pExpr->pList->nExpr % 2) == 0);
143417a7f8ddSdrh       assert(pExpr->pList->nExpr > 0);
1435be5c89acSdrh       pEList = pExpr->pList;
1436be5c89acSdrh       aListelem = pEList->a;
1437be5c89acSdrh       nExpr = pEList->nExpr;
14384adee20fSdanielk1977       expr_end_label = sqlite3VdbeMakeLabel(v);
143917a7f8ddSdrh       if( pExpr->pLeft ){
14404adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pLeft);
1441cce7d176Sdrh       }
1442f5905aa7Sdrh       for(i=0; i<nExpr; i=i+2){
1443be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i].pExpr);
144417a7f8ddSdrh         if( pExpr->pLeft ){
14454adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1446be5c89acSdrh           jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1447be5c89acSdrh                                  OP_Ne, 0, 1);
14484adee20fSdanielk1977           sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1449f5905aa7Sdrh         }else{
14504adee20fSdanielk1977           jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
145117a7f8ddSdrh         }
1452be5c89acSdrh         sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
14534adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
14544adee20fSdanielk1977         addr = sqlite3VdbeCurrentAddr(v);
14554adee20fSdanielk1977         sqlite3VdbeChangeP2(v, jumpInst, addr);
145617a7f8ddSdrh       }
1457f570f011Sdrh       if( pExpr->pLeft ){
14584adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1459f570f011Sdrh       }
146017a7f8ddSdrh       if( pExpr->pRight ){
14614adee20fSdanielk1977         sqlite3ExprCode(pParse, pExpr->pRight);
146217a7f8ddSdrh       }else{
14630f69c1e3Sdanielk1977         sqlite3VdbeAddOp(v, OP_String8, 0, 0);
146417a7f8ddSdrh       }
14654adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, expr_end_label);
14666f34903eSdanielk1977       break;
14676f34903eSdanielk1977     }
14686f34903eSdanielk1977     case TK_RAISE: {
14696f34903eSdanielk1977       if( !pParse->trigStack ){
14704adee20fSdanielk1977         sqlite3ErrorMsg(pParse,
1471da93d238Sdrh                        "RAISE() may only be used within a trigger-program");
14726f34903eSdanielk1977         pParse->nErr++;
14736f34903eSdanielk1977 	return;
14746f34903eSdanielk1977       }
14756f34903eSdanielk1977       if( pExpr->iColumn == OE_Rollback ||
14766f34903eSdanielk1977 	  pExpr->iColumn == OE_Abort ||
14776f34903eSdanielk1977 	  pExpr->iColumn == OE_Fail ){
14784adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1479701a0aebSdrh                            pExpr->token.z, pExpr->token.n);
14804adee20fSdanielk1977 	  sqlite3VdbeDequoteP3(v, -1);
14816f34903eSdanielk1977       } else {
14826f34903eSdanielk1977 	  assert( pExpr->iColumn == OE_Ignore );
14834adee20fSdanielk1977 	  sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1484701a0aebSdrh                            "(IGNORE jump)", 0);
14856f34903eSdanielk1977       }
148617a7f8ddSdrh     }
148717a7f8ddSdrh     break;
148817a7f8ddSdrh   }
1489cce7d176Sdrh }
1490cce7d176Sdrh 
1491cce7d176Sdrh /*
1492268380caSdrh ** Generate code that pushes the value of every element of the given
1493f9b596ebSdrh ** expression list onto the stack.
1494268380caSdrh **
1495268380caSdrh ** Return the number of elements pushed onto the stack.
1496268380caSdrh */
14974adee20fSdanielk1977 int sqlite3ExprCodeExprList(
1498268380caSdrh   Parse *pParse,     /* Parsing context */
1499f9b596ebSdrh   ExprList *pList    /* The expression list to be coded */
1500268380caSdrh ){
1501268380caSdrh   struct ExprList_item *pItem;
1502268380caSdrh   int i, n;
1503268380caSdrh   Vdbe *v;
1504268380caSdrh   if( pList==0 ) return 0;
15054adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1506268380caSdrh   n = pList->nExpr;
1507268380caSdrh   for(pItem=pList->a, i=0; i<n; i++, pItem++){
15084adee20fSdanielk1977     sqlite3ExprCode(pParse, pItem->pExpr);
1509268380caSdrh   }
1510f9b596ebSdrh   return n;
1511268380caSdrh }
1512268380caSdrh 
1513268380caSdrh /*
1514cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made
1515cce7d176Sdrh ** to the label "dest" if the expression is true but execution
1516cce7d176Sdrh ** continues straight thru if the expression is false.
1517f5905aa7Sdrh **
1518f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then
1519f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true.
1520cce7d176Sdrh */
15214adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1522cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1523cce7d176Sdrh   int op = 0;
1524daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1525cce7d176Sdrh   switch( pExpr->op ){
1526cce7d176Sdrh     case TK_LT:       op = OP_Lt;       break;
1527cce7d176Sdrh     case TK_LE:       op = OP_Le;       break;
1528cce7d176Sdrh     case TK_GT:       op = OP_Gt;       break;
1529cce7d176Sdrh     case TK_GE:       op = OP_Ge;       break;
1530cce7d176Sdrh     case TK_NE:       op = OP_Ne;       break;
1531cce7d176Sdrh     case TK_EQ:       op = OP_Eq;       break;
1532cce7d176Sdrh     case TK_ISNULL:   op = OP_IsNull;   break;
1533cce7d176Sdrh     case TK_NOTNULL:  op = OP_NotNull;  break;
1534cce7d176Sdrh     default:  break;
1535cce7d176Sdrh   }
1536cce7d176Sdrh   switch( pExpr->op ){
1537cce7d176Sdrh     case TK_AND: {
15384adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
15394adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
15404adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
15414adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1542cce7d176Sdrh       break;
1543cce7d176Sdrh     }
1544cce7d176Sdrh     case TK_OR: {
15454adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
15464adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1547cce7d176Sdrh       break;
1548cce7d176Sdrh     }
1549cce7d176Sdrh     case TK_NOT: {
15504adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1551cce7d176Sdrh       break;
1552cce7d176Sdrh     }
1553cce7d176Sdrh     case TK_LT:
1554cce7d176Sdrh     case TK_LE:
1555cce7d176Sdrh     case TK_GT:
1556cce7d176Sdrh     case TK_GE:
1557cce7d176Sdrh     case TK_NE:
15580ac65892Sdrh     case TK_EQ: {
15594adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15604adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1561be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1562cce7d176Sdrh       break;
1563cce7d176Sdrh     }
1564cce7d176Sdrh     case TK_ISNULL:
1565cce7d176Sdrh     case TK_NOTNULL: {
15664adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
15674adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1568cce7d176Sdrh       break;
1569cce7d176Sdrh     }
1570fef5208cSdrh     case TK_BETWEEN: {
15710202b29eSdanielk1977       /* The expression "x BETWEEN y AND z" is implemented as:
15720202b29eSdanielk1977       **
15730202b29eSdanielk1977       ** 1 IF (x < y) GOTO 3
15740202b29eSdanielk1977       ** 2 IF (x <= z) GOTO <dest>
15750202b29eSdanielk1977       ** 3 ...
15760202b29eSdanielk1977       */
1577f5905aa7Sdrh       int addr;
1578be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1579be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1580be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
15814adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1582be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1583be5c89acSdrh       addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
15840202b29eSdanielk1977 
1585be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1586be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1587be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
15880202b29eSdanielk1977 
15894adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
15904adee20fSdanielk1977       sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
15914adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1592fef5208cSdrh       break;
1593fef5208cSdrh     }
1594cce7d176Sdrh     default: {
15954adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
15964adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
1597cce7d176Sdrh       break;
1598cce7d176Sdrh     }
1599cce7d176Sdrh   }
1600cce7d176Sdrh }
1601cce7d176Sdrh 
1602cce7d176Sdrh /*
160366b89c8fSdrh ** Generate code for a boolean expression such that a jump is made
1604cce7d176Sdrh ** to the label "dest" if the expression is false but execution
1605cce7d176Sdrh ** continues straight thru if the expression is true.
1606f5905aa7Sdrh **
1607f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then
1608f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1609cce7d176Sdrh */
16104adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1611cce7d176Sdrh   Vdbe *v = pParse->pVdbe;
1612cce7d176Sdrh   int op = 0;
1613daffd0e5Sdrh   if( v==0 || pExpr==0 ) return;
1614cce7d176Sdrh   switch( pExpr->op ){
1615cce7d176Sdrh     case TK_LT:       op = OP_Ge;       break;
1616cce7d176Sdrh     case TK_LE:       op = OP_Gt;       break;
1617cce7d176Sdrh     case TK_GT:       op = OP_Le;       break;
1618cce7d176Sdrh     case TK_GE:       op = OP_Lt;       break;
1619cce7d176Sdrh     case TK_NE:       op = OP_Eq;       break;
1620cce7d176Sdrh     case TK_EQ:       op = OP_Ne;       break;
1621cce7d176Sdrh     case TK_ISNULL:   op = OP_NotNull;  break;
1622cce7d176Sdrh     case TK_NOTNULL:  op = OP_IsNull;   break;
1623cce7d176Sdrh     default:  break;
1624cce7d176Sdrh   }
1625cce7d176Sdrh   switch( pExpr->op ){
1626cce7d176Sdrh     case TK_AND: {
16274adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
16284adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1629cce7d176Sdrh       break;
1630cce7d176Sdrh     }
1631cce7d176Sdrh     case TK_OR: {
16324adee20fSdanielk1977       int d2 = sqlite3VdbeMakeLabel(v);
16334adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
16344adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
16354adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, d2);
1636cce7d176Sdrh       break;
1637cce7d176Sdrh     }
1638cce7d176Sdrh     case TK_NOT: {
16394adee20fSdanielk1977       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1640cce7d176Sdrh       break;
1641cce7d176Sdrh     }
1642cce7d176Sdrh     case TK_LT:
1643cce7d176Sdrh     case TK_LE:
1644cce7d176Sdrh     case TK_GT:
1645cce7d176Sdrh     case TK_GE:
1646cce7d176Sdrh     case TK_NE:
1647cce7d176Sdrh     case TK_EQ: {
16484adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
16494adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pRight);
1650be5c89acSdrh       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1651cce7d176Sdrh       break;
1652cce7d176Sdrh     }
1653cce7d176Sdrh     case TK_ISNULL:
1654cce7d176Sdrh     case TK_NOTNULL: {
16554adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr->pLeft);
16564adee20fSdanielk1977       sqlite3VdbeAddOp(v, op, 1, dest);
1657cce7d176Sdrh       break;
1658cce7d176Sdrh     }
1659fef5208cSdrh     case TK_BETWEEN: {
16600202b29eSdanielk1977       /* The expression is "x BETWEEN y AND z". It is implemented as:
16610202b29eSdanielk1977       **
16620202b29eSdanielk1977       ** 1 IF (x >= y) GOTO 3
16630202b29eSdanielk1977       ** 2 GOTO <dest>
16640202b29eSdanielk1977       ** 3 IF (x > z) GOTO <dest>
16650202b29eSdanielk1977       */
1666fef5208cSdrh       int addr;
1667be5c89acSdrh       Expr *pLeft = pExpr->pLeft;
1668be5c89acSdrh       Expr *pRight = pExpr->pList->a[0].pExpr;
1669be5c89acSdrh       sqlite3ExprCode(pParse, pLeft);
16704adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1671be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
16724adee20fSdanielk1977       addr = sqlite3VdbeCurrentAddr(v);
1673be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1674be5c89acSdrh 
16754adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
16764adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1677be5c89acSdrh       pRight = pExpr->pList->a[1].pExpr;
1678be5c89acSdrh       sqlite3ExprCode(pParse, pRight);
1679be5c89acSdrh       codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
1680fef5208cSdrh       break;
1681fef5208cSdrh     }
1682cce7d176Sdrh     default: {
16834adee20fSdanielk1977       sqlite3ExprCode(pParse, pExpr);
16844adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1685cce7d176Sdrh       break;
1686cce7d176Sdrh     }
1687cce7d176Sdrh   }
1688cce7d176Sdrh }
16892282792aSdrh 
16902282792aSdrh /*
16912282792aSdrh ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
16922282792aSdrh ** if they are identical and return FALSE if they differ in any way.
16932282792aSdrh */
16944adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){
16952282792aSdrh   int i;
16962282792aSdrh   if( pA==0 ){
16972282792aSdrh     return pB==0;
16982282792aSdrh   }else if( pB==0 ){
16992282792aSdrh     return 0;
17002282792aSdrh   }
17012282792aSdrh   if( pA->op!=pB->op ) return 0;
17024adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
17034adee20fSdanielk1977   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
17042282792aSdrh   if( pA->pList ){
17052282792aSdrh     if( pB->pList==0 ) return 0;
17062282792aSdrh     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
17072282792aSdrh     for(i=0; i<pA->pList->nExpr; i++){
17084adee20fSdanielk1977       if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
17092282792aSdrh         return 0;
17102282792aSdrh       }
17112282792aSdrh     }
17122282792aSdrh   }else if( pB->pList ){
17132282792aSdrh     return 0;
17142282792aSdrh   }
17152282792aSdrh   if( pA->pSelect || pB->pSelect ) return 0;
17162f2c01e5Sdrh   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
17172282792aSdrh   if( pA->token.z ){
17182282792aSdrh     if( pB->token.z==0 ) return 0;
17196977fea8Sdrh     if( pB->token.n!=pA->token.n ) return 0;
17204adee20fSdanielk1977     if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
17212282792aSdrh   }
17222282792aSdrh   return 1;
17232282792aSdrh }
17242282792aSdrh 
17252282792aSdrh /*
17262282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index.
17272282792aSdrh */
17282282792aSdrh static int appendAggInfo(Parse *pParse){
17292282792aSdrh   if( (pParse->nAgg & 0x7)==0 ){
17302282792aSdrh     int amt = pParse->nAgg + 8;
17316d4abfbeSdrh     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
17326d4abfbeSdrh     if( aAgg==0 ){
17332282792aSdrh       return -1;
17342282792aSdrh     }
17356d4abfbeSdrh     pParse->aAgg = aAgg;
17362282792aSdrh   }
17372282792aSdrh   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
17382282792aSdrh   return pParse->nAgg++;
17392282792aSdrh }
17402282792aSdrh 
17412282792aSdrh /*
17422282792aSdrh ** Analyze the given expression looking for aggregate functions and
17432282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array.
17442282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary.
17452282792aSdrh **
17462282792aSdrh ** This routine should only be called after the expression has been
17474adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
17482282792aSdrh **
17492282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return
17502282792aSdrh ** the number of errors.
17512282792aSdrh */
17524adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
17532282792aSdrh   int i;
17542282792aSdrh   AggExpr *aAgg;
17552282792aSdrh   int nErr = 0;
17562282792aSdrh 
17572282792aSdrh   if( pExpr==0 ) return 0;
17582282792aSdrh   switch( pExpr->op ){
1759967e8b73Sdrh     case TK_COLUMN: {
17602282792aSdrh       aAgg = pParse->aAgg;
17612282792aSdrh       for(i=0; i<pParse->nAgg; i++){
17622282792aSdrh         if( aAgg[i].isAgg ) continue;
17632282792aSdrh         if( aAgg[i].pExpr->iTable==pExpr->iTable
1764967e8b73Sdrh          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
17652282792aSdrh           break;
17662282792aSdrh         }
17672282792aSdrh       }
17682282792aSdrh       if( i>=pParse->nAgg ){
17692282792aSdrh         i = appendAggInfo(pParse);
17702282792aSdrh         if( i<0 ) return 1;
17712282792aSdrh         pParse->aAgg[i].isAgg = 0;
17722282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
17732282792aSdrh       }
1774aaf88729Sdrh       pExpr->iAgg = i;
17752282792aSdrh       break;
17762282792aSdrh     }
17772282792aSdrh     case TK_AGG_FUNCTION: {
17782282792aSdrh       aAgg = pParse->aAgg;
17792282792aSdrh       for(i=0; i<pParse->nAgg; i++){
17802282792aSdrh         if( !aAgg[i].isAgg ) continue;
17814adee20fSdanielk1977         if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
17822282792aSdrh           break;
17832282792aSdrh         }
17842282792aSdrh       }
17852282792aSdrh       if( i>=pParse->nAgg ){
1786d8123366Sdanielk1977         u8 enc = pParse->db->enc;
17872282792aSdrh         i = appendAggInfo(pParse);
17882282792aSdrh         if( i<0 ) return 1;
17892282792aSdrh         pParse->aAgg[i].isAgg = 1;
17902282792aSdrh         pParse->aAgg[i].pExpr = pExpr;
17914adee20fSdanielk1977         pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
17926977fea8Sdrh              pExpr->token.z, pExpr->token.n,
1793d8123366Sdanielk1977              pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
17942282792aSdrh       }
17952282792aSdrh       pExpr->iAgg = i;
17962282792aSdrh       break;
17972282792aSdrh     }
17982282792aSdrh     default: {
17992282792aSdrh       if( pExpr->pLeft ){
18004adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
18012282792aSdrh       }
18022282792aSdrh       if( nErr==0 && pExpr->pRight ){
18034adee20fSdanielk1977         nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
18042282792aSdrh       }
18052282792aSdrh       if( nErr==0 && pExpr->pList ){
18062282792aSdrh         int n = pExpr->pList->nExpr;
18072282792aSdrh         int i;
18082282792aSdrh         for(i=0; nErr==0 && i<n; i++){
18094adee20fSdanielk1977           nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
18102282792aSdrh         }
18112282792aSdrh       }
18122282792aSdrh       break;
18132282792aSdrh     }
18142282792aSdrh   }
18152282792aSdrh   return nErr;
18162282792aSdrh }
18178e0a2f90Sdrh 
18188e0a2f90Sdrh /*
1819d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag
1820d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
1821d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return
1822d02eb1fdSdanielk1977 ** NULL if the function does not exist.
18238e0a2f90Sdrh **
18240bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef
18258e0a2f90Sdrh ** structure is created and liked into the "db" structure if a
18268e0a2f90Sdrh ** no matching function previously existed.  When createFlag is true
18278e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts
18288e0a2f90Sdrh ** any number of arguments will be returned.
18298e0a2f90Sdrh **
18308e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid
18318e0a2f90Sdrh ** function found is returned.  A function is valid if either xFunc
18328e0a2f90Sdrh ** or xStep is non-zero.
1833d02eb1fdSdanielk1977 **
1834d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and
1835d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not
1836d02eb1fdSdanielk1977 ** match that requested.
18378e0a2f90Sdrh */
18384adee20fSdanielk1977 FuncDef *sqlite3FindFunction(
18399bb575fdSdrh   sqlite3 *db,       /* An open database */
18408e0a2f90Sdrh   const char *zName, /* Name of the function.  Not null-terminated */
18418e0a2f90Sdrh   int nName,         /* Number of characters in the name */
18428e0a2f90Sdrh   int nArg,          /* Number of arguments.  -1 means any number */
1843d8123366Sdanielk1977   u8 enc,            /* Preferred text encoding */
18448e0a2f90Sdrh   int createFlag     /* Create new entry if true and does not otherwise exist */
18458e0a2f90Sdrh ){
1846d02eb1fdSdanielk1977   FuncDef *p;         /* Iterator variable */
1847d02eb1fdSdanielk1977   FuncDef *pFirst;    /* First function with this name */
1848d02eb1fdSdanielk1977   FuncDef *pBest = 0; /* Best match found so far */
1849d8123366Sdanielk1977   int bestmatch = 0;
1850d02eb1fdSdanielk1977 
1851d8123366Sdanielk1977 
1852d8123366Sdanielk1977   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
1853d02eb1fdSdanielk1977   if( nArg<-1 ) nArg = -1;
1854d02eb1fdSdanielk1977 
1855d02eb1fdSdanielk1977   pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1856d02eb1fdSdanielk1977   for(p=pFirst; p; p=p->pNext){
1857d8123366Sdanielk1977     /* During the search for the best function definition, bestmatch is set
1858d8123366Sdanielk1977     ** as follows to indicate the quality of the match with the definition
1859d8123366Sdanielk1977     ** pointed to by pBest:
1860d8123366Sdanielk1977     **
1861d8123366Sdanielk1977     ** 0: pBest is NULL. No match has been found.
1862d8123366Sdanielk1977     ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1863d8123366Sdanielk1977     **    encoding is requested, or vice versa.
1864d8123366Sdanielk1977     ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1865d8123366Sdanielk1977     **    requested, or vice versa.
1866d8123366Sdanielk1977     ** 3: A variable arguments function using the same text encoding.
1867d8123366Sdanielk1977     ** 4: A function with the exact number of arguments requested that
1868d8123366Sdanielk1977     **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1869d8123366Sdanielk1977     ** 5: A function with the exact number of arguments requested that
1870d8123366Sdanielk1977     **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1871d8123366Sdanielk1977     ** 6: An exact match.
1872d8123366Sdanielk1977     **
1873d8123366Sdanielk1977     ** A larger value of 'matchqual' indicates a more desirable match.
1874d8123366Sdanielk1977     */
1875e12c17baSdanielk1977     if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
1876d8123366Sdanielk1977       int match = 1;          /* Quality of this match */
1877d8123366Sdanielk1977       if( p->nArg==nArg || nArg==-1 ){
1878d8123366Sdanielk1977         match = 4;
18798e0a2f90Sdrh       }
1880d8123366Sdanielk1977       if( enc==p->iPrefEnc ){
1881d8123366Sdanielk1977         match += 2;
18828e0a2f90Sdrh       }
1883d8123366Sdanielk1977       else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1884d8123366Sdanielk1977                (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1885d8123366Sdanielk1977         match += 1;
1886d02eb1fdSdanielk1977       }
1887d8123366Sdanielk1977 
1888d8123366Sdanielk1977       if( match>bestmatch ){
1889d02eb1fdSdanielk1977         pBest = p;
1890d8123366Sdanielk1977         bestmatch = match;
1891d02eb1fdSdanielk1977       }
1892d02eb1fdSdanielk1977     }
1893d02eb1fdSdanielk1977   }
1894d02eb1fdSdanielk1977 
1895d8123366Sdanielk1977   /* If the createFlag parameter is true, and the seach did not reveal an
1896d8123366Sdanielk1977   ** exact match for the name, number of arguments and encoding, then add a
1897d8123366Sdanielk1977   ** new entry to the hash table and return it.
1898d8123366Sdanielk1977   */
1899d8123366Sdanielk1977   if( createFlag && bestmatch<6 &&
1900d02eb1fdSdanielk1977       (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1901d02eb1fdSdanielk1977     pBest->nArg = nArg;
1902d02eb1fdSdanielk1977     pBest->pNext = pFirst;
1903d02eb1fdSdanielk1977     pBest->zName = (char*)&pBest[1];
1904d8123366Sdanielk1977     pBest->iPrefEnc = enc;
1905d02eb1fdSdanielk1977     memcpy(pBest->zName, zName, nName);
1906d02eb1fdSdanielk1977     pBest->zName[nName] = 0;
1907d02eb1fdSdanielk1977     sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
1908d02eb1fdSdanielk1977   }
1909d02eb1fdSdanielk1977 
1910d02eb1fdSdanielk1977   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1911d02eb1fdSdanielk1977     return pBest;
1912d02eb1fdSdanielk1977   }
19138e0a2f90Sdrh   return 0;
19148e0a2f90Sdrh }
1915