blob: 27bb7b85a521b1b1234f7ba986f76b5d87123881 [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**
drha34001c2007-02-02 12:44:37 +000015** $Id: expr.c,v 1.274 2007/02/02 12:44:37 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){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
drh8b4c40d2007-02-01 23:02:45 +000053** Set the collating sequence for expression pExpr to be the collating
54** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000055** The collating sequence is marked as "explicit" using the EP_ExpCollate
56** flag. An explicit collating sequence will override implicit
57** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000058*/
59Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
60 CollSeq *pColl;
61 if( pExpr==0 ) return 0;
62 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
63 if( pColl ){
64 pExpr->pColl = pColl;
65 pExpr->flags |= EP_ExpCollate;
66 }
67 return pExpr;
68}
69
70/*
danielk19770202b292004-06-09 09:55:16 +000071** Return the default collation sequence for the expression pExpr. If
72** there is no default collation type, return 0.
73*/
danielk19777cedc8d2004-06-10 10:50:08 +000074CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
75 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000076 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000077 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000078 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000079 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000080 }
81 }
danielk19777cedc8d2004-06-10 10:50:08 +000082 if( sqlite3CheckCollSeq(pParse, pColl) ){
83 pColl = 0;
84 }
85 return pColl;
danielk19770202b292004-06-09 09:55:16 +000086}
87
88/*
drh626a8792005-01-17 22:08:19 +000089** pExpr is an operand of a comparison operator. aff2 is the
90** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000091** type affinity that should be used for the comparison operator.
92*/
danielk1977e014a832004-05-17 10:48:57 +000093char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000094 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000095 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000096 /* Both sides of the comparison are columns. If one has numeric
97 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh8a512562005-11-14 22:29:05 +000099 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000100 return SQLITE_AFF_NUMERIC;
101 }else{
102 return SQLITE_AFF_NONE;
103 }
104 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000105 /* Neither side of the comparison is a column. Compare the
106 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000107 */
drh5f6a87b2004-07-19 00:39:45 +0000108 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000109 }else{
110 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000111 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000112 return (aff1 + aff2);
113 }
114}
115
drh53db1452004-05-20 13:54:53 +0000116/*
117** pExpr is a comparison operator. Return the type affinity that should
118** be applied to both operands prior to doing the comparison.
119*/
danielk1977e014a832004-05-17 10:48:57 +0000120static char comparisonAffinity(Expr *pExpr){
121 char aff;
122 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
123 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
124 pExpr->op==TK_NE );
125 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000126 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000127 if( pExpr->pRight ){
128 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
129 }
130 else if( pExpr->pSelect ){
131 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
132 }
133 else if( !aff ){
134 aff = SQLITE_AFF_NUMERIC;
135 }
136 return aff;
137}
138
139/*
140** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
141** idx_affinity is the affinity of an indexed column. Return true
142** if the index with affinity idx_affinity may be used to implement
143** the comparison in pExpr.
144*/
145int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
146 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000147 switch( aff ){
148 case SQLITE_AFF_NONE:
149 return 1;
150 case SQLITE_AFF_TEXT:
151 return idx_affinity==SQLITE_AFF_TEXT;
152 default:
153 return sqlite3IsNumericAffinity(idx_affinity);
154 }
danielk1977e014a832004-05-17 10:48:57 +0000155}
156
danielk1977a37cdde2004-05-16 11:15:36 +0000157/*
158** Return the P1 value that should be used for a binary comparison
159** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
160** If jumpIfNull is true, then set the low byte of the returned
161** P1 value to tell the opcode to jump if either expression
162** evaluates to NULL.
163*/
danielk1977e014a832004-05-17 10:48:57 +0000164static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000165 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000166 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000167}
168
drha2e00042002-01-22 03:13:42 +0000169/*
danielk19770202b292004-06-09 09:55:16 +0000170** Return a pointer to the collation sequence that should be used by
171** a binary comparison operator comparing pLeft and pRight.
172**
173** If the left hand expression has a collating sequence type, then it is
174** used. Otherwise the collation sequence for the right hand expression
175** is used, or the default (BINARY) if neither expression has a collating
176** type.
177*/
danielk19777cedc8d2004-06-10 10:50:08 +0000178static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
179 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000180 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000181 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000182 }
183 return pColl;
184}
185
186/*
drhbe5c89a2004-07-26 00:31:09 +0000187** Generate code for a comparison operator.
188*/
189static int codeCompare(
190 Parse *pParse, /* The parsing (and code generating) context */
191 Expr *pLeft, /* The left operand */
192 Expr *pRight, /* The right operand */
193 int opcode, /* The comparison opcode */
194 int dest, /* Jump here if true. */
195 int jumpIfNull /* If true, jump if either operand is NULL */
196){
197 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
198 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000199 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000200}
201
202/*
drha76b5df2002-02-23 02:32:10 +0000203** Construct a new expression node and return a pointer to it. Memory
204** for this node is obtained from sqliteMalloc(). The calling function
205** is responsible for making sure the node eventually gets freed.
206*/
drhe4e72072004-11-23 01:47:30 +0000207Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000208 Expr *pNew;
209 pNew = sqliteMalloc( sizeof(Expr) );
210 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000211 /* When malloc fails, delete pLeft and pRight. Expressions passed to
212 ** this function must always be allocated with sqlite3Expr() for this
213 ** reason.
214 */
215 sqlite3ExprDelete(pLeft);
216 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000217 return 0;
218 }
219 pNew->op = op;
220 pNew->pLeft = pLeft;
221 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000222 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000223 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000224 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000225 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000226 }else if( pLeft ){
227 if( pRight ){
228 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
229 if( pRight->flags && EP_ExpCollate ){
230 pNew->flags |= EP_ExpCollate;
231 pNew->pColl = pRight->pColl;
232 }
233 }
234 if( pLeft->flags && EP_ExpCollate ){
235 pNew->flags |= EP_ExpCollate;
236 pNew->pColl = pLeft->pColl;
237 }
drha76b5df2002-02-23 02:32:10 +0000238 }
drha76b5df2002-02-23 02:32:10 +0000239 return pNew;
240}
241
242/*
drh206f3d92006-07-11 13:15:08 +0000243** Works like sqlite3Expr() but frees its pLeft and pRight arguments
244** if it fails due to a malloc problem.
245*/
246Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
247 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
248 if( pNew==0 ){
249 sqlite3ExprDelete(pLeft);
250 sqlite3ExprDelete(pRight);
251 }
252 return pNew;
253}
254
255/*
drh4e0cff62004-11-05 05:10:28 +0000256** When doing a nested parse, you can include terms in an expression
257** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000258** on the stack. "#0" means the top of the stack.
259** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000260**
261** This routine is called by the parser to deal with on of those terms.
262** It immediately generates code to store the value in a memory location.
263** The returns an expression that will code to extract the value from
264** that memory location as needed.
265*/
266Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
267 Vdbe *v = pParse->pVdbe;
268 Expr *p;
269 int depth;
drh4e0cff62004-11-05 05:10:28 +0000270 if( pParse->nested==0 ){
271 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
272 return 0;
273 }
drhbb7ac002005-08-12 22:58:53 +0000274 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000275 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000276 if( p==0 ){
277 return 0; /* Malloc failed */
278 }
drh2646da72005-12-09 20:02:05 +0000279 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000280 p->iTable = pParse->nMem++;
281 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
282 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000283 return p;
284}
285
286/*
drh91bb0ee2004-09-01 03:06:34 +0000287** Join two expressions using an AND operator. If either expression is
288** NULL, then just return the other expression.
289*/
290Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
291 if( pLeft==0 ){
292 return pRight;
293 }else if( pRight==0 ){
294 return pLeft;
295 }else{
296 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
297 }
298}
299
300/*
drh6977fea2002-10-22 23:38:04 +0000301** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000302** text between the two given tokens.
303*/
danielk19774adee202004-05-08 08:23:19 +0000304void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000305 assert( pRight!=0 );
306 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000307 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000308 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000309 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000310 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000311 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000312 }else{
drh6977fea2002-10-22 23:38:04 +0000313 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000314 }
drha76b5df2002-02-23 02:32:10 +0000315 }
316}
317
318/*
319** Construct a new expression node for a function with multiple
320** arguments.
321*/
danielk19774adee202004-05-08 08:23:19 +0000322Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000323 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000324 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000325 pNew = sqliteMalloc( sizeof(Expr) );
326 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000327 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000328 return 0;
329 }
330 pNew->op = TK_FUNCTION;
331 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000332 assert( pToken->dyn==0 );
333 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000334 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000335 return pNew;
336}
337
338/*
drhfa6bc002004-09-07 16:19:52 +0000339** Assign a variable number to an expression that encodes a wildcard
340** in the original SQL statement.
341**
342** Wildcards consisting of a single "?" are assigned the next sequential
343** variable number.
344**
345** Wildcards of the form "?nnn" are assigned the number "nnn". We make
346** sure "nnn" is not too be to avoid a denial of service attack when
347** the SQL statement comes from an external source.
348**
349** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
350** as the previous instance of the same wildcard. Or if this is the first
351** instance of the wildcard, the next sequenial variable number is
352** assigned.
353*/
354void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
355 Token *pToken;
356 if( pExpr==0 ) return;
357 pToken = &pExpr->token;
358 assert( pToken->n>=1 );
359 assert( pToken->z!=0 );
360 assert( pToken->z[0]!=0 );
361 if( pToken->n==1 ){
362 /* Wildcard of the form "?". Assign the next variable number */
363 pExpr->iTable = ++pParse->nVar;
364 }else if( pToken->z[0]=='?' ){
365 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
366 ** use it as the variable number */
367 int i;
drh2646da72005-12-09 20:02:05 +0000368 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000369 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
370 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
371 SQLITE_MAX_VARIABLE_NUMBER);
372 }
373 if( i>pParse->nVar ){
374 pParse->nVar = i;
375 }
376 }else{
377 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
378 ** number as the prior appearance of the same name, or if the name
379 ** has never appeared before, reuse the same variable number
380 */
381 int i, n;
382 n = pToken->n;
383 for(i=0; i<pParse->nVarExpr; i++){
384 Expr *pE;
385 if( (pE = pParse->apVarExpr[i])!=0
386 && pE->token.n==n
387 && memcmp(pE->token.z, pToken->z, n)==0 ){
388 pExpr->iTable = pE->iTable;
389 break;
390 }
391 }
392 if( i>=pParse->nVarExpr ){
393 pExpr->iTable = ++pParse->nVar;
394 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
395 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
danielk1977e7259292006-01-13 06:33:23 +0000396 sqliteReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000397 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
398 }
danielk19779e128002006-01-18 16:51:35 +0000399 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000400 assert( pParse->apVarExpr!=0 );
401 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
402 }
403 }
404 }
405}
406
407/*
drha2e00042002-01-22 03:13:42 +0000408** Recursively delete an expression tree.
409*/
danielk19774adee202004-05-08 08:23:19 +0000410void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000411 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000412 if( p->span.dyn ) sqliteFree((char*)p->span.z);
413 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000414 sqlite3ExprDelete(p->pLeft);
415 sqlite3ExprDelete(p->pRight);
416 sqlite3ExprListDelete(p->pList);
417 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000418 sqliteFree(p);
419}
420
drhd2687b72005-08-12 22:56:09 +0000421/*
422** The Expr.token field might be a string literal that is quoted.
423** If so, remove the quotation marks.
424*/
425void sqlite3DequoteExpr(Expr *p){
426 if( ExprHasAnyProperty(p, EP_Dequoted) ){
427 return;
428 }
429 ExprSetProperty(p, EP_Dequoted);
430 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000431 sqlite3TokenCopy(&p->token, &p->token);
432 }
433 sqlite3Dequote((char*)p->token.z);
434}
435
drha76b5df2002-02-23 02:32:10 +0000436
437/*
drhff78bd22002-02-27 01:47:11 +0000438** The following group of routines make deep copies of expressions,
439** expression lists, ID lists, and select statements. The copies can
440** be deleted (by being passed to their respective ...Delete() routines)
441** without effecting the originals.
442**
danielk19774adee202004-05-08 08:23:19 +0000443** The expression list, ID, and source lists return by sqlite3ExprListDup(),
444** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000445** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000446**
drhad3cab52002-05-24 02:04:32 +0000447** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000448*/
danielk19774adee202004-05-08 08:23:19 +0000449Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000450 Expr *pNew;
451 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000452 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000453 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000454 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000455 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000456 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000457 pNew->token.dyn = 1;
458 }else{
drh4efc4752004-01-16 15:55:37 +0000459 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000460 }
drh6977fea2002-10-22 23:38:04 +0000461 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000462 pNew->pLeft = sqlite3ExprDup(p->pLeft);
463 pNew->pRight = sqlite3ExprDup(p->pRight);
464 pNew->pList = sqlite3ExprListDup(p->pList);
465 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000466 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000467 return pNew;
468}
danielk19774adee202004-05-08 08:23:19 +0000469void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000470 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000471 if( pFrom->z ){
472 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000473 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000474 pTo->dyn = 1;
475 }else{
drh4b59ab52002-08-24 18:24:51 +0000476 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000477 }
478}
danielk19774adee202004-05-08 08:23:19 +0000479ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000480 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000481 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000482 int i;
483 if( p==0 ) return 0;
484 pNew = sqliteMalloc( sizeof(*pNew) );
485 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000486 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000487 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000488 if( pItem==0 ){
489 sqliteFree(pNew);
490 return 0;
491 }
drh145716b2004-09-24 12:24:06 +0000492 pOldItem = p->a;
493 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000494 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000495 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000496 if( pOldExpr->span.z!=0 && pNewExpr ){
497 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000498 ** expression list. The logic in SELECT processing that determines
499 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000500 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000501 }
drh1f3e9052002-10-31 00:09:39 +0000502 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000503 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000504 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000505 pItem->zName = sqliteStrDup(pOldItem->zName);
506 pItem->sortOrder = pOldItem->sortOrder;
507 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000508 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000509 }
510 return pNew;
511}
danielk197793758c82005-01-21 08:13:14 +0000512
513/*
514** If cursors, triggers, views and subqueries are all omitted from
515** the build, then none of the following routines, except for
516** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
517** called with a NULL argument.
518*/
danielk19776a67fe82005-02-04 04:07:16 +0000519#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
520 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000521SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000522 SrcList *pNew;
523 int i;
drh113088e2003-03-20 01:16:58 +0000524 int nByte;
drhad3cab52002-05-24 02:04:32 +0000525 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000526 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000527 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000528 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000529 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000530 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000531 struct SrcList_item *pNewItem = &pNew->a[i];
532 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000533 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000534 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
535 pNewItem->zName = sqliteStrDup(pOldItem->zName);
536 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
537 pNewItem->jointype = pOldItem->jointype;
538 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000539 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000540 pTab = pNewItem->pTab = pOldItem->pTab;
541 if( pTab ){
542 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000543 }
danielk19774adee202004-05-08 08:23:19 +0000544 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
545 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
546 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000547 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000548 }
549 return pNew;
550}
danielk19774adee202004-05-08 08:23:19 +0000551IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000552 IdList *pNew;
553 int i;
554 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000555 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000556 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000557 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000558 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000559 if( pNew->a==0 ){
560 sqliteFree(pNew);
561 return 0;
562 }
drhff78bd22002-02-27 01:47:11 +0000563 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000564 struct IdList_item *pNewItem = &pNew->a[i];
565 struct IdList_item *pOldItem = &p->a[i];
566 pNewItem->zName = sqliteStrDup(pOldItem->zName);
567 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000568 }
569 return pNew;
570}
danielk19774adee202004-05-08 08:23:19 +0000571Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000572 Select *pNew;
573 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000574 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000575 if( pNew==0 ) return 0;
576 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000577 pNew->pEList = sqlite3ExprListDup(p->pEList);
578 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
579 pNew->pWhere = sqlite3ExprDup(p->pWhere);
580 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
581 pNew->pHaving = sqlite3ExprDup(p->pHaving);
582 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000583 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000584 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000585 pNew->pLimit = sqlite3ExprDup(p->pLimit);
586 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000587 pNew->iLimit = -1;
588 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000589 pNew->isResolved = p->isResolved;
590 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000591 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000592 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000593 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000594 pNew->addrOpenEphm[0] = -1;
595 pNew->addrOpenEphm[1] = -1;
596 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000597 return pNew;
598}
danielk197793758c82005-01-21 08:13:14 +0000599#else
600Select *sqlite3SelectDup(Select *p){
601 assert( p==0 );
602 return 0;
603}
604#endif
drhff78bd22002-02-27 01:47:11 +0000605
606
607/*
drha76b5df2002-02-23 02:32:10 +0000608** Add a new element to the end of an expression list. If pList is
609** initially NULL, then create a new expression list.
610*/
danielk19774adee202004-05-08 08:23:19 +0000611ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000612 if( pList==0 ){
613 pList = sqliteMalloc( sizeof(ExprList) );
614 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000615 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000616 }
drh4efc4752004-01-16 15:55:37 +0000617 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000618 }
drh4305d102003-07-30 12:34:12 +0000619 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000620 struct ExprList_item *a;
621 int n = pList->nAlloc*2 + 4;
622 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
623 if( a==0 ){
624 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000625 }
danielk1977d5d56522005-03-16 12:15:20 +0000626 pList->a = a;
627 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000628 }
drh4efc4752004-01-16 15:55:37 +0000629 assert( pList->a!=0 );
630 if( pExpr || pName ){
631 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
632 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000633 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000634 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000635 }
636 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000637
638no_mem:
639 /* Avoid leaking memory if malloc has failed. */
640 sqlite3ExprDelete(pExpr);
641 sqlite3ExprListDelete(pList);
642 return 0;
drha76b5df2002-02-23 02:32:10 +0000643}
644
645/*
646** Delete an entire expression list.
647*/
danielk19774adee202004-05-08 08:23:19 +0000648void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000649 int i;
drhbe5c89a2004-07-26 00:31:09 +0000650 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000651 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000652 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
653 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000654 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
655 sqlite3ExprDelete(pItem->pExpr);
656 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000657 }
658 sqliteFree(pList->a);
659 sqliteFree(pList);
660}
661
662/*
drh626a8792005-01-17 22:08:19 +0000663** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000664**
drh626a8792005-01-17 22:08:19 +0000665** The return value from xFunc determines whether the tree walk continues.
666** 0 means continue walking the tree. 1 means do not walk children
667** of the current node but continue with siblings. 2 means abandon
668** the tree walk completely.
669**
670** The return value from this routine is 1 to abandon the tree walk
671** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000672**
673** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000674*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000675static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000676static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000677 int rc;
678 if( pExpr==0 ) return 0;
679 rc = (*xFunc)(pArg, pExpr);
680 if( rc==0 ){
681 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
682 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000683 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000684 }
685 return rc>1;
686}
687
688/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000689** Call walkExprTree() for every expression in list p.
690*/
691static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
692 int i;
693 struct ExprList_item *pItem;
694 if( !p ) return 0;
695 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
696 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
697 }
698 return 0;
699}
700
701/*
702** Call walkExprTree() for every expression in Select p, not including
703** expressions that are part of sub-selects in any FROM clause or the LIMIT
704** or OFFSET expressions..
705*/
706static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
707 walkExprList(p->pEList, xFunc, pArg);
708 walkExprTree(p->pWhere, xFunc, pArg);
709 walkExprList(p->pGroupBy, xFunc, pArg);
710 walkExprTree(p->pHaving, xFunc, pArg);
711 walkExprList(p->pOrderBy, xFunc, pArg);
712 return 0;
713}
714
715
716/*
drh626a8792005-01-17 22:08:19 +0000717** This routine is designed as an xFunc for walkExprTree().
718**
719** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000720** at pExpr that the expression that contains pExpr is not a constant
721** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
722** If pExpr does does not disqualify the expression from being a constant
723** then do nothing.
724**
725** After walking the whole tree, if no nodes are found that disqualify
726** the expression as constant, then we assume the whole expression
727** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000728*/
729static int exprNodeIsConstant(void *pArg, Expr *pExpr){
730 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000731 /* Consider functions to be constant if all their arguments are constant
732 ** and *pArg==2 */
733 case TK_FUNCTION:
734 if( *((int*)pArg)==2 ) return 0;
735 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000736 case TK_ID:
737 case TK_COLUMN:
738 case TK_DOT:
739 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000740 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000741#ifndef SQLITE_OMIT_SUBQUERY
742 case TK_SELECT:
743 case TK_EXISTS:
744#endif
drh626a8792005-01-17 22:08:19 +0000745 *((int*)pArg) = 0;
746 return 2;
drh87abf5c2005-08-25 12:45:04 +0000747 case TK_IN:
748 if( pExpr->pSelect ){
749 *((int*)pArg) = 0;
750 return 2;
751 }
drh626a8792005-01-17 22:08:19 +0000752 default:
753 return 0;
754 }
755}
756
757/*
drhfef52082000-06-06 01:50:43 +0000758** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000759** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000760**
761** For the purposes of this function, a double-quoted string (ex: "abc")
762** is considered a variable but a single-quoted string (ex: 'abc') is
763** a constant.
drhfef52082000-06-06 01:50:43 +0000764*/
danielk19774adee202004-05-08 08:23:19 +0000765int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000766 int isConst = 1;
767 walkExprTree(p, exprNodeIsConstant, &isConst);
768 return isConst;
drhfef52082000-06-06 01:50:43 +0000769}
770
771/*
drheb55bd22005-06-30 17:04:21 +0000772** Walk an expression tree. Return 1 if the expression is constant
773** or a function call with constant arguments. Return and 0 if there
774** are any variables.
775**
776** For the purposes of this function, a double-quoted string (ex: "abc")
777** is considered a variable but a single-quoted string (ex: 'abc') is
778** a constant.
779*/
780int sqlite3ExprIsConstantOrFunction(Expr *p){
781 int isConst = 2;
782 walkExprTree(p, exprNodeIsConstant, &isConst);
783 return isConst!=0;
784}
785
786/*
drh73b211a2005-01-18 04:00:42 +0000787** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000788** to fit in a 32-bit integer, return 1 and put the value of the integer
789** in *pValue. If the expression is not an integer or if it is too big
790** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000791*/
danielk19774adee202004-05-08 08:23:19 +0000792int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000793 switch( p->op ){
794 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000795 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000796 return 1;
797 }
798 break;
drhe4de1fe2002-06-02 16:09:01 +0000799 }
drh4b59ab52002-08-24 18:24:51 +0000800 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000801 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000802 }
drhe4de1fe2002-06-02 16:09:01 +0000803 case TK_UMINUS: {
804 int v;
danielk19774adee202004-05-08 08:23:19 +0000805 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000806 *pValue = -v;
807 return 1;
808 }
809 break;
810 }
811 default: break;
812 }
813 return 0;
814}
815
816/*
drhc4a3c772001-04-04 11:48:57 +0000817** Return TRUE if the given string is a row-id column name.
818*/
danielk19774adee202004-05-08 08:23:19 +0000819int sqlite3IsRowid(const char *z){
820 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
821 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
822 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000823 return 0;
824}
825
826/*
drh8141f612004-01-25 22:44:58 +0000827** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
828** that name in the set of source tables in pSrcList and make the pExpr
829** expression node refer back to that source column. The following changes
830** are made to pExpr:
831**
832** pExpr->iDb Set the index in db->aDb[] of the database holding
833** the table.
834** pExpr->iTable Set to the cursor number for the table obtained
835** from pSrcList.
836** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000837** pExpr->op Set to TK_COLUMN.
838** pExpr->pLeft Any expression this points to is deleted
839** pExpr->pRight Any expression this points to is deleted.
840**
841** The pDbToken is the name of the database (the "X"). This value may be
842** NULL meaning that name is of the form Y.Z or Z. Any available database
843** can be used. The pTableToken is the name of the table (the "Y"). This
844** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
845** means that the form of the name is Z and that columns from any table
846** can be used.
847**
848** If the name cannot be resolved unambiguously, leave an error message
849** in pParse and return non-zero. Return zero on success.
850*/
851static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000852 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000853 Token *pDbToken, /* Name of the database containing table, or NULL */
854 Token *pTableToken, /* Name of table containing column, or NULL */
855 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000856 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000857 Expr *pExpr /* Make this EXPR node point to the selected column */
858){
859 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
860 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
861 char *zCol = 0; /* Name of the column. The "Z" */
862 int i, j; /* Loop counters */
863 int cnt = 0; /* Number of matching column names */
864 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000865 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000866 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
867 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000868 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000869
870 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000871 zDb = sqlite3NameFromToken(pDbToken);
872 zTab = sqlite3NameFromToken(pTableToken);
873 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000874 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000875 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000876 }
drh8141f612004-01-25 22:44:58 +0000877
878 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000879 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000880 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000881 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000882
danielk1977b3bce662005-01-29 08:32:43 +0000883 if( pSrcList ){
884 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000885 Table *pTab;
886 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000887 Column *pCol;
888
drh43617e92006-03-06 20:55:46 +0000889 pTab = pItem->pTab;
890 assert( pTab!=0 );
891 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000892 assert( pTab->nCol>0 );
893 if( zTab ){
894 if( pItem->zAlias ){
895 char *zTabName = pItem->zAlias;
896 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
897 }else{
898 char *zTabName = pTab->zName;
899 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000900 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000901 continue;
902 }
drh626a8792005-01-17 22:08:19 +0000903 }
drh8141f612004-01-25 22:44:58 +0000904 }
danielk1977b3bce662005-01-29 08:32:43 +0000905 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000906 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000907 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000908 pMatch = pItem;
909 }
910 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
911 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000912 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000913 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000914 cnt++;
915 pExpr->iTable = pItem->iCursor;
916 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000917 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000918 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
919 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
920 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000921 if( (pExpr->flags & EP_ExpCollate)==0 ){
922 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
923 }
drh61dfc312006-12-16 16:25:15 +0000924 if( i<pSrcList->nSrc-1 ){
925 if( pItem[1].jointype & JT_NATURAL ){
926 /* If this match occurred in the left table of a natural join,
927 ** then skip the right table to avoid a duplicate match */
928 pItem++;
929 i++;
930 }else if( (pUsing = pItem[1].pUsing)!=0 ){
931 /* If this match occurs on a column that is in the USING clause
932 ** of a join, skip the search of the right table of the join
933 ** to avoid a duplicate match there. */
934 int k;
935 for(k=0; k<pUsing->nId; k++){
936 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
937 pItem++;
938 i++;
939 break;
940 }
drh873fac02005-06-06 17:11:46 +0000941 }
942 }
943 }
danielk1977b3bce662005-01-29 08:32:43 +0000944 break;
945 }
drh8141f612004-01-25 22:44:58 +0000946 }
947 }
948 }
drh626a8792005-01-17 22:08:19 +0000949
950#ifndef SQLITE_OMIT_TRIGGER
951 /* If we have not already resolved the name, then maybe
952 ** it is a new.* or old.* trigger argument reference
953 */
954 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
955 TriggerStack *pTriggerStack = pParse->trigStack;
956 Table *pTab = 0;
957 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
958 pExpr->iTable = pTriggerStack->newIdx;
959 assert( pTriggerStack->pTab );
960 pTab = pTriggerStack->pTab;
961 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
962 pExpr->iTable = pTriggerStack->oldIdx;
963 assert( pTriggerStack->pTab );
964 pTab = pTriggerStack->pTab;
965 }
966
967 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +0000968 int iCol;
drh626a8792005-01-17 22:08:19 +0000969 Column *pCol = pTab->aCol;
970
danielk1977da184232006-01-05 11:34:32 +0000971 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000972 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +0000973 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +0000974 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +0000975 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +0000976 cnt++;
danielk1977f0113002006-01-24 12:09:17 +0000977 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
978 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000979 if( (pExpr->flags & EP_ExpCollate)==0 ){
980 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
981 }
danielk1977aee18ef2005-03-09 12:26:50 +0000982 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000983 break;
984 }
985 }
986 }
987 }
drhb7f91642004-10-31 02:22:47 +0000988#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000989
drh626a8792005-01-17 22:08:19 +0000990 /*
991 ** Perhaps the name is a reference to the ROWID
992 */
993 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
994 cnt = 1;
995 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +0000996 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +0000997 }
drh8141f612004-01-25 22:44:58 +0000998
drh626a8792005-01-17 22:08:19 +0000999 /*
1000 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1001 ** might refer to an result-set alias. This happens, for example, when
1002 ** we are resolving names in the WHERE clause of the following command:
1003 **
1004 ** SELECT a+b AS x FROM table WHERE x<10;
1005 **
1006 ** In cases like this, replace pExpr with a copy of the expression that
1007 ** forms the result set entry ("a+b" in the example) and return immediately.
1008 ** Note that the expression in the result set should have already been
1009 ** resolved by the time the WHERE clause is resolved.
1010 */
drhffe07b22005-11-03 00:41:17 +00001011 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001012 for(j=0; j<pEList->nExpr; j++){
1013 char *zAs = pEList->a[j].zName;
1014 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1015 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1016 pExpr->op = TK_AS;
1017 pExpr->iColumn = j;
1018 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001019 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001020 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001021 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001022 }
1023 }
1024 }
1025
1026 /* Advance to the next name context. The loop will exit when either
1027 ** we have a match (cnt>0) or when we run out of name contexts.
1028 */
1029 if( cnt==0 ){
1030 pNC = pNC->pNext;
1031 }
drh8141f612004-01-25 22:44:58 +00001032 }
1033
1034 /*
1035 ** If X and Y are NULL (in other words if only the column name Z is
1036 ** supplied) and the value of Z is enclosed in double-quotes, then
1037 ** Z is a string literal if it doesn't match any column names. In that
1038 ** case, we need to return right away and not make any changes to
1039 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001040 **
1041 ** Because no reference was made to outer contexts, the pNC->nRef
1042 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001043 */
1044 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1045 sqliteFree(zCol);
1046 return 0;
1047 }
1048
1049 /*
1050 ** cnt==0 means there was not match. cnt>1 means there were two or
1051 ** more matches. Either way, we have an error.
1052 */
1053 if( cnt!=1 ){
1054 char *z = 0;
1055 char *zErr;
1056 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1057 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001058 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001059 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001060 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001061 }else{
1062 z = sqliteStrDup(zCol);
1063 }
danielk19774adee202004-05-08 08:23:19 +00001064 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001065 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001066 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001067 }
1068
drh51669862004-12-18 18:40:26 +00001069 /* If a column from a table in pSrcList is referenced, then record
1070 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1071 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1072 ** column number is greater than the number of bits in the bitmask
1073 ** then set the high-order bit of the bitmask.
1074 */
1075 if( pExpr->iColumn>=0 && pMatch!=0 ){
1076 int n = pExpr->iColumn;
1077 if( n>=sizeof(Bitmask)*8 ){
1078 n = sizeof(Bitmask)*8-1;
1079 }
1080 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001081 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001082 }
1083
danielk1977d5d56522005-03-16 12:15:20 +00001084lookupname_end:
drh8141f612004-01-25 22:44:58 +00001085 /* Clean up and return
1086 */
1087 sqliteFree(zDb);
1088 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001089 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001090 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001091 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001092 pExpr->pRight = 0;
1093 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001094lookupname_end_2:
1095 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001096 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001097 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001098 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001099 if( pMatch && !pMatch->pSelect ){
1100 pExpr->pTab = pMatch->pTab;
1101 }
drh15ccce12005-05-23 15:06:39 +00001102 /* Increment the nRef value on all name contexts from TopNC up to
1103 ** the point where the name matched. */
1104 for(;;){
1105 assert( pTopNC!=0 );
1106 pTopNC->nRef++;
1107 if( pTopNC==pNC ) break;
1108 pTopNC = pTopNC->pNext;
1109 }
1110 return 0;
1111 } else {
1112 return 1;
drh626a8792005-01-17 22:08:19 +00001113 }
drh8141f612004-01-25 22:44:58 +00001114}
1115
1116/*
drh626a8792005-01-17 22:08:19 +00001117** This routine is designed as an xFunc for walkExprTree().
1118**
drh73b211a2005-01-18 04:00:42 +00001119** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001120** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001121** the tree or 2 to abort the tree walk.
1122**
1123** This routine also does error checking and name resolution for
1124** function names. The operator for aggregate functions is changed
1125** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001126*/
1127static int nameResolverStep(void *pArg, Expr *pExpr){
1128 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001129 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001130
danielk1977b3bce662005-01-29 08:32:43 +00001131 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001132 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001133 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001134
drh626a8792005-01-17 22:08:19 +00001135 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1136 ExprSetProperty(pExpr, EP_Resolved);
1137#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001138 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1139 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001140 int i;
danielk1977f0113002006-01-24 12:09:17 +00001141 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001142 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1143 }
1144 }
1145#endif
1146 switch( pExpr->op ){
1147 /* Double-quoted strings (ex: "abc") are used as identifiers if
1148 ** possible. Otherwise they remain as strings. Single-quoted
1149 ** strings (ex: 'abc') are always string literals.
1150 */
1151 case TK_STRING: {
1152 if( pExpr->token.z[0]=='\'' ) break;
1153 /* Fall thru into the TK_ID case if this is a double-quoted string */
1154 }
1155 /* A lone identifier is the name of a column.
1156 */
1157 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001158 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1159 return 1;
1160 }
1161
1162 /* A table name and column name: ID.ID
1163 ** Or a database, table and column: ID.ID.ID
1164 */
1165 case TK_DOT: {
1166 Token *pColumn;
1167 Token *pTable;
1168 Token *pDb;
1169 Expr *pRight;
1170
danielk1977b3bce662005-01-29 08:32:43 +00001171 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001172 pRight = pExpr->pRight;
1173 if( pRight->op==TK_ID ){
1174 pDb = 0;
1175 pTable = &pExpr->pLeft->token;
1176 pColumn = &pRight->token;
1177 }else{
1178 assert( pRight->op==TK_DOT );
1179 pDb = &pExpr->pLeft->token;
1180 pTable = &pRight->pLeft->token;
1181 pColumn = &pRight->pRight->token;
1182 }
1183 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1184 return 1;
1185 }
1186
1187 /* Resolve function names
1188 */
drhb71090f2005-05-23 17:26:51 +00001189 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001190 case TK_FUNCTION: {
1191 ExprList *pList = pExpr->pList; /* The argument list */
1192 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1193 int no_such_func = 0; /* True if no such function exists */
1194 int wrong_num_args = 0; /* True if wrong number of arguments */
1195 int is_agg = 0; /* True if is an aggregate function */
1196 int i;
drh5169bbc2006-08-24 14:59:45 +00001197 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001198 int nId; /* Number of characters in function name */
1199 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001200 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001201 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001202
drh2646da72005-12-09 20:02:05 +00001203 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001204 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001205 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1206 if( pDef==0 ){
1207 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1208 if( pDef==0 ){
1209 no_such_func = 1;
1210 }else{
1211 wrong_num_args = 1;
1212 }
1213 }else{
1214 is_agg = pDef->xFunc==0;
1215 }
drh2fca7fe2006-11-23 11:59:13 +00001216#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001217 if( pDef ){
1218 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1219 if( auth!=SQLITE_OK ){
1220 if( auth==SQLITE_DENY ){
1221 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1222 pDef->zName);
1223 pNC->nErr++;
1224 }
1225 pExpr->op = TK_NULL;
1226 return 1;
1227 }
1228 }
drhb8b14212006-08-24 15:18:25 +00001229#endif
drh626a8792005-01-17 22:08:19 +00001230 if( is_agg && !pNC->allowAgg ){
1231 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1232 pNC->nErr++;
1233 is_agg = 0;
1234 }else if( no_such_func ){
1235 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1236 pNC->nErr++;
1237 }else if( wrong_num_args ){
1238 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1239 nId, zId);
1240 pNC->nErr++;
1241 }
1242 if( is_agg ){
1243 pExpr->op = TK_AGG_FUNCTION;
1244 pNC->hasAgg = 1;
1245 }
drh73b211a2005-01-18 04:00:42 +00001246 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001247 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001248 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001249 }
drh73b211a2005-01-18 04:00:42 +00001250 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001251 /* FIX ME: Compute pExpr->affinity based on the expected return
1252 ** type of the function
1253 */
1254 return is_agg;
1255 }
danielk1977b3bce662005-01-29 08:32:43 +00001256#ifndef SQLITE_OMIT_SUBQUERY
1257 case TK_SELECT:
1258 case TK_EXISTS:
1259#endif
1260 case TK_IN: {
1261 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001262 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001263#ifndef SQLITE_OMIT_CHECK
1264 if( pNC->isCheck ){
1265 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1266 }
1267#endif
danielk1977b3bce662005-01-29 08:32:43 +00001268 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1269 assert( pNC->nRef>=nRef );
1270 if( nRef!=pNC->nRef ){
1271 ExprSetProperty(pExpr, EP_VarSelect);
1272 }
1273 }
drh4284fb02005-11-03 12:33:28 +00001274 break;
danielk1977b3bce662005-01-29 08:32:43 +00001275 }
drh4284fb02005-11-03 12:33:28 +00001276#ifndef SQLITE_OMIT_CHECK
1277 case TK_VARIABLE: {
1278 if( pNC->isCheck ){
1279 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1280 }
1281 break;
1282 }
1283#endif
drh626a8792005-01-17 22:08:19 +00001284 }
1285 return 0;
1286}
1287
1288/*
drhcce7d172000-05-31 15:34:51 +00001289** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001290** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001291** index to the table in the table list and a column offset. The
1292** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1293** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001294** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001295** VDBE cursor number for a cursor that is pointing into the referenced
1296** table. The Expr.iColumn value is changed to the index of the column
1297** of the referenced table. The Expr.iColumn value for the special
1298** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1299** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001300**
drh626a8792005-01-17 22:08:19 +00001301** Also resolve function names and check the functions for proper
1302** usage. Make sure all function names are recognized and all functions
1303** have the correct number of arguments. Leave an error message
1304** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1305**
drh73b211a2005-01-18 04:00:42 +00001306** If the expression contains aggregate functions then set the EP_Agg
1307** property on the expression.
drh626a8792005-01-17 22:08:19 +00001308*/
1309int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001310 NameContext *pNC, /* Namespace to resolve expressions in. */
1311 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001312){
drh13449892005-09-07 21:22:45 +00001313 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001314 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001315 savedHasAgg = pNC->hasAgg;
1316 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001317 walkExprTree(pExpr, nameResolverStep, pNC);
1318 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001319 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001320 }
drh13449892005-09-07 21:22:45 +00001321 if( pNC->hasAgg ){
1322 ExprSetProperty(pExpr, EP_Agg);
1323 }else if( savedHasAgg ){
1324 pNC->hasAgg = 1;
1325 }
drh73b211a2005-01-18 04:00:42 +00001326 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001327}
1328
drh1398ad32005-01-19 23:24:50 +00001329/*
1330** A pointer instance of this structure is used to pass information
1331** through walkExprTree into codeSubqueryStep().
1332*/
1333typedef struct QueryCoder QueryCoder;
1334struct QueryCoder {
1335 Parse *pParse; /* The parsing context */
1336 NameContext *pNC; /* Namespace of first enclosing query */
1337};
1338
drh626a8792005-01-17 22:08:19 +00001339
1340/*
drh9cbe6352005-11-29 03:13:21 +00001341** Generate code for scalar subqueries used as an expression
1342** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001343**
drh9cbe6352005-11-29 03:13:21 +00001344** (SELECT a FROM b) -- subquery
1345** EXISTS (SELECT a FROM b) -- EXISTS subquery
1346** x IN (4,5,11) -- IN operator with list on right-hand side
1347** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001348**
drh9cbe6352005-11-29 03:13:21 +00001349** The pExpr parameter describes the expression that contains the IN
1350** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001351*/
drh51522cd2005-01-20 13:36:19 +00001352#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001353void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001354 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001355 Vdbe *v = sqlite3GetVdbe(pParse);
1356 if( v==0 ) return;
1357
drh57dbd7b2005-07-08 18:25:26 +00001358 /* This code must be run in its entirety every time it is encountered
1359 ** if any of the following is true:
1360 **
1361 ** * The right-hand side is a correlated subquery
1362 ** * The right-hand side is an expression list containing variables
1363 ** * We are inside a trigger
1364 **
1365 ** If all of the above are false, then we can run this code just once
1366 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001367 */
1368 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1369 int mem = pParse->nMem++;
1370 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001371 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001372 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001373 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001374 }
1375
drhcce7d172000-05-31 15:34:51 +00001376 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001377 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001378 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001379 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001380 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001381
danielk1977bf3b7212004-05-18 10:06:24 +00001382 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001383
1384 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001385 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001386 ** filled with single-field index keys representing the results
1387 ** from the SELECT or the <exprlist>.
1388 **
1389 ** If the 'x' expression is a column value, or the SELECT...
1390 ** statement returns a column value, then the affinity of that
1391 ** column is used to build the index keys. If both 'x' and the
1392 ** SELECT... statement are columns, then numeric affinity is used
1393 ** if either column has NUMERIC or INTEGER affinity. If neither
1394 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1395 ** is used.
1396 */
1397 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001398 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001399 memset(&keyInfo, 0, sizeof(keyInfo));
1400 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001401 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001402
drhfef52082000-06-06 01:50:43 +00001403 if( pExpr->pSelect ){
1404 /* Case 1: expr IN (SELECT ...)
1405 **
danielk1977e014a832004-05-17 10:48:57 +00001406 ** Generate code to write the results of the select into the temporary
1407 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001408 */
danielk1977e014a832004-05-17 10:48:57 +00001409 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001410 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001411 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001412 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001413 pEList = pExpr->pSelect->pEList;
1414 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001415 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001416 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001417 }
drhfef52082000-06-06 01:50:43 +00001418 }else if( pExpr->pList ){
1419 /* Case 2: expr IN (exprlist)
1420 **
danielk1977e014a832004-05-17 10:48:57 +00001421 ** For each expression, build an index key from the evaluation and
1422 ** store it in the temporary table. If <expr> is a column, then use
1423 ** that columns affinity when building index keys. If <expr> is not
1424 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001425 */
danielk1977e014a832004-05-17 10:48:57 +00001426 int i;
drh57dbd7b2005-07-08 18:25:26 +00001427 ExprList *pList = pExpr->pList;
1428 struct ExprList_item *pItem;
1429
danielk1977e014a832004-05-17 10:48:57 +00001430 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001431 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001432 }
danielk19770202b292004-06-09 09:55:16 +00001433 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001434
1435 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001436 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1437 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001438
drh57dbd7b2005-07-08 18:25:26 +00001439 /* If the expression is not constant then we will need to
1440 ** disable the test that was generated above that makes sure
1441 ** this code only executes once. Because for a non-constant
1442 ** expression we need to rerun this code each time.
1443 */
drh6c30be82005-07-29 15:10:17 +00001444 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001445 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001446 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001447 }
danielk1977e014a832004-05-17 10:48:57 +00001448
1449 /* Evaluate the expression and insert it into the temp table */
1450 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001451 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001452 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001453 }
1454 }
danielk19770202b292004-06-09 09:55:16 +00001455 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001456 break;
drhfef52082000-06-06 01:50:43 +00001457 }
1458
drh51522cd2005-01-20 13:36:19 +00001459 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001460 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001461 /* This has to be a scalar SELECT. Generate code to put the
1462 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001463 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001464 */
drh2646da72005-12-09 20:02:05 +00001465 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001466 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001467 int iMem;
1468 int sop;
drh1398ad32005-01-19 23:24:50 +00001469
drhec7429a2005-10-06 16:53:14 +00001470 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001471 pSel = pExpr->pSelect;
1472 if( pExpr->op==TK_SELECT ){
1473 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001474 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1475 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001476 }else{
drh51522cd2005-01-20 13:36:19 +00001477 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001478 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1479 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001480 }
drhec7429a2005-10-06 16:53:14 +00001481 sqlite3ExprDelete(pSel->pLimit);
1482 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1483 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001484 break;
drhcce7d172000-05-31 15:34:51 +00001485 }
1486 }
danielk1977b3bce662005-01-29 08:32:43 +00001487
drh57dbd7b2005-07-08 18:25:26 +00001488 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001489 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001490 }
1491 return;
drhcce7d172000-05-31 15:34:51 +00001492}
drh51522cd2005-01-20 13:36:19 +00001493#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001494
drhcce7d172000-05-31 15:34:51 +00001495/*
drhfec19aa2004-05-19 20:41:03 +00001496** Generate an instruction that will put the integer describe by
1497** text z[0..n-1] on the stack.
1498*/
1499static void codeInteger(Vdbe *v, const char *z, int n){
1500 int i;
drh6fec0762004-05-30 01:38:43 +00001501 if( sqlite3GetInt32(z, &i) ){
1502 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1503 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001504 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001505 }else{
1506 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1507 }
1508}
1509
1510/*
drhcce7d172000-05-31 15:34:51 +00001511** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001512** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001513**
1514** This code depends on the fact that certain token values (ex: TK_EQ)
1515** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1516** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1517** the make process cause these values to align. Assert()s in the code
1518** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001519*/
danielk19774adee202004-05-08 08:23:19 +00001520void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001521 Vdbe *v = pParse->pVdbe;
1522 int op;
drhffe07b22005-11-03 00:41:17 +00001523 int stackChng = 1; /* Amount of change to stack depth */
1524
danielk19777977a172004-11-09 12:44:37 +00001525 if( v==0 ) return;
1526 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001527 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001528 return;
1529 }
drhf2bc0132004-10-04 13:19:23 +00001530 op = pExpr->op;
1531 switch( op ){
drh13449892005-09-07 21:22:45 +00001532 case TK_AGG_COLUMN: {
1533 AggInfo *pAggInfo = pExpr->pAggInfo;
1534 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1535 if( !pAggInfo->directMode ){
1536 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1537 break;
1538 }else if( pAggInfo->useSortingIdx ){
1539 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1540 pCol->iSorterColumn);
1541 break;
1542 }
1543 /* Otherwise, fall thru into the TK_COLUMN case */
1544 }
drh967e8b72000-06-21 13:59:10 +00001545 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001546 if( pExpr->iTable<0 ){
1547 /* This only happens when coding check constraints */
1548 assert( pParse->ckOffset>0 );
1549 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1550 }else if( pExpr->iColumn>=0 ){
drh8a512562005-11-14 22:29:05 +00001551 Table *pTab = pExpr->pTab;
1552 int iCol = pExpr->iColumn;
drh4cbdda92006-06-14 19:00:20 +00001553 int op = (pTab && IsVirtual(pTab)) ? OP_VColumn : OP_Column;
danielk19777dabaa12006-06-13 04:11:43 +00001554 sqlite3VdbeAddOp(v, op, pExpr->iTable, iCol);
drh8a512562005-11-14 22:29:05 +00001555 sqlite3ColumnDefault(v, pTab, iCol);
1556#ifndef SQLITE_OMIT_FLOATING_POINT
1557 if( pTab && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
1558 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1559 }
1560#endif
drhc4a3c772001-04-04 11:48:57 +00001561 }else{
danielk19777dabaa12006-06-13 04:11:43 +00001562 Table *pTab = pExpr->pTab;
drh4cbdda92006-06-14 19:00:20 +00001563 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
danielk19777dabaa12006-06-13 04:11:43 +00001564 sqlite3VdbeAddOp(v, op, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001565 }
drhcce7d172000-05-31 15:34:51 +00001566 break;
1567 }
1568 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001569 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001570 break;
1571 }
1572 case TK_FLOAT:
1573 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001574 assert( TK_FLOAT==OP_Real );
1575 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001576 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001577 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001578 break;
1579 }
drhf0863fe2005-06-12 21:35:51 +00001580 case TK_NULL: {
1581 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1582 break;
1583 }
danielk19775338a5f2005-01-20 13:03:10 +00001584#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001585 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001586 int n;
1587 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001588 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001589 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001590 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001591 assert( n>=0 );
1592 if( n==0 ){
1593 z = "";
1594 }
1595 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001596 break;
1597 }
danielk19775338a5f2005-01-20 13:03:10 +00001598#endif
drh50457892003-09-06 01:10:47 +00001599 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001601 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001602 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001603 }
drh50457892003-09-06 01:10:47 +00001604 break;
1605 }
drh4e0cff62004-11-05 05:10:28 +00001606 case TK_REGISTER: {
1607 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1608 break;
1609 }
drh487e2622005-06-25 18:42:14 +00001610#ifndef SQLITE_OMIT_CAST
1611 case TK_CAST: {
1612 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001613 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001614 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001615 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001616 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1617 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1618 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1619 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1620 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1621 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1622 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001623 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001624 break;
1625 }
1626#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001627 case TK_LT:
1628 case TK_LE:
1629 case TK_GT:
1630 case TK_GE:
1631 case TK_NE:
1632 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001633 assert( TK_LT==OP_Lt );
1634 assert( TK_LE==OP_Le );
1635 assert( TK_GT==OP_Gt );
1636 assert( TK_GE==OP_Ge );
1637 assert( TK_EQ==OP_Eq );
1638 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001639 sqlite3ExprCode(pParse, pExpr->pLeft);
1640 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001641 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001642 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001643 break;
drhc9b84a12002-06-20 11:36:48 +00001644 }
drhcce7d172000-05-31 15:34:51 +00001645 case TK_AND:
1646 case TK_OR:
1647 case TK_PLUS:
1648 case TK_STAR:
1649 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001650 case TK_REM:
1651 case TK_BITAND:
1652 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001653 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001654 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001655 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001656 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001657 assert( TK_AND==OP_And );
1658 assert( TK_OR==OP_Or );
1659 assert( TK_PLUS==OP_Add );
1660 assert( TK_MINUS==OP_Subtract );
1661 assert( TK_REM==OP_Remainder );
1662 assert( TK_BITAND==OP_BitAnd );
1663 assert( TK_BITOR==OP_BitOr );
1664 assert( TK_SLASH==OP_Divide );
1665 assert( TK_LSHIFT==OP_ShiftLeft );
1666 assert( TK_RSHIFT==OP_ShiftRight );
1667 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001668 sqlite3ExprCode(pParse, pExpr->pLeft);
1669 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001670 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001671 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001672 break;
1673 }
drhcce7d172000-05-31 15:34:51 +00001674 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001675 Expr *pLeft = pExpr->pLeft;
1676 assert( pLeft );
1677 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1678 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001679 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001680 if( pLeft->op==TK_FLOAT ){
1681 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001682 }else{
drhfec19aa2004-05-19 20:41:03 +00001683 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001684 }
drh6e142f52000-06-08 13:36:40 +00001685 sqliteFree(z);
1686 break;
1687 }
drh1ccde152000-06-17 13:12:39 +00001688 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001689 }
drhbf4133c2001-10-13 02:59:08 +00001690 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001691 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001692 assert( TK_BITNOT==OP_BitNot );
1693 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001694 sqlite3ExprCode(pParse, pExpr->pLeft);
1695 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001696 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001697 break;
1698 }
1699 case TK_ISNULL:
1700 case TK_NOTNULL: {
1701 int dest;
drhf2bc0132004-10-04 13:19:23 +00001702 assert( TK_ISNULL==OP_IsNull );
1703 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001704 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1705 sqlite3ExprCode(pParse, pExpr->pLeft);
1706 dest = sqlite3VdbeCurrentAddr(v) + 2;
1707 sqlite3VdbeAddOp(v, op, 1, dest);
1708 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001709 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001710 break;
drhcce7d172000-05-31 15:34:51 +00001711 }
drh22827922000-06-06 17:27:05 +00001712 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001713 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001714 if( pInfo==0 ){
1715 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1716 &pExpr->span);
1717 }else{
1718 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1719 }
drh22827922000-06-06 17:27:05 +00001720 break;
1721 }
drhb71090f2005-05-23 17:26:51 +00001722 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001723 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001724 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001725 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001726 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001727 int nId;
1728 const char *zId;
drh13449892005-09-07 21:22:45 +00001729 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001730 int i;
danielk197714db2662006-01-09 16:12:04 +00001731 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001732 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001733 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001734 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001735 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001736 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001737 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001738#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001739 /* Possibly overload the function if the first argument is
1740 ** a virtual table column.
1741 **
1742 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1743 ** second argument, not the first, as the argument to test to
1744 ** see if it is a column in a virtual table. This is done because
1745 ** the left operand of infix functions (the operand we want to
1746 ** control overloading) ends up as the second argument to the
1747 ** function. The expression "A glob B" is equivalent to
1748 ** "glob(B,A). We want to use the A in "A glob B" to test
1749 ** for function overloading. But we use the B term in "glob(B,A)".
1750 */
drh6a03a1c2006-07-08 18:34:59 +00001751 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001752 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1753 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001754 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1755 }
1756#endif
danielk1977682f68b2004-06-05 10:22:17 +00001757 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001758 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001759 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001760 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001761 if( pDef->needCollSeq && !pColl ){
1762 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1763 }
1764 }
1765 if( pDef->needCollSeq ){
1766 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001767 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001768 }
drh13449892005-09-07 21:22:45 +00001769 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001770 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001771 break;
1772 }
drhfe2093d2005-01-20 22:48:47 +00001773#ifndef SQLITE_OMIT_SUBQUERY
1774 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001775 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001776 if( pExpr->iColumn==0 ){
1777 sqlite3CodeSubselect(pParse, pExpr);
1778 }
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001780 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001781 break;
1782 }
drhfef52082000-06-06 01:50:43 +00001783 case TK_IN: {
1784 int addr;
drh94a11212004-09-25 13:12:14 +00001785 char affinity;
drhafa5f682006-01-30 14:36:59 +00001786 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001787 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001788
1789 /* Figure out the affinity to use to create a key from the results
1790 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001791 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001792 */
drh94a11212004-09-25 13:12:14 +00001793 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001794
danielk19774adee202004-05-08 08:23:19 +00001795 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001796 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001797
1798 /* Code the <expr> from "<expr> IN (...)". The temporary table
1799 ** pExpr->iTable contains the values that make up the (...) set.
1800 */
danielk19774adee202004-05-08 08:23:19 +00001801 sqlite3ExprCode(pParse, pExpr->pLeft);
1802 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001803 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001804 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001805 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001806 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001807 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001808 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1809 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1810
drhfef52082000-06-06 01:50:43 +00001811 break;
1812 }
danielk197793758c82005-01-21 08:13:14 +00001813#endif
drhfef52082000-06-06 01:50:43 +00001814 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001815 Expr *pLeft = pExpr->pLeft;
1816 struct ExprList_item *pLItem = pExpr->pList->a;
1817 Expr *pRight = pLItem->pExpr;
1818 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001819 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001820 sqlite3ExprCode(pParse, pRight);
1821 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001822 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001823 pLItem++;
1824 pRight = pLItem->pExpr;
1825 sqlite3ExprCode(pParse, pRight);
1826 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001827 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001828 break;
1829 }
drh51e9a442004-01-16 16:42:53 +00001830 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001831 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001832 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001833 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001834 break;
1835 }
drh17a7f8d2002-03-24 13:13:27 +00001836 case TK_CASE: {
1837 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001838 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001839 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001840 int i;
drhbe5c89a2004-07-26 00:31:09 +00001841 ExprList *pEList;
1842 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001843
1844 assert(pExpr->pList);
1845 assert((pExpr->pList->nExpr % 2) == 0);
1846 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001847 pEList = pExpr->pList;
1848 aListelem = pEList->a;
1849 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001850 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001851 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001852 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001853 }
drhf5905aa2002-05-26 20:54:33 +00001854 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001855 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001856 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001857 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001858 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1859 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001860 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001861 }else{
danielk19774adee202004-05-08 08:23:19 +00001862 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001863 }
drhbe5c89a2004-07-26 00:31:09 +00001864 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001865 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001866 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001867 }
drhf570f012002-05-31 15:51:25 +00001868 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001869 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001870 }
drh17a7f8d2002-03-24 13:13:27 +00001871 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001872 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001873 }else{
drhf0863fe2005-06-12 21:35:51 +00001874 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001875 }
danielk19774adee202004-05-08 08:23:19 +00001876 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001877 break;
1878 }
danielk19775338a5f2005-01-20 13:03:10 +00001879#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001880 case TK_RAISE: {
1881 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001882 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001883 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001884 return;
1885 }
drhad6d9462004-09-19 02:15:24 +00001886 if( pExpr->iColumn!=OE_Ignore ){
1887 assert( pExpr->iColumn==OE_Rollback ||
1888 pExpr->iColumn == OE_Abort ||
1889 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001890 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001891 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001892 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001893 } else {
drhad6d9462004-09-19 02:15:24 +00001894 assert( pExpr->iColumn == OE_Ignore );
1895 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1896 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1897 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001898 }
drhffe07b22005-11-03 00:41:17 +00001899 stackChng = 0;
1900 break;
drh17a7f8d2002-03-24 13:13:27 +00001901 }
danielk19775338a5f2005-01-20 13:03:10 +00001902#endif
drhffe07b22005-11-03 00:41:17 +00001903 }
1904
1905 if( pParse->ckOffset ){
1906 pParse->ckOffset += stackChng;
1907 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001908 }
drhcce7d172000-05-31 15:34:51 +00001909}
1910
danielk197793758c82005-01-21 08:13:14 +00001911#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001912/*
drh25303782004-12-07 15:41:48 +00001913** Generate code that evalutes the given expression and leaves the result
1914** on the stack. See also sqlite3ExprCode().
1915**
1916** This routine might also cache the result and modify the pExpr tree
1917** so that it will make use of the cached result on subsequent evaluations
1918** rather than evaluate the whole expression again. Trivial expressions are
1919** not cached. If the expression is cached, its result is stored in a
1920** memory location.
1921*/
1922void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1923 Vdbe *v = pParse->pVdbe;
1924 int iMem;
1925 int addr1, addr2;
1926 if( v==0 ) return;
1927 addr1 = sqlite3VdbeCurrentAddr(v);
1928 sqlite3ExprCode(pParse, pExpr);
1929 addr2 = sqlite3VdbeCurrentAddr(v);
1930 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1931 iMem = pExpr->iTable = pParse->nMem++;
1932 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1933 pExpr->op = TK_REGISTER;
1934 }
1935}
danielk197793758c82005-01-21 08:13:14 +00001936#endif
drh25303782004-12-07 15:41:48 +00001937
1938/*
drh268380c2004-02-25 13:47:31 +00001939** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001940** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001941**
1942** Return the number of elements pushed onto the stack.
1943*/
danielk19774adee202004-05-08 08:23:19 +00001944int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001945 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001946 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001947){
1948 struct ExprList_item *pItem;
1949 int i, n;
drh268380c2004-02-25 13:47:31 +00001950 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001951 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001952 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001953 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001954 }
drhf9b596e2004-05-26 16:54:42 +00001955 return n;
drh268380c2004-02-25 13:47:31 +00001956}
1957
1958/*
drhcce7d172000-05-31 15:34:51 +00001959** Generate code for a boolean expression such that a jump is made
1960** to the label "dest" if the expression is true but execution
1961** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001962**
1963** If the expression evaluates to NULL (neither true nor false), then
1964** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001965**
1966** This code depends on the fact that certain token values (ex: TK_EQ)
1967** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1968** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1969** the make process cause these values to align. Assert()s in the code
1970** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001971*/
danielk19774adee202004-05-08 08:23:19 +00001972void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001973 Vdbe *v = pParse->pVdbe;
1974 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001975 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001976 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001977 op = pExpr->op;
1978 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001979 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001980 int d2 = sqlite3VdbeMakeLabel(v);
1981 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1982 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1983 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001984 break;
1985 }
1986 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001987 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1988 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001989 break;
1990 }
1991 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001992 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001993 break;
1994 }
1995 case TK_LT:
1996 case TK_LE:
1997 case TK_GT:
1998 case TK_GE:
1999 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002000 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002001 assert( TK_LT==OP_Lt );
2002 assert( TK_LE==OP_Le );
2003 assert( TK_GT==OP_Gt );
2004 assert( TK_GE==OP_Ge );
2005 assert( TK_EQ==OP_Eq );
2006 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002007 sqlite3ExprCode(pParse, pExpr->pLeft);
2008 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002009 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002010 break;
2011 }
2012 case TK_ISNULL:
2013 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002014 assert( TK_ISNULL==OP_IsNull );
2015 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002016 sqlite3ExprCode(pParse, pExpr->pLeft);
2017 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002018 break;
2019 }
drhfef52082000-06-06 01:50:43 +00002020 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002021 /* The expression "x BETWEEN y AND z" is implemented as:
2022 **
2023 ** 1 IF (x < y) GOTO 3
2024 ** 2 IF (x <= z) GOTO <dest>
2025 ** 3 ...
2026 */
drhf5905aa2002-05-26 20:54:33 +00002027 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002028 Expr *pLeft = pExpr->pLeft;
2029 Expr *pRight = pExpr->pList->a[0].pExpr;
2030 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002032 sqlite3ExprCode(pParse, pRight);
2033 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002034
drhbe5c89a2004-07-26 00:31:09 +00002035 pRight = pExpr->pList->a[1].pExpr;
2036 sqlite3ExprCode(pParse, pRight);
2037 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002038
danielk19774adee202004-05-08 08:23:19 +00002039 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002040 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002041 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002042 break;
2043 }
drhcce7d172000-05-31 15:34:51 +00002044 default: {
danielk19774adee202004-05-08 08:23:19 +00002045 sqlite3ExprCode(pParse, pExpr);
2046 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002047 break;
2048 }
2049 }
drhffe07b22005-11-03 00:41:17 +00002050 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002051}
2052
2053/*
drh66b89c82000-11-28 20:47:17 +00002054** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002055** to the label "dest" if the expression is false but execution
2056** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002057**
2058** If the expression evaluates to NULL (neither true nor false) then
2059** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002060*/
danielk19774adee202004-05-08 08:23:19 +00002061void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002062 Vdbe *v = pParse->pVdbe;
2063 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002064 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002065 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002066
2067 /* The value of pExpr->op and op are related as follows:
2068 **
2069 ** pExpr->op op
2070 ** --------- ----------
2071 ** TK_ISNULL OP_NotNull
2072 ** TK_NOTNULL OP_IsNull
2073 ** TK_NE OP_Eq
2074 ** TK_EQ OP_Ne
2075 ** TK_GT OP_Le
2076 ** TK_LE OP_Gt
2077 ** TK_GE OP_Lt
2078 ** TK_LT OP_Ge
2079 **
2080 ** For other values of pExpr->op, op is undefined and unused.
2081 ** The value of TK_ and OP_ constants are arranged such that we
2082 ** can compute the mapping above using the following expression.
2083 ** Assert()s verify that the computation is correct.
2084 */
2085 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2086
2087 /* Verify correct alignment of TK_ and OP_ constants
2088 */
2089 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2090 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2091 assert( pExpr->op!=TK_NE || op==OP_Eq );
2092 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2093 assert( pExpr->op!=TK_LT || op==OP_Ge );
2094 assert( pExpr->op!=TK_LE || op==OP_Gt );
2095 assert( pExpr->op!=TK_GT || op==OP_Le );
2096 assert( pExpr->op!=TK_GE || op==OP_Lt );
2097
drhcce7d172000-05-31 15:34:51 +00002098 switch( pExpr->op ){
2099 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002100 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2101 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002102 break;
2103 }
2104 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002105 int d2 = sqlite3VdbeMakeLabel(v);
2106 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2107 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2108 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002109 break;
2110 }
2111 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002112 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002113 break;
2114 }
2115 case TK_LT:
2116 case TK_LE:
2117 case TK_GT:
2118 case TK_GE:
2119 case TK_NE:
2120 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002121 sqlite3ExprCode(pParse, pExpr->pLeft);
2122 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002123 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002124 break;
2125 }
drhcce7d172000-05-31 15:34:51 +00002126 case TK_ISNULL:
2127 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002128 sqlite3ExprCode(pParse, pExpr->pLeft);
2129 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002130 break;
2131 }
drhfef52082000-06-06 01:50:43 +00002132 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002133 /* The expression is "x BETWEEN y AND z". It is implemented as:
2134 **
2135 ** 1 IF (x >= y) GOTO 3
2136 ** 2 GOTO <dest>
2137 ** 3 IF (x > z) GOTO <dest>
2138 */
drhfef52082000-06-06 01:50:43 +00002139 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002140 Expr *pLeft = pExpr->pLeft;
2141 Expr *pRight = pExpr->pList->a[0].pExpr;
2142 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002143 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002144 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002145 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002146 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2147
danielk19774adee202004-05-08 08:23:19 +00002148 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2149 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002150 pRight = pExpr->pList->a[1].pExpr;
2151 sqlite3ExprCode(pParse, pRight);
2152 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002153 break;
2154 }
drhcce7d172000-05-31 15:34:51 +00002155 default: {
danielk19774adee202004-05-08 08:23:19 +00002156 sqlite3ExprCode(pParse, pExpr);
2157 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002158 break;
2159 }
2160 }
drhffe07b22005-11-03 00:41:17 +00002161 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002162}
drh22827922000-06-06 17:27:05 +00002163
2164/*
2165** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2166** if they are identical and return FALSE if they differ in any way.
2167*/
danielk19774adee202004-05-08 08:23:19 +00002168int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002169 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002170 if( pA==0||pB==0 ){
2171 return pB==pA;
drh22827922000-06-06 17:27:05 +00002172 }
2173 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002174 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002175 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2176 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002177 if( pA->pList ){
2178 if( pB->pList==0 ) return 0;
2179 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2180 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002181 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002182 return 0;
2183 }
2184 }
2185 }else if( pB->pList ){
2186 return 0;
2187 }
2188 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002189 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002190 if( pA->token.z ){
2191 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002192 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002193 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2194 return 0;
2195 }
drh22827922000-06-06 17:27:05 +00002196 }
2197 return 1;
2198}
2199
drh13449892005-09-07 21:22:45 +00002200
drh22827922000-06-06 17:27:05 +00002201/*
drh13449892005-09-07 21:22:45 +00002202** Add a new element to the pAggInfo->aCol[] array. Return the index of
2203** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002204*/
drh13449892005-09-07 21:22:45 +00002205static int addAggInfoColumn(AggInfo *pInfo){
2206 int i;
2207 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2208 if( i<0 ){
2209 return -1;
drh22827922000-06-06 17:27:05 +00002210 }
drh13449892005-09-07 21:22:45 +00002211 return i;
2212}
2213
2214/*
2215** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2216** the new element. Return a negative number if malloc fails.
2217*/
2218static int addAggInfoFunc(AggInfo *pInfo){
2219 int i;
2220 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2221 if( i<0 ){
2222 return -1;
2223 }
2224 return i;
2225}
drh22827922000-06-06 17:27:05 +00002226
2227/*
drh626a8792005-01-17 22:08:19 +00002228** This is an xFunc for walkExprTree() used to implement
2229** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2230** for additional information.
drh22827922000-06-06 17:27:05 +00002231**
drh626a8792005-01-17 22:08:19 +00002232** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002233*/
drh626a8792005-01-17 22:08:19 +00002234static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002235 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002236 NameContext *pNC = (NameContext *)pArg;
2237 Parse *pParse = pNC->pParse;
2238 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002239 AggInfo *pAggInfo = pNC->pAggInfo;
2240
drh22827922000-06-06 17:27:05 +00002241
drh22827922000-06-06 17:27:05 +00002242 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002243 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002244 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002245 /* Check to see if the column is in one of the tables in the FROM
2246 ** clause of the aggregate query */
2247 if( pSrcList ){
2248 struct SrcList_item *pItem = pSrcList->a;
2249 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2250 struct AggInfo_col *pCol;
2251 if( pExpr->iTable==pItem->iCursor ){
2252 /* If we reach this point, it means that pExpr refers to a table
2253 ** that is in the FROM clause of the aggregate query.
2254 **
2255 ** Make an entry for the column in pAggInfo->aCol[] if there
2256 ** is not an entry there already.
2257 */
2258 pCol = pAggInfo->aCol;
2259 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2260 if( pCol->iTable==pExpr->iTable &&
2261 pCol->iColumn==pExpr->iColumn ){
2262 break;
2263 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002264 }
drh13449892005-09-07 21:22:45 +00002265 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2266 pCol = &pAggInfo->aCol[i];
2267 pCol->iTable = pExpr->iTable;
2268 pCol->iColumn = pExpr->iColumn;
2269 pCol->iMem = pParse->nMem++;
2270 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002271 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002272 if( pAggInfo->pGroupBy ){
2273 int j, n;
2274 ExprList *pGB = pAggInfo->pGroupBy;
2275 struct ExprList_item *pTerm = pGB->a;
2276 n = pGB->nExpr;
2277 for(j=0; j<n; j++, pTerm++){
2278 Expr *pE = pTerm->pExpr;
2279 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2280 pE->iColumn==pExpr->iColumn ){
2281 pCol->iSorterColumn = j;
2282 break;
2283 }
2284 }
2285 }
2286 if( pCol->iSorterColumn<0 ){
2287 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2288 }
2289 }
2290 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2291 ** because it was there before or because we just created it).
2292 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2293 ** pAggInfo->aCol[] entry.
2294 */
2295 pExpr->pAggInfo = pAggInfo;
2296 pExpr->op = TK_AGG_COLUMN;
2297 pExpr->iAgg = i;
2298 break;
2299 } /* endif pExpr->iTable==pItem->iCursor */
2300 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002301 }
drh626a8792005-01-17 22:08:19 +00002302 return 1;
drh22827922000-06-06 17:27:05 +00002303 }
2304 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002305 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2306 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002307 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002308 /* Check to see if pExpr is a duplicate of another aggregate
2309 ** function that is already in the pAggInfo structure
2310 */
2311 struct AggInfo_func *pItem = pAggInfo->aFunc;
2312 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2313 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002314 break;
2315 }
drh22827922000-06-06 17:27:05 +00002316 }
drh13449892005-09-07 21:22:45 +00002317 if( i>=pAggInfo->nFunc ){
2318 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2319 */
danielk197714db2662006-01-09 16:12:04 +00002320 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002321 i = addAggInfoFunc(pAggInfo);
2322 if( i>=0 ){
2323 pItem = &pAggInfo->aFunc[i];
2324 pItem->pExpr = pExpr;
2325 pItem->iMem = pParse->nMem++;
2326 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002327 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002328 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002329 if( pExpr->flags & EP_Distinct ){
2330 pItem->iDistinct = pParse->nTab++;
2331 }else{
2332 pItem->iDistinct = -1;
2333 }
drh13449892005-09-07 21:22:45 +00002334 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002335 }
drh13449892005-09-07 21:22:45 +00002336 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2337 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002338 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002339 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002340 return 1;
drh22827922000-06-06 17:27:05 +00002341 }
drh22827922000-06-06 17:27:05 +00002342 }
2343 }
drh13449892005-09-07 21:22:45 +00002344
2345 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2346 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2347 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2348 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002349 if( pExpr->pSelect ){
2350 pNC->nDepth++;
2351 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2352 pNC->nDepth--;
2353 }
drh626a8792005-01-17 22:08:19 +00002354 return 0;
2355}
2356
2357/*
2358** Analyze the given expression looking for aggregate functions and
2359** for variables that need to be added to the pParse->aAgg[] array.
2360** Make additional entries to the pParse->aAgg[] array as necessary.
2361**
2362** This routine should only be called after the expression has been
2363** analyzed by sqlite3ExprResolveNames().
2364**
2365** If errors are seen, leave an error message in zErrMsg and return
2366** the number of errors.
2367*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002368int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2369 int nErr = pNC->pParse->nErr;
2370 walkExprTree(pExpr, analyzeAggregate, pNC);
2371 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002372}
drh5d9a4af2005-08-30 00:54:01 +00002373
2374/*
2375** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2376** expression list. Return the number of errors.
2377**
2378** If an error is found, the analysis is cut short.
2379*/
2380int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2381 struct ExprList_item *pItem;
2382 int i;
2383 int nErr = 0;
2384 if( pList ){
2385 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2386 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2387 }
2388 }
2389 return nErr;
2390}