blob: 4f21800e55ca6c4636124d52475aa3023d9da23d [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**
drh288d37f2005-06-22 08:48:06 +000015** $Id: expr.c,v 1.207 2005/06/22 08:48:06 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh626a8792005-01-17 22:08:19 +000065** pExpr is an operand of a comparison operator. aff2 is the
66** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000067** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000141 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000186 /* When malloc fails, delete pLeft and pRight. Expressions passed to
187 ** this function must always be allocated with sqlite3Expr() for this
188 ** reason.
189 */
190 sqlite3ExprDelete(pLeft);
191 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000192 return 0;
193 }
194 pNew->op = op;
195 pNew->pLeft = pLeft;
196 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000197 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000198 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000199 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000200 pNew->span = pNew->token = *pToken;
201 }else if( pLeft && pRight ){
202 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000203 }
drha76b5df2002-02-23 02:32:10 +0000204 return pNew;
205}
206
207/*
drh4e0cff62004-11-05 05:10:28 +0000208** When doing a nested parse, you can include terms in an expression
209** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000210** on the stack. "#0" means the top of the stack.
211** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000212**
213** This routine is called by the parser to deal with on of those terms.
214** It immediately generates code to store the value in a memory location.
215** The returns an expression that will code to extract the value from
216** that memory location as needed.
217*/
218Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
219 Vdbe *v = pParse->pVdbe;
220 Expr *p;
221 int depth;
222 if( v==0 ) return 0;
223 if( pParse->nested==0 ){
224 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
225 return 0;
226 }
227 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000228 if( p==0 ){
229 return 0; /* Malloc failed */
230 }
drh4e0cff62004-11-05 05:10:28 +0000231 depth = atoi(&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000232 p->iTable = pParse->nMem++;
233 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
234 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000235 return p;
236}
237
238/*
drh91bb0ee2004-09-01 03:06:34 +0000239** Join two expressions using an AND operator. If either expression is
240** NULL, then just return the other expression.
241*/
242Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
243 if( pLeft==0 ){
244 return pRight;
245 }else if( pRight==0 ){
246 return pLeft;
247 }else{
248 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
249 }
250}
251
252/*
drh6977fea2002-10-22 23:38:04 +0000253** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000254** text between the two given tokens.
255*/
danielk19774adee202004-05-08 08:23:19 +0000256void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000257 assert( pRight!=0 );
258 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000259 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000260 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000261 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000262 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000263 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000264 }else{
drh6977fea2002-10-22 23:38:04 +0000265 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000266 }
drha76b5df2002-02-23 02:32:10 +0000267 }
268}
269
270/*
271** Construct a new expression node for a function with multiple
272** arguments.
273*/
danielk19774adee202004-05-08 08:23:19 +0000274Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000275 Expr *pNew;
276 pNew = sqliteMalloc( sizeof(Expr) );
277 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000278 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000279 return 0;
280 }
281 pNew->op = TK_FUNCTION;
282 pNew->pList = pList;
283 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000284 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000285 pNew->token = *pToken;
286 }else{
287 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000288 }
drh6977fea2002-10-22 23:38:04 +0000289 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000290 return pNew;
291}
292
293/*
drhfa6bc002004-09-07 16:19:52 +0000294** Assign a variable number to an expression that encodes a wildcard
295** in the original SQL statement.
296**
297** Wildcards consisting of a single "?" are assigned the next sequential
298** variable number.
299**
300** Wildcards of the form "?nnn" are assigned the number "nnn". We make
301** sure "nnn" is not too be to avoid a denial of service attack when
302** the SQL statement comes from an external source.
303**
304** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
305** as the previous instance of the same wildcard. Or if this is the first
306** instance of the wildcard, the next sequenial variable number is
307** assigned.
308*/
309void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
310 Token *pToken;
311 if( pExpr==0 ) return;
312 pToken = &pExpr->token;
313 assert( pToken->n>=1 );
314 assert( pToken->z!=0 );
315 assert( pToken->z[0]!=0 );
316 if( pToken->n==1 ){
317 /* Wildcard of the form "?". Assign the next variable number */
318 pExpr->iTable = ++pParse->nVar;
319 }else if( pToken->z[0]=='?' ){
320 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
321 ** use it as the variable number */
322 int i;
323 pExpr->iTable = i = atoi(&pToken->z[1]);
324 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
325 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
326 SQLITE_MAX_VARIABLE_NUMBER);
327 }
328 if( i>pParse->nVar ){
329 pParse->nVar = i;
330 }
331 }else{
332 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
333 ** number as the prior appearance of the same name, or if the name
334 ** has never appeared before, reuse the same variable number
335 */
336 int i, n;
337 n = pToken->n;
338 for(i=0; i<pParse->nVarExpr; i++){
339 Expr *pE;
340 if( (pE = pParse->apVarExpr[i])!=0
341 && pE->token.n==n
342 && memcmp(pE->token.z, pToken->z, n)==0 ){
343 pExpr->iTable = pE->iTable;
344 break;
345 }
346 }
347 if( i>=pParse->nVarExpr ){
348 pExpr->iTable = ++pParse->nVar;
349 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
350 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
351 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
352 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
353 }
354 if( !sqlite3_malloc_failed ){
355 assert( pParse->apVarExpr!=0 );
356 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
357 }
358 }
359 }
360}
361
362/*
drha2e00042002-01-22 03:13:42 +0000363** Recursively delete an expression tree.
364*/
danielk19774adee202004-05-08 08:23:19 +0000365void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000366 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000367 if( p->span.dyn ) sqliteFree((char*)p->span.z);
368 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3ExprDelete(p->pLeft);
370 sqlite3ExprDelete(p->pRight);
371 sqlite3ExprListDelete(p->pList);
372 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000373 sqliteFree(p);
374}
375
drha76b5df2002-02-23 02:32:10 +0000376
377/*
drhff78bd22002-02-27 01:47:11 +0000378** The following group of routines make deep copies of expressions,
379** expression lists, ID lists, and select statements. The copies can
380** be deleted (by being passed to their respective ...Delete() routines)
381** without effecting the originals.
382**
danielk19774adee202004-05-08 08:23:19 +0000383** The expression list, ID, and source lists return by sqlite3ExprListDup(),
384** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000385** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000386**
drhad3cab52002-05-24 02:04:32 +0000387** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000388*/
danielk19774adee202004-05-08 08:23:19 +0000389Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000390 Expr *pNew;
391 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000392 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000393 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000394 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000395 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000396 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000397 pNew->token.dyn = 1;
398 }else{
drh4efc4752004-01-16 15:55:37 +0000399 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000400 }
drh6977fea2002-10-22 23:38:04 +0000401 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000402 pNew->pLeft = sqlite3ExprDup(p->pLeft);
403 pNew->pRight = sqlite3ExprDup(p->pRight);
404 pNew->pList = sqlite3ExprListDup(p->pList);
405 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000406 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000407 return pNew;
408}
danielk19774adee202004-05-08 08:23:19 +0000409void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000410 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000411 if( pFrom->z ){
412 pTo->n = pFrom->n;
413 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
414 pTo->dyn = 1;
415 }else{
drh4b59ab52002-08-24 18:24:51 +0000416 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000417 }
418}
danielk19774adee202004-05-08 08:23:19 +0000419ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000420 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000421 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000422 int i;
423 if( p==0 ) return 0;
424 pNew = sqliteMalloc( sizeof(*pNew) );
425 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000426 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000427 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000428 if( pItem==0 ){
429 sqliteFree(pNew);
430 return 0;
431 }
drh145716b2004-09-24 12:24:06 +0000432 pOldItem = p->a;
433 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000434 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000435 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000436 if( pOldExpr->span.z!=0 && pNewExpr ){
437 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000438 ** expression list. The logic in SELECT processing that determines
439 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000440 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000441 }
drh1f3e9052002-10-31 00:09:39 +0000442 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000443 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000444 pItem->zName = sqliteStrDup(pOldItem->zName);
445 pItem->sortOrder = pOldItem->sortOrder;
446 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000447 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000448 }
449 return pNew;
450}
danielk197793758c82005-01-21 08:13:14 +0000451
452/*
453** If cursors, triggers, views and subqueries are all omitted from
454** the build, then none of the following routines, except for
455** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
456** called with a NULL argument.
457*/
danielk19776a67fe82005-02-04 04:07:16 +0000458#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
459 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000460SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000461 SrcList *pNew;
462 int i;
drh113088e2003-03-20 01:16:58 +0000463 int nByte;
drhad3cab52002-05-24 02:04:32 +0000464 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000465 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000466 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000467 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000468 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000469 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000470 struct SrcList_item *pNewItem = &pNew->a[i];
471 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000472 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000473 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
474 pNewItem->zName = sqliteStrDup(pOldItem->zName);
475 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
476 pNewItem->jointype = pOldItem->jointype;
477 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000478 pTab = pNewItem->pTab = pOldItem->pTab;
479 if( pTab ){
480 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000481 }
danielk19774adee202004-05-08 08:23:19 +0000482 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
483 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
484 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000485 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000486 }
487 return pNew;
488}
danielk19774adee202004-05-08 08:23:19 +0000489IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000490 IdList *pNew;
491 int i;
492 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000493 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000494 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000495 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000496 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000497 if( pNew->a==0 ){
498 sqliteFree(pNew);
499 return 0;
500 }
drhff78bd22002-02-27 01:47:11 +0000501 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000502 struct IdList_item *pNewItem = &pNew->a[i];
503 struct IdList_item *pOldItem = &p->a[i];
504 pNewItem->zName = sqliteStrDup(pOldItem->zName);
505 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000506 }
507 return pNew;
508}
danielk19774adee202004-05-08 08:23:19 +0000509Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000510 Select *pNew;
511 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000512 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000513 if( pNew==0 ) return 0;
514 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000515 pNew->pEList = sqlite3ExprListDup(p->pEList);
516 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
517 pNew->pWhere = sqlite3ExprDup(p->pWhere);
518 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
519 pNew->pHaving = sqlite3ExprDup(p->pHaving);
520 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000521 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000522 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000523 pNew->pLimit = sqlite3ExprDup(p->pLimit);
524 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000525 pNew->iLimit = -1;
526 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000527 pNew->ppOpenTemp = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000528 pNew->isResolved = p->isResolved;
529 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000530 return pNew;
531}
danielk197793758c82005-01-21 08:13:14 +0000532#else
533Select *sqlite3SelectDup(Select *p){
534 assert( p==0 );
535 return 0;
536}
537#endif
drhff78bd22002-02-27 01:47:11 +0000538
539
540/*
drha76b5df2002-02-23 02:32:10 +0000541** Add a new element to the end of an expression list. If pList is
542** initially NULL, then create a new expression list.
543*/
danielk19774adee202004-05-08 08:23:19 +0000544ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000545 if( pList==0 ){
546 pList = sqliteMalloc( sizeof(ExprList) );
547 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000548 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000549 }
drh4efc4752004-01-16 15:55:37 +0000550 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000551 }
drh4305d102003-07-30 12:34:12 +0000552 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000553 struct ExprList_item *a;
554 int n = pList->nAlloc*2 + 4;
555 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
556 if( a==0 ){
557 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000558 }
danielk1977d5d56522005-03-16 12:15:20 +0000559 pList->a = a;
560 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000561 }
drh4efc4752004-01-16 15:55:37 +0000562 assert( pList->a!=0 );
563 if( pExpr || pName ){
564 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
565 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000566 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000567 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000568 }
569 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000570
571no_mem:
572 /* Avoid leaking memory if malloc has failed. */
573 sqlite3ExprDelete(pExpr);
574 sqlite3ExprListDelete(pList);
575 return 0;
drha76b5df2002-02-23 02:32:10 +0000576}
577
578/*
579** Delete an entire expression list.
580*/
danielk19774adee202004-05-08 08:23:19 +0000581void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000582 int i;
drhbe5c89a2004-07-26 00:31:09 +0000583 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000584 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000585 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
586 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000587 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
588 sqlite3ExprDelete(pItem->pExpr);
589 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000590 }
591 sqliteFree(pList->a);
592 sqliteFree(pList);
593}
594
595/*
drh626a8792005-01-17 22:08:19 +0000596** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000597**
drh626a8792005-01-17 22:08:19 +0000598** The return value from xFunc determines whether the tree walk continues.
599** 0 means continue walking the tree. 1 means do not walk children
600** of the current node but continue with siblings. 2 means abandon
601** the tree walk completely.
602**
603** The return value from this routine is 1 to abandon the tree walk
604** and 0 to continue.
605*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000606static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000607static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000608 int rc;
609 if( pExpr==0 ) return 0;
610 rc = (*xFunc)(pArg, pExpr);
611 if( rc==0 ){
612 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
613 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000614 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000615 }
616 return rc>1;
617}
618
619/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000620** Call walkExprTree() for every expression in list p.
621*/
622static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
623 int i;
624 struct ExprList_item *pItem;
625 if( !p ) return 0;
626 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
627 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
628 }
629 return 0;
630}
631
632/*
633** Call walkExprTree() for every expression in Select p, not including
634** expressions that are part of sub-selects in any FROM clause or the LIMIT
635** or OFFSET expressions..
636*/
637static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
638 walkExprList(p->pEList, xFunc, pArg);
639 walkExprTree(p->pWhere, xFunc, pArg);
640 walkExprList(p->pGroupBy, xFunc, pArg);
641 walkExprTree(p->pHaving, xFunc, pArg);
642 walkExprList(p->pOrderBy, xFunc, pArg);
643 return 0;
644}
645
646
647/*
drh626a8792005-01-17 22:08:19 +0000648** This routine is designed as an xFunc for walkExprTree().
649**
650** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000651** at pExpr that the expression that contains pExpr is not a constant
652** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
653** If pExpr does does not disqualify the expression from being a constant
654** then do nothing.
655**
656** After walking the whole tree, if no nodes are found that disqualify
657** the expression as constant, then we assume the whole expression
658** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000659*/
660static int exprNodeIsConstant(void *pArg, Expr *pExpr){
661 switch( pExpr->op ){
662 case TK_ID:
663 case TK_COLUMN:
664 case TK_DOT:
665 case TK_AGG_FUNCTION:
666 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000667#ifndef SQLITE_OMIT_SUBQUERY
668 case TK_SELECT:
669 case TK_EXISTS:
670#endif
drh626a8792005-01-17 22:08:19 +0000671 *((int*)pArg) = 0;
672 return 2;
673 default:
674 return 0;
675 }
676}
677
678/*
drhfef52082000-06-06 01:50:43 +0000679** Walk an expression tree. Return 1 if the expression is constant
680** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000681**
682** For the purposes of this function, a double-quoted string (ex: "abc")
683** is considered a variable but a single-quoted string (ex: 'abc') is
684** a constant.
drhfef52082000-06-06 01:50:43 +0000685*/
danielk19774adee202004-05-08 08:23:19 +0000686int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000687 int isConst = 1;
688 walkExprTree(p, exprNodeIsConstant, &isConst);
689 return isConst;
drhfef52082000-06-06 01:50:43 +0000690}
691
692/*
drh73b211a2005-01-18 04:00:42 +0000693** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000694** to fit in a 32-bit integer, return 1 and put the value of the integer
695** in *pValue. If the expression is not an integer or if it is too big
696** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000697*/
danielk19774adee202004-05-08 08:23:19 +0000698int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000699 switch( p->op ){
700 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000701 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000702 return 1;
703 }
704 break;
drhe4de1fe2002-06-02 16:09:01 +0000705 }
drh4b59ab52002-08-24 18:24:51 +0000706 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000707 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000708 }
drhe4de1fe2002-06-02 16:09:01 +0000709 case TK_UMINUS: {
710 int v;
danielk19774adee202004-05-08 08:23:19 +0000711 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000712 *pValue = -v;
713 return 1;
714 }
715 break;
716 }
717 default: break;
718 }
719 return 0;
720}
721
722/*
drhc4a3c772001-04-04 11:48:57 +0000723** Return TRUE if the given string is a row-id column name.
724*/
danielk19774adee202004-05-08 08:23:19 +0000725int sqlite3IsRowid(const char *z){
726 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
727 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
728 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000729 return 0;
730}
731
732/*
drh8141f612004-01-25 22:44:58 +0000733** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
734** that name in the set of source tables in pSrcList and make the pExpr
735** expression node refer back to that source column. The following changes
736** are made to pExpr:
737**
738** pExpr->iDb Set the index in db->aDb[] of the database holding
739** the table.
740** pExpr->iTable Set to the cursor number for the table obtained
741** from pSrcList.
742** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000743** pExpr->op Set to TK_COLUMN.
744** pExpr->pLeft Any expression this points to is deleted
745** pExpr->pRight Any expression this points to is deleted.
746**
747** The pDbToken is the name of the database (the "X"). This value may be
748** NULL meaning that name is of the form Y.Z or Z. Any available database
749** can be used. The pTableToken is the name of the table (the "Y"). This
750** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
751** means that the form of the name is Z and that columns from any table
752** can be used.
753**
754** If the name cannot be resolved unambiguously, leave an error message
755** in pParse and return non-zero. Return zero on success.
756*/
757static int lookupName(
758 Parse *pParse, /* The parsing context */
759 Token *pDbToken, /* Name of the database containing table, or NULL */
760 Token *pTableToken, /* Name of table containing column, or NULL */
761 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000762 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000763 Expr *pExpr /* Make this EXPR node point to the selected column */
764){
765 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
766 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
767 char *zCol = 0; /* Name of the column. The "Z" */
768 int i, j; /* Loop counters */
769 int cnt = 0; /* Number of matching column names */
770 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000771 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000772 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
773 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000774 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000775
776 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000777 zDb = sqlite3NameFromToken(pDbToken);
778 zTab = sqlite3NameFromToken(pTableToken);
779 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000780 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000781 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000782 }
drh8141f612004-01-25 22:44:58 +0000783
784 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000785 while( pNC && cnt==0 ){
786 SrcList *pSrcList = pNC->pSrcList;
787 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000788
drh626a8792005-01-17 22:08:19 +0000789 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000790 if( pSrcList ){
791 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
792 Table *pTab = pItem->pTab;
793 Column *pCol;
794
795 if( pTab==0 ) continue;
796 assert( pTab->nCol>0 );
797 if( zTab ){
798 if( pItem->zAlias ){
799 char *zTabName = pItem->zAlias;
800 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
801 }else{
802 char *zTabName = pTab->zName;
803 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
804 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
805 continue;
806 }
drh626a8792005-01-17 22:08:19 +0000807 }
drh8141f612004-01-25 22:44:58 +0000808 }
danielk1977b3bce662005-01-29 08:32:43 +0000809 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000810 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000811 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000812 pMatch = pItem;
813 }
814 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
815 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000816 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000817 cnt++;
818 pExpr->iTable = pItem->iCursor;
819 pMatch = pItem;
820 pExpr->iDb = pTab->iDb;
821 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
822 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
823 pExpr->affinity = pTab->aCol[j].affinity;
824 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000825 if( pItem->jointype & JT_NATURAL ){
826 /* If this match occurred in the left table of a natural join,
827 ** then skip the right table to avoid a duplicate match */
828 pItem++;
829 i++;
830 }
drh873fac02005-06-06 17:11:46 +0000831 if( (pUsing = pItem->pUsing)!=0 ){
832 /* If this match occurs on a column that is in the USING clause
833 ** of a join, skip the search of the right table of the join
834 ** to avoid a duplicate match there. */
835 int k;
836 for(k=0; k<pUsing->nId; k++){
837 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
838 pItem++;
839 i++;
840 break;
841 }
842 }
843 }
danielk1977b3bce662005-01-29 08:32:43 +0000844 break;
845 }
drh8141f612004-01-25 22:44:58 +0000846 }
847 }
848 }
drh626a8792005-01-17 22:08:19 +0000849
850#ifndef SQLITE_OMIT_TRIGGER
851 /* If we have not already resolved the name, then maybe
852 ** it is a new.* or old.* trigger argument reference
853 */
854 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
855 TriggerStack *pTriggerStack = pParse->trigStack;
856 Table *pTab = 0;
857 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
858 pExpr->iTable = pTriggerStack->newIdx;
859 assert( pTriggerStack->pTab );
860 pTab = pTriggerStack->pTab;
861 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
862 pExpr->iTable = pTriggerStack->oldIdx;
863 assert( pTriggerStack->pTab );
864 pTab = pTriggerStack->pTab;
865 }
866
867 if( pTab ){
868 int j;
869 Column *pCol = pTab->aCol;
870
871 pExpr->iDb = pTab->iDb;
872 cntTab++;
873 for(j=0; j < pTab->nCol; j++, pCol++) {
874 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
875 cnt++;
876 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
877 pExpr->affinity = pTab->aCol[j].affinity;
878 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000879 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000880 break;
881 }
882 }
883 }
884 }
drhb7f91642004-10-31 02:22:47 +0000885#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000886
drh626a8792005-01-17 22:08:19 +0000887 /*
888 ** Perhaps the name is a reference to the ROWID
889 */
890 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
891 cnt = 1;
892 pExpr->iColumn = -1;
893 pExpr->affinity = SQLITE_AFF_INTEGER;
894 }
drh8141f612004-01-25 22:44:58 +0000895
drh626a8792005-01-17 22:08:19 +0000896 /*
897 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
898 ** might refer to an result-set alias. This happens, for example, when
899 ** we are resolving names in the WHERE clause of the following command:
900 **
901 ** SELECT a+b AS x FROM table WHERE x<10;
902 **
903 ** In cases like this, replace pExpr with a copy of the expression that
904 ** forms the result set entry ("a+b" in the example) and return immediately.
905 ** Note that the expression in the result set should have already been
906 ** resolved by the time the WHERE clause is resolved.
907 */
drh79d5f632005-01-18 17:20:10 +0000908 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000909 for(j=0; j<pEList->nExpr; j++){
910 char *zAs = pEList->a[j].zName;
911 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
912 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
913 pExpr->op = TK_AS;
914 pExpr->iColumn = j;
915 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000916 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000917 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000918 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000919 }
920 }
921 }
922
923 /* Advance to the next name context. The loop will exit when either
924 ** we have a match (cnt>0) or when we run out of name contexts.
925 */
926 if( cnt==0 ){
927 pNC = pNC->pNext;
928 }
drh8141f612004-01-25 22:44:58 +0000929 }
930
931 /*
932 ** If X and Y are NULL (in other words if only the column name Z is
933 ** supplied) and the value of Z is enclosed in double-quotes, then
934 ** Z is a string literal if it doesn't match any column names. In that
935 ** case, we need to return right away and not make any changes to
936 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000937 **
938 ** Because no reference was made to outer contexts, the pNC->nRef
939 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000940 */
941 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
942 sqliteFree(zCol);
943 return 0;
944 }
945
946 /*
947 ** cnt==0 means there was not match. cnt>1 means there were two or
948 ** more matches. Either way, we have an error.
949 */
950 if( cnt!=1 ){
951 char *z = 0;
952 char *zErr;
953 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
954 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000955 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000956 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000957 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000958 }else{
959 z = sqliteStrDup(zCol);
960 }
danielk19774adee202004-05-08 08:23:19 +0000961 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000962 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000963 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000964 }
965
drh51669862004-12-18 18:40:26 +0000966 /* If a column from a table in pSrcList is referenced, then record
967 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
968 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
969 ** column number is greater than the number of bits in the bitmask
970 ** then set the high-order bit of the bitmask.
971 */
972 if( pExpr->iColumn>=0 && pMatch!=0 ){
973 int n = pExpr->iColumn;
974 if( n>=sizeof(Bitmask)*8 ){
975 n = sizeof(Bitmask)*8-1;
976 }
977 assert( pMatch->iCursor==pExpr->iTable );
978 pMatch->colUsed |= 1<<n;
979 }
980
danielk1977d5d56522005-03-16 12:15:20 +0000981lookupname_end:
drh8141f612004-01-25 22:44:58 +0000982 /* Clean up and return
983 */
984 sqliteFree(zDb);
985 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +0000986 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000987 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000989 pExpr->pRight = 0;
990 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +0000991lookupname_end_2:
992 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +0000993 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000994 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000995 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +0000996 if( pMatch && !pMatch->pSelect ){
997 pExpr->pTab = pMatch->pTab;
998 }
drh15ccce12005-05-23 15:06:39 +0000999 /* Increment the nRef value on all name contexts from TopNC up to
1000 ** the point where the name matched. */
1001 for(;;){
1002 assert( pTopNC!=0 );
1003 pTopNC->nRef++;
1004 if( pTopNC==pNC ) break;
1005 pTopNC = pTopNC->pNext;
1006 }
1007 return 0;
1008 } else {
1009 return 1;
drh626a8792005-01-17 22:08:19 +00001010 }
drh8141f612004-01-25 22:44:58 +00001011}
1012
1013/*
drh626a8792005-01-17 22:08:19 +00001014** This routine is designed as an xFunc for walkExprTree().
1015**
drh73b211a2005-01-18 04:00:42 +00001016** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001017** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001018** the tree or 2 to abort the tree walk.
1019**
1020** This routine also does error checking and name resolution for
1021** function names. The operator for aggregate functions is changed
1022** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001023*/
1024static int nameResolverStep(void *pArg, Expr *pExpr){
1025 NameContext *pNC = (NameContext*)pArg;
1026 SrcList *pSrcList;
1027 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001028
danielk1977b3bce662005-01-29 08:32:43 +00001029 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001030 assert( pNC!=0 );
1031 pSrcList = pNC->pSrcList;
1032 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001033
drh626a8792005-01-17 22:08:19 +00001034 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1035 ExprSetProperty(pExpr, EP_Resolved);
1036#ifndef NDEBUG
1037 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001038 int i;
drh626a8792005-01-17 22:08:19 +00001039 for(i=0; i<pSrcList->nSrc; i++){
1040 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1041 }
1042 }
1043#endif
1044 switch( pExpr->op ){
1045 /* Double-quoted strings (ex: "abc") are used as identifiers if
1046 ** possible. Otherwise they remain as strings. Single-quoted
1047 ** strings (ex: 'abc') are always string literals.
1048 */
1049 case TK_STRING: {
1050 if( pExpr->token.z[0]=='\'' ) break;
1051 /* Fall thru into the TK_ID case if this is a double-quoted string */
1052 }
1053 /* A lone identifier is the name of a column.
1054 */
1055 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001056 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1057 return 1;
1058 }
1059
1060 /* A table name and column name: ID.ID
1061 ** Or a database, table and column: ID.ID.ID
1062 */
1063 case TK_DOT: {
1064 Token *pColumn;
1065 Token *pTable;
1066 Token *pDb;
1067 Expr *pRight;
1068
danielk1977b3bce662005-01-29 08:32:43 +00001069 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001070 pRight = pExpr->pRight;
1071 if( pRight->op==TK_ID ){
1072 pDb = 0;
1073 pTable = &pExpr->pLeft->token;
1074 pColumn = &pRight->token;
1075 }else{
1076 assert( pRight->op==TK_DOT );
1077 pDb = &pExpr->pLeft->token;
1078 pTable = &pRight->pLeft->token;
1079 pColumn = &pRight->pRight->token;
1080 }
1081 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1082 return 1;
1083 }
1084
1085 /* Resolve function names
1086 */
drhb71090f2005-05-23 17:26:51 +00001087 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001088 case TK_FUNCTION: {
1089 ExprList *pList = pExpr->pList; /* The argument list */
1090 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1091 int no_such_func = 0; /* True if no such function exists */
1092 int wrong_num_args = 0; /* True if wrong number of arguments */
1093 int is_agg = 0; /* True if is an aggregate function */
1094 int i;
1095 int nId; /* Number of characters in function name */
1096 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001097 FuncDef *pDef; /* Information about the function */
1098 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001099
drhb71090f2005-05-23 17:26:51 +00001100 zId = pExpr->token.z;
1101 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001102 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1103 if( pDef==0 ){
1104 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1105 if( pDef==0 ){
1106 no_such_func = 1;
1107 }else{
1108 wrong_num_args = 1;
1109 }
1110 }else{
1111 is_agg = pDef->xFunc==0;
1112 }
1113 if( is_agg && !pNC->allowAgg ){
1114 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1115 pNC->nErr++;
1116 is_agg = 0;
1117 }else if( no_such_func ){
1118 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1119 pNC->nErr++;
1120 }else if( wrong_num_args ){
1121 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1122 nId, zId);
1123 pNC->nErr++;
1124 }
1125 if( is_agg ){
1126 pExpr->op = TK_AGG_FUNCTION;
1127 pNC->hasAgg = 1;
1128 }
drh73b211a2005-01-18 04:00:42 +00001129 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001130 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001131 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001132 }
drh73b211a2005-01-18 04:00:42 +00001133 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001134 /* FIX ME: Compute pExpr->affinity based on the expected return
1135 ** type of the function
1136 */
1137 return is_agg;
1138 }
danielk1977b3bce662005-01-29 08:32:43 +00001139#ifndef SQLITE_OMIT_SUBQUERY
1140 case TK_SELECT:
1141 case TK_EXISTS:
1142#endif
1143 case TK_IN: {
1144 if( pExpr->pSelect ){
1145 int nRef = pNC->nRef;
1146 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1147 assert( pNC->nRef>=nRef );
1148 if( nRef!=pNC->nRef ){
1149 ExprSetProperty(pExpr, EP_VarSelect);
1150 }
1151 }
1152 }
drh626a8792005-01-17 22:08:19 +00001153 }
1154 return 0;
1155}
1156
1157/*
drhcce7d172000-05-31 15:34:51 +00001158** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001159** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001160** index to the table in the table list and a column offset. The
1161** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1162** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001163** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001164** VDBE cursor number for a cursor that is pointing into the referenced
1165** table. The Expr.iColumn value is changed to the index of the column
1166** of the referenced table. The Expr.iColumn value for the special
1167** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1168** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001169**
drh626a8792005-01-17 22:08:19 +00001170** Also resolve function names and check the functions for proper
1171** usage. Make sure all function names are recognized and all functions
1172** have the correct number of arguments. Leave an error message
1173** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1174**
drh73b211a2005-01-18 04:00:42 +00001175** If the expression contains aggregate functions then set the EP_Agg
1176** property on the expression.
drh626a8792005-01-17 22:08:19 +00001177*/
1178int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001179 NameContext *pNC, /* Namespace to resolve expressions in. */
1180 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001181){
drh73b211a2005-01-18 04:00:42 +00001182 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001183 walkExprTree(pExpr, nameResolverStep, pNC);
1184 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001185 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001186 }
1187 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001188}
1189
drh1398ad32005-01-19 23:24:50 +00001190/*
1191** A pointer instance of this structure is used to pass information
1192** through walkExprTree into codeSubqueryStep().
1193*/
1194typedef struct QueryCoder QueryCoder;
1195struct QueryCoder {
1196 Parse *pParse; /* The parsing context */
1197 NameContext *pNC; /* Namespace of first enclosing query */
1198};
1199
drh626a8792005-01-17 22:08:19 +00001200
1201/*
1202** Generate code for subqueries and IN operators.
1203**
drh73b211a2005-01-18 04:00:42 +00001204** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001205**
1206** expr IN (exprlist)
1207** and
1208** expr IN (SELECT ...)
1209**
1210** The first form is handled by creating a set holding the list
1211** of allowed values. The second form causes the SELECT to generate
1212** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001213*/
drh51522cd2005-01-20 13:36:19 +00001214#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001215void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1216 int label = 0; /* Address after sub-select code */
1217 Vdbe *v = sqlite3GetVdbe(pParse);
1218 if( v==0 ) return;
1219
1220 /* If this is not a variable (correlated) select, then execute
1221 ** it only once. Unless this is part of a trigger program. In
1222 ** that case re-execute every time (this could be optimized).
1223 */
1224 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1225 int mem = pParse->nMem++;
1226 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1227 label = sqlite3VdbeMakeLabel(v);
1228 sqlite3VdbeAddOp(v, OP_If, 0, label);
1229 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1230 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1231 }
1232
1233 if( pExpr->pSelect ){
1234 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1235 }
drh6a3ea0e2003-05-02 14:32:12 +00001236
drhcce7d172000-05-31 15:34:51 +00001237 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001238 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001239 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001240 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001241 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001242
danielk1977bf3b7212004-05-18 10:06:24 +00001243 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001244
1245 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1246 ** expression it is handled the same way. A temporary table is
1247 ** filled with single-field index keys representing the results
1248 ** from the SELECT or the <exprlist>.
1249 **
1250 ** If the 'x' expression is a column value, or the SELECT...
1251 ** statement returns a column value, then the affinity of that
1252 ** column is used to build the index keys. If both 'x' and the
1253 ** SELECT... statement are columns, then numeric affinity is used
1254 ** if either column has NUMERIC or INTEGER affinity. If neither
1255 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1256 ** is used.
1257 */
1258 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001259 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001260 memset(&keyInfo, 0, sizeof(keyInfo));
1261 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001262 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001263
drhfef52082000-06-06 01:50:43 +00001264 if( pExpr->pSelect ){
1265 /* Case 1: expr IN (SELECT ...)
1266 **
danielk1977e014a832004-05-17 10:48:57 +00001267 ** Generate code to write the results of the select into the temporary
1268 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001269 */
danielk1977e014a832004-05-17 10:48:57 +00001270 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001271 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001272 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001273 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001274 pEList = pExpr->pSelect->pEList;
1275 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001276 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001277 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001278 }
drhfef52082000-06-06 01:50:43 +00001279 }else if( pExpr->pList ){
1280 /* Case 2: expr IN (exprlist)
1281 **
danielk1977e014a832004-05-17 10:48:57 +00001282 ** For each expression, build an index key from the evaluation and
1283 ** store it in the temporary table. If <expr> is a column, then use
1284 ** that columns affinity when building index keys. If <expr> is not
1285 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001286 */
danielk1977e014a832004-05-17 10:48:57 +00001287 int i;
danielk1977e014a832004-05-17 10:48:57 +00001288 if( !affinity ){
1289 affinity = SQLITE_AFF_NUMERIC;
1290 }
danielk19770202b292004-06-09 09:55:16 +00001291 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001292
1293 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001294 for(i=0; i<pExpr->pList->nExpr; i++){
1295 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001296
1297 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001298 if( !sqlite3ExprIsConstant(pE2) ){
1299 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001300 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001301 return;
drh4794b982000-06-06 13:54:14 +00001302 }
danielk1977e014a832004-05-17 10:48:57 +00001303
1304 /* Evaluate the expression and insert it into the temp table */
1305 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001306 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001307 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001308 }
1309 }
danielk19770202b292004-06-09 09:55:16 +00001310 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001311 break;
drhfef52082000-06-06 01:50:43 +00001312 }
1313
drh51522cd2005-01-20 13:36:19 +00001314 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001315 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001316 /* This has to be a scalar SELECT. Generate code to put the
1317 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001318 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001319 */
drh51522cd2005-01-20 13:36:19 +00001320 int sop;
1321 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001322
drh967e8b72000-06-21 13:59:10 +00001323 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001324 pSel = pExpr->pSelect;
1325 if( pExpr->op==TK_SELECT ){
1326 sop = SRT_Mem;
1327 }else{
1328 static const Token one = { "1", 0, 1 };
1329 sop = SRT_Exists;
1330 sqlite3ExprListDelete(pSel->pEList);
1331 pSel->pEList = sqlite3ExprListAppend(0,
1332 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1333 }
danielk1977b3bce662005-01-29 08:32:43 +00001334 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1335 break;
drhcce7d172000-05-31 15:34:51 +00001336 }
1337 }
danielk1977b3bce662005-01-29 08:32:43 +00001338
1339 if( pExpr->pSelect ){
1340 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1341 }
1342 if( label<0 ){
1343 sqlite3VdbeResolveLabel(v, label);
1344 }
1345 return;
drhcce7d172000-05-31 15:34:51 +00001346}
drh51522cd2005-01-20 13:36:19 +00001347#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001348
drhcce7d172000-05-31 15:34:51 +00001349/*
drhfec19aa2004-05-19 20:41:03 +00001350** Generate an instruction that will put the integer describe by
1351** text z[0..n-1] on the stack.
1352*/
1353static void codeInteger(Vdbe *v, const char *z, int n){
1354 int i;
drh6fec0762004-05-30 01:38:43 +00001355 if( sqlite3GetInt32(z, &i) ){
1356 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1357 }else if( sqlite3FitsIn64Bits(z) ){
1358 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001359 }else{
1360 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1361 }
1362}
1363
1364/*
drhcce7d172000-05-31 15:34:51 +00001365** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001366** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001367**
1368** This code depends on the fact that certain token values (ex: TK_EQ)
1369** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1370** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1371** the make process cause these values to align. Assert()s in the code
1372** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001373*/
danielk19774adee202004-05-08 08:23:19 +00001374void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001375 Vdbe *v = pParse->pVdbe;
1376 int op;
danielk19777977a172004-11-09 12:44:37 +00001377 if( v==0 ) return;
1378 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001379 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001380 return;
1381 }
drhf2bc0132004-10-04 13:19:23 +00001382 op = pExpr->op;
1383 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001384 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001385 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1386 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001387 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001389 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001390 }else{
drhf0863fe2005-06-12 21:35:51 +00001391 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001392 }
drhcce7d172000-05-31 15:34:51 +00001393 break;
1394 }
1395 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001396 codeInteger(v, pExpr->token.z, pExpr->token.n);
1397 break;
1398 }
1399 case TK_FLOAT:
1400 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001401 assert( TK_FLOAT==OP_Real );
1402 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001403 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001404 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001405 break;
1406 }
drhf0863fe2005-06-12 21:35:51 +00001407 case TK_NULL: {
1408 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1409 break;
1410 }
danielk19775338a5f2005-01-20 13:03:10 +00001411#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001412 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001413 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001414 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1415 sqlite3VdbeDequoteP3(v, -1);
1416 break;
1417 }
danielk19775338a5f2005-01-20 13:03:10 +00001418#endif
drh50457892003-09-06 01:10:47 +00001419 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001420 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001421 if( pExpr->token.n>1 ){
1422 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1423 }
drh50457892003-09-06 01:10:47 +00001424 break;
1425 }
drh4e0cff62004-11-05 05:10:28 +00001426 case TK_REGISTER: {
1427 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1428 break;
1429 }
drhc9b84a12002-06-20 11:36:48 +00001430 case TK_LT:
1431 case TK_LE:
1432 case TK_GT:
1433 case TK_GE:
1434 case TK_NE:
1435 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001436 assert( TK_LT==OP_Lt );
1437 assert( TK_LE==OP_Le );
1438 assert( TK_GT==OP_Gt );
1439 assert( TK_GE==OP_Ge );
1440 assert( TK_EQ==OP_Eq );
1441 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001442 sqlite3ExprCode(pParse, pExpr->pLeft);
1443 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001444 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001445 break;
drhc9b84a12002-06-20 11:36:48 +00001446 }
drhcce7d172000-05-31 15:34:51 +00001447 case TK_AND:
1448 case TK_OR:
1449 case TK_PLUS:
1450 case TK_STAR:
1451 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001452 case TK_REM:
1453 case TK_BITAND:
1454 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001455 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001456 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001457 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001458 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001459 assert( TK_AND==OP_And );
1460 assert( TK_OR==OP_Or );
1461 assert( TK_PLUS==OP_Add );
1462 assert( TK_MINUS==OP_Subtract );
1463 assert( TK_REM==OP_Remainder );
1464 assert( TK_BITAND==OP_BitAnd );
1465 assert( TK_BITOR==OP_BitOr );
1466 assert( TK_SLASH==OP_Divide );
1467 assert( TK_LSHIFT==OP_ShiftLeft );
1468 assert( TK_RSHIFT==OP_ShiftRight );
1469 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3ExprCode(pParse, pExpr->pLeft);
1471 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001472 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001473 break;
1474 }
drhcce7d172000-05-31 15:34:51 +00001475 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001476 Expr *pLeft = pExpr->pLeft;
1477 assert( pLeft );
1478 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1479 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001480 char *z = sqliteMalloc( p->n + 2 );
1481 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001482 if( pLeft->op==TK_FLOAT ){
1483 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001484 }else{
drhfec19aa2004-05-19 20:41:03 +00001485 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001486 }
drh6e142f52000-06-08 13:36:40 +00001487 sqliteFree(z);
1488 break;
1489 }
drh1ccde152000-06-17 13:12:39 +00001490 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001491 }
drhbf4133c2001-10-13 02:59:08 +00001492 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001493 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001494 assert( TK_BITNOT==OP_BitNot );
1495 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001496 sqlite3ExprCode(pParse, pExpr->pLeft);
1497 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001498 break;
1499 }
1500 case TK_ISNULL:
1501 case TK_NOTNULL: {
1502 int dest;
drhf2bc0132004-10-04 13:19:23 +00001503 assert( TK_ISNULL==OP_IsNull );
1504 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1506 sqlite3ExprCode(pParse, pExpr->pLeft);
1507 dest = sqlite3VdbeCurrentAddr(v) + 2;
1508 sqlite3VdbeAddOp(v, op, 1, dest);
1509 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001510 break;
drhcce7d172000-05-31 15:34:51 +00001511 }
drh22827922000-06-06 17:27:05 +00001512 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001513 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001514 break;
1515 }
drhb71090f2005-05-23 17:26:51 +00001516 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001517 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001518 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001519 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001520 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001521 int nId;
1522 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001523 int p2 = 0;
1524 int i;
danielk1977d8123362004-06-12 09:25:12 +00001525 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001526 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001527 zId = pExpr->token.z;
1528 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001529 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001530 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001531 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001532 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001533 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1534 p2 |= (1<<i);
1535 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001536 if( pDef->needCollSeq && !pColl ){
1537 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1538 }
1539 }
1540 if( pDef->needCollSeq ){
1541 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001542 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001543 }
1544 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001545 break;
1546 }
drhfe2093d2005-01-20 22:48:47 +00001547#ifndef SQLITE_OMIT_SUBQUERY
1548 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001549 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001550 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001551 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001552 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001553 break;
1554 }
drhfef52082000-06-06 01:50:43 +00001555 case TK_IN: {
1556 int addr;
drh94a11212004-09-25 13:12:14 +00001557 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001558 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001559
1560 /* Figure out the affinity to use to create a key from the results
1561 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001562 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001563 */
drh94a11212004-09-25 13:12:14 +00001564 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001565
danielk19774adee202004-05-08 08:23:19 +00001566 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001567
1568 /* Code the <expr> from "<expr> IN (...)". The temporary table
1569 ** pExpr->iTable contains the values that make up the (...) set.
1570 */
danielk19774adee202004-05-08 08:23:19 +00001571 sqlite3ExprCode(pParse, pExpr->pLeft);
1572 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001573 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001574 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001575 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001576 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001577 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001578 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1579 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1580
drhfef52082000-06-06 01:50:43 +00001581 break;
1582 }
danielk197793758c82005-01-21 08:13:14 +00001583#endif
drhfef52082000-06-06 01:50:43 +00001584 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001585 Expr *pLeft = pExpr->pLeft;
1586 struct ExprList_item *pLItem = pExpr->pList->a;
1587 Expr *pRight = pLItem->pExpr;
1588 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001589 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001590 sqlite3ExprCode(pParse, pRight);
1591 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001593 pLItem++;
1594 pRight = pLItem->pExpr;
1595 sqlite3ExprCode(pParse, pRight);
1596 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001598 break;
1599 }
drh51e9a442004-01-16 16:42:53 +00001600 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001601 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001602 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001603 break;
1604 }
drh17a7f8d2002-03-24 13:13:27 +00001605 case TK_CASE: {
1606 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001607 int jumpInst;
1608 int addr;
1609 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001610 int i;
drhbe5c89a2004-07-26 00:31:09 +00001611 ExprList *pEList;
1612 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001613
1614 assert(pExpr->pList);
1615 assert((pExpr->pList->nExpr % 2) == 0);
1616 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001617 pEList = pExpr->pList;
1618 aListelem = pEList->a;
1619 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001620 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001621 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001622 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001623 }
drhf5905aa2002-05-26 20:54:33 +00001624 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001625 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001626 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001627 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001628 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1629 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001630 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001631 }else{
danielk19774adee202004-05-08 08:23:19 +00001632 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001633 }
drhbe5c89a2004-07-26 00:31:09 +00001634 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001635 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1636 addr = sqlite3VdbeCurrentAddr(v);
1637 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001638 }
drhf570f012002-05-31 15:51:25 +00001639 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001640 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001641 }
drh17a7f8d2002-03-24 13:13:27 +00001642 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001643 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001644 }else{
drhf0863fe2005-06-12 21:35:51 +00001645 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001646 }
danielk19774adee202004-05-08 08:23:19 +00001647 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001648 break;
1649 }
danielk19775338a5f2005-01-20 13:03:10 +00001650#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001651 case TK_RAISE: {
1652 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001653 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001654 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001655 return;
1656 }
drhad6d9462004-09-19 02:15:24 +00001657 if( pExpr->iColumn!=OE_Ignore ){
1658 assert( pExpr->iColumn==OE_Rollback ||
1659 pExpr->iColumn == OE_Abort ||
1660 pExpr->iColumn == OE_Fail );
1661 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1662 pExpr->token.z, pExpr->token.n);
1663 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001664 } else {
drhad6d9462004-09-19 02:15:24 +00001665 assert( pExpr->iColumn == OE_Ignore );
1666 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1667 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1668 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001669 }
drh17a7f8d2002-03-24 13:13:27 +00001670 }
danielk19775338a5f2005-01-20 13:03:10 +00001671#endif
drh17a7f8d2002-03-24 13:13:27 +00001672 break;
drhcce7d172000-05-31 15:34:51 +00001673 }
drhcce7d172000-05-31 15:34:51 +00001674}
1675
danielk197793758c82005-01-21 08:13:14 +00001676#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001677/*
drh25303782004-12-07 15:41:48 +00001678** Generate code that evalutes the given expression and leaves the result
1679** on the stack. See also sqlite3ExprCode().
1680**
1681** This routine might also cache the result and modify the pExpr tree
1682** so that it will make use of the cached result on subsequent evaluations
1683** rather than evaluate the whole expression again. Trivial expressions are
1684** not cached. If the expression is cached, its result is stored in a
1685** memory location.
1686*/
1687void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1688 Vdbe *v = pParse->pVdbe;
1689 int iMem;
1690 int addr1, addr2;
1691 if( v==0 ) return;
1692 addr1 = sqlite3VdbeCurrentAddr(v);
1693 sqlite3ExprCode(pParse, pExpr);
1694 addr2 = sqlite3VdbeCurrentAddr(v);
1695 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1696 iMem = pExpr->iTable = pParse->nMem++;
1697 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1698 pExpr->op = TK_REGISTER;
1699 }
1700}
danielk197793758c82005-01-21 08:13:14 +00001701#endif
drh25303782004-12-07 15:41:48 +00001702
1703/*
drh268380c2004-02-25 13:47:31 +00001704** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001705** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001706**
1707** Return the number of elements pushed onto the stack.
1708*/
danielk19774adee202004-05-08 08:23:19 +00001709int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001710 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001711 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001712){
1713 struct ExprList_item *pItem;
1714 int i, n;
1715 Vdbe *v;
1716 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001717 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001718 n = pList->nExpr;
1719 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001720 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001721 }
drhf9b596e2004-05-26 16:54:42 +00001722 return n;
drh268380c2004-02-25 13:47:31 +00001723}
1724
1725/*
drhcce7d172000-05-31 15:34:51 +00001726** Generate code for a boolean expression such that a jump is made
1727** to the label "dest" if the expression is true but execution
1728** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001729**
1730** If the expression evaluates to NULL (neither true nor false), then
1731** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001732**
1733** This code depends on the fact that certain token values (ex: TK_EQ)
1734** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1735** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1736** the make process cause these values to align. Assert()s in the code
1737** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001738*/
danielk19774adee202004-05-08 08:23:19 +00001739void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001740 Vdbe *v = pParse->pVdbe;
1741 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001742 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001743 op = pExpr->op;
1744 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001745 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001746 int d2 = sqlite3VdbeMakeLabel(v);
1747 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1748 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1749 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001750 break;
1751 }
1752 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001753 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1754 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001755 break;
1756 }
1757 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001758 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001759 break;
1760 }
1761 case TK_LT:
1762 case TK_LE:
1763 case TK_GT:
1764 case TK_GE:
1765 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001766 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001767 assert( TK_LT==OP_Lt );
1768 assert( TK_LE==OP_Le );
1769 assert( TK_GT==OP_Gt );
1770 assert( TK_GE==OP_Ge );
1771 assert( TK_EQ==OP_Eq );
1772 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001773 sqlite3ExprCode(pParse, pExpr->pLeft);
1774 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001775 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001776 break;
1777 }
1778 case TK_ISNULL:
1779 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001780 assert( TK_ISNULL==OP_IsNull );
1781 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001782 sqlite3ExprCode(pParse, pExpr->pLeft);
1783 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001784 break;
1785 }
drhfef52082000-06-06 01:50:43 +00001786 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001787 /* The expression "x BETWEEN y AND z" is implemented as:
1788 **
1789 ** 1 IF (x < y) GOTO 3
1790 ** 2 IF (x <= z) GOTO <dest>
1791 ** 3 ...
1792 */
drhf5905aa2002-05-26 20:54:33 +00001793 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001794 Expr *pLeft = pExpr->pLeft;
1795 Expr *pRight = pExpr->pList->a[0].pExpr;
1796 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001797 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001798 sqlite3ExprCode(pParse, pRight);
1799 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001800
drhbe5c89a2004-07-26 00:31:09 +00001801 pRight = pExpr->pList->a[1].pExpr;
1802 sqlite3ExprCode(pParse, pRight);
1803 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001804
danielk19774adee202004-05-08 08:23:19 +00001805 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1806 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1807 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001808 break;
1809 }
drhcce7d172000-05-31 15:34:51 +00001810 default: {
danielk19774adee202004-05-08 08:23:19 +00001811 sqlite3ExprCode(pParse, pExpr);
1812 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001813 break;
1814 }
1815 }
1816}
1817
1818/*
drh66b89c82000-11-28 20:47:17 +00001819** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001820** to the label "dest" if the expression is false but execution
1821** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001822**
1823** If the expression evaluates to NULL (neither true nor false) then
1824** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001825*/
danielk19774adee202004-05-08 08:23:19 +00001826void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001827 Vdbe *v = pParse->pVdbe;
1828 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001829 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001830
1831 /* The value of pExpr->op and op are related as follows:
1832 **
1833 ** pExpr->op op
1834 ** --------- ----------
1835 ** TK_ISNULL OP_NotNull
1836 ** TK_NOTNULL OP_IsNull
1837 ** TK_NE OP_Eq
1838 ** TK_EQ OP_Ne
1839 ** TK_GT OP_Le
1840 ** TK_LE OP_Gt
1841 ** TK_GE OP_Lt
1842 ** TK_LT OP_Ge
1843 **
1844 ** For other values of pExpr->op, op is undefined and unused.
1845 ** The value of TK_ and OP_ constants are arranged such that we
1846 ** can compute the mapping above using the following expression.
1847 ** Assert()s verify that the computation is correct.
1848 */
1849 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1850
1851 /* Verify correct alignment of TK_ and OP_ constants
1852 */
1853 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1854 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1855 assert( pExpr->op!=TK_NE || op==OP_Eq );
1856 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1857 assert( pExpr->op!=TK_LT || op==OP_Ge );
1858 assert( pExpr->op!=TK_LE || op==OP_Gt );
1859 assert( pExpr->op!=TK_GT || op==OP_Le );
1860 assert( pExpr->op!=TK_GE || op==OP_Lt );
1861
drhcce7d172000-05-31 15:34:51 +00001862 switch( pExpr->op ){
1863 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001864 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1865 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001866 break;
1867 }
1868 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001869 int d2 = sqlite3VdbeMakeLabel(v);
1870 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1871 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1872 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001873 break;
1874 }
1875 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001876 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001877 break;
1878 }
1879 case TK_LT:
1880 case TK_LE:
1881 case TK_GT:
1882 case TK_GE:
1883 case TK_NE:
1884 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001885 sqlite3ExprCode(pParse, pExpr->pLeft);
1886 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001887 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001888 break;
1889 }
drhcce7d172000-05-31 15:34:51 +00001890 case TK_ISNULL:
1891 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001892 sqlite3ExprCode(pParse, pExpr->pLeft);
1893 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001894 break;
1895 }
drhfef52082000-06-06 01:50:43 +00001896 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001897 /* The expression is "x BETWEEN y AND z". It is implemented as:
1898 **
1899 ** 1 IF (x >= y) GOTO 3
1900 ** 2 GOTO <dest>
1901 ** 3 IF (x > z) GOTO <dest>
1902 */
drhfef52082000-06-06 01:50:43 +00001903 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001904 Expr *pLeft = pExpr->pLeft;
1905 Expr *pRight = pExpr->pList->a[0].pExpr;
1906 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001907 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001908 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001909 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001910 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1911
danielk19774adee202004-05-08 08:23:19 +00001912 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1913 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001914 pRight = pExpr->pList->a[1].pExpr;
1915 sqlite3ExprCode(pParse, pRight);
1916 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001917 break;
1918 }
drhcce7d172000-05-31 15:34:51 +00001919 default: {
danielk19774adee202004-05-08 08:23:19 +00001920 sqlite3ExprCode(pParse, pExpr);
1921 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001922 break;
1923 }
1924 }
1925}
drh22827922000-06-06 17:27:05 +00001926
1927/*
1928** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1929** if they are identical and return FALSE if they differ in any way.
1930*/
danielk19774adee202004-05-08 08:23:19 +00001931int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001932 int i;
1933 if( pA==0 ){
1934 return pB==0;
1935 }else if( pB==0 ){
1936 return 0;
1937 }
1938 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001939 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1940 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001941 if( pA->pList ){
1942 if( pB->pList==0 ) return 0;
1943 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1944 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001945 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001946 return 0;
1947 }
1948 }
1949 }else if( pB->pList ){
1950 return 0;
1951 }
1952 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001953 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001954 if( pA->token.z ){
1955 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001956 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001957 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001958 }
1959 return 1;
1960}
1961
1962/*
1963** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001964** The new element is initialized to zero. The calling function is
1965** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001966*/
1967static int appendAggInfo(Parse *pParse){
1968 if( (pParse->nAgg & 0x7)==0 ){
1969 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001970 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1971 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001972 return -1;
1973 }
drh6d4abfb2001-10-22 02:58:08 +00001974 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001975 }
1976 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1977 return pParse->nAgg++;
1978}
1979
1980/*
drh626a8792005-01-17 22:08:19 +00001981** This is an xFunc for walkExprTree() used to implement
1982** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1983** for additional information.
drh22827922000-06-06 17:27:05 +00001984**
drh626a8792005-01-17 22:08:19 +00001985** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001986*/
drh626a8792005-01-17 22:08:19 +00001987static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001988 int i;
1989 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00001990 NameContext *pNC = (NameContext *)pArg;
1991 Parse *pParse = pNC->pParse;
1992 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00001993
drh22827922000-06-06 17:27:05 +00001994 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001995 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001996 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
1997 if( pExpr->iTable==pSrcList->a[i].iCursor ){
1998 aAgg = pParse->aAgg;
1999 for(i=0; i<pParse->nAgg; i++){
2000 if( aAgg[i].isAgg ) continue;
2001 if( aAgg[i].pExpr->iTable==pExpr->iTable
2002 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2003 break;
2004 }
2005 }
2006 if( i>=pParse->nAgg ){
2007 i = appendAggInfo(pParse);
2008 if( i<0 ) return 1;
2009 pParse->aAgg[i].isAgg = 0;
2010 pParse->aAgg[i].pExpr = pExpr;
2011 }
2012 pExpr->iAgg = i;
2013 pExpr->iAggCtx = pNC->nDepth;
2014 return 1;
drh22827922000-06-06 17:27:05 +00002015 }
2016 }
drh626a8792005-01-17 22:08:19 +00002017 return 1;
drh22827922000-06-06 17:27:05 +00002018 }
2019 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002020 if( pNC->nDepth==0 ){
2021 aAgg = pParse->aAgg;
2022 for(i=0; i<pParse->nAgg; i++){
2023 if( !aAgg[i].isAgg ) continue;
2024 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2025 break;
2026 }
drh22827922000-06-06 17:27:05 +00002027 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002028 if( i>=pParse->nAgg ){
2029 u8 enc = pParse->db->enc;
2030 i = appendAggInfo(pParse);
2031 if( i<0 ) return 1;
2032 pParse->aAgg[i].isAgg = 1;
2033 pParse->aAgg[i].pExpr = pExpr;
2034 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2035 pExpr->token.z, pExpr->token.n,
2036 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2037 }
2038 pExpr->iAgg = i;
2039 return 1;
drh22827922000-06-06 17:27:05 +00002040 }
drh22827922000-06-06 17:27:05 +00002041 }
2042 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002043 if( pExpr->pSelect ){
2044 pNC->nDepth++;
2045 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2046 pNC->nDepth--;
2047 }
drh626a8792005-01-17 22:08:19 +00002048 return 0;
2049}
2050
2051/*
2052** Analyze the given expression looking for aggregate functions and
2053** for variables that need to be added to the pParse->aAgg[] array.
2054** Make additional entries to the pParse->aAgg[] array as necessary.
2055**
2056** This routine should only be called after the expression has been
2057** analyzed by sqlite3ExprResolveNames().
2058**
2059** If errors are seen, leave an error message in zErrMsg and return
2060** the number of errors.
2061*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002062int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2063 int nErr = pNC->pParse->nErr;
2064 walkExprTree(pExpr, analyzeAggregate, pNC);
2065 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002066}