blob: b2ff41a79fb418ab20198cdb368eb4a489db796e [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**
danielk1977edaaec22007-06-15 16:37:29 +000015** $Id: expr.c,v 1.298 2007/06/15 16:37:29 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
57 CollSeq *pColl;
58 if( pExpr==0 ) return 0;
59 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
60 if( pColl ){
61 pExpr->pColl = pColl;
62 pExpr->flags |= EP_ExpCollate;
63 }
64 return pExpr;
65}
66
67/*
danielk19770202b292004-06-09 09:55:16 +000068** Return the default collation sequence for the expression pExpr. If
69** there is no default collation type, return 0.
70*/
danielk19777cedc8d2004-06-10 10:50:08 +000071CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
72 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000073 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000074 pColl = pExpr->pColl;
drh4f07e5f2007-05-14 11:34:46 +000075 if( pExpr->op==TK_CAST && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000076 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000077 }
78 }
danielk19777cedc8d2004-06-10 10:50:08 +000079 if( sqlite3CheckCollSeq(pParse, pColl) ){
80 pColl = 0;
81 }
82 return pColl;
danielk19770202b292004-06-09 09:55:16 +000083}
84
85/*
drh626a8792005-01-17 22:08:19 +000086** pExpr is an operand of a comparison operator. aff2 is the
87** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000088** type affinity that should be used for the comparison operator.
89*/
danielk1977e014a832004-05-17 10:48:57 +000090char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000091 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000092 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000093 /* Both sides of the comparison are columns. If one has numeric
94 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000095 */
drh8a512562005-11-14 22:29:05 +000096 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000097 return SQLITE_AFF_NUMERIC;
98 }else{
99 return SQLITE_AFF_NONE;
100 }
101 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000102 /* Neither side of the comparison is a column. Compare the
103 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000104 */
drh5f6a87b2004-07-19 00:39:45 +0000105 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000106 }else{
107 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000108 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000109 return (aff1 + aff2);
110 }
111}
112
drh53db1452004-05-20 13:54:53 +0000113/*
114** pExpr is a comparison operator. Return the type affinity that should
115** be applied to both operands prior to doing the comparison.
116*/
danielk1977e014a832004-05-17 10:48:57 +0000117static char comparisonAffinity(Expr *pExpr){
118 char aff;
119 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
120 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
121 pExpr->op==TK_NE );
122 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000123 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000124 if( pExpr->pRight ){
125 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
126 }
127 else if( pExpr->pSelect ){
128 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
129 }
130 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000131 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000132 }
133 return aff;
134}
135
136/*
137** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
138** idx_affinity is the affinity of an indexed column. Return true
139** if the index with affinity idx_affinity may be used to implement
140** the comparison in pExpr.
141*/
142int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
143 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000144 switch( aff ){
145 case SQLITE_AFF_NONE:
146 return 1;
147 case SQLITE_AFF_TEXT:
148 return idx_affinity==SQLITE_AFF_TEXT;
149 default:
150 return sqlite3IsNumericAffinity(idx_affinity);
151 }
danielk1977e014a832004-05-17 10:48:57 +0000152}
153
danielk1977a37cdde2004-05-16 11:15:36 +0000154/*
155** Return the P1 value that should be used for a binary comparison
156** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
157** If jumpIfNull is true, then set the low byte of the returned
158** P1 value to tell the opcode to jump if either expression
159** evaluates to NULL.
160*/
danielk1977e014a832004-05-17 10:48:57 +0000161static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000162 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000163 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000164}
165
drha2e00042002-01-22 03:13:42 +0000166/*
danielk19770202b292004-06-09 09:55:16 +0000167** Return a pointer to the collation sequence that should be used by
168** a binary comparison operator comparing pLeft and pRight.
169**
170** If the left hand expression has a collating sequence type, then it is
171** used. Otherwise the collation sequence for the right hand expression
172** is used, or the default (BINARY) if neither expression has a collating
173** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000174**
175** Argument pRight (but not pLeft) may be a null pointer. In this case,
176** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000177*/
danielk1977bcbb04e2007-05-29 12:11:29 +0000178CollSeq* sqlite3BinaryCompareCollSeq(
179 Parse *pParse,
180 Expr *pLeft,
181 Expr *pRight
182){
drhec41dda2007-02-07 13:09:45 +0000183 CollSeq *pColl;
184 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000185 if( pLeft->flags & EP_ExpCollate ){
186 assert( pLeft->pColl );
187 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000188 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000189 assert( pRight->pColl );
190 pColl = pRight->pColl;
191 }else{
192 pColl = sqlite3ExprCollSeq(pParse, pLeft);
193 if( !pColl ){
194 pColl = sqlite3ExprCollSeq(pParse, pRight);
195 }
danielk19770202b292004-06-09 09:55:16 +0000196 }
197 return pColl;
198}
199
200/*
drhbe5c89a2004-07-26 00:31:09 +0000201** Generate code for a comparison operator.
202*/
203static int codeCompare(
204 Parse *pParse, /* The parsing (and code generating) context */
205 Expr *pLeft, /* The left operand */
206 Expr *pRight, /* The right operand */
207 int opcode, /* The comparison opcode */
208 int dest, /* Jump here if true. */
209 int jumpIfNull /* If true, jump if either operand is NULL */
210){
211 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
danielk1977bcbb04e2007-05-29 12:11:29 +0000212 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000213 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000214}
215
216/*
drha76b5df2002-02-23 02:32:10 +0000217** Construct a new expression node and return a pointer to it. Memory
218** for this node is obtained from sqliteMalloc(). The calling function
219** is responsible for making sure the node eventually gets freed.
220*/
drhe4e72072004-11-23 01:47:30 +0000221Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000222 Expr *pNew;
223 pNew = sqliteMalloc( sizeof(Expr) );
224 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000225 /* When malloc fails, delete pLeft and pRight. Expressions passed to
226 ** this function must always be allocated with sqlite3Expr() for this
227 ** reason.
228 */
229 sqlite3ExprDelete(pLeft);
230 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000231 return 0;
232 }
233 pNew->op = op;
234 pNew->pLeft = pLeft;
235 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000236 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000237 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000238 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000239 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000240 }else if( pLeft ){
241 if( pRight ){
242 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000243 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000244 pNew->flags |= EP_ExpCollate;
245 pNew->pColl = pRight->pColl;
246 }
247 }
drh5ffb3ac2007-04-18 17:07:57 +0000248 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000249 pNew->flags |= EP_ExpCollate;
250 pNew->pColl = pLeft->pColl;
251 }
drha76b5df2002-02-23 02:32:10 +0000252 }
danielk1977fc976062007-05-10 10:46:56 +0000253
254 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000255 return pNew;
256}
257
258/*
drh206f3d92006-07-11 13:15:08 +0000259** Works like sqlite3Expr() but frees its pLeft and pRight arguments
260** if it fails due to a malloc problem.
261*/
262Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
263 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
264 if( pNew==0 ){
265 sqlite3ExprDelete(pLeft);
266 sqlite3ExprDelete(pRight);
267 }
268 return pNew;
269}
270
271/*
drh4e0cff62004-11-05 05:10:28 +0000272** When doing a nested parse, you can include terms in an expression
273** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000274** on the stack. "#0" means the top of the stack.
275** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000276**
277** This routine is called by the parser to deal with on of those terms.
278** It immediately generates code to store the value in a memory location.
279** The returns an expression that will code to extract the value from
280** that memory location as needed.
281*/
282Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
283 Vdbe *v = pParse->pVdbe;
284 Expr *p;
285 int depth;
drh4e0cff62004-11-05 05:10:28 +0000286 if( pParse->nested==0 ){
287 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drh4e05c832007-05-11 01:44:50 +0000288 return sqlite3Expr(TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000289 }
drhbb7ac002005-08-12 22:58:53 +0000290 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000291 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000292 if( p==0 ){
293 return 0; /* Malloc failed */
294 }
drh2646da72005-12-09 20:02:05 +0000295 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000296 p->iTable = pParse->nMem++;
297 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
298 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000299 return p;
300}
301
302/*
drh91bb0ee2004-09-01 03:06:34 +0000303** Join two expressions using an AND operator. If either expression is
304** NULL, then just return the other expression.
305*/
306Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
307 if( pLeft==0 ){
308 return pRight;
309 }else if( pRight==0 ){
310 return pLeft;
311 }else{
312 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
313 }
314}
315
316/*
drh6977fea2002-10-22 23:38:04 +0000317** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000318** text between the two given tokens.
319*/
danielk19774adee202004-05-08 08:23:19 +0000320void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000321 assert( pRight!=0 );
322 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000323 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000324 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000325 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000326 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000327 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000328 }else{
drh6977fea2002-10-22 23:38:04 +0000329 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000330 }
drha76b5df2002-02-23 02:32:10 +0000331 }
332}
333
334/*
335** Construct a new expression node for a function with multiple
336** arguments.
337*/
danielk19774adee202004-05-08 08:23:19 +0000338Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000339 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000340 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000341 pNew = sqliteMalloc( sizeof(Expr) );
342 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000343 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000344 return 0;
345 }
346 pNew->op = TK_FUNCTION;
347 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000348 assert( pToken->dyn==0 );
349 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000350 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000351
352 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000353 return pNew;
354}
355
356/*
drhfa6bc002004-09-07 16:19:52 +0000357** Assign a variable number to an expression that encodes a wildcard
358** in the original SQL statement.
359**
360** Wildcards consisting of a single "?" are assigned the next sequential
361** variable number.
362**
363** Wildcards of the form "?nnn" are assigned the number "nnn". We make
364** sure "nnn" is not too be to avoid a denial of service attack when
365** the SQL statement comes from an external source.
366**
367** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
368** as the previous instance of the same wildcard. Or if this is the first
369** instance of the wildcard, the next sequenial variable number is
370** assigned.
371*/
372void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
373 Token *pToken;
374 if( pExpr==0 ) return;
375 pToken = &pExpr->token;
376 assert( pToken->n>=1 );
377 assert( pToken->z!=0 );
378 assert( pToken->z[0]!=0 );
379 if( pToken->n==1 ){
380 /* Wildcard of the form "?". Assign the next variable number */
381 pExpr->iTable = ++pParse->nVar;
382 }else if( pToken->z[0]=='?' ){
383 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
384 ** use it as the variable number */
385 int i;
drh2646da72005-12-09 20:02:05 +0000386 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000387 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
388 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
389 SQLITE_MAX_VARIABLE_NUMBER);
390 }
391 if( i>pParse->nVar ){
392 pParse->nVar = i;
393 }
394 }else{
395 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
396 ** number as the prior appearance of the same name, or if the name
397 ** has never appeared before, reuse the same variable number
398 */
399 int i, n;
400 n = pToken->n;
401 for(i=0; i<pParse->nVarExpr; i++){
402 Expr *pE;
403 if( (pE = pParse->apVarExpr[i])!=0
404 && pE->token.n==n
405 && memcmp(pE->token.z, pToken->z, n)==0 ){
406 pExpr->iTable = pE->iTable;
407 break;
408 }
409 }
410 if( i>=pParse->nVarExpr ){
411 pExpr->iTable = ++pParse->nVar;
412 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
413 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000414 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000415 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
416 }
danielk19779e128002006-01-18 16:51:35 +0000417 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000418 assert( pParse->apVarExpr!=0 );
419 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
420 }
421 }
422 }
danielk1977832b2662007-05-09 11:37:22 +0000423 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
424 sqlite3ErrorMsg(pParse, "too many SQL variables");
425 }
drhfa6bc002004-09-07 16:19:52 +0000426}
427
428/*
drha2e00042002-01-22 03:13:42 +0000429** Recursively delete an expression tree.
430*/
danielk19774adee202004-05-08 08:23:19 +0000431void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000432 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000433 if( p->span.dyn ) sqliteFree((char*)p->span.z);
434 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000435 sqlite3ExprDelete(p->pLeft);
436 sqlite3ExprDelete(p->pRight);
437 sqlite3ExprListDelete(p->pList);
438 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000439 sqliteFree(p);
440}
441
drhd2687b72005-08-12 22:56:09 +0000442/*
443** The Expr.token field might be a string literal that is quoted.
444** If so, remove the quotation marks.
445*/
446void sqlite3DequoteExpr(Expr *p){
447 if( ExprHasAnyProperty(p, EP_Dequoted) ){
448 return;
449 }
450 ExprSetProperty(p, EP_Dequoted);
451 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000452 sqlite3TokenCopy(&p->token, &p->token);
453 }
454 sqlite3Dequote((char*)p->token.z);
455}
456
drha76b5df2002-02-23 02:32:10 +0000457
458/*
drhff78bd22002-02-27 01:47:11 +0000459** The following group of routines make deep copies of expressions,
460** expression lists, ID lists, and select statements. The copies can
461** be deleted (by being passed to their respective ...Delete() routines)
462** without effecting the originals.
463**
danielk19774adee202004-05-08 08:23:19 +0000464** The expression list, ID, and source lists return by sqlite3ExprListDup(),
465** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000466** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000467**
drhad3cab52002-05-24 02:04:32 +0000468** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000469*/
danielk19774adee202004-05-08 08:23:19 +0000470Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000471 Expr *pNew;
472 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000473 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000474 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000475 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000476 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000477 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000478 pNew->token.dyn = 1;
479 }else{
drh4efc4752004-01-16 15:55:37 +0000480 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000481 }
drh6977fea2002-10-22 23:38:04 +0000482 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000483 pNew->pLeft = sqlite3ExprDup(p->pLeft);
484 pNew->pRight = sqlite3ExprDup(p->pRight);
485 pNew->pList = sqlite3ExprListDup(p->pList);
486 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000487 return pNew;
488}
danielk19774adee202004-05-08 08:23:19 +0000489void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000490 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000491 if( pFrom->z ){
492 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000493 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000494 pTo->dyn = 1;
495 }else{
drh4b59ab52002-08-24 18:24:51 +0000496 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000497 }
498}
danielk19774adee202004-05-08 08:23:19 +0000499ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000500 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000501 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000502 int i;
503 if( p==0 ) return 0;
504 pNew = sqliteMalloc( sizeof(*pNew) );
505 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000506 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000507 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000508 if( pItem==0 ){
509 sqliteFree(pNew);
510 return 0;
511 }
drh145716b2004-09-24 12:24:06 +0000512 pOldItem = p->a;
513 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000514 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000515 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000516 if( pOldExpr->span.z!=0 && pNewExpr ){
517 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000518 ** expression list. The logic in SELECT processing that determines
519 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000520 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000521 }
drh1f3e9052002-10-31 00:09:39 +0000522 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000523 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000524 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000525 pItem->zName = sqliteStrDup(pOldItem->zName);
526 pItem->sortOrder = pOldItem->sortOrder;
527 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000528 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000529 }
530 return pNew;
531}
danielk197793758c82005-01-21 08:13:14 +0000532
533/*
534** If cursors, triggers, views and subqueries are all omitted from
535** the build, then none of the following routines, except for
536** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
537** called with a NULL argument.
538*/
danielk19776a67fe82005-02-04 04:07:16 +0000539#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
540 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000541SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000542 SrcList *pNew;
543 int i;
drh113088e2003-03-20 01:16:58 +0000544 int nByte;
drhad3cab52002-05-24 02:04:32 +0000545 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000546 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000547 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000548 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000549 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000550 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000551 struct SrcList_item *pNewItem = &pNew->a[i];
552 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000553 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000554 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
555 pNewItem->zName = sqliteStrDup(pOldItem->zName);
556 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
557 pNewItem->jointype = pOldItem->jointype;
558 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000559 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000560 pTab = pNewItem->pTab = pOldItem->pTab;
561 if( pTab ){
562 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000563 }
danielk19774adee202004-05-08 08:23:19 +0000564 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
565 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
566 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000567 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000568 }
569 return pNew;
570}
danielk19774adee202004-05-08 08:23:19 +0000571IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000572 IdList *pNew;
573 int i;
574 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000575 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000576 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000577 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000578 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000579 if( pNew->a==0 ){
580 sqliteFree(pNew);
581 return 0;
582 }
drhff78bd22002-02-27 01:47:11 +0000583 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000584 struct IdList_item *pNewItem = &pNew->a[i];
585 struct IdList_item *pOldItem = &p->a[i];
586 pNewItem->zName = sqliteStrDup(pOldItem->zName);
587 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000588 }
589 return pNew;
590}
danielk19774adee202004-05-08 08:23:19 +0000591Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000592 Select *pNew;
593 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000594 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000595 if( pNew==0 ) return 0;
596 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000597 pNew->pEList = sqlite3ExprListDup(p->pEList);
598 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
599 pNew->pWhere = sqlite3ExprDup(p->pWhere);
600 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
601 pNew->pHaving = sqlite3ExprDup(p->pHaving);
602 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000603 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000604 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000605 pNew->pLimit = sqlite3ExprDup(p->pLimit);
606 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000607 pNew->iLimit = -1;
608 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000609 pNew->isResolved = p->isResolved;
610 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000611 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000612 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000613 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000614 pNew->addrOpenEphm[0] = -1;
615 pNew->addrOpenEphm[1] = -1;
616 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000617 return pNew;
618}
danielk197793758c82005-01-21 08:13:14 +0000619#else
620Select *sqlite3SelectDup(Select *p){
621 assert( p==0 );
622 return 0;
623}
624#endif
drhff78bd22002-02-27 01:47:11 +0000625
626
627/*
drha76b5df2002-02-23 02:32:10 +0000628** Add a new element to the end of an expression list. If pList is
629** initially NULL, then create a new expression list.
630*/
danielk19774adee202004-05-08 08:23:19 +0000631ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000632 if( pList==0 ){
633 pList = sqliteMalloc( sizeof(ExprList) );
634 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000635 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000636 }
drh4efc4752004-01-16 15:55:37 +0000637 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000638 }
drh4305d102003-07-30 12:34:12 +0000639 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000640 struct ExprList_item *a;
641 int n = pList->nAlloc*2 + 4;
642 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
643 if( a==0 ){
644 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000645 }
danielk1977d5d56522005-03-16 12:15:20 +0000646 pList->a = a;
647 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000648 }
drh4efc4752004-01-16 15:55:37 +0000649 assert( pList->a!=0 );
650 if( pExpr || pName ){
651 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
652 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000653 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000654 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000655 }
656 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000657
658no_mem:
659 /* Avoid leaking memory if malloc has failed. */
660 sqlite3ExprDelete(pExpr);
661 sqlite3ExprListDelete(pList);
662 return 0;
drha76b5df2002-02-23 02:32:10 +0000663}
664
665/*
danielk19777a15a4b2007-05-08 17:54:43 +0000666** If the expression list pEList contains more than iLimit elements,
667** leave an error message in pParse.
668*/
669void sqlite3ExprListCheckLength(
670 Parse *pParse,
671 ExprList *pEList,
672 int iLimit,
673 const char *zObject
674){
danielk1977b4fc6792007-05-08 18:04:46 +0000675 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000676 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
677 }
678}
679
danielk1977fc976062007-05-10 10:46:56 +0000680
681#if SQLITE_MAX_EXPR_DEPTH>0
682/* The following three functions, heightOfExpr(), heightOfExprList()
683** and heightOfSelect(), are used to determine the maximum height
684** of any expression tree referenced by the structure passed as the
685** first argument.
686**
687** If this maximum height is greater than the current value pointed
688** to by pnHeight, the second parameter, then set *pnHeight to that
689** value.
690*/
691static void heightOfExpr(Expr *p, int *pnHeight){
692 if( p ){
693 if( p->nHeight>*pnHeight ){
694 *pnHeight = p->nHeight;
695 }
696 }
697}
698static void heightOfExprList(ExprList *p, int *pnHeight){
699 if( p ){
700 int i;
701 for(i=0; i<p->nExpr; i++){
702 heightOfExpr(p->a[i].pExpr, pnHeight);
703 }
704 }
705}
706static void heightOfSelect(Select *p, int *pnHeight){
707 if( p ){
708 heightOfExpr(p->pWhere, pnHeight);
709 heightOfExpr(p->pHaving, pnHeight);
710 heightOfExpr(p->pLimit, pnHeight);
711 heightOfExpr(p->pOffset, pnHeight);
712 heightOfExprList(p->pEList, pnHeight);
713 heightOfExprList(p->pGroupBy, pnHeight);
714 heightOfExprList(p->pOrderBy, pnHeight);
715 heightOfSelect(p->pPrior, pnHeight);
716 }
717}
718
719/*
720** Set the Expr.nHeight variable in the structure passed as an
721** argument. An expression with no children, Expr.pList or
722** Expr.pSelect member has a height of 1. Any other expression
723** has a height equal to the maximum height of any other
724** referenced Expr plus one.
725*/
726void sqlite3ExprSetHeight(Expr *p){
727 int nHeight = 0;
728 heightOfExpr(p->pLeft, &nHeight);
729 heightOfExpr(p->pRight, &nHeight);
730 heightOfExprList(p->pList, &nHeight);
731 heightOfSelect(p->pSelect, &nHeight);
732 p->nHeight = nHeight + 1;
733}
734
735/*
736** Return the maximum height of any expression tree referenced
737** by the select statement passed as an argument.
738*/
739int sqlite3SelectExprHeight(Select *p){
740 int nHeight = 0;
741 heightOfSelect(p, &nHeight);
742 return nHeight;
743}
744#endif
745
danielk19777a15a4b2007-05-08 17:54:43 +0000746/*
drha76b5df2002-02-23 02:32:10 +0000747** Delete an entire expression list.
748*/
danielk19774adee202004-05-08 08:23:19 +0000749void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000750 int i;
drhbe5c89a2004-07-26 00:31:09 +0000751 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000752 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000753 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
754 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000755 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
756 sqlite3ExprDelete(pItem->pExpr);
757 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000758 }
759 sqliteFree(pList->a);
760 sqliteFree(pList);
761}
762
763/*
drh626a8792005-01-17 22:08:19 +0000764** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000765**
drh626a8792005-01-17 22:08:19 +0000766** The return value from xFunc determines whether the tree walk continues.
767** 0 means continue walking the tree. 1 means do not walk children
768** of the current node but continue with siblings. 2 means abandon
769** the tree walk completely.
770**
771** The return value from this routine is 1 to abandon the tree walk
772** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000773**
774** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000775*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000776static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000777static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000778 int rc;
779 if( pExpr==0 ) return 0;
780 rc = (*xFunc)(pArg, pExpr);
781 if( rc==0 ){
782 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
783 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000784 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000785 }
786 return rc>1;
787}
788
789/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000790** Call walkExprTree() for every expression in list p.
791*/
792static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
793 int i;
794 struct ExprList_item *pItem;
795 if( !p ) return 0;
796 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
797 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
798 }
799 return 0;
800}
801
802/*
803** Call walkExprTree() for every expression in Select p, not including
804** expressions that are part of sub-selects in any FROM clause or the LIMIT
805** or OFFSET expressions..
806*/
807static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
808 walkExprList(p->pEList, xFunc, pArg);
809 walkExprTree(p->pWhere, xFunc, pArg);
810 walkExprList(p->pGroupBy, xFunc, pArg);
811 walkExprTree(p->pHaving, xFunc, pArg);
812 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000813 if( p->pPrior ){
814 walkSelectExpr(p->pPrior, xFunc, pArg);
815 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000816 return 0;
817}
818
819
820/*
drh626a8792005-01-17 22:08:19 +0000821** This routine is designed as an xFunc for walkExprTree().
822**
823** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000824** at pExpr that the expression that contains pExpr is not a constant
825** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
826** If pExpr does does not disqualify the expression from being a constant
827** then do nothing.
828**
829** After walking the whole tree, if no nodes are found that disqualify
830** the expression as constant, then we assume the whole expression
831** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000832*/
833static int exprNodeIsConstant(void *pArg, Expr *pExpr){
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 }
danielk1977edaaec22007-06-15 16:37:29 +00001150 if( pExpr->span.dyn ) sqliteFree((char*)pExpr->span.z);
1151 if( pExpr->token.dyn ) sqliteFree((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001152 memcpy(pExpr, pDup, sizeof(*pExpr));
1153 sqliteFree(pDup);
drh15ccce12005-05-23 15:06:39 +00001154 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001155 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001156 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001157 }
1158 }
1159 }
1160
1161 /* Advance to the next name context. The loop will exit when either
1162 ** we have a match (cnt>0) or when we run out of name contexts.
1163 */
1164 if( cnt==0 ){
1165 pNC = pNC->pNext;
1166 }
drh8141f612004-01-25 22:44:58 +00001167 }
1168
1169 /*
1170 ** If X and Y are NULL (in other words if only the column name Z is
1171 ** supplied) and the value of Z is enclosed in double-quotes, then
1172 ** Z is a string literal if it doesn't match any column names. In that
1173 ** case, we need to return right away and not make any changes to
1174 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001175 **
1176 ** Because no reference was made to outer contexts, the pNC->nRef
1177 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001178 */
1179 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1180 sqliteFree(zCol);
1181 return 0;
1182 }
1183
1184 /*
1185 ** cnt==0 means there was not match. cnt>1 means there were two or
1186 ** more matches. Either way, we have an error.
1187 */
1188 if( cnt!=1 ){
1189 char *z = 0;
1190 char *zErr;
1191 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1192 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001193 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001194 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001195 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001196 }else{
1197 z = sqliteStrDup(zCol);
1198 }
danielk19774adee202004-05-08 08:23:19 +00001199 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001200 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001201 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001202 }
1203
drh51669862004-12-18 18:40:26 +00001204 /* If a column from a table in pSrcList is referenced, then record
1205 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1206 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1207 ** column number is greater than the number of bits in the bitmask
1208 ** then set the high-order bit of the bitmask.
1209 */
1210 if( pExpr->iColumn>=0 && pMatch!=0 ){
1211 int n = pExpr->iColumn;
1212 if( n>=sizeof(Bitmask)*8 ){
1213 n = sizeof(Bitmask)*8-1;
1214 }
1215 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001216 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001217 }
1218
danielk1977d5d56522005-03-16 12:15:20 +00001219lookupname_end:
drh8141f612004-01-25 22:44:58 +00001220 /* Clean up and return
1221 */
1222 sqliteFree(zDb);
1223 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001224 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001225 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001226 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001227 pExpr->pRight = 0;
1228 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001229lookupname_end_2:
1230 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001231 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001232 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001233 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001234 if( pMatch && !pMatch->pSelect ){
1235 pExpr->pTab = pMatch->pTab;
1236 }
drh15ccce12005-05-23 15:06:39 +00001237 /* Increment the nRef value on all name contexts from TopNC up to
1238 ** the point where the name matched. */
1239 for(;;){
1240 assert( pTopNC!=0 );
1241 pTopNC->nRef++;
1242 if( pTopNC==pNC ) break;
1243 pTopNC = pTopNC->pNext;
1244 }
1245 return 0;
1246 } else {
1247 return 1;
drh626a8792005-01-17 22:08:19 +00001248 }
drh8141f612004-01-25 22:44:58 +00001249}
1250
1251/*
drh626a8792005-01-17 22:08:19 +00001252** This routine is designed as an xFunc for walkExprTree().
1253**
drh73b211a2005-01-18 04:00:42 +00001254** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001255** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001256** the tree or 2 to abort the tree walk.
1257**
1258** This routine also does error checking and name resolution for
1259** function names. The operator for aggregate functions is changed
1260** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001261*/
1262static int nameResolverStep(void *pArg, Expr *pExpr){
1263 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001264 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001265
danielk1977b3bce662005-01-29 08:32:43 +00001266 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001267 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001268 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001269
drh626a8792005-01-17 22:08:19 +00001270 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1271 ExprSetProperty(pExpr, EP_Resolved);
1272#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001273 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1274 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001275 int i;
danielk1977f0113002006-01-24 12:09:17 +00001276 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001277 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1278 }
1279 }
1280#endif
1281 switch( pExpr->op ){
1282 /* Double-quoted strings (ex: "abc") are used as identifiers if
1283 ** possible. Otherwise they remain as strings. Single-quoted
1284 ** strings (ex: 'abc') are always string literals.
1285 */
1286 case TK_STRING: {
1287 if( pExpr->token.z[0]=='\'' ) break;
1288 /* Fall thru into the TK_ID case if this is a double-quoted string */
1289 }
1290 /* A lone identifier is the name of a column.
1291 */
1292 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001293 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1294 return 1;
1295 }
1296
1297 /* A table name and column name: ID.ID
1298 ** Or a database, table and column: ID.ID.ID
1299 */
1300 case TK_DOT: {
1301 Token *pColumn;
1302 Token *pTable;
1303 Token *pDb;
1304 Expr *pRight;
1305
danielk1977b3bce662005-01-29 08:32:43 +00001306 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001307 pRight = pExpr->pRight;
1308 if( pRight->op==TK_ID ){
1309 pDb = 0;
1310 pTable = &pExpr->pLeft->token;
1311 pColumn = &pRight->token;
1312 }else{
1313 assert( pRight->op==TK_DOT );
1314 pDb = &pExpr->pLeft->token;
1315 pTable = &pRight->pLeft->token;
1316 pColumn = &pRight->pRight->token;
1317 }
1318 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1319 return 1;
1320 }
1321
1322 /* Resolve function names
1323 */
drhb71090f2005-05-23 17:26:51 +00001324 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001325 case TK_FUNCTION: {
1326 ExprList *pList = pExpr->pList; /* The argument list */
1327 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1328 int no_such_func = 0; /* True if no such function exists */
1329 int wrong_num_args = 0; /* True if wrong number of arguments */
1330 int is_agg = 0; /* True if is an aggregate function */
1331 int i;
drh5169bbc2006-08-24 14:59:45 +00001332 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001333 int nId; /* Number of characters in function name */
1334 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001335 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001336 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001337
drh2646da72005-12-09 20:02:05 +00001338 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001339 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001340 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1341 if( pDef==0 ){
1342 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1343 if( pDef==0 ){
1344 no_such_func = 1;
1345 }else{
1346 wrong_num_args = 1;
1347 }
1348 }else{
1349 is_agg = pDef->xFunc==0;
1350 }
drh2fca7fe2006-11-23 11:59:13 +00001351#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001352 if( pDef ){
1353 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1354 if( auth!=SQLITE_OK ){
1355 if( auth==SQLITE_DENY ){
1356 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1357 pDef->zName);
1358 pNC->nErr++;
1359 }
1360 pExpr->op = TK_NULL;
1361 return 1;
1362 }
1363 }
drhb8b14212006-08-24 15:18:25 +00001364#endif
drh626a8792005-01-17 22:08:19 +00001365 if( is_agg && !pNC->allowAgg ){
1366 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1367 pNC->nErr++;
1368 is_agg = 0;
1369 }else if( no_such_func ){
1370 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1371 pNC->nErr++;
1372 }else if( wrong_num_args ){
1373 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1374 nId, zId);
1375 pNC->nErr++;
1376 }
1377 if( is_agg ){
1378 pExpr->op = TK_AGG_FUNCTION;
1379 pNC->hasAgg = 1;
1380 }
drh73b211a2005-01-18 04:00:42 +00001381 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001382 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001383 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001384 }
drh73b211a2005-01-18 04:00:42 +00001385 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001386 /* FIX ME: Compute pExpr->affinity based on the expected return
1387 ** type of the function
1388 */
1389 return is_agg;
1390 }
danielk1977b3bce662005-01-29 08:32:43 +00001391#ifndef SQLITE_OMIT_SUBQUERY
1392 case TK_SELECT:
1393 case TK_EXISTS:
1394#endif
1395 case TK_IN: {
1396 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001397 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001398#ifndef SQLITE_OMIT_CHECK
1399 if( pNC->isCheck ){
1400 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1401 }
1402#endif
danielk1977b3bce662005-01-29 08:32:43 +00001403 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1404 assert( pNC->nRef>=nRef );
1405 if( nRef!=pNC->nRef ){
1406 ExprSetProperty(pExpr, EP_VarSelect);
1407 }
1408 }
drh4284fb02005-11-03 12:33:28 +00001409 break;
danielk1977b3bce662005-01-29 08:32:43 +00001410 }
drh4284fb02005-11-03 12:33:28 +00001411#ifndef SQLITE_OMIT_CHECK
1412 case TK_VARIABLE: {
1413 if( pNC->isCheck ){
1414 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1415 }
1416 break;
1417 }
1418#endif
drh626a8792005-01-17 22:08:19 +00001419 }
1420 return 0;
1421}
1422
1423/*
drhcce7d172000-05-31 15:34:51 +00001424** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001425** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001426** index to the table in the table list and a column offset. The
1427** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1428** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001429** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001430** VDBE cursor number for a cursor that is pointing into the referenced
1431** table. The Expr.iColumn value is changed to the index of the column
1432** of the referenced table. The Expr.iColumn value for the special
1433** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1434** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001435**
drh626a8792005-01-17 22:08:19 +00001436** Also resolve function names and check the functions for proper
1437** usage. Make sure all function names are recognized and all functions
1438** have the correct number of arguments. Leave an error message
1439** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1440**
drh73b211a2005-01-18 04:00:42 +00001441** If the expression contains aggregate functions then set the EP_Agg
1442** property on the expression.
drh626a8792005-01-17 22:08:19 +00001443*/
danielk1977fc976062007-05-10 10:46:56 +00001444int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001445 NameContext *pNC, /* Namespace to resolve expressions in. */
1446 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001447){
drh13449892005-09-07 21:22:45 +00001448 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001449 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001450#if SQLITE_MAX_EXPR_DEPTH>0
1451 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1452 sqlite3ErrorMsg(pNC->pParse,
1453 "Expression tree is too large (maximum depth %d)",
1454 SQLITE_MAX_EXPR_DEPTH
1455 );
1456 return 1;
1457 }
1458 pNC->pParse->nHeight += pExpr->nHeight;
1459#endif
drh13449892005-09-07 21:22:45 +00001460 savedHasAgg = pNC->hasAgg;
1461 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001462 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001463#if SQLITE_MAX_EXPR_DEPTH>0
1464 pNC->pParse->nHeight -= pExpr->nHeight;
1465#endif
danielk1977b3bce662005-01-29 08:32:43 +00001466 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001467 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001468 }
drh13449892005-09-07 21:22:45 +00001469 if( pNC->hasAgg ){
1470 ExprSetProperty(pExpr, EP_Agg);
1471 }else if( savedHasAgg ){
1472 pNC->hasAgg = 1;
1473 }
drh73b211a2005-01-18 04:00:42 +00001474 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001475}
1476
drh1398ad32005-01-19 23:24:50 +00001477/*
1478** A pointer instance of this structure is used to pass information
1479** through walkExprTree into codeSubqueryStep().
1480*/
1481typedef struct QueryCoder QueryCoder;
1482struct QueryCoder {
1483 Parse *pParse; /* The parsing context */
1484 NameContext *pNC; /* Namespace of first enclosing query */
1485};
1486
drh626a8792005-01-17 22:08:19 +00001487
1488/*
drh9cbe6352005-11-29 03:13:21 +00001489** Generate code for scalar subqueries used as an expression
1490** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001491**
drh9cbe6352005-11-29 03:13:21 +00001492** (SELECT a FROM b) -- subquery
1493** EXISTS (SELECT a FROM b) -- EXISTS subquery
1494** x IN (4,5,11) -- IN operator with list on right-hand side
1495** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001496**
drh9cbe6352005-11-29 03:13:21 +00001497** The pExpr parameter describes the expression that contains the IN
1498** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001499*/
drh51522cd2005-01-20 13:36:19 +00001500#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001501void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001502 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001503 Vdbe *v = sqlite3GetVdbe(pParse);
1504 if( v==0 ) return;
1505
danielk1977fc976062007-05-10 10:46:56 +00001506
drh57dbd7b2005-07-08 18:25:26 +00001507 /* This code must be run in its entirety every time it is encountered
1508 ** if any of the following is true:
1509 **
1510 ** * The right-hand side is a correlated subquery
1511 ** * The right-hand side is an expression list containing variables
1512 ** * We are inside a trigger
1513 **
1514 ** If all of the above are false, then we can run this code just once
1515 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001516 */
1517 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1518 int mem = pParse->nMem++;
1519 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001520 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001521 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001522 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001523 }
1524
drhcce7d172000-05-31 15:34:51 +00001525 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001526 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001527 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001528 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001529 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001530
danielk1977bf3b7212004-05-18 10:06:24 +00001531 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001532
1533 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001534 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001535 ** filled with single-field index keys representing the results
1536 ** from the SELECT or the <exprlist>.
1537 **
1538 ** If the 'x' expression is a column value, or the SELECT...
1539 ** statement returns a column value, then the affinity of that
1540 ** column is used to build the index keys. If both 'x' and the
1541 ** SELECT... statement are columns, then numeric affinity is used
1542 ** if either column has NUMERIC or INTEGER affinity. If neither
1543 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1544 ** is used.
1545 */
1546 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001547 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001548 memset(&keyInfo, 0, sizeof(keyInfo));
1549 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001550 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001551
drhfef52082000-06-06 01:50:43 +00001552 if( pExpr->pSelect ){
1553 /* Case 1: expr IN (SELECT ...)
1554 **
danielk1977e014a832004-05-17 10:48:57 +00001555 ** Generate code to write the results of the select into the temporary
1556 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001557 */
danielk1977e014a832004-05-17 10:48:57 +00001558 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001559 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001560 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001561 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1562 return;
1563 }
drhbe5c89a2004-07-26 00:31:09 +00001564 pEList = pExpr->pSelect->pEList;
1565 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001566 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001567 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001568 }
drhfef52082000-06-06 01:50:43 +00001569 }else if( pExpr->pList ){
1570 /* Case 2: expr IN (exprlist)
1571 **
danielk1977e014a832004-05-17 10:48:57 +00001572 ** For each expression, build an index key from the evaluation and
1573 ** store it in the temporary table. If <expr> is a column, then use
1574 ** that columns affinity when building index keys. If <expr> is not
1575 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001576 */
danielk1977e014a832004-05-17 10:48:57 +00001577 int i;
drh57dbd7b2005-07-08 18:25:26 +00001578 ExprList *pList = pExpr->pList;
1579 struct ExprList_item *pItem;
1580
danielk1977e014a832004-05-17 10:48:57 +00001581 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001582 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001583 }
danielk19770202b292004-06-09 09:55:16 +00001584 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001585
1586 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001587 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1588 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001589
drh57dbd7b2005-07-08 18:25:26 +00001590 /* If the expression is not constant then we will need to
1591 ** disable the test that was generated above that makes sure
1592 ** this code only executes once. Because for a non-constant
1593 ** expression we need to rerun this code each time.
1594 */
drh6c30be82005-07-29 15:10:17 +00001595 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001596 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001597 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001598 }
danielk1977e014a832004-05-17 10:48:57 +00001599
1600 /* Evaluate the expression and insert it into the temp table */
1601 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001602 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001603 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001604 }
1605 }
danielk19770202b292004-06-09 09:55:16 +00001606 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001607 break;
drhfef52082000-06-06 01:50:43 +00001608 }
1609
drh51522cd2005-01-20 13:36:19 +00001610 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001611 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001612 /* This has to be a scalar SELECT. Generate code to put the
1613 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001614 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001615 */
drh2646da72005-12-09 20:02:05 +00001616 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001617 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001618 int iMem;
1619 int sop;
drh1398ad32005-01-19 23:24:50 +00001620
drhec7429a2005-10-06 16:53:14 +00001621 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001622 pSel = pExpr->pSelect;
1623 if( pExpr->op==TK_SELECT ){
1624 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001625 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1626 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001627 }else{
drh51522cd2005-01-20 13:36:19 +00001628 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001629 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1630 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001631 }
drhec7429a2005-10-06 16:53:14 +00001632 sqlite3ExprDelete(pSel->pLimit);
1633 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001634 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1635 return;
1636 }
danielk1977b3bce662005-01-29 08:32:43 +00001637 break;
drhcce7d172000-05-31 15:34:51 +00001638 }
1639 }
danielk1977b3bce662005-01-29 08:32:43 +00001640
drh57dbd7b2005-07-08 18:25:26 +00001641 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001642 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001643 }
danielk1977fc976062007-05-10 10:46:56 +00001644
danielk1977b3bce662005-01-29 08:32:43 +00001645 return;
drhcce7d172000-05-31 15:34:51 +00001646}
drh51522cd2005-01-20 13:36:19 +00001647#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001648
drhcce7d172000-05-31 15:34:51 +00001649/*
drhfec19aa2004-05-19 20:41:03 +00001650** Generate an instruction that will put the integer describe by
1651** text z[0..n-1] on the stack.
1652*/
1653static void codeInteger(Vdbe *v, const char *z, int n){
danielk1977c9cf9012007-05-30 10:36:47 +00001654 assert( z || sqlite3MallocFailed() );
1655 if( z ){
1656 int i;
1657 if( sqlite3GetInt32(z, &i) ){
1658 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1659 }else if( sqlite3FitsIn64Bits(z) ){
1660 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1661 }else{
1662 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1663 }
drhfec19aa2004-05-19 20:41:03 +00001664 }
1665}
1666
drh945498f2007-02-24 11:52:52 +00001667
1668/*
1669** Generate code that will extract the iColumn-th column from
1670** table pTab and push that column value on the stack. There
1671** is an open cursor to pTab in iTable. If iColumn<0 then
1672** code is generated that extracts the rowid.
1673*/
1674void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1675 if( iColumn<0 ){
1676 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1677 sqlite3VdbeAddOp(v, op, iTable, 0);
1678 }else if( pTab==0 ){
1679 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1680 }else{
1681 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1682 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1683 sqlite3ColumnDefault(v, pTab, iColumn);
1684#ifndef SQLITE_OMIT_FLOATING_POINT
1685 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1686 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1687 }
1688#endif
1689 }
1690}
1691
drhfec19aa2004-05-19 20:41:03 +00001692/*
drhcce7d172000-05-31 15:34:51 +00001693** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001694** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001695**
1696** This code depends on the fact that certain token values (ex: TK_EQ)
1697** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1698** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1699** the make process cause these values to align. Assert()s in the code
1700** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001701*/
danielk19774adee202004-05-08 08:23:19 +00001702void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001703 Vdbe *v = pParse->pVdbe;
1704 int op;
drhffe07b22005-11-03 00:41:17 +00001705 int stackChng = 1; /* Amount of change to stack depth */
1706
danielk19777977a172004-11-09 12:44:37 +00001707 if( v==0 ) return;
1708 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001709 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001710 return;
1711 }
drhf2bc0132004-10-04 13:19:23 +00001712 op = pExpr->op;
1713 switch( op ){
drh13449892005-09-07 21:22:45 +00001714 case TK_AGG_COLUMN: {
1715 AggInfo *pAggInfo = pExpr->pAggInfo;
1716 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1717 if( !pAggInfo->directMode ){
1718 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1719 break;
1720 }else if( pAggInfo->useSortingIdx ){
1721 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1722 pCol->iSorterColumn);
1723 break;
1724 }
1725 /* Otherwise, fall thru into the TK_COLUMN case */
1726 }
drh967e8b72000-06-21 13:59:10 +00001727 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001728 if( pExpr->iTable<0 ){
1729 /* This only happens when coding check constraints */
1730 assert( pParse->ckOffset>0 );
1731 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001732 }else{
drh945498f2007-02-24 11:52:52 +00001733 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001734 }
drhcce7d172000-05-31 15:34:51 +00001735 break;
1736 }
1737 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001738 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001739 break;
1740 }
1741 case TK_FLOAT:
1742 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001743 assert( TK_FLOAT==OP_Real );
1744 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001745 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001746 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001747 break;
1748 }
drhf0863fe2005-06-12 21:35:51 +00001749 case TK_NULL: {
1750 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1751 break;
1752 }
danielk19775338a5f2005-01-20 13:03:10 +00001753#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001754 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001755 int n;
1756 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001757 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001758 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001759 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001760 assert( n>=0 );
1761 if( n==0 ){
1762 z = "";
1763 }
1764 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001765 break;
1766 }
danielk19775338a5f2005-01-20 13:03:10 +00001767#endif
drh50457892003-09-06 01:10:47 +00001768 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001769 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001770 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001771 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001772 }
drh50457892003-09-06 01:10:47 +00001773 break;
1774 }
drh4e0cff62004-11-05 05:10:28 +00001775 case TK_REGISTER: {
1776 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1777 break;
1778 }
drh487e2622005-06-25 18:42:14 +00001779#ifndef SQLITE_OMIT_CAST
1780 case TK_CAST: {
1781 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001782 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001783 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001784 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001785 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1786 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1787 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1788 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1789 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1790 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1791 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001792 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001793 break;
1794 }
1795#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001796 case TK_LT:
1797 case TK_LE:
1798 case TK_GT:
1799 case TK_GE:
1800 case TK_NE:
1801 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001802 assert( TK_LT==OP_Lt );
1803 assert( TK_LE==OP_Le );
1804 assert( TK_GT==OP_Gt );
1805 assert( TK_GE==OP_Ge );
1806 assert( TK_EQ==OP_Eq );
1807 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001808 sqlite3ExprCode(pParse, pExpr->pLeft);
1809 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001810 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001811 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001812 break;
drhc9b84a12002-06-20 11:36:48 +00001813 }
drhcce7d172000-05-31 15:34:51 +00001814 case TK_AND:
1815 case TK_OR:
1816 case TK_PLUS:
1817 case TK_STAR:
1818 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001819 case TK_REM:
1820 case TK_BITAND:
1821 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001822 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001823 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001824 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001825 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001826 assert( TK_AND==OP_And );
1827 assert( TK_OR==OP_Or );
1828 assert( TK_PLUS==OP_Add );
1829 assert( TK_MINUS==OP_Subtract );
1830 assert( TK_REM==OP_Remainder );
1831 assert( TK_BITAND==OP_BitAnd );
1832 assert( TK_BITOR==OP_BitOr );
1833 assert( TK_SLASH==OP_Divide );
1834 assert( TK_LSHIFT==OP_ShiftLeft );
1835 assert( TK_RSHIFT==OP_ShiftRight );
1836 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001837 sqlite3ExprCode(pParse, pExpr->pLeft);
1838 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001839 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001840 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001841 break;
1842 }
drhcce7d172000-05-31 15:34:51 +00001843 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001844 Expr *pLeft = pExpr->pLeft;
1845 assert( pLeft );
1846 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1847 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001848 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001849 if( pLeft->op==TK_FLOAT ){
1850 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001851 }else{
drhfec19aa2004-05-19 20:41:03 +00001852 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001853 }
drh6e142f52000-06-08 13:36:40 +00001854 sqliteFree(z);
1855 break;
1856 }
drh1ccde152000-06-17 13:12:39 +00001857 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001858 }
drhbf4133c2001-10-13 02:59:08 +00001859 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001860 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001861 assert( TK_BITNOT==OP_BitNot );
1862 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001863 sqlite3ExprCode(pParse, pExpr->pLeft);
1864 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001865 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001866 break;
1867 }
1868 case TK_ISNULL:
1869 case TK_NOTNULL: {
1870 int dest;
drhf2bc0132004-10-04 13:19:23 +00001871 assert( TK_ISNULL==OP_IsNull );
1872 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001873 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1874 sqlite3ExprCode(pParse, pExpr->pLeft);
1875 dest = sqlite3VdbeCurrentAddr(v) + 2;
1876 sqlite3VdbeAddOp(v, op, 1, dest);
1877 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001878 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001879 break;
drhcce7d172000-05-31 15:34:51 +00001880 }
drh22827922000-06-06 17:27:05 +00001881 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001882 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001883 if( pInfo==0 ){
1884 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1885 &pExpr->span);
1886 }else{
1887 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1888 }
drh22827922000-06-06 17:27:05 +00001889 break;
1890 }
drhb71090f2005-05-23 17:26:51 +00001891 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001892 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001893 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001894 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001895 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001896 int nId;
1897 const char *zId;
drh13449892005-09-07 21:22:45 +00001898 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001899 int i;
danielk197714db2662006-01-09 16:12:04 +00001900 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001901 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001902 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001903 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001904 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001905 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001906 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001907#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001908 /* Possibly overload the function if the first argument is
1909 ** a virtual table column.
1910 **
1911 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1912 ** second argument, not the first, as the argument to test to
1913 ** see if it is a column in a virtual table. This is done because
1914 ** the left operand of infix functions (the operand we want to
1915 ** control overloading) ends up as the second argument to the
1916 ** function. The expression "A glob B" is equivalent to
1917 ** "glob(B,A). We want to use the A in "A glob B" to test
1918 ** for function overloading. But we use the B term in "glob(B,A)".
1919 */
drh6a03a1c2006-07-08 18:34:59 +00001920 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001921 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1922 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001923 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1924 }
1925#endif
danielk1977682f68b2004-06-05 10:22:17 +00001926 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001927 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001928 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001929 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001930 if( pDef->needCollSeq && !pColl ){
1931 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1932 }
1933 }
1934 if( pDef->needCollSeq ){
1935 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001936 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001937 }
drh13449892005-09-07 21:22:45 +00001938 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001939 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001940 break;
1941 }
drhfe2093d2005-01-20 22:48:47 +00001942#ifndef SQLITE_OMIT_SUBQUERY
1943 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001944 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001945 if( pExpr->iColumn==0 ){
1946 sqlite3CodeSubselect(pParse, pExpr);
1947 }
danielk19774adee202004-05-08 08:23:19 +00001948 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001949 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001950 break;
1951 }
drhfef52082000-06-06 01:50:43 +00001952 case TK_IN: {
1953 int addr;
drh94a11212004-09-25 13:12:14 +00001954 char affinity;
drhafa5f682006-01-30 14:36:59 +00001955 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001956 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001957
1958 /* Figure out the affinity to use to create a key from the results
1959 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001960 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001961 */
drh94a11212004-09-25 13:12:14 +00001962 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001963
danielk19774adee202004-05-08 08:23:19 +00001964 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00001965 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00001966
1967 /* Code the <expr> from "<expr> IN (...)". The temporary table
1968 ** pExpr->iTable contains the values that make up the (...) set.
1969 */
danielk19774adee202004-05-08 08:23:19 +00001970 sqlite3ExprCode(pParse, pExpr->pLeft);
1971 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001972 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001973 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001974 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001975 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001976 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1978 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1979
drhfef52082000-06-06 01:50:43 +00001980 break;
1981 }
danielk197793758c82005-01-21 08:13:14 +00001982#endif
drhfef52082000-06-06 01:50:43 +00001983 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001984 Expr *pLeft = pExpr->pLeft;
1985 struct ExprList_item *pLItem = pExpr->pList->a;
1986 Expr *pRight = pLItem->pExpr;
1987 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001988 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001989 sqlite3ExprCode(pParse, pRight);
1990 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001991 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001992 pLItem++;
1993 pRight = pLItem->pExpr;
1994 sqlite3ExprCode(pParse, pRight);
1995 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001996 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001997 break;
1998 }
drh4f07e5f2007-05-14 11:34:46 +00001999 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002000 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002001 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002002 break;
2003 }
drh17a7f8d2002-03-24 13:13:27 +00002004 case TK_CASE: {
2005 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002006 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002007 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002008 int i;
drhbe5c89a2004-07-26 00:31:09 +00002009 ExprList *pEList;
2010 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002011
2012 assert(pExpr->pList);
2013 assert((pExpr->pList->nExpr % 2) == 0);
2014 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002015 pEList = pExpr->pList;
2016 aListelem = pEList->a;
2017 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002018 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002019 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002020 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002021 }
drhf5905aa2002-05-26 20:54:33 +00002022 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002023 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002024 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002025 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002026 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2027 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002028 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002029 }else{
danielk19774adee202004-05-08 08:23:19 +00002030 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002031 }
drhbe5c89a2004-07-26 00:31:09 +00002032 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002033 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002034 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002035 }
drhf570f012002-05-31 15:51:25 +00002036 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002037 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002038 }
drh17a7f8d2002-03-24 13:13:27 +00002039 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002040 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002041 }else{
drhf0863fe2005-06-12 21:35:51 +00002042 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002043 }
danielk19774adee202004-05-08 08:23:19 +00002044 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002045 break;
2046 }
danielk19775338a5f2005-01-20 13:03:10 +00002047#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002048 case TK_RAISE: {
2049 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002050 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002051 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002052 return;
2053 }
drhad6d9462004-09-19 02:15:24 +00002054 if( pExpr->iColumn!=OE_Ignore ){
2055 assert( pExpr->iColumn==OE_Rollback ||
2056 pExpr->iColumn == OE_Abort ||
2057 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002058 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002059 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002060 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002061 } else {
drhad6d9462004-09-19 02:15:24 +00002062 assert( pExpr->iColumn == OE_Ignore );
2063 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2064 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2065 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002066 }
drhffe07b22005-11-03 00:41:17 +00002067 stackChng = 0;
2068 break;
drh17a7f8d2002-03-24 13:13:27 +00002069 }
danielk19775338a5f2005-01-20 13:03:10 +00002070#endif
drhffe07b22005-11-03 00:41:17 +00002071 }
2072
2073 if( pParse->ckOffset ){
2074 pParse->ckOffset += stackChng;
2075 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002076 }
drhcce7d172000-05-31 15:34:51 +00002077}
2078
danielk197793758c82005-01-21 08:13:14 +00002079#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002080/*
drh25303782004-12-07 15:41:48 +00002081** Generate code that evalutes the given expression and leaves the result
2082** on the stack. See also sqlite3ExprCode().
2083**
2084** This routine might also cache the result and modify the pExpr tree
2085** so that it will make use of the cached result on subsequent evaluations
2086** rather than evaluate the whole expression again. Trivial expressions are
2087** not cached. If the expression is cached, its result is stored in a
2088** memory location.
2089*/
2090void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2091 Vdbe *v = pParse->pVdbe;
2092 int iMem;
2093 int addr1, addr2;
2094 if( v==0 ) return;
2095 addr1 = sqlite3VdbeCurrentAddr(v);
2096 sqlite3ExprCode(pParse, pExpr);
2097 addr2 = sqlite3VdbeCurrentAddr(v);
2098 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2099 iMem = pExpr->iTable = pParse->nMem++;
2100 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2101 pExpr->op = TK_REGISTER;
2102 }
2103}
danielk197793758c82005-01-21 08:13:14 +00002104#endif
drh25303782004-12-07 15:41:48 +00002105
2106/*
drh268380c2004-02-25 13:47:31 +00002107** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002108** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002109**
2110** Return the number of elements pushed onto the stack.
2111*/
danielk19774adee202004-05-08 08:23:19 +00002112int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002113 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002114 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002115){
2116 struct ExprList_item *pItem;
2117 int i, n;
drh268380c2004-02-25 13:47:31 +00002118 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002119 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002120 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002121 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002122 }
drhf9b596e2004-05-26 16:54:42 +00002123 return n;
drh268380c2004-02-25 13:47:31 +00002124}
2125
2126/*
drhcce7d172000-05-31 15:34:51 +00002127** Generate code for a boolean expression such that a jump is made
2128** to the label "dest" if the expression is true but execution
2129** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002130**
2131** If the expression evaluates to NULL (neither true nor false), then
2132** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002133**
2134** This code depends on the fact that certain token values (ex: TK_EQ)
2135** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2136** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2137** the make process cause these values to align. Assert()s in the code
2138** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002139*/
danielk19774adee202004-05-08 08:23:19 +00002140void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002141 Vdbe *v = pParse->pVdbe;
2142 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002143 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002144 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002145 op = pExpr->op;
2146 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002147 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002148 int d2 = sqlite3VdbeMakeLabel(v);
2149 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2150 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2151 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002152 break;
2153 }
2154 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002155 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2156 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002157 break;
2158 }
2159 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002160 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002161 break;
2162 }
2163 case TK_LT:
2164 case TK_LE:
2165 case TK_GT:
2166 case TK_GE:
2167 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002168 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002169 assert( TK_LT==OP_Lt );
2170 assert( TK_LE==OP_Le );
2171 assert( TK_GT==OP_Gt );
2172 assert( TK_GE==OP_Ge );
2173 assert( TK_EQ==OP_Eq );
2174 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002175 sqlite3ExprCode(pParse, pExpr->pLeft);
2176 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002177 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002178 break;
2179 }
2180 case TK_ISNULL:
2181 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002182 assert( TK_ISNULL==OP_IsNull );
2183 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002184 sqlite3ExprCode(pParse, pExpr->pLeft);
2185 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002186 break;
2187 }
drhfef52082000-06-06 01:50:43 +00002188 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002189 /* The expression "x BETWEEN y AND z" is implemented as:
2190 **
2191 ** 1 IF (x < y) GOTO 3
2192 ** 2 IF (x <= z) GOTO <dest>
2193 ** 3 ...
2194 */
drhf5905aa2002-05-26 20:54:33 +00002195 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002196 Expr *pLeft = pExpr->pLeft;
2197 Expr *pRight = pExpr->pList->a[0].pExpr;
2198 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002199 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002200 sqlite3ExprCode(pParse, pRight);
2201 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002202
drhbe5c89a2004-07-26 00:31:09 +00002203 pRight = pExpr->pList->a[1].pExpr;
2204 sqlite3ExprCode(pParse, pRight);
2205 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002206
danielk19774adee202004-05-08 08:23:19 +00002207 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002208 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002209 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002210 break;
2211 }
drhcce7d172000-05-31 15:34:51 +00002212 default: {
danielk19774adee202004-05-08 08:23:19 +00002213 sqlite3ExprCode(pParse, pExpr);
2214 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002215 break;
2216 }
2217 }
drhffe07b22005-11-03 00:41:17 +00002218 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002219}
2220
2221/*
drh66b89c82000-11-28 20:47:17 +00002222** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002223** to the label "dest" if the expression is false but execution
2224** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002225**
2226** If the expression evaluates to NULL (neither true nor false) then
2227** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002228*/
danielk19774adee202004-05-08 08:23:19 +00002229void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002230 Vdbe *v = pParse->pVdbe;
2231 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002232 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002233 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002234
2235 /* The value of pExpr->op and op are related as follows:
2236 **
2237 ** pExpr->op op
2238 ** --------- ----------
2239 ** TK_ISNULL OP_NotNull
2240 ** TK_NOTNULL OP_IsNull
2241 ** TK_NE OP_Eq
2242 ** TK_EQ OP_Ne
2243 ** TK_GT OP_Le
2244 ** TK_LE OP_Gt
2245 ** TK_GE OP_Lt
2246 ** TK_LT OP_Ge
2247 **
2248 ** For other values of pExpr->op, op is undefined and unused.
2249 ** The value of TK_ and OP_ constants are arranged such that we
2250 ** can compute the mapping above using the following expression.
2251 ** Assert()s verify that the computation is correct.
2252 */
2253 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2254
2255 /* Verify correct alignment of TK_ and OP_ constants
2256 */
2257 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2258 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2259 assert( pExpr->op!=TK_NE || op==OP_Eq );
2260 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2261 assert( pExpr->op!=TK_LT || op==OP_Ge );
2262 assert( pExpr->op!=TK_LE || op==OP_Gt );
2263 assert( pExpr->op!=TK_GT || op==OP_Le );
2264 assert( pExpr->op!=TK_GE || op==OP_Lt );
2265
drhcce7d172000-05-31 15:34:51 +00002266 switch( pExpr->op ){
2267 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002268 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2269 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002270 break;
2271 }
2272 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002273 int d2 = sqlite3VdbeMakeLabel(v);
2274 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2275 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2276 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002277 break;
2278 }
2279 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002280 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002281 break;
2282 }
2283 case TK_LT:
2284 case TK_LE:
2285 case TK_GT:
2286 case TK_GE:
2287 case TK_NE:
2288 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002289 sqlite3ExprCode(pParse, pExpr->pLeft);
2290 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002291 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002292 break;
2293 }
drhcce7d172000-05-31 15:34:51 +00002294 case TK_ISNULL:
2295 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002296 sqlite3ExprCode(pParse, pExpr->pLeft);
2297 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002298 break;
2299 }
drhfef52082000-06-06 01:50:43 +00002300 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002301 /* The expression is "x BETWEEN y AND z". It is implemented as:
2302 **
2303 ** 1 IF (x >= y) GOTO 3
2304 ** 2 GOTO <dest>
2305 ** 3 IF (x > z) GOTO <dest>
2306 */
drhfef52082000-06-06 01:50:43 +00002307 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002308 Expr *pLeft = pExpr->pLeft;
2309 Expr *pRight = pExpr->pList->a[0].pExpr;
2310 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002311 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002312 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002313 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002314 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2315
danielk19774adee202004-05-08 08:23:19 +00002316 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2317 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002318 pRight = pExpr->pList->a[1].pExpr;
2319 sqlite3ExprCode(pParse, pRight);
2320 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002321 break;
2322 }
drhcce7d172000-05-31 15:34:51 +00002323 default: {
danielk19774adee202004-05-08 08:23:19 +00002324 sqlite3ExprCode(pParse, pExpr);
2325 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002326 break;
2327 }
2328 }
drhffe07b22005-11-03 00:41:17 +00002329 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002330}
drh22827922000-06-06 17:27:05 +00002331
2332/*
2333** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2334** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002335**
2336** Sometimes this routine will return FALSE even if the two expressions
2337** really are equivalent. If we cannot prove that the expressions are
2338** identical, we return FALSE just to be safe. So if this routine
2339** returns false, then you do not really know for certain if the two
2340** expressions are the same. But if you get a TRUE return, then you
2341** can be sure the expressions are the same. In the places where
2342** this routine is used, it does not hurt to get an extra FALSE - that
2343** just might result in some slightly slower code. But returning
2344** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002345*/
danielk19774adee202004-05-08 08:23:19 +00002346int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002347 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002348 if( pA==0||pB==0 ){
2349 return pB==pA;
drh22827922000-06-06 17:27:05 +00002350 }
2351 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002352 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002353 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2354 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002355 if( pA->pList ){
2356 if( pB->pList==0 ) return 0;
2357 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2358 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002359 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002360 return 0;
2361 }
2362 }
2363 }else if( pB->pList ){
2364 return 0;
2365 }
2366 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002367 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002368 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002369 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002370 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002371 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2372 return 0;
2373 }
drh22827922000-06-06 17:27:05 +00002374 }
2375 return 1;
2376}
2377
drh13449892005-09-07 21:22:45 +00002378
drh22827922000-06-06 17:27:05 +00002379/*
drh13449892005-09-07 21:22:45 +00002380** Add a new element to the pAggInfo->aCol[] array. Return the index of
2381** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002382*/
drh13449892005-09-07 21:22:45 +00002383static int addAggInfoColumn(AggInfo *pInfo){
2384 int i;
drhcf643722007-03-27 13:36:37 +00002385 pInfo->aCol = sqlite3ArrayAllocate(
2386 pInfo->aCol,
2387 sizeof(pInfo->aCol[0]),
2388 3,
2389 &pInfo->nColumn,
2390 &pInfo->nColumnAlloc,
2391 &i
2392 );
drh13449892005-09-07 21:22:45 +00002393 return i;
2394}
2395
2396/*
2397** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2398** the new element. Return a negative number if malloc fails.
2399*/
2400static int addAggInfoFunc(AggInfo *pInfo){
2401 int i;
drhcf643722007-03-27 13:36:37 +00002402 pInfo->aFunc = sqlite3ArrayAllocate(
2403 pInfo->aFunc,
2404 sizeof(pInfo->aFunc[0]),
2405 3,
2406 &pInfo->nFunc,
2407 &pInfo->nFuncAlloc,
2408 &i
2409 );
drh13449892005-09-07 21:22:45 +00002410 return i;
2411}
drh22827922000-06-06 17:27:05 +00002412
2413/*
drh626a8792005-01-17 22:08:19 +00002414** This is an xFunc for walkExprTree() used to implement
2415** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2416** for additional information.
drh22827922000-06-06 17:27:05 +00002417**
drh626a8792005-01-17 22:08:19 +00002418** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002419*/
drh626a8792005-01-17 22:08:19 +00002420static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002421 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002422 NameContext *pNC = (NameContext *)pArg;
2423 Parse *pParse = pNC->pParse;
2424 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002425 AggInfo *pAggInfo = pNC->pAggInfo;
2426
drh22827922000-06-06 17:27:05 +00002427
drh22827922000-06-06 17:27:05 +00002428 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002429 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002430 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002431 /* Check to see if the column is in one of the tables in the FROM
2432 ** clause of the aggregate query */
2433 if( pSrcList ){
2434 struct SrcList_item *pItem = pSrcList->a;
2435 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2436 struct AggInfo_col *pCol;
2437 if( pExpr->iTable==pItem->iCursor ){
2438 /* If we reach this point, it means that pExpr refers to a table
2439 ** that is in the FROM clause of the aggregate query.
2440 **
2441 ** Make an entry for the column in pAggInfo->aCol[] if there
2442 ** is not an entry there already.
2443 */
drh7f906d62007-03-12 23:48:52 +00002444 int k;
drh13449892005-09-07 21:22:45 +00002445 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002446 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002447 if( pCol->iTable==pExpr->iTable &&
2448 pCol->iColumn==pExpr->iColumn ){
2449 break;
2450 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002451 }
drh7f906d62007-03-12 23:48:52 +00002452 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2453 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002454 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002455 pCol->iTable = pExpr->iTable;
2456 pCol->iColumn = pExpr->iColumn;
2457 pCol->iMem = pParse->nMem++;
2458 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002459 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002460 if( pAggInfo->pGroupBy ){
2461 int j, n;
2462 ExprList *pGB = pAggInfo->pGroupBy;
2463 struct ExprList_item *pTerm = pGB->a;
2464 n = pGB->nExpr;
2465 for(j=0; j<n; j++, pTerm++){
2466 Expr *pE = pTerm->pExpr;
2467 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2468 pE->iColumn==pExpr->iColumn ){
2469 pCol->iSorterColumn = j;
2470 break;
2471 }
2472 }
2473 }
2474 if( pCol->iSorterColumn<0 ){
2475 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2476 }
2477 }
2478 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2479 ** because it was there before or because we just created it).
2480 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2481 ** pAggInfo->aCol[] entry.
2482 */
2483 pExpr->pAggInfo = pAggInfo;
2484 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002485 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002486 break;
2487 } /* endif pExpr->iTable==pItem->iCursor */
2488 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002489 }
drh626a8792005-01-17 22:08:19 +00002490 return 1;
drh22827922000-06-06 17:27:05 +00002491 }
2492 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002493 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2494 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002495 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002496 /* Check to see if pExpr is a duplicate of another aggregate
2497 ** function that is already in the pAggInfo structure
2498 */
2499 struct AggInfo_func *pItem = pAggInfo->aFunc;
2500 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2501 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002502 break;
2503 }
drh22827922000-06-06 17:27:05 +00002504 }
drh13449892005-09-07 21:22:45 +00002505 if( i>=pAggInfo->nFunc ){
2506 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2507 */
danielk197714db2662006-01-09 16:12:04 +00002508 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002509 i = addAggInfoFunc(pAggInfo);
2510 if( i>=0 ){
2511 pItem = &pAggInfo->aFunc[i];
2512 pItem->pExpr = pExpr;
2513 pItem->iMem = pParse->nMem++;
2514 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002515 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002516 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002517 if( pExpr->flags & EP_Distinct ){
2518 pItem->iDistinct = pParse->nTab++;
2519 }else{
2520 pItem->iDistinct = -1;
2521 }
drh13449892005-09-07 21:22:45 +00002522 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002523 }
drh13449892005-09-07 21:22:45 +00002524 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2525 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002526 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002527 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002528 return 1;
drh22827922000-06-06 17:27:05 +00002529 }
drh22827922000-06-06 17:27:05 +00002530 }
2531 }
drh13449892005-09-07 21:22:45 +00002532
2533 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2534 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2535 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2536 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002537 if( pExpr->pSelect ){
2538 pNC->nDepth++;
2539 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2540 pNC->nDepth--;
2541 }
drh626a8792005-01-17 22:08:19 +00002542 return 0;
2543}
2544
2545/*
2546** Analyze the given expression looking for aggregate functions and
2547** for variables that need to be added to the pParse->aAgg[] array.
2548** Make additional entries to the pParse->aAgg[] array as necessary.
2549**
2550** This routine should only be called after the expression has been
2551** analyzed by sqlite3ExprResolveNames().
2552**
2553** If errors are seen, leave an error message in zErrMsg and return
2554** the number of errors.
2555*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002556int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2557 int nErr = pNC->pParse->nErr;
2558 walkExprTree(pExpr, analyzeAggregate, pNC);
2559 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002560}
drh5d9a4af2005-08-30 00:54:01 +00002561
2562/*
2563** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2564** expression list. Return the number of errors.
2565**
2566** If an error is found, the analysis is cut short.
2567*/
2568int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2569 struct ExprList_item *pItem;
2570 int i;
2571 int nErr = 0;
2572 if( pList ){
2573 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2574 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2575 }
2576 }
2577 return nErr;
2578}