blob: efac9776609c8be86767b1a03aca2d0e97e0a4eb [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**
drh487e2622005-06-25 18:42:14 +000015** $Id: expr.c,v 1.208 2005/06/25 18:42:14 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
46 return sqlite3AffinityType(&pExpr->token);
47 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
danielk19770202b292004-06-09 09:55:16 +000053** Return the default collation sequence for the expression pExpr. If
54** there is no default collation type, return 0.
55*/
danielk19777cedc8d2004-06-10 10:50:08 +000056CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
57 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000058 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000059 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000060 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000061 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000062 }
63 }
danielk19777cedc8d2004-06-10 10:50:08 +000064 if( sqlite3CheckCollSeq(pParse, pColl) ){
65 pColl = 0;
66 }
67 return pColl;
danielk19770202b292004-06-09 09:55:16 +000068}
69
70/*
drh626a8792005-01-17 22:08:19 +000071** pExpr is an operand of a comparison operator. aff2 is the
72** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000073** type affinity that should be used for the comparison operator.
74*/
danielk1977e014a832004-05-17 10:48:57 +000075char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000076 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000077 if( aff1 && aff2 ){
78 /* Both sides of the comparison are columns. If one has numeric or
79 ** integer affinity, use that. Otherwise use no affinity.
80 */
81 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
82 return SQLITE_AFF_INTEGER;
83 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
84 return SQLITE_AFF_NUMERIC;
85 }else{
86 return SQLITE_AFF_NONE;
87 }
88 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000089 /* Neither side of the comparison is a column. Compare the
90 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000091 */
drh5f6a87b2004-07-19 00:39:45 +000092 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
93 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000094 }else{
95 /* One side is a column, the other is not. Use the columns affinity. */
96 return (aff1 + aff2);
97 }
98}
99
drh53db1452004-05-20 13:54:53 +0000100/*
101** pExpr is a comparison operator. Return the type affinity that should
102** be applied to both operands prior to doing the comparison.
103*/
danielk1977e014a832004-05-17 10:48:57 +0000104static char comparisonAffinity(Expr *pExpr){
105 char aff;
106 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
107 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
108 pExpr->op==TK_NE );
109 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000110 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000111 if( pExpr->pRight ){
112 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
113 }
114 else if( pExpr->pSelect ){
115 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
116 }
117 else if( !aff ){
118 aff = SQLITE_AFF_NUMERIC;
119 }
120 return aff;
121}
122
123/*
124** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
125** idx_affinity is the affinity of an indexed column. Return true
126** if the index with affinity idx_affinity may be used to implement
127** the comparison in pExpr.
128*/
129int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
130 char aff = comparisonAffinity(pExpr);
131 return
132 (aff==SQLITE_AFF_NONE) ||
133 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
134 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
135 (aff==idx_affinity);
136}
137
danielk1977a37cdde2004-05-16 11:15:36 +0000138/*
139** Return the P1 value that should be used for a binary comparison
140** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
141** If jumpIfNull is true, then set the low byte of the returned
142** P1 value to tell the opcode to jump if either expression
143** evaluates to NULL.
144*/
danielk1977e014a832004-05-17 10:48:57 +0000145static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000146 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000147 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000148}
149
drha2e00042002-01-22 03:13:42 +0000150/*
danielk19770202b292004-06-09 09:55:16 +0000151** Return a pointer to the collation sequence that should be used by
152** a binary comparison operator comparing pLeft and pRight.
153**
154** If the left hand expression has a collating sequence type, then it is
155** used. Otherwise the collation sequence for the right hand expression
156** is used, or the default (BINARY) if neither expression has a collating
157** type.
158*/
danielk19777cedc8d2004-06-10 10:50:08 +0000159static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
160 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000161 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000162 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000163 }
164 return pColl;
165}
166
167/*
drhbe5c89a2004-07-26 00:31:09 +0000168** Generate code for a comparison operator.
169*/
170static int codeCompare(
171 Parse *pParse, /* The parsing (and code generating) context */
172 Expr *pLeft, /* The left operand */
173 Expr *pRight, /* The right operand */
174 int opcode, /* The comparison opcode */
175 int dest, /* Jump here if true. */
176 int jumpIfNull /* If true, jump if either operand is NULL */
177){
178 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
179 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000180 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000181}
182
183/*
drha76b5df2002-02-23 02:32:10 +0000184** Construct a new expression node and return a pointer to it. Memory
185** for this node is obtained from sqliteMalloc(). The calling function
186** is responsible for making sure the node eventually gets freed.
187*/
drhe4e72072004-11-23 01:47:30 +0000188Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000189 Expr *pNew;
190 pNew = sqliteMalloc( sizeof(Expr) );
191 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000192 /* When malloc fails, delete pLeft and pRight. Expressions passed to
193 ** this function must always be allocated with sqlite3Expr() for this
194 ** reason.
195 */
196 sqlite3ExprDelete(pLeft);
197 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000198 return 0;
199 }
200 pNew->op = op;
201 pNew->pLeft = pLeft;
202 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000203 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000204 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000205 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000206 pNew->span = pNew->token = *pToken;
207 }else if( pLeft && pRight ){
208 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000209 }
drha76b5df2002-02-23 02:32:10 +0000210 return pNew;
211}
212
213/*
drh4e0cff62004-11-05 05:10:28 +0000214** When doing a nested parse, you can include terms in an expression
215** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000216** on the stack. "#0" means the top of the stack.
217** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000218**
219** This routine is called by the parser to deal with on of those terms.
220** It immediately generates code to store the value in a memory location.
221** The returns an expression that will code to extract the value from
222** that memory location as needed.
223*/
224Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
225 Vdbe *v = pParse->pVdbe;
226 Expr *p;
227 int depth;
228 if( v==0 ) return 0;
229 if( pParse->nested==0 ){
230 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
231 return 0;
232 }
233 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000234 if( p==0 ){
235 return 0; /* Malloc failed */
236 }
drh4e0cff62004-11-05 05:10:28 +0000237 depth = atoi(&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000238 p->iTable = pParse->nMem++;
239 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
240 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000241 return p;
242}
243
244/*
drh91bb0ee2004-09-01 03:06:34 +0000245** Join two expressions using an AND operator. If either expression is
246** NULL, then just return the other expression.
247*/
248Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
249 if( pLeft==0 ){
250 return pRight;
251 }else if( pRight==0 ){
252 return pLeft;
253 }else{
254 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
255 }
256}
257
258/*
drh6977fea2002-10-22 23:38:04 +0000259** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000260** text between the two given tokens.
261*/
danielk19774adee202004-05-08 08:23:19 +0000262void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000263 assert( pRight!=0 );
264 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000265 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000266 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000267 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000268 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000269 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000270 }else{
drh6977fea2002-10-22 23:38:04 +0000271 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000272 }
drha76b5df2002-02-23 02:32:10 +0000273 }
274}
275
276/*
277** Construct a new expression node for a function with multiple
278** arguments.
279*/
danielk19774adee202004-05-08 08:23:19 +0000280Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000281 Expr *pNew;
282 pNew = sqliteMalloc( sizeof(Expr) );
283 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000284 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000285 return 0;
286 }
287 pNew->op = TK_FUNCTION;
288 pNew->pList = pList;
289 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000290 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000291 pNew->token = *pToken;
292 }else{
293 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000294 }
drh6977fea2002-10-22 23:38:04 +0000295 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000296 return pNew;
297}
298
299/*
drhfa6bc002004-09-07 16:19:52 +0000300** Assign a variable number to an expression that encodes a wildcard
301** in the original SQL statement.
302**
303** Wildcards consisting of a single "?" are assigned the next sequential
304** variable number.
305**
306** Wildcards of the form "?nnn" are assigned the number "nnn". We make
307** sure "nnn" is not too be to avoid a denial of service attack when
308** the SQL statement comes from an external source.
309**
310** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
311** as the previous instance of the same wildcard. Or if this is the first
312** instance of the wildcard, the next sequenial variable number is
313** assigned.
314*/
315void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
316 Token *pToken;
317 if( pExpr==0 ) return;
318 pToken = &pExpr->token;
319 assert( pToken->n>=1 );
320 assert( pToken->z!=0 );
321 assert( pToken->z[0]!=0 );
322 if( pToken->n==1 ){
323 /* Wildcard of the form "?". Assign the next variable number */
324 pExpr->iTable = ++pParse->nVar;
325 }else if( pToken->z[0]=='?' ){
326 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
327 ** use it as the variable number */
328 int i;
329 pExpr->iTable = i = atoi(&pToken->z[1]);
330 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
331 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
332 SQLITE_MAX_VARIABLE_NUMBER);
333 }
334 if( i>pParse->nVar ){
335 pParse->nVar = i;
336 }
337 }else{
338 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
339 ** number as the prior appearance of the same name, or if the name
340 ** has never appeared before, reuse the same variable number
341 */
342 int i, n;
343 n = pToken->n;
344 for(i=0; i<pParse->nVarExpr; i++){
345 Expr *pE;
346 if( (pE = pParse->apVarExpr[i])!=0
347 && pE->token.n==n
348 && memcmp(pE->token.z, pToken->z, n)==0 ){
349 pExpr->iTable = pE->iTable;
350 break;
351 }
352 }
353 if( i>=pParse->nVarExpr ){
354 pExpr->iTable = ++pParse->nVar;
355 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
356 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
357 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
358 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
359 }
360 if( !sqlite3_malloc_failed ){
361 assert( pParse->apVarExpr!=0 );
362 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
363 }
364 }
365 }
366}
367
368/*
drha2e00042002-01-22 03:13:42 +0000369** Recursively delete an expression tree.
370*/
danielk19774adee202004-05-08 08:23:19 +0000371void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000372 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000373 if( p->span.dyn ) sqliteFree((char*)p->span.z);
374 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000375 sqlite3ExprDelete(p->pLeft);
376 sqlite3ExprDelete(p->pRight);
377 sqlite3ExprListDelete(p->pList);
378 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000379 sqliteFree(p);
380}
381
drha76b5df2002-02-23 02:32:10 +0000382
383/*
drhff78bd22002-02-27 01:47:11 +0000384** The following group of routines make deep copies of expressions,
385** expression lists, ID lists, and select statements. The copies can
386** be deleted (by being passed to their respective ...Delete() routines)
387** without effecting the originals.
388**
danielk19774adee202004-05-08 08:23:19 +0000389** The expression list, ID, and source lists return by sqlite3ExprListDup(),
390** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000391** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000392**
drhad3cab52002-05-24 02:04:32 +0000393** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000394*/
danielk19774adee202004-05-08 08:23:19 +0000395Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000396 Expr *pNew;
397 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000398 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000399 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000400 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000401 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000402 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000403 pNew->token.dyn = 1;
404 }else{
drh4efc4752004-01-16 15:55:37 +0000405 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000406 }
drh6977fea2002-10-22 23:38:04 +0000407 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000408 pNew->pLeft = sqlite3ExprDup(p->pLeft);
409 pNew->pRight = sqlite3ExprDup(p->pRight);
410 pNew->pList = sqlite3ExprListDup(p->pList);
411 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000412 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000413 return pNew;
414}
danielk19774adee202004-05-08 08:23:19 +0000415void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000416 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000417 if( pFrom->z ){
418 pTo->n = pFrom->n;
419 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
420 pTo->dyn = 1;
421 }else{
drh4b59ab52002-08-24 18:24:51 +0000422 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000423 }
424}
danielk19774adee202004-05-08 08:23:19 +0000425ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000426 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000427 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000428 int i;
429 if( p==0 ) return 0;
430 pNew = sqliteMalloc( sizeof(*pNew) );
431 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000432 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000433 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000434 if( pItem==0 ){
435 sqliteFree(pNew);
436 return 0;
437 }
drh145716b2004-09-24 12:24:06 +0000438 pOldItem = p->a;
439 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000440 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000441 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000442 if( pOldExpr->span.z!=0 && pNewExpr ){
443 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000444 ** expression list. The logic in SELECT processing that determines
445 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000446 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000447 }
drh1f3e9052002-10-31 00:09:39 +0000448 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000449 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000450 pItem->zName = sqliteStrDup(pOldItem->zName);
451 pItem->sortOrder = pOldItem->sortOrder;
452 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000453 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000454 }
455 return pNew;
456}
danielk197793758c82005-01-21 08:13:14 +0000457
458/*
459** If cursors, triggers, views and subqueries are all omitted from
460** the build, then none of the following routines, except for
461** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
462** called with a NULL argument.
463*/
danielk19776a67fe82005-02-04 04:07:16 +0000464#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
465 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000466SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000467 SrcList *pNew;
468 int i;
drh113088e2003-03-20 01:16:58 +0000469 int nByte;
drhad3cab52002-05-24 02:04:32 +0000470 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000471 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000472 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000473 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000474 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000475 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000476 struct SrcList_item *pNewItem = &pNew->a[i];
477 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000478 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000479 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
480 pNewItem->zName = sqliteStrDup(pOldItem->zName);
481 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
482 pNewItem->jointype = pOldItem->jointype;
483 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000484 pTab = pNewItem->pTab = pOldItem->pTab;
485 if( pTab ){
486 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000487 }
danielk19774adee202004-05-08 08:23:19 +0000488 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
489 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
490 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000491 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000492 }
493 return pNew;
494}
danielk19774adee202004-05-08 08:23:19 +0000495IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000496 IdList *pNew;
497 int i;
498 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000499 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000500 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000501 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000502 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000503 if( pNew->a==0 ){
504 sqliteFree(pNew);
505 return 0;
506 }
drhff78bd22002-02-27 01:47:11 +0000507 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000508 struct IdList_item *pNewItem = &pNew->a[i];
509 struct IdList_item *pOldItem = &p->a[i];
510 pNewItem->zName = sqliteStrDup(pOldItem->zName);
511 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000512 }
513 return pNew;
514}
danielk19774adee202004-05-08 08:23:19 +0000515Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000516 Select *pNew;
517 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000518 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000519 if( pNew==0 ) return 0;
520 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000521 pNew->pEList = sqlite3ExprListDup(p->pEList);
522 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
523 pNew->pWhere = sqlite3ExprDup(p->pWhere);
524 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
525 pNew->pHaving = sqlite3ExprDup(p->pHaving);
526 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000527 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000528 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000529 pNew->pLimit = sqlite3ExprDup(p->pLimit);
530 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000531 pNew->iLimit = -1;
532 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000533 pNew->ppOpenTemp = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000534 pNew->isResolved = p->isResolved;
535 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000536 return pNew;
537}
danielk197793758c82005-01-21 08:13:14 +0000538#else
539Select *sqlite3SelectDup(Select *p){
540 assert( p==0 );
541 return 0;
542}
543#endif
drhff78bd22002-02-27 01:47:11 +0000544
545
546/*
drha76b5df2002-02-23 02:32:10 +0000547** Add a new element to the end of an expression list. If pList is
548** initially NULL, then create a new expression list.
549*/
danielk19774adee202004-05-08 08:23:19 +0000550ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000551 if( pList==0 ){
552 pList = sqliteMalloc( sizeof(ExprList) );
553 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000554 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000555 }
drh4efc4752004-01-16 15:55:37 +0000556 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000557 }
drh4305d102003-07-30 12:34:12 +0000558 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000559 struct ExprList_item *a;
560 int n = pList->nAlloc*2 + 4;
561 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
562 if( a==0 ){
563 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000564 }
danielk1977d5d56522005-03-16 12:15:20 +0000565 pList->a = a;
566 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000567 }
drh4efc4752004-01-16 15:55:37 +0000568 assert( pList->a!=0 );
569 if( pExpr || pName ){
570 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
571 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000572 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000573 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000574 }
575 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000576
577no_mem:
578 /* Avoid leaking memory if malloc has failed. */
579 sqlite3ExprDelete(pExpr);
580 sqlite3ExprListDelete(pList);
581 return 0;
drha76b5df2002-02-23 02:32:10 +0000582}
583
584/*
585** Delete an entire expression list.
586*/
danielk19774adee202004-05-08 08:23:19 +0000587void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000588 int i;
drhbe5c89a2004-07-26 00:31:09 +0000589 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000590 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000591 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
592 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000593 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
594 sqlite3ExprDelete(pItem->pExpr);
595 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000596 }
597 sqliteFree(pList->a);
598 sqliteFree(pList);
599}
600
601/*
drh626a8792005-01-17 22:08:19 +0000602** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000603**
drh626a8792005-01-17 22:08:19 +0000604** The return value from xFunc determines whether the tree walk continues.
605** 0 means continue walking the tree. 1 means do not walk children
606** of the current node but continue with siblings. 2 means abandon
607** the tree walk completely.
608**
609** The return value from this routine is 1 to abandon the tree walk
610** and 0 to continue.
611*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000612static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000613static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000614 int rc;
615 if( pExpr==0 ) return 0;
616 rc = (*xFunc)(pArg, pExpr);
617 if( rc==0 ){
618 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
619 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000620 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000621 }
622 return rc>1;
623}
624
625/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000626** Call walkExprTree() for every expression in list p.
627*/
628static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
629 int i;
630 struct ExprList_item *pItem;
631 if( !p ) return 0;
632 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
633 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
634 }
635 return 0;
636}
637
638/*
639** Call walkExprTree() for every expression in Select p, not including
640** expressions that are part of sub-selects in any FROM clause or the LIMIT
641** or OFFSET expressions..
642*/
643static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
644 walkExprList(p->pEList, xFunc, pArg);
645 walkExprTree(p->pWhere, xFunc, pArg);
646 walkExprList(p->pGroupBy, xFunc, pArg);
647 walkExprTree(p->pHaving, xFunc, pArg);
648 walkExprList(p->pOrderBy, xFunc, pArg);
649 return 0;
650}
651
652
653/*
drh626a8792005-01-17 22:08:19 +0000654** This routine is designed as an xFunc for walkExprTree().
655**
656** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000657** at pExpr that the expression that contains pExpr is not a constant
658** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
659** If pExpr does does not disqualify the expression from being a constant
660** then do nothing.
661**
662** After walking the whole tree, if no nodes are found that disqualify
663** the expression as constant, then we assume the whole expression
664** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000665*/
666static int exprNodeIsConstant(void *pArg, Expr *pExpr){
667 switch( pExpr->op ){
668 case TK_ID:
669 case TK_COLUMN:
670 case TK_DOT:
671 case TK_AGG_FUNCTION:
672 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000673#ifndef SQLITE_OMIT_SUBQUERY
674 case TK_SELECT:
675 case TK_EXISTS:
676#endif
drh626a8792005-01-17 22:08:19 +0000677 *((int*)pArg) = 0;
678 return 2;
679 default:
680 return 0;
681 }
682}
683
684/*
drhfef52082000-06-06 01:50:43 +0000685** Walk an expression tree. Return 1 if the expression is constant
686** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000687**
688** For the purposes of this function, a double-quoted string (ex: "abc")
689** is considered a variable but a single-quoted string (ex: 'abc') is
690** a constant.
drhfef52082000-06-06 01:50:43 +0000691*/
danielk19774adee202004-05-08 08:23:19 +0000692int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000693 int isConst = 1;
694 walkExprTree(p, exprNodeIsConstant, &isConst);
695 return isConst;
drhfef52082000-06-06 01:50:43 +0000696}
697
698/*
drh73b211a2005-01-18 04:00:42 +0000699** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000700** to fit in a 32-bit integer, return 1 and put the value of the integer
701** in *pValue. If the expression is not an integer or if it is too big
702** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000703*/
danielk19774adee202004-05-08 08:23:19 +0000704int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000705 switch( p->op ){
706 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000707 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000708 return 1;
709 }
710 break;
drhe4de1fe2002-06-02 16:09:01 +0000711 }
drh4b59ab52002-08-24 18:24:51 +0000712 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000713 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000714 }
drhe4de1fe2002-06-02 16:09:01 +0000715 case TK_UMINUS: {
716 int v;
danielk19774adee202004-05-08 08:23:19 +0000717 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000718 *pValue = -v;
719 return 1;
720 }
721 break;
722 }
723 default: break;
724 }
725 return 0;
726}
727
728/*
drhc4a3c772001-04-04 11:48:57 +0000729** Return TRUE if the given string is a row-id column name.
730*/
danielk19774adee202004-05-08 08:23:19 +0000731int sqlite3IsRowid(const char *z){
732 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
733 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
734 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000735 return 0;
736}
737
738/*
drh8141f612004-01-25 22:44:58 +0000739** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
740** that name in the set of source tables in pSrcList and make the pExpr
741** expression node refer back to that source column. The following changes
742** are made to pExpr:
743**
744** pExpr->iDb Set the index in db->aDb[] of the database holding
745** the table.
746** pExpr->iTable Set to the cursor number for the table obtained
747** from pSrcList.
748** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000749** pExpr->op Set to TK_COLUMN.
750** pExpr->pLeft Any expression this points to is deleted
751** pExpr->pRight Any expression this points to is deleted.
752**
753** The pDbToken is the name of the database (the "X"). This value may be
754** NULL meaning that name is of the form Y.Z or Z. Any available database
755** can be used. The pTableToken is the name of the table (the "Y"). This
756** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
757** means that the form of the name is Z and that columns from any table
758** can be used.
759**
760** If the name cannot be resolved unambiguously, leave an error message
761** in pParse and return non-zero. Return zero on success.
762*/
763static int lookupName(
764 Parse *pParse, /* The parsing context */
765 Token *pDbToken, /* Name of the database containing table, or NULL */
766 Token *pTableToken, /* Name of table containing column, or NULL */
767 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000768 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000769 Expr *pExpr /* Make this EXPR node point to the selected column */
770){
771 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
772 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
773 char *zCol = 0; /* Name of the column. The "Z" */
774 int i, j; /* Loop counters */
775 int cnt = 0; /* Number of matching column names */
776 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000777 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000778 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
779 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000780 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000781
782 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000783 zDb = sqlite3NameFromToken(pDbToken);
784 zTab = sqlite3NameFromToken(pTableToken);
785 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000786 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000787 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000788 }
drh8141f612004-01-25 22:44:58 +0000789
790 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000791 while( pNC && cnt==0 ){
792 SrcList *pSrcList = pNC->pSrcList;
793 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000794
drh626a8792005-01-17 22:08:19 +0000795 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000796 if( pSrcList ){
797 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
798 Table *pTab = pItem->pTab;
799 Column *pCol;
800
801 if( pTab==0 ) continue;
802 assert( pTab->nCol>0 );
803 if( zTab ){
804 if( pItem->zAlias ){
805 char *zTabName = pItem->zAlias;
806 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
807 }else{
808 char *zTabName = pTab->zName;
809 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
810 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
811 continue;
812 }
drh626a8792005-01-17 22:08:19 +0000813 }
drh8141f612004-01-25 22:44:58 +0000814 }
danielk1977b3bce662005-01-29 08:32:43 +0000815 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000816 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000817 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000818 pMatch = pItem;
819 }
820 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
821 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000822 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000823 cnt++;
824 pExpr->iTable = pItem->iCursor;
825 pMatch = pItem;
826 pExpr->iDb = pTab->iDb;
827 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
828 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
829 pExpr->affinity = pTab->aCol[j].affinity;
830 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000831 if( pItem->jointype & JT_NATURAL ){
832 /* If this match occurred in the left table of a natural join,
833 ** then skip the right table to avoid a duplicate match */
834 pItem++;
835 i++;
836 }
drh873fac02005-06-06 17:11:46 +0000837 if( (pUsing = pItem->pUsing)!=0 ){
838 /* If this match occurs on a column that is in the USING clause
839 ** of a join, skip the search of the right table of the join
840 ** to avoid a duplicate match there. */
841 int k;
842 for(k=0; k<pUsing->nId; k++){
843 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
844 pItem++;
845 i++;
846 break;
847 }
848 }
849 }
danielk1977b3bce662005-01-29 08:32:43 +0000850 break;
851 }
drh8141f612004-01-25 22:44:58 +0000852 }
853 }
854 }
drh626a8792005-01-17 22:08:19 +0000855
856#ifndef SQLITE_OMIT_TRIGGER
857 /* If we have not already resolved the name, then maybe
858 ** it is a new.* or old.* trigger argument reference
859 */
860 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
861 TriggerStack *pTriggerStack = pParse->trigStack;
862 Table *pTab = 0;
863 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
864 pExpr->iTable = pTriggerStack->newIdx;
865 assert( pTriggerStack->pTab );
866 pTab = pTriggerStack->pTab;
867 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
868 pExpr->iTable = pTriggerStack->oldIdx;
869 assert( pTriggerStack->pTab );
870 pTab = pTriggerStack->pTab;
871 }
872
873 if( pTab ){
874 int j;
875 Column *pCol = pTab->aCol;
876
877 pExpr->iDb = pTab->iDb;
878 cntTab++;
879 for(j=0; j < pTab->nCol; j++, pCol++) {
880 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
881 cnt++;
882 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
883 pExpr->affinity = pTab->aCol[j].affinity;
884 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000885 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000886 break;
887 }
888 }
889 }
890 }
drhb7f91642004-10-31 02:22:47 +0000891#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000892
drh626a8792005-01-17 22:08:19 +0000893 /*
894 ** Perhaps the name is a reference to the ROWID
895 */
896 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
897 cnt = 1;
898 pExpr->iColumn = -1;
899 pExpr->affinity = SQLITE_AFF_INTEGER;
900 }
drh8141f612004-01-25 22:44:58 +0000901
drh626a8792005-01-17 22:08:19 +0000902 /*
903 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
904 ** might refer to an result-set alias. This happens, for example, when
905 ** we are resolving names in the WHERE clause of the following command:
906 **
907 ** SELECT a+b AS x FROM table WHERE x<10;
908 **
909 ** In cases like this, replace pExpr with a copy of the expression that
910 ** forms the result set entry ("a+b" in the example) and return immediately.
911 ** Note that the expression in the result set should have already been
912 ** resolved by the time the WHERE clause is resolved.
913 */
drh79d5f632005-01-18 17:20:10 +0000914 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000915 for(j=0; j<pEList->nExpr; j++){
916 char *zAs = pEList->a[j].zName;
917 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
918 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
919 pExpr->op = TK_AS;
920 pExpr->iColumn = j;
921 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000922 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000923 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000924 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000925 }
926 }
927 }
928
929 /* Advance to the next name context. The loop will exit when either
930 ** we have a match (cnt>0) or when we run out of name contexts.
931 */
932 if( cnt==0 ){
933 pNC = pNC->pNext;
934 }
drh8141f612004-01-25 22:44:58 +0000935 }
936
937 /*
938 ** If X and Y are NULL (in other words if only the column name Z is
939 ** supplied) and the value of Z is enclosed in double-quotes, then
940 ** Z is a string literal if it doesn't match any column names. In that
941 ** case, we need to return right away and not make any changes to
942 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000943 **
944 ** Because no reference was made to outer contexts, the pNC->nRef
945 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000946 */
947 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
948 sqliteFree(zCol);
949 return 0;
950 }
951
952 /*
953 ** cnt==0 means there was not match. cnt>1 means there were two or
954 ** more matches. Either way, we have an error.
955 */
956 if( cnt!=1 ){
957 char *z = 0;
958 char *zErr;
959 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
960 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000961 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000962 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000963 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000964 }else{
965 z = sqliteStrDup(zCol);
966 }
danielk19774adee202004-05-08 08:23:19 +0000967 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000968 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000969 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000970 }
971
drh51669862004-12-18 18:40:26 +0000972 /* If a column from a table in pSrcList is referenced, then record
973 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
974 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
975 ** column number is greater than the number of bits in the bitmask
976 ** then set the high-order bit of the bitmask.
977 */
978 if( pExpr->iColumn>=0 && pMatch!=0 ){
979 int n = pExpr->iColumn;
980 if( n>=sizeof(Bitmask)*8 ){
981 n = sizeof(Bitmask)*8-1;
982 }
983 assert( pMatch->iCursor==pExpr->iTable );
984 pMatch->colUsed |= 1<<n;
985 }
986
danielk1977d5d56522005-03-16 12:15:20 +0000987lookupname_end:
drh8141f612004-01-25 22:44:58 +0000988 /* Clean up and return
989 */
990 sqliteFree(zDb);
991 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +0000992 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000993 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000994 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000995 pExpr->pRight = 0;
996 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +0000997lookupname_end_2:
998 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +0000999 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001000 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001001 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001002 if( pMatch && !pMatch->pSelect ){
1003 pExpr->pTab = pMatch->pTab;
1004 }
drh15ccce12005-05-23 15:06:39 +00001005 /* Increment the nRef value on all name contexts from TopNC up to
1006 ** the point where the name matched. */
1007 for(;;){
1008 assert( pTopNC!=0 );
1009 pTopNC->nRef++;
1010 if( pTopNC==pNC ) break;
1011 pTopNC = pTopNC->pNext;
1012 }
1013 return 0;
1014 } else {
1015 return 1;
drh626a8792005-01-17 22:08:19 +00001016 }
drh8141f612004-01-25 22:44:58 +00001017}
1018
1019/*
drh626a8792005-01-17 22:08:19 +00001020** This routine is designed as an xFunc for walkExprTree().
1021**
drh73b211a2005-01-18 04:00:42 +00001022** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001023** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001024** the tree or 2 to abort the tree walk.
1025**
1026** This routine also does error checking and name resolution for
1027** function names. The operator for aggregate functions is changed
1028** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001029*/
1030static int nameResolverStep(void *pArg, Expr *pExpr){
1031 NameContext *pNC = (NameContext*)pArg;
1032 SrcList *pSrcList;
1033 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001034
danielk1977b3bce662005-01-29 08:32:43 +00001035 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001036 assert( pNC!=0 );
1037 pSrcList = pNC->pSrcList;
1038 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001039
drh626a8792005-01-17 22:08:19 +00001040 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1041 ExprSetProperty(pExpr, EP_Resolved);
1042#ifndef NDEBUG
1043 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001044 int i;
drh626a8792005-01-17 22:08:19 +00001045 for(i=0; i<pSrcList->nSrc; i++){
1046 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1047 }
1048 }
1049#endif
1050 switch( pExpr->op ){
1051 /* Double-quoted strings (ex: "abc") are used as identifiers if
1052 ** possible. Otherwise they remain as strings. Single-quoted
1053 ** strings (ex: 'abc') are always string literals.
1054 */
1055 case TK_STRING: {
1056 if( pExpr->token.z[0]=='\'' ) break;
1057 /* Fall thru into the TK_ID case if this is a double-quoted string */
1058 }
1059 /* A lone identifier is the name of a column.
1060 */
1061 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001062 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1063 return 1;
1064 }
1065
1066 /* A table name and column name: ID.ID
1067 ** Or a database, table and column: ID.ID.ID
1068 */
1069 case TK_DOT: {
1070 Token *pColumn;
1071 Token *pTable;
1072 Token *pDb;
1073 Expr *pRight;
1074
danielk1977b3bce662005-01-29 08:32:43 +00001075 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001076 pRight = pExpr->pRight;
1077 if( pRight->op==TK_ID ){
1078 pDb = 0;
1079 pTable = &pExpr->pLeft->token;
1080 pColumn = &pRight->token;
1081 }else{
1082 assert( pRight->op==TK_DOT );
1083 pDb = &pExpr->pLeft->token;
1084 pTable = &pRight->pLeft->token;
1085 pColumn = &pRight->pRight->token;
1086 }
1087 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1088 return 1;
1089 }
1090
1091 /* Resolve function names
1092 */
drhb71090f2005-05-23 17:26:51 +00001093 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001094 case TK_FUNCTION: {
1095 ExprList *pList = pExpr->pList; /* The argument list */
1096 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1097 int no_such_func = 0; /* True if no such function exists */
1098 int wrong_num_args = 0; /* True if wrong number of arguments */
1099 int is_agg = 0; /* True if is an aggregate function */
1100 int i;
1101 int nId; /* Number of characters in function name */
1102 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001103 FuncDef *pDef; /* Information about the function */
1104 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001105
drhb71090f2005-05-23 17:26:51 +00001106 zId = pExpr->token.z;
1107 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001108 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1109 if( pDef==0 ){
1110 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1111 if( pDef==0 ){
1112 no_such_func = 1;
1113 }else{
1114 wrong_num_args = 1;
1115 }
1116 }else{
1117 is_agg = pDef->xFunc==0;
1118 }
1119 if( is_agg && !pNC->allowAgg ){
1120 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1121 pNC->nErr++;
1122 is_agg = 0;
1123 }else if( no_such_func ){
1124 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1125 pNC->nErr++;
1126 }else if( wrong_num_args ){
1127 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1128 nId, zId);
1129 pNC->nErr++;
1130 }
1131 if( is_agg ){
1132 pExpr->op = TK_AGG_FUNCTION;
1133 pNC->hasAgg = 1;
1134 }
drh73b211a2005-01-18 04:00:42 +00001135 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001136 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001137 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001138 }
drh73b211a2005-01-18 04:00:42 +00001139 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001140 /* FIX ME: Compute pExpr->affinity based on the expected return
1141 ** type of the function
1142 */
1143 return is_agg;
1144 }
danielk1977b3bce662005-01-29 08:32:43 +00001145#ifndef SQLITE_OMIT_SUBQUERY
1146 case TK_SELECT:
1147 case TK_EXISTS:
1148#endif
1149 case TK_IN: {
1150 if( pExpr->pSelect ){
1151 int nRef = pNC->nRef;
1152 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1153 assert( pNC->nRef>=nRef );
1154 if( nRef!=pNC->nRef ){
1155 ExprSetProperty(pExpr, EP_VarSelect);
1156 }
1157 }
1158 }
drh626a8792005-01-17 22:08:19 +00001159 }
1160 return 0;
1161}
1162
1163/*
drhcce7d172000-05-31 15:34:51 +00001164** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001165** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001166** index to the table in the table list and a column offset. The
1167** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1168** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001169** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001170** VDBE cursor number for a cursor that is pointing into the referenced
1171** table. The Expr.iColumn value is changed to the index of the column
1172** of the referenced table. The Expr.iColumn value for the special
1173** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1174** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001175**
drh626a8792005-01-17 22:08:19 +00001176** Also resolve function names and check the functions for proper
1177** usage. Make sure all function names are recognized and all functions
1178** have the correct number of arguments. Leave an error message
1179** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1180**
drh73b211a2005-01-18 04:00:42 +00001181** If the expression contains aggregate functions then set the EP_Agg
1182** property on the expression.
drh626a8792005-01-17 22:08:19 +00001183*/
1184int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001185 NameContext *pNC, /* Namespace to resolve expressions in. */
1186 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001187){
drh73b211a2005-01-18 04:00:42 +00001188 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001189 walkExprTree(pExpr, nameResolverStep, pNC);
1190 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001191 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001192 }
1193 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001194}
1195
drh1398ad32005-01-19 23:24:50 +00001196/*
1197** A pointer instance of this structure is used to pass information
1198** through walkExprTree into codeSubqueryStep().
1199*/
1200typedef struct QueryCoder QueryCoder;
1201struct QueryCoder {
1202 Parse *pParse; /* The parsing context */
1203 NameContext *pNC; /* Namespace of first enclosing query */
1204};
1205
drh626a8792005-01-17 22:08:19 +00001206
1207/*
1208** Generate code for subqueries and IN operators.
1209**
drh73b211a2005-01-18 04:00:42 +00001210** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001211**
1212** expr IN (exprlist)
1213** and
1214** expr IN (SELECT ...)
1215**
1216** The first form is handled by creating a set holding the list
1217** of allowed values. The second form causes the SELECT to generate
1218** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001219*/
drh51522cd2005-01-20 13:36:19 +00001220#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001221void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1222 int label = 0; /* Address after sub-select code */
1223 Vdbe *v = sqlite3GetVdbe(pParse);
1224 if( v==0 ) return;
1225
1226 /* If this is not a variable (correlated) select, then execute
1227 ** it only once. Unless this is part of a trigger program. In
1228 ** that case re-execute every time (this could be optimized).
1229 */
1230 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1231 int mem = pParse->nMem++;
1232 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1233 label = sqlite3VdbeMakeLabel(v);
1234 sqlite3VdbeAddOp(v, OP_If, 0, label);
1235 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1236 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1237 }
1238
1239 if( pExpr->pSelect ){
1240 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1241 }
drh6a3ea0e2003-05-02 14:32:12 +00001242
drhcce7d172000-05-31 15:34:51 +00001243 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001244 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001245 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001246 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001247 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001248
danielk1977bf3b7212004-05-18 10:06:24 +00001249 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001250
1251 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1252 ** expression it is handled the same way. A temporary table is
1253 ** filled with single-field index keys representing the results
1254 ** from the SELECT or the <exprlist>.
1255 **
1256 ** If the 'x' expression is a column value, or the SELECT...
1257 ** statement returns a column value, then the affinity of that
1258 ** column is used to build the index keys. If both 'x' and the
1259 ** SELECT... statement are columns, then numeric affinity is used
1260 ** if either column has NUMERIC or INTEGER affinity. If neither
1261 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1262 ** is used.
1263 */
1264 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001265 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001266 memset(&keyInfo, 0, sizeof(keyInfo));
1267 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001268 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001269
drhfef52082000-06-06 01:50:43 +00001270 if( pExpr->pSelect ){
1271 /* Case 1: expr IN (SELECT ...)
1272 **
danielk1977e014a832004-05-17 10:48:57 +00001273 ** Generate code to write the results of the select into the temporary
1274 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001275 */
danielk1977e014a832004-05-17 10:48:57 +00001276 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001277 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001278 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001279 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001280 pEList = pExpr->pSelect->pEList;
1281 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001282 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001283 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001284 }
drhfef52082000-06-06 01:50:43 +00001285 }else if( pExpr->pList ){
1286 /* Case 2: expr IN (exprlist)
1287 **
danielk1977e014a832004-05-17 10:48:57 +00001288 ** For each expression, build an index key from the evaluation and
1289 ** store it in the temporary table. If <expr> is a column, then use
1290 ** that columns affinity when building index keys. If <expr> is not
1291 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001292 */
danielk1977e014a832004-05-17 10:48:57 +00001293 int i;
danielk1977e014a832004-05-17 10:48:57 +00001294 if( !affinity ){
1295 affinity = SQLITE_AFF_NUMERIC;
1296 }
danielk19770202b292004-06-09 09:55:16 +00001297 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001298
1299 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001300 for(i=0; i<pExpr->pList->nExpr; i++){
1301 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001302
1303 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001304 if( !sqlite3ExprIsConstant(pE2) ){
1305 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001306 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001307 return;
drh4794b982000-06-06 13:54:14 +00001308 }
danielk1977e014a832004-05-17 10:48:57 +00001309
1310 /* Evaluate the expression and insert it into the temp table */
1311 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001312 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001313 sqlite3VdbeAddOp(v, OP_IdxInsert, 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 ){
drhf0863fe2005-06-12 21:35:51 +00001385 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001386 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{
drhf0863fe2005-06-12 21:35:51 +00001397 sqlite3VdbeAddOp(v, OP_Rowid, 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 }
drhf0863fe2005-06-12 21:35:51 +00001413 case TK_NULL: {
1414 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1415 break;
1416 }
danielk19775338a5f2005-01-20 13:03:10 +00001417#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001418 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001419 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001420 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1421 sqlite3VdbeDequoteP3(v, -1);
1422 break;
1423 }
danielk19775338a5f2005-01-20 13:03:10 +00001424#endif
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 }
drh487e2622005-06-25 18:42:14 +00001436#ifndef SQLITE_OMIT_CAST
1437 case TK_CAST: {
1438 /* Expressions of the form: CAST(pLeft AS token) */
1439 int aff, op;
1440 sqlite3ExprCode(pParse, pExpr->pLeft);
1441 aff = sqlite3AffinityType(&pExpr->token);
1442 switch( aff ){
1443 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1444 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1445 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1446 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1447 }
1448 sqlite3VdbeAddOp(v, op, 0, 0);
1449 break;
1450 }
1451#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001452 case TK_LT:
1453 case TK_LE:
1454 case TK_GT:
1455 case TK_GE:
1456 case TK_NE:
1457 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001458 assert( TK_LT==OP_Lt );
1459 assert( TK_LE==OP_Le );
1460 assert( TK_GT==OP_Gt );
1461 assert( TK_GE==OP_Ge );
1462 assert( TK_EQ==OP_Eq );
1463 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001464 sqlite3ExprCode(pParse, pExpr->pLeft);
1465 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001466 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001467 break;
drhc9b84a12002-06-20 11:36:48 +00001468 }
drhcce7d172000-05-31 15:34:51 +00001469 case TK_AND:
1470 case TK_OR:
1471 case TK_PLUS:
1472 case TK_STAR:
1473 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001474 case TK_REM:
1475 case TK_BITAND:
1476 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001477 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001478 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001479 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001480 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001481 assert( TK_AND==OP_And );
1482 assert( TK_OR==OP_Or );
1483 assert( TK_PLUS==OP_Add );
1484 assert( TK_MINUS==OP_Subtract );
1485 assert( TK_REM==OP_Remainder );
1486 assert( TK_BITAND==OP_BitAnd );
1487 assert( TK_BITOR==OP_BitOr );
1488 assert( TK_SLASH==OP_Divide );
1489 assert( TK_LSHIFT==OP_ShiftLeft );
1490 assert( TK_RSHIFT==OP_ShiftRight );
1491 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3ExprCode(pParse, pExpr->pLeft);
1493 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001494 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001495 break;
1496 }
drhcce7d172000-05-31 15:34:51 +00001497 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001498 Expr *pLeft = pExpr->pLeft;
1499 assert( pLeft );
1500 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1501 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001502 char *z = sqliteMalloc( p->n + 2 );
1503 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001504 if( pLeft->op==TK_FLOAT ){
1505 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001506 }else{
drhfec19aa2004-05-19 20:41:03 +00001507 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001508 }
drh6e142f52000-06-08 13:36:40 +00001509 sqliteFree(z);
1510 break;
1511 }
drh1ccde152000-06-17 13:12:39 +00001512 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001513 }
drhbf4133c2001-10-13 02:59:08 +00001514 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001515 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001516 assert( TK_BITNOT==OP_BitNot );
1517 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001518 sqlite3ExprCode(pParse, pExpr->pLeft);
1519 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001520 break;
1521 }
1522 case TK_ISNULL:
1523 case TK_NOTNULL: {
1524 int dest;
drhf2bc0132004-10-04 13:19:23 +00001525 assert( TK_ISNULL==OP_IsNull );
1526 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001527 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1528 sqlite3ExprCode(pParse, pExpr->pLeft);
1529 dest = sqlite3VdbeCurrentAddr(v) + 2;
1530 sqlite3VdbeAddOp(v, op, 1, dest);
1531 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001532 break;
drhcce7d172000-05-31 15:34:51 +00001533 }
drh22827922000-06-06 17:27:05 +00001534 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001535 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001536 break;
1537 }
drhb71090f2005-05-23 17:26:51 +00001538 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001539 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001540 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001541 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001542 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001543 int nId;
1544 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001545 int p2 = 0;
1546 int i;
danielk1977d8123362004-06-12 09:25:12 +00001547 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001548 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001549 zId = pExpr->token.z;
1550 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001551 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001552 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001553 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001554 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001555 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1556 p2 |= (1<<i);
1557 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001558 if( pDef->needCollSeq && !pColl ){
1559 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1560 }
1561 }
1562 if( pDef->needCollSeq ){
1563 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001564 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001565 }
1566 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001567 break;
1568 }
drhfe2093d2005-01-20 22:48:47 +00001569#ifndef SQLITE_OMIT_SUBQUERY
1570 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001571 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001572 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001573 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001574 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001575 break;
1576 }
drhfef52082000-06-06 01:50:43 +00001577 case TK_IN: {
1578 int addr;
drh94a11212004-09-25 13:12:14 +00001579 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001580 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001581
1582 /* Figure out the affinity to use to create a key from the results
1583 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001584 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001585 */
drh94a11212004-09-25 13:12:14 +00001586 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001587
danielk19774adee202004-05-08 08:23:19 +00001588 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001589
1590 /* Code the <expr> from "<expr> IN (...)". The temporary table
1591 ** pExpr->iTable contains the values that make up the (...) set.
1592 */
danielk19774adee202004-05-08 08:23:19 +00001593 sqlite3ExprCode(pParse, pExpr->pLeft);
1594 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001595 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001596 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001597 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001598 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001599 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001600 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1601 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1602
drhfef52082000-06-06 01:50:43 +00001603 break;
1604 }
danielk197793758c82005-01-21 08:13:14 +00001605#endif
drhfef52082000-06-06 01:50:43 +00001606 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001607 Expr *pLeft = pExpr->pLeft;
1608 struct ExprList_item *pLItem = pExpr->pList->a;
1609 Expr *pRight = pLItem->pExpr;
1610 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001611 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001612 sqlite3ExprCode(pParse, pRight);
1613 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001614 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001615 pLItem++;
1616 pRight = pLItem->pExpr;
1617 sqlite3ExprCode(pParse, pRight);
1618 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001619 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001620 break;
1621 }
drh51e9a442004-01-16 16:42:53 +00001622 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001623 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001624 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001625 break;
1626 }
drh17a7f8d2002-03-24 13:13:27 +00001627 case TK_CASE: {
1628 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001629 int jumpInst;
1630 int addr;
1631 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001632 int i;
drhbe5c89a2004-07-26 00:31:09 +00001633 ExprList *pEList;
1634 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001635
1636 assert(pExpr->pList);
1637 assert((pExpr->pList->nExpr % 2) == 0);
1638 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001639 pEList = pExpr->pList;
1640 aListelem = pEList->a;
1641 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001642 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001643 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001644 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001645 }
drhf5905aa2002-05-26 20:54:33 +00001646 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001647 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001648 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001649 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001650 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1651 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001652 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001653 }else{
danielk19774adee202004-05-08 08:23:19 +00001654 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001655 }
drhbe5c89a2004-07-26 00:31:09 +00001656 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001657 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1658 addr = sqlite3VdbeCurrentAddr(v);
1659 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001660 }
drhf570f012002-05-31 15:51:25 +00001661 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001662 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001663 }
drh17a7f8d2002-03-24 13:13:27 +00001664 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001665 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001666 }else{
drhf0863fe2005-06-12 21:35:51 +00001667 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001668 }
danielk19774adee202004-05-08 08:23:19 +00001669 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001670 break;
1671 }
danielk19775338a5f2005-01-20 13:03:10 +00001672#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001673 case TK_RAISE: {
1674 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001675 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001676 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001677 return;
1678 }
drhad6d9462004-09-19 02:15:24 +00001679 if( pExpr->iColumn!=OE_Ignore ){
1680 assert( pExpr->iColumn==OE_Rollback ||
1681 pExpr->iColumn == OE_Abort ||
1682 pExpr->iColumn == OE_Fail );
1683 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1684 pExpr->token.z, pExpr->token.n);
1685 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001686 } else {
drhad6d9462004-09-19 02:15:24 +00001687 assert( pExpr->iColumn == OE_Ignore );
1688 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1689 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1690 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001691 }
drh17a7f8d2002-03-24 13:13:27 +00001692 }
danielk19775338a5f2005-01-20 13:03:10 +00001693#endif
drh17a7f8d2002-03-24 13:13:27 +00001694 break;
drhcce7d172000-05-31 15:34:51 +00001695 }
drhcce7d172000-05-31 15:34:51 +00001696}
1697
danielk197793758c82005-01-21 08:13:14 +00001698#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001699/*
drh25303782004-12-07 15:41:48 +00001700** Generate code that evalutes the given expression and leaves the result
1701** on the stack. See also sqlite3ExprCode().
1702**
1703** This routine might also cache the result and modify the pExpr tree
1704** so that it will make use of the cached result on subsequent evaluations
1705** rather than evaluate the whole expression again. Trivial expressions are
1706** not cached. If the expression is cached, its result is stored in a
1707** memory location.
1708*/
1709void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1710 Vdbe *v = pParse->pVdbe;
1711 int iMem;
1712 int addr1, addr2;
1713 if( v==0 ) return;
1714 addr1 = sqlite3VdbeCurrentAddr(v);
1715 sqlite3ExprCode(pParse, pExpr);
1716 addr2 = sqlite3VdbeCurrentAddr(v);
1717 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1718 iMem = pExpr->iTable = pParse->nMem++;
1719 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1720 pExpr->op = TK_REGISTER;
1721 }
1722}
danielk197793758c82005-01-21 08:13:14 +00001723#endif
drh25303782004-12-07 15:41:48 +00001724
1725/*
drh268380c2004-02-25 13:47:31 +00001726** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001727** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001728**
1729** Return the number of elements pushed onto the stack.
1730*/
danielk19774adee202004-05-08 08:23:19 +00001731int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001732 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001733 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001734){
1735 struct ExprList_item *pItem;
1736 int i, n;
1737 Vdbe *v;
1738 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001739 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001740 n = pList->nExpr;
1741 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001742 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001743 }
drhf9b596e2004-05-26 16:54:42 +00001744 return n;
drh268380c2004-02-25 13:47:31 +00001745}
1746
1747/*
drhcce7d172000-05-31 15:34:51 +00001748** Generate code for a boolean expression such that a jump is made
1749** to the label "dest" if the expression is true but execution
1750** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001751**
1752** If the expression evaluates to NULL (neither true nor false), then
1753** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001754**
1755** This code depends on the fact that certain token values (ex: TK_EQ)
1756** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1757** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1758** the make process cause these values to align. Assert()s in the code
1759** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001760*/
danielk19774adee202004-05-08 08:23:19 +00001761void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001762 Vdbe *v = pParse->pVdbe;
1763 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001764 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001765 op = pExpr->op;
1766 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001767 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001768 int d2 = sqlite3VdbeMakeLabel(v);
1769 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1770 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1771 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001772 break;
1773 }
1774 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001775 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1776 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001777 break;
1778 }
1779 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001780 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001781 break;
1782 }
1783 case TK_LT:
1784 case TK_LE:
1785 case TK_GT:
1786 case TK_GE:
1787 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001788 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001789 assert( TK_LT==OP_Lt );
1790 assert( TK_LE==OP_Le );
1791 assert( TK_GT==OP_Gt );
1792 assert( TK_GE==OP_Ge );
1793 assert( TK_EQ==OP_Eq );
1794 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001795 sqlite3ExprCode(pParse, pExpr->pLeft);
1796 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001797 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001798 break;
1799 }
1800 case TK_ISNULL:
1801 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001802 assert( TK_ISNULL==OP_IsNull );
1803 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001804 sqlite3ExprCode(pParse, pExpr->pLeft);
1805 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001806 break;
1807 }
drhfef52082000-06-06 01:50:43 +00001808 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001809 /* The expression "x BETWEEN y AND z" is implemented as:
1810 **
1811 ** 1 IF (x < y) GOTO 3
1812 ** 2 IF (x <= z) GOTO <dest>
1813 ** 3 ...
1814 */
drhf5905aa2002-05-26 20:54:33 +00001815 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001816 Expr *pLeft = pExpr->pLeft;
1817 Expr *pRight = pExpr->pList->a[0].pExpr;
1818 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001819 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001820 sqlite3ExprCode(pParse, pRight);
1821 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001822
drhbe5c89a2004-07-26 00:31:09 +00001823 pRight = pExpr->pList->a[1].pExpr;
1824 sqlite3ExprCode(pParse, pRight);
1825 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001826
danielk19774adee202004-05-08 08:23:19 +00001827 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1828 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1829 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001830 break;
1831 }
drhcce7d172000-05-31 15:34:51 +00001832 default: {
danielk19774adee202004-05-08 08:23:19 +00001833 sqlite3ExprCode(pParse, pExpr);
1834 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001835 break;
1836 }
1837 }
1838}
1839
1840/*
drh66b89c82000-11-28 20:47:17 +00001841** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001842** to the label "dest" if the expression is false but execution
1843** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001844**
1845** If the expression evaluates to NULL (neither true nor false) then
1846** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001847*/
danielk19774adee202004-05-08 08:23:19 +00001848void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001849 Vdbe *v = pParse->pVdbe;
1850 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001851 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001852
1853 /* The value of pExpr->op and op are related as follows:
1854 **
1855 ** pExpr->op op
1856 ** --------- ----------
1857 ** TK_ISNULL OP_NotNull
1858 ** TK_NOTNULL OP_IsNull
1859 ** TK_NE OP_Eq
1860 ** TK_EQ OP_Ne
1861 ** TK_GT OP_Le
1862 ** TK_LE OP_Gt
1863 ** TK_GE OP_Lt
1864 ** TK_LT OP_Ge
1865 **
1866 ** For other values of pExpr->op, op is undefined and unused.
1867 ** The value of TK_ and OP_ constants are arranged such that we
1868 ** can compute the mapping above using the following expression.
1869 ** Assert()s verify that the computation is correct.
1870 */
1871 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1872
1873 /* Verify correct alignment of TK_ and OP_ constants
1874 */
1875 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1876 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1877 assert( pExpr->op!=TK_NE || op==OP_Eq );
1878 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1879 assert( pExpr->op!=TK_LT || op==OP_Ge );
1880 assert( pExpr->op!=TK_LE || op==OP_Gt );
1881 assert( pExpr->op!=TK_GT || op==OP_Le );
1882 assert( pExpr->op!=TK_GE || op==OP_Lt );
1883
drhcce7d172000-05-31 15:34:51 +00001884 switch( pExpr->op ){
1885 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001886 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1887 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001888 break;
1889 }
1890 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001891 int d2 = sqlite3VdbeMakeLabel(v);
1892 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1893 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1894 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001895 break;
1896 }
1897 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001898 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001899 break;
1900 }
1901 case TK_LT:
1902 case TK_LE:
1903 case TK_GT:
1904 case TK_GE:
1905 case TK_NE:
1906 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001907 sqlite3ExprCode(pParse, pExpr->pLeft);
1908 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001909 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001910 break;
1911 }
drhcce7d172000-05-31 15:34:51 +00001912 case TK_ISNULL:
1913 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001914 sqlite3ExprCode(pParse, pExpr->pLeft);
1915 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001916 break;
1917 }
drhfef52082000-06-06 01:50:43 +00001918 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001919 /* The expression is "x BETWEEN y AND z". It is implemented as:
1920 **
1921 ** 1 IF (x >= y) GOTO 3
1922 ** 2 GOTO <dest>
1923 ** 3 IF (x > z) GOTO <dest>
1924 */
drhfef52082000-06-06 01:50:43 +00001925 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001926 Expr *pLeft = pExpr->pLeft;
1927 Expr *pRight = pExpr->pList->a[0].pExpr;
1928 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001929 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001930 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001931 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001932 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1933
danielk19774adee202004-05-08 08:23:19 +00001934 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1935 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001936 pRight = pExpr->pList->a[1].pExpr;
1937 sqlite3ExprCode(pParse, pRight);
1938 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001939 break;
1940 }
drhcce7d172000-05-31 15:34:51 +00001941 default: {
danielk19774adee202004-05-08 08:23:19 +00001942 sqlite3ExprCode(pParse, pExpr);
1943 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001944 break;
1945 }
1946 }
1947}
drh22827922000-06-06 17:27:05 +00001948
1949/*
1950** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1951** if they are identical and return FALSE if they differ in any way.
1952*/
danielk19774adee202004-05-08 08:23:19 +00001953int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001954 int i;
1955 if( pA==0 ){
1956 return pB==0;
1957 }else if( pB==0 ){
1958 return 0;
1959 }
1960 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001961 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1962 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001963 if( pA->pList ){
1964 if( pB->pList==0 ) return 0;
1965 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1966 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001967 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001968 return 0;
1969 }
1970 }
1971 }else if( pB->pList ){
1972 return 0;
1973 }
1974 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001975 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001976 if( pA->token.z ){
1977 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001978 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001979 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001980 }
1981 return 1;
1982}
1983
1984/*
1985** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001986** The new element is initialized to zero. The calling function is
1987** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001988*/
1989static int appendAggInfo(Parse *pParse){
1990 if( (pParse->nAgg & 0x7)==0 ){
1991 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001992 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1993 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001994 return -1;
1995 }
drh6d4abfb2001-10-22 02:58:08 +00001996 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001997 }
1998 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1999 return pParse->nAgg++;
2000}
2001
2002/*
drh626a8792005-01-17 22:08:19 +00002003** This is an xFunc for walkExprTree() used to implement
2004** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2005** for additional information.
drh22827922000-06-06 17:27:05 +00002006**
drh626a8792005-01-17 22:08:19 +00002007** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002008*/
drh626a8792005-01-17 22:08:19 +00002009static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002010 int i;
2011 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00002012 NameContext *pNC = (NameContext *)pArg;
2013 Parse *pParse = pNC->pParse;
2014 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00002015
drh22827922000-06-06 17:27:05 +00002016 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002017 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002018 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2019 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2020 aAgg = pParse->aAgg;
2021 for(i=0; i<pParse->nAgg; i++){
2022 if( aAgg[i].isAgg ) continue;
2023 if( aAgg[i].pExpr->iTable==pExpr->iTable
2024 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2025 break;
2026 }
2027 }
2028 if( i>=pParse->nAgg ){
2029 i = appendAggInfo(pParse);
2030 if( i<0 ) return 1;
2031 pParse->aAgg[i].isAgg = 0;
2032 pParse->aAgg[i].pExpr = pExpr;
2033 }
2034 pExpr->iAgg = i;
2035 pExpr->iAggCtx = pNC->nDepth;
2036 return 1;
drh22827922000-06-06 17:27:05 +00002037 }
2038 }
drh626a8792005-01-17 22:08:19 +00002039 return 1;
drh22827922000-06-06 17:27:05 +00002040 }
2041 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002042 if( pNC->nDepth==0 ){
2043 aAgg = pParse->aAgg;
2044 for(i=0; i<pParse->nAgg; i++){
2045 if( !aAgg[i].isAgg ) continue;
2046 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2047 break;
2048 }
drh22827922000-06-06 17:27:05 +00002049 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002050 if( i>=pParse->nAgg ){
2051 u8 enc = pParse->db->enc;
2052 i = appendAggInfo(pParse);
2053 if( i<0 ) return 1;
2054 pParse->aAgg[i].isAgg = 1;
2055 pParse->aAgg[i].pExpr = pExpr;
2056 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2057 pExpr->token.z, pExpr->token.n,
2058 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2059 }
2060 pExpr->iAgg = i;
2061 return 1;
drh22827922000-06-06 17:27:05 +00002062 }
drh22827922000-06-06 17:27:05 +00002063 }
2064 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002065 if( pExpr->pSelect ){
2066 pNC->nDepth++;
2067 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2068 pNC->nDepth--;
2069 }
drh626a8792005-01-17 22:08:19 +00002070 return 0;
2071}
2072
2073/*
2074** Analyze the given expression looking for aggregate functions and
2075** for variables that need to be added to the pParse->aAgg[] array.
2076** Make additional entries to the pParse->aAgg[] array as necessary.
2077**
2078** This routine should only be called after the expression has been
2079** analyzed by sqlite3ExprResolveNames().
2080**
2081** If errors are seen, leave an error message in zErrMsg and return
2082** the number of errors.
2083*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002084int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2085 int nErr = pNC->pParse->nErr;
2086 walkExprTree(pExpr, analyzeAggregate, pNC);
2087 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002088}