blob: e7358b554059fa47d901456f2574f1105512b7fc [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**
danielk1977fc976062007-05-10 10:46:56 +000015** $Id: expr.c,v 1.289 2007/05/10 10:46:56 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
drh8b4c40d2007-02-01 23:02:45 +000053** Set the collating sequence for expression pExpr to be the collating
54** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000055** The collating sequence is marked as "explicit" using the EP_ExpCollate
56** flag. An explicit collating sequence will override implicit
57** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000058*/
59Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
60 CollSeq *pColl;
61 if( pExpr==0 ) return 0;
62 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
63 if( pColl ){
64 pExpr->pColl = pColl;
65 pExpr->flags |= EP_ExpCollate;
66 }
67 return pExpr;
68}
69
70/*
danielk19770202b292004-06-09 09:55:16 +000071** Return the default collation sequence for the expression pExpr. If
72** there is no default collation type, return 0.
73*/
danielk19777cedc8d2004-06-10 10:50:08 +000074CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
75 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000076 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000077 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000078 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000079 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000080 }
81 }
danielk19777cedc8d2004-06-10 10:50:08 +000082 if( sqlite3CheckCollSeq(pParse, pColl) ){
83 pColl = 0;
84 }
85 return pColl;
danielk19770202b292004-06-09 09:55:16 +000086}
87
88/*
drh626a8792005-01-17 22:08:19 +000089** pExpr is an operand of a comparison operator. aff2 is the
90** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000091** type affinity that should be used for the comparison operator.
92*/
danielk1977e014a832004-05-17 10:48:57 +000093char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000094 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000095 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000096 /* Both sides of the comparison are columns. If one has numeric
97 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh8a512562005-11-14 22:29:05 +000099 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000100 return SQLITE_AFF_NUMERIC;
101 }else{
102 return SQLITE_AFF_NONE;
103 }
104 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000105 /* Neither side of the comparison is a column. Compare the
106 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000107 */
drh5f6a87b2004-07-19 00:39:45 +0000108 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000109 }else{
110 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000111 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000112 return (aff1 + aff2);
113 }
114}
115
drh53db1452004-05-20 13:54:53 +0000116/*
117** pExpr is a comparison operator. Return the type affinity that should
118** be applied to both operands prior to doing the comparison.
119*/
danielk1977e014a832004-05-17 10:48:57 +0000120static char comparisonAffinity(Expr *pExpr){
121 char aff;
122 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
123 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
124 pExpr->op==TK_NE );
125 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000126 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000127 if( pExpr->pRight ){
128 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
129 }
130 else if( pExpr->pSelect ){
131 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
132 }
133 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000134 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000135 }
136 return aff;
137}
138
139/*
140** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
141** idx_affinity is the affinity of an indexed column. Return true
142** if the index with affinity idx_affinity may be used to implement
143** the comparison in pExpr.
144*/
145int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
146 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000147 switch( aff ){
148 case SQLITE_AFF_NONE:
149 return 1;
150 case SQLITE_AFF_TEXT:
151 return idx_affinity==SQLITE_AFF_TEXT;
152 default:
153 return sqlite3IsNumericAffinity(idx_affinity);
154 }
danielk1977e014a832004-05-17 10:48:57 +0000155}
156
danielk1977a37cdde2004-05-16 11:15:36 +0000157/*
158** Return the P1 value that should be used for a binary comparison
159** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
160** If jumpIfNull is true, then set the low byte of the returned
161** P1 value to tell the opcode to jump if either expression
162** evaluates to NULL.
163*/
danielk1977e014a832004-05-17 10:48:57 +0000164static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000165 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000166 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000167}
168
drha2e00042002-01-22 03:13:42 +0000169/*
danielk19770202b292004-06-09 09:55:16 +0000170** Return a pointer to the collation sequence that should be used by
171** a binary comparison operator comparing pLeft and pRight.
172**
173** If the left hand expression has a collating sequence type, then it is
174** used. Otherwise the collation sequence for the right hand expression
175** is used, or the default (BINARY) if neither expression has a collating
176** type.
177*/
danielk19777cedc8d2004-06-10 10:50:08 +0000178static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
drhec41dda2007-02-07 13:09:45 +0000179 CollSeq *pColl;
180 assert( pLeft );
181 assert( pRight );
182 if( pLeft->flags & EP_ExpCollate ){
183 assert( pLeft->pColl );
184 pColl = pLeft->pColl;
185 }else if( pRight->flags & EP_ExpCollate ){
186 assert( pRight->pColl );
187 pColl = pRight->pColl;
188 }else{
189 pColl = sqlite3ExprCollSeq(pParse, pLeft);
190 if( !pColl ){
191 pColl = sqlite3ExprCollSeq(pParse, pRight);
192 }
danielk19770202b292004-06-09 09:55:16 +0000193 }
194 return pColl;
195}
196
197/*
drhbe5c89a2004-07-26 00:31:09 +0000198** Generate code for a comparison operator.
199*/
200static int codeCompare(
201 Parse *pParse, /* The parsing (and code generating) context */
202 Expr *pLeft, /* The left operand */
203 Expr *pRight, /* The right operand */
204 int opcode, /* The comparison opcode */
205 int dest, /* Jump here if true. */
206 int jumpIfNull /* If true, jump if either operand is NULL */
207){
208 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
209 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000210 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000211}
212
213/*
drha76b5df2002-02-23 02:32:10 +0000214** Construct a new expression node and return a pointer to it. Memory
215** for this node is obtained from sqliteMalloc(). The calling function
216** is responsible for making sure the node eventually gets freed.
217*/
drhe4e72072004-11-23 01:47:30 +0000218Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000219 Expr *pNew;
220 pNew = sqliteMalloc( sizeof(Expr) );
221 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000222 /* When malloc fails, delete pLeft and pRight. Expressions passed to
223 ** this function must always be allocated with sqlite3Expr() for this
224 ** reason.
225 */
226 sqlite3ExprDelete(pLeft);
227 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000228 return 0;
229 }
230 pNew->op = op;
231 pNew->pLeft = pLeft;
232 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000233 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000234 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000235 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000236 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000237 }else if( pLeft ){
238 if( pRight ){
239 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000240 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000241 pNew->flags |= EP_ExpCollate;
242 pNew->pColl = pRight->pColl;
243 }
244 }
drh5ffb3ac2007-04-18 17:07:57 +0000245 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000246 pNew->flags |= EP_ExpCollate;
247 pNew->pColl = pLeft->pColl;
248 }
drha76b5df2002-02-23 02:32:10 +0000249 }
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;
drh4e0cff62004-11-05 05:10:28 +0000283 if( pParse->nested==0 ){
284 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
285 return 0;
286 }
drhbb7ac002005-08-12 22:58:53 +0000287 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000288 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000289 if( p==0 ){
290 return 0; /* Malloc failed */
291 }
drh2646da72005-12-09 20:02:05 +0000292 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000293 p->iTable = pParse->nMem++;
294 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
295 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000296 return p;
297}
298
299/*
drh91bb0ee2004-09-01 03:06:34 +0000300** Join two expressions using an AND operator. If either expression is
301** NULL, then just return the other expression.
302*/
303Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
304 if( pLeft==0 ){
305 return pRight;
306 }else if( pRight==0 ){
307 return pLeft;
308 }else{
309 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
310 }
311}
312
313/*
drh6977fea2002-10-22 23:38:04 +0000314** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000315** text between the two given tokens.
316*/
danielk19774adee202004-05-08 08:23:19 +0000317void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000318 assert( pRight!=0 );
319 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000320 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000321 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000322 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000323 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000324 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000325 }else{
drh6977fea2002-10-22 23:38:04 +0000326 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000327 }
drha76b5df2002-02-23 02:32:10 +0000328 }
329}
330
331/*
332** Construct a new expression node for a function with multiple
333** arguments.
334*/
danielk19774adee202004-05-08 08:23:19 +0000335Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000336 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000337 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000338 pNew = sqliteMalloc( sizeof(Expr) );
339 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000340 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000341 return 0;
342 }
343 pNew->op = TK_FUNCTION;
344 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000345 assert( pToken->dyn==0 );
346 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000347 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000348
349 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000350 return pNew;
351}
352
353/*
drhfa6bc002004-09-07 16:19:52 +0000354** Assign a variable number to an expression that encodes a wildcard
355** in the original SQL statement.
356**
357** Wildcards consisting of a single "?" are assigned the next sequential
358** variable number.
359**
360** Wildcards of the form "?nnn" are assigned the number "nnn". We make
361** sure "nnn" is not too be to avoid a denial of service attack when
362** the SQL statement comes from an external source.
363**
364** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
365** as the previous instance of the same wildcard. Or if this is the first
366** instance of the wildcard, the next sequenial variable number is
367** assigned.
368*/
369void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
370 Token *pToken;
371 if( pExpr==0 ) return;
372 pToken = &pExpr->token;
373 assert( pToken->n>=1 );
374 assert( pToken->z!=0 );
375 assert( pToken->z[0]!=0 );
376 if( pToken->n==1 ){
377 /* Wildcard of the form "?". Assign the next variable number */
378 pExpr->iTable = ++pParse->nVar;
379 }else if( pToken->z[0]=='?' ){
380 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
381 ** use it as the variable number */
382 int i;
drh2646da72005-12-09 20:02:05 +0000383 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000384 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
385 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
386 SQLITE_MAX_VARIABLE_NUMBER);
387 }
388 if( i>pParse->nVar ){
389 pParse->nVar = i;
390 }
391 }else{
392 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
393 ** number as the prior appearance of the same name, or if the name
394 ** has never appeared before, reuse the same variable number
395 */
396 int i, n;
397 n = pToken->n;
398 for(i=0; i<pParse->nVarExpr; i++){
399 Expr *pE;
400 if( (pE = pParse->apVarExpr[i])!=0
401 && pE->token.n==n
402 && memcmp(pE->token.z, pToken->z, n)==0 ){
403 pExpr->iTable = pE->iTable;
404 break;
405 }
406 }
407 if( i>=pParse->nVarExpr ){
408 pExpr->iTable = ++pParse->nVar;
409 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
410 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000411 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000412 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
413 }
danielk19779e128002006-01-18 16:51:35 +0000414 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000415 assert( pParse->apVarExpr!=0 );
416 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
417 }
418 }
419 }
danielk1977832b2662007-05-09 11:37:22 +0000420 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
421 sqlite3ErrorMsg(pParse, "too many SQL variables");
422 }
drhfa6bc002004-09-07 16:19:52 +0000423}
424
425/*
drha2e00042002-01-22 03:13:42 +0000426** Recursively delete an expression tree.
427*/
danielk19774adee202004-05-08 08:23:19 +0000428void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000429 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000430 if( p->span.dyn ) sqliteFree((char*)p->span.z);
431 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000432 sqlite3ExprDelete(p->pLeft);
433 sqlite3ExprDelete(p->pRight);
434 sqlite3ExprListDelete(p->pList);
435 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000436 sqliteFree(p);
437}
438
drhd2687b72005-08-12 22:56:09 +0000439/*
440** The Expr.token field might be a string literal that is quoted.
441** If so, remove the quotation marks.
442*/
443void sqlite3DequoteExpr(Expr *p){
444 if( ExprHasAnyProperty(p, EP_Dequoted) ){
445 return;
446 }
447 ExprSetProperty(p, EP_Dequoted);
448 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000449 sqlite3TokenCopy(&p->token, &p->token);
450 }
451 sqlite3Dequote((char*)p->token.z);
452}
453
drha76b5df2002-02-23 02:32:10 +0000454
455/*
drhff78bd22002-02-27 01:47:11 +0000456** The following group of routines make deep copies of expressions,
457** expression lists, ID lists, and select statements. The copies can
458** be deleted (by being passed to their respective ...Delete() routines)
459** without effecting the originals.
460**
danielk19774adee202004-05-08 08:23:19 +0000461** The expression list, ID, and source lists return by sqlite3ExprListDup(),
462** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000463** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000464**
drhad3cab52002-05-24 02:04:32 +0000465** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000466*/
danielk19774adee202004-05-08 08:23:19 +0000467Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000468 Expr *pNew;
469 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000470 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000471 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000472 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000473 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000474 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000475 pNew->token.dyn = 1;
476 }else{
drh4efc4752004-01-16 15:55:37 +0000477 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000478 }
drh6977fea2002-10-22 23:38:04 +0000479 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000480 pNew->pLeft = sqlite3ExprDup(p->pLeft);
481 pNew->pRight = sqlite3ExprDup(p->pRight);
482 pNew->pList = sqlite3ExprListDup(p->pList);
483 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000484 pNew->pTab = p->pTab;
danielk1977fc976062007-05-10 10:46:56 +0000485#if SQLITE_MAX_EXPR_DEPTH>0
486 pNew->nHeight = p->nHeight;
487#endif
drhff78bd22002-02-27 01:47:11 +0000488 return pNew;
489}
danielk19774adee202004-05-08 08:23:19 +0000490void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000491 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000492 if( pFrom->z ){
493 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000494 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000495 pTo->dyn = 1;
496 }else{
drh4b59ab52002-08-24 18:24:51 +0000497 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000498 }
499}
danielk19774adee202004-05-08 08:23:19 +0000500ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000501 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000502 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000503 int i;
504 if( p==0 ) return 0;
505 pNew = sqliteMalloc( sizeof(*pNew) );
506 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000507 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000508 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000509 if( pItem==0 ){
510 sqliteFree(pNew);
511 return 0;
512 }
drh145716b2004-09-24 12:24:06 +0000513 pOldItem = p->a;
514 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000515 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000516 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000517 if( pOldExpr->span.z!=0 && pNewExpr ){
518 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000519 ** expression list. The logic in SELECT processing that determines
520 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000521 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000522 }
drh1f3e9052002-10-31 00:09:39 +0000523 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000524 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000525 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000526 pItem->zName = sqliteStrDup(pOldItem->zName);
527 pItem->sortOrder = pOldItem->sortOrder;
528 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000529 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000530 }
531 return pNew;
532}
danielk197793758c82005-01-21 08:13:14 +0000533
534/*
535** If cursors, triggers, views and subqueries are all omitted from
536** the build, then none of the following routines, except for
537** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
538** called with a NULL argument.
539*/
danielk19776a67fe82005-02-04 04:07:16 +0000540#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
541 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000542SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000543 SrcList *pNew;
544 int i;
drh113088e2003-03-20 01:16:58 +0000545 int nByte;
drhad3cab52002-05-24 02:04:32 +0000546 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000547 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000548 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000549 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000550 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000551 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000552 struct SrcList_item *pNewItem = &pNew->a[i];
553 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000554 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000555 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
556 pNewItem->zName = sqliteStrDup(pOldItem->zName);
557 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
558 pNewItem->jointype = pOldItem->jointype;
559 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000560 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000561 pTab = pNewItem->pTab = pOldItem->pTab;
562 if( pTab ){
563 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000564 }
danielk19774adee202004-05-08 08:23:19 +0000565 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
566 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
567 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000568 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000569 }
570 return pNew;
571}
danielk19774adee202004-05-08 08:23:19 +0000572IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000573 IdList *pNew;
574 int i;
575 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000576 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000577 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000578 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000579 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000580 if( pNew->a==0 ){
581 sqliteFree(pNew);
582 return 0;
583 }
drhff78bd22002-02-27 01:47:11 +0000584 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000585 struct IdList_item *pNewItem = &pNew->a[i];
586 struct IdList_item *pOldItem = &p->a[i];
587 pNewItem->zName = sqliteStrDup(pOldItem->zName);
588 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000589 }
590 return pNew;
591}
danielk19774adee202004-05-08 08:23:19 +0000592Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000593 Select *pNew;
594 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000595 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000596 if( pNew==0 ) return 0;
597 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000598 pNew->pEList = sqlite3ExprListDup(p->pEList);
599 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
600 pNew->pWhere = sqlite3ExprDup(p->pWhere);
601 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
602 pNew->pHaving = sqlite3ExprDup(p->pHaving);
603 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000604 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000605 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000606 pNew->pLimit = sqlite3ExprDup(p->pLimit);
607 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000608 pNew->iLimit = -1;
609 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000610 pNew->isResolved = p->isResolved;
611 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000612 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000613 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000614 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000615 pNew->addrOpenEphm[0] = -1;
616 pNew->addrOpenEphm[1] = -1;
617 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000618 return pNew;
619}
danielk197793758c82005-01-21 08:13:14 +0000620#else
621Select *sqlite3SelectDup(Select *p){
622 assert( p==0 );
623 return 0;
624}
625#endif
drhff78bd22002-02-27 01:47:11 +0000626
627
628/*
drha76b5df2002-02-23 02:32:10 +0000629** Add a new element to the end of an expression list. If pList is
630** initially NULL, then create a new expression list.
631*/
danielk19774adee202004-05-08 08:23:19 +0000632ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000633 if( pList==0 ){
634 pList = sqliteMalloc( sizeof(ExprList) );
635 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000636 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000637 }
drh4efc4752004-01-16 15:55:37 +0000638 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000639 }
drh4305d102003-07-30 12:34:12 +0000640 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000641 struct ExprList_item *a;
642 int n = pList->nAlloc*2 + 4;
643 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
644 if( a==0 ){
645 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000646 }
danielk1977d5d56522005-03-16 12:15:20 +0000647 pList->a = a;
648 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000649 }
drh4efc4752004-01-16 15:55:37 +0000650 assert( pList->a!=0 );
651 if( pExpr || pName ){
652 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
653 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000654 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000655 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000656 }
657 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000658
659no_mem:
660 /* Avoid leaking memory if malloc has failed. */
661 sqlite3ExprDelete(pExpr);
662 sqlite3ExprListDelete(pList);
663 return 0;
drha76b5df2002-02-23 02:32:10 +0000664}
665
666/*
danielk19777a15a4b2007-05-08 17:54:43 +0000667** If the expression list pEList contains more than iLimit elements,
668** leave an error message in pParse.
669*/
670void sqlite3ExprListCheckLength(
671 Parse *pParse,
672 ExprList *pEList,
673 int iLimit,
674 const char *zObject
675){
danielk1977b4fc6792007-05-08 18:04:46 +0000676 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000677 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
678 }
679}
680
danielk1977fc976062007-05-10 10:46:56 +0000681
682#if SQLITE_MAX_EXPR_DEPTH>0
683/* The following three functions, heightOfExpr(), heightOfExprList()
684** and heightOfSelect(), are used to determine the maximum height
685** of any expression tree referenced by the structure passed as the
686** first argument.
687**
688** If this maximum height is greater than the current value pointed
689** to by pnHeight, the second parameter, then set *pnHeight to that
690** value.
691*/
692static void heightOfExpr(Expr *p, int *pnHeight){
693 if( p ){
694 if( p->nHeight>*pnHeight ){
695 *pnHeight = p->nHeight;
696 }
697 }
698}
699static void heightOfExprList(ExprList *p, int *pnHeight){
700 if( p ){
701 int i;
702 for(i=0; i<p->nExpr; i++){
703 heightOfExpr(p->a[i].pExpr, pnHeight);
704 }
705 }
706}
707static void heightOfSelect(Select *p, int *pnHeight){
708 if( p ){
709 heightOfExpr(p->pWhere, pnHeight);
710 heightOfExpr(p->pHaving, pnHeight);
711 heightOfExpr(p->pLimit, pnHeight);
712 heightOfExpr(p->pOffset, pnHeight);
713 heightOfExprList(p->pEList, pnHeight);
714 heightOfExprList(p->pGroupBy, pnHeight);
715 heightOfExprList(p->pOrderBy, pnHeight);
716 heightOfSelect(p->pPrior, pnHeight);
717 }
718}
719
720/*
721** Set the Expr.nHeight variable in the structure passed as an
722** argument. An expression with no children, Expr.pList or
723** Expr.pSelect member has a height of 1. Any other expression
724** has a height equal to the maximum height of any other
725** referenced Expr plus one.
726*/
727void sqlite3ExprSetHeight(Expr *p){
728 int nHeight = 0;
729 heightOfExpr(p->pLeft, &nHeight);
730 heightOfExpr(p->pRight, &nHeight);
731 heightOfExprList(p->pList, &nHeight);
732 heightOfSelect(p->pSelect, &nHeight);
733 p->nHeight = nHeight + 1;
734}
735
736/*
737** Return the maximum height of any expression tree referenced
738** by the select statement passed as an argument.
739*/
740int sqlite3SelectExprHeight(Select *p){
741 int nHeight = 0;
742 heightOfSelect(p, &nHeight);
743 return nHeight;
744}
745#endif
746
danielk19777a15a4b2007-05-08 17:54:43 +0000747/*
drha76b5df2002-02-23 02:32:10 +0000748** Delete an entire expression list.
749*/
danielk19774adee202004-05-08 08:23:19 +0000750void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000751 int i;
drhbe5c89a2004-07-26 00:31:09 +0000752 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000753 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000754 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
755 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000756 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
757 sqlite3ExprDelete(pItem->pExpr);
758 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000759 }
760 sqliteFree(pList->a);
761 sqliteFree(pList);
762}
763
764/*
drh626a8792005-01-17 22:08:19 +0000765** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000766**
drh626a8792005-01-17 22:08:19 +0000767** The return value from xFunc determines whether the tree walk continues.
768** 0 means continue walking the tree. 1 means do not walk children
769** of the current node but continue with siblings. 2 means abandon
770** the tree walk completely.
771**
772** The return value from this routine is 1 to abandon the tree walk
773** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000774**
775** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000776*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000777static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000778static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000779 int rc;
780 if( pExpr==0 ) return 0;
781 rc = (*xFunc)(pArg, pExpr);
782 if( rc==0 ){
783 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
784 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000785 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000786 }
787 return rc>1;
788}
789
790/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000791** Call walkExprTree() for every expression in list p.
792*/
793static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
794 int i;
795 struct ExprList_item *pItem;
796 if( !p ) return 0;
797 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
798 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
799 }
800 return 0;
801}
802
803/*
804** Call walkExprTree() for every expression in Select p, not including
805** expressions that are part of sub-selects in any FROM clause or the LIMIT
806** or OFFSET expressions..
807*/
808static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
809 walkExprList(p->pEList, xFunc, pArg);
810 walkExprTree(p->pWhere, xFunc, pArg);
811 walkExprList(p->pGroupBy, xFunc, pArg);
812 walkExprTree(p->pHaving, xFunc, pArg);
813 walkExprList(p->pOrderBy, xFunc, pArg);
814 return 0;
815}
816
817
818/*
drh626a8792005-01-17 22:08:19 +0000819** This routine is designed as an xFunc for walkExprTree().
820**
821** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000822** at pExpr that the expression that contains pExpr is not a constant
823** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
824** If pExpr does does not disqualify the expression from being a constant
825** then do nothing.
826**
827** After walking the whole tree, if no nodes are found that disqualify
828** the expression as constant, then we assume the whole expression
829** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000830*/
831static int exprNodeIsConstant(void *pArg, Expr *pExpr){
832 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000833 /* Consider functions to be constant if all their arguments are constant
834 ** and *pArg==2 */
835 case TK_FUNCTION:
836 if( *((int*)pArg)==2 ) return 0;
837 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000838 case TK_ID:
839 case TK_COLUMN:
840 case TK_DOT:
841 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000842 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000843#ifndef SQLITE_OMIT_SUBQUERY
844 case TK_SELECT:
845 case TK_EXISTS:
846#endif
drh626a8792005-01-17 22:08:19 +0000847 *((int*)pArg) = 0;
848 return 2;
drh87abf5c2005-08-25 12:45:04 +0000849 case TK_IN:
850 if( pExpr->pSelect ){
851 *((int*)pArg) = 0;
852 return 2;
853 }
drh626a8792005-01-17 22:08:19 +0000854 default:
855 return 0;
856 }
857}
858
859/*
drhfef52082000-06-06 01:50:43 +0000860** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000861** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000862**
863** For the purposes of this function, a double-quoted string (ex: "abc")
864** is considered a variable but a single-quoted string (ex: 'abc') is
865** a constant.
drhfef52082000-06-06 01:50:43 +0000866*/
danielk19774adee202004-05-08 08:23:19 +0000867int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000868 int isConst = 1;
869 walkExprTree(p, exprNodeIsConstant, &isConst);
870 return isConst;
drhfef52082000-06-06 01:50:43 +0000871}
872
873/*
drheb55bd22005-06-30 17:04:21 +0000874** Walk an expression tree. Return 1 if the expression is constant
875** or a function call with constant arguments. Return and 0 if there
876** are any variables.
877**
878** For the purposes of this function, a double-quoted string (ex: "abc")
879** is considered a variable but a single-quoted string (ex: 'abc') is
880** a constant.
881*/
882int sqlite3ExprIsConstantOrFunction(Expr *p){
883 int isConst = 2;
884 walkExprTree(p, exprNodeIsConstant, &isConst);
885 return isConst!=0;
886}
887
888/*
drh73b211a2005-01-18 04:00:42 +0000889** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000890** to fit in a 32-bit integer, return 1 and put the value of the integer
891** in *pValue. If the expression is not an integer or if it is too big
892** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000893*/
danielk19774adee202004-05-08 08:23:19 +0000894int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000895 switch( p->op ){
896 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000897 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000898 return 1;
899 }
900 break;
drhe4de1fe2002-06-02 16:09:01 +0000901 }
drh4b59ab52002-08-24 18:24:51 +0000902 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000903 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000904 }
drhe4de1fe2002-06-02 16:09:01 +0000905 case TK_UMINUS: {
906 int v;
danielk19774adee202004-05-08 08:23:19 +0000907 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000908 *pValue = -v;
909 return 1;
910 }
911 break;
912 }
913 default: break;
914 }
915 return 0;
916}
917
918/*
drhc4a3c772001-04-04 11:48:57 +0000919** Return TRUE if the given string is a row-id column name.
920*/
danielk19774adee202004-05-08 08:23:19 +0000921int sqlite3IsRowid(const char *z){
922 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
923 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
924 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000925 return 0;
926}
927
928/*
drh8141f612004-01-25 22:44:58 +0000929** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
930** that name in the set of source tables in pSrcList and make the pExpr
931** expression node refer back to that source column. The following changes
932** are made to pExpr:
933**
934** pExpr->iDb Set the index in db->aDb[] of the database holding
935** the table.
936** pExpr->iTable Set to the cursor number for the table obtained
937** from pSrcList.
938** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000939** pExpr->op Set to TK_COLUMN.
940** pExpr->pLeft Any expression this points to is deleted
941** pExpr->pRight Any expression this points to is deleted.
942**
943** The pDbToken is the name of the database (the "X"). This value may be
944** NULL meaning that name is of the form Y.Z or Z. Any available database
945** can be used. The pTableToken is the name of the table (the "Y"). This
946** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
947** means that the form of the name is Z and that columns from any table
948** can be used.
949**
950** If the name cannot be resolved unambiguously, leave an error message
951** in pParse and return non-zero. Return zero on success.
952*/
953static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000954 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000955 Token *pDbToken, /* Name of the database containing table, or NULL */
956 Token *pTableToken, /* Name of table containing column, or NULL */
957 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000958 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000959 Expr *pExpr /* Make this EXPR node point to the selected column */
960){
961 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
962 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
963 char *zCol = 0; /* Name of the column. The "Z" */
964 int i, j; /* Loop counters */
965 int cnt = 0; /* Number of matching column names */
966 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000967 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000968 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
969 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000970 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000971
972 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000973 zDb = sqlite3NameFromToken(pDbToken);
974 zTab = sqlite3NameFromToken(pTableToken);
975 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000976 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000977 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000978 }
drh8141f612004-01-25 22:44:58 +0000979
980 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000981 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000982 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000983 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000984
danielk1977b3bce662005-01-29 08:32:43 +0000985 if( pSrcList ){
986 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000987 Table *pTab;
988 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000989 Column *pCol;
990
drh43617e92006-03-06 20:55:46 +0000991 pTab = pItem->pTab;
992 assert( pTab!=0 );
993 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000994 assert( pTab->nCol>0 );
995 if( zTab ){
996 if( pItem->zAlias ){
997 char *zTabName = pItem->zAlias;
998 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
999 }else{
1000 char *zTabName = pTab->zName;
1001 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001002 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001003 continue;
1004 }
drh626a8792005-01-17 22:08:19 +00001005 }
drh8141f612004-01-25 22:44:58 +00001006 }
danielk1977b3bce662005-01-29 08:32:43 +00001007 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001008 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001009 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001010 pMatch = pItem;
1011 }
1012 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1013 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001014 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001015 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001016 cnt++;
1017 pExpr->iTable = pItem->iCursor;
1018 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001019 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001020 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1021 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1022 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001023 if( (pExpr->flags & EP_ExpCollate)==0 ){
1024 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1025 }
drh61dfc312006-12-16 16:25:15 +00001026 if( i<pSrcList->nSrc-1 ){
1027 if( pItem[1].jointype & JT_NATURAL ){
1028 /* If this match occurred in the left table of a natural join,
1029 ** then skip the right table to avoid a duplicate match */
1030 pItem++;
1031 i++;
1032 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1033 /* If this match occurs on a column that is in the USING clause
1034 ** of a join, skip the search of the right table of the join
1035 ** to avoid a duplicate match there. */
1036 int k;
1037 for(k=0; k<pUsing->nId; k++){
1038 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1039 pItem++;
1040 i++;
1041 break;
1042 }
drh873fac02005-06-06 17:11:46 +00001043 }
1044 }
1045 }
danielk1977b3bce662005-01-29 08:32:43 +00001046 break;
1047 }
drh8141f612004-01-25 22:44:58 +00001048 }
1049 }
1050 }
drh626a8792005-01-17 22:08:19 +00001051
1052#ifndef SQLITE_OMIT_TRIGGER
1053 /* If we have not already resolved the name, then maybe
1054 ** it is a new.* or old.* trigger argument reference
1055 */
1056 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1057 TriggerStack *pTriggerStack = pParse->trigStack;
1058 Table *pTab = 0;
1059 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1060 pExpr->iTable = pTriggerStack->newIdx;
1061 assert( pTriggerStack->pTab );
1062 pTab = pTriggerStack->pTab;
1063 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1064 pExpr->iTable = pTriggerStack->oldIdx;
1065 assert( pTriggerStack->pTab );
1066 pTab = pTriggerStack->pTab;
1067 }
1068
1069 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001070 int iCol;
drh626a8792005-01-17 22:08:19 +00001071 Column *pCol = pTab->aCol;
1072
danielk1977da184232006-01-05 11:34:32 +00001073 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001074 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001075 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001076 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001077 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001078 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001079 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1080 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001081 if( (pExpr->flags & EP_ExpCollate)==0 ){
1082 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1083 }
danielk1977aee18ef2005-03-09 12:26:50 +00001084 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001085 break;
1086 }
1087 }
1088 }
1089 }
drhb7f91642004-10-31 02:22:47 +00001090#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001091
drh626a8792005-01-17 22:08:19 +00001092 /*
1093 ** Perhaps the name is a reference to the ROWID
1094 */
1095 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1096 cnt = 1;
1097 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001098 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001099 }
drh8141f612004-01-25 22:44:58 +00001100
drh626a8792005-01-17 22:08:19 +00001101 /*
1102 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1103 ** might refer to an result-set alias. This happens, for example, when
1104 ** we are resolving names in the WHERE clause of the following command:
1105 **
1106 ** SELECT a+b AS x FROM table WHERE x<10;
1107 **
1108 ** In cases like this, replace pExpr with a copy of the expression that
1109 ** forms the result set entry ("a+b" in the example) and return immediately.
1110 ** Note that the expression in the result set should have already been
1111 ** resolved by the time the WHERE clause is resolved.
1112 */
drhffe07b22005-11-03 00:41:17 +00001113 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001114 for(j=0; j<pEList->nExpr; j++){
1115 char *zAs = pEList->a[j].zName;
1116 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1117 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1118 pExpr->op = TK_AS;
1119 pExpr->iColumn = j;
1120 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001121 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001122 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001123 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001124 }
1125 }
1126 }
1127
1128 /* Advance to the next name context. The loop will exit when either
1129 ** we have a match (cnt>0) or when we run out of name contexts.
1130 */
1131 if( cnt==0 ){
1132 pNC = pNC->pNext;
1133 }
drh8141f612004-01-25 22:44:58 +00001134 }
1135
1136 /*
1137 ** If X and Y are NULL (in other words if only the column name Z is
1138 ** supplied) and the value of Z is enclosed in double-quotes, then
1139 ** Z is a string literal if it doesn't match any column names. In that
1140 ** case, we need to return right away and not make any changes to
1141 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001142 **
1143 ** Because no reference was made to outer contexts, the pNC->nRef
1144 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001145 */
1146 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1147 sqliteFree(zCol);
1148 return 0;
1149 }
1150
1151 /*
1152 ** cnt==0 means there was not match. cnt>1 means there were two or
1153 ** more matches. Either way, we have an error.
1154 */
1155 if( cnt!=1 ){
1156 char *z = 0;
1157 char *zErr;
1158 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1159 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001160 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001161 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001162 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001163 }else{
1164 z = sqliteStrDup(zCol);
1165 }
danielk19774adee202004-05-08 08:23:19 +00001166 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001167 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001168 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001169 }
1170
drh51669862004-12-18 18:40:26 +00001171 /* If a column from a table in pSrcList is referenced, then record
1172 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1173 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1174 ** column number is greater than the number of bits in the bitmask
1175 ** then set the high-order bit of the bitmask.
1176 */
1177 if( pExpr->iColumn>=0 && pMatch!=0 ){
1178 int n = pExpr->iColumn;
1179 if( n>=sizeof(Bitmask)*8 ){
1180 n = sizeof(Bitmask)*8-1;
1181 }
1182 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001183 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001184 }
1185
danielk1977d5d56522005-03-16 12:15:20 +00001186lookupname_end:
drh8141f612004-01-25 22:44:58 +00001187 /* Clean up and return
1188 */
1189 sqliteFree(zDb);
1190 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001191 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001192 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001193 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001194 pExpr->pRight = 0;
1195 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001196lookupname_end_2:
1197 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001198 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001199 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001200 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001201 if( pMatch && !pMatch->pSelect ){
1202 pExpr->pTab = pMatch->pTab;
1203 }
drh15ccce12005-05-23 15:06:39 +00001204 /* Increment the nRef value on all name contexts from TopNC up to
1205 ** the point where the name matched. */
1206 for(;;){
1207 assert( pTopNC!=0 );
1208 pTopNC->nRef++;
1209 if( pTopNC==pNC ) break;
1210 pTopNC = pTopNC->pNext;
1211 }
1212 return 0;
1213 } else {
1214 return 1;
drh626a8792005-01-17 22:08:19 +00001215 }
drh8141f612004-01-25 22:44:58 +00001216}
1217
1218/*
drh626a8792005-01-17 22:08:19 +00001219** This routine is designed as an xFunc for walkExprTree().
1220**
drh73b211a2005-01-18 04:00:42 +00001221** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001222** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001223** the tree or 2 to abort the tree walk.
1224**
1225** This routine also does error checking and name resolution for
1226** function names. The operator for aggregate functions is changed
1227** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001228*/
1229static int nameResolverStep(void *pArg, Expr *pExpr){
1230 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001231 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001232
danielk1977b3bce662005-01-29 08:32:43 +00001233 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001234 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001235 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001236
drh626a8792005-01-17 22:08:19 +00001237 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1238 ExprSetProperty(pExpr, EP_Resolved);
1239#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001240 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1241 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001242 int i;
danielk1977f0113002006-01-24 12:09:17 +00001243 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001244 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1245 }
1246 }
1247#endif
1248 switch( pExpr->op ){
1249 /* Double-quoted strings (ex: "abc") are used as identifiers if
1250 ** possible. Otherwise they remain as strings. Single-quoted
1251 ** strings (ex: 'abc') are always string literals.
1252 */
1253 case TK_STRING: {
1254 if( pExpr->token.z[0]=='\'' ) break;
1255 /* Fall thru into the TK_ID case if this is a double-quoted string */
1256 }
1257 /* A lone identifier is the name of a column.
1258 */
1259 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001260 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1261 return 1;
1262 }
1263
1264 /* A table name and column name: ID.ID
1265 ** Or a database, table and column: ID.ID.ID
1266 */
1267 case TK_DOT: {
1268 Token *pColumn;
1269 Token *pTable;
1270 Token *pDb;
1271 Expr *pRight;
1272
danielk1977b3bce662005-01-29 08:32:43 +00001273 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001274 pRight = pExpr->pRight;
1275 if( pRight->op==TK_ID ){
1276 pDb = 0;
1277 pTable = &pExpr->pLeft->token;
1278 pColumn = &pRight->token;
1279 }else{
1280 assert( pRight->op==TK_DOT );
1281 pDb = &pExpr->pLeft->token;
1282 pTable = &pRight->pLeft->token;
1283 pColumn = &pRight->pRight->token;
1284 }
1285 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1286 return 1;
1287 }
1288
1289 /* Resolve function names
1290 */
drhb71090f2005-05-23 17:26:51 +00001291 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001292 case TK_FUNCTION: {
1293 ExprList *pList = pExpr->pList; /* The argument list */
1294 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1295 int no_such_func = 0; /* True if no such function exists */
1296 int wrong_num_args = 0; /* True if wrong number of arguments */
1297 int is_agg = 0; /* True if is an aggregate function */
1298 int i;
drh5169bbc2006-08-24 14:59:45 +00001299 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001300 int nId; /* Number of characters in function name */
1301 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001302 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001303 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001304
drh2646da72005-12-09 20:02:05 +00001305 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001306 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001307 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1308 if( pDef==0 ){
1309 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1310 if( pDef==0 ){
1311 no_such_func = 1;
1312 }else{
1313 wrong_num_args = 1;
1314 }
1315 }else{
1316 is_agg = pDef->xFunc==0;
1317 }
drh2fca7fe2006-11-23 11:59:13 +00001318#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001319 if( pDef ){
1320 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1321 if( auth!=SQLITE_OK ){
1322 if( auth==SQLITE_DENY ){
1323 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1324 pDef->zName);
1325 pNC->nErr++;
1326 }
1327 pExpr->op = TK_NULL;
1328 return 1;
1329 }
1330 }
drhb8b14212006-08-24 15:18:25 +00001331#endif
drh626a8792005-01-17 22:08:19 +00001332 if( is_agg && !pNC->allowAgg ){
1333 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1334 pNC->nErr++;
1335 is_agg = 0;
1336 }else if( no_such_func ){
1337 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1338 pNC->nErr++;
1339 }else if( wrong_num_args ){
1340 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1341 nId, zId);
1342 pNC->nErr++;
1343 }
1344 if( is_agg ){
1345 pExpr->op = TK_AGG_FUNCTION;
1346 pNC->hasAgg = 1;
1347 }
drh73b211a2005-01-18 04:00:42 +00001348 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001349 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001350 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001351 }
drh73b211a2005-01-18 04:00:42 +00001352 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001353 /* FIX ME: Compute pExpr->affinity based on the expected return
1354 ** type of the function
1355 */
1356 return is_agg;
1357 }
danielk1977b3bce662005-01-29 08:32:43 +00001358#ifndef SQLITE_OMIT_SUBQUERY
1359 case TK_SELECT:
1360 case TK_EXISTS:
1361#endif
1362 case TK_IN: {
1363 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001364 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001365#ifndef SQLITE_OMIT_CHECK
1366 if( pNC->isCheck ){
1367 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1368 }
1369#endif
danielk1977b3bce662005-01-29 08:32:43 +00001370 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1371 assert( pNC->nRef>=nRef );
1372 if( nRef!=pNC->nRef ){
1373 ExprSetProperty(pExpr, EP_VarSelect);
1374 }
1375 }
drh4284fb02005-11-03 12:33:28 +00001376 break;
danielk1977b3bce662005-01-29 08:32:43 +00001377 }
drh4284fb02005-11-03 12:33:28 +00001378#ifndef SQLITE_OMIT_CHECK
1379 case TK_VARIABLE: {
1380 if( pNC->isCheck ){
1381 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1382 }
1383 break;
1384 }
1385#endif
drh626a8792005-01-17 22:08:19 +00001386 }
1387 return 0;
1388}
1389
1390/*
drhcce7d172000-05-31 15:34:51 +00001391** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001392** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001393** index to the table in the table list and a column offset. The
1394** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1395** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001396** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001397** VDBE cursor number for a cursor that is pointing into the referenced
1398** table. The Expr.iColumn value is changed to the index of the column
1399** of the referenced table. The Expr.iColumn value for the special
1400** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1401** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001402**
drh626a8792005-01-17 22:08:19 +00001403** Also resolve function names and check the functions for proper
1404** usage. Make sure all function names are recognized and all functions
1405** have the correct number of arguments. Leave an error message
1406** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1407**
drh73b211a2005-01-18 04:00:42 +00001408** If the expression contains aggregate functions then set the EP_Agg
1409** property on the expression.
drh626a8792005-01-17 22:08:19 +00001410*/
danielk1977fc976062007-05-10 10:46:56 +00001411int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001412 NameContext *pNC, /* Namespace to resolve expressions in. */
1413 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001414){
drh13449892005-09-07 21:22:45 +00001415 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001416 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001417#if SQLITE_MAX_EXPR_DEPTH>0
1418 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1419 sqlite3ErrorMsg(pNC->pParse,
1420 "Expression tree is too large (maximum depth %d)",
1421 SQLITE_MAX_EXPR_DEPTH
1422 );
1423 return 1;
1424 }
1425 pNC->pParse->nHeight += pExpr->nHeight;
1426#endif
drh13449892005-09-07 21:22:45 +00001427 savedHasAgg = pNC->hasAgg;
1428 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001429 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001430#if SQLITE_MAX_EXPR_DEPTH>0
1431 pNC->pParse->nHeight -= pExpr->nHeight;
1432#endif
danielk1977b3bce662005-01-29 08:32:43 +00001433 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001434 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001435 }
drh13449892005-09-07 21:22:45 +00001436 if( pNC->hasAgg ){
1437 ExprSetProperty(pExpr, EP_Agg);
1438 }else if( savedHasAgg ){
1439 pNC->hasAgg = 1;
1440 }
drh73b211a2005-01-18 04:00:42 +00001441 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001442}
1443
drh1398ad32005-01-19 23:24:50 +00001444/*
1445** A pointer instance of this structure is used to pass information
1446** through walkExprTree into codeSubqueryStep().
1447*/
1448typedef struct QueryCoder QueryCoder;
1449struct QueryCoder {
1450 Parse *pParse; /* The parsing context */
1451 NameContext *pNC; /* Namespace of first enclosing query */
1452};
1453
drh626a8792005-01-17 22:08:19 +00001454
1455/*
drh9cbe6352005-11-29 03:13:21 +00001456** Generate code for scalar subqueries used as an expression
1457** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001458**
drh9cbe6352005-11-29 03:13:21 +00001459** (SELECT a FROM b) -- subquery
1460** EXISTS (SELECT a FROM b) -- EXISTS subquery
1461** x IN (4,5,11) -- IN operator with list on right-hand side
1462** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001463**
drh9cbe6352005-11-29 03:13:21 +00001464** The pExpr parameter describes the expression that contains the IN
1465** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001466*/
drh51522cd2005-01-20 13:36:19 +00001467#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001468void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001469 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001470 Vdbe *v = sqlite3GetVdbe(pParse);
1471 if( v==0 ) return;
1472
danielk1977fc976062007-05-10 10:46:56 +00001473
drh57dbd7b2005-07-08 18:25:26 +00001474 /* This code must be run in its entirety every time it is encountered
1475 ** if any of the following is true:
1476 **
1477 ** * The right-hand side is a correlated subquery
1478 ** * The right-hand side is an expression list containing variables
1479 ** * We are inside a trigger
1480 **
1481 ** If all of the above are false, then we can run this code just once
1482 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001483 */
1484 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1485 int mem = pParse->nMem++;
1486 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001487 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001488 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001489 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001490 }
1491
drhcce7d172000-05-31 15:34:51 +00001492 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001493 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001494 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001495 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001496 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001497
danielk1977bf3b7212004-05-18 10:06:24 +00001498 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001499
1500 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001501 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001502 ** filled with single-field index keys representing the results
1503 ** from the SELECT or the <exprlist>.
1504 **
1505 ** If the 'x' expression is a column value, or the SELECT...
1506 ** statement returns a column value, then the affinity of that
1507 ** column is used to build the index keys. If both 'x' and the
1508 ** SELECT... statement are columns, then numeric affinity is used
1509 ** if either column has NUMERIC or INTEGER affinity. If neither
1510 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1511 ** is used.
1512 */
1513 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001514 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001515 memset(&keyInfo, 0, sizeof(keyInfo));
1516 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001517 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001518
drhfef52082000-06-06 01:50:43 +00001519 if( pExpr->pSelect ){
1520 /* Case 1: expr IN (SELECT ...)
1521 **
danielk1977e014a832004-05-17 10:48:57 +00001522 ** Generate code to write the results of the select into the temporary
1523 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001524 */
danielk1977e014a832004-05-17 10:48:57 +00001525 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001526 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001527 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001528 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1529 return;
1530 }
drhbe5c89a2004-07-26 00:31:09 +00001531 pEList = pExpr->pSelect->pEList;
1532 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001533 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001534 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001535 }
drhfef52082000-06-06 01:50:43 +00001536 }else if( pExpr->pList ){
1537 /* Case 2: expr IN (exprlist)
1538 **
danielk1977e014a832004-05-17 10:48:57 +00001539 ** For each expression, build an index key from the evaluation and
1540 ** store it in the temporary table. If <expr> is a column, then use
1541 ** that columns affinity when building index keys. If <expr> is not
1542 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001543 */
danielk1977e014a832004-05-17 10:48:57 +00001544 int i;
drh57dbd7b2005-07-08 18:25:26 +00001545 ExprList *pList = pExpr->pList;
1546 struct ExprList_item *pItem;
1547
danielk1977e014a832004-05-17 10:48:57 +00001548 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001549 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001550 }
danielk19770202b292004-06-09 09:55:16 +00001551 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001552
1553 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001554 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1555 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001556
drh57dbd7b2005-07-08 18:25:26 +00001557 /* If the expression is not constant then we will need to
1558 ** disable the test that was generated above that makes sure
1559 ** this code only executes once. Because for a non-constant
1560 ** expression we need to rerun this code each time.
1561 */
drh6c30be82005-07-29 15:10:17 +00001562 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001563 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001564 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001565 }
danielk1977e014a832004-05-17 10:48:57 +00001566
1567 /* Evaluate the expression and insert it into the temp table */
1568 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001569 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001570 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001571 }
1572 }
danielk19770202b292004-06-09 09:55:16 +00001573 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001574 break;
drhfef52082000-06-06 01:50:43 +00001575 }
1576
drh51522cd2005-01-20 13:36:19 +00001577 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001578 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001579 /* This has to be a scalar SELECT. Generate code to put the
1580 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001581 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001582 */
drh2646da72005-12-09 20:02:05 +00001583 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001584 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001585 int iMem;
1586 int sop;
drh1398ad32005-01-19 23:24:50 +00001587
drhec7429a2005-10-06 16:53:14 +00001588 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001589 pSel = pExpr->pSelect;
1590 if( pExpr->op==TK_SELECT ){
1591 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001592 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1593 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001594 }else{
drh51522cd2005-01-20 13:36:19 +00001595 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001596 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1597 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001598 }
drhec7429a2005-10-06 16:53:14 +00001599 sqlite3ExprDelete(pSel->pLimit);
1600 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001601 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1602 return;
1603 }
danielk1977b3bce662005-01-29 08:32:43 +00001604 break;
drhcce7d172000-05-31 15:34:51 +00001605 }
1606 }
danielk1977b3bce662005-01-29 08:32:43 +00001607
drh57dbd7b2005-07-08 18:25:26 +00001608 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001609 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001610 }
danielk1977fc976062007-05-10 10:46:56 +00001611
danielk1977b3bce662005-01-29 08:32:43 +00001612 return;
drhcce7d172000-05-31 15:34:51 +00001613}
drh51522cd2005-01-20 13:36:19 +00001614#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001615
drhcce7d172000-05-31 15:34:51 +00001616/*
drhfec19aa2004-05-19 20:41:03 +00001617** Generate an instruction that will put the integer describe by
1618** text z[0..n-1] on the stack.
1619*/
1620static void codeInteger(Vdbe *v, const char *z, int n){
1621 int i;
drh6fec0762004-05-30 01:38:43 +00001622 if( sqlite3GetInt32(z, &i) ){
1623 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1624 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001625 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001626 }else{
1627 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1628 }
1629}
1630
drh945498f2007-02-24 11:52:52 +00001631
1632/*
1633** Generate code that will extract the iColumn-th column from
1634** table pTab and push that column value on the stack. There
1635** is an open cursor to pTab in iTable. If iColumn<0 then
1636** code is generated that extracts the rowid.
1637*/
1638void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1639 if( iColumn<0 ){
1640 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1641 sqlite3VdbeAddOp(v, op, iTable, 0);
1642 }else if( pTab==0 ){
1643 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1644 }else{
1645 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1646 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1647 sqlite3ColumnDefault(v, pTab, iColumn);
1648#ifndef SQLITE_OMIT_FLOATING_POINT
1649 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1650 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1651 }
1652#endif
1653 }
1654}
1655
drhfec19aa2004-05-19 20:41:03 +00001656/*
drhcce7d172000-05-31 15:34:51 +00001657** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001658** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001659**
1660** This code depends on the fact that certain token values (ex: TK_EQ)
1661** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1662** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1663** the make process cause these values to align. Assert()s in the code
1664** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001665*/
danielk19774adee202004-05-08 08:23:19 +00001666void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001667 Vdbe *v = pParse->pVdbe;
1668 int op;
drhffe07b22005-11-03 00:41:17 +00001669 int stackChng = 1; /* Amount of change to stack depth */
1670
danielk19777977a172004-11-09 12:44:37 +00001671 if( v==0 ) return;
1672 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001673 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001674 return;
1675 }
drhf2bc0132004-10-04 13:19:23 +00001676 op = pExpr->op;
1677 switch( op ){
drh13449892005-09-07 21:22:45 +00001678 case TK_AGG_COLUMN: {
1679 AggInfo *pAggInfo = pExpr->pAggInfo;
1680 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1681 if( !pAggInfo->directMode ){
1682 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1683 break;
1684 }else if( pAggInfo->useSortingIdx ){
1685 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1686 pCol->iSorterColumn);
1687 break;
1688 }
1689 /* Otherwise, fall thru into the TK_COLUMN case */
1690 }
drh967e8b72000-06-21 13:59:10 +00001691 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001692 if( pExpr->iTable<0 ){
1693 /* This only happens when coding check constraints */
1694 assert( pParse->ckOffset>0 );
1695 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001696 }else{
drh945498f2007-02-24 11:52:52 +00001697 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001698 }
drhcce7d172000-05-31 15:34:51 +00001699 break;
1700 }
1701 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001702 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001703 break;
1704 }
1705 case TK_FLOAT:
1706 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001707 assert( TK_FLOAT==OP_Real );
1708 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001709 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001710 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001711 break;
1712 }
drhf0863fe2005-06-12 21:35:51 +00001713 case TK_NULL: {
1714 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1715 break;
1716 }
danielk19775338a5f2005-01-20 13:03:10 +00001717#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001718 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001719 int n;
1720 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001721 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001722 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001723 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001724 assert( n>=0 );
1725 if( n==0 ){
1726 z = "";
1727 }
1728 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001729 break;
1730 }
danielk19775338a5f2005-01-20 13:03:10 +00001731#endif
drh50457892003-09-06 01:10:47 +00001732 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001733 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001734 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001735 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001736 }
drh50457892003-09-06 01:10:47 +00001737 break;
1738 }
drh4e0cff62004-11-05 05:10:28 +00001739 case TK_REGISTER: {
1740 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1741 break;
1742 }
drh487e2622005-06-25 18:42:14 +00001743#ifndef SQLITE_OMIT_CAST
1744 case TK_CAST: {
1745 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001746 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001747 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001748 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001749 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1750 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1751 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1752 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1753 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1754 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1755 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001756 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001757 break;
1758 }
1759#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001760 case TK_LT:
1761 case TK_LE:
1762 case TK_GT:
1763 case TK_GE:
1764 case TK_NE:
1765 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001766 assert( TK_LT==OP_Lt );
1767 assert( TK_LE==OP_Le );
1768 assert( TK_GT==OP_Gt );
1769 assert( TK_GE==OP_Ge );
1770 assert( TK_EQ==OP_Eq );
1771 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001772 sqlite3ExprCode(pParse, pExpr->pLeft);
1773 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001774 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001775 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001776 break;
drhc9b84a12002-06-20 11:36:48 +00001777 }
drhcce7d172000-05-31 15:34:51 +00001778 case TK_AND:
1779 case TK_OR:
1780 case TK_PLUS:
1781 case TK_STAR:
1782 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001783 case TK_REM:
1784 case TK_BITAND:
1785 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001786 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001787 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001788 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001789 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001790 assert( TK_AND==OP_And );
1791 assert( TK_OR==OP_Or );
1792 assert( TK_PLUS==OP_Add );
1793 assert( TK_MINUS==OP_Subtract );
1794 assert( TK_REM==OP_Remainder );
1795 assert( TK_BITAND==OP_BitAnd );
1796 assert( TK_BITOR==OP_BitOr );
1797 assert( TK_SLASH==OP_Divide );
1798 assert( TK_LSHIFT==OP_ShiftLeft );
1799 assert( TK_RSHIFT==OP_ShiftRight );
1800 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001801 sqlite3ExprCode(pParse, pExpr->pLeft);
1802 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001803 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001804 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001805 break;
1806 }
drhcce7d172000-05-31 15:34:51 +00001807 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001808 Expr *pLeft = pExpr->pLeft;
1809 assert( pLeft );
1810 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1811 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001812 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001813 if( pLeft->op==TK_FLOAT ){
1814 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001815 }else{
drhfec19aa2004-05-19 20:41:03 +00001816 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001817 }
drh6e142f52000-06-08 13:36:40 +00001818 sqliteFree(z);
1819 break;
1820 }
drh1ccde152000-06-17 13:12:39 +00001821 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001822 }
drhbf4133c2001-10-13 02:59:08 +00001823 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001824 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001825 assert( TK_BITNOT==OP_BitNot );
1826 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001827 sqlite3ExprCode(pParse, pExpr->pLeft);
1828 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001829 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001830 break;
1831 }
1832 case TK_ISNULL:
1833 case TK_NOTNULL: {
1834 int dest;
drhf2bc0132004-10-04 13:19:23 +00001835 assert( TK_ISNULL==OP_IsNull );
1836 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001837 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1838 sqlite3ExprCode(pParse, pExpr->pLeft);
1839 dest = sqlite3VdbeCurrentAddr(v) + 2;
1840 sqlite3VdbeAddOp(v, op, 1, dest);
1841 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001842 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001843 break;
drhcce7d172000-05-31 15:34:51 +00001844 }
drh22827922000-06-06 17:27:05 +00001845 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001846 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001847 if( pInfo==0 ){
1848 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1849 &pExpr->span);
1850 }else{
1851 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1852 }
drh22827922000-06-06 17:27:05 +00001853 break;
1854 }
drhb71090f2005-05-23 17:26:51 +00001855 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001856 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001857 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001858 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001859 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001860 int nId;
1861 const char *zId;
drh13449892005-09-07 21:22:45 +00001862 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001863 int i;
danielk197714db2662006-01-09 16:12:04 +00001864 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001865 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001866 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001867 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001868 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001869 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001870 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001871#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001872 /* Possibly overload the function if the first argument is
1873 ** a virtual table column.
1874 **
1875 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1876 ** second argument, not the first, as the argument to test to
1877 ** see if it is a column in a virtual table. This is done because
1878 ** the left operand of infix functions (the operand we want to
1879 ** control overloading) ends up as the second argument to the
1880 ** function. The expression "A glob B" is equivalent to
1881 ** "glob(B,A). We want to use the A in "A glob B" to test
1882 ** for function overloading. But we use the B term in "glob(B,A)".
1883 */
drh6a03a1c2006-07-08 18:34:59 +00001884 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001885 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1886 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001887 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1888 }
1889#endif
danielk1977682f68b2004-06-05 10:22:17 +00001890 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001891 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001892 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001893 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001894 if( pDef->needCollSeq && !pColl ){
1895 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1896 }
1897 }
1898 if( pDef->needCollSeq ){
1899 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001900 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001901 }
drh13449892005-09-07 21:22:45 +00001902 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001903 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001904 break;
1905 }
drhfe2093d2005-01-20 22:48:47 +00001906#ifndef SQLITE_OMIT_SUBQUERY
1907 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001908 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001909 if( pExpr->iColumn==0 ){
1910 sqlite3CodeSubselect(pParse, pExpr);
1911 }
danielk19774adee202004-05-08 08:23:19 +00001912 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001913 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001914 break;
1915 }
drhfef52082000-06-06 01:50:43 +00001916 case TK_IN: {
1917 int addr;
drh94a11212004-09-25 13:12:14 +00001918 char affinity;
drhafa5f682006-01-30 14:36:59 +00001919 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001920 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001921
1922 /* Figure out the affinity to use to create a key from the results
1923 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001924 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001925 */
drh94a11212004-09-25 13:12:14 +00001926 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001927
danielk19774adee202004-05-08 08:23:19 +00001928 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001929 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001930
1931 /* Code the <expr> from "<expr> IN (...)". The temporary table
1932 ** pExpr->iTable contains the values that make up the (...) set.
1933 */
danielk19774adee202004-05-08 08:23:19 +00001934 sqlite3ExprCode(pParse, pExpr->pLeft);
1935 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001936 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001937 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001938 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001939 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001940 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001941 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1942 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1943
drhfef52082000-06-06 01:50:43 +00001944 break;
1945 }
danielk197793758c82005-01-21 08:13:14 +00001946#endif
drhfef52082000-06-06 01:50:43 +00001947 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001948 Expr *pLeft = pExpr->pLeft;
1949 struct ExprList_item *pLItem = pExpr->pList->a;
1950 Expr *pRight = pLItem->pExpr;
1951 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001952 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001953 sqlite3ExprCode(pParse, pRight);
1954 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001955 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001956 pLItem++;
1957 pRight = pLItem->pExpr;
1958 sqlite3ExprCode(pParse, pRight);
1959 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001960 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001961 break;
1962 }
drh51e9a442004-01-16 16:42:53 +00001963 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001964 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001965 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001966 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001967 break;
1968 }
drh17a7f8d2002-03-24 13:13:27 +00001969 case TK_CASE: {
1970 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001971 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001972 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001973 int i;
drhbe5c89a2004-07-26 00:31:09 +00001974 ExprList *pEList;
1975 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001976
1977 assert(pExpr->pList);
1978 assert((pExpr->pList->nExpr % 2) == 0);
1979 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001980 pEList = pExpr->pList;
1981 aListelem = pEList->a;
1982 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001983 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001984 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001985 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001986 }
drhf5905aa2002-05-26 20:54:33 +00001987 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001988 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001989 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001990 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001991 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1992 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001993 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001994 }else{
danielk19774adee202004-05-08 08:23:19 +00001995 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001996 }
drhbe5c89a2004-07-26 00:31:09 +00001997 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001998 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001999 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002000 }
drhf570f012002-05-31 15:51:25 +00002001 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002002 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002003 }
drh17a7f8d2002-03-24 13:13:27 +00002004 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002005 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002006 }else{
drhf0863fe2005-06-12 21:35:51 +00002007 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002008 }
danielk19774adee202004-05-08 08:23:19 +00002009 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002010 break;
2011 }
danielk19775338a5f2005-01-20 13:03:10 +00002012#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002013 case TK_RAISE: {
2014 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002015 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002016 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002017 return;
2018 }
drhad6d9462004-09-19 02:15:24 +00002019 if( pExpr->iColumn!=OE_Ignore ){
2020 assert( pExpr->iColumn==OE_Rollback ||
2021 pExpr->iColumn == OE_Abort ||
2022 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002023 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002024 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002025 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002026 } else {
drhad6d9462004-09-19 02:15:24 +00002027 assert( pExpr->iColumn == OE_Ignore );
2028 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2029 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2030 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002031 }
drhffe07b22005-11-03 00:41:17 +00002032 stackChng = 0;
2033 break;
drh17a7f8d2002-03-24 13:13:27 +00002034 }
danielk19775338a5f2005-01-20 13:03:10 +00002035#endif
drhffe07b22005-11-03 00:41:17 +00002036 }
2037
2038 if( pParse->ckOffset ){
2039 pParse->ckOffset += stackChng;
2040 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002041 }
drhcce7d172000-05-31 15:34:51 +00002042}
2043
danielk197793758c82005-01-21 08:13:14 +00002044#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002045/*
drh25303782004-12-07 15:41:48 +00002046** Generate code that evalutes the given expression and leaves the result
2047** on the stack. See also sqlite3ExprCode().
2048**
2049** This routine might also cache the result and modify the pExpr tree
2050** so that it will make use of the cached result on subsequent evaluations
2051** rather than evaluate the whole expression again. Trivial expressions are
2052** not cached. If the expression is cached, its result is stored in a
2053** memory location.
2054*/
2055void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2056 Vdbe *v = pParse->pVdbe;
2057 int iMem;
2058 int addr1, addr2;
2059 if( v==0 ) return;
2060 addr1 = sqlite3VdbeCurrentAddr(v);
2061 sqlite3ExprCode(pParse, pExpr);
2062 addr2 = sqlite3VdbeCurrentAddr(v);
2063 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2064 iMem = pExpr->iTable = pParse->nMem++;
2065 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2066 pExpr->op = TK_REGISTER;
2067 }
2068}
danielk197793758c82005-01-21 08:13:14 +00002069#endif
drh25303782004-12-07 15:41:48 +00002070
2071/*
drh268380c2004-02-25 13:47:31 +00002072** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002073** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002074**
2075** Return the number of elements pushed onto the stack.
2076*/
danielk19774adee202004-05-08 08:23:19 +00002077int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002078 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002079 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002080){
2081 struct ExprList_item *pItem;
2082 int i, n;
drh268380c2004-02-25 13:47:31 +00002083 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002084 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002085 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002086 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002087 }
drhf9b596e2004-05-26 16:54:42 +00002088 return n;
drh268380c2004-02-25 13:47:31 +00002089}
2090
2091/*
drhcce7d172000-05-31 15:34:51 +00002092** Generate code for a boolean expression such that a jump is made
2093** to the label "dest" if the expression is true but execution
2094** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002095**
2096** If the expression evaluates to NULL (neither true nor false), then
2097** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002098**
2099** This code depends on the fact that certain token values (ex: TK_EQ)
2100** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2101** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2102** the make process cause these values to align. Assert()s in the code
2103** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002104*/
danielk19774adee202004-05-08 08:23:19 +00002105void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002106 Vdbe *v = pParse->pVdbe;
2107 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002108 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002109 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002110 op = pExpr->op;
2111 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002112 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002113 int d2 = sqlite3VdbeMakeLabel(v);
2114 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2115 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2116 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002117 break;
2118 }
2119 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002120 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2121 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002122 break;
2123 }
2124 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002125 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002126 break;
2127 }
2128 case TK_LT:
2129 case TK_LE:
2130 case TK_GT:
2131 case TK_GE:
2132 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002133 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002134 assert( TK_LT==OP_Lt );
2135 assert( TK_LE==OP_Le );
2136 assert( TK_GT==OP_Gt );
2137 assert( TK_GE==OP_Ge );
2138 assert( TK_EQ==OP_Eq );
2139 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002140 sqlite3ExprCode(pParse, pExpr->pLeft);
2141 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002142 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002143 break;
2144 }
2145 case TK_ISNULL:
2146 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002147 assert( TK_ISNULL==OP_IsNull );
2148 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002149 sqlite3ExprCode(pParse, pExpr->pLeft);
2150 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002151 break;
2152 }
drhfef52082000-06-06 01:50:43 +00002153 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002154 /* The expression "x BETWEEN y AND z" is implemented as:
2155 **
2156 ** 1 IF (x < y) GOTO 3
2157 ** 2 IF (x <= z) GOTO <dest>
2158 ** 3 ...
2159 */
drhf5905aa2002-05-26 20:54:33 +00002160 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002161 Expr *pLeft = pExpr->pLeft;
2162 Expr *pRight = pExpr->pList->a[0].pExpr;
2163 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002164 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002165 sqlite3ExprCode(pParse, pRight);
2166 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002167
drhbe5c89a2004-07-26 00:31:09 +00002168 pRight = pExpr->pList->a[1].pExpr;
2169 sqlite3ExprCode(pParse, pRight);
2170 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002171
danielk19774adee202004-05-08 08:23:19 +00002172 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002173 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002174 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002175 break;
2176 }
drhcce7d172000-05-31 15:34:51 +00002177 default: {
danielk19774adee202004-05-08 08:23:19 +00002178 sqlite3ExprCode(pParse, pExpr);
2179 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002180 break;
2181 }
2182 }
drhffe07b22005-11-03 00:41:17 +00002183 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002184}
2185
2186/*
drh66b89c82000-11-28 20:47:17 +00002187** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002188** to the label "dest" if the expression is false but execution
2189** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002190**
2191** If the expression evaluates to NULL (neither true nor false) then
2192** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002193*/
danielk19774adee202004-05-08 08:23:19 +00002194void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002195 Vdbe *v = pParse->pVdbe;
2196 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002197 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002198 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002199
2200 /* The value of pExpr->op and op are related as follows:
2201 **
2202 ** pExpr->op op
2203 ** --------- ----------
2204 ** TK_ISNULL OP_NotNull
2205 ** TK_NOTNULL OP_IsNull
2206 ** TK_NE OP_Eq
2207 ** TK_EQ OP_Ne
2208 ** TK_GT OP_Le
2209 ** TK_LE OP_Gt
2210 ** TK_GE OP_Lt
2211 ** TK_LT OP_Ge
2212 **
2213 ** For other values of pExpr->op, op is undefined and unused.
2214 ** The value of TK_ and OP_ constants are arranged such that we
2215 ** can compute the mapping above using the following expression.
2216 ** Assert()s verify that the computation is correct.
2217 */
2218 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2219
2220 /* Verify correct alignment of TK_ and OP_ constants
2221 */
2222 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2223 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2224 assert( pExpr->op!=TK_NE || op==OP_Eq );
2225 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2226 assert( pExpr->op!=TK_LT || op==OP_Ge );
2227 assert( pExpr->op!=TK_LE || op==OP_Gt );
2228 assert( pExpr->op!=TK_GT || op==OP_Le );
2229 assert( pExpr->op!=TK_GE || op==OP_Lt );
2230
drhcce7d172000-05-31 15:34:51 +00002231 switch( pExpr->op ){
2232 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002233 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2234 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002235 break;
2236 }
2237 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002238 int d2 = sqlite3VdbeMakeLabel(v);
2239 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2240 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2241 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002242 break;
2243 }
2244 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002245 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002246 break;
2247 }
2248 case TK_LT:
2249 case TK_LE:
2250 case TK_GT:
2251 case TK_GE:
2252 case TK_NE:
2253 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002254 sqlite3ExprCode(pParse, pExpr->pLeft);
2255 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002256 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002257 break;
2258 }
drhcce7d172000-05-31 15:34:51 +00002259 case TK_ISNULL:
2260 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002261 sqlite3ExprCode(pParse, pExpr->pLeft);
2262 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002263 break;
2264 }
drhfef52082000-06-06 01:50:43 +00002265 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002266 /* The expression is "x BETWEEN y AND z". It is implemented as:
2267 **
2268 ** 1 IF (x >= y) GOTO 3
2269 ** 2 GOTO <dest>
2270 ** 3 IF (x > z) GOTO <dest>
2271 */
drhfef52082000-06-06 01:50:43 +00002272 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002273 Expr *pLeft = pExpr->pLeft;
2274 Expr *pRight = pExpr->pList->a[0].pExpr;
2275 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002276 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002277 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002278 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002279 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2280
danielk19774adee202004-05-08 08:23:19 +00002281 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2282 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002283 pRight = pExpr->pList->a[1].pExpr;
2284 sqlite3ExprCode(pParse, pRight);
2285 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002286 break;
2287 }
drhcce7d172000-05-31 15:34:51 +00002288 default: {
danielk19774adee202004-05-08 08:23:19 +00002289 sqlite3ExprCode(pParse, pExpr);
2290 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002291 break;
2292 }
2293 }
drhffe07b22005-11-03 00:41:17 +00002294 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002295}
drh22827922000-06-06 17:27:05 +00002296
2297/*
2298** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2299** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002300**
2301** Sometimes this routine will return FALSE even if the two expressions
2302** really are equivalent. If we cannot prove that the expressions are
2303** identical, we return FALSE just to be safe. So if this routine
2304** returns false, then you do not really know for certain if the two
2305** expressions are the same. But if you get a TRUE return, then you
2306** can be sure the expressions are the same. In the places where
2307** this routine is used, it does not hurt to get an extra FALSE - that
2308** just might result in some slightly slower code. But returning
2309** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002310*/
danielk19774adee202004-05-08 08:23:19 +00002311int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002312 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002313 if( pA==0||pB==0 ){
2314 return pB==pA;
drh22827922000-06-06 17:27:05 +00002315 }
2316 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002317 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002318 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2319 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002320 if( pA->pList ){
2321 if( pB->pList==0 ) return 0;
2322 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2323 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002324 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002325 return 0;
2326 }
2327 }
2328 }else if( pB->pList ){
2329 return 0;
2330 }
2331 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002332 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002333 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002334 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002335 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002336 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2337 return 0;
2338 }
drh22827922000-06-06 17:27:05 +00002339 }
2340 return 1;
2341}
2342
drh13449892005-09-07 21:22:45 +00002343
drh22827922000-06-06 17:27:05 +00002344/*
drh13449892005-09-07 21:22:45 +00002345** Add a new element to the pAggInfo->aCol[] array. Return the index of
2346** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002347*/
drh13449892005-09-07 21:22:45 +00002348static int addAggInfoColumn(AggInfo *pInfo){
2349 int i;
drhcf643722007-03-27 13:36:37 +00002350 pInfo->aCol = sqlite3ArrayAllocate(
2351 pInfo->aCol,
2352 sizeof(pInfo->aCol[0]),
2353 3,
2354 &pInfo->nColumn,
2355 &pInfo->nColumnAlloc,
2356 &i
2357 );
drh13449892005-09-07 21:22:45 +00002358 return i;
2359}
2360
2361/*
2362** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2363** the new element. Return a negative number if malloc fails.
2364*/
2365static int addAggInfoFunc(AggInfo *pInfo){
2366 int i;
drhcf643722007-03-27 13:36:37 +00002367 pInfo->aFunc = sqlite3ArrayAllocate(
2368 pInfo->aFunc,
2369 sizeof(pInfo->aFunc[0]),
2370 3,
2371 &pInfo->nFunc,
2372 &pInfo->nFuncAlloc,
2373 &i
2374 );
drh13449892005-09-07 21:22:45 +00002375 return i;
2376}
drh22827922000-06-06 17:27:05 +00002377
2378/*
drh626a8792005-01-17 22:08:19 +00002379** This is an xFunc for walkExprTree() used to implement
2380** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2381** for additional information.
drh22827922000-06-06 17:27:05 +00002382**
drh626a8792005-01-17 22:08:19 +00002383** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002384*/
drh626a8792005-01-17 22:08:19 +00002385static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002386 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002387 NameContext *pNC = (NameContext *)pArg;
2388 Parse *pParse = pNC->pParse;
2389 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002390 AggInfo *pAggInfo = pNC->pAggInfo;
2391
drh22827922000-06-06 17:27:05 +00002392
drh22827922000-06-06 17:27:05 +00002393 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002394 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002395 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002396 /* Check to see if the column is in one of the tables in the FROM
2397 ** clause of the aggregate query */
2398 if( pSrcList ){
2399 struct SrcList_item *pItem = pSrcList->a;
2400 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2401 struct AggInfo_col *pCol;
2402 if( pExpr->iTable==pItem->iCursor ){
2403 /* If we reach this point, it means that pExpr refers to a table
2404 ** that is in the FROM clause of the aggregate query.
2405 **
2406 ** Make an entry for the column in pAggInfo->aCol[] if there
2407 ** is not an entry there already.
2408 */
drh7f906d62007-03-12 23:48:52 +00002409 int k;
drh13449892005-09-07 21:22:45 +00002410 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002411 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002412 if( pCol->iTable==pExpr->iTable &&
2413 pCol->iColumn==pExpr->iColumn ){
2414 break;
2415 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002416 }
drh7f906d62007-03-12 23:48:52 +00002417 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2418 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002419 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002420 pCol->iTable = pExpr->iTable;
2421 pCol->iColumn = pExpr->iColumn;
2422 pCol->iMem = pParse->nMem++;
2423 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002424 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002425 if( pAggInfo->pGroupBy ){
2426 int j, n;
2427 ExprList *pGB = pAggInfo->pGroupBy;
2428 struct ExprList_item *pTerm = pGB->a;
2429 n = pGB->nExpr;
2430 for(j=0; j<n; j++, pTerm++){
2431 Expr *pE = pTerm->pExpr;
2432 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2433 pE->iColumn==pExpr->iColumn ){
2434 pCol->iSorterColumn = j;
2435 break;
2436 }
2437 }
2438 }
2439 if( pCol->iSorterColumn<0 ){
2440 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2441 }
2442 }
2443 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2444 ** because it was there before or because we just created it).
2445 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2446 ** pAggInfo->aCol[] entry.
2447 */
2448 pExpr->pAggInfo = pAggInfo;
2449 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002450 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002451 break;
2452 } /* endif pExpr->iTable==pItem->iCursor */
2453 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002454 }
drh626a8792005-01-17 22:08:19 +00002455 return 1;
drh22827922000-06-06 17:27:05 +00002456 }
2457 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002458 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2459 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002460 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002461 /* Check to see if pExpr is a duplicate of another aggregate
2462 ** function that is already in the pAggInfo structure
2463 */
2464 struct AggInfo_func *pItem = pAggInfo->aFunc;
2465 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2466 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002467 break;
2468 }
drh22827922000-06-06 17:27:05 +00002469 }
drh13449892005-09-07 21:22:45 +00002470 if( i>=pAggInfo->nFunc ){
2471 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2472 */
danielk197714db2662006-01-09 16:12:04 +00002473 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002474 i = addAggInfoFunc(pAggInfo);
2475 if( i>=0 ){
2476 pItem = &pAggInfo->aFunc[i];
2477 pItem->pExpr = pExpr;
2478 pItem->iMem = pParse->nMem++;
2479 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002480 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002481 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002482 if( pExpr->flags & EP_Distinct ){
2483 pItem->iDistinct = pParse->nTab++;
2484 }else{
2485 pItem->iDistinct = -1;
2486 }
drh13449892005-09-07 21:22:45 +00002487 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002488 }
drh13449892005-09-07 21:22:45 +00002489 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2490 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002491 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002492 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002493 return 1;
drh22827922000-06-06 17:27:05 +00002494 }
drh22827922000-06-06 17:27:05 +00002495 }
2496 }
drh13449892005-09-07 21:22:45 +00002497
2498 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2499 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2500 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2501 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002502 if( pExpr->pSelect ){
2503 pNC->nDepth++;
2504 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2505 pNC->nDepth--;
2506 }
drh626a8792005-01-17 22:08:19 +00002507 return 0;
2508}
2509
2510/*
2511** Analyze the given expression looking for aggregate functions and
2512** for variables that need to be added to the pParse->aAgg[] array.
2513** Make additional entries to the pParse->aAgg[] array as necessary.
2514**
2515** This routine should only be called after the expression has been
2516** analyzed by sqlite3ExprResolveNames().
2517**
2518** If errors are seen, leave an error message in zErrMsg and return
2519** the number of errors.
2520*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002521int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2522 int nErr = pNC->pParse->nErr;
2523 walkExprTree(pExpr, analyzeAggregate, pNC);
2524 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002525}
drh5d9a4af2005-08-30 00:54:01 +00002526
2527/*
2528** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2529** expression list. Return the number of errors.
2530**
2531** If an error is found, the analysis is cut short.
2532*/
2533int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2534 struct ExprList_item *pItem;
2535 int i;
2536 int nErr = 0;
2537 if( pList ){
2538 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2539 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2540 }
2541 }
2542 return nErr;
2543}