blob: 5cfd280c7a0037ecf1a5005c61f79ea75aff3dc5 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
drh1ccde152000-06-17 13:12:39 +000012** This file contains routines used for analyzing expressions and
drhb19a2bc2001-09-16 00:13:26 +000013** for generating VDBE code that evaluates expressions in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
drh873fac02005-06-06 17:11:46 +000015** $Id: expr.c,v 1.204 2005/06/06 17:11:46 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh626a8792005-01-17 22:08:19 +000065** pExpr is an operand of a comparison operator. aff2 is the
66** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000067** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000186 /* When malloc fails, delete pLeft and pRight. Expressions passed to
187 ** this function must always be allocated with sqlite3Expr() for this
188 ** reason.
189 */
190 sqlite3ExprDelete(pLeft);
191 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000192 return 0;
193 }
194 pNew->op = op;
195 pNew->pLeft = pLeft;
196 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000197 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000198 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000199 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000200 pNew->span = pNew->token = *pToken;
201 }else if( pLeft && pRight ){
202 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000203 }
drha76b5df2002-02-23 02:32:10 +0000204 return pNew;
205}
206
207/*
drh4e0cff62004-11-05 05:10:28 +0000208** When doing a nested parse, you can include terms in an expression
209** that look like this: #0 #1 #2 ... These terms refer to elements
210** on the stack. "#0" (or just "#") means the top of the stack.
drh2958a4e2004-11-12 03:56:15 +0000211** "#1" means the next down on the stack. And so forth. #-1 means
212** memory location 0. #-2 means memory location 1. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000213**
214** This routine is called by the parser to deal with on of those terms.
215** It immediately generates code to store the value in a memory location.
216** The returns an expression that will code to extract the value from
217** that memory location as needed.
218*/
219Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
220 Vdbe *v = pParse->pVdbe;
221 Expr *p;
222 int depth;
223 if( v==0 ) return 0;
224 if( pParse->nested==0 ){
225 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
226 return 0;
227 }
228 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000229 if( p==0 ){
230 return 0; /* Malloc failed */
231 }
drh4e0cff62004-11-05 05:10:28 +0000232 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000233 if( depth>=0 ){
234 p->iTable = pParse->nMem++;
235 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
236 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
237 }else{
238 p->iTable = -1-depth;
239 }
drh4e0cff62004-11-05 05:10:28 +0000240 return p;
241}
242
243/*
drh91bb0ee2004-09-01 03:06:34 +0000244** Join two expressions using an AND operator. If either expression is
245** NULL, then just return the other expression.
246*/
247Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
248 if( pLeft==0 ){
249 return pRight;
250 }else if( pRight==0 ){
251 return pLeft;
252 }else{
253 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
254 }
255}
256
257/*
drh6977fea2002-10-22 23:38:04 +0000258** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000259** text between the two given tokens.
260*/
danielk19774adee202004-05-08 08:23:19 +0000261void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000262 assert( pRight!=0 );
263 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000264 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000265 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000266 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000267 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000268 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000269 }else{
drh6977fea2002-10-22 23:38:04 +0000270 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000271 }
drha76b5df2002-02-23 02:32:10 +0000272 }
273}
274
275/*
276** Construct a new expression node for a function with multiple
277** arguments.
278*/
danielk19774adee202004-05-08 08:23:19 +0000279Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000280 Expr *pNew;
281 pNew = sqliteMalloc( sizeof(Expr) );
282 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000283 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000284 return 0;
285 }
286 pNew->op = TK_FUNCTION;
287 pNew->pList = pList;
288 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000289 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000290 pNew->token = *pToken;
291 }else{
292 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000293 }
drh6977fea2002-10-22 23:38:04 +0000294 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000295 return pNew;
296}
297
298/*
drhfa6bc002004-09-07 16:19:52 +0000299** Assign a variable number to an expression that encodes a wildcard
300** in the original SQL statement.
301**
302** Wildcards consisting of a single "?" are assigned the next sequential
303** variable number.
304**
305** Wildcards of the form "?nnn" are assigned the number "nnn". We make
306** sure "nnn" is not too be to avoid a denial of service attack when
307** the SQL statement comes from an external source.
308**
309** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
310** as the previous instance of the same wildcard. Or if this is the first
311** instance of the wildcard, the next sequenial variable number is
312** assigned.
313*/
314void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
315 Token *pToken;
316 if( pExpr==0 ) return;
317 pToken = &pExpr->token;
318 assert( pToken->n>=1 );
319 assert( pToken->z!=0 );
320 assert( pToken->z[0]!=0 );
321 if( pToken->n==1 ){
322 /* Wildcard of the form "?". Assign the next variable number */
323 pExpr->iTable = ++pParse->nVar;
324 }else if( pToken->z[0]=='?' ){
325 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
326 ** use it as the variable number */
327 int i;
328 pExpr->iTable = i = atoi(&pToken->z[1]);
329 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
330 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
331 SQLITE_MAX_VARIABLE_NUMBER);
332 }
333 if( i>pParse->nVar ){
334 pParse->nVar = i;
335 }
336 }else{
337 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
338 ** number as the prior appearance of the same name, or if the name
339 ** has never appeared before, reuse the same variable number
340 */
341 int i, n;
342 n = pToken->n;
343 for(i=0; i<pParse->nVarExpr; i++){
344 Expr *pE;
345 if( (pE = pParse->apVarExpr[i])!=0
346 && pE->token.n==n
347 && memcmp(pE->token.z, pToken->z, n)==0 ){
348 pExpr->iTable = pE->iTable;
349 break;
350 }
351 }
352 if( i>=pParse->nVarExpr ){
353 pExpr->iTable = ++pParse->nVar;
354 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
355 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
356 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
357 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
358 }
359 if( !sqlite3_malloc_failed ){
360 assert( pParse->apVarExpr!=0 );
361 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
362 }
363 }
364 }
365}
366
367/*
drha2e00042002-01-22 03:13:42 +0000368** Recursively delete an expression tree.
369*/
danielk19774adee202004-05-08 08:23:19 +0000370void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000371 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000372 if( p->span.dyn ) sqliteFree((char*)p->span.z);
373 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3ExprDelete(p->pLeft);
375 sqlite3ExprDelete(p->pRight);
376 sqlite3ExprListDelete(p->pList);
377 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000378 sqliteFree(p);
379}
380
drha76b5df2002-02-23 02:32:10 +0000381
382/*
drhff78bd22002-02-27 01:47:11 +0000383** The following group of routines make deep copies of expressions,
384** expression lists, ID lists, and select statements. The copies can
385** be deleted (by being passed to their respective ...Delete() routines)
386** without effecting the originals.
387**
danielk19774adee202004-05-08 08:23:19 +0000388** The expression list, ID, and source lists return by sqlite3ExprListDup(),
389** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000390** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000391**
drhad3cab52002-05-24 02:04:32 +0000392** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000393*/
danielk19774adee202004-05-08 08:23:19 +0000394Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000395 Expr *pNew;
396 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000397 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000398 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000399 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000400 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000401 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000402 pNew->token.dyn = 1;
403 }else{
drh4efc4752004-01-16 15:55:37 +0000404 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000405 }
drh6977fea2002-10-22 23:38:04 +0000406 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000407 pNew->pLeft = sqlite3ExprDup(p->pLeft);
408 pNew->pRight = sqlite3ExprDup(p->pRight);
409 pNew->pList = sqlite3ExprListDup(p->pList);
410 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000411 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000412 return pNew;
413}
danielk19774adee202004-05-08 08:23:19 +0000414void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000415 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000416 if( pFrom->z ){
417 pTo->n = pFrom->n;
418 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
419 pTo->dyn = 1;
420 }else{
drh4b59ab52002-08-24 18:24:51 +0000421 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000422 }
423}
danielk19774adee202004-05-08 08:23:19 +0000424ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000425 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000426 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000427 int i;
428 if( p==0 ) return 0;
429 pNew = sqliteMalloc( sizeof(*pNew) );
430 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000431 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000432 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000433 if( pItem==0 ){
434 sqliteFree(pNew);
435 return 0;
436 }
drh145716b2004-09-24 12:24:06 +0000437 pOldItem = p->a;
438 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000439 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000440 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000441 if( pOldExpr->span.z!=0 && pNewExpr ){
442 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000443 ** expression list. The logic in SELECT processing that determines
444 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000446 }
drh1f3e9052002-10-31 00:09:39 +0000447 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000448 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000449 pItem->zName = sqliteStrDup(pOldItem->zName);
450 pItem->sortOrder = pOldItem->sortOrder;
451 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000452 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000453 }
454 return pNew;
455}
danielk197793758c82005-01-21 08:13:14 +0000456
457/*
458** If cursors, triggers, views and subqueries are all omitted from
459** the build, then none of the following routines, except for
460** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
461** called with a NULL argument.
462*/
danielk19776a67fe82005-02-04 04:07:16 +0000463#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
464 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000465SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000466 SrcList *pNew;
467 int i;
drh113088e2003-03-20 01:16:58 +0000468 int nByte;
drhad3cab52002-05-24 02:04:32 +0000469 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000470 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000471 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000472 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000473 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000474 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000475 struct SrcList_item *pNewItem = &pNew->a[i];
476 struct SrcList_item *pOldItem = &p->a[i];
477 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
478 pNewItem->zName = sqliteStrDup(pOldItem->zName);
479 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
480 pNewItem->jointype = pOldItem->jointype;
481 pNewItem->iCursor = pOldItem->iCursor;
danielk1977a1cb1832005-02-12 08:59:55 +0000482 pNewItem->pTab = pOldItem->pTab;
483 if( pNewItem->pTab ){
484 pNewItem->pTab->isTransient = 0;
485 }
danielk19774adee202004-05-08 08:23:19 +0000486 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
487 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
488 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000489 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000490 }
491 return pNew;
492}
danielk19774adee202004-05-08 08:23:19 +0000493IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000494 IdList *pNew;
495 int i;
496 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000497 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000498 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000499 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000500 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000501 if( pNew->a==0 ){
502 sqliteFree(pNew);
503 return 0;
504 }
drhff78bd22002-02-27 01:47:11 +0000505 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000506 struct IdList_item *pNewItem = &pNew->a[i];
507 struct IdList_item *pOldItem = &p->a[i];
508 pNewItem->zName = sqliteStrDup(pOldItem->zName);
509 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000510 }
511 return pNew;
512}
danielk19774adee202004-05-08 08:23:19 +0000513Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000514 Select *pNew;
515 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000516 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000517 if( pNew==0 ) return 0;
518 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000519 pNew->pEList = sqlite3ExprListDup(p->pEList);
520 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
521 pNew->pWhere = sqlite3ExprDup(p->pWhere);
522 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
523 pNew->pHaving = sqlite3ExprDup(p->pHaving);
524 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000525 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000526 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000527 pNew->pLimit = sqlite3ExprDup(p->pLimit);
528 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000529 pNew->iLimit = -1;
530 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000531 pNew->ppOpenTemp = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000532 pNew->isResolved = p->isResolved;
533 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000534 return pNew;
535}
danielk197793758c82005-01-21 08:13:14 +0000536#else
537Select *sqlite3SelectDup(Select *p){
538 assert( p==0 );
539 return 0;
540}
541#endif
drhff78bd22002-02-27 01:47:11 +0000542
543
544/*
drha76b5df2002-02-23 02:32:10 +0000545** Add a new element to the end of an expression list. If pList is
546** initially NULL, then create a new expression list.
547*/
danielk19774adee202004-05-08 08:23:19 +0000548ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000549 if( pList==0 ){
550 pList = sqliteMalloc( sizeof(ExprList) );
551 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000552 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000553 }
drh4efc4752004-01-16 15:55:37 +0000554 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000555 }
drh4305d102003-07-30 12:34:12 +0000556 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000557 struct ExprList_item *a;
558 int n = pList->nAlloc*2 + 4;
559 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
560 if( a==0 ){
561 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000562 }
danielk1977d5d56522005-03-16 12:15:20 +0000563 pList->a = a;
564 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000565 }
drh4efc4752004-01-16 15:55:37 +0000566 assert( pList->a!=0 );
567 if( pExpr || pName ){
568 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
569 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000570 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000571 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000572 }
573 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000574
575no_mem:
576 /* Avoid leaking memory if malloc has failed. */
577 sqlite3ExprDelete(pExpr);
578 sqlite3ExprListDelete(pList);
579 return 0;
drha76b5df2002-02-23 02:32:10 +0000580}
581
582/*
583** Delete an entire expression list.
584*/
danielk19774adee202004-05-08 08:23:19 +0000585void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000586 int i;
drhbe5c89a2004-07-26 00:31:09 +0000587 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000588 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000589 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
590 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000591 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
592 sqlite3ExprDelete(pItem->pExpr);
593 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000594 }
595 sqliteFree(pList->a);
596 sqliteFree(pList);
597}
598
599/*
drh626a8792005-01-17 22:08:19 +0000600** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000601**
drh626a8792005-01-17 22:08:19 +0000602** The return value from xFunc determines whether the tree walk continues.
603** 0 means continue walking the tree. 1 means do not walk children
604** of the current node but continue with siblings. 2 means abandon
605** the tree walk completely.
606**
607** The return value from this routine is 1 to abandon the tree walk
608** and 0 to continue.
609*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000610static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000611static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000612 int rc;
613 if( pExpr==0 ) return 0;
614 rc = (*xFunc)(pArg, pExpr);
615 if( rc==0 ){
616 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
617 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000618 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000619 }
620 return rc>1;
621}
622
623/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000624** Call walkExprTree() for every expression in list p.
625*/
626static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
627 int i;
628 struct ExprList_item *pItem;
629 if( !p ) return 0;
630 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
631 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
632 }
633 return 0;
634}
635
636/*
637** Call walkExprTree() for every expression in Select p, not including
638** expressions that are part of sub-selects in any FROM clause or the LIMIT
639** or OFFSET expressions..
640*/
641static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
642 walkExprList(p->pEList, xFunc, pArg);
643 walkExprTree(p->pWhere, xFunc, pArg);
644 walkExprList(p->pGroupBy, xFunc, pArg);
645 walkExprTree(p->pHaving, xFunc, pArg);
646 walkExprList(p->pOrderBy, xFunc, pArg);
647 return 0;
648}
649
650
651/*
drh626a8792005-01-17 22:08:19 +0000652** This routine is designed as an xFunc for walkExprTree().
653**
654** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000655** at pExpr that the expression that contains pExpr is not a constant
656** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
657** If pExpr does does not disqualify the expression from being a constant
658** then do nothing.
659**
660** After walking the whole tree, if no nodes are found that disqualify
661** the expression as constant, then we assume the whole expression
662** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000663*/
664static int exprNodeIsConstant(void *pArg, Expr *pExpr){
665 switch( pExpr->op ){
666 case TK_ID:
667 case TK_COLUMN:
668 case TK_DOT:
669 case TK_AGG_FUNCTION:
670 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000671#ifndef SQLITE_OMIT_SUBQUERY
672 case TK_SELECT:
673 case TK_EXISTS:
674#endif
drh626a8792005-01-17 22:08:19 +0000675 *((int*)pArg) = 0;
676 return 2;
677 default:
678 return 0;
679 }
680}
681
682/*
drhfef52082000-06-06 01:50:43 +0000683** Walk an expression tree. Return 1 if the expression is constant
684** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000685**
686** For the purposes of this function, a double-quoted string (ex: "abc")
687** is considered a variable but a single-quoted string (ex: 'abc') is
688** a constant.
drhfef52082000-06-06 01:50:43 +0000689*/
danielk19774adee202004-05-08 08:23:19 +0000690int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000691 int isConst = 1;
692 walkExprTree(p, exprNodeIsConstant, &isConst);
693 return isConst;
drhfef52082000-06-06 01:50:43 +0000694}
695
696/*
drh73b211a2005-01-18 04:00:42 +0000697** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000698** to fit in a 32-bit integer, return 1 and put the value of the integer
699** in *pValue. If the expression is not an integer or if it is too big
700** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000701*/
danielk19774adee202004-05-08 08:23:19 +0000702int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000703 switch( p->op ){
704 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000705 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000706 return 1;
707 }
708 break;
drhe4de1fe2002-06-02 16:09:01 +0000709 }
drh4b59ab52002-08-24 18:24:51 +0000710 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000711 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000712 }
drhe4de1fe2002-06-02 16:09:01 +0000713 case TK_UMINUS: {
714 int v;
danielk19774adee202004-05-08 08:23:19 +0000715 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000716 *pValue = -v;
717 return 1;
718 }
719 break;
720 }
721 default: break;
722 }
723 return 0;
724}
725
726/*
drhc4a3c772001-04-04 11:48:57 +0000727** Return TRUE if the given string is a row-id column name.
728*/
danielk19774adee202004-05-08 08:23:19 +0000729int sqlite3IsRowid(const char *z){
730 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
731 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
732 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000733 return 0;
734}
735
736/*
drh8141f612004-01-25 22:44:58 +0000737** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
738** that name in the set of source tables in pSrcList and make the pExpr
739** expression node refer back to that source column. The following changes
740** are made to pExpr:
741**
742** pExpr->iDb Set the index in db->aDb[] of the database holding
743** the table.
744** pExpr->iTable Set to the cursor number for the table obtained
745** from pSrcList.
746** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000747** pExpr->op Set to TK_COLUMN.
748** pExpr->pLeft Any expression this points to is deleted
749** pExpr->pRight Any expression this points to is deleted.
750**
751** The pDbToken is the name of the database (the "X"). This value may be
752** NULL meaning that name is of the form Y.Z or Z. Any available database
753** can be used. The pTableToken is the name of the table (the "Y"). This
754** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
755** means that the form of the name is Z and that columns from any table
756** can be used.
757**
758** If the name cannot be resolved unambiguously, leave an error message
759** in pParse and return non-zero. Return zero on success.
760*/
761static int lookupName(
762 Parse *pParse, /* The parsing context */
763 Token *pDbToken, /* Name of the database containing table, or NULL */
764 Token *pTableToken, /* Name of table containing column, or NULL */
765 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000766 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000767 Expr *pExpr /* Make this EXPR node point to the selected column */
768){
769 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
770 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
771 char *zCol = 0; /* Name of the column. The "Z" */
772 int i, j; /* Loop counters */
773 int cnt = 0; /* Number of matching column names */
774 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000775 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000776 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
777 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000778 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000779
780 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000781 zDb = sqlite3NameFromToken(pDbToken);
782 zTab = sqlite3NameFromToken(pTableToken);
783 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000784 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000785 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000786 }
drh8141f612004-01-25 22:44:58 +0000787
788 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000789 while( pNC && cnt==0 ){
790 SrcList *pSrcList = pNC->pSrcList;
791 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000792
drh626a8792005-01-17 22:08:19 +0000793 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000794 if( pSrcList ){
795 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
796 Table *pTab = pItem->pTab;
797 Column *pCol;
798
799 if( pTab==0 ) continue;
800 assert( pTab->nCol>0 );
801 if( zTab ){
802 if( pItem->zAlias ){
803 char *zTabName = pItem->zAlias;
804 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
805 }else{
806 char *zTabName = pTab->zName;
807 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
808 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
809 continue;
810 }
drh626a8792005-01-17 22:08:19 +0000811 }
drh8141f612004-01-25 22:44:58 +0000812 }
danielk1977b3bce662005-01-29 08:32:43 +0000813 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000814 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000815 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000816 pMatch = pItem;
817 }
818 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
819 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000820 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000821 cnt++;
822 pExpr->iTable = pItem->iCursor;
823 pMatch = pItem;
824 pExpr->iDb = pTab->iDb;
825 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
826 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
827 pExpr->affinity = pTab->aCol[j].affinity;
828 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000829 if( pItem->jointype & JT_NATURAL ){
830 /* If this match occurred in the left table of a natural join,
831 ** then skip the right table to avoid a duplicate match */
832 pItem++;
833 i++;
834 }
drh873fac02005-06-06 17:11:46 +0000835 if( (pUsing = pItem->pUsing)!=0 ){
836 /* If this match occurs on a column that is in the USING clause
837 ** of a join, skip the search of the right table of the join
838 ** to avoid a duplicate match there. */
839 int k;
840 for(k=0; k<pUsing->nId; k++){
841 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
842 pItem++;
843 i++;
844 break;
845 }
846 }
847 }
danielk1977b3bce662005-01-29 08:32:43 +0000848 break;
849 }
drh8141f612004-01-25 22:44:58 +0000850 }
851 }
852 }
drh626a8792005-01-17 22:08:19 +0000853
854#ifndef SQLITE_OMIT_TRIGGER
855 /* If we have not already resolved the name, then maybe
856 ** it is a new.* or old.* trigger argument reference
857 */
858 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
859 TriggerStack *pTriggerStack = pParse->trigStack;
860 Table *pTab = 0;
861 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
862 pExpr->iTable = pTriggerStack->newIdx;
863 assert( pTriggerStack->pTab );
864 pTab = pTriggerStack->pTab;
865 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
866 pExpr->iTable = pTriggerStack->oldIdx;
867 assert( pTriggerStack->pTab );
868 pTab = pTriggerStack->pTab;
869 }
870
871 if( pTab ){
872 int j;
873 Column *pCol = pTab->aCol;
874
875 pExpr->iDb = pTab->iDb;
876 cntTab++;
877 for(j=0; j < pTab->nCol; j++, pCol++) {
878 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
879 cnt++;
880 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
881 pExpr->affinity = pTab->aCol[j].affinity;
882 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000883 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000884 break;
885 }
886 }
887 }
888 }
drhb7f91642004-10-31 02:22:47 +0000889#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000890
drh626a8792005-01-17 22:08:19 +0000891 /*
892 ** Perhaps the name is a reference to the ROWID
893 */
894 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
895 cnt = 1;
896 pExpr->iColumn = -1;
897 pExpr->affinity = SQLITE_AFF_INTEGER;
898 }
drh8141f612004-01-25 22:44:58 +0000899
drh626a8792005-01-17 22:08:19 +0000900 /*
901 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
902 ** might refer to an result-set alias. This happens, for example, when
903 ** we are resolving names in the WHERE clause of the following command:
904 **
905 ** SELECT a+b AS x FROM table WHERE x<10;
906 **
907 ** In cases like this, replace pExpr with a copy of the expression that
908 ** forms the result set entry ("a+b" in the example) and return immediately.
909 ** Note that the expression in the result set should have already been
910 ** resolved by the time the WHERE clause is resolved.
911 */
drh79d5f632005-01-18 17:20:10 +0000912 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000913 for(j=0; j<pEList->nExpr; j++){
914 char *zAs = pEList->a[j].zName;
915 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
916 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
917 pExpr->op = TK_AS;
918 pExpr->iColumn = j;
919 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000920 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000921 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000922 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000923 }
924 }
925 }
926
927 /* Advance to the next name context. The loop will exit when either
928 ** we have a match (cnt>0) or when we run out of name contexts.
929 */
930 if( cnt==0 ){
931 pNC = pNC->pNext;
932 }
drh8141f612004-01-25 22:44:58 +0000933 }
934
935 /*
936 ** If X and Y are NULL (in other words if only the column name Z is
937 ** supplied) and the value of Z is enclosed in double-quotes, then
938 ** Z is a string literal if it doesn't match any column names. In that
939 ** case, we need to return right away and not make any changes to
940 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000941 **
942 ** Because no reference was made to outer contexts, the pNC->nRef
943 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000944 */
945 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
946 sqliteFree(zCol);
947 return 0;
948 }
949
950 /*
951 ** cnt==0 means there was not match. cnt>1 means there were two or
952 ** more matches. Either way, we have an error.
953 */
954 if( cnt!=1 ){
955 char *z = 0;
956 char *zErr;
957 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
958 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000959 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000960 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000961 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000962 }else{
963 z = sqliteStrDup(zCol);
964 }
danielk19774adee202004-05-08 08:23:19 +0000965 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000966 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000967 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000968 }
969
drh51669862004-12-18 18:40:26 +0000970 /* If a column from a table in pSrcList is referenced, then record
971 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
972 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
973 ** column number is greater than the number of bits in the bitmask
974 ** then set the high-order bit of the bitmask.
975 */
976 if( pExpr->iColumn>=0 && pMatch!=0 ){
977 int n = pExpr->iColumn;
978 if( n>=sizeof(Bitmask)*8 ){
979 n = sizeof(Bitmask)*8-1;
980 }
981 assert( pMatch->iCursor==pExpr->iTable );
982 pMatch->colUsed |= 1<<n;
983 }
984
danielk1977d5d56522005-03-16 12:15:20 +0000985lookupname_end:
drh8141f612004-01-25 22:44:58 +0000986 /* Clean up and return
987 */
988 sqliteFree(zDb);
989 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +0000990 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000991 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000992 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000993 pExpr->pRight = 0;
994 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +0000995lookupname_end_2:
996 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +0000997 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000998 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000999 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001000 if( pMatch && !pMatch->pSelect ){
1001 pExpr->pTab = pMatch->pTab;
1002 }
drh15ccce12005-05-23 15:06:39 +00001003 /* Increment the nRef value on all name contexts from TopNC up to
1004 ** the point where the name matched. */
1005 for(;;){
1006 assert( pTopNC!=0 );
1007 pTopNC->nRef++;
1008 if( pTopNC==pNC ) break;
1009 pTopNC = pTopNC->pNext;
1010 }
1011 return 0;
1012 } else {
1013 return 1;
drh626a8792005-01-17 22:08:19 +00001014 }
drh8141f612004-01-25 22:44:58 +00001015}
1016
1017/*
drh626a8792005-01-17 22:08:19 +00001018** This routine is designed as an xFunc for walkExprTree().
1019**
drh73b211a2005-01-18 04:00:42 +00001020** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001021** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001022** the tree or 2 to abort the tree walk.
1023**
1024** This routine also does error checking and name resolution for
1025** function names. The operator for aggregate functions is changed
1026** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001027*/
1028static int nameResolverStep(void *pArg, Expr *pExpr){
1029 NameContext *pNC = (NameContext*)pArg;
1030 SrcList *pSrcList;
1031 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001032
danielk1977b3bce662005-01-29 08:32:43 +00001033 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001034 assert( pNC!=0 );
1035 pSrcList = pNC->pSrcList;
1036 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001037
drh626a8792005-01-17 22:08:19 +00001038 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1039 ExprSetProperty(pExpr, EP_Resolved);
1040#ifndef NDEBUG
1041 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001042 int i;
drh626a8792005-01-17 22:08:19 +00001043 for(i=0; i<pSrcList->nSrc; i++){
1044 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1045 }
1046 }
1047#endif
1048 switch( pExpr->op ){
1049 /* Double-quoted strings (ex: "abc") are used as identifiers if
1050 ** possible. Otherwise they remain as strings. Single-quoted
1051 ** strings (ex: 'abc') are always string literals.
1052 */
1053 case TK_STRING: {
1054 if( pExpr->token.z[0]=='\'' ) break;
1055 /* Fall thru into the TK_ID case if this is a double-quoted string */
1056 }
1057 /* A lone identifier is the name of a column.
1058 */
1059 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001060 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1061 return 1;
1062 }
1063
1064 /* A table name and column name: ID.ID
1065 ** Or a database, table and column: ID.ID.ID
1066 */
1067 case TK_DOT: {
1068 Token *pColumn;
1069 Token *pTable;
1070 Token *pDb;
1071 Expr *pRight;
1072
danielk1977b3bce662005-01-29 08:32:43 +00001073 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001074 pRight = pExpr->pRight;
1075 if( pRight->op==TK_ID ){
1076 pDb = 0;
1077 pTable = &pExpr->pLeft->token;
1078 pColumn = &pRight->token;
1079 }else{
1080 assert( pRight->op==TK_DOT );
1081 pDb = &pExpr->pLeft->token;
1082 pTable = &pRight->pLeft->token;
1083 pColumn = &pRight->pRight->token;
1084 }
1085 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1086 return 1;
1087 }
1088
1089 /* Resolve function names
1090 */
drhb71090f2005-05-23 17:26:51 +00001091 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001092 case TK_FUNCTION: {
1093 ExprList *pList = pExpr->pList; /* The argument list */
1094 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1095 int no_such_func = 0; /* True if no such function exists */
1096 int wrong_num_args = 0; /* True if wrong number of arguments */
1097 int is_agg = 0; /* True if is an aggregate function */
1098 int i;
1099 int nId; /* Number of characters in function name */
1100 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001101 FuncDef *pDef; /* Information about the function */
1102 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001103
drhb71090f2005-05-23 17:26:51 +00001104 zId = pExpr->token.z;
1105 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001106 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1107 if( pDef==0 ){
1108 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1109 if( pDef==0 ){
1110 no_such_func = 1;
1111 }else{
1112 wrong_num_args = 1;
1113 }
1114 }else{
1115 is_agg = pDef->xFunc==0;
1116 }
1117 if( is_agg && !pNC->allowAgg ){
1118 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1119 pNC->nErr++;
1120 is_agg = 0;
1121 }else if( no_such_func ){
1122 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1123 pNC->nErr++;
1124 }else if( wrong_num_args ){
1125 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1126 nId, zId);
1127 pNC->nErr++;
1128 }
1129 if( is_agg ){
1130 pExpr->op = TK_AGG_FUNCTION;
1131 pNC->hasAgg = 1;
1132 }
drh73b211a2005-01-18 04:00:42 +00001133 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001134 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001135 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001136 }
drh73b211a2005-01-18 04:00:42 +00001137 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001138 /* FIX ME: Compute pExpr->affinity based on the expected return
1139 ** type of the function
1140 */
1141 return is_agg;
1142 }
danielk1977b3bce662005-01-29 08:32:43 +00001143#ifndef SQLITE_OMIT_SUBQUERY
1144 case TK_SELECT:
1145 case TK_EXISTS:
1146#endif
1147 case TK_IN: {
1148 if( pExpr->pSelect ){
1149 int nRef = pNC->nRef;
1150 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1151 assert( pNC->nRef>=nRef );
1152 if( nRef!=pNC->nRef ){
1153 ExprSetProperty(pExpr, EP_VarSelect);
1154 }
1155 }
1156 }
drh626a8792005-01-17 22:08:19 +00001157 }
1158 return 0;
1159}
1160
1161/*
drhcce7d172000-05-31 15:34:51 +00001162** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001163** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001164** index to the table in the table list and a column offset. The
1165** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1166** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001167** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001168** VDBE cursor number for a cursor that is pointing into the referenced
1169** table. The Expr.iColumn value is changed to the index of the column
1170** of the referenced table. The Expr.iColumn value for the special
1171** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1172** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001173**
drh626a8792005-01-17 22:08:19 +00001174** Also resolve function names and check the functions for proper
1175** usage. Make sure all function names are recognized and all functions
1176** have the correct number of arguments. Leave an error message
1177** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1178**
drh73b211a2005-01-18 04:00:42 +00001179** If the expression contains aggregate functions then set the EP_Agg
1180** property on the expression.
drh626a8792005-01-17 22:08:19 +00001181*/
1182int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001183 NameContext *pNC, /* Namespace to resolve expressions in. */
1184 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001185){
drh73b211a2005-01-18 04:00:42 +00001186 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001187 walkExprTree(pExpr, nameResolverStep, pNC);
1188 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001189 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001190 }
1191 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001192}
1193
drh1398ad32005-01-19 23:24:50 +00001194/*
1195** A pointer instance of this structure is used to pass information
1196** through walkExprTree into codeSubqueryStep().
1197*/
1198typedef struct QueryCoder QueryCoder;
1199struct QueryCoder {
1200 Parse *pParse; /* The parsing context */
1201 NameContext *pNC; /* Namespace of first enclosing query */
1202};
1203
drh626a8792005-01-17 22:08:19 +00001204
1205/*
1206** Generate code for subqueries and IN operators.
1207**
drh73b211a2005-01-18 04:00:42 +00001208** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001209**
1210** expr IN (exprlist)
1211** and
1212** expr IN (SELECT ...)
1213**
1214** The first form is handled by creating a set holding the list
1215** of allowed values. The second form causes the SELECT to generate
1216** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001217*/
drh51522cd2005-01-20 13:36:19 +00001218#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001219void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1220 int label = 0; /* Address after sub-select code */
1221 Vdbe *v = sqlite3GetVdbe(pParse);
1222 if( v==0 ) return;
1223
1224 /* If this is not a variable (correlated) select, then execute
1225 ** it only once. Unless this is part of a trigger program. In
1226 ** that case re-execute every time (this could be optimized).
1227 */
1228 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1229 int mem = pParse->nMem++;
1230 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1231 label = sqlite3VdbeMakeLabel(v);
1232 sqlite3VdbeAddOp(v, OP_If, 0, label);
1233 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1234 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1235 }
1236
1237 if( pExpr->pSelect ){
1238 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1239 }
drh6a3ea0e2003-05-02 14:32:12 +00001240
drhcce7d172000-05-31 15:34:51 +00001241 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001242 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001243 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001244 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001245 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001246
danielk1977bf3b7212004-05-18 10:06:24 +00001247 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001248
1249 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1250 ** expression it is handled the same way. A temporary table is
1251 ** filled with single-field index keys representing the results
1252 ** from the SELECT or the <exprlist>.
1253 **
1254 ** If the 'x' expression is a column value, or the SELECT...
1255 ** statement returns a column value, then the affinity of that
1256 ** column is used to build the index keys. If both 'x' and the
1257 ** SELECT... statement are columns, then numeric affinity is used
1258 ** if either column has NUMERIC or INTEGER affinity. If neither
1259 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1260 ** is used.
1261 */
1262 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001263 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001264 memset(&keyInfo, 0, sizeof(keyInfo));
1265 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001266 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001267
drhfef52082000-06-06 01:50:43 +00001268 if( pExpr->pSelect ){
1269 /* Case 1: expr IN (SELECT ...)
1270 **
danielk1977e014a832004-05-17 10:48:57 +00001271 ** Generate code to write the results of the select into the temporary
1272 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001273 */
danielk1977e014a832004-05-17 10:48:57 +00001274 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001275 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001276 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001277 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001278 pEList = pExpr->pSelect->pEList;
1279 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001280 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001281 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001282 }
drhfef52082000-06-06 01:50:43 +00001283 }else if( pExpr->pList ){
1284 /* Case 2: expr IN (exprlist)
1285 **
danielk1977e014a832004-05-17 10:48:57 +00001286 ** For each expression, build an index key from the evaluation and
1287 ** store it in the temporary table. If <expr> is a column, then use
1288 ** that columns affinity when building index keys. If <expr> is not
1289 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001290 */
danielk1977e014a832004-05-17 10:48:57 +00001291 int i;
danielk1977e014a832004-05-17 10:48:57 +00001292 if( !affinity ){
1293 affinity = SQLITE_AFF_NUMERIC;
1294 }
danielk19770202b292004-06-09 09:55:16 +00001295 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001296
1297 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001298 for(i=0; i<pExpr->pList->nExpr; i++){
1299 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001300
1301 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001302 if( !sqlite3ExprIsConstant(pE2) ){
1303 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001304 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001305 return;
drh4794b982000-06-06 13:54:14 +00001306 }
danielk1977e014a832004-05-17 10:48:57 +00001307
1308 /* Evaluate the expression and insert it into the temp table */
1309 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001310 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001311 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001312 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001313 }
1314 }
danielk19770202b292004-06-09 09:55:16 +00001315 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001316 break;
drhfef52082000-06-06 01:50:43 +00001317 }
1318
drh51522cd2005-01-20 13:36:19 +00001319 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001320 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001321 /* This has to be a scalar SELECT. Generate code to put the
1322 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001323 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001324 */
drh51522cd2005-01-20 13:36:19 +00001325 int sop;
1326 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001327
drh967e8b72000-06-21 13:59:10 +00001328 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001329 pSel = pExpr->pSelect;
1330 if( pExpr->op==TK_SELECT ){
1331 sop = SRT_Mem;
1332 }else{
1333 static const Token one = { "1", 0, 1 };
1334 sop = SRT_Exists;
1335 sqlite3ExprListDelete(pSel->pEList);
1336 pSel->pEList = sqlite3ExprListAppend(0,
1337 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1338 }
danielk1977b3bce662005-01-29 08:32:43 +00001339 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1340 break;
drhcce7d172000-05-31 15:34:51 +00001341 }
1342 }
danielk1977b3bce662005-01-29 08:32:43 +00001343
1344 if( pExpr->pSelect ){
1345 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1346 }
1347 if( label<0 ){
1348 sqlite3VdbeResolveLabel(v, label);
1349 }
1350 return;
drhcce7d172000-05-31 15:34:51 +00001351}
drh51522cd2005-01-20 13:36:19 +00001352#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001353
drhcce7d172000-05-31 15:34:51 +00001354/*
drhfec19aa2004-05-19 20:41:03 +00001355** Generate an instruction that will put the integer describe by
1356** text z[0..n-1] on the stack.
1357*/
1358static void codeInteger(Vdbe *v, const char *z, int n){
1359 int i;
drh6fec0762004-05-30 01:38:43 +00001360 if( sqlite3GetInt32(z, &i) ){
1361 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1362 }else if( sqlite3FitsIn64Bits(z) ){
1363 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001364 }else{
1365 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1366 }
1367}
1368
1369/*
drhcce7d172000-05-31 15:34:51 +00001370** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001371** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001372**
1373** This code depends on the fact that certain token values (ex: TK_EQ)
1374** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1375** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1376** the make process cause these values to align. Assert()s in the code
1377** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001378*/
danielk19774adee202004-05-08 08:23:19 +00001379void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001380 Vdbe *v = pParse->pVdbe;
1381 int op;
danielk19777977a172004-11-09 12:44:37 +00001382 if( v==0 ) return;
1383 if( pExpr==0 ){
1384 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1385 return;
1386 }
drhf2bc0132004-10-04 13:19:23 +00001387 op = pExpr->op;
1388 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001389 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001390 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1391 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001392 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001393 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001394 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001395 }else{
danielk19774adee202004-05-08 08:23:19 +00001396 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001397 }
drhcce7d172000-05-31 15:34:51 +00001398 break;
1399 }
1400 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001401 codeInteger(v, pExpr->token.z, pExpr->token.n);
1402 break;
1403 }
1404 case TK_FLOAT:
1405 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001406 assert( TK_FLOAT==OP_Real );
1407 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001408 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001409 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001410 break;
1411 }
danielk19775338a5f2005-01-20 13:03:10 +00001412#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001413 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001414 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001415 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1416 sqlite3VdbeDequoteP3(v, -1);
1417 break;
1418 }
danielk19775338a5f2005-01-20 13:03:10 +00001419#endif
drhcce7d172000-05-31 15:34:51 +00001420 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001421 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001422 break;
1423 }
drh50457892003-09-06 01:10:47 +00001424 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001425 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001426 if( pExpr->token.n>1 ){
1427 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1428 }
drh50457892003-09-06 01:10:47 +00001429 break;
1430 }
drh4e0cff62004-11-05 05:10:28 +00001431 case TK_REGISTER: {
1432 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1433 break;
1434 }
drhc9b84a12002-06-20 11:36:48 +00001435 case TK_LT:
1436 case TK_LE:
1437 case TK_GT:
1438 case TK_GE:
1439 case TK_NE:
1440 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001441 assert( TK_LT==OP_Lt );
1442 assert( TK_LE==OP_Le );
1443 assert( TK_GT==OP_Gt );
1444 assert( TK_GE==OP_Ge );
1445 assert( TK_EQ==OP_Eq );
1446 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001447 sqlite3ExprCode(pParse, pExpr->pLeft);
1448 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001449 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001450 break;
drhc9b84a12002-06-20 11:36:48 +00001451 }
drhcce7d172000-05-31 15:34:51 +00001452 case TK_AND:
1453 case TK_OR:
1454 case TK_PLUS:
1455 case TK_STAR:
1456 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001457 case TK_REM:
1458 case TK_BITAND:
1459 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001460 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001461 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001462 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001463 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001464 assert( TK_AND==OP_And );
1465 assert( TK_OR==OP_Or );
1466 assert( TK_PLUS==OP_Add );
1467 assert( TK_MINUS==OP_Subtract );
1468 assert( TK_REM==OP_Remainder );
1469 assert( TK_BITAND==OP_BitAnd );
1470 assert( TK_BITOR==OP_BitOr );
1471 assert( TK_SLASH==OP_Divide );
1472 assert( TK_LSHIFT==OP_ShiftLeft );
1473 assert( TK_RSHIFT==OP_ShiftRight );
1474 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001475 sqlite3ExprCode(pParse, pExpr->pLeft);
1476 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001477 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001478 break;
1479 }
drhcce7d172000-05-31 15:34:51 +00001480 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001481 Expr *pLeft = pExpr->pLeft;
1482 assert( pLeft );
1483 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1484 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001485 char *z = sqliteMalloc( p->n + 2 );
1486 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001487 if( pLeft->op==TK_FLOAT ){
1488 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001489 }else{
drhfec19aa2004-05-19 20:41:03 +00001490 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001491 }
drh6e142f52000-06-08 13:36:40 +00001492 sqliteFree(z);
1493 break;
1494 }
drh1ccde152000-06-17 13:12:39 +00001495 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001496 }
drhbf4133c2001-10-13 02:59:08 +00001497 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001498 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001499 assert( TK_BITNOT==OP_BitNot );
1500 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001501 sqlite3ExprCode(pParse, pExpr->pLeft);
1502 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001503 break;
1504 }
1505 case TK_ISNULL:
1506 case TK_NOTNULL: {
1507 int dest;
drhf2bc0132004-10-04 13:19:23 +00001508 assert( TK_ISNULL==OP_IsNull );
1509 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001510 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1511 sqlite3ExprCode(pParse, pExpr->pLeft);
1512 dest = sqlite3VdbeCurrentAddr(v) + 2;
1513 sqlite3VdbeAddOp(v, op, 1, dest);
1514 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001515 break;
drhcce7d172000-05-31 15:34:51 +00001516 }
drh22827922000-06-06 17:27:05 +00001517 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001518 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001519 break;
1520 }
drhb71090f2005-05-23 17:26:51 +00001521 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001522 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001523 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001524 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001525 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001526 int nId;
1527 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001528 int p2 = 0;
1529 int i;
danielk1977d8123362004-06-12 09:25:12 +00001530 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001531 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001532 zId = pExpr->token.z;
1533 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001534 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001535 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001536 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001537 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001538 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1539 p2 |= (1<<i);
1540 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001541 if( pDef->needCollSeq && !pColl ){
1542 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1543 }
1544 }
1545 if( pDef->needCollSeq ){
1546 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001547 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001548 }
1549 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001550 break;
1551 }
drhfe2093d2005-01-20 22:48:47 +00001552#ifndef SQLITE_OMIT_SUBQUERY
1553 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001554 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001555 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001556 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001557 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001558 break;
1559 }
drhfef52082000-06-06 01:50:43 +00001560 case TK_IN: {
1561 int addr;
drh94a11212004-09-25 13:12:14 +00001562 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001563 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001564
1565 /* Figure out the affinity to use to create a key from the results
1566 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001567 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001568 */
drh94a11212004-09-25 13:12:14 +00001569 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001570
danielk19774adee202004-05-08 08:23:19 +00001571 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001572
1573 /* Code the <expr> from "<expr> IN (...)". The temporary table
1574 ** pExpr->iTable contains the values that make up the (...) set.
1575 */
danielk19774adee202004-05-08 08:23:19 +00001576 sqlite3ExprCode(pParse, pExpr->pLeft);
1577 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001578 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001579 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001580 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001581 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001582 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001583 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1584 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1585
drhfef52082000-06-06 01:50:43 +00001586 break;
1587 }
danielk197793758c82005-01-21 08:13:14 +00001588#endif
drhfef52082000-06-06 01:50:43 +00001589 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001590 Expr *pLeft = pExpr->pLeft;
1591 struct ExprList_item *pLItem = pExpr->pList->a;
1592 Expr *pRight = pLItem->pExpr;
1593 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001594 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001595 sqlite3ExprCode(pParse, pRight);
1596 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001598 pLItem++;
1599 pRight = pLItem->pExpr;
1600 sqlite3ExprCode(pParse, pRight);
1601 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001602 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001603 break;
1604 }
drh51e9a442004-01-16 16:42:53 +00001605 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001606 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001607 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001608 break;
1609 }
drh17a7f8d2002-03-24 13:13:27 +00001610 case TK_CASE: {
1611 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001612 int jumpInst;
1613 int addr;
1614 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001615 int i;
drhbe5c89a2004-07-26 00:31:09 +00001616 ExprList *pEList;
1617 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001618
1619 assert(pExpr->pList);
1620 assert((pExpr->pList->nExpr % 2) == 0);
1621 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001622 pEList = pExpr->pList;
1623 aListelem = pEList->a;
1624 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001625 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001626 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001627 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001628 }
drhf5905aa2002-05-26 20:54:33 +00001629 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001630 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001631 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001632 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001633 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1634 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001635 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001636 }else{
danielk19774adee202004-05-08 08:23:19 +00001637 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001638 }
drhbe5c89a2004-07-26 00:31:09 +00001639 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001640 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1641 addr = sqlite3VdbeCurrentAddr(v);
1642 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001643 }
drhf570f012002-05-31 15:51:25 +00001644 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001645 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001646 }
drh17a7f8d2002-03-24 13:13:27 +00001647 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001648 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001649 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001650 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001651 }
danielk19774adee202004-05-08 08:23:19 +00001652 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001653 break;
1654 }
danielk19775338a5f2005-01-20 13:03:10 +00001655#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001656 case TK_RAISE: {
1657 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001658 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001659 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001660 return;
1661 }
drhad6d9462004-09-19 02:15:24 +00001662 if( pExpr->iColumn!=OE_Ignore ){
1663 assert( pExpr->iColumn==OE_Rollback ||
1664 pExpr->iColumn == OE_Abort ||
1665 pExpr->iColumn == OE_Fail );
1666 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1667 pExpr->token.z, pExpr->token.n);
1668 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001669 } else {
drhad6d9462004-09-19 02:15:24 +00001670 assert( pExpr->iColumn == OE_Ignore );
1671 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1672 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1673 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001674 }
drh17a7f8d2002-03-24 13:13:27 +00001675 }
danielk19775338a5f2005-01-20 13:03:10 +00001676#endif
drh17a7f8d2002-03-24 13:13:27 +00001677 break;
drhcce7d172000-05-31 15:34:51 +00001678 }
drhcce7d172000-05-31 15:34:51 +00001679}
1680
danielk197793758c82005-01-21 08:13:14 +00001681#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001682/*
drh25303782004-12-07 15:41:48 +00001683** Generate code that evalutes the given expression and leaves the result
1684** on the stack. See also sqlite3ExprCode().
1685**
1686** This routine might also cache the result and modify the pExpr tree
1687** so that it will make use of the cached result on subsequent evaluations
1688** rather than evaluate the whole expression again. Trivial expressions are
1689** not cached. If the expression is cached, its result is stored in a
1690** memory location.
1691*/
1692void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1693 Vdbe *v = pParse->pVdbe;
1694 int iMem;
1695 int addr1, addr2;
1696 if( v==0 ) return;
1697 addr1 = sqlite3VdbeCurrentAddr(v);
1698 sqlite3ExprCode(pParse, pExpr);
1699 addr2 = sqlite3VdbeCurrentAddr(v);
1700 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1701 iMem = pExpr->iTable = pParse->nMem++;
1702 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1703 pExpr->op = TK_REGISTER;
1704 }
1705}
danielk197793758c82005-01-21 08:13:14 +00001706#endif
drh25303782004-12-07 15:41:48 +00001707
1708/*
drh268380c2004-02-25 13:47:31 +00001709** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001710** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001711**
1712** Return the number of elements pushed onto the stack.
1713*/
danielk19774adee202004-05-08 08:23:19 +00001714int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001715 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001716 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001717){
1718 struct ExprList_item *pItem;
1719 int i, n;
1720 Vdbe *v;
1721 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001722 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001723 n = pList->nExpr;
1724 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001725 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001726 }
drhf9b596e2004-05-26 16:54:42 +00001727 return n;
drh268380c2004-02-25 13:47:31 +00001728}
1729
1730/*
drhcce7d172000-05-31 15:34:51 +00001731** Generate code for a boolean expression such that a jump is made
1732** to the label "dest" if the expression is true but execution
1733** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001734**
1735** If the expression evaluates to NULL (neither true nor false), then
1736** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001737**
1738** This code depends on the fact that certain token values (ex: TK_EQ)
1739** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1740** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1741** the make process cause these values to align. Assert()s in the code
1742** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001743*/
danielk19774adee202004-05-08 08:23:19 +00001744void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001745 Vdbe *v = pParse->pVdbe;
1746 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001747 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001748 op = pExpr->op;
1749 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001750 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001751 int d2 = sqlite3VdbeMakeLabel(v);
1752 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1753 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1754 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001755 break;
1756 }
1757 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001758 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1759 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001760 break;
1761 }
1762 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001763 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001764 break;
1765 }
1766 case TK_LT:
1767 case TK_LE:
1768 case TK_GT:
1769 case TK_GE:
1770 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001771 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001772 assert( TK_LT==OP_Lt );
1773 assert( TK_LE==OP_Le );
1774 assert( TK_GT==OP_Gt );
1775 assert( TK_GE==OP_Ge );
1776 assert( TK_EQ==OP_Eq );
1777 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001778 sqlite3ExprCode(pParse, pExpr->pLeft);
1779 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001780 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001781 break;
1782 }
1783 case TK_ISNULL:
1784 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001785 assert( TK_ISNULL==OP_IsNull );
1786 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001787 sqlite3ExprCode(pParse, pExpr->pLeft);
1788 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001789 break;
1790 }
drhfef52082000-06-06 01:50:43 +00001791 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001792 /* The expression "x BETWEEN y AND z" is implemented as:
1793 **
1794 ** 1 IF (x < y) GOTO 3
1795 ** 2 IF (x <= z) GOTO <dest>
1796 ** 3 ...
1797 */
drhf5905aa2002-05-26 20:54:33 +00001798 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001799 Expr *pLeft = pExpr->pLeft;
1800 Expr *pRight = pExpr->pList->a[0].pExpr;
1801 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001802 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001803 sqlite3ExprCode(pParse, pRight);
1804 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001805
drhbe5c89a2004-07-26 00:31:09 +00001806 pRight = pExpr->pList->a[1].pExpr;
1807 sqlite3ExprCode(pParse, pRight);
1808 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001809
danielk19774adee202004-05-08 08:23:19 +00001810 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1811 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1812 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001813 break;
1814 }
drhcce7d172000-05-31 15:34:51 +00001815 default: {
danielk19774adee202004-05-08 08:23:19 +00001816 sqlite3ExprCode(pParse, pExpr);
1817 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001818 break;
1819 }
1820 }
1821}
1822
1823/*
drh66b89c82000-11-28 20:47:17 +00001824** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001825** to the label "dest" if the expression is false but execution
1826** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001827**
1828** If the expression evaluates to NULL (neither true nor false) then
1829** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001830*/
danielk19774adee202004-05-08 08:23:19 +00001831void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001832 Vdbe *v = pParse->pVdbe;
1833 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001834 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001835
1836 /* The value of pExpr->op and op are related as follows:
1837 **
1838 ** pExpr->op op
1839 ** --------- ----------
1840 ** TK_ISNULL OP_NotNull
1841 ** TK_NOTNULL OP_IsNull
1842 ** TK_NE OP_Eq
1843 ** TK_EQ OP_Ne
1844 ** TK_GT OP_Le
1845 ** TK_LE OP_Gt
1846 ** TK_GE OP_Lt
1847 ** TK_LT OP_Ge
1848 **
1849 ** For other values of pExpr->op, op is undefined and unused.
1850 ** The value of TK_ and OP_ constants are arranged such that we
1851 ** can compute the mapping above using the following expression.
1852 ** Assert()s verify that the computation is correct.
1853 */
1854 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1855
1856 /* Verify correct alignment of TK_ and OP_ constants
1857 */
1858 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1859 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1860 assert( pExpr->op!=TK_NE || op==OP_Eq );
1861 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1862 assert( pExpr->op!=TK_LT || op==OP_Ge );
1863 assert( pExpr->op!=TK_LE || op==OP_Gt );
1864 assert( pExpr->op!=TK_GT || op==OP_Le );
1865 assert( pExpr->op!=TK_GE || op==OP_Lt );
1866
drhcce7d172000-05-31 15:34:51 +00001867 switch( pExpr->op ){
1868 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001869 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1870 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001871 break;
1872 }
1873 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001874 int d2 = sqlite3VdbeMakeLabel(v);
1875 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1876 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1877 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001878 break;
1879 }
1880 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001881 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001882 break;
1883 }
1884 case TK_LT:
1885 case TK_LE:
1886 case TK_GT:
1887 case TK_GE:
1888 case TK_NE:
1889 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001890 sqlite3ExprCode(pParse, pExpr->pLeft);
1891 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001892 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001893 break;
1894 }
drhcce7d172000-05-31 15:34:51 +00001895 case TK_ISNULL:
1896 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001897 sqlite3ExprCode(pParse, pExpr->pLeft);
1898 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001899 break;
1900 }
drhfef52082000-06-06 01:50:43 +00001901 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001902 /* The expression is "x BETWEEN y AND z". It is implemented as:
1903 **
1904 ** 1 IF (x >= y) GOTO 3
1905 ** 2 GOTO <dest>
1906 ** 3 IF (x > z) GOTO <dest>
1907 */
drhfef52082000-06-06 01:50:43 +00001908 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001909 Expr *pLeft = pExpr->pLeft;
1910 Expr *pRight = pExpr->pList->a[0].pExpr;
1911 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001912 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001913 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001914 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001915 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1916
danielk19774adee202004-05-08 08:23:19 +00001917 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1918 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001919 pRight = pExpr->pList->a[1].pExpr;
1920 sqlite3ExprCode(pParse, pRight);
1921 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001922 break;
1923 }
drhcce7d172000-05-31 15:34:51 +00001924 default: {
danielk19774adee202004-05-08 08:23:19 +00001925 sqlite3ExprCode(pParse, pExpr);
1926 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001927 break;
1928 }
1929 }
1930}
drh22827922000-06-06 17:27:05 +00001931
1932/*
1933** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1934** if they are identical and return FALSE if they differ in any way.
1935*/
danielk19774adee202004-05-08 08:23:19 +00001936int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001937 int i;
1938 if( pA==0 ){
1939 return pB==0;
1940 }else if( pB==0 ){
1941 return 0;
1942 }
1943 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001944 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1945 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001946 if( pA->pList ){
1947 if( pB->pList==0 ) return 0;
1948 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1949 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001950 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001951 return 0;
1952 }
1953 }
1954 }else if( pB->pList ){
1955 return 0;
1956 }
1957 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001958 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001959 if( pA->token.z ){
1960 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001961 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001962 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001963 }
1964 return 1;
1965}
1966
1967/*
1968** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001969** The new element is initialized to zero. The calling function is
1970** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001971*/
1972static int appendAggInfo(Parse *pParse){
1973 if( (pParse->nAgg & 0x7)==0 ){
1974 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001975 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1976 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001977 return -1;
1978 }
drh6d4abfb2001-10-22 02:58:08 +00001979 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001980 }
1981 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1982 return pParse->nAgg++;
1983}
1984
1985/*
drh626a8792005-01-17 22:08:19 +00001986** This is an xFunc for walkExprTree() used to implement
1987** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1988** for additional information.
drh22827922000-06-06 17:27:05 +00001989**
drh626a8792005-01-17 22:08:19 +00001990** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001991*/
drh626a8792005-01-17 22:08:19 +00001992static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001993 int i;
1994 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00001995 NameContext *pNC = (NameContext *)pArg;
1996 Parse *pParse = pNC->pParse;
1997 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00001998
drh22827922000-06-06 17:27:05 +00001999 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002000 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002001 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2002 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2003 aAgg = pParse->aAgg;
2004 for(i=0; i<pParse->nAgg; i++){
2005 if( aAgg[i].isAgg ) continue;
2006 if( aAgg[i].pExpr->iTable==pExpr->iTable
2007 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2008 break;
2009 }
2010 }
2011 if( i>=pParse->nAgg ){
2012 i = appendAggInfo(pParse);
2013 if( i<0 ) return 1;
2014 pParse->aAgg[i].isAgg = 0;
2015 pParse->aAgg[i].pExpr = pExpr;
2016 }
2017 pExpr->iAgg = i;
2018 pExpr->iAggCtx = pNC->nDepth;
2019 return 1;
drh22827922000-06-06 17:27:05 +00002020 }
2021 }
drh626a8792005-01-17 22:08:19 +00002022 return 1;
drh22827922000-06-06 17:27:05 +00002023 }
2024 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002025 if( pNC->nDepth==0 ){
2026 aAgg = pParse->aAgg;
2027 for(i=0; i<pParse->nAgg; i++){
2028 if( !aAgg[i].isAgg ) continue;
2029 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2030 break;
2031 }
drh22827922000-06-06 17:27:05 +00002032 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002033 if( i>=pParse->nAgg ){
2034 u8 enc = pParse->db->enc;
2035 i = appendAggInfo(pParse);
2036 if( i<0 ) return 1;
2037 pParse->aAgg[i].isAgg = 1;
2038 pParse->aAgg[i].pExpr = pExpr;
2039 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2040 pExpr->token.z, pExpr->token.n,
2041 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2042 }
2043 pExpr->iAgg = i;
2044 return 1;
drh22827922000-06-06 17:27:05 +00002045 }
drh22827922000-06-06 17:27:05 +00002046 }
2047 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002048 if( pExpr->pSelect ){
2049 pNC->nDepth++;
2050 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2051 pNC->nDepth--;
2052 }
drh626a8792005-01-17 22:08:19 +00002053 return 0;
2054}
2055
2056/*
2057** Analyze the given expression looking for aggregate functions and
2058** for variables that need to be added to the pParse->aAgg[] array.
2059** Make additional entries to the pParse->aAgg[] array as necessary.
2060**
2061** This routine should only be called after the expression has been
2062** analyzed by sqlite3ExprResolveNames().
2063**
2064** If errors are seen, leave an error message in zErrMsg and return
2065** the number of errors.
2066*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002067int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2068 int nErr = pNC->pParse->nErr;
2069 walkExprTree(pExpr, analyzeAggregate, pNC);
2070 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002071}