blob: 5f797029f46b6f1a78ce792942075f12d89e7f28 [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**
drhfe05af82005-07-21 03:14:59 +000015** $Id: expr.c,v 1.212 2005/07/21 03:15:00 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. */
drhfe05af82005-07-21 03:14:59 +000096 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +000097 return (aff1 + aff2);
98 }
99}
100
drh53db1452004-05-20 13:54:53 +0000101/*
102** pExpr is a comparison operator. Return the type affinity that should
103** be applied to both operands prior to doing the comparison.
104*/
danielk1977e014a832004-05-17 10:48:57 +0000105static char comparisonAffinity(Expr *pExpr){
106 char aff;
107 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
108 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
109 pExpr->op==TK_NE );
110 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000111 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000112 if( pExpr->pRight ){
113 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
114 }
115 else if( pExpr->pSelect ){
116 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
117 }
118 else if( !aff ){
119 aff = SQLITE_AFF_NUMERIC;
120 }
121 return aff;
122}
123
124/*
125** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
126** idx_affinity is the affinity of an indexed column. Return true
127** if the index with affinity idx_affinity may be used to implement
128** the comparison in pExpr.
129*/
130int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
131 char aff = comparisonAffinity(pExpr);
132 return
133 (aff==SQLITE_AFF_NONE) ||
134 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
135 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
136 (aff==idx_affinity);
137}
138
danielk1977a37cdde2004-05-16 11:15:36 +0000139/*
140** Return the P1 value that should be used for a binary comparison
141** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
142** If jumpIfNull is true, then set the low byte of the returned
143** P1 value to tell the opcode to jump if either expression
144** evaluates to NULL.
145*/
danielk1977e014a832004-05-17 10:48:57 +0000146static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000147 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000148 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000149}
150
drha2e00042002-01-22 03:13:42 +0000151/*
danielk19770202b292004-06-09 09:55:16 +0000152** Return a pointer to the collation sequence that should be used by
153** a binary comparison operator comparing pLeft and pRight.
154**
155** If the left hand expression has a collating sequence type, then it is
156** used. Otherwise the collation sequence for the right hand expression
157** is used, or the default (BINARY) if neither expression has a collating
158** type.
159*/
danielk19777cedc8d2004-06-10 10:50:08 +0000160static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
161 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000162 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000163 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000164 }
165 return pColl;
166}
167
168/*
drhbe5c89a2004-07-26 00:31:09 +0000169** Generate code for a comparison operator.
170*/
171static int codeCompare(
172 Parse *pParse, /* The parsing (and code generating) context */
173 Expr *pLeft, /* The left operand */
174 Expr *pRight, /* The right operand */
175 int opcode, /* The comparison opcode */
176 int dest, /* Jump here if true. */
177 int jumpIfNull /* If true, jump if either operand is NULL */
178){
179 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
180 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000181 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000182}
183
184/*
drha76b5df2002-02-23 02:32:10 +0000185** Construct a new expression node and return a pointer to it. Memory
186** for this node is obtained from sqliteMalloc(). The calling function
187** is responsible for making sure the node eventually gets freed.
188*/
drhe4e72072004-11-23 01:47:30 +0000189Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000190 Expr *pNew;
191 pNew = sqliteMalloc( sizeof(Expr) );
192 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000193 /* When malloc fails, delete pLeft and pRight. Expressions passed to
194 ** this function must always be allocated with sqlite3Expr() for this
195 ** reason.
196 */
197 sqlite3ExprDelete(pLeft);
198 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000199 return 0;
200 }
201 pNew->op = op;
202 pNew->pLeft = pLeft;
203 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000204 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000205 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000206 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000207 pNew->span = pNew->token = *pToken;
208 }else if( pLeft && pRight ){
209 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000210 }
drha76b5df2002-02-23 02:32:10 +0000211 return pNew;
212}
213
214/*
drh4e0cff62004-11-05 05:10:28 +0000215** When doing a nested parse, you can include terms in an expression
216** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000217** on the stack. "#0" means the top of the stack.
218** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000219**
220** This routine is called by the parser to deal with on of those terms.
221** It immediately generates code to store the value in a memory location.
222** The returns an expression that will code to extract the value from
223** that memory location as needed.
224*/
225Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
226 Vdbe *v = pParse->pVdbe;
227 Expr *p;
228 int depth;
229 if( v==0 ) return 0;
230 if( pParse->nested==0 ){
231 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
232 return 0;
233 }
234 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000235 if( p==0 ){
236 return 0; /* Malloc failed */
237 }
drh4e0cff62004-11-05 05:10:28 +0000238 depth = atoi(&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000239 p->iTable = pParse->nMem++;
240 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
241 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000242 return p;
243}
244
245/*
drh91bb0ee2004-09-01 03:06:34 +0000246** Join two expressions using an AND operator. If either expression is
247** NULL, then just return the other expression.
248*/
249Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
250 if( pLeft==0 ){
251 return pRight;
252 }else if( pRight==0 ){
253 return pLeft;
254 }else{
255 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
256 }
257}
258
259/*
drh6977fea2002-10-22 23:38:04 +0000260** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000261** text between the two given tokens.
262*/
danielk19774adee202004-05-08 08:23:19 +0000263void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000264 assert( pRight!=0 );
265 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000266 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000267 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000268 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000269 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000270 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000271 }else{
drh6977fea2002-10-22 23:38:04 +0000272 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000273 }
drha76b5df2002-02-23 02:32:10 +0000274 }
275}
276
277/*
278** Construct a new expression node for a function with multiple
279** arguments.
280*/
danielk19774adee202004-05-08 08:23:19 +0000281Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000282 Expr *pNew;
283 pNew = sqliteMalloc( sizeof(Expr) );
284 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000285 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000286 return 0;
287 }
288 pNew->op = TK_FUNCTION;
289 pNew->pList = pList;
290 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000291 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000292 pNew->token = *pToken;
293 }else{
294 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000295 }
drh6977fea2002-10-22 23:38:04 +0000296 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000297 return pNew;
298}
299
300/*
drhfa6bc002004-09-07 16:19:52 +0000301** Assign a variable number to an expression that encodes a wildcard
302** in the original SQL statement.
303**
304** Wildcards consisting of a single "?" are assigned the next sequential
305** variable number.
306**
307** Wildcards of the form "?nnn" are assigned the number "nnn". We make
308** sure "nnn" is not too be to avoid a denial of service attack when
309** the SQL statement comes from an external source.
310**
311** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
312** as the previous instance of the same wildcard. Or if this is the first
313** instance of the wildcard, the next sequenial variable number is
314** assigned.
315*/
316void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
317 Token *pToken;
318 if( pExpr==0 ) return;
319 pToken = &pExpr->token;
320 assert( pToken->n>=1 );
321 assert( pToken->z!=0 );
322 assert( pToken->z[0]!=0 );
323 if( pToken->n==1 ){
324 /* Wildcard of the form "?". Assign the next variable number */
325 pExpr->iTable = ++pParse->nVar;
326 }else if( pToken->z[0]=='?' ){
327 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
328 ** use it as the variable number */
329 int i;
330 pExpr->iTable = i = atoi(&pToken->z[1]);
331 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
332 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
333 SQLITE_MAX_VARIABLE_NUMBER);
334 }
335 if( i>pParse->nVar ){
336 pParse->nVar = i;
337 }
338 }else{
339 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
340 ** number as the prior appearance of the same name, or if the name
341 ** has never appeared before, reuse the same variable number
342 */
343 int i, n;
344 n = pToken->n;
345 for(i=0; i<pParse->nVarExpr; i++){
346 Expr *pE;
347 if( (pE = pParse->apVarExpr[i])!=0
348 && pE->token.n==n
349 && memcmp(pE->token.z, pToken->z, n)==0 ){
350 pExpr->iTable = pE->iTable;
351 break;
352 }
353 }
354 if( i>=pParse->nVarExpr ){
355 pExpr->iTable = ++pParse->nVar;
356 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
357 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
358 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
359 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
360 }
361 if( !sqlite3_malloc_failed ){
362 assert( pParse->apVarExpr!=0 );
363 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
364 }
365 }
366 }
367}
368
369/*
drha2e00042002-01-22 03:13:42 +0000370** Recursively delete an expression tree.
371*/
danielk19774adee202004-05-08 08:23:19 +0000372void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000373 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000374 if( p->span.dyn ) sqliteFree((char*)p->span.z);
375 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000376 sqlite3ExprDelete(p->pLeft);
377 sqlite3ExprDelete(p->pRight);
378 sqlite3ExprListDelete(p->pList);
379 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000380 sqliteFree(p);
381}
382
drha76b5df2002-02-23 02:32:10 +0000383
384/*
drhff78bd22002-02-27 01:47:11 +0000385** The following group of routines make deep copies of expressions,
386** expression lists, ID lists, and select statements. The copies can
387** be deleted (by being passed to their respective ...Delete() routines)
388** without effecting the originals.
389**
danielk19774adee202004-05-08 08:23:19 +0000390** The expression list, ID, and source lists return by sqlite3ExprListDup(),
391** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000392** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000393**
drhad3cab52002-05-24 02:04:32 +0000394** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000395*/
danielk19774adee202004-05-08 08:23:19 +0000396Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000397 Expr *pNew;
398 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000399 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000400 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000401 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000402 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000403 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000404 pNew->token.dyn = 1;
405 }else{
drh4efc4752004-01-16 15:55:37 +0000406 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000407 }
drh6977fea2002-10-22 23:38:04 +0000408 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000409 pNew->pLeft = sqlite3ExprDup(p->pLeft);
410 pNew->pRight = sqlite3ExprDup(p->pRight);
411 pNew->pList = sqlite3ExprListDup(p->pList);
412 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000413 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000414 return pNew;
415}
danielk19774adee202004-05-08 08:23:19 +0000416void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000417 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000418 if( pFrom->z ){
419 pTo->n = pFrom->n;
420 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
421 pTo->dyn = 1;
422 }else{
drh4b59ab52002-08-24 18:24:51 +0000423 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000424 }
425}
danielk19774adee202004-05-08 08:23:19 +0000426ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000427 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000428 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000429 int i;
430 if( p==0 ) return 0;
431 pNew = sqliteMalloc( sizeof(*pNew) );
432 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000433 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000434 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000435 if( pItem==0 ){
436 sqliteFree(pNew);
437 return 0;
438 }
drh145716b2004-09-24 12:24:06 +0000439 pOldItem = p->a;
440 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000441 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000442 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000443 if( pOldExpr->span.z!=0 && pNewExpr ){
444 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000445 ** expression list. The logic in SELECT processing that determines
446 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000447 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000448 }
drh1f3e9052002-10-31 00:09:39 +0000449 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000450 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000451 pItem->zName = sqliteStrDup(pOldItem->zName);
452 pItem->sortOrder = pOldItem->sortOrder;
453 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000454 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000455 }
456 return pNew;
457}
danielk197793758c82005-01-21 08:13:14 +0000458
459/*
460** If cursors, triggers, views and subqueries are all omitted from
461** the build, then none of the following routines, except for
462** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
463** called with a NULL argument.
464*/
danielk19776a67fe82005-02-04 04:07:16 +0000465#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
466 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000467SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000468 SrcList *pNew;
469 int i;
drh113088e2003-03-20 01:16:58 +0000470 int nByte;
drhad3cab52002-05-24 02:04:32 +0000471 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000472 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000473 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000474 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000475 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000476 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000477 struct SrcList_item *pNewItem = &pNew->a[i];
478 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000479 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000480 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
481 pNewItem->zName = sqliteStrDup(pOldItem->zName);
482 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
483 pNewItem->jointype = pOldItem->jointype;
484 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000485 pTab = pNewItem->pTab = pOldItem->pTab;
486 if( pTab ){
487 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000488 }
danielk19774adee202004-05-08 08:23:19 +0000489 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
490 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
491 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhfe05af82005-07-21 03:14:59 +0000492 pNewItem->pWIdx = 0;
danielk19776c18b6e2005-01-30 09:17:58 +0000493 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000494 }
495 return pNew;
496}
danielk19774adee202004-05-08 08:23:19 +0000497IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000498 IdList *pNew;
499 int i;
500 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000501 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000502 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000503 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000504 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000505 if( pNew->a==0 ){
506 sqliteFree(pNew);
507 return 0;
508 }
drhff78bd22002-02-27 01:47:11 +0000509 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000510 struct IdList_item *pNewItem = &pNew->a[i];
511 struct IdList_item *pOldItem = &p->a[i];
512 pNewItem->zName = sqliteStrDup(pOldItem->zName);
513 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000514 }
515 return pNew;
516}
danielk19774adee202004-05-08 08:23:19 +0000517Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000518 Select *pNew;
519 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000520 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000521 if( pNew==0 ) return 0;
522 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000523 pNew->pEList = sqlite3ExprListDup(p->pEList);
524 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
525 pNew->pWhere = sqlite3ExprDup(p->pWhere);
526 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
527 pNew->pHaving = sqlite3ExprDup(p->pHaving);
528 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000529 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000530 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000531 pNew->pLimit = sqlite3ExprDup(p->pLimit);
532 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000533 pNew->iLimit = -1;
534 pNew->iOffset = -1;
drh9170dd72005-07-08 17:13:46 +0000535 pNew->ppOpenVirtual = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000536 pNew->isResolved = p->isResolved;
537 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000538 return pNew;
539}
danielk197793758c82005-01-21 08:13:14 +0000540#else
541Select *sqlite3SelectDup(Select *p){
542 assert( p==0 );
543 return 0;
544}
545#endif
drhff78bd22002-02-27 01:47:11 +0000546
547
548/*
drha76b5df2002-02-23 02:32:10 +0000549** Add a new element to the end of an expression list. If pList is
550** initially NULL, then create a new expression list.
551*/
danielk19774adee202004-05-08 08:23:19 +0000552ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000553 if( pList==0 ){
554 pList = sqliteMalloc( sizeof(ExprList) );
555 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000556 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000557 }
drh4efc4752004-01-16 15:55:37 +0000558 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000559 }
drh4305d102003-07-30 12:34:12 +0000560 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000561 struct ExprList_item *a;
562 int n = pList->nAlloc*2 + 4;
563 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
564 if( a==0 ){
565 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000566 }
danielk1977d5d56522005-03-16 12:15:20 +0000567 pList->a = a;
568 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000569 }
drh4efc4752004-01-16 15:55:37 +0000570 assert( pList->a!=0 );
571 if( pExpr || pName ){
572 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
573 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000574 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000575 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000576 }
577 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000578
579no_mem:
580 /* Avoid leaking memory if malloc has failed. */
581 sqlite3ExprDelete(pExpr);
582 sqlite3ExprListDelete(pList);
583 return 0;
drha76b5df2002-02-23 02:32:10 +0000584}
585
586/*
587** Delete an entire expression list.
588*/
danielk19774adee202004-05-08 08:23:19 +0000589void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000590 int i;
drhbe5c89a2004-07-26 00:31:09 +0000591 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000592 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000593 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
594 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000595 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
596 sqlite3ExprDelete(pItem->pExpr);
597 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000598 }
599 sqliteFree(pList->a);
600 sqliteFree(pList);
601}
602
603/*
drh626a8792005-01-17 22:08:19 +0000604** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000605**
drh626a8792005-01-17 22:08:19 +0000606** The return value from xFunc determines whether the tree walk continues.
607** 0 means continue walking the tree. 1 means do not walk children
608** of the current node but continue with siblings. 2 means abandon
609** the tree walk completely.
610**
611** The return value from this routine is 1 to abandon the tree walk
612** and 0 to continue.
613*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000614static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000615static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000616 int rc;
617 if( pExpr==0 ) return 0;
618 rc = (*xFunc)(pArg, pExpr);
619 if( rc==0 ){
620 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
621 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000622 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000623 }
624 return rc>1;
625}
626
627/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000628** Call walkExprTree() for every expression in list p.
629*/
630static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
631 int i;
632 struct ExprList_item *pItem;
633 if( !p ) return 0;
634 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
635 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
636 }
637 return 0;
638}
639
640/*
641** Call walkExprTree() for every expression in Select p, not including
642** expressions that are part of sub-selects in any FROM clause or the LIMIT
643** or OFFSET expressions..
644*/
645static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
646 walkExprList(p->pEList, xFunc, pArg);
647 walkExprTree(p->pWhere, xFunc, pArg);
648 walkExprList(p->pGroupBy, xFunc, pArg);
649 walkExprTree(p->pHaving, xFunc, pArg);
650 walkExprList(p->pOrderBy, xFunc, pArg);
651 return 0;
652}
653
654
655/*
drh626a8792005-01-17 22:08:19 +0000656** This routine is designed as an xFunc for walkExprTree().
657**
658** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000659** at pExpr that the expression that contains pExpr is not a constant
660** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
661** If pExpr does does not disqualify the expression from being a constant
662** then do nothing.
663**
664** After walking the whole tree, if no nodes are found that disqualify
665** the expression as constant, then we assume the whole expression
666** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000667*/
668static int exprNodeIsConstant(void *pArg, Expr *pExpr){
669 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000670 /* Consider functions to be constant if all their arguments are constant
671 ** and *pArg==2 */
672 case TK_FUNCTION:
673 if( *((int*)pArg)==2 ) return 0;
674 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000675 case TK_ID:
676 case TK_COLUMN:
677 case TK_DOT:
678 case TK_AGG_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000679#ifndef SQLITE_OMIT_SUBQUERY
680 case TK_SELECT:
681 case TK_EXISTS:
682#endif
drh626a8792005-01-17 22:08:19 +0000683 *((int*)pArg) = 0;
684 return 2;
685 default:
686 return 0;
687 }
688}
689
690/*
drhfef52082000-06-06 01:50:43 +0000691** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000692** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000693**
694** For the purposes of this function, a double-quoted string (ex: "abc")
695** is considered a variable but a single-quoted string (ex: 'abc') is
696** a constant.
drhfef52082000-06-06 01:50:43 +0000697*/
danielk19774adee202004-05-08 08:23:19 +0000698int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000699 int isConst = 1;
700 walkExprTree(p, exprNodeIsConstant, &isConst);
701 return isConst;
drhfef52082000-06-06 01:50:43 +0000702}
703
704/*
drheb55bd22005-06-30 17:04:21 +0000705** Walk an expression tree. Return 1 if the expression is constant
706** or a function call with constant arguments. Return and 0 if there
707** are any variables.
708**
709** For the purposes of this function, a double-quoted string (ex: "abc")
710** is considered a variable but a single-quoted string (ex: 'abc') is
711** a constant.
712*/
713int sqlite3ExprIsConstantOrFunction(Expr *p){
714 int isConst = 2;
715 walkExprTree(p, exprNodeIsConstant, &isConst);
716 return isConst!=0;
717}
718
719/*
drh73b211a2005-01-18 04:00:42 +0000720** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000721** to fit in a 32-bit integer, return 1 and put the value of the integer
722** in *pValue. If the expression is not an integer or if it is too big
723** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000724*/
danielk19774adee202004-05-08 08:23:19 +0000725int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000726 switch( p->op ){
727 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000728 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000729 return 1;
730 }
731 break;
drhe4de1fe2002-06-02 16:09:01 +0000732 }
drh4b59ab52002-08-24 18:24:51 +0000733 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000734 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000735 }
drhe4de1fe2002-06-02 16:09:01 +0000736 case TK_UMINUS: {
737 int v;
danielk19774adee202004-05-08 08:23:19 +0000738 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000739 *pValue = -v;
740 return 1;
741 }
742 break;
743 }
744 default: break;
745 }
746 return 0;
747}
748
749/*
drhc4a3c772001-04-04 11:48:57 +0000750** Return TRUE if the given string is a row-id column name.
751*/
danielk19774adee202004-05-08 08:23:19 +0000752int sqlite3IsRowid(const char *z){
753 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
754 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
755 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000756 return 0;
757}
758
759/*
drh8141f612004-01-25 22:44:58 +0000760** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
761** that name in the set of source tables in pSrcList and make the pExpr
762** expression node refer back to that source column. The following changes
763** are made to pExpr:
764**
765** pExpr->iDb Set the index in db->aDb[] of the database holding
766** the table.
767** pExpr->iTable Set to the cursor number for the table obtained
768** from pSrcList.
769** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000770** pExpr->op Set to TK_COLUMN.
771** pExpr->pLeft Any expression this points to is deleted
772** pExpr->pRight Any expression this points to is deleted.
773**
774** The pDbToken is the name of the database (the "X"). This value may be
775** NULL meaning that name is of the form Y.Z or Z. Any available database
776** can be used. The pTableToken is the name of the table (the "Y"). This
777** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
778** means that the form of the name is Z and that columns from any table
779** can be used.
780**
781** If the name cannot be resolved unambiguously, leave an error message
782** in pParse and return non-zero. Return zero on success.
783*/
784static int lookupName(
785 Parse *pParse, /* The parsing context */
786 Token *pDbToken, /* Name of the database containing table, or NULL */
787 Token *pTableToken, /* Name of table containing column, or NULL */
788 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000789 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000790 Expr *pExpr /* Make this EXPR node point to the selected column */
791){
792 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
793 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
794 char *zCol = 0; /* Name of the column. The "Z" */
795 int i, j; /* Loop counters */
796 int cnt = 0; /* Number of matching column names */
797 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000798 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000799 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
800 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000801 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000802
803 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000804 zDb = sqlite3NameFromToken(pDbToken);
805 zTab = sqlite3NameFromToken(pTableToken);
806 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000807 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000808 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000809 }
drh8141f612004-01-25 22:44:58 +0000810
811 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000812 while( pNC && cnt==0 ){
813 SrcList *pSrcList = pNC->pSrcList;
814 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000815
drh626a8792005-01-17 22:08:19 +0000816 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000817 if( pSrcList ){
818 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
819 Table *pTab = pItem->pTab;
820 Column *pCol;
821
822 if( pTab==0 ) continue;
823 assert( pTab->nCol>0 );
824 if( zTab ){
825 if( pItem->zAlias ){
826 char *zTabName = pItem->zAlias;
827 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
828 }else{
829 char *zTabName = pTab->zName;
830 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
831 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
832 continue;
833 }
drh626a8792005-01-17 22:08:19 +0000834 }
drh8141f612004-01-25 22:44:58 +0000835 }
danielk1977b3bce662005-01-29 08:32:43 +0000836 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000837 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000838 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000839 pMatch = pItem;
840 }
841 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
842 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000843 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000844 cnt++;
845 pExpr->iTable = pItem->iCursor;
846 pMatch = pItem;
847 pExpr->iDb = pTab->iDb;
848 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
849 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
850 pExpr->affinity = pTab->aCol[j].affinity;
851 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000852 if( pItem->jointype & JT_NATURAL ){
853 /* If this match occurred in the left table of a natural join,
854 ** then skip the right table to avoid a duplicate match */
855 pItem++;
856 i++;
857 }
drh873fac02005-06-06 17:11:46 +0000858 if( (pUsing = pItem->pUsing)!=0 ){
859 /* If this match occurs on a column that is in the USING clause
860 ** of a join, skip the search of the right table of the join
861 ** to avoid a duplicate match there. */
862 int k;
863 for(k=0; k<pUsing->nId; k++){
864 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
865 pItem++;
866 i++;
867 break;
868 }
869 }
870 }
danielk1977b3bce662005-01-29 08:32:43 +0000871 break;
872 }
drh8141f612004-01-25 22:44:58 +0000873 }
874 }
875 }
drh626a8792005-01-17 22:08:19 +0000876
877#ifndef SQLITE_OMIT_TRIGGER
878 /* If we have not already resolved the name, then maybe
879 ** it is a new.* or old.* trigger argument reference
880 */
881 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
882 TriggerStack *pTriggerStack = pParse->trigStack;
883 Table *pTab = 0;
884 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
885 pExpr->iTable = pTriggerStack->newIdx;
886 assert( pTriggerStack->pTab );
887 pTab = pTriggerStack->pTab;
888 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
889 pExpr->iTable = pTriggerStack->oldIdx;
890 assert( pTriggerStack->pTab );
891 pTab = pTriggerStack->pTab;
892 }
893
894 if( pTab ){
895 int j;
896 Column *pCol = pTab->aCol;
897
898 pExpr->iDb = pTab->iDb;
899 cntTab++;
900 for(j=0; j < pTab->nCol; j++, pCol++) {
901 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
902 cnt++;
903 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
904 pExpr->affinity = pTab->aCol[j].affinity;
905 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000906 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000907 break;
908 }
909 }
910 }
911 }
drhb7f91642004-10-31 02:22:47 +0000912#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000913
drh626a8792005-01-17 22:08:19 +0000914 /*
915 ** Perhaps the name is a reference to the ROWID
916 */
917 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
918 cnt = 1;
919 pExpr->iColumn = -1;
920 pExpr->affinity = SQLITE_AFF_INTEGER;
921 }
drh8141f612004-01-25 22:44:58 +0000922
drh626a8792005-01-17 22:08:19 +0000923 /*
924 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
925 ** might refer to an result-set alias. This happens, for example, when
926 ** we are resolving names in the WHERE clause of the following command:
927 **
928 ** SELECT a+b AS x FROM table WHERE x<10;
929 **
930 ** In cases like this, replace pExpr with a copy of the expression that
931 ** forms the result set entry ("a+b" in the example) and return immediately.
932 ** Note that the expression in the result set should have already been
933 ** resolved by the time the WHERE clause is resolved.
934 */
drh79d5f632005-01-18 17:20:10 +0000935 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000936 for(j=0; j<pEList->nExpr; j++){
937 char *zAs = pEList->a[j].zName;
938 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
939 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
940 pExpr->op = TK_AS;
941 pExpr->iColumn = j;
942 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000943 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000944 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000945 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000946 }
947 }
948 }
949
950 /* Advance to the next name context. The loop will exit when either
951 ** we have a match (cnt>0) or when we run out of name contexts.
952 */
953 if( cnt==0 ){
954 pNC = pNC->pNext;
955 }
drh8141f612004-01-25 22:44:58 +0000956 }
957
958 /*
959 ** If X and Y are NULL (in other words if only the column name Z is
960 ** supplied) and the value of Z is enclosed in double-quotes, then
961 ** Z is a string literal if it doesn't match any column names. In that
962 ** case, we need to return right away and not make any changes to
963 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000964 **
965 ** Because no reference was made to outer contexts, the pNC->nRef
966 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000967 */
968 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
969 sqliteFree(zCol);
970 return 0;
971 }
972
973 /*
974 ** cnt==0 means there was not match. cnt>1 means there were two or
975 ** more matches. Either way, we have an error.
976 */
977 if( cnt!=1 ){
978 char *z = 0;
979 char *zErr;
980 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
981 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000982 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000983 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000984 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000985 }else{
986 z = sqliteStrDup(zCol);
987 }
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000989 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000990 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000991 }
992
drh51669862004-12-18 18:40:26 +0000993 /* If a column from a table in pSrcList is referenced, then record
994 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
995 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
996 ** column number is greater than the number of bits in the bitmask
997 ** then set the high-order bit of the bitmask.
998 */
999 if( pExpr->iColumn>=0 && pMatch!=0 ){
1000 int n = pExpr->iColumn;
1001 if( n>=sizeof(Bitmask)*8 ){
1002 n = sizeof(Bitmask)*8-1;
1003 }
1004 assert( pMatch->iCursor==pExpr->iTable );
1005 pMatch->colUsed |= 1<<n;
1006 }
1007
danielk1977d5d56522005-03-16 12:15:20 +00001008lookupname_end:
drh8141f612004-01-25 22:44:58 +00001009 /* Clean up and return
1010 */
1011 sqliteFree(zDb);
1012 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001013 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001014 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001015 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001016 pExpr->pRight = 0;
1017 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001018lookupname_end_2:
1019 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001020 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001021 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001022 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001023 if( pMatch && !pMatch->pSelect ){
1024 pExpr->pTab = pMatch->pTab;
1025 }
drh15ccce12005-05-23 15:06:39 +00001026 /* Increment the nRef value on all name contexts from TopNC up to
1027 ** the point where the name matched. */
1028 for(;;){
1029 assert( pTopNC!=0 );
1030 pTopNC->nRef++;
1031 if( pTopNC==pNC ) break;
1032 pTopNC = pTopNC->pNext;
1033 }
1034 return 0;
1035 } else {
1036 return 1;
drh626a8792005-01-17 22:08:19 +00001037 }
drh8141f612004-01-25 22:44:58 +00001038}
1039
1040/*
drh626a8792005-01-17 22:08:19 +00001041** This routine is designed as an xFunc for walkExprTree().
1042**
drh73b211a2005-01-18 04:00:42 +00001043** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001044** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001045** the tree or 2 to abort the tree walk.
1046**
1047** This routine also does error checking and name resolution for
1048** function names. The operator for aggregate functions is changed
1049** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001050*/
1051static int nameResolverStep(void *pArg, Expr *pExpr){
1052 NameContext *pNC = (NameContext*)pArg;
1053 SrcList *pSrcList;
1054 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001055
danielk1977b3bce662005-01-29 08:32:43 +00001056 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001057 assert( pNC!=0 );
1058 pSrcList = pNC->pSrcList;
1059 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001060
drh626a8792005-01-17 22:08:19 +00001061 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1062 ExprSetProperty(pExpr, EP_Resolved);
1063#ifndef NDEBUG
1064 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001065 int i;
drh626a8792005-01-17 22:08:19 +00001066 for(i=0; i<pSrcList->nSrc; i++){
1067 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1068 }
1069 }
1070#endif
1071 switch( pExpr->op ){
1072 /* Double-quoted strings (ex: "abc") are used as identifiers if
1073 ** possible. Otherwise they remain as strings. Single-quoted
1074 ** strings (ex: 'abc') are always string literals.
1075 */
1076 case TK_STRING: {
1077 if( pExpr->token.z[0]=='\'' ) break;
1078 /* Fall thru into the TK_ID case if this is a double-quoted string */
1079 }
1080 /* A lone identifier is the name of a column.
1081 */
1082 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001083 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1084 return 1;
1085 }
1086
1087 /* A table name and column name: ID.ID
1088 ** Or a database, table and column: ID.ID.ID
1089 */
1090 case TK_DOT: {
1091 Token *pColumn;
1092 Token *pTable;
1093 Token *pDb;
1094 Expr *pRight;
1095
danielk1977b3bce662005-01-29 08:32:43 +00001096 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001097 pRight = pExpr->pRight;
1098 if( pRight->op==TK_ID ){
1099 pDb = 0;
1100 pTable = &pExpr->pLeft->token;
1101 pColumn = &pRight->token;
1102 }else{
1103 assert( pRight->op==TK_DOT );
1104 pDb = &pExpr->pLeft->token;
1105 pTable = &pRight->pLeft->token;
1106 pColumn = &pRight->pRight->token;
1107 }
1108 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1109 return 1;
1110 }
1111
1112 /* Resolve function names
1113 */
drhb71090f2005-05-23 17:26:51 +00001114 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001115 case TK_FUNCTION: {
1116 ExprList *pList = pExpr->pList; /* The argument list */
1117 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1118 int no_such_func = 0; /* True if no such function exists */
1119 int wrong_num_args = 0; /* True if wrong number of arguments */
1120 int is_agg = 0; /* True if is an aggregate function */
1121 int i;
1122 int nId; /* Number of characters in function name */
1123 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001124 FuncDef *pDef; /* Information about the function */
1125 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001126
drhb71090f2005-05-23 17:26:51 +00001127 zId = pExpr->token.z;
1128 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001129 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1130 if( pDef==0 ){
1131 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1132 if( pDef==0 ){
1133 no_such_func = 1;
1134 }else{
1135 wrong_num_args = 1;
1136 }
1137 }else{
1138 is_agg = pDef->xFunc==0;
1139 }
1140 if( is_agg && !pNC->allowAgg ){
1141 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1142 pNC->nErr++;
1143 is_agg = 0;
1144 }else if( no_such_func ){
1145 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1146 pNC->nErr++;
1147 }else if( wrong_num_args ){
1148 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1149 nId, zId);
1150 pNC->nErr++;
1151 }
1152 if( is_agg ){
1153 pExpr->op = TK_AGG_FUNCTION;
1154 pNC->hasAgg = 1;
1155 }
drh73b211a2005-01-18 04:00:42 +00001156 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001157 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001158 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001159 }
drh73b211a2005-01-18 04:00:42 +00001160 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001161 /* FIX ME: Compute pExpr->affinity based on the expected return
1162 ** type of the function
1163 */
1164 return is_agg;
1165 }
danielk1977b3bce662005-01-29 08:32:43 +00001166#ifndef SQLITE_OMIT_SUBQUERY
1167 case TK_SELECT:
1168 case TK_EXISTS:
1169#endif
1170 case TK_IN: {
1171 if( pExpr->pSelect ){
1172 int nRef = pNC->nRef;
1173 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1174 assert( pNC->nRef>=nRef );
1175 if( nRef!=pNC->nRef ){
1176 ExprSetProperty(pExpr, EP_VarSelect);
1177 }
1178 }
1179 }
drh626a8792005-01-17 22:08:19 +00001180 }
1181 return 0;
1182}
1183
1184/*
drhcce7d172000-05-31 15:34:51 +00001185** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001186** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001187** index to the table in the table list and a column offset. The
1188** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1189** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001190** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001191** VDBE cursor number for a cursor that is pointing into the referenced
1192** table. The Expr.iColumn value is changed to the index of the column
1193** of the referenced table. The Expr.iColumn value for the special
1194** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1195** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001196**
drh626a8792005-01-17 22:08:19 +00001197** Also resolve function names and check the functions for proper
1198** usage. Make sure all function names are recognized and all functions
1199** have the correct number of arguments. Leave an error message
1200** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1201**
drh73b211a2005-01-18 04:00:42 +00001202** If the expression contains aggregate functions then set the EP_Agg
1203** property on the expression.
drh626a8792005-01-17 22:08:19 +00001204*/
1205int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001206 NameContext *pNC, /* Namespace to resolve expressions in. */
1207 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001208){
drh73b211a2005-01-18 04:00:42 +00001209 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001210 walkExprTree(pExpr, nameResolverStep, pNC);
1211 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001212 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001213 }
1214 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001215}
1216
drh1398ad32005-01-19 23:24:50 +00001217/*
1218** A pointer instance of this structure is used to pass information
1219** through walkExprTree into codeSubqueryStep().
1220*/
1221typedef struct QueryCoder QueryCoder;
1222struct QueryCoder {
1223 Parse *pParse; /* The parsing context */
1224 NameContext *pNC; /* Namespace of first enclosing query */
1225};
1226
drh626a8792005-01-17 22:08:19 +00001227
1228/*
1229** Generate code for subqueries and IN operators.
1230**
drh73b211a2005-01-18 04:00:42 +00001231** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001232**
1233** expr IN (exprlist)
1234** and
1235** expr IN (SELECT ...)
1236**
1237** The first form is handled by creating a set holding the list
1238** of allowed values. The second form causes the SELECT to generate
1239** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001240*/
drh51522cd2005-01-20 13:36:19 +00001241#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001242void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001243 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001244 Vdbe *v = sqlite3GetVdbe(pParse);
1245 if( v==0 ) return;
1246
drh57dbd7b2005-07-08 18:25:26 +00001247 /* This code must be run in its entirety every time it is encountered
1248 ** if any of the following is true:
1249 **
1250 ** * The right-hand side is a correlated subquery
1251 ** * The right-hand side is an expression list containing variables
1252 ** * We are inside a trigger
1253 **
1254 ** If all of the above are false, then we can run this code just once
1255 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001256 */
1257 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1258 int mem = pParse->nMem++;
1259 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001260 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
1261 assert( testAddr>0 );
danielk1977b3bce662005-01-29 08:32:43 +00001262 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1263 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1264 }
1265
1266 if( pExpr->pSelect ){
1267 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1268 }
drh6a3ea0e2003-05-02 14:32:12 +00001269
drhcce7d172000-05-31 15:34:51 +00001270 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001271 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001272 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001273 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001274 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001275
danielk1977bf3b7212004-05-18 10:06:24 +00001276 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001277
1278 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001279 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001280 ** filled with single-field index keys representing the results
1281 ** from the SELECT or the <exprlist>.
1282 **
1283 ** If the 'x' expression is a column value, or the SELECT...
1284 ** statement returns a column value, then the affinity of that
1285 ** column is used to build the index keys. If both 'x' and the
1286 ** SELECT... statement are columns, then numeric affinity is used
1287 ** if either column has NUMERIC or INTEGER affinity. If neither
1288 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1289 ** is used.
1290 */
1291 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001292 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001293 memset(&keyInfo, 0, sizeof(keyInfo));
1294 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001295 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001296
drhfef52082000-06-06 01:50:43 +00001297 if( pExpr->pSelect ){
1298 /* Case 1: expr IN (SELECT ...)
1299 **
danielk1977e014a832004-05-17 10:48:57 +00001300 ** Generate code to write the results of the select into the temporary
1301 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001302 */
danielk1977e014a832004-05-17 10:48:57 +00001303 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001304 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001305 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001306 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001307 pEList = pExpr->pSelect->pEList;
1308 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001309 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001310 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001311 }
drhfef52082000-06-06 01:50:43 +00001312 }else if( pExpr->pList ){
1313 /* Case 2: expr IN (exprlist)
1314 **
danielk1977e014a832004-05-17 10:48:57 +00001315 ** For each expression, build an index key from the evaluation and
1316 ** store it in the temporary table. If <expr> is a column, then use
1317 ** that columns affinity when building index keys. If <expr> is not
1318 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001319 */
danielk1977e014a832004-05-17 10:48:57 +00001320 int i;
drh57dbd7b2005-07-08 18:25:26 +00001321 ExprList *pList = pExpr->pList;
1322 struct ExprList_item *pItem;
1323
danielk1977e014a832004-05-17 10:48:57 +00001324 if( !affinity ){
1325 affinity = SQLITE_AFF_NUMERIC;
1326 }
danielk19770202b292004-06-09 09:55:16 +00001327 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001328
1329 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001330 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1331 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001332
drh57dbd7b2005-07-08 18:25:26 +00001333 /* If the expression is not constant then we will need to
1334 ** disable the test that was generated above that makes sure
1335 ** this code only executes once. Because for a non-constant
1336 ** expression we need to rerun this code each time.
1337 */
1338 if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
1339 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1340 int i;
1341 for(i=0; i<4; i++){
1342 aOp[i].opcode = OP_Noop;
1343 }
1344 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001345 }
danielk1977e014a832004-05-17 10:48:57 +00001346
1347 /* Evaluate the expression and insert it into the temp table */
1348 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001349 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001350 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001351 }
1352 }
danielk19770202b292004-06-09 09:55:16 +00001353 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001354 break;
drhfef52082000-06-06 01:50:43 +00001355 }
1356
drh51522cd2005-01-20 13:36:19 +00001357 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001358 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001359 /* This has to be a scalar SELECT. Generate code to put the
1360 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001361 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001362 */
drh51522cd2005-01-20 13:36:19 +00001363 int sop;
1364 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001365
drh967e8b72000-06-21 13:59:10 +00001366 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001367 pSel = pExpr->pSelect;
1368 if( pExpr->op==TK_SELECT ){
1369 sop = SRT_Mem;
1370 }else{
1371 static const Token one = { "1", 0, 1 };
1372 sop = SRT_Exists;
1373 sqlite3ExprListDelete(pSel->pEList);
1374 pSel->pEList = sqlite3ExprListAppend(0,
1375 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1376 }
danielk1977b3bce662005-01-29 08:32:43 +00001377 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1378 break;
drhcce7d172000-05-31 15:34:51 +00001379 }
1380 }
danielk1977b3bce662005-01-29 08:32:43 +00001381
1382 if( pExpr->pSelect ){
1383 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1384 }
drh57dbd7b2005-07-08 18:25:26 +00001385 if( testAddr ){
1386 sqlite3VdbeChangeP2(v, testAddr, sqlite3VdbeCurrentAddr(v));
danielk1977b3bce662005-01-29 08:32:43 +00001387 }
1388 return;
drhcce7d172000-05-31 15:34:51 +00001389}
drh51522cd2005-01-20 13:36:19 +00001390#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001391
drhcce7d172000-05-31 15:34:51 +00001392/*
drhfec19aa2004-05-19 20:41:03 +00001393** Generate an instruction that will put the integer describe by
1394** text z[0..n-1] on the stack.
1395*/
1396static void codeInteger(Vdbe *v, const char *z, int n){
1397 int i;
drh6fec0762004-05-30 01:38:43 +00001398 if( sqlite3GetInt32(z, &i) ){
1399 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1400 }else if( sqlite3FitsIn64Bits(z) ){
1401 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001402 }else{
1403 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1404 }
1405}
1406
1407/*
drhcce7d172000-05-31 15:34:51 +00001408** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001409** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001410**
1411** This code depends on the fact that certain token values (ex: TK_EQ)
1412** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1413** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1414** the make process cause these values to align. Assert()s in the code
1415** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001416*/
danielk19774adee202004-05-08 08:23:19 +00001417void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001418 Vdbe *v = pParse->pVdbe;
1419 int op;
danielk19777977a172004-11-09 12:44:37 +00001420 if( v==0 ) return;
1421 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001422 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001423 return;
1424 }
drhf2bc0132004-10-04 13:19:23 +00001425 op = pExpr->op;
1426 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001427 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001428 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1429 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001430 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001431 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001432 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001433 }else{
drhf0863fe2005-06-12 21:35:51 +00001434 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001435 }
drhcce7d172000-05-31 15:34:51 +00001436 break;
1437 }
1438 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001439 codeInteger(v, pExpr->token.z, pExpr->token.n);
1440 break;
1441 }
1442 case TK_FLOAT:
1443 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001444 assert( TK_FLOAT==OP_Real );
1445 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001446 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001447 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001448 break;
1449 }
drhf0863fe2005-06-12 21:35:51 +00001450 case TK_NULL: {
1451 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1452 break;
1453 }
danielk19775338a5f2005-01-20 13:03:10 +00001454#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001455 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001456 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001457 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1458 sqlite3VdbeDequoteP3(v, -1);
1459 break;
1460 }
danielk19775338a5f2005-01-20 13:03:10 +00001461#endif
drh50457892003-09-06 01:10:47 +00001462 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001463 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001464 if( pExpr->token.n>1 ){
1465 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1466 }
drh50457892003-09-06 01:10:47 +00001467 break;
1468 }
drh4e0cff62004-11-05 05:10:28 +00001469 case TK_REGISTER: {
1470 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1471 break;
1472 }
drh487e2622005-06-25 18:42:14 +00001473#ifndef SQLITE_OMIT_CAST
1474 case TK_CAST: {
1475 /* Expressions of the form: CAST(pLeft AS token) */
1476 int aff, op;
1477 sqlite3ExprCode(pParse, pExpr->pLeft);
1478 aff = sqlite3AffinityType(&pExpr->token);
1479 switch( aff ){
1480 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1481 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1482 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1483 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1484 }
1485 sqlite3VdbeAddOp(v, op, 0, 0);
1486 break;
1487 }
1488#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001489 case TK_LT:
1490 case TK_LE:
1491 case TK_GT:
1492 case TK_GE:
1493 case TK_NE:
1494 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001495 assert( TK_LT==OP_Lt );
1496 assert( TK_LE==OP_Le );
1497 assert( TK_GT==OP_Gt );
1498 assert( TK_GE==OP_Ge );
1499 assert( TK_EQ==OP_Eq );
1500 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001501 sqlite3ExprCode(pParse, pExpr->pLeft);
1502 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001503 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001504 break;
drhc9b84a12002-06-20 11:36:48 +00001505 }
drhcce7d172000-05-31 15:34:51 +00001506 case TK_AND:
1507 case TK_OR:
1508 case TK_PLUS:
1509 case TK_STAR:
1510 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001511 case TK_REM:
1512 case TK_BITAND:
1513 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001514 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001515 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001516 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001517 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001518 assert( TK_AND==OP_And );
1519 assert( TK_OR==OP_Or );
1520 assert( TK_PLUS==OP_Add );
1521 assert( TK_MINUS==OP_Subtract );
1522 assert( TK_REM==OP_Remainder );
1523 assert( TK_BITAND==OP_BitAnd );
1524 assert( TK_BITOR==OP_BitOr );
1525 assert( TK_SLASH==OP_Divide );
1526 assert( TK_LSHIFT==OP_ShiftLeft );
1527 assert( TK_RSHIFT==OP_ShiftRight );
1528 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001529 sqlite3ExprCode(pParse, pExpr->pLeft);
1530 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001531 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001532 break;
1533 }
drhcce7d172000-05-31 15:34:51 +00001534 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001535 Expr *pLeft = pExpr->pLeft;
1536 assert( pLeft );
1537 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1538 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001539 char *z = sqliteMalloc( p->n + 2 );
1540 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001541 if( pLeft->op==TK_FLOAT ){
1542 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001543 }else{
drhfec19aa2004-05-19 20:41:03 +00001544 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001545 }
drh6e142f52000-06-08 13:36:40 +00001546 sqliteFree(z);
1547 break;
1548 }
drh1ccde152000-06-17 13:12:39 +00001549 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001550 }
drhbf4133c2001-10-13 02:59:08 +00001551 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001552 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001553 assert( TK_BITNOT==OP_BitNot );
1554 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001555 sqlite3ExprCode(pParse, pExpr->pLeft);
1556 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001557 break;
1558 }
1559 case TK_ISNULL:
1560 case TK_NOTNULL: {
1561 int dest;
drhf2bc0132004-10-04 13:19:23 +00001562 assert( TK_ISNULL==OP_IsNull );
1563 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001564 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1565 sqlite3ExprCode(pParse, pExpr->pLeft);
1566 dest = sqlite3VdbeCurrentAddr(v) + 2;
1567 sqlite3VdbeAddOp(v, op, 1, dest);
1568 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001569 break;
drhcce7d172000-05-31 15:34:51 +00001570 }
drh22827922000-06-06 17:27:05 +00001571 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001572 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001573 break;
1574 }
drhb71090f2005-05-23 17:26:51 +00001575 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001576 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001577 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001578 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001579 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001580 int nId;
1581 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001582 int p2 = 0;
1583 int i;
danielk1977d8123362004-06-12 09:25:12 +00001584 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001585 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001586 zId = pExpr->token.z;
1587 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001588 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001589 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001590 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001591 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001592 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1593 p2 |= (1<<i);
1594 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001595 if( pDef->needCollSeq && !pColl ){
1596 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1597 }
1598 }
1599 if( pDef->needCollSeq ){
1600 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001601 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001602 }
1603 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001604 break;
1605 }
drhfe2093d2005-01-20 22:48:47 +00001606#ifndef SQLITE_OMIT_SUBQUERY
1607 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001608 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001609 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001610 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001611 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001612 break;
1613 }
drhfef52082000-06-06 01:50:43 +00001614 case TK_IN: {
1615 int addr;
drh94a11212004-09-25 13:12:14 +00001616 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001617 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001618
1619 /* Figure out the affinity to use to create a key from the results
1620 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001621 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001622 */
drh94a11212004-09-25 13:12:14 +00001623 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001624
danielk19774adee202004-05-08 08:23:19 +00001625 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001626
1627 /* Code the <expr> from "<expr> IN (...)". The temporary table
1628 ** pExpr->iTable contains the values that make up the (...) set.
1629 */
danielk19774adee202004-05-08 08:23:19 +00001630 sqlite3ExprCode(pParse, pExpr->pLeft);
1631 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001632 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001633 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001634 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001635 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001636 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001637 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1638 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1639
drhfef52082000-06-06 01:50:43 +00001640 break;
1641 }
danielk197793758c82005-01-21 08:13:14 +00001642#endif
drhfef52082000-06-06 01:50:43 +00001643 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001644 Expr *pLeft = pExpr->pLeft;
1645 struct ExprList_item *pLItem = pExpr->pList->a;
1646 Expr *pRight = pLItem->pExpr;
1647 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001648 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001649 sqlite3ExprCode(pParse, pRight);
1650 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001651 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001652 pLItem++;
1653 pRight = pLItem->pExpr;
1654 sqlite3ExprCode(pParse, pRight);
1655 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001657 break;
1658 }
drh51e9a442004-01-16 16:42:53 +00001659 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001660 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001661 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001662 break;
1663 }
drh17a7f8d2002-03-24 13:13:27 +00001664 case TK_CASE: {
1665 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001666 int jumpInst;
1667 int addr;
1668 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001669 int i;
drhbe5c89a2004-07-26 00:31:09 +00001670 ExprList *pEList;
1671 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001672
1673 assert(pExpr->pList);
1674 assert((pExpr->pList->nExpr % 2) == 0);
1675 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001676 pEList = pExpr->pList;
1677 aListelem = pEList->a;
1678 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001679 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001680 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001681 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001682 }
drhf5905aa2002-05-26 20:54:33 +00001683 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001684 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001685 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001686 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001687 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1688 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001689 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001690 }else{
danielk19774adee202004-05-08 08:23:19 +00001691 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001692 }
drhbe5c89a2004-07-26 00:31:09 +00001693 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001694 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1695 addr = sqlite3VdbeCurrentAddr(v);
1696 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001697 }
drhf570f012002-05-31 15:51:25 +00001698 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001699 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001700 }
drh17a7f8d2002-03-24 13:13:27 +00001701 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001702 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001703 }else{
drhf0863fe2005-06-12 21:35:51 +00001704 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001705 }
danielk19774adee202004-05-08 08:23:19 +00001706 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001707 break;
1708 }
danielk19775338a5f2005-01-20 13:03:10 +00001709#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001710 case TK_RAISE: {
1711 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001712 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001713 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001714 return;
1715 }
drhad6d9462004-09-19 02:15:24 +00001716 if( pExpr->iColumn!=OE_Ignore ){
1717 assert( pExpr->iColumn==OE_Rollback ||
1718 pExpr->iColumn == OE_Abort ||
1719 pExpr->iColumn == OE_Fail );
1720 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1721 pExpr->token.z, pExpr->token.n);
1722 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001723 } else {
drhad6d9462004-09-19 02:15:24 +00001724 assert( pExpr->iColumn == OE_Ignore );
1725 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1726 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1727 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001728 }
drh17a7f8d2002-03-24 13:13:27 +00001729 }
danielk19775338a5f2005-01-20 13:03:10 +00001730#endif
drh17a7f8d2002-03-24 13:13:27 +00001731 break;
drhcce7d172000-05-31 15:34:51 +00001732 }
drhcce7d172000-05-31 15:34:51 +00001733}
1734
danielk197793758c82005-01-21 08:13:14 +00001735#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001736/*
drh25303782004-12-07 15:41:48 +00001737** Generate code that evalutes the given expression and leaves the result
1738** on the stack. See also sqlite3ExprCode().
1739**
1740** This routine might also cache the result and modify the pExpr tree
1741** so that it will make use of the cached result on subsequent evaluations
1742** rather than evaluate the whole expression again. Trivial expressions are
1743** not cached. If the expression is cached, its result is stored in a
1744** memory location.
1745*/
1746void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1747 Vdbe *v = pParse->pVdbe;
1748 int iMem;
1749 int addr1, addr2;
1750 if( v==0 ) return;
1751 addr1 = sqlite3VdbeCurrentAddr(v);
1752 sqlite3ExprCode(pParse, pExpr);
1753 addr2 = sqlite3VdbeCurrentAddr(v);
1754 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1755 iMem = pExpr->iTable = pParse->nMem++;
1756 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1757 pExpr->op = TK_REGISTER;
1758 }
1759}
danielk197793758c82005-01-21 08:13:14 +00001760#endif
drh25303782004-12-07 15:41:48 +00001761
1762/*
drh268380c2004-02-25 13:47:31 +00001763** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001764** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001765**
1766** Return the number of elements pushed onto the stack.
1767*/
danielk19774adee202004-05-08 08:23:19 +00001768int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001769 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001770 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001771){
1772 struct ExprList_item *pItem;
1773 int i, n;
1774 Vdbe *v;
1775 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001776 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001777 n = pList->nExpr;
1778 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001780 }
drhf9b596e2004-05-26 16:54:42 +00001781 return n;
drh268380c2004-02-25 13:47:31 +00001782}
1783
1784/*
drhcce7d172000-05-31 15:34:51 +00001785** Generate code for a boolean expression such that a jump is made
1786** to the label "dest" if the expression is true but execution
1787** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001788**
1789** If the expression evaluates to NULL (neither true nor false), then
1790** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001791**
1792** This code depends on the fact that certain token values (ex: TK_EQ)
1793** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1794** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1795** the make process cause these values to align. Assert()s in the code
1796** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001797*/
danielk19774adee202004-05-08 08:23:19 +00001798void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001799 Vdbe *v = pParse->pVdbe;
1800 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001801 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001802 op = pExpr->op;
1803 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001804 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001805 int d2 = sqlite3VdbeMakeLabel(v);
1806 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1807 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1808 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001809 break;
1810 }
1811 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001812 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1813 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001814 break;
1815 }
1816 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001817 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001818 break;
1819 }
1820 case TK_LT:
1821 case TK_LE:
1822 case TK_GT:
1823 case TK_GE:
1824 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001825 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001826 assert( TK_LT==OP_Lt );
1827 assert( TK_LE==OP_Le );
1828 assert( TK_GT==OP_Gt );
1829 assert( TK_GE==OP_Ge );
1830 assert( TK_EQ==OP_Eq );
1831 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001832 sqlite3ExprCode(pParse, pExpr->pLeft);
1833 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001834 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001835 break;
1836 }
1837 case TK_ISNULL:
1838 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001839 assert( TK_ISNULL==OP_IsNull );
1840 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001841 sqlite3ExprCode(pParse, pExpr->pLeft);
1842 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001843 break;
1844 }
drhfef52082000-06-06 01:50:43 +00001845 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001846 /* The expression "x BETWEEN y AND z" is implemented as:
1847 **
1848 ** 1 IF (x < y) GOTO 3
1849 ** 2 IF (x <= z) GOTO <dest>
1850 ** 3 ...
1851 */
drhf5905aa2002-05-26 20:54:33 +00001852 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001853 Expr *pLeft = pExpr->pLeft;
1854 Expr *pRight = pExpr->pList->a[0].pExpr;
1855 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001856 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001857 sqlite3ExprCode(pParse, pRight);
1858 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001859
drhbe5c89a2004-07-26 00:31:09 +00001860 pRight = pExpr->pList->a[1].pExpr;
1861 sqlite3ExprCode(pParse, pRight);
1862 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001863
danielk19774adee202004-05-08 08:23:19 +00001864 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1865 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1866 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001867 break;
1868 }
drhcce7d172000-05-31 15:34:51 +00001869 default: {
danielk19774adee202004-05-08 08:23:19 +00001870 sqlite3ExprCode(pParse, pExpr);
1871 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001872 break;
1873 }
1874 }
1875}
1876
1877/*
drh66b89c82000-11-28 20:47:17 +00001878** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001879** to the label "dest" if the expression is false but execution
1880** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001881**
1882** If the expression evaluates to NULL (neither true nor false) then
1883** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001884*/
danielk19774adee202004-05-08 08:23:19 +00001885void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001886 Vdbe *v = pParse->pVdbe;
1887 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001888 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001889
1890 /* The value of pExpr->op and op are related as follows:
1891 **
1892 ** pExpr->op op
1893 ** --------- ----------
1894 ** TK_ISNULL OP_NotNull
1895 ** TK_NOTNULL OP_IsNull
1896 ** TK_NE OP_Eq
1897 ** TK_EQ OP_Ne
1898 ** TK_GT OP_Le
1899 ** TK_LE OP_Gt
1900 ** TK_GE OP_Lt
1901 ** TK_LT OP_Ge
1902 **
1903 ** For other values of pExpr->op, op is undefined and unused.
1904 ** The value of TK_ and OP_ constants are arranged such that we
1905 ** can compute the mapping above using the following expression.
1906 ** Assert()s verify that the computation is correct.
1907 */
1908 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1909
1910 /* Verify correct alignment of TK_ and OP_ constants
1911 */
1912 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1913 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1914 assert( pExpr->op!=TK_NE || op==OP_Eq );
1915 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1916 assert( pExpr->op!=TK_LT || op==OP_Ge );
1917 assert( pExpr->op!=TK_LE || op==OP_Gt );
1918 assert( pExpr->op!=TK_GT || op==OP_Le );
1919 assert( pExpr->op!=TK_GE || op==OP_Lt );
1920
drhcce7d172000-05-31 15:34:51 +00001921 switch( pExpr->op ){
1922 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001923 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1924 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001925 break;
1926 }
1927 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001928 int d2 = sqlite3VdbeMakeLabel(v);
1929 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1930 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1931 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001932 break;
1933 }
1934 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001935 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001936 break;
1937 }
1938 case TK_LT:
1939 case TK_LE:
1940 case TK_GT:
1941 case TK_GE:
1942 case TK_NE:
1943 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001944 sqlite3ExprCode(pParse, pExpr->pLeft);
1945 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001946 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001947 break;
1948 }
drhcce7d172000-05-31 15:34:51 +00001949 case TK_ISNULL:
1950 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001951 sqlite3ExprCode(pParse, pExpr->pLeft);
1952 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001953 break;
1954 }
drhfef52082000-06-06 01:50:43 +00001955 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001956 /* The expression is "x BETWEEN y AND z". It is implemented as:
1957 **
1958 ** 1 IF (x >= y) GOTO 3
1959 ** 2 GOTO <dest>
1960 ** 3 IF (x > z) GOTO <dest>
1961 */
drhfef52082000-06-06 01:50:43 +00001962 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001963 Expr *pLeft = pExpr->pLeft;
1964 Expr *pRight = pExpr->pList->a[0].pExpr;
1965 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001966 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001967 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001968 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001969 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1970
danielk19774adee202004-05-08 08:23:19 +00001971 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1972 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001973 pRight = pExpr->pList->a[1].pExpr;
1974 sqlite3ExprCode(pParse, pRight);
1975 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001976 break;
1977 }
drhcce7d172000-05-31 15:34:51 +00001978 default: {
danielk19774adee202004-05-08 08:23:19 +00001979 sqlite3ExprCode(pParse, pExpr);
1980 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001981 break;
1982 }
1983 }
1984}
drh22827922000-06-06 17:27:05 +00001985
1986/*
1987** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1988** if they are identical and return FALSE if they differ in any way.
1989*/
danielk19774adee202004-05-08 08:23:19 +00001990int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001991 int i;
1992 if( pA==0 ){
1993 return pB==0;
1994 }else if( pB==0 ){
1995 return 0;
1996 }
1997 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001998 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1999 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002000 if( pA->pList ){
2001 if( pB->pList==0 ) return 0;
2002 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2003 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002004 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002005 return 0;
2006 }
2007 }
2008 }else if( pB->pList ){
2009 return 0;
2010 }
2011 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002012 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002013 if( pA->token.z ){
2014 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002015 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002016 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00002017 }
2018 return 1;
2019}
2020
2021/*
2022** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00002023** The new element is initialized to zero. The calling function is
2024** expected to fill it in.
drh22827922000-06-06 17:27:05 +00002025*/
2026static int appendAggInfo(Parse *pParse){
2027 if( (pParse->nAgg & 0x7)==0 ){
2028 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00002029 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
2030 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00002031 return -1;
2032 }
drh6d4abfb2001-10-22 02:58:08 +00002033 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00002034 }
2035 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
2036 return pParse->nAgg++;
2037}
2038
2039/*
drh626a8792005-01-17 22:08:19 +00002040** This is an xFunc for walkExprTree() used to implement
2041** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2042** for additional information.
drh22827922000-06-06 17:27:05 +00002043**
drh626a8792005-01-17 22:08:19 +00002044** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002045*/
drh626a8792005-01-17 22:08:19 +00002046static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002047 int i;
2048 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00002049 NameContext *pNC = (NameContext *)pArg;
2050 Parse *pParse = pNC->pParse;
2051 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00002052
drh22827922000-06-06 17:27:05 +00002053 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002054 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002055 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2056 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2057 aAgg = pParse->aAgg;
2058 for(i=0; i<pParse->nAgg; i++){
2059 if( aAgg[i].isAgg ) continue;
2060 if( aAgg[i].pExpr->iTable==pExpr->iTable
2061 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2062 break;
2063 }
2064 }
2065 if( i>=pParse->nAgg ){
2066 i = appendAggInfo(pParse);
2067 if( i<0 ) return 1;
2068 pParse->aAgg[i].isAgg = 0;
2069 pParse->aAgg[i].pExpr = pExpr;
2070 }
2071 pExpr->iAgg = i;
2072 pExpr->iAggCtx = pNC->nDepth;
2073 return 1;
drh22827922000-06-06 17:27:05 +00002074 }
2075 }
drh626a8792005-01-17 22:08:19 +00002076 return 1;
drh22827922000-06-06 17:27:05 +00002077 }
2078 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002079 if( pNC->nDepth==0 ){
2080 aAgg = pParse->aAgg;
2081 for(i=0; i<pParse->nAgg; i++){
2082 if( !aAgg[i].isAgg ) continue;
2083 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2084 break;
2085 }
drh22827922000-06-06 17:27:05 +00002086 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002087 if( i>=pParse->nAgg ){
2088 u8 enc = pParse->db->enc;
2089 i = appendAggInfo(pParse);
2090 if( i<0 ) return 1;
2091 pParse->aAgg[i].isAgg = 1;
2092 pParse->aAgg[i].pExpr = pExpr;
2093 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2094 pExpr->token.z, pExpr->token.n,
2095 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2096 }
2097 pExpr->iAgg = i;
2098 return 1;
drh22827922000-06-06 17:27:05 +00002099 }
drh22827922000-06-06 17:27:05 +00002100 }
2101 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002102 if( pExpr->pSelect ){
2103 pNC->nDepth++;
2104 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2105 pNC->nDepth--;
2106 }
drh626a8792005-01-17 22:08:19 +00002107 return 0;
2108}
2109
2110/*
2111** Analyze the given expression looking for aggregate functions and
2112** for variables that need to be added to the pParse->aAgg[] array.
2113** Make additional entries to the pParse->aAgg[] array as necessary.
2114**
2115** This routine should only be called after the expression has been
2116** analyzed by sqlite3ExprResolveNames().
2117**
2118** If errors are seen, leave an error message in zErrMsg and return
2119** the number of errors.
2120*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002121int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2122 int nErr = pNC->pParse->nErr;
2123 walkExprTree(pExpr, analyzeAggregate, pNC);
2124 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002125}