blob: 5c48c0b5efb854c88f61a1dbc242fcd4f7467dc1 [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**
drh0a168372007-06-08 00:20:47 +000015** $Id: expr.c,v 1.297 2007/06/08 00:20:48 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
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){
drh0a168372007-06-08 00:20:47 +0000834 int *pN = (int*)pArg;
835
836 /* If *pArg is 3 then any term of the expression that comes from
837 ** the ON or USING clauses of a join disqualifies the expression
838 ** from being considered constant. */
839 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
840 *pN = 0;
841 return 2;
842 }
843
drh626a8792005-01-17 22:08:19 +0000844 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000845 /* Consider functions to be constant if all their arguments are constant
846 ** and *pArg==2 */
847 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000848 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000849 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000850 case TK_ID:
851 case TK_COLUMN:
852 case TK_DOT:
853 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000854 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000855#ifndef SQLITE_OMIT_SUBQUERY
856 case TK_SELECT:
857 case TK_EXISTS:
858#endif
drh0a168372007-06-08 00:20:47 +0000859 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000860 return 2;
drh87abf5c2005-08-25 12:45:04 +0000861 case TK_IN:
862 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000863 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000864 return 2;
865 }
drh626a8792005-01-17 22:08:19 +0000866 default:
867 return 0;
868 }
869}
870
871/*
drhfef52082000-06-06 01:50:43 +0000872** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000873** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000874**
875** For the purposes of this function, a double-quoted string (ex: "abc")
876** is considered a variable but a single-quoted string (ex: 'abc') is
877** a constant.
drhfef52082000-06-06 01:50:43 +0000878*/
danielk19774adee202004-05-08 08:23:19 +0000879int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000880 int isConst = 1;
881 walkExprTree(p, exprNodeIsConstant, &isConst);
882 return isConst;
drhfef52082000-06-06 01:50:43 +0000883}
884
885/*
drheb55bd22005-06-30 17:04:21 +0000886** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000887** that does no originate from the ON or USING clauses of a join.
888** Return 0 if it involves variables or function calls or terms from
889** an ON or USING clause.
890*/
891int sqlite3ExprIsConstantNotJoin(Expr *p){
892 int isConst = 3;
893 walkExprTree(p, exprNodeIsConstant, &isConst);
894 return isConst!=0;
895}
896
897/*
898** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000899** or a function call with constant arguments. Return and 0 if there
900** are any variables.
901**
902** For the purposes of this function, a double-quoted string (ex: "abc")
903** is considered a variable but a single-quoted string (ex: 'abc') is
904** a constant.
905*/
906int sqlite3ExprIsConstantOrFunction(Expr *p){
907 int isConst = 2;
908 walkExprTree(p, exprNodeIsConstant, &isConst);
909 return isConst!=0;
910}
911
912/*
drh73b211a2005-01-18 04:00:42 +0000913** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000914** to fit in a 32-bit integer, return 1 and put the value of the integer
915** in *pValue. If the expression is not an integer or if it is too big
916** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000917*/
danielk19774adee202004-05-08 08:23:19 +0000918int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000919 switch( p->op ){
920 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000921 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000922 return 1;
923 }
924 break;
drhe4de1fe2002-06-02 16:09:01 +0000925 }
drh4b59ab52002-08-24 18:24:51 +0000926 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000927 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000928 }
drhe4de1fe2002-06-02 16:09:01 +0000929 case TK_UMINUS: {
930 int v;
danielk19774adee202004-05-08 08:23:19 +0000931 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000932 *pValue = -v;
933 return 1;
934 }
935 break;
936 }
937 default: break;
938 }
939 return 0;
940}
941
942/*
drhc4a3c772001-04-04 11:48:57 +0000943** Return TRUE if the given string is a row-id column name.
944*/
danielk19774adee202004-05-08 08:23:19 +0000945int sqlite3IsRowid(const char *z){
946 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
947 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
948 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000949 return 0;
950}
951
952/*
drh8141f612004-01-25 22:44:58 +0000953** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
954** that name in the set of source tables in pSrcList and make the pExpr
955** expression node refer back to that source column. The following changes
956** are made to pExpr:
957**
958** pExpr->iDb Set the index in db->aDb[] of the database holding
959** the table.
960** pExpr->iTable Set to the cursor number for the table obtained
961** from pSrcList.
962** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000963** pExpr->op Set to TK_COLUMN.
964** pExpr->pLeft Any expression this points to is deleted
965** pExpr->pRight Any expression this points to is deleted.
966**
967** The pDbToken is the name of the database (the "X"). This value may be
968** NULL meaning that name is of the form Y.Z or Z. Any available database
969** can be used. The pTableToken is the name of the table (the "Y"). This
970** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
971** means that the form of the name is Z and that columns from any table
972** can be used.
973**
974** If the name cannot be resolved unambiguously, leave an error message
975** in pParse and return non-zero. Return zero on success.
976*/
977static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000978 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000979 Token *pDbToken, /* Name of the database containing table, or NULL */
980 Token *pTableToken, /* Name of table containing column, or NULL */
981 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000982 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000983 Expr *pExpr /* Make this EXPR node point to the selected column */
984){
985 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
986 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
987 char *zCol = 0; /* Name of the column. The "Z" */
988 int i, j; /* Loop counters */
989 int cnt = 0; /* Number of matching column names */
990 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000991 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000992 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
993 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000994 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000995
996 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000997 zDb = sqlite3NameFromToken(pDbToken);
998 zTab = sqlite3NameFromToken(pTableToken);
999 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +00001000 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +00001001 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001002 }
drh8141f612004-01-25 22:44:58 +00001003
1004 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001005 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001006 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001007 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001008
danielk1977b3bce662005-01-29 08:32:43 +00001009 if( pSrcList ){
1010 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001011 Table *pTab;
1012 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001013 Column *pCol;
1014
drh43617e92006-03-06 20:55:46 +00001015 pTab = pItem->pTab;
1016 assert( pTab!=0 );
1017 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001018 assert( pTab->nCol>0 );
1019 if( zTab ){
1020 if( pItem->zAlias ){
1021 char *zTabName = pItem->zAlias;
1022 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1023 }else{
1024 char *zTabName = pTab->zName;
1025 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001026 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001027 continue;
1028 }
drh626a8792005-01-17 22:08:19 +00001029 }
drh8141f612004-01-25 22:44:58 +00001030 }
danielk1977b3bce662005-01-29 08:32:43 +00001031 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001032 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001033 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001034 pMatch = pItem;
1035 }
1036 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1037 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001038 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001039 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001040 cnt++;
1041 pExpr->iTable = pItem->iCursor;
1042 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001043 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001044 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1045 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1046 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001047 if( (pExpr->flags & EP_ExpCollate)==0 ){
1048 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1049 }
drh61dfc312006-12-16 16:25:15 +00001050 if( i<pSrcList->nSrc-1 ){
1051 if( pItem[1].jointype & JT_NATURAL ){
1052 /* If this match occurred in the left table of a natural join,
1053 ** then skip the right table to avoid a duplicate match */
1054 pItem++;
1055 i++;
1056 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1057 /* If this match occurs on a column that is in the USING clause
1058 ** of a join, skip the search of the right table of the join
1059 ** to avoid a duplicate match there. */
1060 int k;
1061 for(k=0; k<pUsing->nId; k++){
1062 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1063 pItem++;
1064 i++;
1065 break;
1066 }
drh873fac02005-06-06 17:11:46 +00001067 }
1068 }
1069 }
danielk1977b3bce662005-01-29 08:32:43 +00001070 break;
1071 }
drh8141f612004-01-25 22:44:58 +00001072 }
1073 }
1074 }
drh626a8792005-01-17 22:08:19 +00001075
1076#ifndef SQLITE_OMIT_TRIGGER
1077 /* If we have not already resolved the name, then maybe
1078 ** it is a new.* or old.* trigger argument reference
1079 */
1080 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1081 TriggerStack *pTriggerStack = pParse->trigStack;
1082 Table *pTab = 0;
1083 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1084 pExpr->iTable = pTriggerStack->newIdx;
1085 assert( pTriggerStack->pTab );
1086 pTab = pTriggerStack->pTab;
1087 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1088 pExpr->iTable = pTriggerStack->oldIdx;
1089 assert( pTriggerStack->pTab );
1090 pTab = pTriggerStack->pTab;
1091 }
1092
1093 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001094 int iCol;
drh626a8792005-01-17 22:08:19 +00001095 Column *pCol = pTab->aCol;
1096
danielk1977da184232006-01-05 11:34:32 +00001097 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001098 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001099 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001100 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001101 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001102 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001103 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1104 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001105 if( (pExpr->flags & EP_ExpCollate)==0 ){
1106 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1107 }
danielk1977aee18ef2005-03-09 12:26:50 +00001108 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001109 break;
1110 }
1111 }
1112 }
1113 }
drhb7f91642004-10-31 02:22:47 +00001114#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001115
drh626a8792005-01-17 22:08:19 +00001116 /*
1117 ** Perhaps the name is a reference to the ROWID
1118 */
1119 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1120 cnt = 1;
1121 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001122 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001123 }
drh8141f612004-01-25 22:44:58 +00001124
drh626a8792005-01-17 22:08:19 +00001125 /*
1126 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1127 ** might refer to an result-set alias. This happens, for example, when
1128 ** we are resolving names in the WHERE clause of the following command:
1129 **
1130 ** SELECT a+b AS x FROM table WHERE x<10;
1131 **
1132 ** In cases like this, replace pExpr with a copy of the expression that
1133 ** forms the result set entry ("a+b" in the example) and return immediately.
1134 ** Note that the expression in the result set should have already been
1135 ** resolved by the time the WHERE clause is resolved.
1136 */
drhffe07b22005-11-03 00:41:17 +00001137 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001138 for(j=0; j<pEList->nExpr; j++){
1139 char *zAs = pEList->a[j].zName;
1140 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh4f07e5f2007-05-14 11:34:46 +00001141 Expr *pDup;
drh626a8792005-01-17 22:08:19 +00001142 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001143 assert( pExpr->pList==0 );
1144 assert( pExpr->pSelect==0 );
1145 pDup = sqlite3ExprDup(pEList->a[j].pExpr);
1146 if( pExpr->flags & EP_ExpCollate ){
1147 pDup->pColl = pExpr->pColl;
1148 pDup->flags |= EP_ExpCollate;
1149 }
1150 memcpy(pExpr, pDup, sizeof(*pExpr));
1151 sqliteFree(pDup);
drh15ccce12005-05-23 15:06:39 +00001152 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001153 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001154 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001155 }
1156 }
1157 }
1158
1159 /* Advance to the next name context. The loop will exit when either
1160 ** we have a match (cnt>0) or when we run out of name contexts.
1161 */
1162 if( cnt==0 ){
1163 pNC = pNC->pNext;
1164 }
drh8141f612004-01-25 22:44:58 +00001165 }
1166
1167 /*
1168 ** If X and Y are NULL (in other words if only the column name Z is
1169 ** supplied) and the value of Z is enclosed in double-quotes, then
1170 ** Z is a string literal if it doesn't match any column names. In that
1171 ** case, we need to return right away and not make any changes to
1172 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001173 **
1174 ** Because no reference was made to outer contexts, the pNC->nRef
1175 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001176 */
1177 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1178 sqliteFree(zCol);
1179 return 0;
1180 }
1181
1182 /*
1183 ** cnt==0 means there was not match. cnt>1 means there were two or
1184 ** more matches. Either way, we have an error.
1185 */
1186 if( cnt!=1 ){
1187 char *z = 0;
1188 char *zErr;
1189 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1190 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001191 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001192 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001193 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001194 }else{
1195 z = sqliteStrDup(zCol);
1196 }
danielk19774adee202004-05-08 08:23:19 +00001197 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001198 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001199 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001200 }
1201
drh51669862004-12-18 18:40:26 +00001202 /* If a column from a table in pSrcList is referenced, then record
1203 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1204 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1205 ** column number is greater than the number of bits in the bitmask
1206 ** then set the high-order bit of the bitmask.
1207 */
1208 if( pExpr->iColumn>=0 && pMatch!=0 ){
1209 int n = pExpr->iColumn;
1210 if( n>=sizeof(Bitmask)*8 ){
1211 n = sizeof(Bitmask)*8-1;
1212 }
1213 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001214 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001215 }
1216
danielk1977d5d56522005-03-16 12:15:20 +00001217lookupname_end:
drh8141f612004-01-25 22:44:58 +00001218 /* Clean up and return
1219 */
1220 sqliteFree(zDb);
1221 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001222 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001223 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001224 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001225 pExpr->pRight = 0;
1226 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001227lookupname_end_2:
1228 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001229 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001230 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001231 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001232 if( pMatch && !pMatch->pSelect ){
1233 pExpr->pTab = pMatch->pTab;
1234 }
drh15ccce12005-05-23 15:06:39 +00001235 /* Increment the nRef value on all name contexts from TopNC up to
1236 ** the point where the name matched. */
1237 for(;;){
1238 assert( pTopNC!=0 );
1239 pTopNC->nRef++;
1240 if( pTopNC==pNC ) break;
1241 pTopNC = pTopNC->pNext;
1242 }
1243 return 0;
1244 } else {
1245 return 1;
drh626a8792005-01-17 22:08:19 +00001246 }
drh8141f612004-01-25 22:44:58 +00001247}
1248
1249/*
drh626a8792005-01-17 22:08:19 +00001250** This routine is designed as an xFunc for walkExprTree().
1251**
drh73b211a2005-01-18 04:00:42 +00001252** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001253** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001254** the tree or 2 to abort the tree walk.
1255**
1256** This routine also does error checking and name resolution for
1257** function names. The operator for aggregate functions is changed
1258** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001259*/
1260static int nameResolverStep(void *pArg, Expr *pExpr){
1261 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001262 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001263
danielk1977b3bce662005-01-29 08:32:43 +00001264 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001265 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001266 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001267
drh626a8792005-01-17 22:08:19 +00001268 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1269 ExprSetProperty(pExpr, EP_Resolved);
1270#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001271 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1272 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001273 int i;
danielk1977f0113002006-01-24 12:09:17 +00001274 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001275 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1276 }
1277 }
1278#endif
1279 switch( pExpr->op ){
1280 /* Double-quoted strings (ex: "abc") are used as identifiers if
1281 ** possible. Otherwise they remain as strings. Single-quoted
1282 ** strings (ex: 'abc') are always string literals.
1283 */
1284 case TK_STRING: {
1285 if( pExpr->token.z[0]=='\'' ) break;
1286 /* Fall thru into the TK_ID case if this is a double-quoted string */
1287 }
1288 /* A lone identifier is the name of a column.
1289 */
1290 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001291 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1292 return 1;
1293 }
1294
1295 /* A table name and column name: ID.ID
1296 ** Or a database, table and column: ID.ID.ID
1297 */
1298 case TK_DOT: {
1299 Token *pColumn;
1300 Token *pTable;
1301 Token *pDb;
1302 Expr *pRight;
1303
danielk1977b3bce662005-01-29 08:32:43 +00001304 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001305 pRight = pExpr->pRight;
1306 if( pRight->op==TK_ID ){
1307 pDb = 0;
1308 pTable = &pExpr->pLeft->token;
1309 pColumn = &pRight->token;
1310 }else{
1311 assert( pRight->op==TK_DOT );
1312 pDb = &pExpr->pLeft->token;
1313 pTable = &pRight->pLeft->token;
1314 pColumn = &pRight->pRight->token;
1315 }
1316 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1317 return 1;
1318 }
1319
1320 /* Resolve function names
1321 */
drhb71090f2005-05-23 17:26:51 +00001322 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001323 case TK_FUNCTION: {
1324 ExprList *pList = pExpr->pList; /* The argument list */
1325 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1326 int no_such_func = 0; /* True if no such function exists */
1327 int wrong_num_args = 0; /* True if wrong number of arguments */
1328 int is_agg = 0; /* True if is an aggregate function */
1329 int i;
drh5169bbc2006-08-24 14:59:45 +00001330 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001331 int nId; /* Number of characters in function name */
1332 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001333 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001334 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001335
drh2646da72005-12-09 20:02:05 +00001336 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001337 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001338 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1339 if( pDef==0 ){
1340 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1341 if( pDef==0 ){
1342 no_such_func = 1;
1343 }else{
1344 wrong_num_args = 1;
1345 }
1346 }else{
1347 is_agg = pDef->xFunc==0;
1348 }
drh2fca7fe2006-11-23 11:59:13 +00001349#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001350 if( pDef ){
1351 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1352 if( auth!=SQLITE_OK ){
1353 if( auth==SQLITE_DENY ){
1354 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1355 pDef->zName);
1356 pNC->nErr++;
1357 }
1358 pExpr->op = TK_NULL;
1359 return 1;
1360 }
1361 }
drhb8b14212006-08-24 15:18:25 +00001362#endif
drh626a8792005-01-17 22:08:19 +00001363 if( is_agg && !pNC->allowAgg ){
1364 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1365 pNC->nErr++;
1366 is_agg = 0;
1367 }else if( no_such_func ){
1368 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1369 pNC->nErr++;
1370 }else if( wrong_num_args ){
1371 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1372 nId, zId);
1373 pNC->nErr++;
1374 }
1375 if( is_agg ){
1376 pExpr->op = TK_AGG_FUNCTION;
1377 pNC->hasAgg = 1;
1378 }
drh73b211a2005-01-18 04:00:42 +00001379 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001380 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001381 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001382 }
drh73b211a2005-01-18 04:00:42 +00001383 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001384 /* FIX ME: Compute pExpr->affinity based on the expected return
1385 ** type of the function
1386 */
1387 return is_agg;
1388 }
danielk1977b3bce662005-01-29 08:32:43 +00001389#ifndef SQLITE_OMIT_SUBQUERY
1390 case TK_SELECT:
1391 case TK_EXISTS:
1392#endif
1393 case TK_IN: {
1394 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001395 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001396#ifndef SQLITE_OMIT_CHECK
1397 if( pNC->isCheck ){
1398 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1399 }
1400#endif
danielk1977b3bce662005-01-29 08:32:43 +00001401 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1402 assert( pNC->nRef>=nRef );
1403 if( nRef!=pNC->nRef ){
1404 ExprSetProperty(pExpr, EP_VarSelect);
1405 }
1406 }
drh4284fb02005-11-03 12:33:28 +00001407 break;
danielk1977b3bce662005-01-29 08:32:43 +00001408 }
drh4284fb02005-11-03 12:33:28 +00001409#ifndef SQLITE_OMIT_CHECK
1410 case TK_VARIABLE: {
1411 if( pNC->isCheck ){
1412 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1413 }
1414 break;
1415 }
1416#endif
drh626a8792005-01-17 22:08:19 +00001417 }
1418 return 0;
1419}
1420
1421/*
drhcce7d172000-05-31 15:34:51 +00001422** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001423** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001424** index to the table in the table list and a column offset. The
1425** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1426** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001427** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001428** VDBE cursor number for a cursor that is pointing into the referenced
1429** table. The Expr.iColumn value is changed to the index of the column
1430** of the referenced table. The Expr.iColumn value for the special
1431** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1432** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001433**
drh626a8792005-01-17 22:08:19 +00001434** Also resolve function names and check the functions for proper
1435** usage. Make sure all function names are recognized and all functions
1436** have the correct number of arguments. Leave an error message
1437** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1438**
drh73b211a2005-01-18 04:00:42 +00001439** If the expression contains aggregate functions then set the EP_Agg
1440** property on the expression.
drh626a8792005-01-17 22:08:19 +00001441*/
danielk1977fc976062007-05-10 10:46:56 +00001442int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001443 NameContext *pNC, /* Namespace to resolve expressions in. */
1444 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001445){
drh13449892005-09-07 21:22:45 +00001446 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001447 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001448#if SQLITE_MAX_EXPR_DEPTH>0
1449 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1450 sqlite3ErrorMsg(pNC->pParse,
1451 "Expression tree is too large (maximum depth %d)",
1452 SQLITE_MAX_EXPR_DEPTH
1453 );
1454 return 1;
1455 }
1456 pNC->pParse->nHeight += pExpr->nHeight;
1457#endif
drh13449892005-09-07 21:22:45 +00001458 savedHasAgg = pNC->hasAgg;
1459 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001460 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001461#if SQLITE_MAX_EXPR_DEPTH>0
1462 pNC->pParse->nHeight -= pExpr->nHeight;
1463#endif
danielk1977b3bce662005-01-29 08:32:43 +00001464 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001465 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001466 }
drh13449892005-09-07 21:22:45 +00001467 if( pNC->hasAgg ){
1468 ExprSetProperty(pExpr, EP_Agg);
1469 }else if( savedHasAgg ){
1470 pNC->hasAgg = 1;
1471 }
drh73b211a2005-01-18 04:00:42 +00001472 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001473}
1474
drh1398ad32005-01-19 23:24:50 +00001475/*
1476** A pointer instance of this structure is used to pass information
1477** through walkExprTree into codeSubqueryStep().
1478*/
1479typedef struct QueryCoder QueryCoder;
1480struct QueryCoder {
1481 Parse *pParse; /* The parsing context */
1482 NameContext *pNC; /* Namespace of first enclosing query */
1483};
1484
drh626a8792005-01-17 22:08:19 +00001485
1486/*
drh9cbe6352005-11-29 03:13:21 +00001487** Generate code for scalar subqueries used as an expression
1488** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001489**
drh9cbe6352005-11-29 03:13:21 +00001490** (SELECT a FROM b) -- subquery
1491** EXISTS (SELECT a FROM b) -- EXISTS subquery
1492** x IN (4,5,11) -- IN operator with list on right-hand side
1493** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001494**
drh9cbe6352005-11-29 03:13:21 +00001495** The pExpr parameter describes the expression that contains the IN
1496** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001497*/
drh51522cd2005-01-20 13:36:19 +00001498#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001499void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001500 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001501 Vdbe *v = sqlite3GetVdbe(pParse);
1502 if( v==0 ) return;
1503
danielk1977fc976062007-05-10 10:46:56 +00001504
drh57dbd7b2005-07-08 18:25:26 +00001505 /* This code must be run in its entirety every time it is encountered
1506 ** if any of the following is true:
1507 **
1508 ** * The right-hand side is a correlated subquery
1509 ** * The right-hand side is an expression list containing variables
1510 ** * We are inside a trigger
1511 **
1512 ** If all of the above are false, then we can run this code just once
1513 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001514 */
1515 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1516 int mem = pParse->nMem++;
1517 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001518 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001519 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001520 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001521 }
1522
drhcce7d172000-05-31 15:34:51 +00001523 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001524 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001525 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001526 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001527 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001528
danielk1977bf3b7212004-05-18 10:06:24 +00001529 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001530
1531 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001532 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001533 ** filled with single-field index keys representing the results
1534 ** from the SELECT or the <exprlist>.
1535 **
1536 ** If the 'x' expression is a column value, or the SELECT...
1537 ** statement returns a column value, then the affinity of that
1538 ** column is used to build the index keys. If both 'x' and the
1539 ** SELECT... statement are columns, then numeric affinity is used
1540 ** if either column has NUMERIC or INTEGER affinity. If neither
1541 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1542 ** is used.
1543 */
1544 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001545 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001546 memset(&keyInfo, 0, sizeof(keyInfo));
1547 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001548 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001549
drhfef52082000-06-06 01:50:43 +00001550 if( pExpr->pSelect ){
1551 /* Case 1: expr IN (SELECT ...)
1552 **
danielk1977e014a832004-05-17 10:48:57 +00001553 ** Generate code to write the results of the select into the temporary
1554 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001555 */
danielk1977e014a832004-05-17 10:48:57 +00001556 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001557 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001558 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001559 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1560 return;
1561 }
drhbe5c89a2004-07-26 00:31:09 +00001562 pEList = pExpr->pSelect->pEList;
1563 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001564 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001565 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001566 }
drhfef52082000-06-06 01:50:43 +00001567 }else if( pExpr->pList ){
1568 /* Case 2: expr IN (exprlist)
1569 **
danielk1977e014a832004-05-17 10:48:57 +00001570 ** For each expression, build an index key from the evaluation and
1571 ** store it in the temporary table. If <expr> is a column, then use
1572 ** that columns affinity when building index keys. If <expr> is not
1573 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001574 */
danielk1977e014a832004-05-17 10:48:57 +00001575 int i;
drh57dbd7b2005-07-08 18:25:26 +00001576 ExprList *pList = pExpr->pList;
1577 struct ExprList_item *pItem;
1578
danielk1977e014a832004-05-17 10:48:57 +00001579 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001580 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001581 }
danielk19770202b292004-06-09 09:55:16 +00001582 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001583
1584 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001585 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1586 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001587
drh57dbd7b2005-07-08 18:25:26 +00001588 /* If the expression is not constant then we will need to
1589 ** disable the test that was generated above that makes sure
1590 ** this code only executes once. Because for a non-constant
1591 ** expression we need to rerun this code each time.
1592 */
drh6c30be82005-07-29 15:10:17 +00001593 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001594 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001595 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001596 }
danielk1977e014a832004-05-17 10:48:57 +00001597
1598 /* Evaluate the expression and insert it into the temp table */
1599 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001600 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001601 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001602 }
1603 }
danielk19770202b292004-06-09 09:55:16 +00001604 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001605 break;
drhfef52082000-06-06 01:50:43 +00001606 }
1607
drh51522cd2005-01-20 13:36:19 +00001608 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001609 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001610 /* This has to be a scalar SELECT. Generate code to put the
1611 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001612 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001613 */
drh2646da72005-12-09 20:02:05 +00001614 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001615 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001616 int iMem;
1617 int sop;
drh1398ad32005-01-19 23:24:50 +00001618
drhec7429a2005-10-06 16:53:14 +00001619 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001620 pSel = pExpr->pSelect;
1621 if( pExpr->op==TK_SELECT ){
1622 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001623 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1624 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001625 }else{
drh51522cd2005-01-20 13:36:19 +00001626 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001627 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1628 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001629 }
drhec7429a2005-10-06 16:53:14 +00001630 sqlite3ExprDelete(pSel->pLimit);
1631 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001632 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1633 return;
1634 }
danielk1977b3bce662005-01-29 08:32:43 +00001635 break;
drhcce7d172000-05-31 15:34:51 +00001636 }
1637 }
danielk1977b3bce662005-01-29 08:32:43 +00001638
drh57dbd7b2005-07-08 18:25:26 +00001639 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001640 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001641 }
danielk1977fc976062007-05-10 10:46:56 +00001642
danielk1977b3bce662005-01-29 08:32:43 +00001643 return;
drhcce7d172000-05-31 15:34:51 +00001644}
drh51522cd2005-01-20 13:36:19 +00001645#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001646
drhcce7d172000-05-31 15:34:51 +00001647/*
drhfec19aa2004-05-19 20:41:03 +00001648** Generate an instruction that will put the integer describe by
1649** text z[0..n-1] on the stack.
1650*/
1651static void codeInteger(Vdbe *v, const char *z, int n){
danielk1977c9cf9012007-05-30 10:36:47 +00001652 assert( z || sqlite3MallocFailed() );
1653 if( z ){
1654 int i;
1655 if( sqlite3GetInt32(z, &i) ){
1656 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1657 }else if( sqlite3FitsIn64Bits(z) ){
1658 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1659 }else{
1660 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1661 }
drhfec19aa2004-05-19 20:41:03 +00001662 }
1663}
1664
drh945498f2007-02-24 11:52:52 +00001665
1666/*
1667** Generate code that will extract the iColumn-th column from
1668** table pTab and push that column value on the stack. There
1669** is an open cursor to pTab in iTable. If iColumn<0 then
1670** code is generated that extracts the rowid.
1671*/
1672void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1673 if( iColumn<0 ){
1674 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1675 sqlite3VdbeAddOp(v, op, iTable, 0);
1676 }else if( pTab==0 ){
1677 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1678 }else{
1679 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1680 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1681 sqlite3ColumnDefault(v, pTab, iColumn);
1682#ifndef SQLITE_OMIT_FLOATING_POINT
1683 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1684 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1685 }
1686#endif
1687 }
1688}
1689
drhfec19aa2004-05-19 20:41:03 +00001690/*
drhcce7d172000-05-31 15:34:51 +00001691** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001692** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001693**
1694** This code depends on the fact that certain token values (ex: TK_EQ)
1695** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1696** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1697** the make process cause these values to align. Assert()s in the code
1698** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001699*/
danielk19774adee202004-05-08 08:23:19 +00001700void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001701 Vdbe *v = pParse->pVdbe;
1702 int op;
drhffe07b22005-11-03 00:41:17 +00001703 int stackChng = 1; /* Amount of change to stack depth */
1704
danielk19777977a172004-11-09 12:44:37 +00001705 if( v==0 ) return;
1706 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001707 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001708 return;
1709 }
drhf2bc0132004-10-04 13:19:23 +00001710 op = pExpr->op;
1711 switch( op ){
drh13449892005-09-07 21:22:45 +00001712 case TK_AGG_COLUMN: {
1713 AggInfo *pAggInfo = pExpr->pAggInfo;
1714 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1715 if( !pAggInfo->directMode ){
1716 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1717 break;
1718 }else if( pAggInfo->useSortingIdx ){
1719 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1720 pCol->iSorterColumn);
1721 break;
1722 }
1723 /* Otherwise, fall thru into the TK_COLUMN case */
1724 }
drh967e8b72000-06-21 13:59:10 +00001725 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001726 if( pExpr->iTable<0 ){
1727 /* This only happens when coding check constraints */
1728 assert( pParse->ckOffset>0 );
1729 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001730 }else{
drh945498f2007-02-24 11:52:52 +00001731 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001732 }
drhcce7d172000-05-31 15:34:51 +00001733 break;
1734 }
1735 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001736 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001737 break;
1738 }
1739 case TK_FLOAT:
1740 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001741 assert( TK_FLOAT==OP_Real );
1742 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001743 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001744 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001745 break;
1746 }
drhf0863fe2005-06-12 21:35:51 +00001747 case TK_NULL: {
1748 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1749 break;
1750 }
danielk19775338a5f2005-01-20 13:03:10 +00001751#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001752 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001753 int n;
1754 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001755 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001756 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001757 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001758 assert( n>=0 );
1759 if( n==0 ){
1760 z = "";
1761 }
1762 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001763 break;
1764 }
danielk19775338a5f2005-01-20 13:03:10 +00001765#endif
drh50457892003-09-06 01:10:47 +00001766 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001767 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001768 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001769 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001770 }
drh50457892003-09-06 01:10:47 +00001771 break;
1772 }
drh4e0cff62004-11-05 05:10:28 +00001773 case TK_REGISTER: {
1774 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1775 break;
1776 }
drh487e2622005-06-25 18:42:14 +00001777#ifndef SQLITE_OMIT_CAST
1778 case TK_CAST: {
1779 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001780 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001781 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001782 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001783 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1784 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1785 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1786 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1787 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1788 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1789 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001790 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001791 break;
1792 }
1793#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001794 case TK_LT:
1795 case TK_LE:
1796 case TK_GT:
1797 case TK_GE:
1798 case TK_NE:
1799 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001800 assert( TK_LT==OP_Lt );
1801 assert( TK_LE==OP_Le );
1802 assert( TK_GT==OP_Gt );
1803 assert( TK_GE==OP_Ge );
1804 assert( TK_EQ==OP_Eq );
1805 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001806 sqlite3ExprCode(pParse, pExpr->pLeft);
1807 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001808 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001809 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001810 break;
drhc9b84a12002-06-20 11:36:48 +00001811 }
drhcce7d172000-05-31 15:34:51 +00001812 case TK_AND:
1813 case TK_OR:
1814 case TK_PLUS:
1815 case TK_STAR:
1816 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001817 case TK_REM:
1818 case TK_BITAND:
1819 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001820 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001821 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001822 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001823 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001824 assert( TK_AND==OP_And );
1825 assert( TK_OR==OP_Or );
1826 assert( TK_PLUS==OP_Add );
1827 assert( TK_MINUS==OP_Subtract );
1828 assert( TK_REM==OP_Remainder );
1829 assert( TK_BITAND==OP_BitAnd );
1830 assert( TK_BITOR==OP_BitOr );
1831 assert( TK_SLASH==OP_Divide );
1832 assert( TK_LSHIFT==OP_ShiftLeft );
1833 assert( TK_RSHIFT==OP_ShiftRight );
1834 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001835 sqlite3ExprCode(pParse, pExpr->pLeft);
1836 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001837 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001838 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001839 break;
1840 }
drhcce7d172000-05-31 15:34:51 +00001841 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001842 Expr *pLeft = pExpr->pLeft;
1843 assert( pLeft );
1844 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1845 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001846 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001847 if( pLeft->op==TK_FLOAT ){
1848 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001849 }else{
drhfec19aa2004-05-19 20:41:03 +00001850 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001851 }
drh6e142f52000-06-08 13:36:40 +00001852 sqliteFree(z);
1853 break;
1854 }
drh1ccde152000-06-17 13:12:39 +00001855 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001856 }
drhbf4133c2001-10-13 02:59:08 +00001857 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001858 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001859 assert( TK_BITNOT==OP_BitNot );
1860 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001861 sqlite3ExprCode(pParse, pExpr->pLeft);
1862 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001863 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001864 break;
1865 }
1866 case TK_ISNULL:
1867 case TK_NOTNULL: {
1868 int dest;
drhf2bc0132004-10-04 13:19:23 +00001869 assert( TK_ISNULL==OP_IsNull );
1870 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001871 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1872 sqlite3ExprCode(pParse, pExpr->pLeft);
1873 dest = sqlite3VdbeCurrentAddr(v) + 2;
1874 sqlite3VdbeAddOp(v, op, 1, dest);
1875 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001876 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001877 break;
drhcce7d172000-05-31 15:34:51 +00001878 }
drh22827922000-06-06 17:27:05 +00001879 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001880 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001881 if( pInfo==0 ){
1882 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1883 &pExpr->span);
1884 }else{
1885 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1886 }
drh22827922000-06-06 17:27:05 +00001887 break;
1888 }
drhb71090f2005-05-23 17:26:51 +00001889 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001890 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001891 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001892 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001893 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001894 int nId;
1895 const char *zId;
drh13449892005-09-07 21:22:45 +00001896 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001897 int i;
danielk197714db2662006-01-09 16:12:04 +00001898 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001899 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001900 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001901 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001902 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001903 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001904 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001905#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001906 /* Possibly overload the function if the first argument is
1907 ** a virtual table column.
1908 **
1909 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1910 ** second argument, not the first, as the argument to test to
1911 ** see if it is a column in a virtual table. This is done because
1912 ** the left operand of infix functions (the operand we want to
1913 ** control overloading) ends up as the second argument to the
1914 ** function. The expression "A glob B" is equivalent to
1915 ** "glob(B,A). We want to use the A in "A glob B" to test
1916 ** for function overloading. But we use the B term in "glob(B,A)".
1917 */
drh6a03a1c2006-07-08 18:34:59 +00001918 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001919 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1920 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001921 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1922 }
1923#endif
danielk1977682f68b2004-06-05 10:22:17 +00001924 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001925 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001926 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001927 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001928 if( pDef->needCollSeq && !pColl ){
1929 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1930 }
1931 }
1932 if( pDef->needCollSeq ){
1933 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001934 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001935 }
drh13449892005-09-07 21:22:45 +00001936 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001937 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001938 break;
1939 }
drhfe2093d2005-01-20 22:48:47 +00001940#ifndef SQLITE_OMIT_SUBQUERY
1941 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001942 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001943 if( pExpr->iColumn==0 ){
1944 sqlite3CodeSubselect(pParse, pExpr);
1945 }
danielk19774adee202004-05-08 08:23:19 +00001946 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001947 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001948 break;
1949 }
drhfef52082000-06-06 01:50:43 +00001950 case TK_IN: {
1951 int addr;
drh94a11212004-09-25 13:12:14 +00001952 char affinity;
drhafa5f682006-01-30 14:36:59 +00001953 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001954 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001955
1956 /* Figure out the affinity to use to create a key from the results
1957 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001958 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001959 */
drh94a11212004-09-25 13:12:14 +00001960 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001961
danielk19774adee202004-05-08 08:23:19 +00001962 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00001963 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00001964
1965 /* Code the <expr> from "<expr> IN (...)". The temporary table
1966 ** pExpr->iTable contains the values that make up the (...) set.
1967 */
danielk19774adee202004-05-08 08:23:19 +00001968 sqlite3ExprCode(pParse, pExpr->pLeft);
1969 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001970 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001971 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001972 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001973 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001974 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001975 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1976 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1977
drhfef52082000-06-06 01:50:43 +00001978 break;
1979 }
danielk197793758c82005-01-21 08:13:14 +00001980#endif
drhfef52082000-06-06 01:50:43 +00001981 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001982 Expr *pLeft = pExpr->pLeft;
1983 struct ExprList_item *pLItem = pExpr->pList->a;
1984 Expr *pRight = pLItem->pExpr;
1985 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001986 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001987 sqlite3ExprCode(pParse, pRight);
1988 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001989 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001990 pLItem++;
1991 pRight = pLItem->pExpr;
1992 sqlite3ExprCode(pParse, pRight);
1993 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001994 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001995 break;
1996 }
drh4f07e5f2007-05-14 11:34:46 +00001997 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00001998 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001999 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002000 break;
2001 }
drh17a7f8d2002-03-24 13:13:27 +00002002 case TK_CASE: {
2003 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002004 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002005 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002006 int i;
drhbe5c89a2004-07-26 00:31:09 +00002007 ExprList *pEList;
2008 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002009
2010 assert(pExpr->pList);
2011 assert((pExpr->pList->nExpr % 2) == 0);
2012 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002013 pEList = pExpr->pList;
2014 aListelem = pEList->a;
2015 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002016 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002017 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002018 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002019 }
drhf5905aa2002-05-26 20:54:33 +00002020 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002021 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002022 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002023 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002024 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2025 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002026 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002027 }else{
danielk19774adee202004-05-08 08:23:19 +00002028 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002029 }
drhbe5c89a2004-07-26 00:31:09 +00002030 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002032 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002033 }
drhf570f012002-05-31 15:51:25 +00002034 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002035 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002036 }
drh17a7f8d2002-03-24 13:13:27 +00002037 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002038 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002039 }else{
drhf0863fe2005-06-12 21:35:51 +00002040 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002041 }
danielk19774adee202004-05-08 08:23:19 +00002042 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002043 break;
2044 }
danielk19775338a5f2005-01-20 13:03:10 +00002045#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002046 case TK_RAISE: {
2047 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002048 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002049 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002050 return;
2051 }
drhad6d9462004-09-19 02:15:24 +00002052 if( pExpr->iColumn!=OE_Ignore ){
2053 assert( pExpr->iColumn==OE_Rollback ||
2054 pExpr->iColumn == OE_Abort ||
2055 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002056 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002057 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002058 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002059 } else {
drhad6d9462004-09-19 02:15:24 +00002060 assert( pExpr->iColumn == OE_Ignore );
2061 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2062 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2063 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002064 }
drhffe07b22005-11-03 00:41:17 +00002065 stackChng = 0;
2066 break;
drh17a7f8d2002-03-24 13:13:27 +00002067 }
danielk19775338a5f2005-01-20 13:03:10 +00002068#endif
drhffe07b22005-11-03 00:41:17 +00002069 }
2070
2071 if( pParse->ckOffset ){
2072 pParse->ckOffset += stackChng;
2073 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002074 }
drhcce7d172000-05-31 15:34:51 +00002075}
2076
danielk197793758c82005-01-21 08:13:14 +00002077#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002078/*
drh25303782004-12-07 15:41:48 +00002079** Generate code that evalutes the given expression and leaves the result
2080** on the stack. See also sqlite3ExprCode().
2081**
2082** This routine might also cache the result and modify the pExpr tree
2083** so that it will make use of the cached result on subsequent evaluations
2084** rather than evaluate the whole expression again. Trivial expressions are
2085** not cached. If the expression is cached, its result is stored in a
2086** memory location.
2087*/
2088void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2089 Vdbe *v = pParse->pVdbe;
2090 int iMem;
2091 int addr1, addr2;
2092 if( v==0 ) return;
2093 addr1 = sqlite3VdbeCurrentAddr(v);
2094 sqlite3ExprCode(pParse, pExpr);
2095 addr2 = sqlite3VdbeCurrentAddr(v);
2096 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2097 iMem = pExpr->iTable = pParse->nMem++;
2098 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2099 pExpr->op = TK_REGISTER;
2100 }
2101}
danielk197793758c82005-01-21 08:13:14 +00002102#endif
drh25303782004-12-07 15:41:48 +00002103
2104/*
drh268380c2004-02-25 13:47:31 +00002105** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002106** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002107**
2108** Return the number of elements pushed onto the stack.
2109*/
danielk19774adee202004-05-08 08:23:19 +00002110int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002111 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002112 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002113){
2114 struct ExprList_item *pItem;
2115 int i, n;
drh268380c2004-02-25 13:47:31 +00002116 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002117 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002118 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002119 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002120 }
drhf9b596e2004-05-26 16:54:42 +00002121 return n;
drh268380c2004-02-25 13:47:31 +00002122}
2123
2124/*
drhcce7d172000-05-31 15:34:51 +00002125** Generate code for a boolean expression such that a jump is made
2126** to the label "dest" if the expression is true but execution
2127** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002128**
2129** If the expression evaluates to NULL (neither true nor false), then
2130** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002131**
2132** This code depends on the fact that certain token values (ex: TK_EQ)
2133** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2134** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2135** the make process cause these values to align. Assert()s in the code
2136** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002137*/
danielk19774adee202004-05-08 08:23:19 +00002138void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002139 Vdbe *v = pParse->pVdbe;
2140 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002141 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002142 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002143 op = pExpr->op;
2144 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002145 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002146 int d2 = sqlite3VdbeMakeLabel(v);
2147 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2148 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2149 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002150 break;
2151 }
2152 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002153 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2154 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002155 break;
2156 }
2157 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002158 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002159 break;
2160 }
2161 case TK_LT:
2162 case TK_LE:
2163 case TK_GT:
2164 case TK_GE:
2165 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002166 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002167 assert( TK_LT==OP_Lt );
2168 assert( TK_LE==OP_Le );
2169 assert( TK_GT==OP_Gt );
2170 assert( TK_GE==OP_Ge );
2171 assert( TK_EQ==OP_Eq );
2172 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002173 sqlite3ExprCode(pParse, pExpr->pLeft);
2174 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002175 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002176 break;
2177 }
2178 case TK_ISNULL:
2179 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002180 assert( TK_ISNULL==OP_IsNull );
2181 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002182 sqlite3ExprCode(pParse, pExpr->pLeft);
2183 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002184 break;
2185 }
drhfef52082000-06-06 01:50:43 +00002186 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002187 /* The expression "x BETWEEN y AND z" is implemented as:
2188 **
2189 ** 1 IF (x < y) GOTO 3
2190 ** 2 IF (x <= z) GOTO <dest>
2191 ** 3 ...
2192 */
drhf5905aa2002-05-26 20:54:33 +00002193 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002194 Expr *pLeft = pExpr->pLeft;
2195 Expr *pRight = pExpr->pList->a[0].pExpr;
2196 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002197 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002198 sqlite3ExprCode(pParse, pRight);
2199 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002200
drhbe5c89a2004-07-26 00:31:09 +00002201 pRight = pExpr->pList->a[1].pExpr;
2202 sqlite3ExprCode(pParse, pRight);
2203 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002204
danielk19774adee202004-05-08 08:23:19 +00002205 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002206 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002207 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002208 break;
2209 }
drhcce7d172000-05-31 15:34:51 +00002210 default: {
danielk19774adee202004-05-08 08:23:19 +00002211 sqlite3ExprCode(pParse, pExpr);
2212 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002213 break;
2214 }
2215 }
drhffe07b22005-11-03 00:41:17 +00002216 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002217}
2218
2219/*
drh66b89c82000-11-28 20:47:17 +00002220** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002221** to the label "dest" if the expression is false but execution
2222** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002223**
2224** If the expression evaluates to NULL (neither true nor false) then
2225** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002226*/
danielk19774adee202004-05-08 08:23:19 +00002227void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002228 Vdbe *v = pParse->pVdbe;
2229 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002230 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002231 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002232
2233 /* The value of pExpr->op and op are related as follows:
2234 **
2235 ** pExpr->op op
2236 ** --------- ----------
2237 ** TK_ISNULL OP_NotNull
2238 ** TK_NOTNULL OP_IsNull
2239 ** TK_NE OP_Eq
2240 ** TK_EQ OP_Ne
2241 ** TK_GT OP_Le
2242 ** TK_LE OP_Gt
2243 ** TK_GE OP_Lt
2244 ** TK_LT OP_Ge
2245 **
2246 ** For other values of pExpr->op, op is undefined and unused.
2247 ** The value of TK_ and OP_ constants are arranged such that we
2248 ** can compute the mapping above using the following expression.
2249 ** Assert()s verify that the computation is correct.
2250 */
2251 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2252
2253 /* Verify correct alignment of TK_ and OP_ constants
2254 */
2255 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2256 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2257 assert( pExpr->op!=TK_NE || op==OP_Eq );
2258 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2259 assert( pExpr->op!=TK_LT || op==OP_Ge );
2260 assert( pExpr->op!=TK_LE || op==OP_Gt );
2261 assert( pExpr->op!=TK_GT || op==OP_Le );
2262 assert( pExpr->op!=TK_GE || op==OP_Lt );
2263
drhcce7d172000-05-31 15:34:51 +00002264 switch( pExpr->op ){
2265 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002266 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2267 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002268 break;
2269 }
2270 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002271 int d2 = sqlite3VdbeMakeLabel(v);
2272 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2273 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2274 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002275 break;
2276 }
2277 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002278 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002279 break;
2280 }
2281 case TK_LT:
2282 case TK_LE:
2283 case TK_GT:
2284 case TK_GE:
2285 case TK_NE:
2286 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002287 sqlite3ExprCode(pParse, pExpr->pLeft);
2288 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002289 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002290 break;
2291 }
drhcce7d172000-05-31 15:34:51 +00002292 case TK_ISNULL:
2293 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002294 sqlite3ExprCode(pParse, pExpr->pLeft);
2295 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002296 break;
2297 }
drhfef52082000-06-06 01:50:43 +00002298 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002299 /* The expression is "x BETWEEN y AND z". It is implemented as:
2300 **
2301 ** 1 IF (x >= y) GOTO 3
2302 ** 2 GOTO <dest>
2303 ** 3 IF (x > z) GOTO <dest>
2304 */
drhfef52082000-06-06 01:50:43 +00002305 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002306 Expr *pLeft = pExpr->pLeft;
2307 Expr *pRight = pExpr->pList->a[0].pExpr;
2308 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002309 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002310 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002311 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002312 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2313
danielk19774adee202004-05-08 08:23:19 +00002314 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2315 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002316 pRight = pExpr->pList->a[1].pExpr;
2317 sqlite3ExprCode(pParse, pRight);
2318 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002319 break;
2320 }
drhcce7d172000-05-31 15:34:51 +00002321 default: {
danielk19774adee202004-05-08 08:23:19 +00002322 sqlite3ExprCode(pParse, pExpr);
2323 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002324 break;
2325 }
2326 }
drhffe07b22005-11-03 00:41:17 +00002327 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002328}
drh22827922000-06-06 17:27:05 +00002329
2330/*
2331** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2332** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002333**
2334** Sometimes this routine will return FALSE even if the two expressions
2335** really are equivalent. If we cannot prove that the expressions are
2336** identical, we return FALSE just to be safe. So if this routine
2337** returns false, then you do not really know for certain if the two
2338** expressions are the same. But if you get a TRUE return, then you
2339** can be sure the expressions are the same. In the places where
2340** this routine is used, it does not hurt to get an extra FALSE - that
2341** just might result in some slightly slower code. But returning
2342** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002343*/
danielk19774adee202004-05-08 08:23:19 +00002344int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002345 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002346 if( pA==0||pB==0 ){
2347 return pB==pA;
drh22827922000-06-06 17:27:05 +00002348 }
2349 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002350 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002351 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2352 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002353 if( pA->pList ){
2354 if( pB->pList==0 ) return 0;
2355 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2356 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002357 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002358 return 0;
2359 }
2360 }
2361 }else if( pB->pList ){
2362 return 0;
2363 }
2364 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002365 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002366 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002367 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002368 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002369 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2370 return 0;
2371 }
drh22827922000-06-06 17:27:05 +00002372 }
2373 return 1;
2374}
2375
drh13449892005-09-07 21:22:45 +00002376
drh22827922000-06-06 17:27:05 +00002377/*
drh13449892005-09-07 21:22:45 +00002378** Add a new element to the pAggInfo->aCol[] array. Return the index of
2379** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002380*/
drh13449892005-09-07 21:22:45 +00002381static int addAggInfoColumn(AggInfo *pInfo){
2382 int i;
drhcf643722007-03-27 13:36:37 +00002383 pInfo->aCol = sqlite3ArrayAllocate(
2384 pInfo->aCol,
2385 sizeof(pInfo->aCol[0]),
2386 3,
2387 &pInfo->nColumn,
2388 &pInfo->nColumnAlloc,
2389 &i
2390 );
drh13449892005-09-07 21:22:45 +00002391 return i;
2392}
2393
2394/*
2395** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2396** the new element. Return a negative number if malloc fails.
2397*/
2398static int addAggInfoFunc(AggInfo *pInfo){
2399 int i;
drhcf643722007-03-27 13:36:37 +00002400 pInfo->aFunc = sqlite3ArrayAllocate(
2401 pInfo->aFunc,
2402 sizeof(pInfo->aFunc[0]),
2403 3,
2404 &pInfo->nFunc,
2405 &pInfo->nFuncAlloc,
2406 &i
2407 );
drh13449892005-09-07 21:22:45 +00002408 return i;
2409}
drh22827922000-06-06 17:27:05 +00002410
2411/*
drh626a8792005-01-17 22:08:19 +00002412** This is an xFunc for walkExprTree() used to implement
2413** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2414** for additional information.
drh22827922000-06-06 17:27:05 +00002415**
drh626a8792005-01-17 22:08:19 +00002416** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002417*/
drh626a8792005-01-17 22:08:19 +00002418static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002419 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002420 NameContext *pNC = (NameContext *)pArg;
2421 Parse *pParse = pNC->pParse;
2422 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002423 AggInfo *pAggInfo = pNC->pAggInfo;
2424
drh22827922000-06-06 17:27:05 +00002425
drh22827922000-06-06 17:27:05 +00002426 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002427 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002428 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002429 /* Check to see if the column is in one of the tables in the FROM
2430 ** clause of the aggregate query */
2431 if( pSrcList ){
2432 struct SrcList_item *pItem = pSrcList->a;
2433 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2434 struct AggInfo_col *pCol;
2435 if( pExpr->iTable==pItem->iCursor ){
2436 /* If we reach this point, it means that pExpr refers to a table
2437 ** that is in the FROM clause of the aggregate query.
2438 **
2439 ** Make an entry for the column in pAggInfo->aCol[] if there
2440 ** is not an entry there already.
2441 */
drh7f906d62007-03-12 23:48:52 +00002442 int k;
drh13449892005-09-07 21:22:45 +00002443 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002444 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002445 if( pCol->iTable==pExpr->iTable &&
2446 pCol->iColumn==pExpr->iColumn ){
2447 break;
2448 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002449 }
drh7f906d62007-03-12 23:48:52 +00002450 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2451 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002452 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002453 pCol->iTable = pExpr->iTable;
2454 pCol->iColumn = pExpr->iColumn;
2455 pCol->iMem = pParse->nMem++;
2456 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002457 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002458 if( pAggInfo->pGroupBy ){
2459 int j, n;
2460 ExprList *pGB = pAggInfo->pGroupBy;
2461 struct ExprList_item *pTerm = pGB->a;
2462 n = pGB->nExpr;
2463 for(j=0; j<n; j++, pTerm++){
2464 Expr *pE = pTerm->pExpr;
2465 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2466 pE->iColumn==pExpr->iColumn ){
2467 pCol->iSorterColumn = j;
2468 break;
2469 }
2470 }
2471 }
2472 if( pCol->iSorterColumn<0 ){
2473 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2474 }
2475 }
2476 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2477 ** because it was there before or because we just created it).
2478 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2479 ** pAggInfo->aCol[] entry.
2480 */
2481 pExpr->pAggInfo = pAggInfo;
2482 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002483 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002484 break;
2485 } /* endif pExpr->iTable==pItem->iCursor */
2486 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002487 }
drh626a8792005-01-17 22:08:19 +00002488 return 1;
drh22827922000-06-06 17:27:05 +00002489 }
2490 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002491 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2492 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002493 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002494 /* Check to see if pExpr is a duplicate of another aggregate
2495 ** function that is already in the pAggInfo structure
2496 */
2497 struct AggInfo_func *pItem = pAggInfo->aFunc;
2498 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2499 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002500 break;
2501 }
drh22827922000-06-06 17:27:05 +00002502 }
drh13449892005-09-07 21:22:45 +00002503 if( i>=pAggInfo->nFunc ){
2504 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2505 */
danielk197714db2662006-01-09 16:12:04 +00002506 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002507 i = addAggInfoFunc(pAggInfo);
2508 if( i>=0 ){
2509 pItem = &pAggInfo->aFunc[i];
2510 pItem->pExpr = pExpr;
2511 pItem->iMem = pParse->nMem++;
2512 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002513 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002514 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002515 if( pExpr->flags & EP_Distinct ){
2516 pItem->iDistinct = pParse->nTab++;
2517 }else{
2518 pItem->iDistinct = -1;
2519 }
drh13449892005-09-07 21:22:45 +00002520 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002521 }
drh13449892005-09-07 21:22:45 +00002522 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2523 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002524 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002525 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002526 return 1;
drh22827922000-06-06 17:27:05 +00002527 }
drh22827922000-06-06 17:27:05 +00002528 }
2529 }
drh13449892005-09-07 21:22:45 +00002530
2531 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2532 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2533 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2534 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002535 if( pExpr->pSelect ){
2536 pNC->nDepth++;
2537 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2538 pNC->nDepth--;
2539 }
drh626a8792005-01-17 22:08:19 +00002540 return 0;
2541}
2542
2543/*
2544** Analyze the given expression looking for aggregate functions and
2545** for variables that need to be added to the pParse->aAgg[] array.
2546** Make additional entries to the pParse->aAgg[] array as necessary.
2547**
2548** This routine should only be called after the expression has been
2549** analyzed by sqlite3ExprResolveNames().
2550**
2551** If errors are seen, leave an error message in zErrMsg and return
2552** the number of errors.
2553*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002554int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2555 int nErr = pNC->pParse->nErr;
2556 walkExprTree(pExpr, analyzeAggregate, pNC);
2557 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002558}
drh5d9a4af2005-08-30 00:54:01 +00002559
2560/*
2561** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2562** expression list. Return the number of errors.
2563**
2564** If an error is found, the analysis is cut short.
2565*/
2566int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2567 struct ExprList_item *pItem;
2568 int i;
2569 int nErr = 0;
2570 if( pList ){
2571 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2572 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2573 }
2574 }
2575 return nErr;
2576}