blob: 2303a7a16af08931343609d8dc3c1b69cae1985d [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**
drh9170dd72005-07-08 17:13:46 +000015** $Id: expr.c,v 1.210 2005/07/08 17:13:47 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;
drh9170dd72005-07-08 17:13:46 +0000533 pNew->ppOpenVirtual = 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 ){
drheb55bd22005-06-30 17:04:21 +0000668 /* Consider functions to be constant if all their arguments are constant
669 ** and *pArg==2 */
670 case TK_FUNCTION:
671 if( *((int*)pArg)==2 ) return 0;
672 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000673 case TK_ID:
674 case TK_COLUMN:
675 case TK_DOT:
676 case TK_AGG_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000677#ifndef SQLITE_OMIT_SUBQUERY
678 case TK_SELECT:
679 case TK_EXISTS:
680#endif
drh626a8792005-01-17 22:08:19 +0000681 *((int*)pArg) = 0;
682 return 2;
683 default:
684 return 0;
685 }
686}
687
688/*
drhfef52082000-06-06 01:50:43 +0000689** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000690** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000691**
692** For the purposes of this function, a double-quoted string (ex: "abc")
693** is considered a variable but a single-quoted string (ex: 'abc') is
694** a constant.
drhfef52082000-06-06 01:50:43 +0000695*/
danielk19774adee202004-05-08 08:23:19 +0000696int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000697 int isConst = 1;
698 walkExprTree(p, exprNodeIsConstant, &isConst);
699 return isConst;
drhfef52082000-06-06 01:50:43 +0000700}
701
702/*
drheb55bd22005-06-30 17:04:21 +0000703** Walk an expression tree. Return 1 if the expression is constant
704** or a function call with constant arguments. Return and 0 if there
705** are any variables.
706**
707** For the purposes of this function, a double-quoted string (ex: "abc")
708** is considered a variable but a single-quoted string (ex: 'abc') is
709** a constant.
710*/
711int sqlite3ExprIsConstantOrFunction(Expr *p){
712 int isConst = 2;
713 walkExprTree(p, exprNodeIsConstant, &isConst);
714 return isConst!=0;
715}
716
717/*
drh73b211a2005-01-18 04:00:42 +0000718** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000719** to fit in a 32-bit integer, return 1 and put the value of the integer
720** in *pValue. If the expression is not an integer or if it is too big
721** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000722*/
danielk19774adee202004-05-08 08:23:19 +0000723int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000724 switch( p->op ){
725 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000726 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000727 return 1;
728 }
729 break;
drhe4de1fe2002-06-02 16:09:01 +0000730 }
drh4b59ab52002-08-24 18:24:51 +0000731 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000732 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000733 }
drhe4de1fe2002-06-02 16:09:01 +0000734 case TK_UMINUS: {
735 int v;
danielk19774adee202004-05-08 08:23:19 +0000736 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000737 *pValue = -v;
738 return 1;
739 }
740 break;
741 }
742 default: break;
743 }
744 return 0;
745}
746
747/*
drhc4a3c772001-04-04 11:48:57 +0000748** Return TRUE if the given string is a row-id column name.
749*/
danielk19774adee202004-05-08 08:23:19 +0000750int sqlite3IsRowid(const char *z){
751 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
752 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
753 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000754 return 0;
755}
756
757/*
drh8141f612004-01-25 22:44:58 +0000758** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
759** that name in the set of source tables in pSrcList and make the pExpr
760** expression node refer back to that source column. The following changes
761** are made to pExpr:
762**
763** pExpr->iDb Set the index in db->aDb[] of the database holding
764** the table.
765** pExpr->iTable Set to the cursor number for the table obtained
766** from pSrcList.
767** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000768** pExpr->op Set to TK_COLUMN.
769** pExpr->pLeft Any expression this points to is deleted
770** pExpr->pRight Any expression this points to is deleted.
771**
772** The pDbToken is the name of the database (the "X"). This value may be
773** NULL meaning that name is of the form Y.Z or Z. Any available database
774** can be used. The pTableToken is the name of the table (the "Y"). This
775** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
776** means that the form of the name is Z and that columns from any table
777** can be used.
778**
779** If the name cannot be resolved unambiguously, leave an error message
780** in pParse and return non-zero. Return zero on success.
781*/
782static int lookupName(
783 Parse *pParse, /* The parsing context */
784 Token *pDbToken, /* Name of the database containing table, or NULL */
785 Token *pTableToken, /* Name of table containing column, or NULL */
786 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000787 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000788 Expr *pExpr /* Make this EXPR node point to the selected column */
789){
790 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
791 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
792 char *zCol = 0; /* Name of the column. The "Z" */
793 int i, j; /* Loop counters */
794 int cnt = 0; /* Number of matching column names */
795 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000796 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000797 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
798 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000799 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000800
801 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000802 zDb = sqlite3NameFromToken(pDbToken);
803 zTab = sqlite3NameFromToken(pTableToken);
804 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000805 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000806 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000807 }
drh8141f612004-01-25 22:44:58 +0000808
809 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000810 while( pNC && cnt==0 ){
811 SrcList *pSrcList = pNC->pSrcList;
812 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000813
drh626a8792005-01-17 22:08:19 +0000814 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000815 if( pSrcList ){
816 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
817 Table *pTab = pItem->pTab;
818 Column *pCol;
819
820 if( pTab==0 ) continue;
821 assert( pTab->nCol>0 );
822 if( zTab ){
823 if( pItem->zAlias ){
824 char *zTabName = pItem->zAlias;
825 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
826 }else{
827 char *zTabName = pTab->zName;
828 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
829 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
830 continue;
831 }
drh626a8792005-01-17 22:08:19 +0000832 }
drh8141f612004-01-25 22:44:58 +0000833 }
danielk1977b3bce662005-01-29 08:32:43 +0000834 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000835 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000836 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000837 pMatch = pItem;
838 }
839 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
840 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000841 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000842 cnt++;
843 pExpr->iTable = pItem->iCursor;
844 pMatch = pItem;
845 pExpr->iDb = pTab->iDb;
846 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
847 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
848 pExpr->affinity = pTab->aCol[j].affinity;
849 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000850 if( pItem->jointype & JT_NATURAL ){
851 /* If this match occurred in the left table of a natural join,
852 ** then skip the right table to avoid a duplicate match */
853 pItem++;
854 i++;
855 }
drh873fac02005-06-06 17:11:46 +0000856 if( (pUsing = pItem->pUsing)!=0 ){
857 /* If this match occurs on a column that is in the USING clause
858 ** of a join, skip the search of the right table of the join
859 ** to avoid a duplicate match there. */
860 int k;
861 for(k=0; k<pUsing->nId; k++){
862 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
863 pItem++;
864 i++;
865 break;
866 }
867 }
868 }
danielk1977b3bce662005-01-29 08:32:43 +0000869 break;
870 }
drh8141f612004-01-25 22:44:58 +0000871 }
872 }
873 }
drh626a8792005-01-17 22:08:19 +0000874
875#ifndef SQLITE_OMIT_TRIGGER
876 /* If we have not already resolved the name, then maybe
877 ** it is a new.* or old.* trigger argument reference
878 */
879 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
880 TriggerStack *pTriggerStack = pParse->trigStack;
881 Table *pTab = 0;
882 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
883 pExpr->iTable = pTriggerStack->newIdx;
884 assert( pTriggerStack->pTab );
885 pTab = pTriggerStack->pTab;
886 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
887 pExpr->iTable = pTriggerStack->oldIdx;
888 assert( pTriggerStack->pTab );
889 pTab = pTriggerStack->pTab;
890 }
891
892 if( pTab ){
893 int j;
894 Column *pCol = pTab->aCol;
895
896 pExpr->iDb = pTab->iDb;
897 cntTab++;
898 for(j=0; j < pTab->nCol; j++, pCol++) {
899 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
900 cnt++;
901 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
902 pExpr->affinity = pTab->aCol[j].affinity;
903 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000904 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000905 break;
906 }
907 }
908 }
909 }
drhb7f91642004-10-31 02:22:47 +0000910#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000911
drh626a8792005-01-17 22:08:19 +0000912 /*
913 ** Perhaps the name is a reference to the ROWID
914 */
915 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
916 cnt = 1;
917 pExpr->iColumn = -1;
918 pExpr->affinity = SQLITE_AFF_INTEGER;
919 }
drh8141f612004-01-25 22:44:58 +0000920
drh626a8792005-01-17 22:08:19 +0000921 /*
922 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
923 ** might refer to an result-set alias. This happens, for example, when
924 ** we are resolving names in the WHERE clause of the following command:
925 **
926 ** SELECT a+b AS x FROM table WHERE x<10;
927 **
928 ** In cases like this, replace pExpr with a copy of the expression that
929 ** forms the result set entry ("a+b" in the example) and return immediately.
930 ** Note that the expression in the result set should have already been
931 ** resolved by the time the WHERE clause is resolved.
932 */
drh79d5f632005-01-18 17:20:10 +0000933 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000934 for(j=0; j<pEList->nExpr; j++){
935 char *zAs = pEList->a[j].zName;
936 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
937 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
938 pExpr->op = TK_AS;
939 pExpr->iColumn = j;
940 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000941 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000942 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000943 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000944 }
945 }
946 }
947
948 /* Advance to the next name context. The loop will exit when either
949 ** we have a match (cnt>0) or when we run out of name contexts.
950 */
951 if( cnt==0 ){
952 pNC = pNC->pNext;
953 }
drh8141f612004-01-25 22:44:58 +0000954 }
955
956 /*
957 ** If X and Y are NULL (in other words if only the column name Z is
958 ** supplied) and the value of Z is enclosed in double-quotes, then
959 ** Z is a string literal if it doesn't match any column names. In that
960 ** case, we need to return right away and not make any changes to
961 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000962 **
963 ** Because no reference was made to outer contexts, the pNC->nRef
964 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000965 */
966 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
967 sqliteFree(zCol);
968 return 0;
969 }
970
971 /*
972 ** cnt==0 means there was not match. cnt>1 means there were two or
973 ** more matches. Either way, we have an error.
974 */
975 if( cnt!=1 ){
976 char *z = 0;
977 char *zErr;
978 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
979 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000980 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000981 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000982 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000983 }else{
984 z = sqliteStrDup(zCol);
985 }
danielk19774adee202004-05-08 08:23:19 +0000986 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000987 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000988 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000989 }
990
drh51669862004-12-18 18:40:26 +0000991 /* If a column from a table in pSrcList is referenced, then record
992 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
993 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
994 ** column number is greater than the number of bits in the bitmask
995 ** then set the high-order bit of the bitmask.
996 */
997 if( pExpr->iColumn>=0 && pMatch!=0 ){
998 int n = pExpr->iColumn;
999 if( n>=sizeof(Bitmask)*8 ){
1000 n = sizeof(Bitmask)*8-1;
1001 }
1002 assert( pMatch->iCursor==pExpr->iTable );
1003 pMatch->colUsed |= 1<<n;
1004 }
1005
danielk1977d5d56522005-03-16 12:15:20 +00001006lookupname_end:
drh8141f612004-01-25 22:44:58 +00001007 /* Clean up and return
1008 */
1009 sqliteFree(zDb);
1010 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001011 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001012 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001013 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001014 pExpr->pRight = 0;
1015 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001016lookupname_end_2:
1017 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001018 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001019 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001020 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001021 if( pMatch && !pMatch->pSelect ){
1022 pExpr->pTab = pMatch->pTab;
1023 }
drh15ccce12005-05-23 15:06:39 +00001024 /* Increment the nRef value on all name contexts from TopNC up to
1025 ** the point where the name matched. */
1026 for(;;){
1027 assert( pTopNC!=0 );
1028 pTopNC->nRef++;
1029 if( pTopNC==pNC ) break;
1030 pTopNC = pTopNC->pNext;
1031 }
1032 return 0;
1033 } else {
1034 return 1;
drh626a8792005-01-17 22:08:19 +00001035 }
drh8141f612004-01-25 22:44:58 +00001036}
1037
1038/*
drh626a8792005-01-17 22:08:19 +00001039** This routine is designed as an xFunc for walkExprTree().
1040**
drh73b211a2005-01-18 04:00:42 +00001041** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001042** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001043** the tree or 2 to abort the tree walk.
1044**
1045** This routine also does error checking and name resolution for
1046** function names. The operator for aggregate functions is changed
1047** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001048*/
1049static int nameResolverStep(void *pArg, Expr *pExpr){
1050 NameContext *pNC = (NameContext*)pArg;
1051 SrcList *pSrcList;
1052 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001053
danielk1977b3bce662005-01-29 08:32:43 +00001054 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001055 assert( pNC!=0 );
1056 pSrcList = pNC->pSrcList;
1057 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001058
drh626a8792005-01-17 22:08:19 +00001059 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1060 ExprSetProperty(pExpr, EP_Resolved);
1061#ifndef NDEBUG
1062 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001063 int i;
drh626a8792005-01-17 22:08:19 +00001064 for(i=0; i<pSrcList->nSrc; i++){
1065 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1066 }
1067 }
1068#endif
1069 switch( pExpr->op ){
1070 /* Double-quoted strings (ex: "abc") are used as identifiers if
1071 ** possible. Otherwise they remain as strings. Single-quoted
1072 ** strings (ex: 'abc') are always string literals.
1073 */
1074 case TK_STRING: {
1075 if( pExpr->token.z[0]=='\'' ) break;
1076 /* Fall thru into the TK_ID case if this is a double-quoted string */
1077 }
1078 /* A lone identifier is the name of a column.
1079 */
1080 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001081 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1082 return 1;
1083 }
1084
1085 /* A table name and column name: ID.ID
1086 ** Or a database, table and column: ID.ID.ID
1087 */
1088 case TK_DOT: {
1089 Token *pColumn;
1090 Token *pTable;
1091 Token *pDb;
1092 Expr *pRight;
1093
danielk1977b3bce662005-01-29 08:32:43 +00001094 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001095 pRight = pExpr->pRight;
1096 if( pRight->op==TK_ID ){
1097 pDb = 0;
1098 pTable = &pExpr->pLeft->token;
1099 pColumn = &pRight->token;
1100 }else{
1101 assert( pRight->op==TK_DOT );
1102 pDb = &pExpr->pLeft->token;
1103 pTable = &pRight->pLeft->token;
1104 pColumn = &pRight->pRight->token;
1105 }
1106 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1107 return 1;
1108 }
1109
1110 /* Resolve function names
1111 */
drhb71090f2005-05-23 17:26:51 +00001112 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001113 case TK_FUNCTION: {
1114 ExprList *pList = pExpr->pList; /* The argument list */
1115 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1116 int no_such_func = 0; /* True if no such function exists */
1117 int wrong_num_args = 0; /* True if wrong number of arguments */
1118 int is_agg = 0; /* True if is an aggregate function */
1119 int i;
1120 int nId; /* Number of characters in function name */
1121 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001122 FuncDef *pDef; /* Information about the function */
1123 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001124
drhb71090f2005-05-23 17:26:51 +00001125 zId = pExpr->token.z;
1126 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001127 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1128 if( pDef==0 ){
1129 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1130 if( pDef==0 ){
1131 no_such_func = 1;
1132 }else{
1133 wrong_num_args = 1;
1134 }
1135 }else{
1136 is_agg = pDef->xFunc==0;
1137 }
1138 if( is_agg && !pNC->allowAgg ){
1139 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1140 pNC->nErr++;
1141 is_agg = 0;
1142 }else if( no_such_func ){
1143 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1144 pNC->nErr++;
1145 }else if( wrong_num_args ){
1146 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1147 nId, zId);
1148 pNC->nErr++;
1149 }
1150 if( is_agg ){
1151 pExpr->op = TK_AGG_FUNCTION;
1152 pNC->hasAgg = 1;
1153 }
drh73b211a2005-01-18 04:00:42 +00001154 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001155 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001156 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001157 }
drh73b211a2005-01-18 04:00:42 +00001158 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001159 /* FIX ME: Compute pExpr->affinity based on the expected return
1160 ** type of the function
1161 */
1162 return is_agg;
1163 }
danielk1977b3bce662005-01-29 08:32:43 +00001164#ifndef SQLITE_OMIT_SUBQUERY
1165 case TK_SELECT:
1166 case TK_EXISTS:
1167#endif
1168 case TK_IN: {
1169 if( pExpr->pSelect ){
1170 int nRef = pNC->nRef;
1171 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1172 assert( pNC->nRef>=nRef );
1173 if( nRef!=pNC->nRef ){
1174 ExprSetProperty(pExpr, EP_VarSelect);
1175 }
1176 }
1177 }
drh626a8792005-01-17 22:08:19 +00001178 }
1179 return 0;
1180}
1181
1182/*
drhcce7d172000-05-31 15:34:51 +00001183** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001184** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001185** index to the table in the table list and a column offset. The
1186** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1187** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001188** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001189** VDBE cursor number for a cursor that is pointing into the referenced
1190** table. The Expr.iColumn value is changed to the index of the column
1191** of the referenced table. The Expr.iColumn value for the special
1192** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1193** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001194**
drh626a8792005-01-17 22:08:19 +00001195** Also resolve function names and check the functions for proper
1196** usage. Make sure all function names are recognized and all functions
1197** have the correct number of arguments. Leave an error message
1198** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1199**
drh73b211a2005-01-18 04:00:42 +00001200** If the expression contains aggregate functions then set the EP_Agg
1201** property on the expression.
drh626a8792005-01-17 22:08:19 +00001202*/
1203int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001204 NameContext *pNC, /* Namespace to resolve expressions in. */
1205 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001206){
drh73b211a2005-01-18 04:00:42 +00001207 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001208 walkExprTree(pExpr, nameResolverStep, pNC);
1209 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001210 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001211 }
1212 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001213}
1214
drh1398ad32005-01-19 23:24:50 +00001215/*
1216** A pointer instance of this structure is used to pass information
1217** through walkExprTree into codeSubqueryStep().
1218*/
1219typedef struct QueryCoder QueryCoder;
1220struct QueryCoder {
1221 Parse *pParse; /* The parsing context */
1222 NameContext *pNC; /* Namespace of first enclosing query */
1223};
1224
drh626a8792005-01-17 22:08:19 +00001225
1226/*
1227** Generate code for subqueries and IN operators.
1228**
drh73b211a2005-01-18 04:00:42 +00001229** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001230**
1231** expr IN (exprlist)
1232** and
1233** expr IN (SELECT ...)
1234**
1235** The first form is handled by creating a set holding the list
1236** of allowed values. The second form causes the SELECT to generate
1237** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001238*/
drh51522cd2005-01-20 13:36:19 +00001239#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001240void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1241 int label = 0; /* Address after sub-select code */
1242 Vdbe *v = sqlite3GetVdbe(pParse);
1243 if( v==0 ) return;
1244
1245 /* If this is not a variable (correlated) select, then execute
1246 ** it only once. Unless this is part of a trigger program. In
1247 ** that case re-execute every time (this could be optimized).
1248 */
1249 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1250 int mem = pParse->nMem++;
1251 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1252 label = sqlite3VdbeMakeLabel(v);
1253 sqlite3VdbeAddOp(v, OP_If, 0, label);
1254 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1255 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1256 }
1257
1258 if( pExpr->pSelect ){
1259 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1260 }
drh6a3ea0e2003-05-02 14:32:12 +00001261
drhcce7d172000-05-31 15:34:51 +00001262 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001263 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001264 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001265 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001266 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001267
danielk1977bf3b7212004-05-18 10:06:24 +00001268 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001269
1270 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1271 ** expression it is handled the same way. A temporary table is
1272 ** filled with single-field index keys representing the results
1273 ** from the SELECT or the <exprlist>.
1274 **
1275 ** If the 'x' expression is a column value, or the SELECT...
1276 ** statement returns a column value, then the affinity of that
1277 ** column is used to build the index keys. If both 'x' and the
1278 ** SELECT... statement are columns, then numeric affinity is used
1279 ** if either column has NUMERIC or INTEGER affinity. If neither
1280 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1281 ** is used.
1282 */
1283 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001284 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001285 memset(&keyInfo, 0, sizeof(keyInfo));
1286 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001287 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001288
drhfef52082000-06-06 01:50:43 +00001289 if( pExpr->pSelect ){
1290 /* Case 1: expr IN (SELECT ...)
1291 **
danielk1977e014a832004-05-17 10:48:57 +00001292 ** Generate code to write the results of the select into the temporary
1293 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001294 */
danielk1977e014a832004-05-17 10:48:57 +00001295 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001296 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001297 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001298 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001299 pEList = pExpr->pSelect->pEList;
1300 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001301 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001302 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001303 }
drhfef52082000-06-06 01:50:43 +00001304 }else if( pExpr->pList ){
1305 /* Case 2: expr IN (exprlist)
1306 **
danielk1977e014a832004-05-17 10:48:57 +00001307 ** For each expression, build an index key from the evaluation and
1308 ** store it in the temporary table. If <expr> is a column, then use
1309 ** that columns affinity when building index keys. If <expr> is not
1310 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001311 */
danielk1977e014a832004-05-17 10:48:57 +00001312 int i;
danielk1977e014a832004-05-17 10:48:57 +00001313 if( !affinity ){
1314 affinity = SQLITE_AFF_NUMERIC;
1315 }
danielk19770202b292004-06-09 09:55:16 +00001316 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001317
1318 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001319 for(i=0; i<pExpr->pList->nExpr; i++){
1320 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001321
1322 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001323 if( !sqlite3ExprIsConstant(pE2) ){
1324 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001325 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001326 return;
drh4794b982000-06-06 13:54:14 +00001327 }
danielk1977e014a832004-05-17 10:48:57 +00001328
1329 /* Evaluate the expression and insert it into the temp table */
1330 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001331 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001332 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001333 }
1334 }
danielk19770202b292004-06-09 09:55:16 +00001335 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001336 break;
drhfef52082000-06-06 01:50:43 +00001337 }
1338
drh51522cd2005-01-20 13:36:19 +00001339 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001340 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001341 /* This has to be a scalar SELECT. Generate code to put the
1342 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001343 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001344 */
drh51522cd2005-01-20 13:36:19 +00001345 int sop;
1346 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001347
drh967e8b72000-06-21 13:59:10 +00001348 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001349 pSel = pExpr->pSelect;
1350 if( pExpr->op==TK_SELECT ){
1351 sop = SRT_Mem;
1352 }else{
1353 static const Token one = { "1", 0, 1 };
1354 sop = SRT_Exists;
1355 sqlite3ExprListDelete(pSel->pEList);
1356 pSel->pEList = sqlite3ExprListAppend(0,
1357 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1358 }
danielk1977b3bce662005-01-29 08:32:43 +00001359 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1360 break;
drhcce7d172000-05-31 15:34:51 +00001361 }
1362 }
danielk1977b3bce662005-01-29 08:32:43 +00001363
1364 if( pExpr->pSelect ){
1365 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1366 }
1367 if( label<0 ){
1368 sqlite3VdbeResolveLabel(v, label);
1369 }
1370 return;
drhcce7d172000-05-31 15:34:51 +00001371}
drh51522cd2005-01-20 13:36:19 +00001372#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001373
drhcce7d172000-05-31 15:34:51 +00001374/*
drhfec19aa2004-05-19 20:41:03 +00001375** Generate an instruction that will put the integer describe by
1376** text z[0..n-1] on the stack.
1377*/
1378static void codeInteger(Vdbe *v, const char *z, int n){
1379 int i;
drh6fec0762004-05-30 01:38:43 +00001380 if( sqlite3GetInt32(z, &i) ){
1381 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1382 }else if( sqlite3FitsIn64Bits(z) ){
1383 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001384 }else{
1385 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1386 }
1387}
1388
1389/*
drhcce7d172000-05-31 15:34:51 +00001390** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001391** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001392**
1393** This code depends on the fact that certain token values (ex: TK_EQ)
1394** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1395** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1396** the make process cause these values to align. Assert()s in the code
1397** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001398*/
danielk19774adee202004-05-08 08:23:19 +00001399void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001400 Vdbe *v = pParse->pVdbe;
1401 int op;
danielk19777977a172004-11-09 12:44:37 +00001402 if( v==0 ) return;
1403 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001404 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001405 return;
1406 }
drhf2bc0132004-10-04 13:19:23 +00001407 op = pExpr->op;
1408 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001409 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001410 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1411 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001412 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001413 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001414 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001415 }else{
drhf0863fe2005-06-12 21:35:51 +00001416 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001417 }
drhcce7d172000-05-31 15:34:51 +00001418 break;
1419 }
1420 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001421 codeInteger(v, pExpr->token.z, pExpr->token.n);
1422 break;
1423 }
1424 case TK_FLOAT:
1425 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001426 assert( TK_FLOAT==OP_Real );
1427 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001428 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001429 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001430 break;
1431 }
drhf0863fe2005-06-12 21:35:51 +00001432 case TK_NULL: {
1433 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1434 break;
1435 }
danielk19775338a5f2005-01-20 13:03:10 +00001436#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001437 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001438 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001439 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1440 sqlite3VdbeDequoteP3(v, -1);
1441 break;
1442 }
danielk19775338a5f2005-01-20 13:03:10 +00001443#endif
drh50457892003-09-06 01:10:47 +00001444 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001445 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001446 if( pExpr->token.n>1 ){
1447 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1448 }
drh50457892003-09-06 01:10:47 +00001449 break;
1450 }
drh4e0cff62004-11-05 05:10:28 +00001451 case TK_REGISTER: {
1452 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1453 break;
1454 }
drh487e2622005-06-25 18:42:14 +00001455#ifndef SQLITE_OMIT_CAST
1456 case TK_CAST: {
1457 /* Expressions of the form: CAST(pLeft AS token) */
1458 int aff, op;
1459 sqlite3ExprCode(pParse, pExpr->pLeft);
1460 aff = sqlite3AffinityType(&pExpr->token);
1461 switch( aff ){
1462 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1463 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1464 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1465 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1466 }
1467 sqlite3VdbeAddOp(v, op, 0, 0);
1468 break;
1469 }
1470#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001471 case TK_LT:
1472 case TK_LE:
1473 case TK_GT:
1474 case TK_GE:
1475 case TK_NE:
1476 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001477 assert( TK_LT==OP_Lt );
1478 assert( TK_LE==OP_Le );
1479 assert( TK_GT==OP_Gt );
1480 assert( TK_GE==OP_Ge );
1481 assert( TK_EQ==OP_Eq );
1482 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001483 sqlite3ExprCode(pParse, pExpr->pLeft);
1484 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001485 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001486 break;
drhc9b84a12002-06-20 11:36:48 +00001487 }
drhcce7d172000-05-31 15:34:51 +00001488 case TK_AND:
1489 case TK_OR:
1490 case TK_PLUS:
1491 case TK_STAR:
1492 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001493 case TK_REM:
1494 case TK_BITAND:
1495 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001496 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001497 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001498 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001499 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001500 assert( TK_AND==OP_And );
1501 assert( TK_OR==OP_Or );
1502 assert( TK_PLUS==OP_Add );
1503 assert( TK_MINUS==OP_Subtract );
1504 assert( TK_REM==OP_Remainder );
1505 assert( TK_BITAND==OP_BitAnd );
1506 assert( TK_BITOR==OP_BitOr );
1507 assert( TK_SLASH==OP_Divide );
1508 assert( TK_LSHIFT==OP_ShiftLeft );
1509 assert( TK_RSHIFT==OP_ShiftRight );
1510 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001511 sqlite3ExprCode(pParse, pExpr->pLeft);
1512 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001513 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001514 break;
1515 }
drhcce7d172000-05-31 15:34:51 +00001516 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001517 Expr *pLeft = pExpr->pLeft;
1518 assert( pLeft );
1519 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1520 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001521 char *z = sqliteMalloc( p->n + 2 );
1522 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001523 if( pLeft->op==TK_FLOAT ){
1524 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001525 }else{
drhfec19aa2004-05-19 20:41:03 +00001526 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001527 }
drh6e142f52000-06-08 13:36:40 +00001528 sqliteFree(z);
1529 break;
1530 }
drh1ccde152000-06-17 13:12:39 +00001531 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001532 }
drhbf4133c2001-10-13 02:59:08 +00001533 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001534 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001535 assert( TK_BITNOT==OP_BitNot );
1536 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001537 sqlite3ExprCode(pParse, pExpr->pLeft);
1538 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001539 break;
1540 }
1541 case TK_ISNULL:
1542 case TK_NOTNULL: {
1543 int dest;
drhf2bc0132004-10-04 13:19:23 +00001544 assert( TK_ISNULL==OP_IsNull );
1545 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001546 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1547 sqlite3ExprCode(pParse, pExpr->pLeft);
1548 dest = sqlite3VdbeCurrentAddr(v) + 2;
1549 sqlite3VdbeAddOp(v, op, 1, dest);
1550 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001551 break;
drhcce7d172000-05-31 15:34:51 +00001552 }
drh22827922000-06-06 17:27:05 +00001553 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001554 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001555 break;
1556 }
drhb71090f2005-05-23 17:26:51 +00001557 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001558 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001559 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001560 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001561 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001562 int nId;
1563 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001564 int p2 = 0;
1565 int i;
danielk1977d8123362004-06-12 09:25:12 +00001566 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001567 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001568 zId = pExpr->token.z;
1569 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001570 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001571 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001572 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001573 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001574 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1575 p2 |= (1<<i);
1576 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001577 if( pDef->needCollSeq && !pColl ){
1578 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1579 }
1580 }
1581 if( pDef->needCollSeq ){
1582 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001583 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001584 }
1585 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001586 break;
1587 }
drhfe2093d2005-01-20 22:48:47 +00001588#ifndef SQLITE_OMIT_SUBQUERY
1589 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001590 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001591 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001593 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001594 break;
1595 }
drhfef52082000-06-06 01:50:43 +00001596 case TK_IN: {
1597 int addr;
drh94a11212004-09-25 13:12:14 +00001598 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001599 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001600
1601 /* Figure out the affinity to use to create a key from the results
1602 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001603 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001604 */
drh94a11212004-09-25 13:12:14 +00001605 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001606
danielk19774adee202004-05-08 08:23:19 +00001607 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001608
1609 /* Code the <expr> from "<expr> IN (...)". The temporary table
1610 ** pExpr->iTable contains the values that make up the (...) set.
1611 */
danielk19774adee202004-05-08 08:23:19 +00001612 sqlite3ExprCode(pParse, pExpr->pLeft);
1613 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001614 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001615 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001616 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001617 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001618 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001619 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1620 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1621
drhfef52082000-06-06 01:50:43 +00001622 break;
1623 }
danielk197793758c82005-01-21 08:13:14 +00001624#endif
drhfef52082000-06-06 01:50:43 +00001625 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001626 Expr *pLeft = pExpr->pLeft;
1627 struct ExprList_item *pLItem = pExpr->pList->a;
1628 Expr *pRight = pLItem->pExpr;
1629 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001630 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001631 sqlite3ExprCode(pParse, pRight);
1632 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001633 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001634 pLItem++;
1635 pRight = pLItem->pExpr;
1636 sqlite3ExprCode(pParse, pRight);
1637 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001638 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001639 break;
1640 }
drh51e9a442004-01-16 16:42:53 +00001641 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001642 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001643 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001644 break;
1645 }
drh17a7f8d2002-03-24 13:13:27 +00001646 case TK_CASE: {
1647 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001648 int jumpInst;
1649 int addr;
1650 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001651 int i;
drhbe5c89a2004-07-26 00:31:09 +00001652 ExprList *pEList;
1653 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001654
1655 assert(pExpr->pList);
1656 assert((pExpr->pList->nExpr % 2) == 0);
1657 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001658 pEList = pExpr->pList;
1659 aListelem = pEList->a;
1660 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001661 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001662 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001663 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001664 }
drhf5905aa2002-05-26 20:54:33 +00001665 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001666 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001667 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001668 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001669 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1670 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001671 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001672 }else{
danielk19774adee202004-05-08 08:23:19 +00001673 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001674 }
drhbe5c89a2004-07-26 00:31:09 +00001675 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001676 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1677 addr = sqlite3VdbeCurrentAddr(v);
1678 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001679 }
drhf570f012002-05-31 15:51:25 +00001680 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001681 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001682 }
drh17a7f8d2002-03-24 13:13:27 +00001683 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001684 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001685 }else{
drhf0863fe2005-06-12 21:35:51 +00001686 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001687 }
danielk19774adee202004-05-08 08:23:19 +00001688 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001689 break;
1690 }
danielk19775338a5f2005-01-20 13:03:10 +00001691#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001692 case TK_RAISE: {
1693 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001694 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001695 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001696 return;
1697 }
drhad6d9462004-09-19 02:15:24 +00001698 if( pExpr->iColumn!=OE_Ignore ){
1699 assert( pExpr->iColumn==OE_Rollback ||
1700 pExpr->iColumn == OE_Abort ||
1701 pExpr->iColumn == OE_Fail );
1702 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1703 pExpr->token.z, pExpr->token.n);
1704 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001705 } else {
drhad6d9462004-09-19 02:15:24 +00001706 assert( pExpr->iColumn == OE_Ignore );
1707 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1708 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1709 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001710 }
drh17a7f8d2002-03-24 13:13:27 +00001711 }
danielk19775338a5f2005-01-20 13:03:10 +00001712#endif
drh17a7f8d2002-03-24 13:13:27 +00001713 break;
drhcce7d172000-05-31 15:34:51 +00001714 }
drhcce7d172000-05-31 15:34:51 +00001715}
1716
danielk197793758c82005-01-21 08:13:14 +00001717#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001718/*
drh25303782004-12-07 15:41:48 +00001719** Generate code that evalutes the given expression and leaves the result
1720** on the stack. See also sqlite3ExprCode().
1721**
1722** This routine might also cache the result and modify the pExpr tree
1723** so that it will make use of the cached result on subsequent evaluations
1724** rather than evaluate the whole expression again. Trivial expressions are
1725** not cached. If the expression is cached, its result is stored in a
1726** memory location.
1727*/
1728void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1729 Vdbe *v = pParse->pVdbe;
1730 int iMem;
1731 int addr1, addr2;
1732 if( v==0 ) return;
1733 addr1 = sqlite3VdbeCurrentAddr(v);
1734 sqlite3ExprCode(pParse, pExpr);
1735 addr2 = sqlite3VdbeCurrentAddr(v);
1736 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1737 iMem = pExpr->iTable = pParse->nMem++;
1738 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1739 pExpr->op = TK_REGISTER;
1740 }
1741}
danielk197793758c82005-01-21 08:13:14 +00001742#endif
drh25303782004-12-07 15:41:48 +00001743
1744/*
drh268380c2004-02-25 13:47:31 +00001745** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001746** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001747**
1748** Return the number of elements pushed onto the stack.
1749*/
danielk19774adee202004-05-08 08:23:19 +00001750int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001751 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001752 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001753){
1754 struct ExprList_item *pItem;
1755 int i, n;
1756 Vdbe *v;
1757 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001758 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001759 n = pList->nExpr;
1760 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001761 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001762 }
drhf9b596e2004-05-26 16:54:42 +00001763 return n;
drh268380c2004-02-25 13:47:31 +00001764}
1765
1766/*
drhcce7d172000-05-31 15:34:51 +00001767** Generate code for a boolean expression such that a jump is made
1768** to the label "dest" if the expression is true but execution
1769** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001770**
1771** If the expression evaluates to NULL (neither true nor false), then
1772** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001773**
1774** This code depends on the fact that certain token values (ex: TK_EQ)
1775** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1776** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1777** the make process cause these values to align. Assert()s in the code
1778** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001779*/
danielk19774adee202004-05-08 08:23:19 +00001780void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001781 Vdbe *v = pParse->pVdbe;
1782 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001783 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001784 op = pExpr->op;
1785 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001786 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001787 int d2 = sqlite3VdbeMakeLabel(v);
1788 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1789 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1790 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001791 break;
1792 }
1793 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001794 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1795 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001796 break;
1797 }
1798 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001799 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001800 break;
1801 }
1802 case TK_LT:
1803 case TK_LE:
1804 case TK_GT:
1805 case TK_GE:
1806 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001807 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001808 assert( TK_LT==OP_Lt );
1809 assert( TK_LE==OP_Le );
1810 assert( TK_GT==OP_Gt );
1811 assert( TK_GE==OP_Ge );
1812 assert( TK_EQ==OP_Eq );
1813 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001814 sqlite3ExprCode(pParse, pExpr->pLeft);
1815 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001816 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001817 break;
1818 }
1819 case TK_ISNULL:
1820 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001821 assert( TK_ISNULL==OP_IsNull );
1822 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001823 sqlite3ExprCode(pParse, pExpr->pLeft);
1824 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001825 break;
1826 }
drhfef52082000-06-06 01:50:43 +00001827 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001828 /* The expression "x BETWEEN y AND z" is implemented as:
1829 **
1830 ** 1 IF (x < y) GOTO 3
1831 ** 2 IF (x <= z) GOTO <dest>
1832 ** 3 ...
1833 */
drhf5905aa2002-05-26 20:54:33 +00001834 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001835 Expr *pLeft = pExpr->pLeft;
1836 Expr *pRight = pExpr->pList->a[0].pExpr;
1837 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001838 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001839 sqlite3ExprCode(pParse, pRight);
1840 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001841
drhbe5c89a2004-07-26 00:31:09 +00001842 pRight = pExpr->pList->a[1].pExpr;
1843 sqlite3ExprCode(pParse, pRight);
1844 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001845
danielk19774adee202004-05-08 08:23:19 +00001846 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1847 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1848 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001849 break;
1850 }
drhcce7d172000-05-31 15:34:51 +00001851 default: {
danielk19774adee202004-05-08 08:23:19 +00001852 sqlite3ExprCode(pParse, pExpr);
1853 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001854 break;
1855 }
1856 }
1857}
1858
1859/*
drh66b89c82000-11-28 20:47:17 +00001860** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001861** to the label "dest" if the expression is false but execution
1862** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001863**
1864** If the expression evaluates to NULL (neither true nor false) then
1865** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001866*/
danielk19774adee202004-05-08 08:23:19 +00001867void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001868 Vdbe *v = pParse->pVdbe;
1869 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001870 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001871
1872 /* The value of pExpr->op and op are related as follows:
1873 **
1874 ** pExpr->op op
1875 ** --------- ----------
1876 ** TK_ISNULL OP_NotNull
1877 ** TK_NOTNULL OP_IsNull
1878 ** TK_NE OP_Eq
1879 ** TK_EQ OP_Ne
1880 ** TK_GT OP_Le
1881 ** TK_LE OP_Gt
1882 ** TK_GE OP_Lt
1883 ** TK_LT OP_Ge
1884 **
1885 ** For other values of pExpr->op, op is undefined and unused.
1886 ** The value of TK_ and OP_ constants are arranged such that we
1887 ** can compute the mapping above using the following expression.
1888 ** Assert()s verify that the computation is correct.
1889 */
1890 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1891
1892 /* Verify correct alignment of TK_ and OP_ constants
1893 */
1894 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1895 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1896 assert( pExpr->op!=TK_NE || op==OP_Eq );
1897 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1898 assert( pExpr->op!=TK_LT || op==OP_Ge );
1899 assert( pExpr->op!=TK_LE || op==OP_Gt );
1900 assert( pExpr->op!=TK_GT || op==OP_Le );
1901 assert( pExpr->op!=TK_GE || op==OP_Lt );
1902
drhcce7d172000-05-31 15:34:51 +00001903 switch( pExpr->op ){
1904 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001905 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1906 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001907 break;
1908 }
1909 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001910 int d2 = sqlite3VdbeMakeLabel(v);
1911 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1912 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1913 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001914 break;
1915 }
1916 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001917 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001918 break;
1919 }
1920 case TK_LT:
1921 case TK_LE:
1922 case TK_GT:
1923 case TK_GE:
1924 case TK_NE:
1925 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001926 sqlite3ExprCode(pParse, pExpr->pLeft);
1927 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001928 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001929 break;
1930 }
drhcce7d172000-05-31 15:34:51 +00001931 case TK_ISNULL:
1932 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001933 sqlite3ExprCode(pParse, pExpr->pLeft);
1934 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001935 break;
1936 }
drhfef52082000-06-06 01:50:43 +00001937 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001938 /* The expression is "x BETWEEN y AND z". It is implemented as:
1939 **
1940 ** 1 IF (x >= y) GOTO 3
1941 ** 2 GOTO <dest>
1942 ** 3 IF (x > z) GOTO <dest>
1943 */
drhfef52082000-06-06 01:50:43 +00001944 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001945 Expr *pLeft = pExpr->pLeft;
1946 Expr *pRight = pExpr->pList->a[0].pExpr;
1947 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001948 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001949 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001950 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001951 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1952
danielk19774adee202004-05-08 08:23:19 +00001953 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1954 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001955 pRight = pExpr->pList->a[1].pExpr;
1956 sqlite3ExprCode(pParse, pRight);
1957 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001958 break;
1959 }
drhcce7d172000-05-31 15:34:51 +00001960 default: {
danielk19774adee202004-05-08 08:23:19 +00001961 sqlite3ExprCode(pParse, pExpr);
1962 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001963 break;
1964 }
1965 }
1966}
drh22827922000-06-06 17:27:05 +00001967
1968/*
1969** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1970** if they are identical and return FALSE if they differ in any way.
1971*/
danielk19774adee202004-05-08 08:23:19 +00001972int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001973 int i;
1974 if( pA==0 ){
1975 return pB==0;
1976 }else if( pB==0 ){
1977 return 0;
1978 }
1979 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001980 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1981 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001982 if( pA->pList ){
1983 if( pB->pList==0 ) return 0;
1984 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1985 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001986 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001987 return 0;
1988 }
1989 }
1990 }else if( pB->pList ){
1991 return 0;
1992 }
1993 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001994 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001995 if( pA->token.z ){
1996 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001997 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001998 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001999 }
2000 return 1;
2001}
2002
2003/*
2004** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00002005** The new element is initialized to zero. The calling function is
2006** expected to fill it in.
drh22827922000-06-06 17:27:05 +00002007*/
2008static int appendAggInfo(Parse *pParse){
2009 if( (pParse->nAgg & 0x7)==0 ){
2010 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00002011 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
2012 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00002013 return -1;
2014 }
drh6d4abfb2001-10-22 02:58:08 +00002015 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00002016 }
2017 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
2018 return pParse->nAgg++;
2019}
2020
2021/*
drh626a8792005-01-17 22:08:19 +00002022** This is an xFunc for walkExprTree() used to implement
2023** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2024** for additional information.
drh22827922000-06-06 17:27:05 +00002025**
drh626a8792005-01-17 22:08:19 +00002026** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002027*/
drh626a8792005-01-17 22:08:19 +00002028static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002029 int i;
2030 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00002031 NameContext *pNC = (NameContext *)pArg;
2032 Parse *pParse = pNC->pParse;
2033 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00002034
drh22827922000-06-06 17:27:05 +00002035 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002036 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002037 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2038 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2039 aAgg = pParse->aAgg;
2040 for(i=0; i<pParse->nAgg; i++){
2041 if( aAgg[i].isAgg ) continue;
2042 if( aAgg[i].pExpr->iTable==pExpr->iTable
2043 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2044 break;
2045 }
2046 }
2047 if( i>=pParse->nAgg ){
2048 i = appendAggInfo(pParse);
2049 if( i<0 ) return 1;
2050 pParse->aAgg[i].isAgg = 0;
2051 pParse->aAgg[i].pExpr = pExpr;
2052 }
2053 pExpr->iAgg = i;
2054 pExpr->iAggCtx = pNC->nDepth;
2055 return 1;
drh22827922000-06-06 17:27:05 +00002056 }
2057 }
drh626a8792005-01-17 22:08:19 +00002058 return 1;
drh22827922000-06-06 17:27:05 +00002059 }
2060 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002061 if( pNC->nDepth==0 ){
2062 aAgg = pParse->aAgg;
2063 for(i=0; i<pParse->nAgg; i++){
2064 if( !aAgg[i].isAgg ) continue;
2065 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2066 break;
2067 }
drh22827922000-06-06 17:27:05 +00002068 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002069 if( i>=pParse->nAgg ){
2070 u8 enc = pParse->db->enc;
2071 i = appendAggInfo(pParse);
2072 if( i<0 ) return 1;
2073 pParse->aAgg[i].isAgg = 1;
2074 pParse->aAgg[i].pExpr = pExpr;
2075 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2076 pExpr->token.z, pExpr->token.n,
2077 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2078 }
2079 pExpr->iAgg = i;
2080 return 1;
drh22827922000-06-06 17:27:05 +00002081 }
drh22827922000-06-06 17:27:05 +00002082 }
2083 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002084 if( pExpr->pSelect ){
2085 pNC->nDepth++;
2086 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2087 pNC->nDepth--;
2088 }
drh626a8792005-01-17 22:08:19 +00002089 return 0;
2090}
2091
2092/*
2093** Analyze the given expression looking for aggregate functions and
2094** for variables that need to be added to the pParse->aAgg[] array.
2095** Make additional entries to the pParse->aAgg[] array as necessary.
2096**
2097** This routine should only be called after the expression has been
2098** analyzed by sqlite3ExprResolveNames().
2099**
2100** If errors are seen, leave an error message in zErrMsg and return
2101** the number of errors.
2102*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002103int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2104 int nErr = pNC->pParse->nErr;
2105 walkExprTree(pExpr, analyzeAggregate, pNC);
2106 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002107}