blob: c4f395bf8c9bd932a9e9373c6e395489a1ba7920 [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**
danielk1977bcbb04e2007-05-29 12:11:29 +000015** $Id: expr.c,v 1.295 2007/05/29 12:11:30 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;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
57 CollSeq *pColl;
58 if( pExpr==0 ) return 0;
59 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
60 if( pColl ){
61 pExpr->pColl = pColl;
62 pExpr->flags |= EP_ExpCollate;
63 }
64 return pExpr;
65}
66
67/*
danielk19770202b292004-06-09 09:55:16 +000068** Return the default collation sequence for the expression pExpr. If
69** there is no default collation type, return 0.
70*/
danielk19777cedc8d2004-06-10 10:50:08 +000071CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
72 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000073 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000074 pColl = pExpr->pColl;
drh4f07e5f2007-05-14 11:34:46 +000075 if( pExpr->op==TK_CAST && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000076 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000077 }
78 }
danielk19777cedc8d2004-06-10 10:50:08 +000079 if( sqlite3CheckCollSeq(pParse, pColl) ){
80 pColl = 0;
81 }
82 return pColl;
danielk19770202b292004-06-09 09:55:16 +000083}
84
85/*
drh626a8792005-01-17 22:08:19 +000086** pExpr is an operand of a comparison operator. aff2 is the
87** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000088** type affinity that should be used for the comparison operator.
89*/
danielk1977e014a832004-05-17 10:48:57 +000090char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000091 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000092 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000093 /* Both sides of the comparison are columns. If one has numeric
94 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000095 */
drh8a512562005-11-14 22:29:05 +000096 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000097 return SQLITE_AFF_NUMERIC;
98 }else{
99 return SQLITE_AFF_NONE;
100 }
101 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000102 /* Neither side of the comparison is a column. Compare the
103 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000104 */
drh5f6a87b2004-07-19 00:39:45 +0000105 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000106 }else{
107 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000108 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000109 return (aff1 + aff2);
110 }
111}
112
drh53db1452004-05-20 13:54:53 +0000113/*
114** pExpr is a comparison operator. Return the type affinity that should
115** be applied to both operands prior to doing the comparison.
116*/
danielk1977e014a832004-05-17 10:48:57 +0000117static char comparisonAffinity(Expr *pExpr){
118 char aff;
119 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
120 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
121 pExpr->op==TK_NE );
122 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000123 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000124 if( pExpr->pRight ){
125 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
126 }
127 else if( pExpr->pSelect ){
128 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
129 }
130 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000131 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000132 }
133 return aff;
134}
135
136/*
137** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
138** idx_affinity is the affinity of an indexed column. Return true
139** if the index with affinity idx_affinity may be used to implement
140** the comparison in pExpr.
141*/
142int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
143 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000144 switch( aff ){
145 case SQLITE_AFF_NONE:
146 return 1;
147 case SQLITE_AFF_TEXT:
148 return idx_affinity==SQLITE_AFF_TEXT;
149 default:
150 return sqlite3IsNumericAffinity(idx_affinity);
151 }
danielk1977e014a832004-05-17 10:48:57 +0000152}
153
danielk1977a37cdde2004-05-16 11:15:36 +0000154/*
155** Return the P1 value that should be used for a binary comparison
156** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
157** If jumpIfNull is true, then set the low byte of the returned
158** P1 value to tell the opcode to jump if either expression
159** evaluates to NULL.
160*/
danielk1977e014a832004-05-17 10:48:57 +0000161static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000162 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000163 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000164}
165
drha2e00042002-01-22 03:13:42 +0000166/*
danielk19770202b292004-06-09 09:55:16 +0000167** Return a pointer to the collation sequence that should be used by
168** a binary comparison operator comparing pLeft and pRight.
169**
170** If the left hand expression has a collating sequence type, then it is
171** used. Otherwise the collation sequence for the right hand expression
172** is used, or the default (BINARY) if neither expression has a collating
173** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000174**
175** Argument pRight (but not pLeft) may be a null pointer. In this case,
176** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000177*/
danielk1977bcbb04e2007-05-29 12:11:29 +0000178CollSeq* sqlite3BinaryCompareCollSeq(
179 Parse *pParse,
180 Expr *pLeft,
181 Expr *pRight
182){
drhec41dda2007-02-07 13:09:45 +0000183 CollSeq *pColl;
184 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000185 if( pLeft->flags & EP_ExpCollate ){
186 assert( pLeft->pColl );
187 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000188 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000189 assert( pRight->pColl );
190 pColl = pRight->pColl;
191 }else{
192 pColl = sqlite3ExprCollSeq(pParse, pLeft);
193 if( !pColl ){
194 pColl = sqlite3ExprCollSeq(pParse, pRight);
195 }
danielk19770202b292004-06-09 09:55:16 +0000196 }
197 return pColl;
198}
199
200/*
drhbe5c89a2004-07-26 00:31:09 +0000201** Generate code for a comparison operator.
202*/
203static int codeCompare(
204 Parse *pParse, /* The parsing (and code generating) context */
205 Expr *pLeft, /* The left operand */
206 Expr *pRight, /* The right operand */
207 int opcode, /* The comparison opcode */
208 int dest, /* Jump here if true. */
209 int jumpIfNull /* If true, jump if either operand is NULL */
210){
211 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
danielk1977bcbb04e2007-05-29 12:11:29 +0000212 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000213 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000214}
215
216/*
drha76b5df2002-02-23 02:32:10 +0000217** Construct a new expression node and return a pointer to it. Memory
218** for this node is obtained from sqliteMalloc(). The calling function
219** is responsible for making sure the node eventually gets freed.
220*/
drhe4e72072004-11-23 01:47:30 +0000221Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000222 Expr *pNew;
223 pNew = sqliteMalloc( sizeof(Expr) );
224 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000225 /* When malloc fails, delete pLeft and pRight. Expressions passed to
226 ** this function must always be allocated with sqlite3Expr() for this
227 ** reason.
228 */
229 sqlite3ExprDelete(pLeft);
230 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000231 return 0;
232 }
233 pNew->op = op;
234 pNew->pLeft = pLeft;
235 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000236 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000237 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000238 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000239 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000240 }else if( pLeft ){
241 if( pRight ){
242 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000243 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000244 pNew->flags |= EP_ExpCollate;
245 pNew->pColl = pRight->pColl;
246 }
247 }
drh5ffb3ac2007-04-18 17:07:57 +0000248 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000249 pNew->flags |= EP_ExpCollate;
250 pNew->pColl = pLeft->pColl;
251 }
drha76b5df2002-02-23 02:32:10 +0000252 }
danielk1977fc976062007-05-10 10:46:56 +0000253
254 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000255 return pNew;
256}
257
258/*
drh206f3d92006-07-11 13:15:08 +0000259** Works like sqlite3Expr() but frees its pLeft and pRight arguments
260** if it fails due to a malloc problem.
261*/
262Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
263 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
264 if( pNew==0 ){
265 sqlite3ExprDelete(pLeft);
266 sqlite3ExprDelete(pRight);
267 }
268 return pNew;
269}
270
271/*
drh4e0cff62004-11-05 05:10:28 +0000272** When doing a nested parse, you can include terms in an expression
273** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000274** on the stack. "#0" means the top of the stack.
275** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000276**
277** This routine is called by the parser to deal with on of those terms.
278** It immediately generates code to store the value in a memory location.
279** The returns an expression that will code to extract the value from
280** that memory location as needed.
281*/
282Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
283 Vdbe *v = pParse->pVdbe;
284 Expr *p;
285 int depth;
drh4e0cff62004-11-05 05:10:28 +0000286 if( pParse->nested==0 ){
287 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drh4e05c832007-05-11 01:44:50 +0000288 return sqlite3Expr(TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000289 }
drhbb7ac002005-08-12 22:58:53 +0000290 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000291 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000292 if( p==0 ){
293 return 0; /* Malloc failed */
294 }
drh2646da72005-12-09 20:02:05 +0000295 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000296 p->iTable = pParse->nMem++;
297 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
298 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000299 return p;
300}
301
302/*
drh91bb0ee2004-09-01 03:06:34 +0000303** Join two expressions using an AND operator. If either expression is
304** NULL, then just return the other expression.
305*/
306Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
307 if( pLeft==0 ){
308 return pRight;
309 }else if( pRight==0 ){
310 return pLeft;
311 }else{
312 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
313 }
314}
315
316/*
drh6977fea2002-10-22 23:38:04 +0000317** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000318** text between the two given tokens.
319*/
danielk19774adee202004-05-08 08:23:19 +0000320void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000321 assert( pRight!=0 );
322 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000323 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000324 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000325 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000326 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000327 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000328 }else{
drh6977fea2002-10-22 23:38:04 +0000329 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000330 }
drha76b5df2002-02-23 02:32:10 +0000331 }
332}
333
334/*
335** Construct a new expression node for a function with multiple
336** arguments.
337*/
danielk19774adee202004-05-08 08:23:19 +0000338Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000339 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000340 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000341 pNew = sqliteMalloc( sizeof(Expr) );
342 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000343 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000344 return 0;
345 }
346 pNew->op = TK_FUNCTION;
347 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000348 assert( pToken->dyn==0 );
349 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000350 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000351
352 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000353 return pNew;
354}
355
356/*
drhfa6bc002004-09-07 16:19:52 +0000357** Assign a variable number to an expression that encodes a wildcard
358** in the original SQL statement.
359**
360** Wildcards consisting of a single "?" are assigned the next sequential
361** variable number.
362**
363** Wildcards of the form "?nnn" are assigned the number "nnn". We make
364** sure "nnn" is not too be to avoid a denial of service attack when
365** the SQL statement comes from an external source.
366**
367** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
368** as the previous instance of the same wildcard. Or if this is the first
369** instance of the wildcard, the next sequenial variable number is
370** assigned.
371*/
372void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
373 Token *pToken;
374 if( pExpr==0 ) return;
375 pToken = &pExpr->token;
376 assert( pToken->n>=1 );
377 assert( pToken->z!=0 );
378 assert( pToken->z[0]!=0 );
379 if( pToken->n==1 ){
380 /* Wildcard of the form "?". Assign the next variable number */
381 pExpr->iTable = ++pParse->nVar;
382 }else if( pToken->z[0]=='?' ){
383 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
384 ** use it as the variable number */
385 int i;
drh2646da72005-12-09 20:02:05 +0000386 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000387 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
388 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
389 SQLITE_MAX_VARIABLE_NUMBER);
390 }
391 if( i>pParse->nVar ){
392 pParse->nVar = i;
393 }
394 }else{
395 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
396 ** number as the prior appearance of the same name, or if the name
397 ** has never appeared before, reuse the same variable number
398 */
399 int i, n;
400 n = pToken->n;
401 for(i=0; i<pParse->nVarExpr; i++){
402 Expr *pE;
403 if( (pE = pParse->apVarExpr[i])!=0
404 && pE->token.n==n
405 && memcmp(pE->token.z, pToken->z, n)==0 ){
406 pExpr->iTable = pE->iTable;
407 break;
408 }
409 }
410 if( i>=pParse->nVarExpr ){
411 pExpr->iTable = ++pParse->nVar;
412 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
413 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000414 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000415 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
416 }
danielk19779e128002006-01-18 16:51:35 +0000417 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000418 assert( pParse->apVarExpr!=0 );
419 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
420 }
421 }
422 }
danielk1977832b2662007-05-09 11:37:22 +0000423 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
424 sqlite3ErrorMsg(pParse, "too many SQL variables");
425 }
drhfa6bc002004-09-07 16:19:52 +0000426}
427
428/*
drha2e00042002-01-22 03:13:42 +0000429** Recursively delete an expression tree.
430*/
danielk19774adee202004-05-08 08:23:19 +0000431void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000432 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000433 if( p->span.dyn ) sqliteFree((char*)p->span.z);
434 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000435 sqlite3ExprDelete(p->pLeft);
436 sqlite3ExprDelete(p->pRight);
437 sqlite3ExprListDelete(p->pList);
438 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000439 sqliteFree(p);
440}
441
drhd2687b72005-08-12 22:56:09 +0000442/*
443** The Expr.token field might be a string literal that is quoted.
444** If so, remove the quotation marks.
445*/
446void sqlite3DequoteExpr(Expr *p){
447 if( ExprHasAnyProperty(p, EP_Dequoted) ){
448 return;
449 }
450 ExprSetProperty(p, EP_Dequoted);
451 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000452 sqlite3TokenCopy(&p->token, &p->token);
453 }
454 sqlite3Dequote((char*)p->token.z);
455}
456
drha76b5df2002-02-23 02:32:10 +0000457
458/*
drhff78bd22002-02-27 01:47:11 +0000459** The following group of routines make deep copies of expressions,
460** expression lists, ID lists, and select statements. The copies can
461** be deleted (by being passed to their respective ...Delete() routines)
462** without effecting the originals.
463**
danielk19774adee202004-05-08 08:23:19 +0000464** The expression list, ID, and source lists return by sqlite3ExprListDup(),
465** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000466** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000467**
drhad3cab52002-05-24 02:04:32 +0000468** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000469*/
danielk19774adee202004-05-08 08:23:19 +0000470Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000471 Expr *pNew;
472 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000473 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000474 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000475 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000476 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000477 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000478 pNew->token.dyn = 1;
479 }else{
drh4efc4752004-01-16 15:55:37 +0000480 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000481 }
drh6977fea2002-10-22 23:38:04 +0000482 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000483 pNew->pLeft = sqlite3ExprDup(p->pLeft);
484 pNew->pRight = sqlite3ExprDup(p->pRight);
485 pNew->pList = sqlite3ExprListDup(p->pList);
486 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000487 return pNew;
488}
danielk19774adee202004-05-08 08:23:19 +0000489void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000490 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000491 if( pFrom->z ){
492 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000493 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000494 pTo->dyn = 1;
495 }else{
drh4b59ab52002-08-24 18:24:51 +0000496 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000497 }
498}
danielk19774adee202004-05-08 08:23:19 +0000499ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000500 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000501 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000502 int i;
503 if( p==0 ) return 0;
504 pNew = sqliteMalloc( sizeof(*pNew) );
505 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000506 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000507 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000508 if( pItem==0 ){
509 sqliteFree(pNew);
510 return 0;
511 }
drh145716b2004-09-24 12:24:06 +0000512 pOldItem = p->a;
513 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000514 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000515 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000516 if( pOldExpr->span.z!=0 && pNewExpr ){
517 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000518 ** expression list. The logic in SELECT processing that determines
519 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000520 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000521 }
drh1f3e9052002-10-31 00:09:39 +0000522 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000523 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000524 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000525 pItem->zName = sqliteStrDup(pOldItem->zName);
526 pItem->sortOrder = pOldItem->sortOrder;
527 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000528 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000529 }
530 return pNew;
531}
danielk197793758c82005-01-21 08:13:14 +0000532
533/*
534** If cursors, triggers, views and subqueries are all omitted from
535** the build, then none of the following routines, except for
536** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
537** called with a NULL argument.
538*/
danielk19776a67fe82005-02-04 04:07:16 +0000539#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
540 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000541SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000542 SrcList *pNew;
543 int i;
drh113088e2003-03-20 01:16:58 +0000544 int nByte;
drhad3cab52002-05-24 02:04:32 +0000545 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000546 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000547 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000548 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000549 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000550 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000551 struct SrcList_item *pNewItem = &pNew->a[i];
552 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000553 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000554 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
555 pNewItem->zName = sqliteStrDup(pOldItem->zName);
556 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
557 pNewItem->jointype = pOldItem->jointype;
558 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000559 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000560 pTab = pNewItem->pTab = pOldItem->pTab;
561 if( pTab ){
562 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000563 }
danielk19774adee202004-05-08 08:23:19 +0000564 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
565 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
566 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000567 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000568 }
569 return pNew;
570}
danielk19774adee202004-05-08 08:23:19 +0000571IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000572 IdList *pNew;
573 int i;
574 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000575 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000576 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000577 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000578 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000579 if( pNew->a==0 ){
580 sqliteFree(pNew);
581 return 0;
582 }
drhff78bd22002-02-27 01:47:11 +0000583 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000584 struct IdList_item *pNewItem = &pNew->a[i];
585 struct IdList_item *pOldItem = &p->a[i];
586 pNewItem->zName = sqliteStrDup(pOldItem->zName);
587 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000588 }
589 return pNew;
590}
danielk19774adee202004-05-08 08:23:19 +0000591Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000592 Select *pNew;
593 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000594 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000595 if( pNew==0 ) return 0;
596 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000597 pNew->pEList = sqlite3ExprListDup(p->pEList);
598 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
599 pNew->pWhere = sqlite3ExprDup(p->pWhere);
600 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
601 pNew->pHaving = sqlite3ExprDup(p->pHaving);
602 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000603 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000604 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000605 pNew->pLimit = sqlite3ExprDup(p->pLimit);
606 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000607 pNew->iLimit = -1;
608 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000609 pNew->isResolved = p->isResolved;
610 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000611 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000612 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000613 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000614 pNew->addrOpenEphm[0] = -1;
615 pNew->addrOpenEphm[1] = -1;
616 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000617 return pNew;
618}
danielk197793758c82005-01-21 08:13:14 +0000619#else
620Select *sqlite3SelectDup(Select *p){
621 assert( p==0 );
622 return 0;
623}
624#endif
drhff78bd22002-02-27 01:47:11 +0000625
626
627/*
drha76b5df2002-02-23 02:32:10 +0000628** Add a new element to the end of an expression list. If pList is
629** initially NULL, then create a new expression list.
630*/
danielk19774adee202004-05-08 08:23:19 +0000631ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000632 if( pList==0 ){
633 pList = sqliteMalloc( sizeof(ExprList) );
634 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000635 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000636 }
drh4efc4752004-01-16 15:55:37 +0000637 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000638 }
drh4305d102003-07-30 12:34:12 +0000639 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000640 struct ExprList_item *a;
641 int n = pList->nAlloc*2 + 4;
642 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
643 if( a==0 ){
644 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000645 }
danielk1977d5d56522005-03-16 12:15:20 +0000646 pList->a = a;
647 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000648 }
drh4efc4752004-01-16 15:55:37 +0000649 assert( pList->a!=0 );
650 if( pExpr || pName ){
651 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
652 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000653 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000654 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000655 }
656 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000657
658no_mem:
659 /* Avoid leaking memory if malloc has failed. */
660 sqlite3ExprDelete(pExpr);
661 sqlite3ExprListDelete(pList);
662 return 0;
drha76b5df2002-02-23 02:32:10 +0000663}
664
665/*
danielk19777a15a4b2007-05-08 17:54:43 +0000666** If the expression list pEList contains more than iLimit elements,
667** leave an error message in pParse.
668*/
669void sqlite3ExprListCheckLength(
670 Parse *pParse,
671 ExprList *pEList,
672 int iLimit,
673 const char *zObject
674){
danielk1977b4fc6792007-05-08 18:04:46 +0000675 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000676 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
677 }
678}
679
danielk1977fc976062007-05-10 10:46:56 +0000680
681#if SQLITE_MAX_EXPR_DEPTH>0
682/* The following three functions, heightOfExpr(), heightOfExprList()
683** and heightOfSelect(), are used to determine the maximum height
684** of any expression tree referenced by the structure passed as the
685** first argument.
686**
687** If this maximum height is greater than the current value pointed
688** to by pnHeight, the second parameter, then set *pnHeight to that
689** value.
690*/
691static void heightOfExpr(Expr *p, int *pnHeight){
692 if( p ){
693 if( p->nHeight>*pnHeight ){
694 *pnHeight = p->nHeight;
695 }
696 }
697}
698static void heightOfExprList(ExprList *p, int *pnHeight){
699 if( p ){
700 int i;
701 for(i=0; i<p->nExpr; i++){
702 heightOfExpr(p->a[i].pExpr, pnHeight);
703 }
704 }
705}
706static void heightOfSelect(Select *p, int *pnHeight){
707 if( p ){
708 heightOfExpr(p->pWhere, pnHeight);
709 heightOfExpr(p->pHaving, pnHeight);
710 heightOfExpr(p->pLimit, pnHeight);
711 heightOfExpr(p->pOffset, pnHeight);
712 heightOfExprList(p->pEList, pnHeight);
713 heightOfExprList(p->pGroupBy, pnHeight);
714 heightOfExprList(p->pOrderBy, pnHeight);
715 heightOfSelect(p->pPrior, pnHeight);
716 }
717}
718
719/*
720** Set the Expr.nHeight variable in the structure passed as an
721** argument. An expression with no children, Expr.pList or
722** Expr.pSelect member has a height of 1. Any other expression
723** has a height equal to the maximum height of any other
724** referenced Expr plus one.
725*/
726void sqlite3ExprSetHeight(Expr *p){
727 int nHeight = 0;
728 heightOfExpr(p->pLeft, &nHeight);
729 heightOfExpr(p->pRight, &nHeight);
730 heightOfExprList(p->pList, &nHeight);
731 heightOfSelect(p->pSelect, &nHeight);
732 p->nHeight = nHeight + 1;
733}
734
735/*
736** Return the maximum height of any expression tree referenced
737** by the select statement passed as an argument.
738*/
739int sqlite3SelectExprHeight(Select *p){
740 int nHeight = 0;
741 heightOfSelect(p, &nHeight);
742 return nHeight;
743}
744#endif
745
danielk19777a15a4b2007-05-08 17:54:43 +0000746/*
drha76b5df2002-02-23 02:32:10 +0000747** Delete an entire expression list.
748*/
danielk19774adee202004-05-08 08:23:19 +0000749void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000750 int i;
drhbe5c89a2004-07-26 00:31:09 +0000751 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000752 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000753 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
754 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000755 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
756 sqlite3ExprDelete(pItem->pExpr);
757 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000758 }
759 sqliteFree(pList->a);
760 sqliteFree(pList);
761}
762
763/*
drh626a8792005-01-17 22:08:19 +0000764** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000765**
drh626a8792005-01-17 22:08:19 +0000766** The return value from xFunc determines whether the tree walk continues.
767** 0 means continue walking the tree. 1 means do not walk children
768** of the current node but continue with siblings. 2 means abandon
769** the tree walk completely.
770**
771** The return value from this routine is 1 to abandon the tree walk
772** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000773**
774** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000775*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000776static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000777static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000778 int rc;
779 if( pExpr==0 ) return 0;
780 rc = (*xFunc)(pArg, pExpr);
781 if( rc==0 ){
782 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
783 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000784 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000785 }
786 return rc>1;
787}
788
789/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000790** Call walkExprTree() for every expression in list p.
791*/
792static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
793 int i;
794 struct ExprList_item *pItem;
795 if( !p ) return 0;
796 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
797 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
798 }
799 return 0;
800}
801
802/*
803** Call walkExprTree() for every expression in Select p, not including
804** expressions that are part of sub-selects in any FROM clause or the LIMIT
805** or OFFSET expressions..
806*/
807static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
808 walkExprList(p->pEList, xFunc, pArg);
809 walkExprTree(p->pWhere, xFunc, pArg);
810 walkExprList(p->pGroupBy, xFunc, pArg);
811 walkExprTree(p->pHaving, xFunc, pArg);
812 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000813 if( p->pPrior ){
814 walkSelectExpr(p->pPrior, xFunc, pArg);
815 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000816 return 0;
817}
818
819
820/*
drh626a8792005-01-17 22:08:19 +0000821** This routine is designed as an xFunc for walkExprTree().
822**
823** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000824** at pExpr that the expression that contains pExpr is not a constant
825** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
826** If pExpr does does not disqualify the expression from being a constant
827** then do nothing.
828**
829** After walking the whole tree, if no nodes are found that disqualify
830** the expression as constant, then we assume the whole expression
831** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000832*/
833static int exprNodeIsConstant(void *pArg, Expr *pExpr){
834 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000835 /* Consider functions to be constant if all their arguments are constant
836 ** and *pArg==2 */
837 case TK_FUNCTION:
838 if( *((int*)pArg)==2 ) return 0;
839 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000840 case TK_ID:
841 case TK_COLUMN:
842 case TK_DOT:
843 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000844 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000845#ifndef SQLITE_OMIT_SUBQUERY
846 case TK_SELECT:
847 case TK_EXISTS:
848#endif
drh626a8792005-01-17 22:08:19 +0000849 *((int*)pArg) = 0;
850 return 2;
drh87abf5c2005-08-25 12:45:04 +0000851 case TK_IN:
852 if( pExpr->pSelect ){
853 *((int*)pArg) = 0;
854 return 2;
855 }
drh626a8792005-01-17 22:08:19 +0000856 default:
857 return 0;
858 }
859}
860
861/*
drhfef52082000-06-06 01:50:43 +0000862** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000863** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000864**
865** For the purposes of this function, a double-quoted string (ex: "abc")
866** is considered a variable but a single-quoted string (ex: 'abc') is
867** a constant.
drhfef52082000-06-06 01:50:43 +0000868*/
danielk19774adee202004-05-08 08:23:19 +0000869int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000870 int isConst = 1;
871 walkExprTree(p, exprNodeIsConstant, &isConst);
872 return isConst;
drhfef52082000-06-06 01:50:43 +0000873}
874
875/*
drheb55bd22005-06-30 17:04:21 +0000876** Walk an expression tree. Return 1 if the expression is constant
877** or a function call with constant arguments. Return and 0 if there
878** are any variables.
879**
880** For the purposes of this function, a double-quoted string (ex: "abc")
881** is considered a variable but a single-quoted string (ex: 'abc') is
882** a constant.
883*/
884int sqlite3ExprIsConstantOrFunction(Expr *p){
885 int isConst = 2;
886 walkExprTree(p, exprNodeIsConstant, &isConst);
887 return isConst!=0;
888}
889
890/*
drh73b211a2005-01-18 04:00:42 +0000891** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000892** to fit in a 32-bit integer, return 1 and put the value of the integer
893** in *pValue. If the expression is not an integer or if it is too big
894** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000895*/
danielk19774adee202004-05-08 08:23:19 +0000896int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000897 switch( p->op ){
898 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000899 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000900 return 1;
901 }
902 break;
drhe4de1fe2002-06-02 16:09:01 +0000903 }
drh4b59ab52002-08-24 18:24:51 +0000904 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000905 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000906 }
drhe4de1fe2002-06-02 16:09:01 +0000907 case TK_UMINUS: {
908 int v;
danielk19774adee202004-05-08 08:23:19 +0000909 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000910 *pValue = -v;
911 return 1;
912 }
913 break;
914 }
915 default: break;
916 }
917 return 0;
918}
919
920/*
drhc4a3c772001-04-04 11:48:57 +0000921** Return TRUE if the given string is a row-id column name.
922*/
danielk19774adee202004-05-08 08:23:19 +0000923int sqlite3IsRowid(const char *z){
924 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
925 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
926 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000927 return 0;
928}
929
930/*
drh8141f612004-01-25 22:44:58 +0000931** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
932** that name in the set of source tables in pSrcList and make the pExpr
933** expression node refer back to that source column. The following changes
934** are made to pExpr:
935**
936** pExpr->iDb Set the index in db->aDb[] of the database holding
937** the table.
938** pExpr->iTable Set to the cursor number for the table obtained
939** from pSrcList.
940** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000941** pExpr->op Set to TK_COLUMN.
942** pExpr->pLeft Any expression this points to is deleted
943** pExpr->pRight Any expression this points to is deleted.
944**
945** The pDbToken is the name of the database (the "X"). This value may be
946** NULL meaning that name is of the form Y.Z or Z. Any available database
947** can be used. The pTableToken is the name of the table (the "Y"). This
948** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
949** means that the form of the name is Z and that columns from any table
950** can be used.
951**
952** If the name cannot be resolved unambiguously, leave an error message
953** in pParse and return non-zero. Return zero on success.
954*/
955static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000956 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000957 Token *pDbToken, /* Name of the database containing table, or NULL */
958 Token *pTableToken, /* Name of table containing column, or NULL */
959 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000960 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000961 Expr *pExpr /* Make this EXPR node point to the selected column */
962){
963 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
964 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
965 char *zCol = 0; /* Name of the column. The "Z" */
966 int i, j; /* Loop counters */
967 int cnt = 0; /* Number of matching column names */
968 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000969 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000970 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
971 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000972 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000973
974 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000975 zDb = sqlite3NameFromToken(pDbToken);
976 zTab = sqlite3NameFromToken(pTableToken);
977 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000978 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000979 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000980 }
drh8141f612004-01-25 22:44:58 +0000981
982 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000983 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000984 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000985 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000986
danielk1977b3bce662005-01-29 08:32:43 +0000987 if( pSrcList ){
988 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000989 Table *pTab;
990 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000991 Column *pCol;
992
drh43617e92006-03-06 20:55:46 +0000993 pTab = pItem->pTab;
994 assert( pTab!=0 );
995 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000996 assert( pTab->nCol>0 );
997 if( zTab ){
998 if( pItem->zAlias ){
999 char *zTabName = pItem->zAlias;
1000 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1001 }else{
1002 char *zTabName = pTab->zName;
1003 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001004 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001005 continue;
1006 }
drh626a8792005-01-17 22:08:19 +00001007 }
drh8141f612004-01-25 22:44:58 +00001008 }
danielk1977b3bce662005-01-29 08:32:43 +00001009 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001010 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001011 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001012 pMatch = pItem;
1013 }
1014 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1015 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001016 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001017 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001018 cnt++;
1019 pExpr->iTable = pItem->iCursor;
1020 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001021 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001022 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1023 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1024 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001025 if( (pExpr->flags & EP_ExpCollate)==0 ){
1026 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1027 }
drh61dfc312006-12-16 16:25:15 +00001028 if( i<pSrcList->nSrc-1 ){
1029 if( pItem[1].jointype & JT_NATURAL ){
1030 /* If this match occurred in the left table of a natural join,
1031 ** then skip the right table to avoid a duplicate match */
1032 pItem++;
1033 i++;
1034 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1035 /* If this match occurs on a column that is in the USING clause
1036 ** of a join, skip the search of the right table of the join
1037 ** to avoid a duplicate match there. */
1038 int k;
1039 for(k=0; k<pUsing->nId; k++){
1040 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1041 pItem++;
1042 i++;
1043 break;
1044 }
drh873fac02005-06-06 17:11:46 +00001045 }
1046 }
1047 }
danielk1977b3bce662005-01-29 08:32:43 +00001048 break;
1049 }
drh8141f612004-01-25 22:44:58 +00001050 }
1051 }
1052 }
drh626a8792005-01-17 22:08:19 +00001053
1054#ifndef SQLITE_OMIT_TRIGGER
1055 /* If we have not already resolved the name, then maybe
1056 ** it is a new.* or old.* trigger argument reference
1057 */
1058 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1059 TriggerStack *pTriggerStack = pParse->trigStack;
1060 Table *pTab = 0;
1061 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1062 pExpr->iTable = pTriggerStack->newIdx;
1063 assert( pTriggerStack->pTab );
1064 pTab = pTriggerStack->pTab;
1065 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1066 pExpr->iTable = pTriggerStack->oldIdx;
1067 assert( pTriggerStack->pTab );
1068 pTab = pTriggerStack->pTab;
1069 }
1070
1071 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001072 int iCol;
drh626a8792005-01-17 22:08:19 +00001073 Column *pCol = pTab->aCol;
1074
danielk1977da184232006-01-05 11:34:32 +00001075 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001076 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001077 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001078 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001079 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001080 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001081 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1082 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001083 if( (pExpr->flags & EP_ExpCollate)==0 ){
1084 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1085 }
danielk1977aee18ef2005-03-09 12:26:50 +00001086 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001087 break;
1088 }
1089 }
1090 }
1091 }
drhb7f91642004-10-31 02:22:47 +00001092#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001093
drh626a8792005-01-17 22:08:19 +00001094 /*
1095 ** Perhaps the name is a reference to the ROWID
1096 */
1097 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1098 cnt = 1;
1099 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001100 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001101 }
drh8141f612004-01-25 22:44:58 +00001102
drh626a8792005-01-17 22:08:19 +00001103 /*
1104 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1105 ** might refer to an result-set alias. This happens, for example, when
1106 ** we are resolving names in the WHERE clause of the following command:
1107 **
1108 ** SELECT a+b AS x FROM table WHERE x<10;
1109 **
1110 ** In cases like this, replace pExpr with a copy of the expression that
1111 ** forms the result set entry ("a+b" in the example) and return immediately.
1112 ** Note that the expression in the result set should have already been
1113 ** resolved by the time the WHERE clause is resolved.
1114 */
drhffe07b22005-11-03 00:41:17 +00001115 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001116 for(j=0; j<pEList->nExpr; j++){
1117 char *zAs = pEList->a[j].zName;
1118 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh4f07e5f2007-05-14 11:34:46 +00001119 Expr *pDup;
drh626a8792005-01-17 22:08:19 +00001120 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001121 assert( pExpr->pList==0 );
1122 assert( pExpr->pSelect==0 );
1123 pDup = sqlite3ExprDup(pEList->a[j].pExpr);
1124 if( pExpr->flags & EP_ExpCollate ){
1125 pDup->pColl = pExpr->pColl;
1126 pDup->flags |= EP_ExpCollate;
1127 }
1128 memcpy(pExpr, pDup, sizeof(*pExpr));
1129 sqliteFree(pDup);
drh15ccce12005-05-23 15:06:39 +00001130 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001131 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001132 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001133 }
1134 }
1135 }
1136
1137 /* Advance to the next name context. The loop will exit when either
1138 ** we have a match (cnt>0) or when we run out of name contexts.
1139 */
1140 if( cnt==0 ){
1141 pNC = pNC->pNext;
1142 }
drh8141f612004-01-25 22:44:58 +00001143 }
1144
1145 /*
1146 ** If X and Y are NULL (in other words if only the column name Z is
1147 ** supplied) and the value of Z is enclosed in double-quotes, then
1148 ** Z is a string literal if it doesn't match any column names. In that
1149 ** case, we need to return right away and not make any changes to
1150 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001151 **
1152 ** Because no reference was made to outer contexts, the pNC->nRef
1153 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001154 */
1155 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1156 sqliteFree(zCol);
1157 return 0;
1158 }
1159
1160 /*
1161 ** cnt==0 means there was not match. cnt>1 means there were two or
1162 ** more matches. Either way, we have an error.
1163 */
1164 if( cnt!=1 ){
1165 char *z = 0;
1166 char *zErr;
1167 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1168 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001169 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001170 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001171 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001172 }else{
1173 z = sqliteStrDup(zCol);
1174 }
danielk19774adee202004-05-08 08:23:19 +00001175 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001176 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001177 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001178 }
1179
drh51669862004-12-18 18:40:26 +00001180 /* If a column from a table in pSrcList is referenced, then record
1181 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1182 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1183 ** column number is greater than the number of bits in the bitmask
1184 ** then set the high-order bit of the bitmask.
1185 */
1186 if( pExpr->iColumn>=0 && pMatch!=0 ){
1187 int n = pExpr->iColumn;
1188 if( n>=sizeof(Bitmask)*8 ){
1189 n = sizeof(Bitmask)*8-1;
1190 }
1191 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001192 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001193 }
1194
danielk1977d5d56522005-03-16 12:15:20 +00001195lookupname_end:
drh8141f612004-01-25 22:44:58 +00001196 /* Clean up and return
1197 */
1198 sqliteFree(zDb);
1199 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001201 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001202 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001203 pExpr->pRight = 0;
1204 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001205lookupname_end_2:
1206 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001207 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001208 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001209 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001210 if( pMatch && !pMatch->pSelect ){
1211 pExpr->pTab = pMatch->pTab;
1212 }
drh15ccce12005-05-23 15:06:39 +00001213 /* Increment the nRef value on all name contexts from TopNC up to
1214 ** the point where the name matched. */
1215 for(;;){
1216 assert( pTopNC!=0 );
1217 pTopNC->nRef++;
1218 if( pTopNC==pNC ) break;
1219 pTopNC = pTopNC->pNext;
1220 }
1221 return 0;
1222 } else {
1223 return 1;
drh626a8792005-01-17 22:08:19 +00001224 }
drh8141f612004-01-25 22:44:58 +00001225}
1226
1227/*
drh626a8792005-01-17 22:08:19 +00001228** This routine is designed as an xFunc for walkExprTree().
1229**
drh73b211a2005-01-18 04:00:42 +00001230** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001231** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001232** the tree or 2 to abort the tree walk.
1233**
1234** This routine also does error checking and name resolution for
1235** function names. The operator for aggregate functions is changed
1236** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001237*/
1238static int nameResolverStep(void *pArg, Expr *pExpr){
1239 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001240 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001241
danielk1977b3bce662005-01-29 08:32:43 +00001242 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001243 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001244 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001245
drh626a8792005-01-17 22:08:19 +00001246 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1247 ExprSetProperty(pExpr, EP_Resolved);
1248#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001249 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1250 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001251 int i;
danielk1977f0113002006-01-24 12:09:17 +00001252 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001253 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1254 }
1255 }
1256#endif
1257 switch( pExpr->op ){
1258 /* Double-quoted strings (ex: "abc") are used as identifiers if
1259 ** possible. Otherwise they remain as strings. Single-quoted
1260 ** strings (ex: 'abc') are always string literals.
1261 */
1262 case TK_STRING: {
1263 if( pExpr->token.z[0]=='\'' ) break;
1264 /* Fall thru into the TK_ID case if this is a double-quoted string */
1265 }
1266 /* A lone identifier is the name of a column.
1267 */
1268 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001269 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1270 return 1;
1271 }
1272
1273 /* A table name and column name: ID.ID
1274 ** Or a database, table and column: ID.ID.ID
1275 */
1276 case TK_DOT: {
1277 Token *pColumn;
1278 Token *pTable;
1279 Token *pDb;
1280 Expr *pRight;
1281
danielk1977b3bce662005-01-29 08:32:43 +00001282 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001283 pRight = pExpr->pRight;
1284 if( pRight->op==TK_ID ){
1285 pDb = 0;
1286 pTable = &pExpr->pLeft->token;
1287 pColumn = &pRight->token;
1288 }else{
1289 assert( pRight->op==TK_DOT );
1290 pDb = &pExpr->pLeft->token;
1291 pTable = &pRight->pLeft->token;
1292 pColumn = &pRight->pRight->token;
1293 }
1294 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1295 return 1;
1296 }
1297
1298 /* Resolve function names
1299 */
drhb71090f2005-05-23 17:26:51 +00001300 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001301 case TK_FUNCTION: {
1302 ExprList *pList = pExpr->pList; /* The argument list */
1303 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1304 int no_such_func = 0; /* True if no such function exists */
1305 int wrong_num_args = 0; /* True if wrong number of arguments */
1306 int is_agg = 0; /* True if is an aggregate function */
1307 int i;
drh5169bbc2006-08-24 14:59:45 +00001308 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001309 int nId; /* Number of characters in function name */
1310 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001311 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001312 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001313
drh2646da72005-12-09 20:02:05 +00001314 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001315 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001316 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1317 if( pDef==0 ){
1318 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1319 if( pDef==0 ){
1320 no_such_func = 1;
1321 }else{
1322 wrong_num_args = 1;
1323 }
1324 }else{
1325 is_agg = pDef->xFunc==0;
1326 }
drh2fca7fe2006-11-23 11:59:13 +00001327#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001328 if( pDef ){
1329 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1330 if( auth!=SQLITE_OK ){
1331 if( auth==SQLITE_DENY ){
1332 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1333 pDef->zName);
1334 pNC->nErr++;
1335 }
1336 pExpr->op = TK_NULL;
1337 return 1;
1338 }
1339 }
drhb8b14212006-08-24 15:18:25 +00001340#endif
drh626a8792005-01-17 22:08:19 +00001341 if( is_agg && !pNC->allowAgg ){
1342 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1343 pNC->nErr++;
1344 is_agg = 0;
1345 }else if( no_such_func ){
1346 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1347 pNC->nErr++;
1348 }else if( wrong_num_args ){
1349 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1350 nId, zId);
1351 pNC->nErr++;
1352 }
1353 if( is_agg ){
1354 pExpr->op = TK_AGG_FUNCTION;
1355 pNC->hasAgg = 1;
1356 }
drh73b211a2005-01-18 04:00:42 +00001357 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001358 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001359 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001360 }
drh73b211a2005-01-18 04:00:42 +00001361 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001362 /* FIX ME: Compute pExpr->affinity based on the expected return
1363 ** type of the function
1364 */
1365 return is_agg;
1366 }
danielk1977b3bce662005-01-29 08:32:43 +00001367#ifndef SQLITE_OMIT_SUBQUERY
1368 case TK_SELECT:
1369 case TK_EXISTS:
1370#endif
1371 case TK_IN: {
1372 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001373 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001374#ifndef SQLITE_OMIT_CHECK
1375 if( pNC->isCheck ){
1376 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1377 }
1378#endif
danielk1977b3bce662005-01-29 08:32:43 +00001379 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1380 assert( pNC->nRef>=nRef );
1381 if( nRef!=pNC->nRef ){
1382 ExprSetProperty(pExpr, EP_VarSelect);
1383 }
1384 }
drh4284fb02005-11-03 12:33:28 +00001385 break;
danielk1977b3bce662005-01-29 08:32:43 +00001386 }
drh4284fb02005-11-03 12:33:28 +00001387#ifndef SQLITE_OMIT_CHECK
1388 case TK_VARIABLE: {
1389 if( pNC->isCheck ){
1390 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1391 }
1392 break;
1393 }
1394#endif
drh626a8792005-01-17 22:08:19 +00001395 }
1396 return 0;
1397}
1398
1399/*
drhcce7d172000-05-31 15:34:51 +00001400** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001401** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001402** index to the table in the table list and a column offset. The
1403** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1404** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001405** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001406** VDBE cursor number for a cursor that is pointing into the referenced
1407** table. The Expr.iColumn value is changed to the index of the column
1408** of the referenced table. The Expr.iColumn value for the special
1409** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1410** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001411**
drh626a8792005-01-17 22:08:19 +00001412** Also resolve function names and check the functions for proper
1413** usage. Make sure all function names are recognized and all functions
1414** have the correct number of arguments. Leave an error message
1415** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1416**
drh73b211a2005-01-18 04:00:42 +00001417** If the expression contains aggregate functions then set the EP_Agg
1418** property on the expression.
drh626a8792005-01-17 22:08:19 +00001419*/
danielk1977fc976062007-05-10 10:46:56 +00001420int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001421 NameContext *pNC, /* Namespace to resolve expressions in. */
1422 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001423){
drh13449892005-09-07 21:22:45 +00001424 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001425 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001426#if SQLITE_MAX_EXPR_DEPTH>0
1427 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1428 sqlite3ErrorMsg(pNC->pParse,
1429 "Expression tree is too large (maximum depth %d)",
1430 SQLITE_MAX_EXPR_DEPTH
1431 );
1432 return 1;
1433 }
1434 pNC->pParse->nHeight += pExpr->nHeight;
1435#endif
drh13449892005-09-07 21:22:45 +00001436 savedHasAgg = pNC->hasAgg;
1437 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001438 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001439#if SQLITE_MAX_EXPR_DEPTH>0
1440 pNC->pParse->nHeight -= pExpr->nHeight;
1441#endif
danielk1977b3bce662005-01-29 08:32:43 +00001442 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001443 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001444 }
drh13449892005-09-07 21:22:45 +00001445 if( pNC->hasAgg ){
1446 ExprSetProperty(pExpr, EP_Agg);
1447 }else if( savedHasAgg ){
1448 pNC->hasAgg = 1;
1449 }
drh73b211a2005-01-18 04:00:42 +00001450 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001451}
1452
drh1398ad32005-01-19 23:24:50 +00001453/*
1454** A pointer instance of this structure is used to pass information
1455** through walkExprTree into codeSubqueryStep().
1456*/
1457typedef struct QueryCoder QueryCoder;
1458struct QueryCoder {
1459 Parse *pParse; /* The parsing context */
1460 NameContext *pNC; /* Namespace of first enclosing query */
1461};
1462
drh626a8792005-01-17 22:08:19 +00001463
1464/*
drh9cbe6352005-11-29 03:13:21 +00001465** Generate code for scalar subqueries used as an expression
1466** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001467**
drh9cbe6352005-11-29 03:13:21 +00001468** (SELECT a FROM b) -- subquery
1469** EXISTS (SELECT a FROM b) -- EXISTS subquery
1470** x IN (4,5,11) -- IN operator with list on right-hand side
1471** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001472**
drh9cbe6352005-11-29 03:13:21 +00001473** The pExpr parameter describes the expression that contains the IN
1474** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001475*/
drh51522cd2005-01-20 13:36:19 +00001476#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001477void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001478 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001479 Vdbe *v = sqlite3GetVdbe(pParse);
1480 if( v==0 ) return;
1481
danielk1977fc976062007-05-10 10:46:56 +00001482
drh57dbd7b2005-07-08 18:25:26 +00001483 /* This code must be run in its entirety every time it is encountered
1484 ** if any of the following is true:
1485 **
1486 ** * The right-hand side is a correlated subquery
1487 ** * The right-hand side is an expression list containing variables
1488 ** * We are inside a trigger
1489 **
1490 ** If all of the above are false, then we can run this code just once
1491 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001492 */
1493 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1494 int mem = pParse->nMem++;
1495 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001496 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001497 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001498 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001499 }
1500
drhcce7d172000-05-31 15:34:51 +00001501 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001502 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001503 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001504 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001505 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001506
danielk1977bf3b7212004-05-18 10:06:24 +00001507 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001508
1509 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001510 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001511 ** filled with single-field index keys representing the results
1512 ** from the SELECT or the <exprlist>.
1513 **
1514 ** If the 'x' expression is a column value, or the SELECT...
1515 ** statement returns a column value, then the affinity of that
1516 ** column is used to build the index keys. If both 'x' and the
1517 ** SELECT... statement are columns, then numeric affinity is used
1518 ** if either column has NUMERIC or INTEGER affinity. If neither
1519 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1520 ** is used.
1521 */
1522 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001523 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001524 memset(&keyInfo, 0, sizeof(keyInfo));
1525 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001526 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001527
drhfef52082000-06-06 01:50:43 +00001528 if( pExpr->pSelect ){
1529 /* Case 1: expr IN (SELECT ...)
1530 **
danielk1977e014a832004-05-17 10:48:57 +00001531 ** Generate code to write the results of the select into the temporary
1532 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001533 */
danielk1977e014a832004-05-17 10:48:57 +00001534 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001535 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001536 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001537 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1538 return;
1539 }
drhbe5c89a2004-07-26 00:31:09 +00001540 pEList = pExpr->pSelect->pEList;
1541 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001542 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001543 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001544 }
drhfef52082000-06-06 01:50:43 +00001545 }else if( pExpr->pList ){
1546 /* Case 2: expr IN (exprlist)
1547 **
danielk1977e014a832004-05-17 10:48:57 +00001548 ** For each expression, build an index key from the evaluation and
1549 ** store it in the temporary table. If <expr> is a column, then use
1550 ** that columns affinity when building index keys. If <expr> is not
1551 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001552 */
danielk1977e014a832004-05-17 10:48:57 +00001553 int i;
drh57dbd7b2005-07-08 18:25:26 +00001554 ExprList *pList = pExpr->pList;
1555 struct ExprList_item *pItem;
1556
danielk1977e014a832004-05-17 10:48:57 +00001557 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001558 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001559 }
danielk19770202b292004-06-09 09:55:16 +00001560 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001561
1562 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001563 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1564 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001565
drh57dbd7b2005-07-08 18:25:26 +00001566 /* If the expression is not constant then we will need to
1567 ** disable the test that was generated above that makes sure
1568 ** this code only executes once. Because for a non-constant
1569 ** expression we need to rerun this code each time.
1570 */
drh6c30be82005-07-29 15:10:17 +00001571 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001572 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001573 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001574 }
danielk1977e014a832004-05-17 10:48:57 +00001575
1576 /* Evaluate the expression and insert it into the temp table */
1577 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001578 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001579 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001580 }
1581 }
danielk19770202b292004-06-09 09:55:16 +00001582 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001583 break;
drhfef52082000-06-06 01:50:43 +00001584 }
1585
drh51522cd2005-01-20 13:36:19 +00001586 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001587 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001588 /* This has to be a scalar SELECT. Generate code to put the
1589 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001590 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001591 */
drh2646da72005-12-09 20:02:05 +00001592 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001593 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001594 int iMem;
1595 int sop;
drh1398ad32005-01-19 23:24:50 +00001596
drhec7429a2005-10-06 16:53:14 +00001597 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001598 pSel = pExpr->pSelect;
1599 if( pExpr->op==TK_SELECT ){
1600 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001601 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1602 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001603 }else{
drh51522cd2005-01-20 13:36:19 +00001604 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001605 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1606 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001607 }
drhec7429a2005-10-06 16:53:14 +00001608 sqlite3ExprDelete(pSel->pLimit);
1609 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001610 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1611 return;
1612 }
danielk1977b3bce662005-01-29 08:32:43 +00001613 break;
drhcce7d172000-05-31 15:34:51 +00001614 }
1615 }
danielk1977b3bce662005-01-29 08:32:43 +00001616
drh57dbd7b2005-07-08 18:25:26 +00001617 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001618 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001619 }
danielk1977fc976062007-05-10 10:46:56 +00001620
danielk1977b3bce662005-01-29 08:32:43 +00001621 return;
drhcce7d172000-05-31 15:34:51 +00001622}
drh51522cd2005-01-20 13:36:19 +00001623#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001624
drhcce7d172000-05-31 15:34:51 +00001625/*
drhfec19aa2004-05-19 20:41:03 +00001626** Generate an instruction that will put the integer describe by
1627** text z[0..n-1] on the stack.
1628*/
1629static void codeInteger(Vdbe *v, const char *z, int n){
1630 int i;
drh6fec0762004-05-30 01:38:43 +00001631 if( sqlite3GetInt32(z, &i) ){
1632 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1633 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001634 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001635 }else{
1636 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1637 }
1638}
1639
drh945498f2007-02-24 11:52:52 +00001640
1641/*
1642** Generate code that will extract the iColumn-th column from
1643** table pTab and push that column value on the stack. There
1644** is an open cursor to pTab in iTable. If iColumn<0 then
1645** code is generated that extracts the rowid.
1646*/
1647void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1648 if( iColumn<0 ){
1649 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1650 sqlite3VdbeAddOp(v, op, iTable, 0);
1651 }else if( pTab==0 ){
1652 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1653 }else{
1654 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1655 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1656 sqlite3ColumnDefault(v, pTab, iColumn);
1657#ifndef SQLITE_OMIT_FLOATING_POINT
1658 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1659 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1660 }
1661#endif
1662 }
1663}
1664
drhfec19aa2004-05-19 20:41:03 +00001665/*
drhcce7d172000-05-31 15:34:51 +00001666** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001667** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001668**
1669** This code depends on the fact that certain token values (ex: TK_EQ)
1670** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1671** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1672** the make process cause these values to align. Assert()s in the code
1673** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001674*/
danielk19774adee202004-05-08 08:23:19 +00001675void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001676 Vdbe *v = pParse->pVdbe;
1677 int op;
drhffe07b22005-11-03 00:41:17 +00001678 int stackChng = 1; /* Amount of change to stack depth */
1679
danielk19777977a172004-11-09 12:44:37 +00001680 if( v==0 ) return;
1681 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001682 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001683 return;
1684 }
drhf2bc0132004-10-04 13:19:23 +00001685 op = pExpr->op;
1686 switch( op ){
drh13449892005-09-07 21:22:45 +00001687 case TK_AGG_COLUMN: {
1688 AggInfo *pAggInfo = pExpr->pAggInfo;
1689 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1690 if( !pAggInfo->directMode ){
1691 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1692 break;
1693 }else if( pAggInfo->useSortingIdx ){
1694 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1695 pCol->iSorterColumn);
1696 break;
1697 }
1698 /* Otherwise, fall thru into the TK_COLUMN case */
1699 }
drh967e8b72000-06-21 13:59:10 +00001700 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001701 if( pExpr->iTable<0 ){
1702 /* This only happens when coding check constraints */
1703 assert( pParse->ckOffset>0 );
1704 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001705 }else{
drh945498f2007-02-24 11:52:52 +00001706 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001707 }
drhcce7d172000-05-31 15:34:51 +00001708 break;
1709 }
1710 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001711 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001712 break;
1713 }
1714 case TK_FLOAT:
1715 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001716 assert( TK_FLOAT==OP_Real );
1717 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001718 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001719 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001720 break;
1721 }
drhf0863fe2005-06-12 21:35:51 +00001722 case TK_NULL: {
1723 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1724 break;
1725 }
danielk19775338a5f2005-01-20 13:03:10 +00001726#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001727 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001728 int n;
1729 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001730 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001731 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001732 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001733 assert( n>=0 );
1734 if( n==0 ){
1735 z = "";
1736 }
1737 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001738 break;
1739 }
danielk19775338a5f2005-01-20 13:03:10 +00001740#endif
drh50457892003-09-06 01:10:47 +00001741 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001742 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001743 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001744 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001745 }
drh50457892003-09-06 01:10:47 +00001746 break;
1747 }
drh4e0cff62004-11-05 05:10:28 +00001748 case TK_REGISTER: {
1749 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1750 break;
1751 }
drh487e2622005-06-25 18:42:14 +00001752#ifndef SQLITE_OMIT_CAST
1753 case TK_CAST: {
1754 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001755 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001756 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001757 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001758 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1759 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1760 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1761 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1762 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1763 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1764 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001765 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001766 break;
1767 }
1768#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001769 case TK_LT:
1770 case TK_LE:
1771 case TK_GT:
1772 case TK_GE:
1773 case TK_NE:
1774 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001775 assert( TK_LT==OP_Lt );
1776 assert( TK_LE==OP_Le );
1777 assert( TK_GT==OP_Gt );
1778 assert( TK_GE==OP_Ge );
1779 assert( TK_EQ==OP_Eq );
1780 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001781 sqlite3ExprCode(pParse, pExpr->pLeft);
1782 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001783 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001784 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001785 break;
drhc9b84a12002-06-20 11:36:48 +00001786 }
drhcce7d172000-05-31 15:34:51 +00001787 case TK_AND:
1788 case TK_OR:
1789 case TK_PLUS:
1790 case TK_STAR:
1791 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001792 case TK_REM:
1793 case TK_BITAND:
1794 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001795 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001796 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001797 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001798 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001799 assert( TK_AND==OP_And );
1800 assert( TK_OR==OP_Or );
1801 assert( TK_PLUS==OP_Add );
1802 assert( TK_MINUS==OP_Subtract );
1803 assert( TK_REM==OP_Remainder );
1804 assert( TK_BITAND==OP_BitAnd );
1805 assert( TK_BITOR==OP_BitOr );
1806 assert( TK_SLASH==OP_Divide );
1807 assert( TK_LSHIFT==OP_ShiftLeft );
1808 assert( TK_RSHIFT==OP_ShiftRight );
1809 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001810 sqlite3ExprCode(pParse, pExpr->pLeft);
1811 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001812 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001813 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001814 break;
1815 }
drhcce7d172000-05-31 15:34:51 +00001816 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001817 Expr *pLeft = pExpr->pLeft;
1818 assert( pLeft );
1819 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1820 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001821 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001822 if( pLeft->op==TK_FLOAT ){
1823 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001824 }else{
drhfec19aa2004-05-19 20:41:03 +00001825 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001826 }
drh6e142f52000-06-08 13:36:40 +00001827 sqliteFree(z);
1828 break;
1829 }
drh1ccde152000-06-17 13:12:39 +00001830 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001831 }
drhbf4133c2001-10-13 02:59:08 +00001832 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001833 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001834 assert( TK_BITNOT==OP_BitNot );
1835 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001836 sqlite3ExprCode(pParse, pExpr->pLeft);
1837 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001838 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001839 break;
1840 }
1841 case TK_ISNULL:
1842 case TK_NOTNULL: {
1843 int dest;
drhf2bc0132004-10-04 13:19:23 +00001844 assert( TK_ISNULL==OP_IsNull );
1845 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001846 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1847 sqlite3ExprCode(pParse, pExpr->pLeft);
1848 dest = sqlite3VdbeCurrentAddr(v) + 2;
1849 sqlite3VdbeAddOp(v, op, 1, dest);
1850 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001851 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001852 break;
drhcce7d172000-05-31 15:34:51 +00001853 }
drh22827922000-06-06 17:27:05 +00001854 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001855 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001856 if( pInfo==0 ){
1857 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1858 &pExpr->span);
1859 }else{
1860 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1861 }
drh22827922000-06-06 17:27:05 +00001862 break;
1863 }
drhb71090f2005-05-23 17:26:51 +00001864 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001865 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001866 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001867 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001868 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001869 int nId;
1870 const char *zId;
drh13449892005-09-07 21:22:45 +00001871 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001872 int i;
danielk197714db2662006-01-09 16:12:04 +00001873 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001874 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001875 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001876 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001877 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001878 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001879 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001880#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001881 /* Possibly overload the function if the first argument is
1882 ** a virtual table column.
1883 **
1884 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1885 ** second argument, not the first, as the argument to test to
1886 ** see if it is a column in a virtual table. This is done because
1887 ** the left operand of infix functions (the operand we want to
1888 ** control overloading) ends up as the second argument to the
1889 ** function. The expression "A glob B" is equivalent to
1890 ** "glob(B,A). We want to use the A in "A glob B" to test
1891 ** for function overloading. But we use the B term in "glob(B,A)".
1892 */
drh6a03a1c2006-07-08 18:34:59 +00001893 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001894 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1895 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001896 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1897 }
1898#endif
danielk1977682f68b2004-06-05 10:22:17 +00001899 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001900 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001901 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001902 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001903 if( pDef->needCollSeq && !pColl ){
1904 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1905 }
1906 }
1907 if( pDef->needCollSeq ){
1908 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001909 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001910 }
drh13449892005-09-07 21:22:45 +00001911 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001912 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001913 break;
1914 }
drhfe2093d2005-01-20 22:48:47 +00001915#ifndef SQLITE_OMIT_SUBQUERY
1916 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001917 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001918 if( pExpr->iColumn==0 ){
1919 sqlite3CodeSubselect(pParse, pExpr);
1920 }
danielk19774adee202004-05-08 08:23:19 +00001921 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001922 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001923 break;
1924 }
drhfef52082000-06-06 01:50:43 +00001925 case TK_IN: {
1926 int addr;
drh94a11212004-09-25 13:12:14 +00001927 char affinity;
drhafa5f682006-01-30 14:36:59 +00001928 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001929 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001930
1931 /* Figure out the affinity to use to create a key from the results
1932 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001933 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001934 */
drh94a11212004-09-25 13:12:14 +00001935 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001936
danielk19774adee202004-05-08 08:23:19 +00001937 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00001938 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00001939
1940 /* Code the <expr> from "<expr> IN (...)". The temporary table
1941 ** pExpr->iTable contains the values that make up the (...) set.
1942 */
danielk19774adee202004-05-08 08:23:19 +00001943 sqlite3ExprCode(pParse, pExpr->pLeft);
1944 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001945 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001946 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001947 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001948 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001949 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001950 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1951 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1952
drhfef52082000-06-06 01:50:43 +00001953 break;
1954 }
danielk197793758c82005-01-21 08:13:14 +00001955#endif
drhfef52082000-06-06 01:50:43 +00001956 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001957 Expr *pLeft = pExpr->pLeft;
1958 struct ExprList_item *pLItem = pExpr->pList->a;
1959 Expr *pRight = pLItem->pExpr;
1960 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001961 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001962 sqlite3ExprCode(pParse, pRight);
1963 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001964 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001965 pLItem++;
1966 pRight = pLItem->pExpr;
1967 sqlite3ExprCode(pParse, pRight);
1968 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001969 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001970 break;
1971 }
drh4f07e5f2007-05-14 11:34:46 +00001972 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00001973 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001974 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001975 break;
1976 }
drh17a7f8d2002-03-24 13:13:27 +00001977 case TK_CASE: {
1978 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001979 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001980 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001981 int i;
drhbe5c89a2004-07-26 00:31:09 +00001982 ExprList *pEList;
1983 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001984
1985 assert(pExpr->pList);
1986 assert((pExpr->pList->nExpr % 2) == 0);
1987 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001988 pEList = pExpr->pList;
1989 aListelem = pEList->a;
1990 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001991 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001992 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001993 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001994 }
drhf5905aa2002-05-26 20:54:33 +00001995 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001996 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001997 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001998 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001999 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2000 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002001 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002002 }else{
danielk19774adee202004-05-08 08:23:19 +00002003 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002004 }
drhbe5c89a2004-07-26 00:31:09 +00002005 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002006 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002007 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002008 }
drhf570f012002-05-31 15:51:25 +00002009 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002010 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002011 }
drh17a7f8d2002-03-24 13:13:27 +00002012 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002013 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002014 }else{
drhf0863fe2005-06-12 21:35:51 +00002015 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002016 }
danielk19774adee202004-05-08 08:23:19 +00002017 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002018 break;
2019 }
danielk19775338a5f2005-01-20 13:03:10 +00002020#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002021 case TK_RAISE: {
2022 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002023 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002024 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002025 return;
2026 }
drhad6d9462004-09-19 02:15:24 +00002027 if( pExpr->iColumn!=OE_Ignore ){
2028 assert( pExpr->iColumn==OE_Rollback ||
2029 pExpr->iColumn == OE_Abort ||
2030 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002031 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002032 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002033 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002034 } else {
drhad6d9462004-09-19 02:15:24 +00002035 assert( pExpr->iColumn == OE_Ignore );
2036 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2037 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2038 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002039 }
drhffe07b22005-11-03 00:41:17 +00002040 stackChng = 0;
2041 break;
drh17a7f8d2002-03-24 13:13:27 +00002042 }
danielk19775338a5f2005-01-20 13:03:10 +00002043#endif
drhffe07b22005-11-03 00:41:17 +00002044 }
2045
2046 if( pParse->ckOffset ){
2047 pParse->ckOffset += stackChng;
2048 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002049 }
drhcce7d172000-05-31 15:34:51 +00002050}
2051
danielk197793758c82005-01-21 08:13:14 +00002052#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002053/*
drh25303782004-12-07 15:41:48 +00002054** Generate code that evalutes the given expression and leaves the result
2055** on the stack. See also sqlite3ExprCode().
2056**
2057** This routine might also cache the result and modify the pExpr tree
2058** so that it will make use of the cached result on subsequent evaluations
2059** rather than evaluate the whole expression again. Trivial expressions are
2060** not cached. If the expression is cached, its result is stored in a
2061** memory location.
2062*/
2063void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2064 Vdbe *v = pParse->pVdbe;
2065 int iMem;
2066 int addr1, addr2;
2067 if( v==0 ) return;
2068 addr1 = sqlite3VdbeCurrentAddr(v);
2069 sqlite3ExprCode(pParse, pExpr);
2070 addr2 = sqlite3VdbeCurrentAddr(v);
2071 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2072 iMem = pExpr->iTable = pParse->nMem++;
2073 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2074 pExpr->op = TK_REGISTER;
2075 }
2076}
danielk197793758c82005-01-21 08:13:14 +00002077#endif
drh25303782004-12-07 15:41:48 +00002078
2079/*
drh268380c2004-02-25 13:47:31 +00002080** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002081** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002082**
2083** Return the number of elements pushed onto the stack.
2084*/
danielk19774adee202004-05-08 08:23:19 +00002085int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002086 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002087 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002088){
2089 struct ExprList_item *pItem;
2090 int i, n;
drh268380c2004-02-25 13:47:31 +00002091 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002092 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002093 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002094 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002095 }
drhf9b596e2004-05-26 16:54:42 +00002096 return n;
drh268380c2004-02-25 13:47:31 +00002097}
2098
2099/*
drhcce7d172000-05-31 15:34:51 +00002100** Generate code for a boolean expression such that a jump is made
2101** to the label "dest" if the expression is true but execution
2102** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002103**
2104** If the expression evaluates to NULL (neither true nor false), then
2105** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002106**
2107** This code depends on the fact that certain token values (ex: TK_EQ)
2108** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2109** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2110** the make process cause these values to align. Assert()s in the code
2111** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002112*/
danielk19774adee202004-05-08 08:23:19 +00002113void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002114 Vdbe *v = pParse->pVdbe;
2115 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002116 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002117 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002118 op = pExpr->op;
2119 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002120 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002121 int d2 = sqlite3VdbeMakeLabel(v);
2122 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2123 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2124 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002125 break;
2126 }
2127 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002128 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2129 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002130 break;
2131 }
2132 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002133 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002134 break;
2135 }
2136 case TK_LT:
2137 case TK_LE:
2138 case TK_GT:
2139 case TK_GE:
2140 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002141 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002142 assert( TK_LT==OP_Lt );
2143 assert( TK_LE==OP_Le );
2144 assert( TK_GT==OP_Gt );
2145 assert( TK_GE==OP_Ge );
2146 assert( TK_EQ==OP_Eq );
2147 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002148 sqlite3ExprCode(pParse, pExpr->pLeft);
2149 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002150 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002151 break;
2152 }
2153 case TK_ISNULL:
2154 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002155 assert( TK_ISNULL==OP_IsNull );
2156 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002157 sqlite3ExprCode(pParse, pExpr->pLeft);
2158 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002159 break;
2160 }
drhfef52082000-06-06 01:50:43 +00002161 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002162 /* The expression "x BETWEEN y AND z" is implemented as:
2163 **
2164 ** 1 IF (x < y) GOTO 3
2165 ** 2 IF (x <= z) GOTO <dest>
2166 ** 3 ...
2167 */
drhf5905aa2002-05-26 20:54:33 +00002168 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002169 Expr *pLeft = pExpr->pLeft;
2170 Expr *pRight = pExpr->pList->a[0].pExpr;
2171 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002172 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002173 sqlite3ExprCode(pParse, pRight);
2174 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002175
drhbe5c89a2004-07-26 00:31:09 +00002176 pRight = pExpr->pList->a[1].pExpr;
2177 sqlite3ExprCode(pParse, pRight);
2178 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002179
danielk19774adee202004-05-08 08:23:19 +00002180 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002181 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002182 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002183 break;
2184 }
drhcce7d172000-05-31 15:34:51 +00002185 default: {
danielk19774adee202004-05-08 08:23:19 +00002186 sqlite3ExprCode(pParse, pExpr);
2187 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002188 break;
2189 }
2190 }
drhffe07b22005-11-03 00:41:17 +00002191 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002192}
2193
2194/*
drh66b89c82000-11-28 20:47:17 +00002195** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002196** to the label "dest" if the expression is false but execution
2197** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002198**
2199** If the expression evaluates to NULL (neither true nor false) then
2200** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002201*/
danielk19774adee202004-05-08 08:23:19 +00002202void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002203 Vdbe *v = pParse->pVdbe;
2204 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002205 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002206 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002207
2208 /* The value of pExpr->op and op are related as follows:
2209 **
2210 ** pExpr->op op
2211 ** --------- ----------
2212 ** TK_ISNULL OP_NotNull
2213 ** TK_NOTNULL OP_IsNull
2214 ** TK_NE OP_Eq
2215 ** TK_EQ OP_Ne
2216 ** TK_GT OP_Le
2217 ** TK_LE OP_Gt
2218 ** TK_GE OP_Lt
2219 ** TK_LT OP_Ge
2220 **
2221 ** For other values of pExpr->op, op is undefined and unused.
2222 ** The value of TK_ and OP_ constants are arranged such that we
2223 ** can compute the mapping above using the following expression.
2224 ** Assert()s verify that the computation is correct.
2225 */
2226 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2227
2228 /* Verify correct alignment of TK_ and OP_ constants
2229 */
2230 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2231 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2232 assert( pExpr->op!=TK_NE || op==OP_Eq );
2233 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2234 assert( pExpr->op!=TK_LT || op==OP_Ge );
2235 assert( pExpr->op!=TK_LE || op==OP_Gt );
2236 assert( pExpr->op!=TK_GT || op==OP_Le );
2237 assert( pExpr->op!=TK_GE || op==OP_Lt );
2238
drhcce7d172000-05-31 15:34:51 +00002239 switch( pExpr->op ){
2240 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002241 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2242 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002243 break;
2244 }
2245 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002246 int d2 = sqlite3VdbeMakeLabel(v);
2247 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2248 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2249 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002250 break;
2251 }
2252 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002253 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002254 break;
2255 }
2256 case TK_LT:
2257 case TK_LE:
2258 case TK_GT:
2259 case TK_GE:
2260 case TK_NE:
2261 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002262 sqlite3ExprCode(pParse, pExpr->pLeft);
2263 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002264 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002265 break;
2266 }
drhcce7d172000-05-31 15:34:51 +00002267 case TK_ISNULL:
2268 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002269 sqlite3ExprCode(pParse, pExpr->pLeft);
2270 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002271 break;
2272 }
drhfef52082000-06-06 01:50:43 +00002273 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002274 /* The expression is "x BETWEEN y AND z". It is implemented as:
2275 **
2276 ** 1 IF (x >= y) GOTO 3
2277 ** 2 GOTO <dest>
2278 ** 3 IF (x > z) GOTO <dest>
2279 */
drhfef52082000-06-06 01:50:43 +00002280 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002281 Expr *pLeft = pExpr->pLeft;
2282 Expr *pRight = pExpr->pList->a[0].pExpr;
2283 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002284 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002285 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002286 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002287 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2288
danielk19774adee202004-05-08 08:23:19 +00002289 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2290 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002291 pRight = pExpr->pList->a[1].pExpr;
2292 sqlite3ExprCode(pParse, pRight);
2293 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002294 break;
2295 }
drhcce7d172000-05-31 15:34:51 +00002296 default: {
danielk19774adee202004-05-08 08:23:19 +00002297 sqlite3ExprCode(pParse, pExpr);
2298 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002299 break;
2300 }
2301 }
drhffe07b22005-11-03 00:41:17 +00002302 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002303}
drh22827922000-06-06 17:27:05 +00002304
2305/*
2306** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2307** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002308**
2309** Sometimes this routine will return FALSE even if the two expressions
2310** really are equivalent. If we cannot prove that the expressions are
2311** identical, we return FALSE just to be safe. So if this routine
2312** returns false, then you do not really know for certain if the two
2313** expressions are the same. But if you get a TRUE return, then you
2314** can be sure the expressions are the same. In the places where
2315** this routine is used, it does not hurt to get an extra FALSE - that
2316** just might result in some slightly slower code. But returning
2317** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002318*/
danielk19774adee202004-05-08 08:23:19 +00002319int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002320 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002321 if( pA==0||pB==0 ){
2322 return pB==pA;
drh22827922000-06-06 17:27:05 +00002323 }
2324 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002325 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002326 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2327 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002328 if( pA->pList ){
2329 if( pB->pList==0 ) return 0;
2330 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2331 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002332 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002333 return 0;
2334 }
2335 }
2336 }else if( pB->pList ){
2337 return 0;
2338 }
2339 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002340 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002341 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002342 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002343 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002344 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2345 return 0;
2346 }
drh22827922000-06-06 17:27:05 +00002347 }
2348 return 1;
2349}
2350
drh13449892005-09-07 21:22:45 +00002351
drh22827922000-06-06 17:27:05 +00002352/*
drh13449892005-09-07 21:22:45 +00002353** Add a new element to the pAggInfo->aCol[] array. Return the index of
2354** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002355*/
drh13449892005-09-07 21:22:45 +00002356static int addAggInfoColumn(AggInfo *pInfo){
2357 int i;
drhcf643722007-03-27 13:36:37 +00002358 pInfo->aCol = sqlite3ArrayAllocate(
2359 pInfo->aCol,
2360 sizeof(pInfo->aCol[0]),
2361 3,
2362 &pInfo->nColumn,
2363 &pInfo->nColumnAlloc,
2364 &i
2365 );
drh13449892005-09-07 21:22:45 +00002366 return i;
2367}
2368
2369/*
2370** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2371** the new element. Return a negative number if malloc fails.
2372*/
2373static int addAggInfoFunc(AggInfo *pInfo){
2374 int i;
drhcf643722007-03-27 13:36:37 +00002375 pInfo->aFunc = sqlite3ArrayAllocate(
2376 pInfo->aFunc,
2377 sizeof(pInfo->aFunc[0]),
2378 3,
2379 &pInfo->nFunc,
2380 &pInfo->nFuncAlloc,
2381 &i
2382 );
drh13449892005-09-07 21:22:45 +00002383 return i;
2384}
drh22827922000-06-06 17:27:05 +00002385
2386/*
drh626a8792005-01-17 22:08:19 +00002387** This is an xFunc for walkExprTree() used to implement
2388** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2389** for additional information.
drh22827922000-06-06 17:27:05 +00002390**
drh626a8792005-01-17 22:08:19 +00002391** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002392*/
drh626a8792005-01-17 22:08:19 +00002393static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002394 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002395 NameContext *pNC = (NameContext *)pArg;
2396 Parse *pParse = pNC->pParse;
2397 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002398 AggInfo *pAggInfo = pNC->pAggInfo;
2399
drh22827922000-06-06 17:27:05 +00002400
drh22827922000-06-06 17:27:05 +00002401 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002402 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002403 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002404 /* Check to see if the column is in one of the tables in the FROM
2405 ** clause of the aggregate query */
2406 if( pSrcList ){
2407 struct SrcList_item *pItem = pSrcList->a;
2408 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2409 struct AggInfo_col *pCol;
2410 if( pExpr->iTable==pItem->iCursor ){
2411 /* If we reach this point, it means that pExpr refers to a table
2412 ** that is in the FROM clause of the aggregate query.
2413 **
2414 ** Make an entry for the column in pAggInfo->aCol[] if there
2415 ** is not an entry there already.
2416 */
drh7f906d62007-03-12 23:48:52 +00002417 int k;
drh13449892005-09-07 21:22:45 +00002418 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002419 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002420 if( pCol->iTable==pExpr->iTable &&
2421 pCol->iColumn==pExpr->iColumn ){
2422 break;
2423 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002424 }
drh7f906d62007-03-12 23:48:52 +00002425 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2426 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002427 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002428 pCol->iTable = pExpr->iTable;
2429 pCol->iColumn = pExpr->iColumn;
2430 pCol->iMem = pParse->nMem++;
2431 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002432 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002433 if( pAggInfo->pGroupBy ){
2434 int j, n;
2435 ExprList *pGB = pAggInfo->pGroupBy;
2436 struct ExprList_item *pTerm = pGB->a;
2437 n = pGB->nExpr;
2438 for(j=0; j<n; j++, pTerm++){
2439 Expr *pE = pTerm->pExpr;
2440 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2441 pE->iColumn==pExpr->iColumn ){
2442 pCol->iSorterColumn = j;
2443 break;
2444 }
2445 }
2446 }
2447 if( pCol->iSorterColumn<0 ){
2448 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2449 }
2450 }
2451 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2452 ** because it was there before or because we just created it).
2453 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2454 ** pAggInfo->aCol[] entry.
2455 */
2456 pExpr->pAggInfo = pAggInfo;
2457 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002458 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002459 break;
2460 } /* endif pExpr->iTable==pItem->iCursor */
2461 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002462 }
drh626a8792005-01-17 22:08:19 +00002463 return 1;
drh22827922000-06-06 17:27:05 +00002464 }
2465 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002466 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2467 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002468 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002469 /* Check to see if pExpr is a duplicate of another aggregate
2470 ** function that is already in the pAggInfo structure
2471 */
2472 struct AggInfo_func *pItem = pAggInfo->aFunc;
2473 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2474 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002475 break;
2476 }
drh22827922000-06-06 17:27:05 +00002477 }
drh13449892005-09-07 21:22:45 +00002478 if( i>=pAggInfo->nFunc ){
2479 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2480 */
danielk197714db2662006-01-09 16:12:04 +00002481 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002482 i = addAggInfoFunc(pAggInfo);
2483 if( i>=0 ){
2484 pItem = &pAggInfo->aFunc[i];
2485 pItem->pExpr = pExpr;
2486 pItem->iMem = pParse->nMem++;
2487 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002488 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002489 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002490 if( pExpr->flags & EP_Distinct ){
2491 pItem->iDistinct = pParse->nTab++;
2492 }else{
2493 pItem->iDistinct = -1;
2494 }
drh13449892005-09-07 21:22:45 +00002495 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002496 }
drh13449892005-09-07 21:22:45 +00002497 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2498 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002499 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002500 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002501 return 1;
drh22827922000-06-06 17:27:05 +00002502 }
drh22827922000-06-06 17:27:05 +00002503 }
2504 }
drh13449892005-09-07 21:22:45 +00002505
2506 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2507 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2508 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2509 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002510 if( pExpr->pSelect ){
2511 pNC->nDepth++;
2512 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2513 pNC->nDepth--;
2514 }
drh626a8792005-01-17 22:08:19 +00002515 return 0;
2516}
2517
2518/*
2519** Analyze the given expression looking for aggregate functions and
2520** for variables that need to be added to the pParse->aAgg[] array.
2521** Make additional entries to the pParse->aAgg[] array as necessary.
2522**
2523** This routine should only be called after the expression has been
2524** analyzed by sqlite3ExprResolveNames().
2525**
2526** If errors are seen, leave an error message in zErrMsg and return
2527** the number of errors.
2528*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002529int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2530 int nErr = pNC->pParse->nErr;
2531 walkExprTree(pExpr, analyzeAggregate, pNC);
2532 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002533}
drh5d9a4af2005-08-30 00:54:01 +00002534
2535/*
2536** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2537** expression list. Return the number of errors.
2538**
2539** If an error is found, the analysis is cut short.
2540*/
2541int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2542 struct ExprList_item *pItem;
2543 int i;
2544 int nErr = 0;
2545 if( pList ){
2546 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2547 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2548 }
2549 }
2550 return nErr;
2551}