blob: 26057c7b578fddcf13612f4fddfec41febb6e683 [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**
drhdb83f822007-05-11 00:20:08 +000015** $Id: expr.c,v 1.290 2007/05/11 00:20:08 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
drh8b4c40d2007-02-01 23:02:45 +000053** Set the collating sequence for expression pExpr to be the collating
54** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000055** The collating sequence is marked as "explicit" using the EP_ExpCollate
56** flag. An explicit collating sequence will override implicit
57** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000058*/
59Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
60 CollSeq *pColl;
61 if( pExpr==0 ) return 0;
62 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
63 if( pColl ){
64 pExpr->pColl = pColl;
65 pExpr->flags |= EP_ExpCollate;
66 }
67 return pExpr;
68}
69
70/*
danielk19770202b292004-06-09 09:55:16 +000071** Return the default collation sequence for the expression pExpr. If
72** there is no default collation type, return 0.
73*/
danielk19777cedc8d2004-06-10 10:50:08 +000074CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
75 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000076 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000077 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000078 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000079 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000080 }
81 }
danielk19777cedc8d2004-06-10 10:50:08 +000082 if( sqlite3CheckCollSeq(pParse, pColl) ){
83 pColl = 0;
84 }
85 return pColl;
danielk19770202b292004-06-09 09:55:16 +000086}
87
88/*
drh626a8792005-01-17 22:08:19 +000089** pExpr is an operand of a comparison operator. aff2 is the
90** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000091** type affinity that should be used for the comparison operator.
92*/
danielk1977e014a832004-05-17 10:48:57 +000093char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000094 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000095 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000096 /* Both sides of the comparison are columns. If one has numeric
97 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh8a512562005-11-14 22:29:05 +000099 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000100 return SQLITE_AFF_NUMERIC;
101 }else{
102 return SQLITE_AFF_NONE;
103 }
104 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000105 /* Neither side of the comparison is a column. Compare the
106 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000107 */
drh5f6a87b2004-07-19 00:39:45 +0000108 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000109 }else{
110 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000111 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000112 return (aff1 + aff2);
113 }
114}
115
drh53db1452004-05-20 13:54:53 +0000116/*
117** pExpr is a comparison operator. Return the type affinity that should
118** be applied to both operands prior to doing the comparison.
119*/
danielk1977e014a832004-05-17 10:48:57 +0000120static char comparisonAffinity(Expr *pExpr){
121 char aff;
122 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
123 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
124 pExpr->op==TK_NE );
125 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000126 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000127 if( pExpr->pRight ){
128 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
129 }
130 else if( pExpr->pSelect ){
131 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
132 }
133 else if( !aff ){
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 }
danielk1977fc976062007-05-10 10:46:56 +0000250
251 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000252 return pNew;
253}
254
255/*
drh206f3d92006-07-11 13:15:08 +0000256** Works like sqlite3Expr() but frees its pLeft and pRight arguments
257** if it fails due to a malloc problem.
258*/
259Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
260 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
261 if( pNew==0 ){
262 sqlite3ExprDelete(pLeft);
263 sqlite3ExprDelete(pRight);
264 }
265 return pNew;
266}
267
268/*
drh4e0cff62004-11-05 05:10:28 +0000269** When doing a nested parse, you can include terms in an expression
270** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000271** on the stack. "#0" means the top of the stack.
272** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000273**
274** This routine is called by the parser to deal with on of those terms.
275** It immediately generates code to store the value in a memory location.
276** The returns an expression that will code to extract the value from
277** that memory location as needed.
278*/
279Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
280 Vdbe *v = pParse->pVdbe;
281 Expr *p;
282 int depth;
drhdb83f822007-05-11 00:20:08 +0000283 static const Token zeroToken = { (u8*)"0", 0, 1 };
drh4e0cff62004-11-05 05:10:28 +0000284 if( pParse->nested==0 ){
285 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drhdb83f822007-05-11 00:20:08 +0000286 return sqlite3Expr(TK_INTEGER, 0, 0, &zeroToken);
drh4e0cff62004-11-05 05:10:28 +0000287 }
drhbb7ac002005-08-12 22:58:53 +0000288 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000289 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000290 if( p==0 ){
291 return 0; /* Malloc failed */
292 }
drh2646da72005-12-09 20:02:05 +0000293 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000294 p->iTable = pParse->nMem++;
295 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
296 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000297 return p;
298}
299
300/*
drh91bb0ee2004-09-01 03:06:34 +0000301** Join two expressions using an AND operator. If either expression is
302** NULL, then just return the other expression.
303*/
304Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
305 if( pLeft==0 ){
306 return pRight;
307 }else if( pRight==0 ){
308 return pLeft;
309 }else{
310 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
311 }
312}
313
314/*
drh6977fea2002-10-22 23:38:04 +0000315** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000316** text between the two given tokens.
317*/
danielk19774adee202004-05-08 08:23:19 +0000318void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000319 assert( pRight!=0 );
320 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000321 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000322 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000323 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000324 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000325 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000326 }else{
drh6977fea2002-10-22 23:38:04 +0000327 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000328 }
drha76b5df2002-02-23 02:32:10 +0000329 }
330}
331
332/*
333** Construct a new expression node for a function with multiple
334** arguments.
335*/
danielk19774adee202004-05-08 08:23:19 +0000336Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000337 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000338 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000339 pNew = sqliteMalloc( sizeof(Expr) );
340 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000341 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000342 return 0;
343 }
344 pNew->op = TK_FUNCTION;
345 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000346 assert( pToken->dyn==0 );
347 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000348 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000349
350 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000351 return pNew;
352}
353
354/*
drhfa6bc002004-09-07 16:19:52 +0000355** Assign a variable number to an expression that encodes a wildcard
356** in the original SQL statement.
357**
358** Wildcards consisting of a single "?" are assigned the next sequential
359** variable number.
360**
361** Wildcards of the form "?nnn" are assigned the number "nnn". We make
362** sure "nnn" is not too be to avoid a denial of service attack when
363** the SQL statement comes from an external source.
364**
365** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
366** as the previous instance of the same wildcard. Or if this is the first
367** instance of the wildcard, the next sequenial variable number is
368** assigned.
369*/
370void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
371 Token *pToken;
372 if( pExpr==0 ) return;
373 pToken = &pExpr->token;
374 assert( pToken->n>=1 );
375 assert( pToken->z!=0 );
376 assert( pToken->z[0]!=0 );
377 if( pToken->n==1 ){
378 /* Wildcard of the form "?". Assign the next variable number */
379 pExpr->iTable = ++pParse->nVar;
380 }else if( pToken->z[0]=='?' ){
381 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
382 ** use it as the variable number */
383 int i;
drh2646da72005-12-09 20:02:05 +0000384 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000385 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
386 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
387 SQLITE_MAX_VARIABLE_NUMBER);
388 }
389 if( i>pParse->nVar ){
390 pParse->nVar = i;
391 }
392 }else{
393 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
394 ** number as the prior appearance of the same name, or if the name
395 ** has never appeared before, reuse the same variable number
396 */
397 int i, n;
398 n = pToken->n;
399 for(i=0; i<pParse->nVarExpr; i++){
400 Expr *pE;
401 if( (pE = pParse->apVarExpr[i])!=0
402 && pE->token.n==n
403 && memcmp(pE->token.z, pToken->z, n)==0 ){
404 pExpr->iTable = pE->iTable;
405 break;
406 }
407 }
408 if( i>=pParse->nVarExpr ){
409 pExpr->iTable = ++pParse->nVar;
410 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
411 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000412 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000413 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
414 }
danielk19779e128002006-01-18 16:51:35 +0000415 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000416 assert( pParse->apVarExpr!=0 );
417 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
418 }
419 }
420 }
danielk1977832b2662007-05-09 11:37:22 +0000421 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
422 sqlite3ErrorMsg(pParse, "too many SQL variables");
423 }
drhfa6bc002004-09-07 16:19:52 +0000424}
425
426/*
drha2e00042002-01-22 03:13:42 +0000427** Recursively delete an expression tree.
428*/
danielk19774adee202004-05-08 08:23:19 +0000429void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000430 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000431 if( p->span.dyn ) sqliteFree((char*)p->span.z);
432 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000433 sqlite3ExprDelete(p->pLeft);
434 sqlite3ExprDelete(p->pRight);
435 sqlite3ExprListDelete(p->pList);
436 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000437 sqliteFree(p);
438}
439
drhd2687b72005-08-12 22:56:09 +0000440/*
441** The Expr.token field might be a string literal that is quoted.
442** If so, remove the quotation marks.
443*/
444void sqlite3DequoteExpr(Expr *p){
445 if( ExprHasAnyProperty(p, EP_Dequoted) ){
446 return;
447 }
448 ExprSetProperty(p, EP_Dequoted);
449 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000450 sqlite3TokenCopy(&p->token, &p->token);
451 }
452 sqlite3Dequote((char*)p->token.z);
453}
454
drha76b5df2002-02-23 02:32:10 +0000455
456/*
drhff78bd22002-02-27 01:47:11 +0000457** The following group of routines make deep copies of expressions,
458** expression lists, ID lists, and select statements. The copies can
459** be deleted (by being passed to their respective ...Delete() routines)
460** without effecting the originals.
461**
danielk19774adee202004-05-08 08:23:19 +0000462** The expression list, ID, and source lists return by sqlite3ExprListDup(),
463** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000464** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000465**
drhad3cab52002-05-24 02:04:32 +0000466** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000467*/
danielk19774adee202004-05-08 08:23:19 +0000468Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000469 Expr *pNew;
470 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000471 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000472 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000473 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000474 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000475 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000476 pNew->token.dyn = 1;
477 }else{
drh4efc4752004-01-16 15:55:37 +0000478 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000479 }
drh6977fea2002-10-22 23:38:04 +0000480 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000481 pNew->pLeft = sqlite3ExprDup(p->pLeft);
482 pNew->pRight = sqlite3ExprDup(p->pRight);
483 pNew->pList = sqlite3ExprListDup(p->pList);
484 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000485 pNew->pTab = p->pTab;
danielk1977fc976062007-05-10 10:46:56 +0000486#if SQLITE_MAX_EXPR_DEPTH>0
487 pNew->nHeight = p->nHeight;
488#endif
drhff78bd22002-02-27 01:47:11 +0000489 return pNew;
490}
danielk19774adee202004-05-08 08:23:19 +0000491void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000492 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000493 if( pFrom->z ){
494 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000495 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000496 pTo->dyn = 1;
497 }else{
drh4b59ab52002-08-24 18:24:51 +0000498 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000499 }
500}
danielk19774adee202004-05-08 08:23:19 +0000501ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000502 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000503 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000504 int i;
505 if( p==0 ) return 0;
506 pNew = sqliteMalloc( sizeof(*pNew) );
507 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000508 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000509 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000510 if( pItem==0 ){
511 sqliteFree(pNew);
512 return 0;
513 }
drh145716b2004-09-24 12:24:06 +0000514 pOldItem = p->a;
515 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000516 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000517 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000518 if( pOldExpr->span.z!=0 && pNewExpr ){
519 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000520 ** expression list. The logic in SELECT processing that determines
521 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000522 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000523 }
drh1f3e9052002-10-31 00:09:39 +0000524 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000525 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000526 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000527 pItem->zName = sqliteStrDup(pOldItem->zName);
528 pItem->sortOrder = pOldItem->sortOrder;
529 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000530 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000531 }
532 return pNew;
533}
danielk197793758c82005-01-21 08:13:14 +0000534
535/*
536** If cursors, triggers, views and subqueries are all omitted from
537** the build, then none of the following routines, except for
538** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
539** called with a NULL argument.
540*/
danielk19776a67fe82005-02-04 04:07:16 +0000541#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
542 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000543SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000544 SrcList *pNew;
545 int i;
drh113088e2003-03-20 01:16:58 +0000546 int nByte;
drhad3cab52002-05-24 02:04:32 +0000547 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000548 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000549 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000550 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000551 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000552 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000553 struct SrcList_item *pNewItem = &pNew->a[i];
554 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000555 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000556 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
557 pNewItem->zName = sqliteStrDup(pOldItem->zName);
558 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
559 pNewItem->jointype = pOldItem->jointype;
560 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000561 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000562 pTab = pNewItem->pTab = pOldItem->pTab;
563 if( pTab ){
564 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000565 }
danielk19774adee202004-05-08 08:23:19 +0000566 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
567 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
568 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000569 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000570 }
571 return pNew;
572}
danielk19774adee202004-05-08 08:23:19 +0000573IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000574 IdList *pNew;
575 int i;
576 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000577 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000578 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000579 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000580 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000581 if( pNew->a==0 ){
582 sqliteFree(pNew);
583 return 0;
584 }
drhff78bd22002-02-27 01:47:11 +0000585 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000586 struct IdList_item *pNewItem = &pNew->a[i];
587 struct IdList_item *pOldItem = &p->a[i];
588 pNewItem->zName = sqliteStrDup(pOldItem->zName);
589 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000590 }
591 return pNew;
592}
danielk19774adee202004-05-08 08:23:19 +0000593Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000594 Select *pNew;
595 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000596 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000597 if( pNew==0 ) return 0;
598 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000599 pNew->pEList = sqlite3ExprListDup(p->pEList);
600 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
601 pNew->pWhere = sqlite3ExprDup(p->pWhere);
602 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
603 pNew->pHaving = sqlite3ExprDup(p->pHaving);
604 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000605 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000606 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000607 pNew->pLimit = sqlite3ExprDup(p->pLimit);
608 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000609 pNew->iLimit = -1;
610 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000611 pNew->isResolved = p->isResolved;
612 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000613 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000614 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000615 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000616 pNew->addrOpenEphm[0] = -1;
617 pNew->addrOpenEphm[1] = -1;
618 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000619 return pNew;
620}
danielk197793758c82005-01-21 08:13:14 +0000621#else
622Select *sqlite3SelectDup(Select *p){
623 assert( p==0 );
624 return 0;
625}
626#endif
drhff78bd22002-02-27 01:47:11 +0000627
628
629/*
drha76b5df2002-02-23 02:32:10 +0000630** Add a new element to the end of an expression list. If pList is
631** initially NULL, then create a new expression list.
632*/
danielk19774adee202004-05-08 08:23:19 +0000633ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000634 if( pList==0 ){
635 pList = sqliteMalloc( sizeof(ExprList) );
636 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000637 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000638 }
drh4efc4752004-01-16 15:55:37 +0000639 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000640 }
drh4305d102003-07-30 12:34:12 +0000641 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000642 struct ExprList_item *a;
643 int n = pList->nAlloc*2 + 4;
644 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
645 if( a==0 ){
646 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000647 }
danielk1977d5d56522005-03-16 12:15:20 +0000648 pList->a = a;
649 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000650 }
drh4efc4752004-01-16 15:55:37 +0000651 assert( pList->a!=0 );
652 if( pExpr || pName ){
653 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
654 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000655 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000656 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000657 }
658 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000659
660no_mem:
661 /* Avoid leaking memory if malloc has failed. */
662 sqlite3ExprDelete(pExpr);
663 sqlite3ExprListDelete(pList);
664 return 0;
drha76b5df2002-02-23 02:32:10 +0000665}
666
667/*
danielk19777a15a4b2007-05-08 17:54:43 +0000668** If the expression list pEList contains more than iLimit elements,
669** leave an error message in pParse.
670*/
671void sqlite3ExprListCheckLength(
672 Parse *pParse,
673 ExprList *pEList,
674 int iLimit,
675 const char *zObject
676){
danielk1977b4fc6792007-05-08 18:04:46 +0000677 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000678 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
679 }
680}
681
danielk1977fc976062007-05-10 10:46:56 +0000682
683#if SQLITE_MAX_EXPR_DEPTH>0
684/* The following three functions, heightOfExpr(), heightOfExprList()
685** and heightOfSelect(), are used to determine the maximum height
686** of any expression tree referenced by the structure passed as the
687** first argument.
688**
689** If this maximum height is greater than the current value pointed
690** to by pnHeight, the second parameter, then set *pnHeight to that
691** value.
692*/
693static void heightOfExpr(Expr *p, int *pnHeight){
694 if( p ){
695 if( p->nHeight>*pnHeight ){
696 *pnHeight = p->nHeight;
697 }
698 }
699}
700static void heightOfExprList(ExprList *p, int *pnHeight){
701 if( p ){
702 int i;
703 for(i=0; i<p->nExpr; i++){
704 heightOfExpr(p->a[i].pExpr, pnHeight);
705 }
706 }
707}
708static void heightOfSelect(Select *p, int *pnHeight){
709 if( p ){
710 heightOfExpr(p->pWhere, pnHeight);
711 heightOfExpr(p->pHaving, pnHeight);
712 heightOfExpr(p->pLimit, pnHeight);
713 heightOfExpr(p->pOffset, pnHeight);
714 heightOfExprList(p->pEList, pnHeight);
715 heightOfExprList(p->pGroupBy, pnHeight);
716 heightOfExprList(p->pOrderBy, pnHeight);
717 heightOfSelect(p->pPrior, pnHeight);
718 }
719}
720
721/*
722** Set the Expr.nHeight variable in the structure passed as an
723** argument. An expression with no children, Expr.pList or
724** Expr.pSelect member has a height of 1. Any other expression
725** has a height equal to the maximum height of any other
726** referenced Expr plus one.
727*/
728void sqlite3ExprSetHeight(Expr *p){
729 int nHeight = 0;
730 heightOfExpr(p->pLeft, &nHeight);
731 heightOfExpr(p->pRight, &nHeight);
732 heightOfExprList(p->pList, &nHeight);
733 heightOfSelect(p->pSelect, &nHeight);
734 p->nHeight = nHeight + 1;
735}
736
737/*
738** Return the maximum height of any expression tree referenced
739** by the select statement passed as an argument.
740*/
741int sqlite3SelectExprHeight(Select *p){
742 int nHeight = 0;
743 heightOfSelect(p, &nHeight);
744 return nHeight;
745}
746#endif
747
danielk19777a15a4b2007-05-08 17:54:43 +0000748/*
drha76b5df2002-02-23 02:32:10 +0000749** Delete an entire expression list.
750*/
danielk19774adee202004-05-08 08:23:19 +0000751void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000752 int i;
drhbe5c89a2004-07-26 00:31:09 +0000753 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000754 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000755 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
756 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000757 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
758 sqlite3ExprDelete(pItem->pExpr);
759 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000760 }
761 sqliteFree(pList->a);
762 sqliteFree(pList);
763}
764
765/*
drh626a8792005-01-17 22:08:19 +0000766** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000767**
drh626a8792005-01-17 22:08:19 +0000768** The return value from xFunc determines whether the tree walk continues.
769** 0 means continue walking the tree. 1 means do not walk children
770** of the current node but continue with siblings. 2 means abandon
771** the tree walk completely.
772**
773** The return value from this routine is 1 to abandon the tree walk
774** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000775**
776** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000777*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000778static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000779static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000780 int rc;
781 if( pExpr==0 ) return 0;
782 rc = (*xFunc)(pArg, pExpr);
783 if( rc==0 ){
784 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
785 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000786 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000787 }
788 return rc>1;
789}
790
791/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000792** Call walkExprTree() for every expression in list p.
793*/
794static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
795 int i;
796 struct ExprList_item *pItem;
797 if( !p ) return 0;
798 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
799 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
800 }
801 return 0;
802}
803
804/*
805** Call walkExprTree() for every expression in Select p, not including
806** expressions that are part of sub-selects in any FROM clause or the LIMIT
807** or OFFSET expressions..
808*/
809static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
810 walkExprList(p->pEList, xFunc, pArg);
811 walkExprTree(p->pWhere, xFunc, pArg);
812 walkExprList(p->pGroupBy, xFunc, pArg);
813 walkExprTree(p->pHaving, xFunc, pArg);
814 walkExprList(p->pOrderBy, xFunc, pArg);
815 return 0;
816}
817
818
819/*
drh626a8792005-01-17 22:08:19 +0000820** This routine is designed as an xFunc for walkExprTree().
821**
822** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000823** at pExpr that the expression that contains pExpr is not a constant
824** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
825** If pExpr does does not disqualify the expression from being a constant
826** then do nothing.
827**
828** After walking the whole tree, if no nodes are found that disqualify
829** the expression as constant, then we assume the whole expression
830** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000831*/
832static int exprNodeIsConstant(void *pArg, Expr *pExpr){
833 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000834 /* Consider functions to be constant if all their arguments are constant
835 ** and *pArg==2 */
836 case TK_FUNCTION:
837 if( *((int*)pArg)==2 ) return 0;
838 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000839 case TK_ID:
840 case TK_COLUMN:
841 case TK_DOT:
842 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000843 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000844#ifndef SQLITE_OMIT_SUBQUERY
845 case TK_SELECT:
846 case TK_EXISTS:
847#endif
drh626a8792005-01-17 22:08:19 +0000848 *((int*)pArg) = 0;
849 return 2;
drh87abf5c2005-08-25 12:45:04 +0000850 case TK_IN:
851 if( pExpr->pSelect ){
852 *((int*)pArg) = 0;
853 return 2;
854 }
drh626a8792005-01-17 22:08:19 +0000855 default:
856 return 0;
857 }
858}
859
860/*
drhfef52082000-06-06 01:50:43 +0000861** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000862** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000863**
864** For the purposes of this function, a double-quoted string (ex: "abc")
865** is considered a variable but a single-quoted string (ex: 'abc') is
866** a constant.
drhfef52082000-06-06 01:50:43 +0000867*/
danielk19774adee202004-05-08 08:23:19 +0000868int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000869 int isConst = 1;
870 walkExprTree(p, exprNodeIsConstant, &isConst);
871 return isConst;
drhfef52082000-06-06 01:50:43 +0000872}
873
874/*
drheb55bd22005-06-30 17:04:21 +0000875** Walk an expression tree. Return 1 if the expression is constant
876** or a function call with constant arguments. Return and 0 if there
877** are any variables.
878**
879** For the purposes of this function, a double-quoted string (ex: "abc")
880** is considered a variable but a single-quoted string (ex: 'abc') is
881** a constant.
882*/
883int sqlite3ExprIsConstantOrFunction(Expr *p){
884 int isConst = 2;
885 walkExprTree(p, exprNodeIsConstant, &isConst);
886 return isConst!=0;
887}
888
889/*
drh73b211a2005-01-18 04:00:42 +0000890** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000891** to fit in a 32-bit integer, return 1 and put the value of the integer
892** in *pValue. If the expression is not an integer or if it is too big
893** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000894*/
danielk19774adee202004-05-08 08:23:19 +0000895int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000896 switch( p->op ){
897 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000898 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000899 return 1;
900 }
901 break;
drhe4de1fe2002-06-02 16:09:01 +0000902 }
drh4b59ab52002-08-24 18:24:51 +0000903 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000904 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000905 }
drhe4de1fe2002-06-02 16:09:01 +0000906 case TK_UMINUS: {
907 int v;
danielk19774adee202004-05-08 08:23:19 +0000908 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000909 *pValue = -v;
910 return 1;
911 }
912 break;
913 }
914 default: break;
915 }
916 return 0;
917}
918
919/*
drhc4a3c772001-04-04 11:48:57 +0000920** Return TRUE if the given string is a row-id column name.
921*/
danielk19774adee202004-05-08 08:23:19 +0000922int sqlite3IsRowid(const char *z){
923 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
924 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
925 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000926 return 0;
927}
928
929/*
drh8141f612004-01-25 22:44:58 +0000930** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
931** that name in the set of source tables in pSrcList and make the pExpr
932** expression node refer back to that source column. The following changes
933** are made to pExpr:
934**
935** pExpr->iDb Set the index in db->aDb[] of the database holding
936** the table.
937** pExpr->iTable Set to the cursor number for the table obtained
938** from pSrcList.
939** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000940** pExpr->op Set to TK_COLUMN.
941** pExpr->pLeft Any expression this points to is deleted
942** pExpr->pRight Any expression this points to is deleted.
943**
944** The pDbToken is the name of the database (the "X"). This value may be
945** NULL meaning that name is of the form Y.Z or Z. Any available database
946** can be used. The pTableToken is the name of the table (the "Y"). This
947** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
948** means that the form of the name is Z and that columns from any table
949** can be used.
950**
951** If the name cannot be resolved unambiguously, leave an error message
952** in pParse and return non-zero. Return zero on success.
953*/
954static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000955 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000956 Token *pDbToken, /* Name of the database containing table, or NULL */
957 Token *pTableToken, /* Name of table containing column, or NULL */
958 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000959 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000960 Expr *pExpr /* Make this EXPR node point to the selected column */
961){
962 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
963 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
964 char *zCol = 0; /* Name of the column. The "Z" */
965 int i, j; /* Loop counters */
966 int cnt = 0; /* Number of matching column names */
967 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000968 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000969 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
970 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000971 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000972
973 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000974 zDb = sqlite3NameFromToken(pDbToken);
975 zTab = sqlite3NameFromToken(pTableToken);
976 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000977 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000978 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000979 }
drh8141f612004-01-25 22:44:58 +0000980
981 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000982 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000983 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000984 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000985
danielk1977b3bce662005-01-29 08:32:43 +0000986 if( pSrcList ){
987 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000988 Table *pTab;
989 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000990 Column *pCol;
991
drh43617e92006-03-06 20:55:46 +0000992 pTab = pItem->pTab;
993 assert( pTab!=0 );
994 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000995 assert( pTab->nCol>0 );
996 if( zTab ){
997 if( pItem->zAlias ){
998 char *zTabName = pItem->zAlias;
999 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1000 }else{
1001 char *zTabName = pTab->zName;
1002 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001003 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001004 continue;
1005 }
drh626a8792005-01-17 22:08:19 +00001006 }
drh8141f612004-01-25 22:44:58 +00001007 }
danielk1977b3bce662005-01-29 08:32:43 +00001008 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001009 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001010 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001011 pMatch = pItem;
1012 }
1013 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1014 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001015 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001016 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001017 cnt++;
1018 pExpr->iTable = pItem->iCursor;
1019 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001020 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001021 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1022 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1023 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001024 if( (pExpr->flags & EP_ExpCollate)==0 ){
1025 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1026 }
drh61dfc312006-12-16 16:25:15 +00001027 if( i<pSrcList->nSrc-1 ){
1028 if( pItem[1].jointype & JT_NATURAL ){
1029 /* If this match occurred in the left table of a natural join,
1030 ** then skip the right table to avoid a duplicate match */
1031 pItem++;
1032 i++;
1033 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1034 /* If this match occurs on a column that is in the USING clause
1035 ** of a join, skip the search of the right table of the join
1036 ** to avoid a duplicate match there. */
1037 int k;
1038 for(k=0; k<pUsing->nId; k++){
1039 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1040 pItem++;
1041 i++;
1042 break;
1043 }
drh873fac02005-06-06 17:11:46 +00001044 }
1045 }
1046 }
danielk1977b3bce662005-01-29 08:32:43 +00001047 break;
1048 }
drh8141f612004-01-25 22:44:58 +00001049 }
1050 }
1051 }
drh626a8792005-01-17 22:08:19 +00001052
1053#ifndef SQLITE_OMIT_TRIGGER
1054 /* If we have not already resolved the name, then maybe
1055 ** it is a new.* or old.* trigger argument reference
1056 */
1057 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1058 TriggerStack *pTriggerStack = pParse->trigStack;
1059 Table *pTab = 0;
1060 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1061 pExpr->iTable = pTriggerStack->newIdx;
1062 assert( pTriggerStack->pTab );
1063 pTab = pTriggerStack->pTab;
1064 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1065 pExpr->iTable = pTriggerStack->oldIdx;
1066 assert( pTriggerStack->pTab );
1067 pTab = pTriggerStack->pTab;
1068 }
1069
1070 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001071 int iCol;
drh626a8792005-01-17 22:08:19 +00001072 Column *pCol = pTab->aCol;
1073
danielk1977da184232006-01-05 11:34:32 +00001074 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001075 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001076 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001077 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001078 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001079 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001080 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1081 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001082 if( (pExpr->flags & EP_ExpCollate)==0 ){
1083 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1084 }
danielk1977aee18ef2005-03-09 12:26:50 +00001085 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001086 break;
1087 }
1088 }
1089 }
1090 }
drhb7f91642004-10-31 02:22:47 +00001091#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001092
drh626a8792005-01-17 22:08:19 +00001093 /*
1094 ** Perhaps the name is a reference to the ROWID
1095 */
1096 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1097 cnt = 1;
1098 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001099 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001100 }
drh8141f612004-01-25 22:44:58 +00001101
drh626a8792005-01-17 22:08:19 +00001102 /*
1103 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1104 ** might refer to an result-set alias. This happens, for example, when
1105 ** we are resolving names in the WHERE clause of the following command:
1106 **
1107 ** SELECT a+b AS x FROM table WHERE x<10;
1108 **
1109 ** In cases like this, replace pExpr with a copy of the expression that
1110 ** forms the result set entry ("a+b" in the example) and return immediately.
1111 ** Note that the expression in the result set should have already been
1112 ** resolved by the time the WHERE clause is resolved.
1113 */
drhffe07b22005-11-03 00:41:17 +00001114 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001115 for(j=0; j<pEList->nExpr; j++){
1116 char *zAs = pEList->a[j].zName;
1117 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1118 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1119 pExpr->op = TK_AS;
1120 pExpr->iColumn = j;
1121 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001122 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001123 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001124 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001125 }
1126 }
1127 }
1128
1129 /* Advance to the next name context. The loop will exit when either
1130 ** we have a match (cnt>0) or when we run out of name contexts.
1131 */
1132 if( cnt==0 ){
1133 pNC = pNC->pNext;
1134 }
drh8141f612004-01-25 22:44:58 +00001135 }
1136
1137 /*
1138 ** If X and Y are NULL (in other words if only the column name Z is
1139 ** supplied) and the value of Z is enclosed in double-quotes, then
1140 ** Z is a string literal if it doesn't match any column names. In that
1141 ** case, we need to return right away and not make any changes to
1142 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001143 **
1144 ** Because no reference was made to outer contexts, the pNC->nRef
1145 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001146 */
1147 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1148 sqliteFree(zCol);
1149 return 0;
1150 }
1151
1152 /*
1153 ** cnt==0 means there was not match. cnt>1 means there were two or
1154 ** more matches. Either way, we have an error.
1155 */
1156 if( cnt!=1 ){
1157 char *z = 0;
1158 char *zErr;
1159 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1160 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001161 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001162 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001163 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001164 }else{
1165 z = sqliteStrDup(zCol);
1166 }
danielk19774adee202004-05-08 08:23:19 +00001167 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001168 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001169 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001170 }
1171
drh51669862004-12-18 18:40:26 +00001172 /* If a column from a table in pSrcList is referenced, then record
1173 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1174 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1175 ** column number is greater than the number of bits in the bitmask
1176 ** then set the high-order bit of the bitmask.
1177 */
1178 if( pExpr->iColumn>=0 && pMatch!=0 ){
1179 int n = pExpr->iColumn;
1180 if( n>=sizeof(Bitmask)*8 ){
1181 n = sizeof(Bitmask)*8-1;
1182 }
1183 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001184 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001185 }
1186
danielk1977d5d56522005-03-16 12:15:20 +00001187lookupname_end:
drh8141f612004-01-25 22:44:58 +00001188 /* Clean up and return
1189 */
1190 sqliteFree(zDb);
1191 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001193 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001194 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001195 pExpr->pRight = 0;
1196 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001197lookupname_end_2:
1198 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001199 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001200 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001201 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001202 if( pMatch && !pMatch->pSelect ){
1203 pExpr->pTab = pMatch->pTab;
1204 }
drh15ccce12005-05-23 15:06:39 +00001205 /* Increment the nRef value on all name contexts from TopNC up to
1206 ** the point where the name matched. */
1207 for(;;){
1208 assert( pTopNC!=0 );
1209 pTopNC->nRef++;
1210 if( pTopNC==pNC ) break;
1211 pTopNC = pTopNC->pNext;
1212 }
1213 return 0;
1214 } else {
1215 return 1;
drh626a8792005-01-17 22:08:19 +00001216 }
drh8141f612004-01-25 22:44:58 +00001217}
1218
1219/*
drh626a8792005-01-17 22:08:19 +00001220** This routine is designed as an xFunc for walkExprTree().
1221**
drh73b211a2005-01-18 04:00:42 +00001222** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001223** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001224** the tree or 2 to abort the tree walk.
1225**
1226** This routine also does error checking and name resolution for
1227** function names. The operator for aggregate functions is changed
1228** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001229*/
1230static int nameResolverStep(void *pArg, Expr *pExpr){
1231 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001232 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001233
danielk1977b3bce662005-01-29 08:32:43 +00001234 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001235 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001236 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001237
drh626a8792005-01-17 22:08:19 +00001238 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1239 ExprSetProperty(pExpr, EP_Resolved);
1240#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001241 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1242 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001243 int i;
danielk1977f0113002006-01-24 12:09:17 +00001244 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001245 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1246 }
1247 }
1248#endif
1249 switch( pExpr->op ){
1250 /* Double-quoted strings (ex: "abc") are used as identifiers if
1251 ** possible. Otherwise they remain as strings. Single-quoted
1252 ** strings (ex: 'abc') are always string literals.
1253 */
1254 case TK_STRING: {
1255 if( pExpr->token.z[0]=='\'' ) break;
1256 /* Fall thru into the TK_ID case if this is a double-quoted string */
1257 }
1258 /* A lone identifier is the name of a column.
1259 */
1260 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001261 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1262 return 1;
1263 }
1264
1265 /* A table name and column name: ID.ID
1266 ** Or a database, table and column: ID.ID.ID
1267 */
1268 case TK_DOT: {
1269 Token *pColumn;
1270 Token *pTable;
1271 Token *pDb;
1272 Expr *pRight;
1273
danielk1977b3bce662005-01-29 08:32:43 +00001274 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001275 pRight = pExpr->pRight;
1276 if( pRight->op==TK_ID ){
1277 pDb = 0;
1278 pTable = &pExpr->pLeft->token;
1279 pColumn = &pRight->token;
1280 }else{
1281 assert( pRight->op==TK_DOT );
1282 pDb = &pExpr->pLeft->token;
1283 pTable = &pRight->pLeft->token;
1284 pColumn = &pRight->pRight->token;
1285 }
1286 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1287 return 1;
1288 }
1289
1290 /* Resolve function names
1291 */
drhb71090f2005-05-23 17:26:51 +00001292 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001293 case TK_FUNCTION: {
1294 ExprList *pList = pExpr->pList; /* The argument list */
1295 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1296 int no_such_func = 0; /* True if no such function exists */
1297 int wrong_num_args = 0; /* True if wrong number of arguments */
1298 int is_agg = 0; /* True if is an aggregate function */
1299 int i;
drh5169bbc2006-08-24 14:59:45 +00001300 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001301 int nId; /* Number of characters in function name */
1302 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001303 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001304 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001305
drh2646da72005-12-09 20:02:05 +00001306 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001307 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001308 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1309 if( pDef==0 ){
1310 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1311 if( pDef==0 ){
1312 no_such_func = 1;
1313 }else{
1314 wrong_num_args = 1;
1315 }
1316 }else{
1317 is_agg = pDef->xFunc==0;
1318 }
drh2fca7fe2006-11-23 11:59:13 +00001319#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001320 if( pDef ){
1321 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1322 if( auth!=SQLITE_OK ){
1323 if( auth==SQLITE_DENY ){
1324 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1325 pDef->zName);
1326 pNC->nErr++;
1327 }
1328 pExpr->op = TK_NULL;
1329 return 1;
1330 }
1331 }
drhb8b14212006-08-24 15:18:25 +00001332#endif
drh626a8792005-01-17 22:08:19 +00001333 if( is_agg && !pNC->allowAgg ){
1334 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1335 pNC->nErr++;
1336 is_agg = 0;
1337 }else if( no_such_func ){
1338 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1339 pNC->nErr++;
1340 }else if( wrong_num_args ){
1341 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1342 nId, zId);
1343 pNC->nErr++;
1344 }
1345 if( is_agg ){
1346 pExpr->op = TK_AGG_FUNCTION;
1347 pNC->hasAgg = 1;
1348 }
drh73b211a2005-01-18 04:00:42 +00001349 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001350 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001351 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001352 }
drh73b211a2005-01-18 04:00:42 +00001353 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001354 /* FIX ME: Compute pExpr->affinity based on the expected return
1355 ** type of the function
1356 */
1357 return is_agg;
1358 }
danielk1977b3bce662005-01-29 08:32:43 +00001359#ifndef SQLITE_OMIT_SUBQUERY
1360 case TK_SELECT:
1361 case TK_EXISTS:
1362#endif
1363 case TK_IN: {
1364 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001365 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001366#ifndef SQLITE_OMIT_CHECK
1367 if( pNC->isCheck ){
1368 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1369 }
1370#endif
danielk1977b3bce662005-01-29 08:32:43 +00001371 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1372 assert( pNC->nRef>=nRef );
1373 if( nRef!=pNC->nRef ){
1374 ExprSetProperty(pExpr, EP_VarSelect);
1375 }
1376 }
drh4284fb02005-11-03 12:33:28 +00001377 break;
danielk1977b3bce662005-01-29 08:32:43 +00001378 }
drh4284fb02005-11-03 12:33:28 +00001379#ifndef SQLITE_OMIT_CHECK
1380 case TK_VARIABLE: {
1381 if( pNC->isCheck ){
1382 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1383 }
1384 break;
1385 }
1386#endif
drh626a8792005-01-17 22:08:19 +00001387 }
1388 return 0;
1389}
1390
1391/*
drhcce7d172000-05-31 15:34:51 +00001392** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001393** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001394** index to the table in the table list and a column offset. The
1395** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1396** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001397** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001398** VDBE cursor number for a cursor that is pointing into the referenced
1399** table. The Expr.iColumn value is changed to the index of the column
1400** of the referenced table. The Expr.iColumn value for the special
1401** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1402** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001403**
drh626a8792005-01-17 22:08:19 +00001404** Also resolve function names and check the functions for proper
1405** usage. Make sure all function names are recognized and all functions
1406** have the correct number of arguments. Leave an error message
1407** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1408**
drh73b211a2005-01-18 04:00:42 +00001409** If the expression contains aggregate functions then set the EP_Agg
1410** property on the expression.
drh626a8792005-01-17 22:08:19 +00001411*/
danielk1977fc976062007-05-10 10:46:56 +00001412int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001413 NameContext *pNC, /* Namespace to resolve expressions in. */
1414 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001415){
drh13449892005-09-07 21:22:45 +00001416 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001417 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001418#if SQLITE_MAX_EXPR_DEPTH>0
1419 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1420 sqlite3ErrorMsg(pNC->pParse,
1421 "Expression tree is too large (maximum depth %d)",
1422 SQLITE_MAX_EXPR_DEPTH
1423 );
1424 return 1;
1425 }
1426 pNC->pParse->nHeight += pExpr->nHeight;
1427#endif
drh13449892005-09-07 21:22:45 +00001428 savedHasAgg = pNC->hasAgg;
1429 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001430 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001431#if SQLITE_MAX_EXPR_DEPTH>0
1432 pNC->pParse->nHeight -= pExpr->nHeight;
1433#endif
danielk1977b3bce662005-01-29 08:32:43 +00001434 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001435 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001436 }
drh13449892005-09-07 21:22:45 +00001437 if( pNC->hasAgg ){
1438 ExprSetProperty(pExpr, EP_Agg);
1439 }else if( savedHasAgg ){
1440 pNC->hasAgg = 1;
1441 }
drh73b211a2005-01-18 04:00:42 +00001442 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001443}
1444
drh1398ad32005-01-19 23:24:50 +00001445/*
1446** A pointer instance of this structure is used to pass information
1447** through walkExprTree into codeSubqueryStep().
1448*/
1449typedef struct QueryCoder QueryCoder;
1450struct QueryCoder {
1451 Parse *pParse; /* The parsing context */
1452 NameContext *pNC; /* Namespace of first enclosing query */
1453};
1454
drh626a8792005-01-17 22:08:19 +00001455
1456/*
drh9cbe6352005-11-29 03:13:21 +00001457** Generate code for scalar subqueries used as an expression
1458** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001459**
drh9cbe6352005-11-29 03:13:21 +00001460** (SELECT a FROM b) -- subquery
1461** EXISTS (SELECT a FROM b) -- EXISTS subquery
1462** x IN (4,5,11) -- IN operator with list on right-hand side
1463** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001464**
drh9cbe6352005-11-29 03:13:21 +00001465** The pExpr parameter describes the expression that contains the IN
1466** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001467*/
drh51522cd2005-01-20 13:36:19 +00001468#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001469void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001470 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001471 Vdbe *v = sqlite3GetVdbe(pParse);
1472 if( v==0 ) return;
1473
danielk1977fc976062007-05-10 10:46:56 +00001474
drh57dbd7b2005-07-08 18:25:26 +00001475 /* This code must be run in its entirety every time it is encountered
1476 ** if any of the following is true:
1477 **
1478 ** * The right-hand side is a correlated subquery
1479 ** * The right-hand side is an expression list containing variables
1480 ** * We are inside a trigger
1481 **
1482 ** If all of the above are false, then we can run this code just once
1483 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001484 */
1485 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1486 int mem = pParse->nMem++;
1487 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001488 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001489 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001490 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001491 }
1492
drhcce7d172000-05-31 15:34:51 +00001493 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001494 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001495 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001496 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001497 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001498
danielk1977bf3b7212004-05-18 10:06:24 +00001499 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001500
1501 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001502 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001503 ** filled with single-field index keys representing the results
1504 ** from the SELECT or the <exprlist>.
1505 **
1506 ** If the 'x' expression is a column value, or the SELECT...
1507 ** statement returns a column value, then the affinity of that
1508 ** column is used to build the index keys. If both 'x' and the
1509 ** SELECT... statement are columns, then numeric affinity is used
1510 ** if either column has NUMERIC or INTEGER affinity. If neither
1511 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1512 ** is used.
1513 */
1514 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001515 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001516 memset(&keyInfo, 0, sizeof(keyInfo));
1517 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001518 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001519
drhfef52082000-06-06 01:50:43 +00001520 if( pExpr->pSelect ){
1521 /* Case 1: expr IN (SELECT ...)
1522 **
danielk1977e014a832004-05-17 10:48:57 +00001523 ** Generate code to write the results of the select into the temporary
1524 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001525 */
danielk1977e014a832004-05-17 10:48:57 +00001526 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001527 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001528 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001529 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1530 return;
1531 }
drhbe5c89a2004-07-26 00:31:09 +00001532 pEList = pExpr->pSelect->pEList;
1533 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001534 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001535 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001536 }
drhfef52082000-06-06 01:50:43 +00001537 }else if( pExpr->pList ){
1538 /* Case 2: expr IN (exprlist)
1539 **
danielk1977e014a832004-05-17 10:48:57 +00001540 ** For each expression, build an index key from the evaluation and
1541 ** store it in the temporary table. If <expr> is a column, then use
1542 ** that columns affinity when building index keys. If <expr> is not
1543 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001544 */
danielk1977e014a832004-05-17 10:48:57 +00001545 int i;
drh57dbd7b2005-07-08 18:25:26 +00001546 ExprList *pList = pExpr->pList;
1547 struct ExprList_item *pItem;
1548
danielk1977e014a832004-05-17 10:48:57 +00001549 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001550 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001551 }
danielk19770202b292004-06-09 09:55:16 +00001552 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001553
1554 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001555 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1556 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001557
drh57dbd7b2005-07-08 18:25:26 +00001558 /* If the expression is not constant then we will need to
1559 ** disable the test that was generated above that makes sure
1560 ** this code only executes once. Because for a non-constant
1561 ** expression we need to rerun this code each time.
1562 */
drh6c30be82005-07-29 15:10:17 +00001563 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001564 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001565 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001566 }
danielk1977e014a832004-05-17 10:48:57 +00001567
1568 /* Evaluate the expression and insert it into the temp table */
1569 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001570 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001571 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001572 }
1573 }
danielk19770202b292004-06-09 09:55:16 +00001574 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001575 break;
drhfef52082000-06-06 01:50:43 +00001576 }
1577
drh51522cd2005-01-20 13:36:19 +00001578 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001579 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001580 /* This has to be a scalar SELECT. Generate code to put the
1581 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001582 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001583 */
drh2646da72005-12-09 20:02:05 +00001584 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001585 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001586 int iMem;
1587 int sop;
drh1398ad32005-01-19 23:24:50 +00001588
drhec7429a2005-10-06 16:53:14 +00001589 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001590 pSel = pExpr->pSelect;
1591 if( pExpr->op==TK_SELECT ){
1592 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001593 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1594 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001595 }else{
drh51522cd2005-01-20 13:36:19 +00001596 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001597 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1598 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001599 }
drhec7429a2005-10-06 16:53:14 +00001600 sqlite3ExprDelete(pSel->pLimit);
1601 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001602 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1603 return;
1604 }
danielk1977b3bce662005-01-29 08:32:43 +00001605 break;
drhcce7d172000-05-31 15:34:51 +00001606 }
1607 }
danielk1977b3bce662005-01-29 08:32:43 +00001608
drh57dbd7b2005-07-08 18:25:26 +00001609 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001610 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001611 }
danielk1977fc976062007-05-10 10:46:56 +00001612
danielk1977b3bce662005-01-29 08:32:43 +00001613 return;
drhcce7d172000-05-31 15:34:51 +00001614}
drh51522cd2005-01-20 13:36:19 +00001615#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001616
drhcce7d172000-05-31 15:34:51 +00001617/*
drhfec19aa2004-05-19 20:41:03 +00001618** Generate an instruction that will put the integer describe by
1619** text z[0..n-1] on the stack.
1620*/
1621static void codeInteger(Vdbe *v, const char *z, int n){
1622 int i;
drh6fec0762004-05-30 01:38:43 +00001623 if( sqlite3GetInt32(z, &i) ){
1624 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1625 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001626 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001627 }else{
1628 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1629 }
1630}
1631
drh945498f2007-02-24 11:52:52 +00001632
1633/*
1634** Generate code that will extract the iColumn-th column from
1635** table pTab and push that column value on the stack. There
1636** is an open cursor to pTab in iTable. If iColumn<0 then
1637** code is generated that extracts the rowid.
1638*/
1639void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1640 if( iColumn<0 ){
1641 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1642 sqlite3VdbeAddOp(v, op, iTable, 0);
1643 }else if( pTab==0 ){
1644 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1645 }else{
1646 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1647 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1648 sqlite3ColumnDefault(v, pTab, iColumn);
1649#ifndef SQLITE_OMIT_FLOATING_POINT
1650 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1651 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1652 }
1653#endif
1654 }
1655}
1656
drhfec19aa2004-05-19 20:41:03 +00001657/*
drhcce7d172000-05-31 15:34:51 +00001658** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001659** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001660**
1661** This code depends on the fact that certain token values (ex: TK_EQ)
1662** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1663** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1664** the make process cause these values to align. Assert()s in the code
1665** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001666*/
danielk19774adee202004-05-08 08:23:19 +00001667void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001668 Vdbe *v = pParse->pVdbe;
1669 int op;
drhffe07b22005-11-03 00:41:17 +00001670 int stackChng = 1; /* Amount of change to stack depth */
1671
danielk19777977a172004-11-09 12:44:37 +00001672 if( v==0 ) return;
1673 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001674 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001675 return;
1676 }
drhf2bc0132004-10-04 13:19:23 +00001677 op = pExpr->op;
1678 switch( op ){
drh13449892005-09-07 21:22:45 +00001679 case TK_AGG_COLUMN: {
1680 AggInfo *pAggInfo = pExpr->pAggInfo;
1681 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1682 if( !pAggInfo->directMode ){
1683 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1684 break;
1685 }else if( pAggInfo->useSortingIdx ){
1686 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1687 pCol->iSorterColumn);
1688 break;
1689 }
1690 /* Otherwise, fall thru into the TK_COLUMN case */
1691 }
drh967e8b72000-06-21 13:59:10 +00001692 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001693 if( pExpr->iTable<0 ){
1694 /* This only happens when coding check constraints */
1695 assert( pParse->ckOffset>0 );
1696 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001697 }else{
drh945498f2007-02-24 11:52:52 +00001698 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001699 }
drhcce7d172000-05-31 15:34:51 +00001700 break;
1701 }
1702 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001703 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001704 break;
1705 }
1706 case TK_FLOAT:
1707 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001708 assert( TK_FLOAT==OP_Real );
1709 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001710 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001711 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001712 break;
1713 }
drhf0863fe2005-06-12 21:35:51 +00001714 case TK_NULL: {
1715 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1716 break;
1717 }
danielk19775338a5f2005-01-20 13:03:10 +00001718#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001719 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001720 int n;
1721 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001722 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001723 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001724 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001725 assert( n>=0 );
1726 if( n==0 ){
1727 z = "";
1728 }
1729 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001730 break;
1731 }
danielk19775338a5f2005-01-20 13:03:10 +00001732#endif
drh50457892003-09-06 01:10:47 +00001733 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001734 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001735 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001736 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001737 }
drh50457892003-09-06 01:10:47 +00001738 break;
1739 }
drh4e0cff62004-11-05 05:10:28 +00001740 case TK_REGISTER: {
1741 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1742 break;
1743 }
drh487e2622005-06-25 18:42:14 +00001744#ifndef SQLITE_OMIT_CAST
1745 case TK_CAST: {
1746 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001747 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001748 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001749 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001750 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1751 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1752 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1753 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1754 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1755 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1756 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001757 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001758 break;
1759 }
1760#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001761 case TK_LT:
1762 case TK_LE:
1763 case TK_GT:
1764 case TK_GE:
1765 case TK_NE:
1766 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001767 assert( TK_LT==OP_Lt );
1768 assert( TK_LE==OP_Le );
1769 assert( TK_GT==OP_Gt );
1770 assert( TK_GE==OP_Ge );
1771 assert( TK_EQ==OP_Eq );
1772 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001773 sqlite3ExprCode(pParse, pExpr->pLeft);
1774 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001775 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001776 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001777 break;
drhc9b84a12002-06-20 11:36:48 +00001778 }
drhcce7d172000-05-31 15:34:51 +00001779 case TK_AND:
1780 case TK_OR:
1781 case TK_PLUS:
1782 case TK_STAR:
1783 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001784 case TK_REM:
1785 case TK_BITAND:
1786 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001787 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001788 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001789 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001790 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001791 assert( TK_AND==OP_And );
1792 assert( TK_OR==OP_Or );
1793 assert( TK_PLUS==OP_Add );
1794 assert( TK_MINUS==OP_Subtract );
1795 assert( TK_REM==OP_Remainder );
1796 assert( TK_BITAND==OP_BitAnd );
1797 assert( TK_BITOR==OP_BitOr );
1798 assert( TK_SLASH==OP_Divide );
1799 assert( TK_LSHIFT==OP_ShiftLeft );
1800 assert( TK_RSHIFT==OP_ShiftRight );
1801 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001802 sqlite3ExprCode(pParse, pExpr->pLeft);
1803 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001804 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001805 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001806 break;
1807 }
drhcce7d172000-05-31 15:34:51 +00001808 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001809 Expr *pLeft = pExpr->pLeft;
1810 assert( pLeft );
1811 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1812 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001813 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001814 if( pLeft->op==TK_FLOAT ){
1815 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001816 }else{
drhfec19aa2004-05-19 20:41:03 +00001817 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001818 }
drh6e142f52000-06-08 13:36:40 +00001819 sqliteFree(z);
1820 break;
1821 }
drh1ccde152000-06-17 13:12:39 +00001822 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001823 }
drhbf4133c2001-10-13 02:59:08 +00001824 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001825 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001826 assert( TK_BITNOT==OP_BitNot );
1827 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001828 sqlite3ExprCode(pParse, pExpr->pLeft);
1829 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001830 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001831 break;
1832 }
1833 case TK_ISNULL:
1834 case TK_NOTNULL: {
1835 int dest;
drhf2bc0132004-10-04 13:19:23 +00001836 assert( TK_ISNULL==OP_IsNull );
1837 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001838 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1839 sqlite3ExprCode(pParse, pExpr->pLeft);
1840 dest = sqlite3VdbeCurrentAddr(v) + 2;
1841 sqlite3VdbeAddOp(v, op, 1, dest);
1842 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001843 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001844 break;
drhcce7d172000-05-31 15:34:51 +00001845 }
drh22827922000-06-06 17:27:05 +00001846 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001847 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001848 if( pInfo==0 ){
1849 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1850 &pExpr->span);
1851 }else{
1852 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1853 }
drh22827922000-06-06 17:27:05 +00001854 break;
1855 }
drhb71090f2005-05-23 17:26:51 +00001856 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001857 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001858 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001859 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001860 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001861 int nId;
1862 const char *zId;
drh13449892005-09-07 21:22:45 +00001863 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001864 int i;
danielk197714db2662006-01-09 16:12:04 +00001865 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001866 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001867 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001868 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001869 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001870 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001871 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001872#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001873 /* Possibly overload the function if the first argument is
1874 ** a virtual table column.
1875 **
1876 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1877 ** second argument, not the first, as the argument to test to
1878 ** see if it is a column in a virtual table. This is done because
1879 ** the left operand of infix functions (the operand we want to
1880 ** control overloading) ends up as the second argument to the
1881 ** function. The expression "A glob B" is equivalent to
1882 ** "glob(B,A). We want to use the A in "A glob B" to test
1883 ** for function overloading. But we use the B term in "glob(B,A)".
1884 */
drh6a03a1c2006-07-08 18:34:59 +00001885 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001886 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1887 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001888 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1889 }
1890#endif
danielk1977682f68b2004-06-05 10:22:17 +00001891 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001892 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001893 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001894 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001895 if( pDef->needCollSeq && !pColl ){
1896 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1897 }
1898 }
1899 if( pDef->needCollSeq ){
1900 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001901 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001902 }
drh13449892005-09-07 21:22:45 +00001903 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001904 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001905 break;
1906 }
drhfe2093d2005-01-20 22:48:47 +00001907#ifndef SQLITE_OMIT_SUBQUERY
1908 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001909 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001910 if( pExpr->iColumn==0 ){
1911 sqlite3CodeSubselect(pParse, pExpr);
1912 }
danielk19774adee202004-05-08 08:23:19 +00001913 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001914 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001915 break;
1916 }
drhfef52082000-06-06 01:50:43 +00001917 case TK_IN: {
1918 int addr;
drh94a11212004-09-25 13:12:14 +00001919 char affinity;
drhafa5f682006-01-30 14:36:59 +00001920 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001921 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001922
1923 /* Figure out the affinity to use to create a key from the results
1924 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001925 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001926 */
drh94a11212004-09-25 13:12:14 +00001927 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001928
danielk19774adee202004-05-08 08:23:19 +00001929 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001930 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001931
1932 /* Code the <expr> from "<expr> IN (...)". The temporary table
1933 ** pExpr->iTable contains the values that make up the (...) set.
1934 */
danielk19774adee202004-05-08 08:23:19 +00001935 sqlite3ExprCode(pParse, pExpr->pLeft);
1936 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001937 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001938 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001939 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001940 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001941 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001942 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1943 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1944
drhfef52082000-06-06 01:50:43 +00001945 break;
1946 }
danielk197793758c82005-01-21 08:13:14 +00001947#endif
drhfef52082000-06-06 01:50:43 +00001948 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001949 Expr *pLeft = pExpr->pLeft;
1950 struct ExprList_item *pLItem = pExpr->pList->a;
1951 Expr *pRight = pLItem->pExpr;
1952 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001953 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001954 sqlite3ExprCode(pParse, pRight);
1955 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001956 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001957 pLItem++;
1958 pRight = pLItem->pExpr;
1959 sqlite3ExprCode(pParse, pRight);
1960 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001961 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001962 break;
1963 }
drh51e9a442004-01-16 16:42:53 +00001964 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001965 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001966 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001967 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001968 break;
1969 }
drh17a7f8d2002-03-24 13:13:27 +00001970 case TK_CASE: {
1971 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001972 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001973 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001974 int i;
drhbe5c89a2004-07-26 00:31:09 +00001975 ExprList *pEList;
1976 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001977
1978 assert(pExpr->pList);
1979 assert((pExpr->pList->nExpr % 2) == 0);
1980 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001981 pEList = pExpr->pList;
1982 aListelem = pEList->a;
1983 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001984 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001985 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001986 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001987 }
drhf5905aa2002-05-26 20:54:33 +00001988 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001989 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001990 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001991 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001992 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1993 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001994 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001995 }else{
danielk19774adee202004-05-08 08:23:19 +00001996 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001997 }
drhbe5c89a2004-07-26 00:31:09 +00001998 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001999 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002000 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002001 }
drhf570f012002-05-31 15:51:25 +00002002 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002003 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002004 }
drh17a7f8d2002-03-24 13:13:27 +00002005 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002006 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002007 }else{
drhf0863fe2005-06-12 21:35:51 +00002008 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002009 }
danielk19774adee202004-05-08 08:23:19 +00002010 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002011 break;
2012 }
danielk19775338a5f2005-01-20 13:03:10 +00002013#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002014 case TK_RAISE: {
2015 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002016 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002017 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002018 return;
2019 }
drhad6d9462004-09-19 02:15:24 +00002020 if( pExpr->iColumn!=OE_Ignore ){
2021 assert( pExpr->iColumn==OE_Rollback ||
2022 pExpr->iColumn == OE_Abort ||
2023 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002024 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002025 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002026 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002027 } else {
drhad6d9462004-09-19 02:15:24 +00002028 assert( pExpr->iColumn == OE_Ignore );
2029 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2030 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2031 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002032 }
drhffe07b22005-11-03 00:41:17 +00002033 stackChng = 0;
2034 break;
drh17a7f8d2002-03-24 13:13:27 +00002035 }
danielk19775338a5f2005-01-20 13:03:10 +00002036#endif
drhffe07b22005-11-03 00:41:17 +00002037 }
2038
2039 if( pParse->ckOffset ){
2040 pParse->ckOffset += stackChng;
2041 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002042 }
drhcce7d172000-05-31 15:34:51 +00002043}
2044
danielk197793758c82005-01-21 08:13:14 +00002045#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002046/*
drh25303782004-12-07 15:41:48 +00002047** Generate code that evalutes the given expression and leaves the result
2048** on the stack. See also sqlite3ExprCode().
2049**
2050** This routine might also cache the result and modify the pExpr tree
2051** so that it will make use of the cached result on subsequent evaluations
2052** rather than evaluate the whole expression again. Trivial expressions are
2053** not cached. If the expression is cached, its result is stored in a
2054** memory location.
2055*/
2056void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2057 Vdbe *v = pParse->pVdbe;
2058 int iMem;
2059 int addr1, addr2;
2060 if( v==0 ) return;
2061 addr1 = sqlite3VdbeCurrentAddr(v);
2062 sqlite3ExprCode(pParse, pExpr);
2063 addr2 = sqlite3VdbeCurrentAddr(v);
2064 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2065 iMem = pExpr->iTable = pParse->nMem++;
2066 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2067 pExpr->op = TK_REGISTER;
2068 }
2069}
danielk197793758c82005-01-21 08:13:14 +00002070#endif
drh25303782004-12-07 15:41:48 +00002071
2072/*
drh268380c2004-02-25 13:47:31 +00002073** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002074** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002075**
2076** Return the number of elements pushed onto the stack.
2077*/
danielk19774adee202004-05-08 08:23:19 +00002078int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002079 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002080 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002081){
2082 struct ExprList_item *pItem;
2083 int i, n;
drh268380c2004-02-25 13:47:31 +00002084 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002085 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002086 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002087 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002088 }
drhf9b596e2004-05-26 16:54:42 +00002089 return n;
drh268380c2004-02-25 13:47:31 +00002090}
2091
2092/*
drhcce7d172000-05-31 15:34:51 +00002093** Generate code for a boolean expression such that a jump is made
2094** to the label "dest" if the expression is true but execution
2095** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002096**
2097** If the expression evaluates to NULL (neither true nor false), then
2098** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002099**
2100** This code depends on the fact that certain token values (ex: TK_EQ)
2101** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2102** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2103** the make process cause these values to align. Assert()s in the code
2104** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002105*/
danielk19774adee202004-05-08 08:23:19 +00002106void sqlite3ExprIfTrue(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 op = pExpr->op;
2112 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002113 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002114 int d2 = sqlite3VdbeMakeLabel(v);
2115 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2116 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2117 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002118 break;
2119 }
2120 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002121 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2122 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002123 break;
2124 }
2125 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002126 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002127 break;
2128 }
2129 case TK_LT:
2130 case TK_LE:
2131 case TK_GT:
2132 case TK_GE:
2133 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002134 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002135 assert( TK_LT==OP_Lt );
2136 assert( TK_LE==OP_Le );
2137 assert( TK_GT==OP_Gt );
2138 assert( TK_GE==OP_Ge );
2139 assert( TK_EQ==OP_Eq );
2140 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002141 sqlite3ExprCode(pParse, pExpr->pLeft);
2142 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002143 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002144 break;
2145 }
2146 case TK_ISNULL:
2147 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002148 assert( TK_ISNULL==OP_IsNull );
2149 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002150 sqlite3ExprCode(pParse, pExpr->pLeft);
2151 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002152 break;
2153 }
drhfef52082000-06-06 01:50:43 +00002154 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002155 /* The expression "x BETWEEN y AND z" is implemented as:
2156 **
2157 ** 1 IF (x < y) GOTO 3
2158 ** 2 IF (x <= z) GOTO <dest>
2159 ** 3 ...
2160 */
drhf5905aa2002-05-26 20:54:33 +00002161 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002162 Expr *pLeft = pExpr->pLeft;
2163 Expr *pRight = pExpr->pList->a[0].pExpr;
2164 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002165 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002166 sqlite3ExprCode(pParse, pRight);
2167 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002168
drhbe5c89a2004-07-26 00:31:09 +00002169 pRight = pExpr->pList->a[1].pExpr;
2170 sqlite3ExprCode(pParse, pRight);
2171 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002172
danielk19774adee202004-05-08 08:23:19 +00002173 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002174 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002175 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002176 break;
2177 }
drhcce7d172000-05-31 15:34:51 +00002178 default: {
danielk19774adee202004-05-08 08:23:19 +00002179 sqlite3ExprCode(pParse, pExpr);
2180 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002181 break;
2182 }
2183 }
drhffe07b22005-11-03 00:41:17 +00002184 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002185}
2186
2187/*
drh66b89c82000-11-28 20:47:17 +00002188** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002189** to the label "dest" if the expression is false but execution
2190** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002191**
2192** If the expression evaluates to NULL (neither true nor false) then
2193** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002194*/
danielk19774adee202004-05-08 08:23:19 +00002195void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002196 Vdbe *v = pParse->pVdbe;
2197 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002198 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002199 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002200
2201 /* The value of pExpr->op and op are related as follows:
2202 **
2203 ** pExpr->op op
2204 ** --------- ----------
2205 ** TK_ISNULL OP_NotNull
2206 ** TK_NOTNULL OP_IsNull
2207 ** TK_NE OP_Eq
2208 ** TK_EQ OP_Ne
2209 ** TK_GT OP_Le
2210 ** TK_LE OP_Gt
2211 ** TK_GE OP_Lt
2212 ** TK_LT OP_Ge
2213 **
2214 ** For other values of pExpr->op, op is undefined and unused.
2215 ** The value of TK_ and OP_ constants are arranged such that we
2216 ** can compute the mapping above using the following expression.
2217 ** Assert()s verify that the computation is correct.
2218 */
2219 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2220
2221 /* Verify correct alignment of TK_ and OP_ constants
2222 */
2223 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2224 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2225 assert( pExpr->op!=TK_NE || op==OP_Eq );
2226 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2227 assert( pExpr->op!=TK_LT || op==OP_Ge );
2228 assert( pExpr->op!=TK_LE || op==OP_Gt );
2229 assert( pExpr->op!=TK_GT || op==OP_Le );
2230 assert( pExpr->op!=TK_GE || op==OP_Lt );
2231
drhcce7d172000-05-31 15:34:51 +00002232 switch( pExpr->op ){
2233 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002234 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2235 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002236 break;
2237 }
2238 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002239 int d2 = sqlite3VdbeMakeLabel(v);
2240 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2241 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2242 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002243 break;
2244 }
2245 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002246 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002247 break;
2248 }
2249 case TK_LT:
2250 case TK_LE:
2251 case TK_GT:
2252 case TK_GE:
2253 case TK_NE:
2254 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002255 sqlite3ExprCode(pParse, pExpr->pLeft);
2256 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002257 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002258 break;
2259 }
drhcce7d172000-05-31 15:34:51 +00002260 case TK_ISNULL:
2261 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002262 sqlite3ExprCode(pParse, pExpr->pLeft);
2263 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002264 break;
2265 }
drhfef52082000-06-06 01:50:43 +00002266 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002267 /* The expression is "x BETWEEN y AND z". It is implemented as:
2268 **
2269 ** 1 IF (x >= y) GOTO 3
2270 ** 2 GOTO <dest>
2271 ** 3 IF (x > z) GOTO <dest>
2272 */
drhfef52082000-06-06 01:50:43 +00002273 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002274 Expr *pLeft = pExpr->pLeft;
2275 Expr *pRight = pExpr->pList->a[0].pExpr;
2276 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002277 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002278 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002279 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002280 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2281
danielk19774adee202004-05-08 08:23:19 +00002282 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2283 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002284 pRight = pExpr->pList->a[1].pExpr;
2285 sqlite3ExprCode(pParse, pRight);
2286 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002287 break;
2288 }
drhcce7d172000-05-31 15:34:51 +00002289 default: {
danielk19774adee202004-05-08 08:23:19 +00002290 sqlite3ExprCode(pParse, pExpr);
2291 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002292 break;
2293 }
2294 }
drhffe07b22005-11-03 00:41:17 +00002295 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002296}
drh22827922000-06-06 17:27:05 +00002297
2298/*
2299** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2300** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002301**
2302** Sometimes this routine will return FALSE even if the two expressions
2303** really are equivalent. If we cannot prove that the expressions are
2304** identical, we return FALSE just to be safe. So if this routine
2305** returns false, then you do not really know for certain if the two
2306** expressions are the same. But if you get a TRUE return, then you
2307** can be sure the expressions are the same. In the places where
2308** this routine is used, it does not hurt to get an extra FALSE - that
2309** just might result in some slightly slower code. But returning
2310** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002311*/
danielk19774adee202004-05-08 08:23:19 +00002312int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002313 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002314 if( pA==0||pB==0 ){
2315 return pB==pA;
drh22827922000-06-06 17:27:05 +00002316 }
2317 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002318 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002319 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2320 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002321 if( pA->pList ){
2322 if( pB->pList==0 ) return 0;
2323 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2324 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002325 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002326 return 0;
2327 }
2328 }
2329 }else if( pB->pList ){
2330 return 0;
2331 }
2332 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002333 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002334 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002335 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002336 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002337 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2338 return 0;
2339 }
drh22827922000-06-06 17:27:05 +00002340 }
2341 return 1;
2342}
2343
drh13449892005-09-07 21:22:45 +00002344
drh22827922000-06-06 17:27:05 +00002345/*
drh13449892005-09-07 21:22:45 +00002346** Add a new element to the pAggInfo->aCol[] array. Return the index of
2347** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002348*/
drh13449892005-09-07 21:22:45 +00002349static int addAggInfoColumn(AggInfo *pInfo){
2350 int i;
drhcf643722007-03-27 13:36:37 +00002351 pInfo->aCol = sqlite3ArrayAllocate(
2352 pInfo->aCol,
2353 sizeof(pInfo->aCol[0]),
2354 3,
2355 &pInfo->nColumn,
2356 &pInfo->nColumnAlloc,
2357 &i
2358 );
drh13449892005-09-07 21:22:45 +00002359 return i;
2360}
2361
2362/*
2363** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2364** the new element. Return a negative number if malloc fails.
2365*/
2366static int addAggInfoFunc(AggInfo *pInfo){
2367 int i;
drhcf643722007-03-27 13:36:37 +00002368 pInfo->aFunc = sqlite3ArrayAllocate(
2369 pInfo->aFunc,
2370 sizeof(pInfo->aFunc[0]),
2371 3,
2372 &pInfo->nFunc,
2373 &pInfo->nFuncAlloc,
2374 &i
2375 );
drh13449892005-09-07 21:22:45 +00002376 return i;
2377}
drh22827922000-06-06 17:27:05 +00002378
2379/*
drh626a8792005-01-17 22:08:19 +00002380** This is an xFunc for walkExprTree() used to implement
2381** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2382** for additional information.
drh22827922000-06-06 17:27:05 +00002383**
drh626a8792005-01-17 22:08:19 +00002384** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002385*/
drh626a8792005-01-17 22:08:19 +00002386static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002387 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002388 NameContext *pNC = (NameContext *)pArg;
2389 Parse *pParse = pNC->pParse;
2390 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002391 AggInfo *pAggInfo = pNC->pAggInfo;
2392
drh22827922000-06-06 17:27:05 +00002393
drh22827922000-06-06 17:27:05 +00002394 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002395 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002396 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002397 /* Check to see if the column is in one of the tables in the FROM
2398 ** clause of the aggregate query */
2399 if( pSrcList ){
2400 struct SrcList_item *pItem = pSrcList->a;
2401 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2402 struct AggInfo_col *pCol;
2403 if( pExpr->iTable==pItem->iCursor ){
2404 /* If we reach this point, it means that pExpr refers to a table
2405 ** that is in the FROM clause of the aggregate query.
2406 **
2407 ** Make an entry for the column in pAggInfo->aCol[] if there
2408 ** is not an entry there already.
2409 */
drh7f906d62007-03-12 23:48:52 +00002410 int k;
drh13449892005-09-07 21:22:45 +00002411 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002412 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002413 if( pCol->iTable==pExpr->iTable &&
2414 pCol->iColumn==pExpr->iColumn ){
2415 break;
2416 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002417 }
drh7f906d62007-03-12 23:48:52 +00002418 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2419 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002420 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002421 pCol->iTable = pExpr->iTable;
2422 pCol->iColumn = pExpr->iColumn;
2423 pCol->iMem = pParse->nMem++;
2424 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002425 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002426 if( pAggInfo->pGroupBy ){
2427 int j, n;
2428 ExprList *pGB = pAggInfo->pGroupBy;
2429 struct ExprList_item *pTerm = pGB->a;
2430 n = pGB->nExpr;
2431 for(j=0; j<n; j++, pTerm++){
2432 Expr *pE = pTerm->pExpr;
2433 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2434 pE->iColumn==pExpr->iColumn ){
2435 pCol->iSorterColumn = j;
2436 break;
2437 }
2438 }
2439 }
2440 if( pCol->iSorterColumn<0 ){
2441 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2442 }
2443 }
2444 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2445 ** because it was there before or because we just created it).
2446 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2447 ** pAggInfo->aCol[] entry.
2448 */
2449 pExpr->pAggInfo = pAggInfo;
2450 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002451 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002452 break;
2453 } /* endif pExpr->iTable==pItem->iCursor */
2454 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002455 }
drh626a8792005-01-17 22:08:19 +00002456 return 1;
drh22827922000-06-06 17:27:05 +00002457 }
2458 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002459 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2460 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002461 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002462 /* Check to see if pExpr is a duplicate of another aggregate
2463 ** function that is already in the pAggInfo structure
2464 */
2465 struct AggInfo_func *pItem = pAggInfo->aFunc;
2466 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2467 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002468 break;
2469 }
drh22827922000-06-06 17:27:05 +00002470 }
drh13449892005-09-07 21:22:45 +00002471 if( i>=pAggInfo->nFunc ){
2472 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2473 */
danielk197714db2662006-01-09 16:12:04 +00002474 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002475 i = addAggInfoFunc(pAggInfo);
2476 if( i>=0 ){
2477 pItem = &pAggInfo->aFunc[i];
2478 pItem->pExpr = pExpr;
2479 pItem->iMem = pParse->nMem++;
2480 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002481 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002482 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002483 if( pExpr->flags & EP_Distinct ){
2484 pItem->iDistinct = pParse->nTab++;
2485 }else{
2486 pItem->iDistinct = -1;
2487 }
drh13449892005-09-07 21:22:45 +00002488 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002489 }
drh13449892005-09-07 21:22:45 +00002490 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2491 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002492 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002493 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002494 return 1;
drh22827922000-06-06 17:27:05 +00002495 }
drh22827922000-06-06 17:27:05 +00002496 }
2497 }
drh13449892005-09-07 21:22:45 +00002498
2499 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2500 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2501 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2502 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002503 if( pExpr->pSelect ){
2504 pNC->nDepth++;
2505 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2506 pNC->nDepth--;
2507 }
drh626a8792005-01-17 22:08:19 +00002508 return 0;
2509}
2510
2511/*
2512** Analyze the given expression looking for aggregate functions and
2513** for variables that need to be added to the pParse->aAgg[] array.
2514** Make additional entries to the pParse->aAgg[] array as necessary.
2515**
2516** This routine should only be called after the expression has been
2517** analyzed by sqlite3ExprResolveNames().
2518**
2519** If errors are seen, leave an error message in zErrMsg and return
2520** the number of errors.
2521*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002522int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2523 int nErr = pNC->pParse->nErr;
2524 walkExprTree(pExpr, analyzeAggregate, pNC);
2525 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002526}
drh5d9a4af2005-08-30 00:54:01 +00002527
2528/*
2529** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2530** expression list. Return the number of errors.
2531**
2532** If an error is found, the analysis is cut short.
2533*/
2534int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2535 struct ExprList_item *pItem;
2536 int i;
2537 int nErr = 0;
2538 if( pList ){
2539 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2540 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2541 }
2542 }
2543 return nErr;
2544}