blob: 39d928ad4598f4f2f3a5f239bdda168f42533588 [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**
drhed8a3bb2005-06-06 21:19:56 +000015** $Id: expr.c,v 1.205 2005/06/06 21:19:57 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh626a8792005-01-17 22:08:19 +000065** pExpr is an operand of a comparison operator. aff2 is the
66** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000067** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000186 /* When malloc fails, delete pLeft and pRight. Expressions passed to
187 ** this function must always be allocated with sqlite3Expr() for this
188 ** reason.
189 */
190 sqlite3ExprDelete(pLeft);
191 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000192 return 0;
193 }
194 pNew->op = op;
195 pNew->pLeft = pLeft;
196 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000197 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000198 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000199 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000200 pNew->span = pNew->token = *pToken;
201 }else if( pLeft && pRight ){
202 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000203 }
drha76b5df2002-02-23 02:32:10 +0000204 return pNew;
205}
206
207/*
drh4e0cff62004-11-05 05:10:28 +0000208** When doing a nested parse, you can include terms in an expression
209** that look like this: #0 #1 #2 ... These terms refer to elements
210** on the stack. "#0" (or just "#") means the top of the stack.
drh2958a4e2004-11-12 03:56:15 +0000211** "#1" means the next down on the stack. And so forth. #-1 means
212** memory location 0. #-2 means memory location 1. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000213**
214** This routine is called by the parser to deal with on of those terms.
215** It immediately generates code to store the value in a memory location.
216** The returns an expression that will code to extract the value from
217** that memory location as needed.
218*/
219Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
220 Vdbe *v = pParse->pVdbe;
221 Expr *p;
222 int depth;
223 if( v==0 ) return 0;
224 if( pParse->nested==0 ){
225 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
226 return 0;
227 }
228 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000229 if( p==0 ){
230 return 0; /* Malloc failed */
231 }
drh4e0cff62004-11-05 05:10:28 +0000232 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000233 if( depth>=0 ){
234 p->iTable = pParse->nMem++;
235 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
236 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
237 }else{
238 p->iTable = -1-depth;
239 }
drh4e0cff62004-11-05 05:10:28 +0000240 return p;
241}
242
243/*
drh91bb0ee2004-09-01 03:06:34 +0000244** Join two expressions using an AND operator. If either expression is
245** NULL, then just return the other expression.
246*/
247Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
248 if( pLeft==0 ){
249 return pRight;
250 }else if( pRight==0 ){
251 return pLeft;
252 }else{
253 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
254 }
255}
256
257/*
drh6977fea2002-10-22 23:38:04 +0000258** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000259** text between the two given tokens.
260*/
danielk19774adee202004-05-08 08:23:19 +0000261void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000262 assert( pRight!=0 );
263 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000264 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000265 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000266 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000267 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000268 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000269 }else{
drh6977fea2002-10-22 23:38:04 +0000270 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000271 }
drha76b5df2002-02-23 02:32:10 +0000272 }
273}
274
275/*
276** Construct a new expression node for a function with multiple
277** arguments.
278*/
danielk19774adee202004-05-08 08:23:19 +0000279Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000280 Expr *pNew;
281 pNew = sqliteMalloc( sizeof(Expr) );
282 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000283 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000284 return 0;
285 }
286 pNew->op = TK_FUNCTION;
287 pNew->pList = pList;
288 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000289 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000290 pNew->token = *pToken;
291 }else{
292 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000293 }
drh6977fea2002-10-22 23:38:04 +0000294 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000295 return pNew;
296}
297
298/*
drhfa6bc002004-09-07 16:19:52 +0000299** Assign a variable number to an expression that encodes a wildcard
300** in the original SQL statement.
301**
302** Wildcards consisting of a single "?" are assigned the next sequential
303** variable number.
304**
305** Wildcards of the form "?nnn" are assigned the number "nnn". We make
306** sure "nnn" is not too be to avoid a denial of service attack when
307** the SQL statement comes from an external source.
308**
309** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
310** as the previous instance of the same wildcard. Or if this is the first
311** instance of the wildcard, the next sequenial variable number is
312** assigned.
313*/
314void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
315 Token *pToken;
316 if( pExpr==0 ) return;
317 pToken = &pExpr->token;
318 assert( pToken->n>=1 );
319 assert( pToken->z!=0 );
320 assert( pToken->z[0]!=0 );
321 if( pToken->n==1 ){
322 /* Wildcard of the form "?". Assign the next variable number */
323 pExpr->iTable = ++pParse->nVar;
324 }else if( pToken->z[0]=='?' ){
325 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
326 ** use it as the variable number */
327 int i;
328 pExpr->iTable = i = atoi(&pToken->z[1]);
329 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
330 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
331 SQLITE_MAX_VARIABLE_NUMBER);
332 }
333 if( i>pParse->nVar ){
334 pParse->nVar = i;
335 }
336 }else{
337 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
338 ** number as the prior appearance of the same name, or if the name
339 ** has never appeared before, reuse the same variable number
340 */
341 int i, n;
342 n = pToken->n;
343 for(i=0; i<pParse->nVarExpr; i++){
344 Expr *pE;
345 if( (pE = pParse->apVarExpr[i])!=0
346 && pE->token.n==n
347 && memcmp(pE->token.z, pToken->z, n)==0 ){
348 pExpr->iTable = pE->iTable;
349 break;
350 }
351 }
352 if( i>=pParse->nVarExpr ){
353 pExpr->iTable = ++pParse->nVar;
354 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
355 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
356 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
357 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
358 }
359 if( !sqlite3_malloc_failed ){
360 assert( pParse->apVarExpr!=0 );
361 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
362 }
363 }
364 }
365}
366
367/*
drha2e00042002-01-22 03:13:42 +0000368** Recursively delete an expression tree.
369*/
danielk19774adee202004-05-08 08:23:19 +0000370void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000371 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000372 if( p->span.dyn ) sqliteFree((char*)p->span.z);
373 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3ExprDelete(p->pLeft);
375 sqlite3ExprDelete(p->pRight);
376 sqlite3ExprListDelete(p->pList);
377 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000378 sqliteFree(p);
379}
380
drha76b5df2002-02-23 02:32:10 +0000381
382/*
drhff78bd22002-02-27 01:47:11 +0000383** The following group of routines make deep copies of expressions,
384** expression lists, ID lists, and select statements. The copies can
385** be deleted (by being passed to their respective ...Delete() routines)
386** without effecting the originals.
387**
danielk19774adee202004-05-08 08:23:19 +0000388** The expression list, ID, and source lists return by sqlite3ExprListDup(),
389** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000390** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000391**
drhad3cab52002-05-24 02:04:32 +0000392** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000393*/
danielk19774adee202004-05-08 08:23:19 +0000394Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000395 Expr *pNew;
396 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000397 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000398 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000399 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000400 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000401 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000402 pNew->token.dyn = 1;
403 }else{
drh4efc4752004-01-16 15:55:37 +0000404 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000405 }
drh6977fea2002-10-22 23:38:04 +0000406 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000407 pNew->pLeft = sqlite3ExprDup(p->pLeft);
408 pNew->pRight = sqlite3ExprDup(p->pRight);
409 pNew->pList = sqlite3ExprListDup(p->pList);
410 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000411 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000412 return pNew;
413}
danielk19774adee202004-05-08 08:23:19 +0000414void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000415 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000416 if( pFrom->z ){
417 pTo->n = pFrom->n;
418 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
419 pTo->dyn = 1;
420 }else{
drh4b59ab52002-08-24 18:24:51 +0000421 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000422 }
423}
danielk19774adee202004-05-08 08:23:19 +0000424ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000425 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000426 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000427 int i;
428 if( p==0 ) return 0;
429 pNew = sqliteMalloc( sizeof(*pNew) );
430 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000431 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000432 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000433 if( pItem==0 ){
434 sqliteFree(pNew);
435 return 0;
436 }
drh145716b2004-09-24 12:24:06 +0000437 pOldItem = p->a;
438 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000439 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000440 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000441 if( pOldExpr->span.z!=0 && pNewExpr ){
442 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000443 ** expression list. The logic in SELECT processing that determines
444 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000446 }
drh1f3e9052002-10-31 00:09:39 +0000447 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000448 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000449 pItem->zName = sqliteStrDup(pOldItem->zName);
450 pItem->sortOrder = pOldItem->sortOrder;
451 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000452 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000453 }
454 return pNew;
455}
danielk197793758c82005-01-21 08:13:14 +0000456
457/*
458** If cursors, triggers, views and subqueries are all omitted from
459** the build, then none of the following routines, except for
460** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
461** called with a NULL argument.
462*/
danielk19776a67fe82005-02-04 04:07:16 +0000463#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
464 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000465SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000466 SrcList *pNew;
467 int i;
drh113088e2003-03-20 01:16:58 +0000468 int nByte;
drhad3cab52002-05-24 02:04:32 +0000469 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000470 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000471 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000472 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000473 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000474 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000475 struct SrcList_item *pNewItem = &pNew->a[i];
476 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000477 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000478 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
479 pNewItem->zName = sqliteStrDup(pOldItem->zName);
480 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
481 pNewItem->jointype = pOldItem->jointype;
482 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000483 pTab = pNewItem->pTab = pOldItem->pTab;
484 if( pTab ){
485 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000486 }
danielk19774adee202004-05-08 08:23:19 +0000487 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
488 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
489 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000490 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000491 }
492 return pNew;
493}
danielk19774adee202004-05-08 08:23:19 +0000494IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000495 IdList *pNew;
496 int i;
497 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000498 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000499 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000500 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000501 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000502 if( pNew->a==0 ){
503 sqliteFree(pNew);
504 return 0;
505 }
drhff78bd22002-02-27 01:47:11 +0000506 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000507 struct IdList_item *pNewItem = &pNew->a[i];
508 struct IdList_item *pOldItem = &p->a[i];
509 pNewItem->zName = sqliteStrDup(pOldItem->zName);
510 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000511 }
512 return pNew;
513}
danielk19774adee202004-05-08 08:23:19 +0000514Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000515 Select *pNew;
516 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000517 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000518 if( pNew==0 ) return 0;
519 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000520 pNew->pEList = sqlite3ExprListDup(p->pEList);
521 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
522 pNew->pWhere = sqlite3ExprDup(p->pWhere);
523 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
524 pNew->pHaving = sqlite3ExprDup(p->pHaving);
525 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000526 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000527 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000528 pNew->pLimit = sqlite3ExprDup(p->pLimit);
529 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000530 pNew->iLimit = -1;
531 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000532 pNew->ppOpenTemp = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000533 pNew->isResolved = p->isResolved;
534 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000535 return pNew;
536}
danielk197793758c82005-01-21 08:13:14 +0000537#else
538Select *sqlite3SelectDup(Select *p){
539 assert( p==0 );
540 return 0;
541}
542#endif
drhff78bd22002-02-27 01:47:11 +0000543
544
545/*
drha76b5df2002-02-23 02:32:10 +0000546** Add a new element to the end of an expression list. If pList is
547** initially NULL, then create a new expression list.
548*/
danielk19774adee202004-05-08 08:23:19 +0000549ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000550 if( pList==0 ){
551 pList = sqliteMalloc( sizeof(ExprList) );
552 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000553 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000554 }
drh4efc4752004-01-16 15:55:37 +0000555 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000556 }
drh4305d102003-07-30 12:34:12 +0000557 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000558 struct ExprList_item *a;
559 int n = pList->nAlloc*2 + 4;
560 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
561 if( a==0 ){
562 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000563 }
danielk1977d5d56522005-03-16 12:15:20 +0000564 pList->a = a;
565 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000566 }
drh4efc4752004-01-16 15:55:37 +0000567 assert( pList->a!=0 );
568 if( pExpr || pName ){
569 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
570 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000571 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000572 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000573 }
574 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000575
576no_mem:
577 /* Avoid leaking memory if malloc has failed. */
578 sqlite3ExprDelete(pExpr);
579 sqlite3ExprListDelete(pList);
580 return 0;
drha76b5df2002-02-23 02:32:10 +0000581}
582
583/*
584** Delete an entire expression list.
585*/
danielk19774adee202004-05-08 08:23:19 +0000586void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000587 int i;
drhbe5c89a2004-07-26 00:31:09 +0000588 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000589 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000590 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
591 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000592 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
593 sqlite3ExprDelete(pItem->pExpr);
594 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000595 }
596 sqliteFree(pList->a);
597 sqliteFree(pList);
598}
599
600/*
drh626a8792005-01-17 22:08:19 +0000601** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000602**
drh626a8792005-01-17 22:08:19 +0000603** The return value from xFunc determines whether the tree walk continues.
604** 0 means continue walking the tree. 1 means do not walk children
605** of the current node but continue with siblings. 2 means abandon
606** the tree walk completely.
607**
608** The return value from this routine is 1 to abandon the tree walk
609** and 0 to continue.
610*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000611static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000612static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000613 int rc;
614 if( pExpr==0 ) return 0;
615 rc = (*xFunc)(pArg, pExpr);
616 if( rc==0 ){
617 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
618 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000619 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000620 }
621 return rc>1;
622}
623
624/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000625** Call walkExprTree() for every expression in list p.
626*/
627static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
628 int i;
629 struct ExprList_item *pItem;
630 if( !p ) return 0;
631 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
632 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
633 }
634 return 0;
635}
636
637/*
638** Call walkExprTree() for every expression in Select p, not including
639** expressions that are part of sub-selects in any FROM clause or the LIMIT
640** or OFFSET expressions..
641*/
642static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
643 walkExprList(p->pEList, xFunc, pArg);
644 walkExprTree(p->pWhere, xFunc, pArg);
645 walkExprList(p->pGroupBy, xFunc, pArg);
646 walkExprTree(p->pHaving, xFunc, pArg);
647 walkExprList(p->pOrderBy, xFunc, pArg);
648 return 0;
649}
650
651
652/*
drh626a8792005-01-17 22:08:19 +0000653** This routine is designed as an xFunc for walkExprTree().
654**
655** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000656** at pExpr that the expression that contains pExpr is not a constant
657** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
658** If pExpr does does not disqualify the expression from being a constant
659** then do nothing.
660**
661** After walking the whole tree, if no nodes are found that disqualify
662** the expression as constant, then we assume the whole expression
663** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000664*/
665static int exprNodeIsConstant(void *pArg, Expr *pExpr){
666 switch( pExpr->op ){
667 case TK_ID:
668 case TK_COLUMN:
669 case TK_DOT:
670 case TK_AGG_FUNCTION:
671 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000672#ifndef SQLITE_OMIT_SUBQUERY
673 case TK_SELECT:
674 case TK_EXISTS:
675#endif
drh626a8792005-01-17 22:08:19 +0000676 *((int*)pArg) = 0;
677 return 2;
678 default:
679 return 0;
680 }
681}
682
683/*
drhfef52082000-06-06 01:50:43 +0000684** Walk an expression tree. Return 1 if the expression is constant
685** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000686**
687** For the purposes of this function, a double-quoted string (ex: "abc")
688** is considered a variable but a single-quoted string (ex: 'abc') is
689** a constant.
drhfef52082000-06-06 01:50:43 +0000690*/
danielk19774adee202004-05-08 08:23:19 +0000691int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000692 int isConst = 1;
693 walkExprTree(p, exprNodeIsConstant, &isConst);
694 return isConst;
drhfef52082000-06-06 01:50:43 +0000695}
696
697/*
drh73b211a2005-01-18 04:00:42 +0000698** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000699** to fit in a 32-bit integer, return 1 and put the value of the integer
700** in *pValue. If the expression is not an integer or if it is too big
701** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000702*/
danielk19774adee202004-05-08 08:23:19 +0000703int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000704 switch( p->op ){
705 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000706 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000707 return 1;
708 }
709 break;
drhe4de1fe2002-06-02 16:09:01 +0000710 }
drh4b59ab52002-08-24 18:24:51 +0000711 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000712 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000713 }
drhe4de1fe2002-06-02 16:09:01 +0000714 case TK_UMINUS: {
715 int v;
danielk19774adee202004-05-08 08:23:19 +0000716 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000717 *pValue = -v;
718 return 1;
719 }
720 break;
721 }
722 default: break;
723 }
724 return 0;
725}
726
727/*
drhc4a3c772001-04-04 11:48:57 +0000728** Return TRUE if the given string is a row-id column name.
729*/
danielk19774adee202004-05-08 08:23:19 +0000730int sqlite3IsRowid(const char *z){
731 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
732 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
733 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000734 return 0;
735}
736
737/*
drh8141f612004-01-25 22:44:58 +0000738** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
739** that name in the set of source tables in pSrcList and make the pExpr
740** expression node refer back to that source column. The following changes
741** are made to pExpr:
742**
743** pExpr->iDb Set the index in db->aDb[] of the database holding
744** the table.
745** pExpr->iTable Set to the cursor number for the table obtained
746** from pSrcList.
747** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000748** pExpr->op Set to TK_COLUMN.
749** pExpr->pLeft Any expression this points to is deleted
750** pExpr->pRight Any expression this points to is deleted.
751**
752** The pDbToken is the name of the database (the "X"). This value may be
753** NULL meaning that name is of the form Y.Z or Z. Any available database
754** can be used. The pTableToken is the name of the table (the "Y"). This
755** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
756** means that the form of the name is Z and that columns from any table
757** can be used.
758**
759** If the name cannot be resolved unambiguously, leave an error message
760** in pParse and return non-zero. Return zero on success.
761*/
762static int lookupName(
763 Parse *pParse, /* The parsing context */
764 Token *pDbToken, /* Name of the database containing table, or NULL */
765 Token *pTableToken, /* Name of table containing column, or NULL */
766 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000767 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000768 Expr *pExpr /* Make this EXPR node point to the selected column */
769){
770 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
771 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
772 char *zCol = 0; /* Name of the column. The "Z" */
773 int i, j; /* Loop counters */
774 int cnt = 0; /* Number of matching column names */
775 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000776 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000777 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
778 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000779 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000780
781 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000782 zDb = sqlite3NameFromToken(pDbToken);
783 zTab = sqlite3NameFromToken(pTableToken);
784 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000785 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000786 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000787 }
drh8141f612004-01-25 22:44:58 +0000788
789 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000790 while( pNC && cnt==0 ){
791 SrcList *pSrcList = pNC->pSrcList;
792 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000793
drh626a8792005-01-17 22:08:19 +0000794 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000795 if( pSrcList ){
796 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
797 Table *pTab = pItem->pTab;
798 Column *pCol;
799
800 if( pTab==0 ) continue;
801 assert( pTab->nCol>0 );
802 if( zTab ){
803 if( pItem->zAlias ){
804 char *zTabName = pItem->zAlias;
805 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
806 }else{
807 char *zTabName = pTab->zName;
808 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
809 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
810 continue;
811 }
drh626a8792005-01-17 22:08:19 +0000812 }
drh8141f612004-01-25 22:44:58 +0000813 }
danielk1977b3bce662005-01-29 08:32:43 +0000814 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000815 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000816 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000817 pMatch = pItem;
818 }
819 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
820 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000821 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000822 cnt++;
823 pExpr->iTable = pItem->iCursor;
824 pMatch = pItem;
825 pExpr->iDb = pTab->iDb;
826 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
827 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
828 pExpr->affinity = pTab->aCol[j].affinity;
829 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000830 if( pItem->jointype & JT_NATURAL ){
831 /* If this match occurred in the left table of a natural join,
832 ** then skip the right table to avoid a duplicate match */
833 pItem++;
834 i++;
835 }
drh873fac02005-06-06 17:11:46 +0000836 if( (pUsing = pItem->pUsing)!=0 ){
837 /* If this match occurs on a column that is in the USING clause
838 ** of a join, skip the search of the right table of the join
839 ** to avoid a duplicate match there. */
840 int k;
841 for(k=0; k<pUsing->nId; k++){
842 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
843 pItem++;
844 i++;
845 break;
846 }
847 }
848 }
danielk1977b3bce662005-01-29 08:32:43 +0000849 break;
850 }
drh8141f612004-01-25 22:44:58 +0000851 }
852 }
853 }
drh626a8792005-01-17 22:08:19 +0000854
855#ifndef SQLITE_OMIT_TRIGGER
856 /* If we have not already resolved the name, then maybe
857 ** it is a new.* or old.* trigger argument reference
858 */
859 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
860 TriggerStack *pTriggerStack = pParse->trigStack;
861 Table *pTab = 0;
862 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
863 pExpr->iTable = pTriggerStack->newIdx;
864 assert( pTriggerStack->pTab );
865 pTab = pTriggerStack->pTab;
866 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
867 pExpr->iTable = pTriggerStack->oldIdx;
868 assert( pTriggerStack->pTab );
869 pTab = pTriggerStack->pTab;
870 }
871
872 if( pTab ){
873 int j;
874 Column *pCol = pTab->aCol;
875
876 pExpr->iDb = pTab->iDb;
877 cntTab++;
878 for(j=0; j < pTab->nCol; j++, pCol++) {
879 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
880 cnt++;
881 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
882 pExpr->affinity = pTab->aCol[j].affinity;
883 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000884 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000885 break;
886 }
887 }
888 }
889 }
drhb7f91642004-10-31 02:22:47 +0000890#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000891
drh626a8792005-01-17 22:08:19 +0000892 /*
893 ** Perhaps the name is a reference to the ROWID
894 */
895 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
896 cnt = 1;
897 pExpr->iColumn = -1;
898 pExpr->affinity = SQLITE_AFF_INTEGER;
899 }
drh8141f612004-01-25 22:44:58 +0000900
drh626a8792005-01-17 22:08:19 +0000901 /*
902 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
903 ** might refer to an result-set alias. This happens, for example, when
904 ** we are resolving names in the WHERE clause of the following command:
905 **
906 ** SELECT a+b AS x FROM table WHERE x<10;
907 **
908 ** In cases like this, replace pExpr with a copy of the expression that
909 ** forms the result set entry ("a+b" in the example) and return immediately.
910 ** Note that the expression in the result set should have already been
911 ** resolved by the time the WHERE clause is resolved.
912 */
drh79d5f632005-01-18 17:20:10 +0000913 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000914 for(j=0; j<pEList->nExpr; j++){
915 char *zAs = pEList->a[j].zName;
916 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
917 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
918 pExpr->op = TK_AS;
919 pExpr->iColumn = j;
920 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000921 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000922 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000923 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000924 }
925 }
926 }
927
928 /* Advance to the next name context. The loop will exit when either
929 ** we have a match (cnt>0) or when we run out of name contexts.
930 */
931 if( cnt==0 ){
932 pNC = pNC->pNext;
933 }
drh8141f612004-01-25 22:44:58 +0000934 }
935
936 /*
937 ** If X and Y are NULL (in other words if only the column name Z is
938 ** supplied) and the value of Z is enclosed in double-quotes, then
939 ** Z is a string literal if it doesn't match any column names. In that
940 ** case, we need to return right away and not make any changes to
941 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000942 **
943 ** Because no reference was made to outer contexts, the pNC->nRef
944 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000945 */
946 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
947 sqliteFree(zCol);
948 return 0;
949 }
950
951 /*
952 ** cnt==0 means there was not match. cnt>1 means there were two or
953 ** more matches. Either way, we have an error.
954 */
955 if( cnt!=1 ){
956 char *z = 0;
957 char *zErr;
958 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
959 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000960 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000961 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000962 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000963 }else{
964 z = sqliteStrDup(zCol);
965 }
danielk19774adee202004-05-08 08:23:19 +0000966 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000967 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000968 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000969 }
970
drh51669862004-12-18 18:40:26 +0000971 /* If a column from a table in pSrcList is referenced, then record
972 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
973 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
974 ** column number is greater than the number of bits in the bitmask
975 ** then set the high-order bit of the bitmask.
976 */
977 if( pExpr->iColumn>=0 && pMatch!=0 ){
978 int n = pExpr->iColumn;
979 if( n>=sizeof(Bitmask)*8 ){
980 n = sizeof(Bitmask)*8-1;
981 }
982 assert( pMatch->iCursor==pExpr->iTable );
983 pMatch->colUsed |= 1<<n;
984 }
985
danielk1977d5d56522005-03-16 12:15:20 +0000986lookupname_end:
drh8141f612004-01-25 22:44:58 +0000987 /* Clean up and return
988 */
989 sqliteFree(zDb);
990 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +0000991 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000992 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000993 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000994 pExpr->pRight = 0;
995 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +0000996lookupname_end_2:
997 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +0000998 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000999 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001000 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001001 if( pMatch && !pMatch->pSelect ){
1002 pExpr->pTab = pMatch->pTab;
1003 }
drh15ccce12005-05-23 15:06:39 +00001004 /* Increment the nRef value on all name contexts from TopNC up to
1005 ** the point where the name matched. */
1006 for(;;){
1007 assert( pTopNC!=0 );
1008 pTopNC->nRef++;
1009 if( pTopNC==pNC ) break;
1010 pTopNC = pTopNC->pNext;
1011 }
1012 return 0;
1013 } else {
1014 return 1;
drh626a8792005-01-17 22:08:19 +00001015 }
drh8141f612004-01-25 22:44:58 +00001016}
1017
1018/*
drh626a8792005-01-17 22:08:19 +00001019** This routine is designed as an xFunc for walkExprTree().
1020**
drh73b211a2005-01-18 04:00:42 +00001021** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001022** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001023** the tree or 2 to abort the tree walk.
1024**
1025** This routine also does error checking and name resolution for
1026** function names. The operator for aggregate functions is changed
1027** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001028*/
1029static int nameResolverStep(void *pArg, Expr *pExpr){
1030 NameContext *pNC = (NameContext*)pArg;
1031 SrcList *pSrcList;
1032 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001033
danielk1977b3bce662005-01-29 08:32:43 +00001034 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001035 assert( pNC!=0 );
1036 pSrcList = pNC->pSrcList;
1037 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001038
drh626a8792005-01-17 22:08:19 +00001039 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1040 ExprSetProperty(pExpr, EP_Resolved);
1041#ifndef NDEBUG
1042 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001043 int i;
drh626a8792005-01-17 22:08:19 +00001044 for(i=0; i<pSrcList->nSrc; i++){
1045 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1046 }
1047 }
1048#endif
1049 switch( pExpr->op ){
1050 /* Double-quoted strings (ex: "abc") are used as identifiers if
1051 ** possible. Otherwise they remain as strings. Single-quoted
1052 ** strings (ex: 'abc') are always string literals.
1053 */
1054 case TK_STRING: {
1055 if( pExpr->token.z[0]=='\'' ) break;
1056 /* Fall thru into the TK_ID case if this is a double-quoted string */
1057 }
1058 /* A lone identifier is the name of a column.
1059 */
1060 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001061 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1062 return 1;
1063 }
1064
1065 /* A table name and column name: ID.ID
1066 ** Or a database, table and column: ID.ID.ID
1067 */
1068 case TK_DOT: {
1069 Token *pColumn;
1070 Token *pTable;
1071 Token *pDb;
1072 Expr *pRight;
1073
danielk1977b3bce662005-01-29 08:32:43 +00001074 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001075 pRight = pExpr->pRight;
1076 if( pRight->op==TK_ID ){
1077 pDb = 0;
1078 pTable = &pExpr->pLeft->token;
1079 pColumn = &pRight->token;
1080 }else{
1081 assert( pRight->op==TK_DOT );
1082 pDb = &pExpr->pLeft->token;
1083 pTable = &pRight->pLeft->token;
1084 pColumn = &pRight->pRight->token;
1085 }
1086 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1087 return 1;
1088 }
1089
1090 /* Resolve function names
1091 */
drhb71090f2005-05-23 17:26:51 +00001092 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001093 case TK_FUNCTION: {
1094 ExprList *pList = pExpr->pList; /* The argument list */
1095 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1096 int no_such_func = 0; /* True if no such function exists */
1097 int wrong_num_args = 0; /* True if wrong number of arguments */
1098 int is_agg = 0; /* True if is an aggregate function */
1099 int i;
1100 int nId; /* Number of characters in function name */
1101 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001102 FuncDef *pDef; /* Information about the function */
1103 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001104
drhb71090f2005-05-23 17:26:51 +00001105 zId = pExpr->token.z;
1106 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001107 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1108 if( pDef==0 ){
1109 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1110 if( pDef==0 ){
1111 no_such_func = 1;
1112 }else{
1113 wrong_num_args = 1;
1114 }
1115 }else{
1116 is_agg = pDef->xFunc==0;
1117 }
1118 if( is_agg && !pNC->allowAgg ){
1119 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1120 pNC->nErr++;
1121 is_agg = 0;
1122 }else if( no_such_func ){
1123 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1124 pNC->nErr++;
1125 }else if( wrong_num_args ){
1126 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1127 nId, zId);
1128 pNC->nErr++;
1129 }
1130 if( is_agg ){
1131 pExpr->op = TK_AGG_FUNCTION;
1132 pNC->hasAgg = 1;
1133 }
drh73b211a2005-01-18 04:00:42 +00001134 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001135 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001136 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001137 }
drh73b211a2005-01-18 04:00:42 +00001138 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001139 /* FIX ME: Compute pExpr->affinity based on the expected return
1140 ** type of the function
1141 */
1142 return is_agg;
1143 }
danielk1977b3bce662005-01-29 08:32:43 +00001144#ifndef SQLITE_OMIT_SUBQUERY
1145 case TK_SELECT:
1146 case TK_EXISTS:
1147#endif
1148 case TK_IN: {
1149 if( pExpr->pSelect ){
1150 int nRef = pNC->nRef;
1151 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1152 assert( pNC->nRef>=nRef );
1153 if( nRef!=pNC->nRef ){
1154 ExprSetProperty(pExpr, EP_VarSelect);
1155 }
1156 }
1157 }
drh626a8792005-01-17 22:08:19 +00001158 }
1159 return 0;
1160}
1161
1162/*
drhcce7d172000-05-31 15:34:51 +00001163** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001164** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001165** index to the table in the table list and a column offset. The
1166** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1167** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001168** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001169** VDBE cursor number for a cursor that is pointing into the referenced
1170** table. The Expr.iColumn value is changed to the index of the column
1171** of the referenced table. The Expr.iColumn value for the special
1172** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1173** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001174**
drh626a8792005-01-17 22:08:19 +00001175** Also resolve function names and check the functions for proper
1176** usage. Make sure all function names are recognized and all functions
1177** have the correct number of arguments. Leave an error message
1178** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1179**
drh73b211a2005-01-18 04:00:42 +00001180** If the expression contains aggregate functions then set the EP_Agg
1181** property on the expression.
drh626a8792005-01-17 22:08:19 +00001182*/
1183int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001184 NameContext *pNC, /* Namespace to resolve expressions in. */
1185 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001186){
drh73b211a2005-01-18 04:00:42 +00001187 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001188 walkExprTree(pExpr, nameResolverStep, pNC);
1189 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001190 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001191 }
1192 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001193}
1194
drh1398ad32005-01-19 23:24:50 +00001195/*
1196** A pointer instance of this structure is used to pass information
1197** through walkExprTree into codeSubqueryStep().
1198*/
1199typedef struct QueryCoder QueryCoder;
1200struct QueryCoder {
1201 Parse *pParse; /* The parsing context */
1202 NameContext *pNC; /* Namespace of first enclosing query */
1203};
1204
drh626a8792005-01-17 22:08:19 +00001205
1206/*
1207** Generate code for subqueries and IN operators.
1208**
drh73b211a2005-01-18 04:00:42 +00001209** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001210**
1211** expr IN (exprlist)
1212** and
1213** expr IN (SELECT ...)
1214**
1215** The first form is handled by creating a set holding the list
1216** of allowed values. The second form causes the SELECT to generate
1217** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001218*/
drh51522cd2005-01-20 13:36:19 +00001219#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001220void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1221 int label = 0; /* Address after sub-select code */
1222 Vdbe *v = sqlite3GetVdbe(pParse);
1223 if( v==0 ) return;
1224
1225 /* If this is not a variable (correlated) select, then execute
1226 ** it only once. Unless this is part of a trigger program. In
1227 ** that case re-execute every time (this could be optimized).
1228 */
1229 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1230 int mem = pParse->nMem++;
1231 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1232 label = sqlite3VdbeMakeLabel(v);
1233 sqlite3VdbeAddOp(v, OP_If, 0, label);
1234 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1235 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1236 }
1237
1238 if( pExpr->pSelect ){
1239 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1240 }
drh6a3ea0e2003-05-02 14:32:12 +00001241
drhcce7d172000-05-31 15:34:51 +00001242 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001243 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001244 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001245 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001246 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001247
danielk1977bf3b7212004-05-18 10:06:24 +00001248 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001249
1250 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1251 ** expression it is handled the same way. A temporary table is
1252 ** filled with single-field index keys representing the results
1253 ** from the SELECT or the <exprlist>.
1254 **
1255 ** If the 'x' expression is a column value, or the SELECT...
1256 ** statement returns a column value, then the affinity of that
1257 ** column is used to build the index keys. If both 'x' and the
1258 ** SELECT... statement are columns, then numeric affinity is used
1259 ** if either column has NUMERIC or INTEGER affinity. If neither
1260 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1261 ** is used.
1262 */
1263 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001264 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001265 memset(&keyInfo, 0, sizeof(keyInfo));
1266 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001267 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001268
drhfef52082000-06-06 01:50:43 +00001269 if( pExpr->pSelect ){
1270 /* Case 1: expr IN (SELECT ...)
1271 **
danielk1977e014a832004-05-17 10:48:57 +00001272 ** Generate code to write the results of the select into the temporary
1273 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001274 */
danielk1977e014a832004-05-17 10:48:57 +00001275 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001276 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001277 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001278 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001279 pEList = pExpr->pSelect->pEList;
1280 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001281 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001282 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001283 }
drhfef52082000-06-06 01:50:43 +00001284 }else if( pExpr->pList ){
1285 /* Case 2: expr IN (exprlist)
1286 **
danielk1977e014a832004-05-17 10:48:57 +00001287 ** For each expression, build an index key from the evaluation and
1288 ** store it in the temporary table. If <expr> is a column, then use
1289 ** that columns affinity when building index keys. If <expr> is not
1290 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001291 */
danielk1977e014a832004-05-17 10:48:57 +00001292 int i;
danielk1977e014a832004-05-17 10:48:57 +00001293 if( !affinity ){
1294 affinity = SQLITE_AFF_NUMERIC;
1295 }
danielk19770202b292004-06-09 09:55:16 +00001296 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001297
1298 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001299 for(i=0; i<pExpr->pList->nExpr; i++){
1300 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001301
1302 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001303 if( !sqlite3ExprIsConstant(pE2) ){
1304 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001305 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001306 return;
drh4794b982000-06-06 13:54:14 +00001307 }
danielk1977e014a832004-05-17 10:48:57 +00001308
1309 /* Evaluate the expression and insert it into the temp table */
1310 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001311 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001312 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001313 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001314 }
1315 }
danielk19770202b292004-06-09 09:55:16 +00001316 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001317 break;
drhfef52082000-06-06 01:50:43 +00001318 }
1319
drh51522cd2005-01-20 13:36:19 +00001320 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001321 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001322 /* This has to be a scalar SELECT. Generate code to put the
1323 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001324 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001325 */
drh51522cd2005-01-20 13:36:19 +00001326 int sop;
1327 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001328
drh967e8b72000-06-21 13:59:10 +00001329 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001330 pSel = pExpr->pSelect;
1331 if( pExpr->op==TK_SELECT ){
1332 sop = SRT_Mem;
1333 }else{
1334 static const Token one = { "1", 0, 1 };
1335 sop = SRT_Exists;
1336 sqlite3ExprListDelete(pSel->pEList);
1337 pSel->pEList = sqlite3ExprListAppend(0,
1338 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1339 }
danielk1977b3bce662005-01-29 08:32:43 +00001340 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1341 break;
drhcce7d172000-05-31 15:34:51 +00001342 }
1343 }
danielk1977b3bce662005-01-29 08:32:43 +00001344
1345 if( pExpr->pSelect ){
1346 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1347 }
1348 if( label<0 ){
1349 sqlite3VdbeResolveLabel(v, label);
1350 }
1351 return;
drhcce7d172000-05-31 15:34:51 +00001352}
drh51522cd2005-01-20 13:36:19 +00001353#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001354
drhcce7d172000-05-31 15:34:51 +00001355/*
drhfec19aa2004-05-19 20:41:03 +00001356** Generate an instruction that will put the integer describe by
1357** text z[0..n-1] on the stack.
1358*/
1359static void codeInteger(Vdbe *v, const char *z, int n){
1360 int i;
drh6fec0762004-05-30 01:38:43 +00001361 if( sqlite3GetInt32(z, &i) ){
1362 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1363 }else if( sqlite3FitsIn64Bits(z) ){
1364 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001365 }else{
1366 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1367 }
1368}
1369
1370/*
drhcce7d172000-05-31 15:34:51 +00001371** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001372** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001373**
1374** This code depends on the fact that certain token values (ex: TK_EQ)
1375** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1376** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1377** the make process cause these values to align. Assert()s in the code
1378** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001379*/
danielk19774adee202004-05-08 08:23:19 +00001380void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001381 Vdbe *v = pParse->pVdbe;
1382 int op;
danielk19777977a172004-11-09 12:44:37 +00001383 if( v==0 ) return;
1384 if( pExpr==0 ){
1385 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1386 return;
1387 }
drhf2bc0132004-10-04 13:19:23 +00001388 op = pExpr->op;
1389 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001390 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001391 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1392 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001393 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001394 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001395 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001396 }else{
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001398 }
drhcce7d172000-05-31 15:34:51 +00001399 break;
1400 }
1401 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001402 codeInteger(v, pExpr->token.z, pExpr->token.n);
1403 break;
1404 }
1405 case TK_FLOAT:
1406 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001407 assert( TK_FLOAT==OP_Real );
1408 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001409 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001410 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001411 break;
1412 }
danielk19775338a5f2005-01-20 13:03:10 +00001413#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001414 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001415 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001416 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1417 sqlite3VdbeDequoteP3(v, -1);
1418 break;
1419 }
danielk19775338a5f2005-01-20 13:03:10 +00001420#endif
drhcce7d172000-05-31 15:34:51 +00001421 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001422 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001423 break;
1424 }
drh50457892003-09-06 01:10:47 +00001425 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001426 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001427 if( pExpr->token.n>1 ){
1428 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1429 }
drh50457892003-09-06 01:10:47 +00001430 break;
1431 }
drh4e0cff62004-11-05 05:10:28 +00001432 case TK_REGISTER: {
1433 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1434 break;
1435 }
drhc9b84a12002-06-20 11:36:48 +00001436 case TK_LT:
1437 case TK_LE:
1438 case TK_GT:
1439 case TK_GE:
1440 case TK_NE:
1441 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001442 assert( TK_LT==OP_Lt );
1443 assert( TK_LE==OP_Le );
1444 assert( TK_GT==OP_Gt );
1445 assert( TK_GE==OP_Ge );
1446 assert( TK_EQ==OP_Eq );
1447 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001448 sqlite3ExprCode(pParse, pExpr->pLeft);
1449 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001450 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001451 break;
drhc9b84a12002-06-20 11:36:48 +00001452 }
drhcce7d172000-05-31 15:34:51 +00001453 case TK_AND:
1454 case TK_OR:
1455 case TK_PLUS:
1456 case TK_STAR:
1457 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001458 case TK_REM:
1459 case TK_BITAND:
1460 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001461 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001462 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001463 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001464 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001465 assert( TK_AND==OP_And );
1466 assert( TK_OR==OP_Or );
1467 assert( TK_PLUS==OP_Add );
1468 assert( TK_MINUS==OP_Subtract );
1469 assert( TK_REM==OP_Remainder );
1470 assert( TK_BITAND==OP_BitAnd );
1471 assert( TK_BITOR==OP_BitOr );
1472 assert( TK_SLASH==OP_Divide );
1473 assert( TK_LSHIFT==OP_ShiftLeft );
1474 assert( TK_RSHIFT==OP_ShiftRight );
1475 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001476 sqlite3ExprCode(pParse, pExpr->pLeft);
1477 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001478 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001479 break;
1480 }
drhcce7d172000-05-31 15:34:51 +00001481 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001482 Expr *pLeft = pExpr->pLeft;
1483 assert( pLeft );
1484 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1485 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001486 char *z = sqliteMalloc( p->n + 2 );
1487 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001488 if( pLeft->op==TK_FLOAT ){
1489 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001490 }else{
drhfec19aa2004-05-19 20:41:03 +00001491 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001492 }
drh6e142f52000-06-08 13:36:40 +00001493 sqliteFree(z);
1494 break;
1495 }
drh1ccde152000-06-17 13:12:39 +00001496 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001497 }
drhbf4133c2001-10-13 02:59:08 +00001498 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001499 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001500 assert( TK_BITNOT==OP_BitNot );
1501 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001502 sqlite3ExprCode(pParse, pExpr->pLeft);
1503 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001504 break;
1505 }
1506 case TK_ISNULL:
1507 case TK_NOTNULL: {
1508 int dest;
drhf2bc0132004-10-04 13:19:23 +00001509 assert( TK_ISNULL==OP_IsNull );
1510 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001511 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1512 sqlite3ExprCode(pParse, pExpr->pLeft);
1513 dest = sqlite3VdbeCurrentAddr(v) + 2;
1514 sqlite3VdbeAddOp(v, op, 1, dest);
1515 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001516 break;
drhcce7d172000-05-31 15:34:51 +00001517 }
drh22827922000-06-06 17:27:05 +00001518 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001519 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001520 break;
1521 }
drhb71090f2005-05-23 17:26:51 +00001522 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001523 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001524 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001525 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001526 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001527 int nId;
1528 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001529 int p2 = 0;
1530 int i;
danielk1977d8123362004-06-12 09:25:12 +00001531 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001532 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001533 zId = pExpr->token.z;
1534 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001535 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001536 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001537 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001538 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001539 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1540 p2 |= (1<<i);
1541 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001542 if( pDef->needCollSeq && !pColl ){
1543 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1544 }
1545 }
1546 if( pDef->needCollSeq ){
1547 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001548 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001549 }
1550 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001551 break;
1552 }
drhfe2093d2005-01-20 22:48:47 +00001553#ifndef SQLITE_OMIT_SUBQUERY
1554 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001555 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001556 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001557 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001558 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001559 break;
1560 }
drhfef52082000-06-06 01:50:43 +00001561 case TK_IN: {
1562 int addr;
drh94a11212004-09-25 13:12:14 +00001563 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001564 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001565
1566 /* Figure out the affinity to use to create a key from the results
1567 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001568 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001569 */
drh94a11212004-09-25 13:12:14 +00001570 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001571
danielk19774adee202004-05-08 08:23:19 +00001572 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001573
1574 /* Code the <expr> from "<expr> IN (...)". The temporary table
1575 ** pExpr->iTable contains the values that make up the (...) set.
1576 */
danielk19774adee202004-05-08 08:23:19 +00001577 sqlite3ExprCode(pParse, pExpr->pLeft);
1578 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001579 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001580 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001581 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001582 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001583 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001584 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1585 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1586
drhfef52082000-06-06 01:50:43 +00001587 break;
1588 }
danielk197793758c82005-01-21 08:13:14 +00001589#endif
drhfef52082000-06-06 01:50:43 +00001590 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001591 Expr *pLeft = pExpr->pLeft;
1592 struct ExprList_item *pLItem = pExpr->pList->a;
1593 Expr *pRight = pLItem->pExpr;
1594 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001595 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001596 sqlite3ExprCode(pParse, pRight);
1597 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001598 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001599 pLItem++;
1600 pRight = pLItem->pExpr;
1601 sqlite3ExprCode(pParse, pRight);
1602 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001603 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001604 break;
1605 }
drh51e9a442004-01-16 16:42:53 +00001606 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001607 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001608 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001609 break;
1610 }
drh17a7f8d2002-03-24 13:13:27 +00001611 case TK_CASE: {
1612 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001613 int jumpInst;
1614 int addr;
1615 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001616 int i;
drhbe5c89a2004-07-26 00:31:09 +00001617 ExprList *pEList;
1618 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001619
1620 assert(pExpr->pList);
1621 assert((pExpr->pList->nExpr % 2) == 0);
1622 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001623 pEList = pExpr->pList;
1624 aListelem = pEList->a;
1625 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001626 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001627 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001628 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001629 }
drhf5905aa2002-05-26 20:54:33 +00001630 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001631 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001632 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001633 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001634 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1635 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001636 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001637 }else{
danielk19774adee202004-05-08 08:23:19 +00001638 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001639 }
drhbe5c89a2004-07-26 00:31:09 +00001640 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001641 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1642 addr = sqlite3VdbeCurrentAddr(v);
1643 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001644 }
drhf570f012002-05-31 15:51:25 +00001645 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001646 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001647 }
drh17a7f8d2002-03-24 13:13:27 +00001648 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001649 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001650 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001651 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001652 }
danielk19774adee202004-05-08 08:23:19 +00001653 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001654 break;
1655 }
danielk19775338a5f2005-01-20 13:03:10 +00001656#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001657 case TK_RAISE: {
1658 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001659 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001660 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001661 return;
1662 }
drhad6d9462004-09-19 02:15:24 +00001663 if( pExpr->iColumn!=OE_Ignore ){
1664 assert( pExpr->iColumn==OE_Rollback ||
1665 pExpr->iColumn == OE_Abort ||
1666 pExpr->iColumn == OE_Fail );
1667 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1668 pExpr->token.z, pExpr->token.n);
1669 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001670 } else {
drhad6d9462004-09-19 02:15:24 +00001671 assert( pExpr->iColumn == OE_Ignore );
1672 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1673 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1674 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001675 }
drh17a7f8d2002-03-24 13:13:27 +00001676 }
danielk19775338a5f2005-01-20 13:03:10 +00001677#endif
drh17a7f8d2002-03-24 13:13:27 +00001678 break;
drhcce7d172000-05-31 15:34:51 +00001679 }
drhcce7d172000-05-31 15:34:51 +00001680}
1681
danielk197793758c82005-01-21 08:13:14 +00001682#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001683/*
drh25303782004-12-07 15:41:48 +00001684** Generate code that evalutes the given expression and leaves the result
1685** on the stack. See also sqlite3ExprCode().
1686**
1687** This routine might also cache the result and modify the pExpr tree
1688** so that it will make use of the cached result on subsequent evaluations
1689** rather than evaluate the whole expression again. Trivial expressions are
1690** not cached. If the expression is cached, its result is stored in a
1691** memory location.
1692*/
1693void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1694 Vdbe *v = pParse->pVdbe;
1695 int iMem;
1696 int addr1, addr2;
1697 if( v==0 ) return;
1698 addr1 = sqlite3VdbeCurrentAddr(v);
1699 sqlite3ExprCode(pParse, pExpr);
1700 addr2 = sqlite3VdbeCurrentAddr(v);
1701 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1702 iMem = pExpr->iTable = pParse->nMem++;
1703 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1704 pExpr->op = TK_REGISTER;
1705 }
1706}
danielk197793758c82005-01-21 08:13:14 +00001707#endif
drh25303782004-12-07 15:41:48 +00001708
1709/*
drh268380c2004-02-25 13:47:31 +00001710** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001711** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001712**
1713** Return the number of elements pushed onto the stack.
1714*/
danielk19774adee202004-05-08 08:23:19 +00001715int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001716 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001717 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001718){
1719 struct ExprList_item *pItem;
1720 int i, n;
1721 Vdbe *v;
1722 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001723 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001724 n = pList->nExpr;
1725 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001726 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001727 }
drhf9b596e2004-05-26 16:54:42 +00001728 return n;
drh268380c2004-02-25 13:47:31 +00001729}
1730
1731/*
drhcce7d172000-05-31 15:34:51 +00001732** Generate code for a boolean expression such that a jump is made
1733** to the label "dest" if the expression is true but execution
1734** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001735**
1736** If the expression evaluates to NULL (neither true nor false), then
1737** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001738**
1739** This code depends on the fact that certain token values (ex: TK_EQ)
1740** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1741** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1742** the make process cause these values to align. Assert()s in the code
1743** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001744*/
danielk19774adee202004-05-08 08:23:19 +00001745void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001746 Vdbe *v = pParse->pVdbe;
1747 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001748 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001749 op = pExpr->op;
1750 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001751 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001752 int d2 = sqlite3VdbeMakeLabel(v);
1753 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1754 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1755 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001756 break;
1757 }
1758 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001759 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1760 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001761 break;
1762 }
1763 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001764 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001765 break;
1766 }
1767 case TK_LT:
1768 case TK_LE:
1769 case TK_GT:
1770 case TK_GE:
1771 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001772 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001773 assert( TK_LT==OP_Lt );
1774 assert( TK_LE==OP_Le );
1775 assert( TK_GT==OP_Gt );
1776 assert( TK_GE==OP_Ge );
1777 assert( TK_EQ==OP_Eq );
1778 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3ExprCode(pParse, pExpr->pLeft);
1780 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001781 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001782 break;
1783 }
1784 case TK_ISNULL:
1785 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001786 assert( TK_ISNULL==OP_IsNull );
1787 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001788 sqlite3ExprCode(pParse, pExpr->pLeft);
1789 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001790 break;
1791 }
drhfef52082000-06-06 01:50:43 +00001792 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001793 /* The expression "x BETWEEN y AND z" is implemented as:
1794 **
1795 ** 1 IF (x < y) GOTO 3
1796 ** 2 IF (x <= z) GOTO <dest>
1797 ** 3 ...
1798 */
drhf5905aa2002-05-26 20:54:33 +00001799 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001800 Expr *pLeft = pExpr->pLeft;
1801 Expr *pRight = pExpr->pList->a[0].pExpr;
1802 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001803 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001804 sqlite3ExprCode(pParse, pRight);
1805 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001806
drhbe5c89a2004-07-26 00:31:09 +00001807 pRight = pExpr->pList->a[1].pExpr;
1808 sqlite3ExprCode(pParse, pRight);
1809 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001810
danielk19774adee202004-05-08 08:23:19 +00001811 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1812 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1813 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001814 break;
1815 }
drhcce7d172000-05-31 15:34:51 +00001816 default: {
danielk19774adee202004-05-08 08:23:19 +00001817 sqlite3ExprCode(pParse, pExpr);
1818 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001819 break;
1820 }
1821 }
1822}
1823
1824/*
drh66b89c82000-11-28 20:47:17 +00001825** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001826** to the label "dest" if the expression is false but execution
1827** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001828**
1829** If the expression evaluates to NULL (neither true nor false) then
1830** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001831*/
danielk19774adee202004-05-08 08:23:19 +00001832void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001833 Vdbe *v = pParse->pVdbe;
1834 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001835 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001836
1837 /* The value of pExpr->op and op are related as follows:
1838 **
1839 ** pExpr->op op
1840 ** --------- ----------
1841 ** TK_ISNULL OP_NotNull
1842 ** TK_NOTNULL OP_IsNull
1843 ** TK_NE OP_Eq
1844 ** TK_EQ OP_Ne
1845 ** TK_GT OP_Le
1846 ** TK_LE OP_Gt
1847 ** TK_GE OP_Lt
1848 ** TK_LT OP_Ge
1849 **
1850 ** For other values of pExpr->op, op is undefined and unused.
1851 ** The value of TK_ and OP_ constants are arranged such that we
1852 ** can compute the mapping above using the following expression.
1853 ** Assert()s verify that the computation is correct.
1854 */
1855 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1856
1857 /* Verify correct alignment of TK_ and OP_ constants
1858 */
1859 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1860 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1861 assert( pExpr->op!=TK_NE || op==OP_Eq );
1862 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1863 assert( pExpr->op!=TK_LT || op==OP_Ge );
1864 assert( pExpr->op!=TK_LE || op==OP_Gt );
1865 assert( pExpr->op!=TK_GT || op==OP_Le );
1866 assert( pExpr->op!=TK_GE || op==OP_Lt );
1867
drhcce7d172000-05-31 15:34:51 +00001868 switch( pExpr->op ){
1869 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001870 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1871 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001872 break;
1873 }
1874 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001875 int d2 = sqlite3VdbeMakeLabel(v);
1876 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1877 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1878 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001879 break;
1880 }
1881 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001882 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001883 break;
1884 }
1885 case TK_LT:
1886 case TK_LE:
1887 case TK_GT:
1888 case TK_GE:
1889 case TK_NE:
1890 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001891 sqlite3ExprCode(pParse, pExpr->pLeft);
1892 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001893 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001894 break;
1895 }
drhcce7d172000-05-31 15:34:51 +00001896 case TK_ISNULL:
1897 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001898 sqlite3ExprCode(pParse, pExpr->pLeft);
1899 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001900 break;
1901 }
drhfef52082000-06-06 01:50:43 +00001902 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001903 /* The expression is "x BETWEEN y AND z". It is implemented as:
1904 **
1905 ** 1 IF (x >= y) GOTO 3
1906 ** 2 GOTO <dest>
1907 ** 3 IF (x > z) GOTO <dest>
1908 */
drhfef52082000-06-06 01:50:43 +00001909 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001910 Expr *pLeft = pExpr->pLeft;
1911 Expr *pRight = pExpr->pList->a[0].pExpr;
1912 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001913 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001914 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001915 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001916 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1917
danielk19774adee202004-05-08 08:23:19 +00001918 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1919 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001920 pRight = pExpr->pList->a[1].pExpr;
1921 sqlite3ExprCode(pParse, pRight);
1922 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001923 break;
1924 }
drhcce7d172000-05-31 15:34:51 +00001925 default: {
danielk19774adee202004-05-08 08:23:19 +00001926 sqlite3ExprCode(pParse, pExpr);
1927 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001928 break;
1929 }
1930 }
1931}
drh22827922000-06-06 17:27:05 +00001932
1933/*
1934** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1935** if they are identical and return FALSE if they differ in any way.
1936*/
danielk19774adee202004-05-08 08:23:19 +00001937int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001938 int i;
1939 if( pA==0 ){
1940 return pB==0;
1941 }else if( pB==0 ){
1942 return 0;
1943 }
1944 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001945 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1946 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001947 if( pA->pList ){
1948 if( pB->pList==0 ) return 0;
1949 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1950 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001951 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001952 return 0;
1953 }
1954 }
1955 }else if( pB->pList ){
1956 return 0;
1957 }
1958 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001959 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001960 if( pA->token.z ){
1961 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001962 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001963 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001964 }
1965 return 1;
1966}
1967
1968/*
1969** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001970** The new element is initialized to zero. The calling function is
1971** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001972*/
1973static int appendAggInfo(Parse *pParse){
1974 if( (pParse->nAgg & 0x7)==0 ){
1975 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001976 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1977 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001978 return -1;
1979 }
drh6d4abfb2001-10-22 02:58:08 +00001980 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001981 }
1982 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1983 return pParse->nAgg++;
1984}
1985
1986/*
drh626a8792005-01-17 22:08:19 +00001987** This is an xFunc for walkExprTree() used to implement
1988** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1989** for additional information.
drh22827922000-06-06 17:27:05 +00001990**
drh626a8792005-01-17 22:08:19 +00001991** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001992*/
drh626a8792005-01-17 22:08:19 +00001993static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001994 int i;
1995 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00001996 NameContext *pNC = (NameContext *)pArg;
1997 Parse *pParse = pNC->pParse;
1998 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00001999
drh22827922000-06-06 17:27:05 +00002000 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002001 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002002 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2003 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2004 aAgg = pParse->aAgg;
2005 for(i=0; i<pParse->nAgg; i++){
2006 if( aAgg[i].isAgg ) continue;
2007 if( aAgg[i].pExpr->iTable==pExpr->iTable
2008 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2009 break;
2010 }
2011 }
2012 if( i>=pParse->nAgg ){
2013 i = appendAggInfo(pParse);
2014 if( i<0 ) return 1;
2015 pParse->aAgg[i].isAgg = 0;
2016 pParse->aAgg[i].pExpr = pExpr;
2017 }
2018 pExpr->iAgg = i;
2019 pExpr->iAggCtx = pNC->nDepth;
2020 return 1;
drh22827922000-06-06 17:27:05 +00002021 }
2022 }
drh626a8792005-01-17 22:08:19 +00002023 return 1;
drh22827922000-06-06 17:27:05 +00002024 }
2025 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002026 if( pNC->nDepth==0 ){
2027 aAgg = pParse->aAgg;
2028 for(i=0; i<pParse->nAgg; i++){
2029 if( !aAgg[i].isAgg ) continue;
2030 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2031 break;
2032 }
drh22827922000-06-06 17:27:05 +00002033 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002034 if( i>=pParse->nAgg ){
2035 u8 enc = pParse->db->enc;
2036 i = appendAggInfo(pParse);
2037 if( i<0 ) return 1;
2038 pParse->aAgg[i].isAgg = 1;
2039 pParse->aAgg[i].pExpr = pExpr;
2040 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2041 pExpr->token.z, pExpr->token.n,
2042 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2043 }
2044 pExpr->iAgg = i;
2045 return 1;
drh22827922000-06-06 17:27:05 +00002046 }
drh22827922000-06-06 17:27:05 +00002047 }
2048 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002049 if( pExpr->pSelect ){
2050 pNC->nDepth++;
2051 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2052 pNC->nDepth--;
2053 }
drh626a8792005-01-17 22:08:19 +00002054 return 0;
2055}
2056
2057/*
2058** Analyze the given expression looking for aggregate functions and
2059** for variables that need to be added to the pParse->aAgg[] array.
2060** Make additional entries to the pParse->aAgg[] array as necessary.
2061**
2062** This routine should only be called after the expression has been
2063** analyzed by sqlite3ExprResolveNames().
2064**
2065** If errors are seen, leave an error message in zErrMsg and return
2066** the number of errors.
2067*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002068int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2069 int nErr = pNC->pParse->nErr;
2070 walkExprTree(pExpr, analyzeAggregate, pNC);
2071 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002072}