blob: 828cf6050851aca90950c20169efdc4ac035f7f4 [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**
danielk1977832b2662007-05-09 11:37:22 +000015** $Id: expr.c,v 1.288 2007/05/09 11:37:23 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 }
danielk1977832b2662007-05-09 11:37:22 +0000416 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
417 sqlite3ErrorMsg(pParse, "too many SQL variables");
418 }
drhfa6bc002004-09-07 16:19:52 +0000419}
420
421/*
drha2e00042002-01-22 03:13:42 +0000422** Recursively delete an expression tree.
423*/
danielk19774adee202004-05-08 08:23:19 +0000424void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000425 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000426 if( p->span.dyn ) sqliteFree((char*)p->span.z);
427 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000428 sqlite3ExprDelete(p->pLeft);
429 sqlite3ExprDelete(p->pRight);
430 sqlite3ExprListDelete(p->pList);
431 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000432 sqliteFree(p);
433}
434
drhd2687b72005-08-12 22:56:09 +0000435/*
436** The Expr.token field might be a string literal that is quoted.
437** If so, remove the quotation marks.
438*/
439void sqlite3DequoteExpr(Expr *p){
440 if( ExprHasAnyProperty(p, EP_Dequoted) ){
441 return;
442 }
443 ExprSetProperty(p, EP_Dequoted);
444 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000445 sqlite3TokenCopy(&p->token, &p->token);
446 }
447 sqlite3Dequote((char*)p->token.z);
448}
449
drha76b5df2002-02-23 02:32:10 +0000450
451/*
drhff78bd22002-02-27 01:47:11 +0000452** The following group of routines make deep copies of expressions,
453** expression lists, ID lists, and select statements. The copies can
454** be deleted (by being passed to their respective ...Delete() routines)
455** without effecting the originals.
456**
danielk19774adee202004-05-08 08:23:19 +0000457** The expression list, ID, and source lists return by sqlite3ExprListDup(),
458** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000459** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000460**
drhad3cab52002-05-24 02:04:32 +0000461** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000462*/
danielk19774adee202004-05-08 08:23:19 +0000463Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000464 Expr *pNew;
465 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000466 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000467 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000468 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000469 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000470 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000471 pNew->token.dyn = 1;
472 }else{
drh4efc4752004-01-16 15:55:37 +0000473 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000474 }
drh6977fea2002-10-22 23:38:04 +0000475 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000476 pNew->pLeft = sqlite3ExprDup(p->pLeft);
477 pNew->pRight = sqlite3ExprDup(p->pRight);
478 pNew->pList = sqlite3ExprListDup(p->pList);
479 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000480 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000481 return pNew;
482}
danielk19774adee202004-05-08 08:23:19 +0000483void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000484 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000485 if( pFrom->z ){
486 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000487 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000488 pTo->dyn = 1;
489 }else{
drh4b59ab52002-08-24 18:24:51 +0000490 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000491 }
492}
danielk19774adee202004-05-08 08:23:19 +0000493ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000494 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000495 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000496 int i;
497 if( p==0 ) return 0;
498 pNew = sqliteMalloc( sizeof(*pNew) );
499 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000500 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000501 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000502 if( pItem==0 ){
503 sqliteFree(pNew);
504 return 0;
505 }
drh145716b2004-09-24 12:24:06 +0000506 pOldItem = p->a;
507 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000508 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000509 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000510 if( pOldExpr->span.z!=0 && pNewExpr ){
511 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000512 ** expression list. The logic in SELECT processing that determines
513 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000514 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000515 }
drh1f3e9052002-10-31 00:09:39 +0000516 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000517 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000518 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000519 pItem->zName = sqliteStrDup(pOldItem->zName);
520 pItem->sortOrder = pOldItem->sortOrder;
521 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000522 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000523 }
524 return pNew;
525}
danielk197793758c82005-01-21 08:13:14 +0000526
527/*
528** If cursors, triggers, views and subqueries are all omitted from
529** the build, then none of the following routines, except for
530** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
531** called with a NULL argument.
532*/
danielk19776a67fe82005-02-04 04:07:16 +0000533#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
534 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000535SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000536 SrcList *pNew;
537 int i;
drh113088e2003-03-20 01:16:58 +0000538 int nByte;
drhad3cab52002-05-24 02:04:32 +0000539 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000540 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000541 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000542 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000543 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000544 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000545 struct SrcList_item *pNewItem = &pNew->a[i];
546 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000547 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000548 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
549 pNewItem->zName = sqliteStrDup(pOldItem->zName);
550 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
551 pNewItem->jointype = pOldItem->jointype;
552 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000553 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000554 pTab = pNewItem->pTab = pOldItem->pTab;
555 if( pTab ){
556 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000557 }
danielk19774adee202004-05-08 08:23:19 +0000558 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
559 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
560 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000561 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000562 }
563 return pNew;
564}
danielk19774adee202004-05-08 08:23:19 +0000565IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000566 IdList *pNew;
567 int i;
568 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000569 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000570 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000571 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000572 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000573 if( pNew->a==0 ){
574 sqliteFree(pNew);
575 return 0;
576 }
drhff78bd22002-02-27 01:47:11 +0000577 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000578 struct IdList_item *pNewItem = &pNew->a[i];
579 struct IdList_item *pOldItem = &p->a[i];
580 pNewItem->zName = sqliteStrDup(pOldItem->zName);
581 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000582 }
583 return pNew;
584}
danielk19774adee202004-05-08 08:23:19 +0000585Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000586 Select *pNew;
587 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000588 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000589 if( pNew==0 ) return 0;
590 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000591 pNew->pEList = sqlite3ExprListDup(p->pEList);
592 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
593 pNew->pWhere = sqlite3ExprDup(p->pWhere);
594 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
595 pNew->pHaving = sqlite3ExprDup(p->pHaving);
596 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000597 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000598 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000599 pNew->pLimit = sqlite3ExprDup(p->pLimit);
600 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000601 pNew->iLimit = -1;
602 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000603 pNew->isResolved = p->isResolved;
604 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000605 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000606 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000607 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000608 pNew->addrOpenEphm[0] = -1;
609 pNew->addrOpenEphm[1] = -1;
610 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000611 return pNew;
612}
danielk197793758c82005-01-21 08:13:14 +0000613#else
614Select *sqlite3SelectDup(Select *p){
615 assert( p==0 );
616 return 0;
617}
618#endif
drhff78bd22002-02-27 01:47:11 +0000619
620
621/*
drha76b5df2002-02-23 02:32:10 +0000622** Add a new element to the end of an expression list. If pList is
623** initially NULL, then create a new expression list.
624*/
danielk19774adee202004-05-08 08:23:19 +0000625ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000626 if( pList==0 ){
627 pList = sqliteMalloc( sizeof(ExprList) );
628 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000629 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000630 }
drh4efc4752004-01-16 15:55:37 +0000631 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000632 }
drh4305d102003-07-30 12:34:12 +0000633 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000634 struct ExprList_item *a;
635 int n = pList->nAlloc*2 + 4;
636 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
637 if( a==0 ){
638 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000639 }
danielk1977d5d56522005-03-16 12:15:20 +0000640 pList->a = a;
641 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000642 }
drh4efc4752004-01-16 15:55:37 +0000643 assert( pList->a!=0 );
644 if( pExpr || pName ){
645 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
646 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000647 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000648 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000649 }
650 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000651
652no_mem:
653 /* Avoid leaking memory if malloc has failed. */
654 sqlite3ExprDelete(pExpr);
655 sqlite3ExprListDelete(pList);
656 return 0;
drha76b5df2002-02-23 02:32:10 +0000657}
658
659/*
danielk19777a15a4b2007-05-08 17:54:43 +0000660** If the expression list pEList contains more than iLimit elements,
661** leave an error message in pParse.
662*/
663void sqlite3ExprListCheckLength(
664 Parse *pParse,
665 ExprList *pEList,
666 int iLimit,
667 const char *zObject
668){
danielk1977b4fc6792007-05-08 18:04:46 +0000669 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000670 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
671 }
672}
673
674/*
drha76b5df2002-02-23 02:32:10 +0000675** Delete an entire expression list.
676*/
danielk19774adee202004-05-08 08:23:19 +0000677void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000678 int i;
drhbe5c89a2004-07-26 00:31:09 +0000679 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000680 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000681 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
682 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000683 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
684 sqlite3ExprDelete(pItem->pExpr);
685 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000686 }
687 sqliteFree(pList->a);
688 sqliteFree(pList);
689}
690
691/*
drh626a8792005-01-17 22:08:19 +0000692** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000693**
drh626a8792005-01-17 22:08:19 +0000694** The return value from xFunc determines whether the tree walk continues.
695** 0 means continue walking the tree. 1 means do not walk children
696** of the current node but continue with siblings. 2 means abandon
697** the tree walk completely.
698**
699** The return value from this routine is 1 to abandon the tree walk
700** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000701**
702** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000703*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000704static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000705static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000706 int rc;
707 if( pExpr==0 ) return 0;
708 rc = (*xFunc)(pArg, pExpr);
709 if( rc==0 ){
710 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
711 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000712 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000713 }
714 return rc>1;
715}
716
717/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000718** Call walkExprTree() for every expression in list p.
719*/
720static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
721 int i;
722 struct ExprList_item *pItem;
723 if( !p ) return 0;
724 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
725 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
726 }
727 return 0;
728}
729
730/*
731** Call walkExprTree() for every expression in Select p, not including
732** expressions that are part of sub-selects in any FROM clause or the LIMIT
733** or OFFSET expressions..
734*/
735static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
736 walkExprList(p->pEList, xFunc, pArg);
737 walkExprTree(p->pWhere, xFunc, pArg);
738 walkExprList(p->pGroupBy, xFunc, pArg);
739 walkExprTree(p->pHaving, xFunc, pArg);
740 walkExprList(p->pOrderBy, xFunc, pArg);
741 return 0;
742}
743
744
745/*
drh626a8792005-01-17 22:08:19 +0000746** This routine is designed as an xFunc for walkExprTree().
747**
748** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000749** at pExpr that the expression that contains pExpr is not a constant
750** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
751** If pExpr does does not disqualify the expression from being a constant
752** then do nothing.
753**
754** After walking the whole tree, if no nodes are found that disqualify
755** the expression as constant, then we assume the whole expression
756** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000757*/
758static int exprNodeIsConstant(void *pArg, Expr *pExpr){
759 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000760 /* Consider functions to be constant if all their arguments are constant
761 ** and *pArg==2 */
762 case TK_FUNCTION:
763 if( *((int*)pArg)==2 ) return 0;
764 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000765 case TK_ID:
766 case TK_COLUMN:
767 case TK_DOT:
768 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000769 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000770#ifndef SQLITE_OMIT_SUBQUERY
771 case TK_SELECT:
772 case TK_EXISTS:
773#endif
drh626a8792005-01-17 22:08:19 +0000774 *((int*)pArg) = 0;
775 return 2;
drh87abf5c2005-08-25 12:45:04 +0000776 case TK_IN:
777 if( pExpr->pSelect ){
778 *((int*)pArg) = 0;
779 return 2;
780 }
drh626a8792005-01-17 22:08:19 +0000781 default:
782 return 0;
783 }
784}
785
786/*
drhfef52082000-06-06 01:50:43 +0000787** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000788** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000789**
790** For the purposes of this function, a double-quoted string (ex: "abc")
791** is considered a variable but a single-quoted string (ex: 'abc') is
792** a constant.
drhfef52082000-06-06 01:50:43 +0000793*/
danielk19774adee202004-05-08 08:23:19 +0000794int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000795 int isConst = 1;
796 walkExprTree(p, exprNodeIsConstant, &isConst);
797 return isConst;
drhfef52082000-06-06 01:50:43 +0000798}
799
800/*
drheb55bd22005-06-30 17:04:21 +0000801** Walk an expression tree. Return 1 if the expression is constant
802** or a function call with constant arguments. Return and 0 if there
803** are any variables.
804**
805** For the purposes of this function, a double-quoted string (ex: "abc")
806** is considered a variable but a single-quoted string (ex: 'abc') is
807** a constant.
808*/
809int sqlite3ExprIsConstantOrFunction(Expr *p){
810 int isConst = 2;
811 walkExprTree(p, exprNodeIsConstant, &isConst);
812 return isConst!=0;
813}
814
815/*
drh73b211a2005-01-18 04:00:42 +0000816** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000817** to fit in a 32-bit integer, return 1 and put the value of the integer
818** in *pValue. If the expression is not an integer or if it is too big
819** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000820*/
danielk19774adee202004-05-08 08:23:19 +0000821int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000822 switch( p->op ){
823 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000824 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000825 return 1;
826 }
827 break;
drhe4de1fe2002-06-02 16:09:01 +0000828 }
drh4b59ab52002-08-24 18:24:51 +0000829 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000830 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000831 }
drhe4de1fe2002-06-02 16:09:01 +0000832 case TK_UMINUS: {
833 int v;
danielk19774adee202004-05-08 08:23:19 +0000834 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000835 *pValue = -v;
836 return 1;
837 }
838 break;
839 }
840 default: break;
841 }
842 return 0;
843}
844
845/*
drhc4a3c772001-04-04 11:48:57 +0000846** Return TRUE if the given string is a row-id column name.
847*/
danielk19774adee202004-05-08 08:23:19 +0000848int sqlite3IsRowid(const char *z){
849 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
850 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
851 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000852 return 0;
853}
854
855/*
drh8141f612004-01-25 22:44:58 +0000856** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
857** that name in the set of source tables in pSrcList and make the pExpr
858** expression node refer back to that source column. The following changes
859** are made to pExpr:
860**
861** pExpr->iDb Set the index in db->aDb[] of the database holding
862** the table.
863** pExpr->iTable Set to the cursor number for the table obtained
864** from pSrcList.
865** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000866** pExpr->op Set to TK_COLUMN.
867** pExpr->pLeft Any expression this points to is deleted
868** pExpr->pRight Any expression this points to is deleted.
869**
870** The pDbToken is the name of the database (the "X"). This value may be
871** NULL meaning that name is of the form Y.Z or Z. Any available database
872** can be used. The pTableToken is the name of the table (the "Y"). This
873** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
874** means that the form of the name is Z and that columns from any table
875** can be used.
876**
877** If the name cannot be resolved unambiguously, leave an error message
878** in pParse and return non-zero. Return zero on success.
879*/
880static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000881 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000882 Token *pDbToken, /* Name of the database containing table, or NULL */
883 Token *pTableToken, /* Name of table containing column, or NULL */
884 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000885 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000886 Expr *pExpr /* Make this EXPR node point to the selected column */
887){
888 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
889 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
890 char *zCol = 0; /* Name of the column. The "Z" */
891 int i, j; /* Loop counters */
892 int cnt = 0; /* Number of matching column names */
893 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000894 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000895 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
896 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000897 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000898
899 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000900 zDb = sqlite3NameFromToken(pDbToken);
901 zTab = sqlite3NameFromToken(pTableToken);
902 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000903 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000904 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000905 }
drh8141f612004-01-25 22:44:58 +0000906
907 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000908 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000909 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000910 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000911
danielk1977b3bce662005-01-29 08:32:43 +0000912 if( pSrcList ){
913 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000914 Table *pTab;
915 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000916 Column *pCol;
917
drh43617e92006-03-06 20:55:46 +0000918 pTab = pItem->pTab;
919 assert( pTab!=0 );
920 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000921 assert( pTab->nCol>0 );
922 if( zTab ){
923 if( pItem->zAlias ){
924 char *zTabName = pItem->zAlias;
925 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
926 }else{
927 char *zTabName = pTab->zName;
928 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000929 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000930 continue;
931 }
drh626a8792005-01-17 22:08:19 +0000932 }
drh8141f612004-01-25 22:44:58 +0000933 }
danielk1977b3bce662005-01-29 08:32:43 +0000934 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000935 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000936 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000937 pMatch = pItem;
938 }
939 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
940 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000941 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000942 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000943 cnt++;
944 pExpr->iTable = pItem->iCursor;
945 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000946 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000947 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
948 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
949 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000950 if( (pExpr->flags & EP_ExpCollate)==0 ){
951 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
952 }
drh61dfc312006-12-16 16:25:15 +0000953 if( i<pSrcList->nSrc-1 ){
954 if( pItem[1].jointype & JT_NATURAL ){
955 /* If this match occurred in the left table of a natural join,
956 ** then skip the right table to avoid a duplicate match */
957 pItem++;
958 i++;
959 }else if( (pUsing = pItem[1].pUsing)!=0 ){
960 /* If this match occurs on a column that is in the USING clause
961 ** of a join, skip the search of the right table of the join
962 ** to avoid a duplicate match there. */
963 int k;
964 for(k=0; k<pUsing->nId; k++){
965 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
966 pItem++;
967 i++;
968 break;
969 }
drh873fac02005-06-06 17:11:46 +0000970 }
971 }
972 }
danielk1977b3bce662005-01-29 08:32:43 +0000973 break;
974 }
drh8141f612004-01-25 22:44:58 +0000975 }
976 }
977 }
drh626a8792005-01-17 22:08:19 +0000978
979#ifndef SQLITE_OMIT_TRIGGER
980 /* If we have not already resolved the name, then maybe
981 ** it is a new.* or old.* trigger argument reference
982 */
983 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
984 TriggerStack *pTriggerStack = pParse->trigStack;
985 Table *pTab = 0;
986 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
987 pExpr->iTable = pTriggerStack->newIdx;
988 assert( pTriggerStack->pTab );
989 pTab = pTriggerStack->pTab;
990 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
991 pExpr->iTable = pTriggerStack->oldIdx;
992 assert( pTriggerStack->pTab );
993 pTab = pTriggerStack->pTab;
994 }
995
996 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +0000997 int iCol;
drh626a8792005-01-17 22:08:19 +0000998 Column *pCol = pTab->aCol;
999
danielk1977da184232006-01-05 11:34:32 +00001000 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001001 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001002 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001003 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001004 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001005 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001006 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1007 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001008 if( (pExpr->flags & EP_ExpCollate)==0 ){
1009 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1010 }
danielk1977aee18ef2005-03-09 12:26:50 +00001011 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001012 break;
1013 }
1014 }
1015 }
1016 }
drhb7f91642004-10-31 02:22:47 +00001017#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001018
drh626a8792005-01-17 22:08:19 +00001019 /*
1020 ** Perhaps the name is a reference to the ROWID
1021 */
1022 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1023 cnt = 1;
1024 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001025 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001026 }
drh8141f612004-01-25 22:44:58 +00001027
drh626a8792005-01-17 22:08:19 +00001028 /*
1029 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1030 ** might refer to an result-set alias. This happens, for example, when
1031 ** we are resolving names in the WHERE clause of the following command:
1032 **
1033 ** SELECT a+b AS x FROM table WHERE x<10;
1034 **
1035 ** In cases like this, replace pExpr with a copy of the expression that
1036 ** forms the result set entry ("a+b" in the example) and return immediately.
1037 ** Note that the expression in the result set should have already been
1038 ** resolved by the time the WHERE clause is resolved.
1039 */
drhffe07b22005-11-03 00:41:17 +00001040 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001041 for(j=0; j<pEList->nExpr; j++){
1042 char *zAs = pEList->a[j].zName;
1043 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1044 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1045 pExpr->op = TK_AS;
1046 pExpr->iColumn = j;
1047 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001048 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001049 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001050 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001051 }
1052 }
1053 }
1054
1055 /* Advance to the next name context. The loop will exit when either
1056 ** we have a match (cnt>0) or when we run out of name contexts.
1057 */
1058 if( cnt==0 ){
1059 pNC = pNC->pNext;
1060 }
drh8141f612004-01-25 22:44:58 +00001061 }
1062
1063 /*
1064 ** If X and Y are NULL (in other words if only the column name Z is
1065 ** supplied) and the value of Z is enclosed in double-quotes, then
1066 ** Z is a string literal if it doesn't match any column names. In that
1067 ** case, we need to return right away and not make any changes to
1068 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001069 **
1070 ** Because no reference was made to outer contexts, the pNC->nRef
1071 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001072 */
1073 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1074 sqliteFree(zCol);
1075 return 0;
1076 }
1077
1078 /*
1079 ** cnt==0 means there was not match. cnt>1 means there were two or
1080 ** more matches. Either way, we have an error.
1081 */
1082 if( cnt!=1 ){
1083 char *z = 0;
1084 char *zErr;
1085 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1086 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001087 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001088 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001089 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001090 }else{
1091 z = sqliteStrDup(zCol);
1092 }
danielk19774adee202004-05-08 08:23:19 +00001093 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001094 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001095 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001096 }
1097
drh51669862004-12-18 18:40:26 +00001098 /* If a column from a table in pSrcList is referenced, then record
1099 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1100 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1101 ** column number is greater than the number of bits in the bitmask
1102 ** then set the high-order bit of the bitmask.
1103 */
1104 if( pExpr->iColumn>=0 && pMatch!=0 ){
1105 int n = pExpr->iColumn;
1106 if( n>=sizeof(Bitmask)*8 ){
1107 n = sizeof(Bitmask)*8-1;
1108 }
1109 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001110 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001111 }
1112
danielk1977d5d56522005-03-16 12:15:20 +00001113lookupname_end:
drh8141f612004-01-25 22:44:58 +00001114 /* Clean up and return
1115 */
1116 sqliteFree(zDb);
1117 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001118 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001119 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001120 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001121 pExpr->pRight = 0;
1122 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001123lookupname_end_2:
1124 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001125 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001126 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001127 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001128 if( pMatch && !pMatch->pSelect ){
1129 pExpr->pTab = pMatch->pTab;
1130 }
drh15ccce12005-05-23 15:06:39 +00001131 /* Increment the nRef value on all name contexts from TopNC up to
1132 ** the point where the name matched. */
1133 for(;;){
1134 assert( pTopNC!=0 );
1135 pTopNC->nRef++;
1136 if( pTopNC==pNC ) break;
1137 pTopNC = pTopNC->pNext;
1138 }
1139 return 0;
1140 } else {
1141 return 1;
drh626a8792005-01-17 22:08:19 +00001142 }
drh8141f612004-01-25 22:44:58 +00001143}
1144
1145/*
drh626a8792005-01-17 22:08:19 +00001146** This routine is designed as an xFunc for walkExprTree().
1147**
drh73b211a2005-01-18 04:00:42 +00001148** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001149** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001150** the tree or 2 to abort the tree walk.
1151**
1152** This routine also does error checking and name resolution for
1153** function names. The operator for aggregate functions is changed
1154** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001155*/
1156static int nameResolverStep(void *pArg, Expr *pExpr){
1157 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001158 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001159
danielk1977b3bce662005-01-29 08:32:43 +00001160 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001161 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001162 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001163
drh626a8792005-01-17 22:08:19 +00001164 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1165 ExprSetProperty(pExpr, EP_Resolved);
1166#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001167 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1168 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001169 int i;
danielk1977f0113002006-01-24 12:09:17 +00001170 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001171 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1172 }
1173 }
1174#endif
1175 switch( pExpr->op ){
1176 /* Double-quoted strings (ex: "abc") are used as identifiers if
1177 ** possible. Otherwise they remain as strings. Single-quoted
1178 ** strings (ex: 'abc') are always string literals.
1179 */
1180 case TK_STRING: {
1181 if( pExpr->token.z[0]=='\'' ) break;
1182 /* Fall thru into the TK_ID case if this is a double-quoted string */
1183 }
1184 /* A lone identifier is the name of a column.
1185 */
1186 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001187 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1188 return 1;
1189 }
1190
1191 /* A table name and column name: ID.ID
1192 ** Or a database, table and column: ID.ID.ID
1193 */
1194 case TK_DOT: {
1195 Token *pColumn;
1196 Token *pTable;
1197 Token *pDb;
1198 Expr *pRight;
1199
danielk1977b3bce662005-01-29 08:32:43 +00001200 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001201 pRight = pExpr->pRight;
1202 if( pRight->op==TK_ID ){
1203 pDb = 0;
1204 pTable = &pExpr->pLeft->token;
1205 pColumn = &pRight->token;
1206 }else{
1207 assert( pRight->op==TK_DOT );
1208 pDb = &pExpr->pLeft->token;
1209 pTable = &pRight->pLeft->token;
1210 pColumn = &pRight->pRight->token;
1211 }
1212 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1213 return 1;
1214 }
1215
1216 /* Resolve function names
1217 */
drhb71090f2005-05-23 17:26:51 +00001218 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001219 case TK_FUNCTION: {
1220 ExprList *pList = pExpr->pList; /* The argument list */
1221 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1222 int no_such_func = 0; /* True if no such function exists */
1223 int wrong_num_args = 0; /* True if wrong number of arguments */
1224 int is_agg = 0; /* True if is an aggregate function */
1225 int i;
drh5169bbc2006-08-24 14:59:45 +00001226 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001227 int nId; /* Number of characters in function name */
1228 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001229 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001230 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001231
drh2646da72005-12-09 20:02:05 +00001232 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001233 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001234 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1235 if( pDef==0 ){
1236 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1237 if( pDef==0 ){
1238 no_such_func = 1;
1239 }else{
1240 wrong_num_args = 1;
1241 }
1242 }else{
1243 is_agg = pDef->xFunc==0;
1244 }
drh2fca7fe2006-11-23 11:59:13 +00001245#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001246 if( pDef ){
1247 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1248 if( auth!=SQLITE_OK ){
1249 if( auth==SQLITE_DENY ){
1250 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1251 pDef->zName);
1252 pNC->nErr++;
1253 }
1254 pExpr->op = TK_NULL;
1255 return 1;
1256 }
1257 }
drhb8b14212006-08-24 15:18:25 +00001258#endif
drh626a8792005-01-17 22:08:19 +00001259 if( is_agg && !pNC->allowAgg ){
1260 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1261 pNC->nErr++;
1262 is_agg = 0;
1263 }else if( no_such_func ){
1264 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1265 pNC->nErr++;
1266 }else if( wrong_num_args ){
1267 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1268 nId, zId);
1269 pNC->nErr++;
1270 }
1271 if( is_agg ){
1272 pExpr->op = TK_AGG_FUNCTION;
1273 pNC->hasAgg = 1;
1274 }
drh73b211a2005-01-18 04:00:42 +00001275 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001276 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001277 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001278 }
drh73b211a2005-01-18 04:00:42 +00001279 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001280 /* FIX ME: Compute pExpr->affinity based on the expected return
1281 ** type of the function
1282 */
1283 return is_agg;
1284 }
danielk1977b3bce662005-01-29 08:32:43 +00001285#ifndef SQLITE_OMIT_SUBQUERY
1286 case TK_SELECT:
1287 case TK_EXISTS:
1288#endif
1289 case TK_IN: {
1290 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001291 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001292#ifndef SQLITE_OMIT_CHECK
1293 if( pNC->isCheck ){
1294 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1295 }
1296#endif
danielk1977b3bce662005-01-29 08:32:43 +00001297 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1298 assert( pNC->nRef>=nRef );
1299 if( nRef!=pNC->nRef ){
1300 ExprSetProperty(pExpr, EP_VarSelect);
1301 }
1302 }
drh4284fb02005-11-03 12:33:28 +00001303 break;
danielk1977b3bce662005-01-29 08:32:43 +00001304 }
drh4284fb02005-11-03 12:33:28 +00001305#ifndef SQLITE_OMIT_CHECK
1306 case TK_VARIABLE: {
1307 if( pNC->isCheck ){
1308 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1309 }
1310 break;
1311 }
1312#endif
drh626a8792005-01-17 22:08:19 +00001313 }
1314 return 0;
1315}
1316
1317/*
drhcce7d172000-05-31 15:34:51 +00001318** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001319** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001320** index to the table in the table list and a column offset. The
1321** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1322** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001323** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001324** VDBE cursor number for a cursor that is pointing into the referenced
1325** table. The Expr.iColumn value is changed to the index of the column
1326** of the referenced table. The Expr.iColumn value for the special
1327** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1328** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001329**
drh626a8792005-01-17 22:08:19 +00001330** Also resolve function names and check the functions for proper
1331** usage. Make sure all function names are recognized and all functions
1332** have the correct number of arguments. Leave an error message
1333** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1334**
drh73b211a2005-01-18 04:00:42 +00001335** If the expression contains aggregate functions then set the EP_Agg
1336** property on the expression.
drh626a8792005-01-17 22:08:19 +00001337*/
1338int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001339 NameContext *pNC, /* Namespace to resolve expressions in. */
1340 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001341){
drh13449892005-09-07 21:22:45 +00001342 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001343 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001344 savedHasAgg = pNC->hasAgg;
1345 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001346 walkExprTree(pExpr, nameResolverStep, pNC);
1347 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001348 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001349 }
drh13449892005-09-07 21:22:45 +00001350 if( pNC->hasAgg ){
1351 ExprSetProperty(pExpr, EP_Agg);
1352 }else if( savedHasAgg ){
1353 pNC->hasAgg = 1;
1354 }
drh73b211a2005-01-18 04:00:42 +00001355 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001356}
1357
drh1398ad32005-01-19 23:24:50 +00001358/*
1359** A pointer instance of this structure is used to pass information
1360** through walkExprTree into codeSubqueryStep().
1361*/
1362typedef struct QueryCoder QueryCoder;
1363struct QueryCoder {
1364 Parse *pParse; /* The parsing context */
1365 NameContext *pNC; /* Namespace of first enclosing query */
1366};
1367
drh626a8792005-01-17 22:08:19 +00001368
1369/*
drh9cbe6352005-11-29 03:13:21 +00001370** Generate code for scalar subqueries used as an expression
1371** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001372**
drh9cbe6352005-11-29 03:13:21 +00001373** (SELECT a FROM b) -- subquery
1374** EXISTS (SELECT a FROM b) -- EXISTS subquery
1375** x IN (4,5,11) -- IN operator with list on right-hand side
1376** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001377**
drh9cbe6352005-11-29 03:13:21 +00001378** The pExpr parameter describes the expression that contains the IN
1379** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001380*/
drh51522cd2005-01-20 13:36:19 +00001381#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001382void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001383 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001384 Vdbe *v = sqlite3GetVdbe(pParse);
1385 if( v==0 ) return;
1386
drh57dbd7b2005-07-08 18:25:26 +00001387 /* This code must be run in its entirety every time it is encountered
1388 ** if any of the following is true:
1389 **
1390 ** * The right-hand side is a correlated subquery
1391 ** * The right-hand side is an expression list containing variables
1392 ** * We are inside a trigger
1393 **
1394 ** If all of the above are false, then we can run this code just once
1395 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001396 */
1397 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1398 int mem = pParse->nMem++;
1399 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001400 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001401 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001402 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001403 }
1404
drhcce7d172000-05-31 15:34:51 +00001405 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001406 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001407 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001408 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001409 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001410
danielk1977bf3b7212004-05-18 10:06:24 +00001411 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001412
1413 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001414 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001415 ** filled with single-field index keys representing the results
1416 ** from the SELECT or the <exprlist>.
1417 **
1418 ** If the 'x' expression is a column value, or the SELECT...
1419 ** statement returns a column value, then the affinity of that
1420 ** column is used to build the index keys. If both 'x' and the
1421 ** SELECT... statement are columns, then numeric affinity is used
1422 ** if either column has NUMERIC or INTEGER affinity. If neither
1423 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1424 ** is used.
1425 */
1426 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001427 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001428 memset(&keyInfo, 0, sizeof(keyInfo));
1429 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001430 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001431
drhfef52082000-06-06 01:50:43 +00001432 if( pExpr->pSelect ){
1433 /* Case 1: expr IN (SELECT ...)
1434 **
danielk1977e014a832004-05-17 10:48:57 +00001435 ** Generate code to write the results of the select into the temporary
1436 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001437 */
danielk1977e014a832004-05-17 10:48:57 +00001438 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001439 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001440 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001441 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1442 return;
1443 }
drhbe5c89a2004-07-26 00:31:09 +00001444 pEList = pExpr->pSelect->pEList;
1445 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001446 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001447 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001448 }
drhfef52082000-06-06 01:50:43 +00001449 }else if( pExpr->pList ){
1450 /* Case 2: expr IN (exprlist)
1451 **
danielk1977e014a832004-05-17 10:48:57 +00001452 ** For each expression, build an index key from the evaluation and
1453 ** store it in the temporary table. If <expr> is a column, then use
1454 ** that columns affinity when building index keys. If <expr> is not
1455 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001456 */
danielk1977e014a832004-05-17 10:48:57 +00001457 int i;
drh57dbd7b2005-07-08 18:25:26 +00001458 ExprList *pList = pExpr->pList;
1459 struct ExprList_item *pItem;
1460
danielk1977e014a832004-05-17 10:48:57 +00001461 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001462 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001463 }
danielk19770202b292004-06-09 09:55:16 +00001464 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001465
1466 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001467 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1468 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001469
drh57dbd7b2005-07-08 18:25:26 +00001470 /* If the expression is not constant then we will need to
1471 ** disable the test that was generated above that makes sure
1472 ** this code only executes once. Because for a non-constant
1473 ** expression we need to rerun this code each time.
1474 */
drh6c30be82005-07-29 15:10:17 +00001475 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001476 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001477 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001478 }
danielk1977e014a832004-05-17 10:48:57 +00001479
1480 /* Evaluate the expression and insert it into the temp table */
1481 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001482 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001483 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001484 }
1485 }
danielk19770202b292004-06-09 09:55:16 +00001486 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001487 break;
drhfef52082000-06-06 01:50:43 +00001488 }
1489
drh51522cd2005-01-20 13:36:19 +00001490 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001491 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001492 /* This has to be a scalar SELECT. Generate code to put the
1493 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001494 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001495 */
drh2646da72005-12-09 20:02:05 +00001496 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001497 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001498 int iMem;
1499 int sop;
drh1398ad32005-01-19 23:24:50 +00001500
drhec7429a2005-10-06 16:53:14 +00001501 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001502 pSel = pExpr->pSelect;
1503 if( pExpr->op==TK_SELECT ){
1504 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001505 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1506 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001507 }else{
drh51522cd2005-01-20 13:36:19 +00001508 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001509 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1510 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001511 }
drhec7429a2005-10-06 16:53:14 +00001512 sqlite3ExprDelete(pSel->pLimit);
1513 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001514 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1515 return;
1516 }
danielk1977b3bce662005-01-29 08:32:43 +00001517 break;
drhcce7d172000-05-31 15:34:51 +00001518 }
1519 }
danielk1977b3bce662005-01-29 08:32:43 +00001520
drh57dbd7b2005-07-08 18:25:26 +00001521 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001522 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001523 }
1524 return;
drhcce7d172000-05-31 15:34:51 +00001525}
drh51522cd2005-01-20 13:36:19 +00001526#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001527
drhcce7d172000-05-31 15:34:51 +00001528/*
drhfec19aa2004-05-19 20:41:03 +00001529** Generate an instruction that will put the integer describe by
1530** text z[0..n-1] on the stack.
1531*/
1532static void codeInteger(Vdbe *v, const char *z, int n){
1533 int i;
drh6fec0762004-05-30 01:38:43 +00001534 if( sqlite3GetInt32(z, &i) ){
1535 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1536 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001537 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001538 }else{
1539 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1540 }
1541}
1542
drh945498f2007-02-24 11:52:52 +00001543
1544/*
1545** Generate code that will extract the iColumn-th column from
1546** table pTab and push that column value on the stack. There
1547** is an open cursor to pTab in iTable. If iColumn<0 then
1548** code is generated that extracts the rowid.
1549*/
1550void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1551 if( iColumn<0 ){
1552 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1553 sqlite3VdbeAddOp(v, op, iTable, 0);
1554 }else if( pTab==0 ){
1555 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1556 }else{
1557 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1558 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1559 sqlite3ColumnDefault(v, pTab, iColumn);
1560#ifndef SQLITE_OMIT_FLOATING_POINT
1561 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1562 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1563 }
1564#endif
1565 }
1566}
1567
drhfec19aa2004-05-19 20:41:03 +00001568/*
drhcce7d172000-05-31 15:34:51 +00001569** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001570** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001571**
1572** This code depends on the fact that certain token values (ex: TK_EQ)
1573** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1574** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1575** the make process cause these values to align. Assert()s in the code
1576** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001577*/
danielk19774adee202004-05-08 08:23:19 +00001578void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001579 Vdbe *v = pParse->pVdbe;
1580 int op;
drhffe07b22005-11-03 00:41:17 +00001581 int stackChng = 1; /* Amount of change to stack depth */
1582
danielk19777977a172004-11-09 12:44:37 +00001583 if( v==0 ) return;
1584 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001585 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001586 return;
1587 }
drhf2bc0132004-10-04 13:19:23 +00001588 op = pExpr->op;
1589 switch( op ){
drh13449892005-09-07 21:22:45 +00001590 case TK_AGG_COLUMN: {
1591 AggInfo *pAggInfo = pExpr->pAggInfo;
1592 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1593 if( !pAggInfo->directMode ){
1594 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1595 break;
1596 }else if( pAggInfo->useSortingIdx ){
1597 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1598 pCol->iSorterColumn);
1599 break;
1600 }
1601 /* Otherwise, fall thru into the TK_COLUMN case */
1602 }
drh967e8b72000-06-21 13:59:10 +00001603 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001604 if( pExpr->iTable<0 ){
1605 /* This only happens when coding check constraints */
1606 assert( pParse->ckOffset>0 );
1607 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001608 }else{
drh945498f2007-02-24 11:52:52 +00001609 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001610 }
drhcce7d172000-05-31 15:34:51 +00001611 break;
1612 }
1613 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001614 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001615 break;
1616 }
1617 case TK_FLOAT:
1618 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001619 assert( TK_FLOAT==OP_Real );
1620 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001621 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001622 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001623 break;
1624 }
drhf0863fe2005-06-12 21:35:51 +00001625 case TK_NULL: {
1626 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1627 break;
1628 }
danielk19775338a5f2005-01-20 13:03:10 +00001629#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001630 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001631 int n;
1632 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001633 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001634 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001635 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001636 assert( n>=0 );
1637 if( n==0 ){
1638 z = "";
1639 }
1640 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001641 break;
1642 }
danielk19775338a5f2005-01-20 13:03:10 +00001643#endif
drh50457892003-09-06 01:10:47 +00001644 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001645 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001646 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001647 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001648 }
drh50457892003-09-06 01:10:47 +00001649 break;
1650 }
drh4e0cff62004-11-05 05:10:28 +00001651 case TK_REGISTER: {
1652 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1653 break;
1654 }
drh487e2622005-06-25 18:42:14 +00001655#ifndef SQLITE_OMIT_CAST
1656 case TK_CAST: {
1657 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001658 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001659 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001660 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001661 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1662 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1663 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1664 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1665 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1666 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1667 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001668 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001669 break;
1670 }
1671#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001672 case TK_LT:
1673 case TK_LE:
1674 case TK_GT:
1675 case TK_GE:
1676 case TK_NE:
1677 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001678 assert( TK_LT==OP_Lt );
1679 assert( TK_LE==OP_Le );
1680 assert( TK_GT==OP_Gt );
1681 assert( TK_GE==OP_Ge );
1682 assert( TK_EQ==OP_Eq );
1683 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001684 sqlite3ExprCode(pParse, pExpr->pLeft);
1685 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001686 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001687 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001688 break;
drhc9b84a12002-06-20 11:36:48 +00001689 }
drhcce7d172000-05-31 15:34:51 +00001690 case TK_AND:
1691 case TK_OR:
1692 case TK_PLUS:
1693 case TK_STAR:
1694 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001695 case TK_REM:
1696 case TK_BITAND:
1697 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001698 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001699 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001700 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001701 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001702 assert( TK_AND==OP_And );
1703 assert( TK_OR==OP_Or );
1704 assert( TK_PLUS==OP_Add );
1705 assert( TK_MINUS==OP_Subtract );
1706 assert( TK_REM==OP_Remainder );
1707 assert( TK_BITAND==OP_BitAnd );
1708 assert( TK_BITOR==OP_BitOr );
1709 assert( TK_SLASH==OP_Divide );
1710 assert( TK_LSHIFT==OP_ShiftLeft );
1711 assert( TK_RSHIFT==OP_ShiftRight );
1712 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001713 sqlite3ExprCode(pParse, pExpr->pLeft);
1714 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001715 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001716 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001717 break;
1718 }
drhcce7d172000-05-31 15:34:51 +00001719 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001720 Expr *pLeft = pExpr->pLeft;
1721 assert( pLeft );
1722 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1723 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001724 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001725 if( pLeft->op==TK_FLOAT ){
1726 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001727 }else{
drhfec19aa2004-05-19 20:41:03 +00001728 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001729 }
drh6e142f52000-06-08 13:36:40 +00001730 sqliteFree(z);
1731 break;
1732 }
drh1ccde152000-06-17 13:12:39 +00001733 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001734 }
drhbf4133c2001-10-13 02:59:08 +00001735 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001736 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001737 assert( TK_BITNOT==OP_BitNot );
1738 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001739 sqlite3ExprCode(pParse, pExpr->pLeft);
1740 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001741 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001742 break;
1743 }
1744 case TK_ISNULL:
1745 case TK_NOTNULL: {
1746 int dest;
drhf2bc0132004-10-04 13:19:23 +00001747 assert( TK_ISNULL==OP_IsNull );
1748 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001749 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1750 sqlite3ExprCode(pParse, pExpr->pLeft);
1751 dest = sqlite3VdbeCurrentAddr(v) + 2;
1752 sqlite3VdbeAddOp(v, op, 1, dest);
1753 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001754 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001755 break;
drhcce7d172000-05-31 15:34:51 +00001756 }
drh22827922000-06-06 17:27:05 +00001757 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001758 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001759 if( pInfo==0 ){
1760 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1761 &pExpr->span);
1762 }else{
1763 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1764 }
drh22827922000-06-06 17:27:05 +00001765 break;
1766 }
drhb71090f2005-05-23 17:26:51 +00001767 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001768 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001769 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001770 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001771 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001772 int nId;
1773 const char *zId;
drh13449892005-09-07 21:22:45 +00001774 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001775 int i;
danielk197714db2662006-01-09 16:12:04 +00001776 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001777 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001778 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001779 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001780 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001781 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001782 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001783#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001784 /* Possibly overload the function if the first argument is
1785 ** a virtual table column.
1786 **
1787 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1788 ** second argument, not the first, as the argument to test to
1789 ** see if it is a column in a virtual table. This is done because
1790 ** the left operand of infix functions (the operand we want to
1791 ** control overloading) ends up as the second argument to the
1792 ** function. The expression "A glob B" is equivalent to
1793 ** "glob(B,A). We want to use the A in "A glob B" to test
1794 ** for function overloading. But we use the B term in "glob(B,A)".
1795 */
drh6a03a1c2006-07-08 18:34:59 +00001796 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001797 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1798 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001799 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1800 }
1801#endif
danielk1977682f68b2004-06-05 10:22:17 +00001802 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001803 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001804 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001805 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001806 if( pDef->needCollSeq && !pColl ){
1807 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1808 }
1809 }
1810 if( pDef->needCollSeq ){
1811 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001812 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001813 }
drh13449892005-09-07 21:22:45 +00001814 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001815 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001816 break;
1817 }
drhfe2093d2005-01-20 22:48:47 +00001818#ifndef SQLITE_OMIT_SUBQUERY
1819 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001820 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001821 if( pExpr->iColumn==0 ){
1822 sqlite3CodeSubselect(pParse, pExpr);
1823 }
danielk19774adee202004-05-08 08:23:19 +00001824 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001825 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001826 break;
1827 }
drhfef52082000-06-06 01:50:43 +00001828 case TK_IN: {
1829 int addr;
drh94a11212004-09-25 13:12:14 +00001830 char affinity;
drhafa5f682006-01-30 14:36:59 +00001831 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001832 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001833
1834 /* Figure out the affinity to use to create a key from the results
1835 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001836 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001837 */
drh94a11212004-09-25 13:12:14 +00001838 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001839
danielk19774adee202004-05-08 08:23:19 +00001840 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001841 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001842
1843 /* Code the <expr> from "<expr> IN (...)". The temporary table
1844 ** pExpr->iTable contains the values that make up the (...) set.
1845 */
danielk19774adee202004-05-08 08:23:19 +00001846 sqlite3ExprCode(pParse, pExpr->pLeft);
1847 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001848 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001849 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001850 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001851 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001852 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001853 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1854 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1855
drhfef52082000-06-06 01:50:43 +00001856 break;
1857 }
danielk197793758c82005-01-21 08:13:14 +00001858#endif
drhfef52082000-06-06 01:50:43 +00001859 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001860 Expr *pLeft = pExpr->pLeft;
1861 struct ExprList_item *pLItem = pExpr->pList->a;
1862 Expr *pRight = pLItem->pExpr;
1863 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001864 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001865 sqlite3ExprCode(pParse, pRight);
1866 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001867 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001868 pLItem++;
1869 pRight = pLItem->pExpr;
1870 sqlite3ExprCode(pParse, pRight);
1871 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001872 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001873 break;
1874 }
drh51e9a442004-01-16 16:42:53 +00001875 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001876 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001877 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001878 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001879 break;
1880 }
drh17a7f8d2002-03-24 13:13:27 +00001881 case TK_CASE: {
1882 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001883 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001884 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001885 int i;
drhbe5c89a2004-07-26 00:31:09 +00001886 ExprList *pEList;
1887 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001888
1889 assert(pExpr->pList);
1890 assert((pExpr->pList->nExpr % 2) == 0);
1891 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001892 pEList = pExpr->pList;
1893 aListelem = pEList->a;
1894 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001895 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001896 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001897 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001898 }
drhf5905aa2002-05-26 20:54:33 +00001899 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001900 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001901 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001902 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001903 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1904 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001905 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001906 }else{
danielk19774adee202004-05-08 08:23:19 +00001907 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001908 }
drhbe5c89a2004-07-26 00:31:09 +00001909 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001910 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001911 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001912 }
drhf570f012002-05-31 15:51:25 +00001913 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001914 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001915 }
drh17a7f8d2002-03-24 13:13:27 +00001916 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001917 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001918 }else{
drhf0863fe2005-06-12 21:35:51 +00001919 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001920 }
danielk19774adee202004-05-08 08:23:19 +00001921 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001922 break;
1923 }
danielk19775338a5f2005-01-20 13:03:10 +00001924#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001925 case TK_RAISE: {
1926 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001927 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001928 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001929 return;
1930 }
drhad6d9462004-09-19 02:15:24 +00001931 if( pExpr->iColumn!=OE_Ignore ){
1932 assert( pExpr->iColumn==OE_Rollback ||
1933 pExpr->iColumn == OE_Abort ||
1934 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001935 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001936 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001937 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001938 } else {
drhad6d9462004-09-19 02:15:24 +00001939 assert( pExpr->iColumn == OE_Ignore );
1940 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1941 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1942 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001943 }
drhffe07b22005-11-03 00:41:17 +00001944 stackChng = 0;
1945 break;
drh17a7f8d2002-03-24 13:13:27 +00001946 }
danielk19775338a5f2005-01-20 13:03:10 +00001947#endif
drhffe07b22005-11-03 00:41:17 +00001948 }
1949
1950 if( pParse->ckOffset ){
1951 pParse->ckOffset += stackChng;
1952 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001953 }
drhcce7d172000-05-31 15:34:51 +00001954}
1955
danielk197793758c82005-01-21 08:13:14 +00001956#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001957/*
drh25303782004-12-07 15:41:48 +00001958** Generate code that evalutes the given expression and leaves the result
1959** on the stack. See also sqlite3ExprCode().
1960**
1961** This routine might also cache the result and modify the pExpr tree
1962** so that it will make use of the cached result on subsequent evaluations
1963** rather than evaluate the whole expression again. Trivial expressions are
1964** not cached. If the expression is cached, its result is stored in a
1965** memory location.
1966*/
1967void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1968 Vdbe *v = pParse->pVdbe;
1969 int iMem;
1970 int addr1, addr2;
1971 if( v==0 ) return;
1972 addr1 = sqlite3VdbeCurrentAddr(v);
1973 sqlite3ExprCode(pParse, pExpr);
1974 addr2 = sqlite3VdbeCurrentAddr(v);
1975 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1976 iMem = pExpr->iTable = pParse->nMem++;
1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1978 pExpr->op = TK_REGISTER;
1979 }
1980}
danielk197793758c82005-01-21 08:13:14 +00001981#endif
drh25303782004-12-07 15:41:48 +00001982
1983/*
drh268380c2004-02-25 13:47:31 +00001984** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001985** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001986**
1987** Return the number of elements pushed onto the stack.
1988*/
danielk19774adee202004-05-08 08:23:19 +00001989int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001990 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001991 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001992){
1993 struct ExprList_item *pItem;
1994 int i, n;
drh268380c2004-02-25 13:47:31 +00001995 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001996 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001997 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001998 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001999 }
drhf9b596e2004-05-26 16:54:42 +00002000 return n;
drh268380c2004-02-25 13:47:31 +00002001}
2002
2003/*
drhcce7d172000-05-31 15:34:51 +00002004** Generate code for a boolean expression such that a jump is made
2005** to the label "dest" if the expression is true but execution
2006** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002007**
2008** If the expression evaluates to NULL (neither true nor false), then
2009** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002010**
2011** This code depends on the fact that certain token values (ex: TK_EQ)
2012** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2013** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2014** the make process cause these values to align. Assert()s in the code
2015** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002016*/
danielk19774adee202004-05-08 08:23:19 +00002017void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002018 Vdbe *v = pParse->pVdbe;
2019 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002020 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002021 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002022 op = pExpr->op;
2023 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002024 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002025 int d2 = sqlite3VdbeMakeLabel(v);
2026 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2027 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2028 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002029 break;
2030 }
2031 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002032 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2033 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002034 break;
2035 }
2036 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002037 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002038 break;
2039 }
2040 case TK_LT:
2041 case TK_LE:
2042 case TK_GT:
2043 case TK_GE:
2044 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002045 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002046 assert( TK_LT==OP_Lt );
2047 assert( TK_LE==OP_Le );
2048 assert( TK_GT==OP_Gt );
2049 assert( TK_GE==OP_Ge );
2050 assert( TK_EQ==OP_Eq );
2051 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002052 sqlite3ExprCode(pParse, pExpr->pLeft);
2053 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002054 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002055 break;
2056 }
2057 case TK_ISNULL:
2058 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002059 assert( TK_ISNULL==OP_IsNull );
2060 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002061 sqlite3ExprCode(pParse, pExpr->pLeft);
2062 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002063 break;
2064 }
drhfef52082000-06-06 01:50:43 +00002065 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002066 /* The expression "x BETWEEN y AND z" is implemented as:
2067 **
2068 ** 1 IF (x < y) GOTO 3
2069 ** 2 IF (x <= z) GOTO <dest>
2070 ** 3 ...
2071 */
drhf5905aa2002-05-26 20:54:33 +00002072 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002073 Expr *pLeft = pExpr->pLeft;
2074 Expr *pRight = pExpr->pList->a[0].pExpr;
2075 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002076 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002077 sqlite3ExprCode(pParse, pRight);
2078 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002079
drhbe5c89a2004-07-26 00:31:09 +00002080 pRight = pExpr->pList->a[1].pExpr;
2081 sqlite3ExprCode(pParse, pRight);
2082 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002083
danielk19774adee202004-05-08 08:23:19 +00002084 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002085 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002086 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002087 break;
2088 }
drhcce7d172000-05-31 15:34:51 +00002089 default: {
danielk19774adee202004-05-08 08:23:19 +00002090 sqlite3ExprCode(pParse, pExpr);
2091 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002092 break;
2093 }
2094 }
drhffe07b22005-11-03 00:41:17 +00002095 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002096}
2097
2098/*
drh66b89c82000-11-28 20:47:17 +00002099** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002100** to the label "dest" if the expression is false but execution
2101** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002102**
2103** If the expression evaluates to NULL (neither true nor false) then
2104** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002105*/
danielk19774adee202004-05-08 08:23:19 +00002106void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002107 Vdbe *v = pParse->pVdbe;
2108 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002109 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002110 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002111
2112 /* The value of pExpr->op and op are related as follows:
2113 **
2114 ** pExpr->op op
2115 ** --------- ----------
2116 ** TK_ISNULL OP_NotNull
2117 ** TK_NOTNULL OP_IsNull
2118 ** TK_NE OP_Eq
2119 ** TK_EQ OP_Ne
2120 ** TK_GT OP_Le
2121 ** TK_LE OP_Gt
2122 ** TK_GE OP_Lt
2123 ** TK_LT OP_Ge
2124 **
2125 ** For other values of pExpr->op, op is undefined and unused.
2126 ** The value of TK_ and OP_ constants are arranged such that we
2127 ** can compute the mapping above using the following expression.
2128 ** Assert()s verify that the computation is correct.
2129 */
2130 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2131
2132 /* Verify correct alignment of TK_ and OP_ constants
2133 */
2134 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2135 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2136 assert( pExpr->op!=TK_NE || op==OP_Eq );
2137 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2138 assert( pExpr->op!=TK_LT || op==OP_Ge );
2139 assert( pExpr->op!=TK_LE || op==OP_Gt );
2140 assert( pExpr->op!=TK_GT || op==OP_Le );
2141 assert( pExpr->op!=TK_GE || op==OP_Lt );
2142
drhcce7d172000-05-31 15:34:51 +00002143 switch( pExpr->op ){
2144 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002145 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2146 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002147 break;
2148 }
2149 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002150 int d2 = sqlite3VdbeMakeLabel(v);
2151 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2152 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2153 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002154 break;
2155 }
2156 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002157 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002158 break;
2159 }
2160 case TK_LT:
2161 case TK_LE:
2162 case TK_GT:
2163 case TK_GE:
2164 case TK_NE:
2165 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002166 sqlite3ExprCode(pParse, pExpr->pLeft);
2167 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002168 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002169 break;
2170 }
drhcce7d172000-05-31 15:34:51 +00002171 case TK_ISNULL:
2172 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002173 sqlite3ExprCode(pParse, pExpr->pLeft);
2174 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002175 break;
2176 }
drhfef52082000-06-06 01:50:43 +00002177 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002178 /* The expression is "x BETWEEN y AND z". It is implemented as:
2179 **
2180 ** 1 IF (x >= y) GOTO 3
2181 ** 2 GOTO <dest>
2182 ** 3 IF (x > z) GOTO <dest>
2183 */
drhfef52082000-06-06 01:50:43 +00002184 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002185 Expr *pLeft = pExpr->pLeft;
2186 Expr *pRight = pExpr->pList->a[0].pExpr;
2187 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002188 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002189 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002190 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002191 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2192
danielk19774adee202004-05-08 08:23:19 +00002193 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2194 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002195 pRight = pExpr->pList->a[1].pExpr;
2196 sqlite3ExprCode(pParse, pRight);
2197 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002198 break;
2199 }
drhcce7d172000-05-31 15:34:51 +00002200 default: {
danielk19774adee202004-05-08 08:23:19 +00002201 sqlite3ExprCode(pParse, pExpr);
2202 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002203 break;
2204 }
2205 }
drhffe07b22005-11-03 00:41:17 +00002206 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002207}
drh22827922000-06-06 17:27:05 +00002208
2209/*
2210** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2211** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002212**
2213** Sometimes this routine will return FALSE even if the two expressions
2214** really are equivalent. If we cannot prove that the expressions are
2215** identical, we return FALSE just to be safe. So if this routine
2216** returns false, then you do not really know for certain if the two
2217** expressions are the same. But if you get a TRUE return, then you
2218** can be sure the expressions are the same. In the places where
2219** this routine is used, it does not hurt to get an extra FALSE - that
2220** just might result in some slightly slower code. But returning
2221** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002222*/
danielk19774adee202004-05-08 08:23:19 +00002223int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002224 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002225 if( pA==0||pB==0 ){
2226 return pB==pA;
drh22827922000-06-06 17:27:05 +00002227 }
2228 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002229 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002230 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2231 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002232 if( pA->pList ){
2233 if( pB->pList==0 ) return 0;
2234 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2235 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002236 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002237 return 0;
2238 }
2239 }
2240 }else if( pB->pList ){
2241 return 0;
2242 }
2243 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002244 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002245 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002246 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002247 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002248 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2249 return 0;
2250 }
drh22827922000-06-06 17:27:05 +00002251 }
2252 return 1;
2253}
2254
drh13449892005-09-07 21:22:45 +00002255
drh22827922000-06-06 17:27:05 +00002256/*
drh13449892005-09-07 21:22:45 +00002257** Add a new element to the pAggInfo->aCol[] array. Return the index of
2258** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002259*/
drh13449892005-09-07 21:22:45 +00002260static int addAggInfoColumn(AggInfo *pInfo){
2261 int i;
drhcf643722007-03-27 13:36:37 +00002262 pInfo->aCol = sqlite3ArrayAllocate(
2263 pInfo->aCol,
2264 sizeof(pInfo->aCol[0]),
2265 3,
2266 &pInfo->nColumn,
2267 &pInfo->nColumnAlloc,
2268 &i
2269 );
drh13449892005-09-07 21:22:45 +00002270 return i;
2271}
2272
2273/*
2274** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2275** the new element. Return a negative number if malloc fails.
2276*/
2277static int addAggInfoFunc(AggInfo *pInfo){
2278 int i;
drhcf643722007-03-27 13:36:37 +00002279 pInfo->aFunc = sqlite3ArrayAllocate(
2280 pInfo->aFunc,
2281 sizeof(pInfo->aFunc[0]),
2282 3,
2283 &pInfo->nFunc,
2284 &pInfo->nFuncAlloc,
2285 &i
2286 );
drh13449892005-09-07 21:22:45 +00002287 return i;
2288}
drh22827922000-06-06 17:27:05 +00002289
2290/*
drh626a8792005-01-17 22:08:19 +00002291** This is an xFunc for walkExprTree() used to implement
2292** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2293** for additional information.
drh22827922000-06-06 17:27:05 +00002294**
drh626a8792005-01-17 22:08:19 +00002295** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002296*/
drh626a8792005-01-17 22:08:19 +00002297static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002298 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002299 NameContext *pNC = (NameContext *)pArg;
2300 Parse *pParse = pNC->pParse;
2301 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002302 AggInfo *pAggInfo = pNC->pAggInfo;
2303
drh22827922000-06-06 17:27:05 +00002304
drh22827922000-06-06 17:27:05 +00002305 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002306 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002307 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002308 /* Check to see if the column is in one of the tables in the FROM
2309 ** clause of the aggregate query */
2310 if( pSrcList ){
2311 struct SrcList_item *pItem = pSrcList->a;
2312 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2313 struct AggInfo_col *pCol;
2314 if( pExpr->iTable==pItem->iCursor ){
2315 /* If we reach this point, it means that pExpr refers to a table
2316 ** that is in the FROM clause of the aggregate query.
2317 **
2318 ** Make an entry for the column in pAggInfo->aCol[] if there
2319 ** is not an entry there already.
2320 */
drh7f906d62007-03-12 23:48:52 +00002321 int k;
drh13449892005-09-07 21:22:45 +00002322 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002323 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002324 if( pCol->iTable==pExpr->iTable &&
2325 pCol->iColumn==pExpr->iColumn ){
2326 break;
2327 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002328 }
drh7f906d62007-03-12 23:48:52 +00002329 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2330 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002331 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002332 pCol->iTable = pExpr->iTable;
2333 pCol->iColumn = pExpr->iColumn;
2334 pCol->iMem = pParse->nMem++;
2335 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002336 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002337 if( pAggInfo->pGroupBy ){
2338 int j, n;
2339 ExprList *pGB = pAggInfo->pGroupBy;
2340 struct ExprList_item *pTerm = pGB->a;
2341 n = pGB->nExpr;
2342 for(j=0; j<n; j++, pTerm++){
2343 Expr *pE = pTerm->pExpr;
2344 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2345 pE->iColumn==pExpr->iColumn ){
2346 pCol->iSorterColumn = j;
2347 break;
2348 }
2349 }
2350 }
2351 if( pCol->iSorterColumn<0 ){
2352 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2353 }
2354 }
2355 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2356 ** because it was there before or because we just created it).
2357 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2358 ** pAggInfo->aCol[] entry.
2359 */
2360 pExpr->pAggInfo = pAggInfo;
2361 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002362 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002363 break;
2364 } /* endif pExpr->iTable==pItem->iCursor */
2365 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002366 }
drh626a8792005-01-17 22:08:19 +00002367 return 1;
drh22827922000-06-06 17:27:05 +00002368 }
2369 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002370 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2371 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002372 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002373 /* Check to see if pExpr is a duplicate of another aggregate
2374 ** function that is already in the pAggInfo structure
2375 */
2376 struct AggInfo_func *pItem = pAggInfo->aFunc;
2377 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2378 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002379 break;
2380 }
drh22827922000-06-06 17:27:05 +00002381 }
drh13449892005-09-07 21:22:45 +00002382 if( i>=pAggInfo->nFunc ){
2383 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2384 */
danielk197714db2662006-01-09 16:12:04 +00002385 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002386 i = addAggInfoFunc(pAggInfo);
2387 if( i>=0 ){
2388 pItem = &pAggInfo->aFunc[i];
2389 pItem->pExpr = pExpr;
2390 pItem->iMem = pParse->nMem++;
2391 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002392 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002393 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002394 if( pExpr->flags & EP_Distinct ){
2395 pItem->iDistinct = pParse->nTab++;
2396 }else{
2397 pItem->iDistinct = -1;
2398 }
drh13449892005-09-07 21:22:45 +00002399 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002400 }
drh13449892005-09-07 21:22:45 +00002401 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2402 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002403 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002404 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002405 return 1;
drh22827922000-06-06 17:27:05 +00002406 }
drh22827922000-06-06 17:27:05 +00002407 }
2408 }
drh13449892005-09-07 21:22:45 +00002409
2410 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2411 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2412 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2413 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002414 if( pExpr->pSelect ){
2415 pNC->nDepth++;
2416 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2417 pNC->nDepth--;
2418 }
drh626a8792005-01-17 22:08:19 +00002419 return 0;
2420}
2421
2422/*
2423** Analyze the given expression looking for aggregate functions and
2424** for variables that need to be added to the pParse->aAgg[] array.
2425** Make additional entries to the pParse->aAgg[] array as necessary.
2426**
2427** This routine should only be called after the expression has been
2428** analyzed by sqlite3ExprResolveNames().
2429**
2430** If errors are seen, leave an error message in zErrMsg and return
2431** the number of errors.
2432*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002433int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2434 int nErr = pNC->pParse->nErr;
2435 walkExprTree(pExpr, analyzeAggregate, pNC);
2436 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002437}
drh5d9a4af2005-08-30 00:54:01 +00002438
2439/*
2440** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2441** expression list. Return the number of errors.
2442**
2443** If an error is found, the analysis is cut short.
2444*/
2445int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2446 struct ExprList_item *pItem;
2447 int i;
2448 int nErr = 0;
2449 if( pList ){
2450 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2451 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2452 }
2453 }
2454 return nErr;
2455}