blob: 9808af930ad3a6580e2f2ca7936fa6aee6a2ceb8 [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**
danielk19777a15a4b2007-05-08 17:54:43 +000015** $Id: expr.c,v 1.286 2007/05/08 17:54:44 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
drh8b4c40d2007-02-01 23:02:45 +000053** Set the collating sequence for expression pExpr to be the collating
54** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000055** The collating sequence is marked as "explicit" using the EP_ExpCollate
56** flag. An explicit collating sequence will override implicit
57** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000058*/
59Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
60 CollSeq *pColl;
61 if( pExpr==0 ) return 0;
62 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
63 if( pColl ){
64 pExpr->pColl = pColl;
65 pExpr->flags |= EP_ExpCollate;
66 }
67 return pExpr;
68}
69
70/*
danielk19770202b292004-06-09 09:55:16 +000071** Return the default collation sequence for the expression pExpr. If
72** there is no default collation type, return 0.
73*/
danielk19777cedc8d2004-06-10 10:50:08 +000074CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
75 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000076 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000077 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000078 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000079 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000080 }
81 }
danielk19777cedc8d2004-06-10 10:50:08 +000082 if( sqlite3CheckCollSeq(pParse, pColl) ){
83 pColl = 0;
84 }
85 return pColl;
danielk19770202b292004-06-09 09:55:16 +000086}
87
88/*
drh626a8792005-01-17 22:08:19 +000089** pExpr is an operand of a comparison operator. aff2 is the
90** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000091** type affinity that should be used for the comparison operator.
92*/
danielk1977e014a832004-05-17 10:48:57 +000093char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000094 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000095 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000096 /* Both sides of the comparison are columns. If one has numeric
97 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh8a512562005-11-14 22:29:05 +000099 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000100 return SQLITE_AFF_NUMERIC;
101 }else{
102 return SQLITE_AFF_NONE;
103 }
104 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000105 /* Neither side of the comparison is a column. Compare the
106 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000107 */
drh5f6a87b2004-07-19 00:39:45 +0000108 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000109 }else{
110 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000111 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000112 return (aff1 + aff2);
113 }
114}
115
drh53db1452004-05-20 13:54:53 +0000116/*
117** pExpr is a comparison operator. Return the type affinity that should
118** be applied to both operands prior to doing the comparison.
119*/
danielk1977e014a832004-05-17 10:48:57 +0000120static char comparisonAffinity(Expr *pExpr){
121 char aff;
122 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
123 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
124 pExpr->op==TK_NE );
125 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000126 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000127 if( pExpr->pRight ){
128 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
129 }
130 else if( pExpr->pSelect ){
131 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
132 }
133 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000134 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000135 }
136 return aff;
137}
138
139/*
140** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
141** idx_affinity is the affinity of an indexed column. Return true
142** if the index with affinity idx_affinity may be used to implement
143** the comparison in pExpr.
144*/
145int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
146 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000147 switch( aff ){
148 case SQLITE_AFF_NONE:
149 return 1;
150 case SQLITE_AFF_TEXT:
151 return idx_affinity==SQLITE_AFF_TEXT;
152 default:
153 return sqlite3IsNumericAffinity(idx_affinity);
154 }
danielk1977e014a832004-05-17 10:48:57 +0000155}
156
danielk1977a37cdde2004-05-16 11:15:36 +0000157/*
158** Return the P1 value that should be used for a binary comparison
159** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
160** If jumpIfNull is true, then set the low byte of the returned
161** P1 value to tell the opcode to jump if either expression
162** evaluates to NULL.
163*/
danielk1977e014a832004-05-17 10:48:57 +0000164static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000165 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000166 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000167}
168
drha2e00042002-01-22 03:13:42 +0000169/*
danielk19770202b292004-06-09 09:55:16 +0000170** Return a pointer to the collation sequence that should be used by
171** a binary comparison operator comparing pLeft and pRight.
172**
173** If the left hand expression has a collating sequence type, then it is
174** used. Otherwise the collation sequence for the right hand expression
175** is used, or the default (BINARY) if neither expression has a collating
176** type.
177*/
danielk19777cedc8d2004-06-10 10:50:08 +0000178static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
drhec41dda2007-02-07 13:09:45 +0000179 CollSeq *pColl;
180 assert( pLeft );
181 assert( pRight );
182 if( pLeft->flags & EP_ExpCollate ){
183 assert( pLeft->pColl );
184 pColl = pLeft->pColl;
185 }else if( pRight->flags & EP_ExpCollate ){
186 assert( pRight->pColl );
187 pColl = pRight->pColl;
188 }else{
189 pColl = sqlite3ExprCollSeq(pParse, pLeft);
190 if( !pColl ){
191 pColl = sqlite3ExprCollSeq(pParse, pRight);
192 }
danielk19770202b292004-06-09 09:55:16 +0000193 }
194 return pColl;
195}
196
197/*
drhbe5c89a2004-07-26 00:31:09 +0000198** Generate code for a comparison operator.
199*/
200static int codeCompare(
201 Parse *pParse, /* The parsing (and code generating) context */
202 Expr *pLeft, /* The left operand */
203 Expr *pRight, /* The right operand */
204 int opcode, /* The comparison opcode */
205 int dest, /* Jump here if true. */
206 int jumpIfNull /* If true, jump if either operand is NULL */
207){
208 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
209 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000210 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000211}
212
213/*
drha76b5df2002-02-23 02:32:10 +0000214** Construct a new expression node and return a pointer to it. Memory
215** for this node is obtained from sqliteMalloc(). The calling function
216** is responsible for making sure the node eventually gets freed.
217*/
drhe4e72072004-11-23 01:47:30 +0000218Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000219 Expr *pNew;
220 pNew = sqliteMalloc( sizeof(Expr) );
221 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000222 /* When malloc fails, delete pLeft and pRight. Expressions passed to
223 ** this function must always be allocated with sqlite3Expr() for this
224 ** reason.
225 */
226 sqlite3ExprDelete(pLeft);
227 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000228 return 0;
229 }
230 pNew->op = op;
231 pNew->pLeft = pLeft;
232 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000233 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000234 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000235 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000236 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000237 }else if( pLeft ){
238 if( pRight ){
239 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000240 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000241 pNew->flags |= EP_ExpCollate;
242 pNew->pColl = pRight->pColl;
243 }
244 }
drh5ffb3ac2007-04-18 17:07:57 +0000245 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000246 pNew->flags |= EP_ExpCollate;
247 pNew->pColl = pLeft->pColl;
248 }
drha76b5df2002-02-23 02:32:10 +0000249 }
drha76b5df2002-02-23 02:32:10 +0000250 return pNew;
251}
252
253/*
drh206f3d92006-07-11 13:15:08 +0000254** Works like sqlite3Expr() but frees its pLeft and pRight arguments
255** if it fails due to a malloc problem.
256*/
257Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
258 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
259 if( pNew==0 ){
260 sqlite3ExprDelete(pLeft);
261 sqlite3ExprDelete(pRight);
262 }
263 return pNew;
264}
265
266/*
drh4e0cff62004-11-05 05:10:28 +0000267** When doing a nested parse, you can include terms in an expression
268** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000269** on the stack. "#0" means the top of the stack.
270** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000271**
272** This routine is called by the parser to deal with on of those terms.
273** It immediately generates code to store the value in a memory location.
274** The returns an expression that will code to extract the value from
275** that memory location as needed.
276*/
277Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
278 Vdbe *v = pParse->pVdbe;
279 Expr *p;
280 int depth;
drh4e0cff62004-11-05 05:10:28 +0000281 if( pParse->nested==0 ){
282 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
283 return 0;
284 }
drhbb7ac002005-08-12 22:58:53 +0000285 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000286 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000287 if( p==0 ){
288 return 0; /* Malloc failed */
289 }
drh2646da72005-12-09 20:02:05 +0000290 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000291 p->iTable = pParse->nMem++;
292 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
293 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000294 return p;
295}
296
297/*
drh91bb0ee2004-09-01 03:06:34 +0000298** Join two expressions using an AND operator. If either expression is
299** NULL, then just return the other expression.
300*/
301Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
302 if( pLeft==0 ){
303 return pRight;
304 }else if( pRight==0 ){
305 return pLeft;
306 }else{
307 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
308 }
309}
310
311/*
drh6977fea2002-10-22 23:38:04 +0000312** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000313** text between the two given tokens.
314*/
danielk19774adee202004-05-08 08:23:19 +0000315void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000316 assert( pRight!=0 );
317 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000318 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000319 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000320 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000321 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000322 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000323 }else{
drh6977fea2002-10-22 23:38:04 +0000324 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000325 }
drha76b5df2002-02-23 02:32:10 +0000326 }
327}
328
329/*
330** Construct a new expression node for a function with multiple
331** arguments.
332*/
danielk19774adee202004-05-08 08:23:19 +0000333Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000334 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000335 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000336 pNew = sqliteMalloc( sizeof(Expr) );
337 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000338 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000339 return 0;
340 }
341 pNew->op = TK_FUNCTION;
342 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000343 assert( pToken->dyn==0 );
344 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000345 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000346 return pNew;
347}
348
349/*
drhfa6bc002004-09-07 16:19:52 +0000350** Assign a variable number to an expression that encodes a wildcard
351** in the original SQL statement.
352**
353** Wildcards consisting of a single "?" are assigned the next sequential
354** variable number.
355**
356** Wildcards of the form "?nnn" are assigned the number "nnn". We make
357** sure "nnn" is not too be to avoid a denial of service attack when
358** the SQL statement comes from an external source.
359**
360** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
361** as the previous instance of the same wildcard. Or if this is the first
362** instance of the wildcard, the next sequenial variable number is
363** assigned.
364*/
365void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
366 Token *pToken;
367 if( pExpr==0 ) return;
368 pToken = &pExpr->token;
369 assert( pToken->n>=1 );
370 assert( pToken->z!=0 );
371 assert( pToken->z[0]!=0 );
372 if( pToken->n==1 ){
373 /* Wildcard of the form "?". Assign the next variable number */
374 pExpr->iTable = ++pParse->nVar;
375 }else if( pToken->z[0]=='?' ){
376 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
377 ** use it as the variable number */
378 int i;
drh2646da72005-12-09 20:02:05 +0000379 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000380 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
381 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
382 SQLITE_MAX_VARIABLE_NUMBER);
383 }
384 if( i>pParse->nVar ){
385 pParse->nVar = i;
386 }
387 }else{
388 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
389 ** number as the prior appearance of the same name, or if the name
390 ** has never appeared before, reuse the same variable number
391 */
392 int i, n;
393 n = pToken->n;
394 for(i=0; i<pParse->nVarExpr; i++){
395 Expr *pE;
396 if( (pE = pParse->apVarExpr[i])!=0
397 && pE->token.n==n
398 && memcmp(pE->token.z, pToken->z, n)==0 ){
399 pExpr->iTable = pE->iTable;
400 break;
401 }
402 }
403 if( i>=pParse->nVarExpr ){
404 pExpr->iTable = ++pParse->nVar;
405 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
406 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000407 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000408 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
409 }
danielk19779e128002006-01-18 16:51:35 +0000410 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000411 assert( pParse->apVarExpr!=0 );
412 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
413 }
414 }
415 }
416}
417
418/*
drha2e00042002-01-22 03:13:42 +0000419** Recursively delete an expression tree.
420*/
danielk19774adee202004-05-08 08:23:19 +0000421void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000422 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000423 if( p->span.dyn ) sqliteFree((char*)p->span.z);
424 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3ExprDelete(p->pLeft);
426 sqlite3ExprDelete(p->pRight);
427 sqlite3ExprListDelete(p->pList);
428 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000429 sqliteFree(p);
430}
431
drhd2687b72005-08-12 22:56:09 +0000432/*
433** The Expr.token field might be a string literal that is quoted.
434** If so, remove the quotation marks.
435*/
436void sqlite3DequoteExpr(Expr *p){
437 if( ExprHasAnyProperty(p, EP_Dequoted) ){
438 return;
439 }
440 ExprSetProperty(p, EP_Dequoted);
441 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000442 sqlite3TokenCopy(&p->token, &p->token);
443 }
444 sqlite3Dequote((char*)p->token.z);
445}
446
drha76b5df2002-02-23 02:32:10 +0000447
448/*
drhff78bd22002-02-27 01:47:11 +0000449** The following group of routines make deep copies of expressions,
450** expression lists, ID lists, and select statements. The copies can
451** be deleted (by being passed to their respective ...Delete() routines)
452** without effecting the originals.
453**
danielk19774adee202004-05-08 08:23:19 +0000454** The expression list, ID, and source lists return by sqlite3ExprListDup(),
455** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000456** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000457**
drhad3cab52002-05-24 02:04:32 +0000458** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000459*/
danielk19774adee202004-05-08 08:23:19 +0000460Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000461 Expr *pNew;
462 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000463 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000464 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000465 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000466 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000467 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000468 pNew->token.dyn = 1;
469 }else{
drh4efc4752004-01-16 15:55:37 +0000470 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000471 }
drh6977fea2002-10-22 23:38:04 +0000472 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000473 pNew->pLeft = sqlite3ExprDup(p->pLeft);
474 pNew->pRight = sqlite3ExprDup(p->pRight);
475 pNew->pList = sqlite3ExprListDup(p->pList);
476 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000477 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000478 return pNew;
479}
danielk19774adee202004-05-08 08:23:19 +0000480void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000481 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000482 if( pFrom->z ){
483 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000484 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000485 pTo->dyn = 1;
486 }else{
drh4b59ab52002-08-24 18:24:51 +0000487 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000488 }
489}
danielk19774adee202004-05-08 08:23:19 +0000490ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000491 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000492 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000493 int i;
494 if( p==0 ) return 0;
495 pNew = sqliteMalloc( sizeof(*pNew) );
496 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000497 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000498 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000499 if( pItem==0 ){
500 sqliteFree(pNew);
501 return 0;
502 }
drh145716b2004-09-24 12:24:06 +0000503 pOldItem = p->a;
504 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000505 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000506 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000507 if( pOldExpr->span.z!=0 && pNewExpr ){
508 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000509 ** expression list. The logic in SELECT processing that determines
510 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000511 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000512 }
drh1f3e9052002-10-31 00:09:39 +0000513 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000514 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000515 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000516 pItem->zName = sqliteStrDup(pOldItem->zName);
517 pItem->sortOrder = pOldItem->sortOrder;
518 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000519 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000520 }
521 return pNew;
522}
danielk197793758c82005-01-21 08:13:14 +0000523
524/*
525** If cursors, triggers, views and subqueries are all omitted from
526** the build, then none of the following routines, except for
527** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
528** called with a NULL argument.
529*/
danielk19776a67fe82005-02-04 04:07:16 +0000530#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
531 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000532SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000533 SrcList *pNew;
534 int i;
drh113088e2003-03-20 01:16:58 +0000535 int nByte;
drhad3cab52002-05-24 02:04:32 +0000536 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000537 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000538 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000539 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000540 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000541 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000542 struct SrcList_item *pNewItem = &pNew->a[i];
543 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000544 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000545 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
546 pNewItem->zName = sqliteStrDup(pOldItem->zName);
547 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
548 pNewItem->jointype = pOldItem->jointype;
549 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000550 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000551 pTab = pNewItem->pTab = pOldItem->pTab;
552 if( pTab ){
553 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000554 }
danielk19774adee202004-05-08 08:23:19 +0000555 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
556 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
557 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000558 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000559 }
560 return pNew;
561}
danielk19774adee202004-05-08 08:23:19 +0000562IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000563 IdList *pNew;
564 int i;
565 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000566 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000567 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000568 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000569 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000570 if( pNew->a==0 ){
571 sqliteFree(pNew);
572 return 0;
573 }
drhff78bd22002-02-27 01:47:11 +0000574 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000575 struct IdList_item *pNewItem = &pNew->a[i];
576 struct IdList_item *pOldItem = &p->a[i];
577 pNewItem->zName = sqliteStrDup(pOldItem->zName);
578 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000579 }
580 return pNew;
581}
danielk19774adee202004-05-08 08:23:19 +0000582Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000583 Select *pNew;
584 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000585 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000586 if( pNew==0 ) return 0;
587 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000588 pNew->pEList = sqlite3ExprListDup(p->pEList);
589 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
590 pNew->pWhere = sqlite3ExprDup(p->pWhere);
591 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
592 pNew->pHaving = sqlite3ExprDup(p->pHaving);
593 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000594 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000595 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000596 pNew->pLimit = sqlite3ExprDup(p->pLimit);
597 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000598 pNew->iLimit = -1;
599 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000600 pNew->isResolved = p->isResolved;
601 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000602 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000603 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000604 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000605 pNew->addrOpenEphm[0] = -1;
606 pNew->addrOpenEphm[1] = -1;
607 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000608 return pNew;
609}
danielk197793758c82005-01-21 08:13:14 +0000610#else
611Select *sqlite3SelectDup(Select *p){
612 assert( p==0 );
613 return 0;
614}
615#endif
drhff78bd22002-02-27 01:47:11 +0000616
617
618/*
drha76b5df2002-02-23 02:32:10 +0000619** Add a new element to the end of an expression list. If pList is
620** initially NULL, then create a new expression list.
621*/
danielk19774adee202004-05-08 08:23:19 +0000622ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000623 if( pList==0 ){
624 pList = sqliteMalloc( sizeof(ExprList) );
625 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000626 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000627 }
drh4efc4752004-01-16 15:55:37 +0000628 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000629 }
drh4305d102003-07-30 12:34:12 +0000630 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000631 struct ExprList_item *a;
632 int n = pList->nAlloc*2 + 4;
633 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
634 if( a==0 ){
635 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000636 }
danielk1977d5d56522005-03-16 12:15:20 +0000637 pList->a = a;
638 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000639 }
drh4efc4752004-01-16 15:55:37 +0000640 assert( pList->a!=0 );
641 if( pExpr || pName ){
642 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
643 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000644 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000645 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000646 }
647 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000648
649no_mem:
650 /* Avoid leaking memory if malloc has failed. */
651 sqlite3ExprDelete(pExpr);
652 sqlite3ExprListDelete(pList);
653 return 0;
drha76b5df2002-02-23 02:32:10 +0000654}
655
656/*
danielk19777a15a4b2007-05-08 17:54:43 +0000657** If the expression list pEList contains more than iLimit elements,
658** leave an error message in pParse.
659*/
660void sqlite3ExprListCheckLength(
661 Parse *pParse,
662 ExprList *pEList,
663 int iLimit,
664 const char *zObject
665){
666 if( pEList->nExpr>iLimit ){
667 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
668 }
669}
670
671/*
drha76b5df2002-02-23 02:32:10 +0000672** Delete an entire expression list.
673*/
danielk19774adee202004-05-08 08:23:19 +0000674void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000675 int i;
drhbe5c89a2004-07-26 00:31:09 +0000676 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000677 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000678 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
679 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000680 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
681 sqlite3ExprDelete(pItem->pExpr);
682 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000683 }
684 sqliteFree(pList->a);
685 sqliteFree(pList);
686}
687
688/*
drh626a8792005-01-17 22:08:19 +0000689** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000690**
drh626a8792005-01-17 22:08:19 +0000691** The return value from xFunc determines whether the tree walk continues.
692** 0 means continue walking the tree. 1 means do not walk children
693** of the current node but continue with siblings. 2 means abandon
694** the tree walk completely.
695**
696** The return value from this routine is 1 to abandon the tree walk
697** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000698**
699** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000700*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000701static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000702static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000703 int rc;
704 if( pExpr==0 ) return 0;
705 rc = (*xFunc)(pArg, pExpr);
706 if( rc==0 ){
707 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
708 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000709 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000710 }
711 return rc>1;
712}
713
714/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000715** Call walkExprTree() for every expression in list p.
716*/
717static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
718 int i;
719 struct ExprList_item *pItem;
720 if( !p ) return 0;
721 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
722 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
723 }
724 return 0;
725}
726
727/*
728** Call walkExprTree() for every expression in Select p, not including
729** expressions that are part of sub-selects in any FROM clause or the LIMIT
730** or OFFSET expressions..
731*/
732static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
733 walkExprList(p->pEList, xFunc, pArg);
734 walkExprTree(p->pWhere, xFunc, pArg);
735 walkExprList(p->pGroupBy, xFunc, pArg);
736 walkExprTree(p->pHaving, xFunc, pArg);
737 walkExprList(p->pOrderBy, xFunc, pArg);
738 return 0;
739}
740
741
742/*
drh626a8792005-01-17 22:08:19 +0000743** This routine is designed as an xFunc for walkExprTree().
744**
745** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000746** at pExpr that the expression that contains pExpr is not a constant
747** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
748** If pExpr does does not disqualify the expression from being a constant
749** then do nothing.
750**
751** After walking the whole tree, if no nodes are found that disqualify
752** the expression as constant, then we assume the whole expression
753** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000754*/
755static int exprNodeIsConstant(void *pArg, Expr *pExpr){
756 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000757 /* Consider functions to be constant if all their arguments are constant
758 ** and *pArg==2 */
759 case TK_FUNCTION:
760 if( *((int*)pArg)==2 ) return 0;
761 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000762 case TK_ID:
763 case TK_COLUMN:
764 case TK_DOT:
765 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000766 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000767#ifndef SQLITE_OMIT_SUBQUERY
768 case TK_SELECT:
769 case TK_EXISTS:
770#endif
drh626a8792005-01-17 22:08:19 +0000771 *((int*)pArg) = 0;
772 return 2;
drh87abf5c2005-08-25 12:45:04 +0000773 case TK_IN:
774 if( pExpr->pSelect ){
775 *((int*)pArg) = 0;
776 return 2;
777 }
drh626a8792005-01-17 22:08:19 +0000778 default:
779 return 0;
780 }
781}
782
783/*
drhfef52082000-06-06 01:50:43 +0000784** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000785** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000786**
787** For the purposes of this function, a double-quoted string (ex: "abc")
788** is considered a variable but a single-quoted string (ex: 'abc') is
789** a constant.
drhfef52082000-06-06 01:50:43 +0000790*/
danielk19774adee202004-05-08 08:23:19 +0000791int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000792 int isConst = 1;
793 walkExprTree(p, exprNodeIsConstant, &isConst);
794 return isConst;
drhfef52082000-06-06 01:50:43 +0000795}
796
797/*
drheb55bd22005-06-30 17:04:21 +0000798** Walk an expression tree. Return 1 if the expression is constant
799** or a function call with constant arguments. Return and 0 if there
800** are any variables.
801**
802** For the purposes of this function, a double-quoted string (ex: "abc")
803** is considered a variable but a single-quoted string (ex: 'abc') is
804** a constant.
805*/
806int sqlite3ExprIsConstantOrFunction(Expr *p){
807 int isConst = 2;
808 walkExprTree(p, exprNodeIsConstant, &isConst);
809 return isConst!=0;
810}
811
812/*
drh73b211a2005-01-18 04:00:42 +0000813** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000814** to fit in a 32-bit integer, return 1 and put the value of the integer
815** in *pValue. If the expression is not an integer or if it is too big
816** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000817*/
danielk19774adee202004-05-08 08:23:19 +0000818int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000819 switch( p->op ){
820 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000821 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000822 return 1;
823 }
824 break;
drhe4de1fe2002-06-02 16:09:01 +0000825 }
drh4b59ab52002-08-24 18:24:51 +0000826 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000827 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000828 }
drhe4de1fe2002-06-02 16:09:01 +0000829 case TK_UMINUS: {
830 int v;
danielk19774adee202004-05-08 08:23:19 +0000831 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000832 *pValue = -v;
833 return 1;
834 }
835 break;
836 }
837 default: break;
838 }
839 return 0;
840}
841
842/*
drhc4a3c772001-04-04 11:48:57 +0000843** Return TRUE if the given string is a row-id column name.
844*/
danielk19774adee202004-05-08 08:23:19 +0000845int sqlite3IsRowid(const char *z){
846 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
847 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
848 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000849 return 0;
850}
851
852/*
drh8141f612004-01-25 22:44:58 +0000853** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
854** that name in the set of source tables in pSrcList and make the pExpr
855** expression node refer back to that source column. The following changes
856** are made to pExpr:
857**
858** pExpr->iDb Set the index in db->aDb[] of the database holding
859** the table.
860** pExpr->iTable Set to the cursor number for the table obtained
861** from pSrcList.
862** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000863** pExpr->op Set to TK_COLUMN.
864** pExpr->pLeft Any expression this points to is deleted
865** pExpr->pRight Any expression this points to is deleted.
866**
867** The pDbToken is the name of the database (the "X"). This value may be
868** NULL meaning that name is of the form Y.Z or Z. Any available database
869** can be used. The pTableToken is the name of the table (the "Y"). This
870** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
871** means that the form of the name is Z and that columns from any table
872** can be used.
873**
874** If the name cannot be resolved unambiguously, leave an error message
875** in pParse and return non-zero. Return zero on success.
876*/
877static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000878 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000879 Token *pDbToken, /* Name of the database containing table, or NULL */
880 Token *pTableToken, /* Name of table containing column, or NULL */
881 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000882 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000883 Expr *pExpr /* Make this EXPR node point to the selected column */
884){
885 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
886 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
887 char *zCol = 0; /* Name of the column. The "Z" */
888 int i, j; /* Loop counters */
889 int cnt = 0; /* Number of matching column names */
890 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000891 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000892 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
893 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000894 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000895
896 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000897 zDb = sqlite3NameFromToken(pDbToken);
898 zTab = sqlite3NameFromToken(pTableToken);
899 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000900 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000901 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000902 }
drh8141f612004-01-25 22:44:58 +0000903
904 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000905 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000906 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000907 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000908
danielk1977b3bce662005-01-29 08:32:43 +0000909 if( pSrcList ){
910 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000911 Table *pTab;
912 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000913 Column *pCol;
914
drh43617e92006-03-06 20:55:46 +0000915 pTab = pItem->pTab;
916 assert( pTab!=0 );
917 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000918 assert( pTab->nCol>0 );
919 if( zTab ){
920 if( pItem->zAlias ){
921 char *zTabName = pItem->zAlias;
922 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
923 }else{
924 char *zTabName = pTab->zName;
925 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000926 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000927 continue;
928 }
drh626a8792005-01-17 22:08:19 +0000929 }
drh8141f612004-01-25 22:44:58 +0000930 }
danielk1977b3bce662005-01-29 08:32:43 +0000931 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000932 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000933 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000934 pMatch = pItem;
935 }
936 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
937 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000938 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000939 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000940 cnt++;
941 pExpr->iTable = pItem->iCursor;
942 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000943 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000944 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
945 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
946 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000947 if( (pExpr->flags & EP_ExpCollate)==0 ){
948 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
949 }
drh61dfc312006-12-16 16:25:15 +0000950 if( i<pSrcList->nSrc-1 ){
951 if( pItem[1].jointype & JT_NATURAL ){
952 /* If this match occurred in the left table of a natural join,
953 ** then skip the right table to avoid a duplicate match */
954 pItem++;
955 i++;
956 }else if( (pUsing = pItem[1].pUsing)!=0 ){
957 /* If this match occurs on a column that is in the USING clause
958 ** of a join, skip the search of the right table of the join
959 ** to avoid a duplicate match there. */
960 int k;
961 for(k=0; k<pUsing->nId; k++){
962 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
963 pItem++;
964 i++;
965 break;
966 }
drh873fac02005-06-06 17:11:46 +0000967 }
968 }
969 }
danielk1977b3bce662005-01-29 08:32:43 +0000970 break;
971 }
drh8141f612004-01-25 22:44:58 +0000972 }
973 }
974 }
drh626a8792005-01-17 22:08:19 +0000975
976#ifndef SQLITE_OMIT_TRIGGER
977 /* If we have not already resolved the name, then maybe
978 ** it is a new.* or old.* trigger argument reference
979 */
980 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
981 TriggerStack *pTriggerStack = pParse->trigStack;
982 Table *pTab = 0;
983 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
984 pExpr->iTable = pTriggerStack->newIdx;
985 assert( pTriggerStack->pTab );
986 pTab = pTriggerStack->pTab;
987 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
988 pExpr->iTable = pTriggerStack->oldIdx;
989 assert( pTriggerStack->pTab );
990 pTab = pTriggerStack->pTab;
991 }
992
993 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +0000994 int iCol;
drh626a8792005-01-17 22:08:19 +0000995 Column *pCol = pTab->aCol;
996
danielk1977da184232006-01-05 11:34:32 +0000997 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000998 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +0000999 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001000 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001001 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001002 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001003 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1004 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001005 if( (pExpr->flags & EP_ExpCollate)==0 ){
1006 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1007 }
danielk1977aee18ef2005-03-09 12:26:50 +00001008 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001009 break;
1010 }
1011 }
1012 }
1013 }
drhb7f91642004-10-31 02:22:47 +00001014#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001015
drh626a8792005-01-17 22:08:19 +00001016 /*
1017 ** Perhaps the name is a reference to the ROWID
1018 */
1019 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1020 cnt = 1;
1021 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001022 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001023 }
drh8141f612004-01-25 22:44:58 +00001024
drh626a8792005-01-17 22:08:19 +00001025 /*
1026 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1027 ** might refer to an result-set alias. This happens, for example, when
1028 ** we are resolving names in the WHERE clause of the following command:
1029 **
1030 ** SELECT a+b AS x FROM table WHERE x<10;
1031 **
1032 ** In cases like this, replace pExpr with a copy of the expression that
1033 ** forms the result set entry ("a+b" in the example) and return immediately.
1034 ** Note that the expression in the result set should have already been
1035 ** resolved by the time the WHERE clause is resolved.
1036 */
drhffe07b22005-11-03 00:41:17 +00001037 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001038 for(j=0; j<pEList->nExpr; j++){
1039 char *zAs = pEList->a[j].zName;
1040 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1041 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1042 pExpr->op = TK_AS;
1043 pExpr->iColumn = j;
1044 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001045 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001046 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001047 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001048 }
1049 }
1050 }
1051
1052 /* Advance to the next name context. The loop will exit when either
1053 ** we have a match (cnt>0) or when we run out of name contexts.
1054 */
1055 if( cnt==0 ){
1056 pNC = pNC->pNext;
1057 }
drh8141f612004-01-25 22:44:58 +00001058 }
1059
1060 /*
1061 ** If X and Y are NULL (in other words if only the column name Z is
1062 ** supplied) and the value of Z is enclosed in double-quotes, then
1063 ** Z is a string literal if it doesn't match any column names. In that
1064 ** case, we need to return right away and not make any changes to
1065 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001066 **
1067 ** Because no reference was made to outer contexts, the pNC->nRef
1068 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001069 */
1070 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1071 sqliteFree(zCol);
1072 return 0;
1073 }
1074
1075 /*
1076 ** cnt==0 means there was not match. cnt>1 means there were two or
1077 ** more matches. Either way, we have an error.
1078 */
1079 if( cnt!=1 ){
1080 char *z = 0;
1081 char *zErr;
1082 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1083 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001084 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001085 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001086 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001087 }else{
1088 z = sqliteStrDup(zCol);
1089 }
danielk19774adee202004-05-08 08:23:19 +00001090 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001091 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001092 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001093 }
1094
drh51669862004-12-18 18:40:26 +00001095 /* If a column from a table in pSrcList is referenced, then record
1096 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1097 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1098 ** column number is greater than the number of bits in the bitmask
1099 ** then set the high-order bit of the bitmask.
1100 */
1101 if( pExpr->iColumn>=0 && pMatch!=0 ){
1102 int n = pExpr->iColumn;
1103 if( n>=sizeof(Bitmask)*8 ){
1104 n = sizeof(Bitmask)*8-1;
1105 }
1106 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001107 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001108 }
1109
danielk1977d5d56522005-03-16 12:15:20 +00001110lookupname_end:
drh8141f612004-01-25 22:44:58 +00001111 /* Clean up and return
1112 */
1113 sqliteFree(zDb);
1114 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001115 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001116 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001117 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001118 pExpr->pRight = 0;
1119 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001120lookupname_end_2:
1121 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001122 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001123 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001124 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001125 if( pMatch && !pMatch->pSelect ){
1126 pExpr->pTab = pMatch->pTab;
1127 }
drh15ccce12005-05-23 15:06:39 +00001128 /* Increment the nRef value on all name contexts from TopNC up to
1129 ** the point where the name matched. */
1130 for(;;){
1131 assert( pTopNC!=0 );
1132 pTopNC->nRef++;
1133 if( pTopNC==pNC ) break;
1134 pTopNC = pTopNC->pNext;
1135 }
1136 return 0;
1137 } else {
1138 return 1;
drh626a8792005-01-17 22:08:19 +00001139 }
drh8141f612004-01-25 22:44:58 +00001140}
1141
1142/*
drh626a8792005-01-17 22:08:19 +00001143** This routine is designed as an xFunc for walkExprTree().
1144**
drh73b211a2005-01-18 04:00:42 +00001145** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001146** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001147** the tree or 2 to abort the tree walk.
1148**
1149** This routine also does error checking and name resolution for
1150** function names. The operator for aggregate functions is changed
1151** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001152*/
1153static int nameResolverStep(void *pArg, Expr *pExpr){
1154 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001155 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001156
danielk1977b3bce662005-01-29 08:32:43 +00001157 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001158 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001159 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001160
drh626a8792005-01-17 22:08:19 +00001161 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1162 ExprSetProperty(pExpr, EP_Resolved);
1163#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001164 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1165 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001166 int i;
danielk1977f0113002006-01-24 12:09:17 +00001167 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001168 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1169 }
1170 }
1171#endif
1172 switch( pExpr->op ){
1173 /* Double-quoted strings (ex: "abc") are used as identifiers if
1174 ** possible. Otherwise they remain as strings. Single-quoted
1175 ** strings (ex: 'abc') are always string literals.
1176 */
1177 case TK_STRING: {
1178 if( pExpr->token.z[0]=='\'' ) break;
1179 /* Fall thru into the TK_ID case if this is a double-quoted string */
1180 }
1181 /* A lone identifier is the name of a column.
1182 */
1183 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001184 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1185 return 1;
1186 }
1187
1188 /* A table name and column name: ID.ID
1189 ** Or a database, table and column: ID.ID.ID
1190 */
1191 case TK_DOT: {
1192 Token *pColumn;
1193 Token *pTable;
1194 Token *pDb;
1195 Expr *pRight;
1196
danielk1977b3bce662005-01-29 08:32:43 +00001197 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001198 pRight = pExpr->pRight;
1199 if( pRight->op==TK_ID ){
1200 pDb = 0;
1201 pTable = &pExpr->pLeft->token;
1202 pColumn = &pRight->token;
1203 }else{
1204 assert( pRight->op==TK_DOT );
1205 pDb = &pExpr->pLeft->token;
1206 pTable = &pRight->pLeft->token;
1207 pColumn = &pRight->pRight->token;
1208 }
1209 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1210 return 1;
1211 }
1212
1213 /* Resolve function names
1214 */
drhb71090f2005-05-23 17:26:51 +00001215 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001216 case TK_FUNCTION: {
1217 ExprList *pList = pExpr->pList; /* The argument list */
1218 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1219 int no_such_func = 0; /* True if no such function exists */
1220 int wrong_num_args = 0; /* True if wrong number of arguments */
1221 int is_agg = 0; /* True if is an aggregate function */
1222 int i;
drh5169bbc2006-08-24 14:59:45 +00001223 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001224 int nId; /* Number of characters in function name */
1225 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001226 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001227 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001228
drh2646da72005-12-09 20:02:05 +00001229 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001230 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001231 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1232 if( pDef==0 ){
1233 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1234 if( pDef==0 ){
1235 no_such_func = 1;
1236 }else{
1237 wrong_num_args = 1;
1238 }
1239 }else{
1240 is_agg = pDef->xFunc==0;
1241 }
drh2fca7fe2006-11-23 11:59:13 +00001242#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001243 if( pDef ){
1244 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1245 if( auth!=SQLITE_OK ){
1246 if( auth==SQLITE_DENY ){
1247 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1248 pDef->zName);
1249 pNC->nErr++;
1250 }
1251 pExpr->op = TK_NULL;
1252 return 1;
1253 }
1254 }
drhb8b14212006-08-24 15:18:25 +00001255#endif
drh626a8792005-01-17 22:08:19 +00001256 if( is_agg && !pNC->allowAgg ){
1257 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1258 pNC->nErr++;
1259 is_agg = 0;
1260 }else if( no_such_func ){
1261 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1262 pNC->nErr++;
1263 }else if( wrong_num_args ){
1264 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1265 nId, zId);
1266 pNC->nErr++;
1267 }
1268 if( is_agg ){
1269 pExpr->op = TK_AGG_FUNCTION;
1270 pNC->hasAgg = 1;
1271 }
drh73b211a2005-01-18 04:00:42 +00001272 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001273 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001274 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001275 }
drh73b211a2005-01-18 04:00:42 +00001276 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001277 /* FIX ME: Compute pExpr->affinity based on the expected return
1278 ** type of the function
1279 */
1280 return is_agg;
1281 }
danielk1977b3bce662005-01-29 08:32:43 +00001282#ifndef SQLITE_OMIT_SUBQUERY
1283 case TK_SELECT:
1284 case TK_EXISTS:
1285#endif
1286 case TK_IN: {
1287 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001288 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001289#ifndef SQLITE_OMIT_CHECK
1290 if( pNC->isCheck ){
1291 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1292 }
1293#endif
danielk1977b3bce662005-01-29 08:32:43 +00001294 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1295 assert( pNC->nRef>=nRef );
1296 if( nRef!=pNC->nRef ){
1297 ExprSetProperty(pExpr, EP_VarSelect);
1298 }
1299 }
drh4284fb02005-11-03 12:33:28 +00001300 break;
danielk1977b3bce662005-01-29 08:32:43 +00001301 }
drh4284fb02005-11-03 12:33:28 +00001302#ifndef SQLITE_OMIT_CHECK
1303 case TK_VARIABLE: {
1304 if( pNC->isCheck ){
1305 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1306 }
1307 break;
1308 }
1309#endif
drh626a8792005-01-17 22:08:19 +00001310 }
1311 return 0;
1312}
1313
1314/*
drhcce7d172000-05-31 15:34:51 +00001315** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001316** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001317** index to the table in the table list and a column offset. The
1318** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1319** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001320** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001321** VDBE cursor number for a cursor that is pointing into the referenced
1322** table. The Expr.iColumn value is changed to the index of the column
1323** of the referenced table. The Expr.iColumn value for the special
1324** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1325** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001326**
drh626a8792005-01-17 22:08:19 +00001327** Also resolve function names and check the functions for proper
1328** usage. Make sure all function names are recognized and all functions
1329** have the correct number of arguments. Leave an error message
1330** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1331**
drh73b211a2005-01-18 04:00:42 +00001332** If the expression contains aggregate functions then set the EP_Agg
1333** property on the expression.
drh626a8792005-01-17 22:08:19 +00001334*/
1335int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001336 NameContext *pNC, /* Namespace to resolve expressions in. */
1337 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001338){
drh13449892005-09-07 21:22:45 +00001339 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001340 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001341 savedHasAgg = pNC->hasAgg;
1342 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001343 walkExprTree(pExpr, nameResolverStep, pNC);
1344 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001345 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001346 }
drh13449892005-09-07 21:22:45 +00001347 if( pNC->hasAgg ){
1348 ExprSetProperty(pExpr, EP_Agg);
1349 }else if( savedHasAgg ){
1350 pNC->hasAgg = 1;
1351 }
drh73b211a2005-01-18 04:00:42 +00001352 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001353}
1354
drh1398ad32005-01-19 23:24:50 +00001355/*
1356** A pointer instance of this structure is used to pass information
1357** through walkExprTree into codeSubqueryStep().
1358*/
1359typedef struct QueryCoder QueryCoder;
1360struct QueryCoder {
1361 Parse *pParse; /* The parsing context */
1362 NameContext *pNC; /* Namespace of first enclosing query */
1363};
1364
drh626a8792005-01-17 22:08:19 +00001365
1366/*
drh9cbe6352005-11-29 03:13:21 +00001367** Generate code for scalar subqueries used as an expression
1368** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001369**
drh9cbe6352005-11-29 03:13:21 +00001370** (SELECT a FROM b) -- subquery
1371** EXISTS (SELECT a FROM b) -- EXISTS subquery
1372** x IN (4,5,11) -- IN operator with list on right-hand side
1373** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001374**
drh9cbe6352005-11-29 03:13:21 +00001375** The pExpr parameter describes the expression that contains the IN
1376** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001377*/
drh51522cd2005-01-20 13:36:19 +00001378#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001379void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001380 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001381 Vdbe *v = sqlite3GetVdbe(pParse);
1382 if( v==0 ) return;
1383
drh57dbd7b2005-07-08 18:25:26 +00001384 /* This code must be run in its entirety every time it is encountered
1385 ** if any of the following is true:
1386 **
1387 ** * The right-hand side is a correlated subquery
1388 ** * The right-hand side is an expression list containing variables
1389 ** * We are inside a trigger
1390 **
1391 ** If all of the above are false, then we can run this code just once
1392 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001393 */
1394 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1395 int mem = pParse->nMem++;
1396 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001397 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001398 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001399 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001400 }
1401
drhcce7d172000-05-31 15:34:51 +00001402 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001403 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001404 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001405 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001406 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001407
danielk1977bf3b7212004-05-18 10:06:24 +00001408 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001409
1410 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001411 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001412 ** filled with single-field index keys representing the results
1413 ** from the SELECT or the <exprlist>.
1414 **
1415 ** If the 'x' expression is a column value, or the SELECT...
1416 ** statement returns a column value, then the affinity of that
1417 ** column is used to build the index keys. If both 'x' and the
1418 ** SELECT... statement are columns, then numeric affinity is used
1419 ** if either column has NUMERIC or INTEGER affinity. If neither
1420 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1421 ** is used.
1422 */
1423 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001424 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001425 memset(&keyInfo, 0, sizeof(keyInfo));
1426 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001427 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001428
drhfef52082000-06-06 01:50:43 +00001429 if( pExpr->pSelect ){
1430 /* Case 1: expr IN (SELECT ...)
1431 **
danielk1977e014a832004-05-17 10:48:57 +00001432 ** Generate code to write the results of the select into the temporary
1433 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001434 */
danielk1977e014a832004-05-17 10:48:57 +00001435 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001436 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001437 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001438 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1439 return;
1440 }
drhbe5c89a2004-07-26 00:31:09 +00001441 pEList = pExpr->pSelect->pEList;
1442 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001443 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001444 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001445 }
drhfef52082000-06-06 01:50:43 +00001446 }else if( pExpr->pList ){
1447 /* Case 2: expr IN (exprlist)
1448 **
danielk1977e014a832004-05-17 10:48:57 +00001449 ** For each expression, build an index key from the evaluation and
1450 ** store it in the temporary table. If <expr> is a column, then use
1451 ** that columns affinity when building index keys. If <expr> is not
1452 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001453 */
danielk1977e014a832004-05-17 10:48:57 +00001454 int i;
drh57dbd7b2005-07-08 18:25:26 +00001455 ExprList *pList = pExpr->pList;
1456 struct ExprList_item *pItem;
1457
danielk1977e014a832004-05-17 10:48:57 +00001458 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001459 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001460 }
danielk19770202b292004-06-09 09:55:16 +00001461 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001462
1463 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001464 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1465 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001466
drh57dbd7b2005-07-08 18:25:26 +00001467 /* If the expression is not constant then we will need to
1468 ** disable the test that was generated above that makes sure
1469 ** this code only executes once. Because for a non-constant
1470 ** expression we need to rerun this code each time.
1471 */
drh6c30be82005-07-29 15:10:17 +00001472 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001473 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001474 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001475 }
danielk1977e014a832004-05-17 10:48:57 +00001476
1477 /* Evaluate the expression and insert it into the temp table */
1478 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001479 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001480 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001481 }
1482 }
danielk19770202b292004-06-09 09:55:16 +00001483 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001484 break;
drhfef52082000-06-06 01:50:43 +00001485 }
1486
drh51522cd2005-01-20 13:36:19 +00001487 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001488 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001489 /* This has to be a scalar SELECT. Generate code to put the
1490 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001491 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001492 */
drh2646da72005-12-09 20:02:05 +00001493 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001494 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001495 int iMem;
1496 int sop;
drh1398ad32005-01-19 23:24:50 +00001497
drhec7429a2005-10-06 16:53:14 +00001498 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001499 pSel = pExpr->pSelect;
1500 if( pExpr->op==TK_SELECT ){
1501 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001502 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1503 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001504 }else{
drh51522cd2005-01-20 13:36:19 +00001505 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001506 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1507 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001508 }
drhec7429a2005-10-06 16:53:14 +00001509 sqlite3ExprDelete(pSel->pLimit);
1510 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001511 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1512 return;
1513 }
danielk1977b3bce662005-01-29 08:32:43 +00001514 break;
drhcce7d172000-05-31 15:34:51 +00001515 }
1516 }
danielk1977b3bce662005-01-29 08:32:43 +00001517
drh57dbd7b2005-07-08 18:25:26 +00001518 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001519 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001520 }
1521 return;
drhcce7d172000-05-31 15:34:51 +00001522}
drh51522cd2005-01-20 13:36:19 +00001523#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001524
drhcce7d172000-05-31 15:34:51 +00001525/*
drhfec19aa2004-05-19 20:41:03 +00001526** Generate an instruction that will put the integer describe by
1527** text z[0..n-1] on the stack.
1528*/
1529static void codeInteger(Vdbe *v, const char *z, int n){
1530 int i;
drh6fec0762004-05-30 01:38:43 +00001531 if( sqlite3GetInt32(z, &i) ){
1532 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1533 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001534 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001535 }else{
1536 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1537 }
1538}
1539
drh945498f2007-02-24 11:52:52 +00001540
1541/*
1542** Generate code that will extract the iColumn-th column from
1543** table pTab and push that column value on the stack. There
1544** is an open cursor to pTab in iTable. If iColumn<0 then
1545** code is generated that extracts the rowid.
1546*/
1547void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1548 if( iColumn<0 ){
1549 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1550 sqlite3VdbeAddOp(v, op, iTable, 0);
1551 }else if( pTab==0 ){
1552 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1553 }else{
1554 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1555 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1556 sqlite3ColumnDefault(v, pTab, iColumn);
1557#ifndef SQLITE_OMIT_FLOATING_POINT
1558 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1559 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1560 }
1561#endif
1562 }
1563}
1564
drhfec19aa2004-05-19 20:41:03 +00001565/*
drhcce7d172000-05-31 15:34:51 +00001566** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001567** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001568**
1569** This code depends on the fact that certain token values (ex: TK_EQ)
1570** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1571** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1572** the make process cause these values to align. Assert()s in the code
1573** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001574*/
danielk19774adee202004-05-08 08:23:19 +00001575void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001576 Vdbe *v = pParse->pVdbe;
1577 int op;
drhffe07b22005-11-03 00:41:17 +00001578 int stackChng = 1; /* Amount of change to stack depth */
1579
danielk19777977a172004-11-09 12:44:37 +00001580 if( v==0 ) return;
1581 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001582 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001583 return;
1584 }
drhf2bc0132004-10-04 13:19:23 +00001585 op = pExpr->op;
1586 switch( op ){
drh13449892005-09-07 21:22:45 +00001587 case TK_AGG_COLUMN: {
1588 AggInfo *pAggInfo = pExpr->pAggInfo;
1589 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1590 if( !pAggInfo->directMode ){
1591 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1592 break;
1593 }else if( pAggInfo->useSortingIdx ){
1594 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1595 pCol->iSorterColumn);
1596 break;
1597 }
1598 /* Otherwise, fall thru into the TK_COLUMN case */
1599 }
drh967e8b72000-06-21 13:59:10 +00001600 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001601 if( pExpr->iTable<0 ){
1602 /* This only happens when coding check constraints */
1603 assert( pParse->ckOffset>0 );
1604 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001605 }else{
drh945498f2007-02-24 11:52:52 +00001606 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001607 }
drhcce7d172000-05-31 15:34:51 +00001608 break;
1609 }
1610 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001611 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001612 break;
1613 }
1614 case TK_FLOAT:
1615 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001616 assert( TK_FLOAT==OP_Real );
1617 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001618 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001619 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001620 break;
1621 }
drhf0863fe2005-06-12 21:35:51 +00001622 case TK_NULL: {
1623 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1624 break;
1625 }
danielk19775338a5f2005-01-20 13:03:10 +00001626#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001627 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001628 int n;
1629 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001630 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001631 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001632 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001633 assert( n>=0 );
1634 if( n==0 ){
1635 z = "";
1636 }
1637 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001638 break;
1639 }
danielk19775338a5f2005-01-20 13:03:10 +00001640#endif
drh50457892003-09-06 01:10:47 +00001641 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001642 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001643 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001644 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001645 }
drh50457892003-09-06 01:10:47 +00001646 break;
1647 }
drh4e0cff62004-11-05 05:10:28 +00001648 case TK_REGISTER: {
1649 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1650 break;
1651 }
drh487e2622005-06-25 18:42:14 +00001652#ifndef SQLITE_OMIT_CAST
1653 case TK_CAST: {
1654 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001655 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001656 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001657 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001658 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1659 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1660 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1661 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1662 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1663 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1664 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001665 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001666 break;
1667 }
1668#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001669 case TK_LT:
1670 case TK_LE:
1671 case TK_GT:
1672 case TK_GE:
1673 case TK_NE:
1674 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001675 assert( TK_LT==OP_Lt );
1676 assert( TK_LE==OP_Le );
1677 assert( TK_GT==OP_Gt );
1678 assert( TK_GE==OP_Ge );
1679 assert( TK_EQ==OP_Eq );
1680 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001681 sqlite3ExprCode(pParse, pExpr->pLeft);
1682 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001683 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001684 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001685 break;
drhc9b84a12002-06-20 11:36:48 +00001686 }
drhcce7d172000-05-31 15:34:51 +00001687 case TK_AND:
1688 case TK_OR:
1689 case TK_PLUS:
1690 case TK_STAR:
1691 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001692 case TK_REM:
1693 case TK_BITAND:
1694 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001695 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001696 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001697 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001698 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001699 assert( TK_AND==OP_And );
1700 assert( TK_OR==OP_Or );
1701 assert( TK_PLUS==OP_Add );
1702 assert( TK_MINUS==OP_Subtract );
1703 assert( TK_REM==OP_Remainder );
1704 assert( TK_BITAND==OP_BitAnd );
1705 assert( TK_BITOR==OP_BitOr );
1706 assert( TK_SLASH==OP_Divide );
1707 assert( TK_LSHIFT==OP_ShiftLeft );
1708 assert( TK_RSHIFT==OP_ShiftRight );
1709 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001710 sqlite3ExprCode(pParse, pExpr->pLeft);
1711 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001712 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001713 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001714 break;
1715 }
drhcce7d172000-05-31 15:34:51 +00001716 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001717 Expr *pLeft = pExpr->pLeft;
1718 assert( pLeft );
1719 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1720 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001721 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001722 if( pLeft->op==TK_FLOAT ){
1723 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001724 }else{
drhfec19aa2004-05-19 20:41:03 +00001725 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001726 }
drh6e142f52000-06-08 13:36:40 +00001727 sqliteFree(z);
1728 break;
1729 }
drh1ccde152000-06-17 13:12:39 +00001730 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001731 }
drhbf4133c2001-10-13 02:59:08 +00001732 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001733 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001734 assert( TK_BITNOT==OP_BitNot );
1735 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001736 sqlite3ExprCode(pParse, pExpr->pLeft);
1737 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001738 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001739 break;
1740 }
1741 case TK_ISNULL:
1742 case TK_NOTNULL: {
1743 int dest;
drhf2bc0132004-10-04 13:19:23 +00001744 assert( TK_ISNULL==OP_IsNull );
1745 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001746 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1747 sqlite3ExprCode(pParse, pExpr->pLeft);
1748 dest = sqlite3VdbeCurrentAddr(v) + 2;
1749 sqlite3VdbeAddOp(v, op, 1, dest);
1750 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001751 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001752 break;
drhcce7d172000-05-31 15:34:51 +00001753 }
drh22827922000-06-06 17:27:05 +00001754 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001755 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001756 if( pInfo==0 ){
1757 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1758 &pExpr->span);
1759 }else{
1760 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1761 }
drh22827922000-06-06 17:27:05 +00001762 break;
1763 }
drhb71090f2005-05-23 17:26:51 +00001764 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001765 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001766 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001767 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001768 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001769 int nId;
1770 const char *zId;
drh13449892005-09-07 21:22:45 +00001771 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001772 int i;
danielk197714db2662006-01-09 16:12:04 +00001773 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001774 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001775 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001776 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001777 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001778 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001779 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001780#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001781 /* Possibly overload the function if the first argument is
1782 ** a virtual table column.
1783 **
1784 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1785 ** second argument, not the first, as the argument to test to
1786 ** see if it is a column in a virtual table. This is done because
1787 ** the left operand of infix functions (the operand we want to
1788 ** control overloading) ends up as the second argument to the
1789 ** function. The expression "A glob B" is equivalent to
1790 ** "glob(B,A). We want to use the A in "A glob B" to test
1791 ** for function overloading. But we use the B term in "glob(B,A)".
1792 */
drh6a03a1c2006-07-08 18:34:59 +00001793 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001794 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1795 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001796 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1797 }
1798#endif
danielk1977682f68b2004-06-05 10:22:17 +00001799 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001800 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001801 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001802 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001803 if( pDef->needCollSeq && !pColl ){
1804 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1805 }
1806 }
1807 if( pDef->needCollSeq ){
1808 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001809 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001810 }
drh13449892005-09-07 21:22:45 +00001811 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001812 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001813 break;
1814 }
drhfe2093d2005-01-20 22:48:47 +00001815#ifndef SQLITE_OMIT_SUBQUERY
1816 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001817 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001818 if( pExpr->iColumn==0 ){
1819 sqlite3CodeSubselect(pParse, pExpr);
1820 }
danielk19774adee202004-05-08 08:23:19 +00001821 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001822 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001823 break;
1824 }
drhfef52082000-06-06 01:50:43 +00001825 case TK_IN: {
1826 int addr;
drh94a11212004-09-25 13:12:14 +00001827 char affinity;
drhafa5f682006-01-30 14:36:59 +00001828 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001829 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001830
1831 /* Figure out the affinity to use to create a key from the results
1832 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001833 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001834 */
drh94a11212004-09-25 13:12:14 +00001835 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001836
danielk19774adee202004-05-08 08:23:19 +00001837 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001838 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001839
1840 /* Code the <expr> from "<expr> IN (...)". The temporary table
1841 ** pExpr->iTable contains the values that make up the (...) set.
1842 */
danielk19774adee202004-05-08 08:23:19 +00001843 sqlite3ExprCode(pParse, pExpr->pLeft);
1844 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001845 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001846 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001847 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001848 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001849 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001850 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1851 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1852
drhfef52082000-06-06 01:50:43 +00001853 break;
1854 }
danielk197793758c82005-01-21 08:13:14 +00001855#endif
drhfef52082000-06-06 01:50:43 +00001856 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001857 Expr *pLeft = pExpr->pLeft;
1858 struct ExprList_item *pLItem = pExpr->pList->a;
1859 Expr *pRight = pLItem->pExpr;
1860 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001861 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001862 sqlite3ExprCode(pParse, pRight);
1863 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001864 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001865 pLItem++;
1866 pRight = pLItem->pExpr;
1867 sqlite3ExprCode(pParse, pRight);
1868 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001869 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001870 break;
1871 }
drh51e9a442004-01-16 16:42:53 +00001872 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001873 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001874 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001875 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001876 break;
1877 }
drh17a7f8d2002-03-24 13:13:27 +00001878 case TK_CASE: {
1879 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001880 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001881 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001882 int i;
drhbe5c89a2004-07-26 00:31:09 +00001883 ExprList *pEList;
1884 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001885
1886 assert(pExpr->pList);
1887 assert((pExpr->pList->nExpr % 2) == 0);
1888 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001889 pEList = pExpr->pList;
1890 aListelem = pEList->a;
1891 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001892 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001893 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001894 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001895 }
drhf5905aa2002-05-26 20:54:33 +00001896 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001897 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001898 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001899 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001900 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1901 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001902 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001903 }else{
danielk19774adee202004-05-08 08:23:19 +00001904 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001905 }
drhbe5c89a2004-07-26 00:31:09 +00001906 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001907 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001908 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001909 }
drhf570f012002-05-31 15:51:25 +00001910 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001911 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001912 }
drh17a7f8d2002-03-24 13:13:27 +00001913 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001914 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001915 }else{
drhf0863fe2005-06-12 21:35:51 +00001916 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001917 }
danielk19774adee202004-05-08 08:23:19 +00001918 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001919 break;
1920 }
danielk19775338a5f2005-01-20 13:03:10 +00001921#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001922 case TK_RAISE: {
1923 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001924 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001925 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001926 return;
1927 }
drhad6d9462004-09-19 02:15:24 +00001928 if( pExpr->iColumn!=OE_Ignore ){
1929 assert( pExpr->iColumn==OE_Rollback ||
1930 pExpr->iColumn == OE_Abort ||
1931 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001932 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001933 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001934 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001935 } else {
drhad6d9462004-09-19 02:15:24 +00001936 assert( pExpr->iColumn == OE_Ignore );
1937 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1938 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1939 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001940 }
drhffe07b22005-11-03 00:41:17 +00001941 stackChng = 0;
1942 break;
drh17a7f8d2002-03-24 13:13:27 +00001943 }
danielk19775338a5f2005-01-20 13:03:10 +00001944#endif
drhffe07b22005-11-03 00:41:17 +00001945 }
1946
1947 if( pParse->ckOffset ){
1948 pParse->ckOffset += stackChng;
1949 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001950 }
drhcce7d172000-05-31 15:34:51 +00001951}
1952
danielk197793758c82005-01-21 08:13:14 +00001953#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001954/*
drh25303782004-12-07 15:41:48 +00001955** Generate code that evalutes the given expression and leaves the result
1956** on the stack. See also sqlite3ExprCode().
1957**
1958** This routine might also cache the result and modify the pExpr tree
1959** so that it will make use of the cached result on subsequent evaluations
1960** rather than evaluate the whole expression again. Trivial expressions are
1961** not cached. If the expression is cached, its result is stored in a
1962** memory location.
1963*/
1964void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1965 Vdbe *v = pParse->pVdbe;
1966 int iMem;
1967 int addr1, addr2;
1968 if( v==0 ) return;
1969 addr1 = sqlite3VdbeCurrentAddr(v);
1970 sqlite3ExprCode(pParse, pExpr);
1971 addr2 = sqlite3VdbeCurrentAddr(v);
1972 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1973 iMem = pExpr->iTable = pParse->nMem++;
1974 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1975 pExpr->op = TK_REGISTER;
1976 }
1977}
danielk197793758c82005-01-21 08:13:14 +00001978#endif
drh25303782004-12-07 15:41:48 +00001979
1980/*
drh268380c2004-02-25 13:47:31 +00001981** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001982** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001983**
1984** Return the number of elements pushed onto the stack.
1985*/
danielk19774adee202004-05-08 08:23:19 +00001986int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001987 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001988 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001989){
1990 struct ExprList_item *pItem;
1991 int i, n;
drh268380c2004-02-25 13:47:31 +00001992 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001993 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001994 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001995 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001996 }
drhf9b596e2004-05-26 16:54:42 +00001997 return n;
drh268380c2004-02-25 13:47:31 +00001998}
1999
2000/*
drhcce7d172000-05-31 15:34:51 +00002001** Generate code for a boolean expression such that a jump is made
2002** to the label "dest" if the expression is true but execution
2003** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002004**
2005** If the expression evaluates to NULL (neither true nor false), then
2006** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002007**
2008** This code depends on the fact that certain token values (ex: TK_EQ)
2009** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2010** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2011** the make process cause these values to align. Assert()s in the code
2012** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002013*/
danielk19774adee202004-05-08 08:23:19 +00002014void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002015 Vdbe *v = pParse->pVdbe;
2016 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002017 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002018 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002019 op = pExpr->op;
2020 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002021 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002022 int d2 = sqlite3VdbeMakeLabel(v);
2023 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2024 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2025 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002026 break;
2027 }
2028 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002029 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2030 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002031 break;
2032 }
2033 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002034 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002035 break;
2036 }
2037 case TK_LT:
2038 case TK_LE:
2039 case TK_GT:
2040 case TK_GE:
2041 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002042 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002043 assert( TK_LT==OP_Lt );
2044 assert( TK_LE==OP_Le );
2045 assert( TK_GT==OP_Gt );
2046 assert( TK_GE==OP_Ge );
2047 assert( TK_EQ==OP_Eq );
2048 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002049 sqlite3ExprCode(pParse, pExpr->pLeft);
2050 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002051 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002052 break;
2053 }
2054 case TK_ISNULL:
2055 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002056 assert( TK_ISNULL==OP_IsNull );
2057 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002058 sqlite3ExprCode(pParse, pExpr->pLeft);
2059 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002060 break;
2061 }
drhfef52082000-06-06 01:50:43 +00002062 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002063 /* The expression "x BETWEEN y AND z" is implemented as:
2064 **
2065 ** 1 IF (x < y) GOTO 3
2066 ** 2 IF (x <= z) GOTO <dest>
2067 ** 3 ...
2068 */
drhf5905aa2002-05-26 20:54:33 +00002069 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002070 Expr *pLeft = pExpr->pLeft;
2071 Expr *pRight = pExpr->pList->a[0].pExpr;
2072 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002073 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002074 sqlite3ExprCode(pParse, pRight);
2075 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002076
drhbe5c89a2004-07-26 00:31:09 +00002077 pRight = pExpr->pList->a[1].pExpr;
2078 sqlite3ExprCode(pParse, pRight);
2079 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002080
danielk19774adee202004-05-08 08:23:19 +00002081 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002082 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002083 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002084 break;
2085 }
drhcce7d172000-05-31 15:34:51 +00002086 default: {
danielk19774adee202004-05-08 08:23:19 +00002087 sqlite3ExprCode(pParse, pExpr);
2088 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002089 break;
2090 }
2091 }
drhffe07b22005-11-03 00:41:17 +00002092 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002093}
2094
2095/*
drh66b89c82000-11-28 20:47:17 +00002096** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002097** to the label "dest" if the expression is false but execution
2098** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002099**
2100** If the expression evaluates to NULL (neither true nor false) then
2101** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002102*/
danielk19774adee202004-05-08 08:23:19 +00002103void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002104 Vdbe *v = pParse->pVdbe;
2105 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002106 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002107 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002108
2109 /* The value of pExpr->op and op are related as follows:
2110 **
2111 ** pExpr->op op
2112 ** --------- ----------
2113 ** TK_ISNULL OP_NotNull
2114 ** TK_NOTNULL OP_IsNull
2115 ** TK_NE OP_Eq
2116 ** TK_EQ OP_Ne
2117 ** TK_GT OP_Le
2118 ** TK_LE OP_Gt
2119 ** TK_GE OP_Lt
2120 ** TK_LT OP_Ge
2121 **
2122 ** For other values of pExpr->op, op is undefined and unused.
2123 ** The value of TK_ and OP_ constants are arranged such that we
2124 ** can compute the mapping above using the following expression.
2125 ** Assert()s verify that the computation is correct.
2126 */
2127 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2128
2129 /* Verify correct alignment of TK_ and OP_ constants
2130 */
2131 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2132 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2133 assert( pExpr->op!=TK_NE || op==OP_Eq );
2134 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2135 assert( pExpr->op!=TK_LT || op==OP_Ge );
2136 assert( pExpr->op!=TK_LE || op==OP_Gt );
2137 assert( pExpr->op!=TK_GT || op==OP_Le );
2138 assert( pExpr->op!=TK_GE || op==OP_Lt );
2139
drhcce7d172000-05-31 15:34:51 +00002140 switch( pExpr->op ){
2141 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002142 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2143 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002144 break;
2145 }
2146 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002147 int d2 = sqlite3VdbeMakeLabel(v);
2148 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2149 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2150 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002151 break;
2152 }
2153 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002154 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002155 break;
2156 }
2157 case TK_LT:
2158 case TK_LE:
2159 case TK_GT:
2160 case TK_GE:
2161 case TK_NE:
2162 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002163 sqlite3ExprCode(pParse, pExpr->pLeft);
2164 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002165 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002166 break;
2167 }
drhcce7d172000-05-31 15:34:51 +00002168 case TK_ISNULL:
2169 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002170 sqlite3ExprCode(pParse, pExpr->pLeft);
2171 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002172 break;
2173 }
drhfef52082000-06-06 01:50:43 +00002174 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002175 /* The expression is "x BETWEEN y AND z". It is implemented as:
2176 **
2177 ** 1 IF (x >= y) GOTO 3
2178 ** 2 GOTO <dest>
2179 ** 3 IF (x > z) GOTO <dest>
2180 */
drhfef52082000-06-06 01:50:43 +00002181 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002182 Expr *pLeft = pExpr->pLeft;
2183 Expr *pRight = pExpr->pList->a[0].pExpr;
2184 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002185 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002186 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002187 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002188 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2189
danielk19774adee202004-05-08 08:23:19 +00002190 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2191 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002192 pRight = pExpr->pList->a[1].pExpr;
2193 sqlite3ExprCode(pParse, pRight);
2194 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002195 break;
2196 }
drhcce7d172000-05-31 15:34:51 +00002197 default: {
danielk19774adee202004-05-08 08:23:19 +00002198 sqlite3ExprCode(pParse, pExpr);
2199 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002200 break;
2201 }
2202 }
drhffe07b22005-11-03 00:41:17 +00002203 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002204}
drh22827922000-06-06 17:27:05 +00002205
2206/*
2207** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2208** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002209**
2210** Sometimes this routine will return FALSE even if the two expressions
2211** really are equivalent. If we cannot prove that the expressions are
2212** identical, we return FALSE just to be safe. So if this routine
2213** returns false, then you do not really know for certain if the two
2214** expressions are the same. But if you get a TRUE return, then you
2215** can be sure the expressions are the same. In the places where
2216** this routine is used, it does not hurt to get an extra FALSE - that
2217** just might result in some slightly slower code. But returning
2218** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002219*/
danielk19774adee202004-05-08 08:23:19 +00002220int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002221 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002222 if( pA==0||pB==0 ){
2223 return pB==pA;
drh22827922000-06-06 17:27:05 +00002224 }
2225 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002226 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002227 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2228 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002229 if( pA->pList ){
2230 if( pB->pList==0 ) return 0;
2231 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2232 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002233 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002234 return 0;
2235 }
2236 }
2237 }else if( pB->pList ){
2238 return 0;
2239 }
2240 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002241 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002242 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002243 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002244 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002245 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2246 return 0;
2247 }
drh22827922000-06-06 17:27:05 +00002248 }
2249 return 1;
2250}
2251
drh13449892005-09-07 21:22:45 +00002252
drh22827922000-06-06 17:27:05 +00002253/*
drh13449892005-09-07 21:22:45 +00002254** Add a new element to the pAggInfo->aCol[] array. Return the index of
2255** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002256*/
drh13449892005-09-07 21:22:45 +00002257static int addAggInfoColumn(AggInfo *pInfo){
2258 int i;
drhcf643722007-03-27 13:36:37 +00002259 pInfo->aCol = sqlite3ArrayAllocate(
2260 pInfo->aCol,
2261 sizeof(pInfo->aCol[0]),
2262 3,
2263 &pInfo->nColumn,
2264 &pInfo->nColumnAlloc,
2265 &i
2266 );
drh13449892005-09-07 21:22:45 +00002267 return i;
2268}
2269
2270/*
2271** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2272** the new element. Return a negative number if malloc fails.
2273*/
2274static int addAggInfoFunc(AggInfo *pInfo){
2275 int i;
drhcf643722007-03-27 13:36:37 +00002276 pInfo->aFunc = sqlite3ArrayAllocate(
2277 pInfo->aFunc,
2278 sizeof(pInfo->aFunc[0]),
2279 3,
2280 &pInfo->nFunc,
2281 &pInfo->nFuncAlloc,
2282 &i
2283 );
drh13449892005-09-07 21:22:45 +00002284 return i;
2285}
drh22827922000-06-06 17:27:05 +00002286
2287/*
drh626a8792005-01-17 22:08:19 +00002288** This is an xFunc for walkExprTree() used to implement
2289** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2290** for additional information.
drh22827922000-06-06 17:27:05 +00002291**
drh626a8792005-01-17 22:08:19 +00002292** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002293*/
drh626a8792005-01-17 22:08:19 +00002294static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002295 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002296 NameContext *pNC = (NameContext *)pArg;
2297 Parse *pParse = pNC->pParse;
2298 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002299 AggInfo *pAggInfo = pNC->pAggInfo;
2300
drh22827922000-06-06 17:27:05 +00002301
drh22827922000-06-06 17:27:05 +00002302 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002303 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002304 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002305 /* Check to see if the column is in one of the tables in the FROM
2306 ** clause of the aggregate query */
2307 if( pSrcList ){
2308 struct SrcList_item *pItem = pSrcList->a;
2309 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2310 struct AggInfo_col *pCol;
2311 if( pExpr->iTable==pItem->iCursor ){
2312 /* If we reach this point, it means that pExpr refers to a table
2313 ** that is in the FROM clause of the aggregate query.
2314 **
2315 ** Make an entry for the column in pAggInfo->aCol[] if there
2316 ** is not an entry there already.
2317 */
drh7f906d62007-03-12 23:48:52 +00002318 int k;
drh13449892005-09-07 21:22:45 +00002319 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002320 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002321 if( pCol->iTable==pExpr->iTable &&
2322 pCol->iColumn==pExpr->iColumn ){
2323 break;
2324 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002325 }
drh7f906d62007-03-12 23:48:52 +00002326 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2327 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002328 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002329 pCol->iTable = pExpr->iTable;
2330 pCol->iColumn = pExpr->iColumn;
2331 pCol->iMem = pParse->nMem++;
2332 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002333 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002334 if( pAggInfo->pGroupBy ){
2335 int j, n;
2336 ExprList *pGB = pAggInfo->pGroupBy;
2337 struct ExprList_item *pTerm = pGB->a;
2338 n = pGB->nExpr;
2339 for(j=0; j<n; j++, pTerm++){
2340 Expr *pE = pTerm->pExpr;
2341 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2342 pE->iColumn==pExpr->iColumn ){
2343 pCol->iSorterColumn = j;
2344 break;
2345 }
2346 }
2347 }
2348 if( pCol->iSorterColumn<0 ){
2349 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2350 }
2351 }
2352 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2353 ** because it was there before or because we just created it).
2354 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2355 ** pAggInfo->aCol[] entry.
2356 */
2357 pExpr->pAggInfo = pAggInfo;
2358 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002359 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002360 break;
2361 } /* endif pExpr->iTable==pItem->iCursor */
2362 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002363 }
drh626a8792005-01-17 22:08:19 +00002364 return 1;
drh22827922000-06-06 17:27:05 +00002365 }
2366 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002367 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2368 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002369 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002370 /* Check to see if pExpr is a duplicate of another aggregate
2371 ** function that is already in the pAggInfo structure
2372 */
2373 struct AggInfo_func *pItem = pAggInfo->aFunc;
2374 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2375 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002376 break;
2377 }
drh22827922000-06-06 17:27:05 +00002378 }
drh13449892005-09-07 21:22:45 +00002379 if( i>=pAggInfo->nFunc ){
2380 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2381 */
danielk197714db2662006-01-09 16:12:04 +00002382 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002383 i = addAggInfoFunc(pAggInfo);
2384 if( i>=0 ){
2385 pItem = &pAggInfo->aFunc[i];
2386 pItem->pExpr = pExpr;
2387 pItem->iMem = pParse->nMem++;
2388 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002389 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002390 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002391 if( pExpr->flags & EP_Distinct ){
2392 pItem->iDistinct = pParse->nTab++;
2393 }else{
2394 pItem->iDistinct = -1;
2395 }
drh13449892005-09-07 21:22:45 +00002396 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002397 }
drh13449892005-09-07 21:22:45 +00002398 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2399 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002400 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002401 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002402 return 1;
drh22827922000-06-06 17:27:05 +00002403 }
drh22827922000-06-06 17:27:05 +00002404 }
2405 }
drh13449892005-09-07 21:22:45 +00002406
2407 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2408 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2409 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2410 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002411 if( pExpr->pSelect ){
2412 pNC->nDepth++;
2413 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2414 pNC->nDepth--;
2415 }
drh626a8792005-01-17 22:08:19 +00002416 return 0;
2417}
2418
2419/*
2420** Analyze the given expression looking for aggregate functions and
2421** for variables that need to be added to the pParse->aAgg[] array.
2422** Make additional entries to the pParse->aAgg[] array as necessary.
2423**
2424** This routine should only be called after the expression has been
2425** analyzed by sqlite3ExprResolveNames().
2426**
2427** If errors are seen, leave an error message in zErrMsg and return
2428** the number of errors.
2429*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002430int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2431 int nErr = pNC->pParse->nErr;
2432 walkExprTree(pExpr, analyzeAggregate, pNC);
2433 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002434}
drh5d9a4af2005-08-30 00:54:01 +00002435
2436/*
2437** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2438** expression list. Return the number of errors.
2439**
2440** If an error is found, the analysis is cut short.
2441*/
2442int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2443 struct ExprList_item *pItem;
2444 int i;
2445 int nErr = 0;
2446 if( pList ){
2447 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2448 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2449 }
2450 }
2451 return nErr;
2452}