blob: 6237e75d21698ee0f10c87893b29f75d4e7289bd [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**
drh15ccce12005-05-23 15:06:39 +000015** $Id: expr.c,v 1.199 2005/05/23 15:06:39 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh626a8792005-01-17 22:08:19 +000065** pExpr is an operand of a comparison operator. aff2 is the
66** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000067** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000186 /* When malloc fails, delete pLeft and pRight. Expressions passed to
187 ** this function must always be allocated with sqlite3Expr() for this
188 ** reason.
189 */
190 sqlite3ExprDelete(pLeft);
191 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000192 return 0;
193 }
194 pNew->op = op;
195 pNew->pLeft = pLeft;
196 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000197 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000198 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000199 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000200 pNew->span = pNew->token = *pToken;
201 }else if( pLeft && pRight ){
202 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000203 }
drha76b5df2002-02-23 02:32:10 +0000204 return pNew;
205}
206
207/*
drh4e0cff62004-11-05 05:10:28 +0000208** When doing a nested parse, you can include terms in an expression
209** that look like this: #0 #1 #2 ... These terms refer to elements
210** on the stack. "#0" (or just "#") means the top of the stack.
drh2958a4e2004-11-12 03:56:15 +0000211** "#1" means the next down on the stack. And so forth. #-1 means
212** memory location 0. #-2 means memory location 1. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000213**
214** This routine is called by the parser to deal with on of those terms.
215** It immediately generates code to store the value in a memory location.
216** The returns an expression that will code to extract the value from
217** that memory location as needed.
218*/
219Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
220 Vdbe *v = pParse->pVdbe;
221 Expr *p;
222 int depth;
223 if( v==0 ) return 0;
224 if( pParse->nested==0 ){
225 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
226 return 0;
227 }
228 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000229 if( p==0 ){
230 return 0; /* Malloc failed */
231 }
drh4e0cff62004-11-05 05:10:28 +0000232 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000233 if( depth>=0 ){
234 p->iTable = pParse->nMem++;
235 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
236 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
237 }else{
238 p->iTable = -1-depth;
239 }
drh4e0cff62004-11-05 05:10:28 +0000240 return p;
241}
242
243/*
drh91bb0ee2004-09-01 03:06:34 +0000244** Join two expressions using an AND operator. If either expression is
245** NULL, then just return the other expression.
246*/
247Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
248 if( pLeft==0 ){
249 return pRight;
250 }else if( pRight==0 ){
251 return pLeft;
252 }else{
253 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
254 }
255}
256
257/*
drh6977fea2002-10-22 23:38:04 +0000258** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000259** text between the two given tokens.
260*/
danielk19774adee202004-05-08 08:23:19 +0000261void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000262 assert( pRight!=0 );
263 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000264 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000265 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000266 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000267 pExpr->span.z = pLeft->z;
268 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000269 }else{
drh6977fea2002-10-22 23:38:04 +0000270 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000271 }
drha76b5df2002-02-23 02:32:10 +0000272 }
273}
274
275/*
276** Construct a new expression node for a function with multiple
277** arguments.
278*/
danielk19774adee202004-05-08 08:23:19 +0000279Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000280 Expr *pNew;
281 pNew = sqliteMalloc( sizeof(Expr) );
282 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000283 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000284 return 0;
285 }
286 pNew->op = TK_FUNCTION;
287 pNew->pList = pList;
288 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000289 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000290 pNew->token = *pToken;
291 }else{
292 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000293 }
drh6977fea2002-10-22 23:38:04 +0000294 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000295 return pNew;
296}
297
298/*
drhfa6bc002004-09-07 16:19:52 +0000299** Assign a variable number to an expression that encodes a wildcard
300** in the original SQL statement.
301**
302** Wildcards consisting of a single "?" are assigned the next sequential
303** variable number.
304**
305** Wildcards of the form "?nnn" are assigned the number "nnn". We make
306** sure "nnn" is not too be to avoid a denial of service attack when
307** the SQL statement comes from an external source.
308**
309** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
310** as the previous instance of the same wildcard. Or if this is the first
311** instance of the wildcard, the next sequenial variable number is
312** assigned.
313*/
314void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
315 Token *pToken;
316 if( pExpr==0 ) return;
317 pToken = &pExpr->token;
318 assert( pToken->n>=1 );
319 assert( pToken->z!=0 );
320 assert( pToken->z[0]!=0 );
321 if( pToken->n==1 ){
322 /* Wildcard of the form "?". Assign the next variable number */
323 pExpr->iTable = ++pParse->nVar;
324 }else if( pToken->z[0]=='?' ){
325 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
326 ** use it as the variable number */
327 int i;
328 pExpr->iTable = i = atoi(&pToken->z[1]);
329 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
330 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
331 SQLITE_MAX_VARIABLE_NUMBER);
332 }
333 if( i>pParse->nVar ){
334 pParse->nVar = i;
335 }
336 }else{
337 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
338 ** number as the prior appearance of the same name, or if the name
339 ** has never appeared before, reuse the same variable number
340 */
341 int i, n;
342 n = pToken->n;
343 for(i=0; i<pParse->nVarExpr; i++){
344 Expr *pE;
345 if( (pE = pParse->apVarExpr[i])!=0
346 && pE->token.n==n
347 && memcmp(pE->token.z, pToken->z, n)==0 ){
348 pExpr->iTable = pE->iTable;
349 break;
350 }
351 }
352 if( i>=pParse->nVarExpr ){
353 pExpr->iTable = ++pParse->nVar;
354 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
355 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
356 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
357 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
358 }
359 if( !sqlite3_malloc_failed ){
360 assert( pParse->apVarExpr!=0 );
361 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
362 }
363 }
364 }
365}
366
367/*
drha2e00042002-01-22 03:13:42 +0000368** Recursively delete an expression tree.
369*/
danielk19774adee202004-05-08 08:23:19 +0000370void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000371 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000372 if( p->span.dyn ) sqliteFree((char*)p->span.z);
373 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3ExprDelete(p->pLeft);
375 sqlite3ExprDelete(p->pRight);
376 sqlite3ExprListDelete(p->pList);
377 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000378 sqliteFree(p);
379}
380
drha76b5df2002-02-23 02:32:10 +0000381
382/*
drhff78bd22002-02-27 01:47:11 +0000383** The following group of routines make deep copies of expressions,
384** expression lists, ID lists, and select statements. The copies can
385** be deleted (by being passed to their respective ...Delete() routines)
386** without effecting the originals.
387**
danielk19774adee202004-05-08 08:23:19 +0000388** The expression list, ID, and source lists return by sqlite3ExprListDup(),
389** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000390** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000391**
drhad3cab52002-05-24 02:04:32 +0000392** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000393*/
danielk19774adee202004-05-08 08:23:19 +0000394Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000395 Expr *pNew;
396 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000397 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000398 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000399 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000400 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000401 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000402 pNew->token.dyn = 1;
403 }else{
drh4efc4752004-01-16 15:55:37 +0000404 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000405 }
drh6977fea2002-10-22 23:38:04 +0000406 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000407 pNew->pLeft = sqlite3ExprDup(p->pLeft);
408 pNew->pRight = sqlite3ExprDup(p->pRight);
409 pNew->pList = sqlite3ExprListDup(p->pList);
410 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000411 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000412 return pNew;
413}
danielk19774adee202004-05-08 08:23:19 +0000414void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000415 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000416 if( pFrom->z ){
417 pTo->n = pFrom->n;
418 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
419 pTo->dyn = 1;
420 }else{
drh4b59ab52002-08-24 18:24:51 +0000421 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000422 }
423}
danielk19774adee202004-05-08 08:23:19 +0000424ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000425 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000426 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000427 int i;
428 if( p==0 ) return 0;
429 pNew = sqliteMalloc( sizeof(*pNew) );
430 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000431 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000432 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000433 if( pItem==0 ){
434 sqliteFree(pNew);
435 return 0;
436 }
drh145716b2004-09-24 12:24:06 +0000437 pOldItem = p->a;
438 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000439 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000440 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000441 if( pOldExpr->span.z!=0 && pNewExpr ){
442 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000443 ** expression list. The logic in SELECT processing that determines
444 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000446 }
drh1f3e9052002-10-31 00:09:39 +0000447 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000448 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000449 pItem->zName = sqliteStrDup(pOldItem->zName);
450 pItem->sortOrder = pOldItem->sortOrder;
451 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000452 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000453 }
454 return pNew;
455}
danielk197793758c82005-01-21 08:13:14 +0000456
457/*
458** If cursors, triggers, views and subqueries are all omitted from
459** the build, then none of the following routines, except for
460** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
461** called with a NULL argument.
462*/
danielk19776a67fe82005-02-04 04:07:16 +0000463#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
464 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000465SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000466 SrcList *pNew;
467 int i;
drh113088e2003-03-20 01:16:58 +0000468 int nByte;
drhad3cab52002-05-24 02:04:32 +0000469 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000470 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000471 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000472 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000473 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000474 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000475 struct SrcList_item *pNewItem = &pNew->a[i];
476 struct SrcList_item *pOldItem = &p->a[i];
477 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
478 pNewItem->zName = sqliteStrDup(pOldItem->zName);
479 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
480 pNewItem->jointype = pOldItem->jointype;
481 pNewItem->iCursor = pOldItem->iCursor;
danielk1977a1cb1832005-02-12 08:59:55 +0000482 pNewItem->pTab = pOldItem->pTab;
483 if( pNewItem->pTab ){
484 pNewItem->pTab->isTransient = 0;
485 }
danielk19774adee202004-05-08 08:23:19 +0000486 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
487 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
488 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000489 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000490 }
491 return pNew;
492}
danielk19774adee202004-05-08 08:23:19 +0000493IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000494 IdList *pNew;
495 int i;
496 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000497 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000498 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000499 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000500 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000501 if( pNew->a==0 ){
502 sqliteFree(pNew);
503 return 0;
504 }
drhff78bd22002-02-27 01:47:11 +0000505 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000506 struct IdList_item *pNewItem = &pNew->a[i];
507 struct IdList_item *pOldItem = &p->a[i];
508 pNewItem->zName = sqliteStrDup(pOldItem->zName);
509 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000510 }
511 return pNew;
512}
danielk19774adee202004-05-08 08:23:19 +0000513Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000514 Select *pNew;
515 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000516 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000517 if( pNew==0 ) return 0;
518 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000519 pNew->pEList = sqlite3ExprListDup(p->pEList);
520 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
521 pNew->pWhere = sqlite3ExprDup(p->pWhere);
522 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
523 pNew->pHaving = sqlite3ExprDup(p->pHaving);
524 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000525 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000526 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000527 pNew->pLimit = sqlite3ExprDup(p->pLimit);
528 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000529 pNew->iLimit = -1;
530 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000531 pNew->ppOpenTemp = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000532 pNew->isResolved = p->isResolved;
533 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000534 return pNew;
535}
danielk197793758c82005-01-21 08:13:14 +0000536#else
537Select *sqlite3SelectDup(Select *p){
538 assert( p==0 );
539 return 0;
540}
541#endif
drhff78bd22002-02-27 01:47:11 +0000542
543
544/*
drha76b5df2002-02-23 02:32:10 +0000545** Add a new element to the end of an expression list. If pList is
546** initially NULL, then create a new expression list.
547*/
danielk19774adee202004-05-08 08:23:19 +0000548ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000549 if( pList==0 ){
550 pList = sqliteMalloc( sizeof(ExprList) );
551 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000552 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000553 }
drh4efc4752004-01-16 15:55:37 +0000554 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000555 }
drh4305d102003-07-30 12:34:12 +0000556 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000557 struct ExprList_item *a;
558 int n = pList->nAlloc*2 + 4;
559 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
560 if( a==0 ){
561 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000562 }
danielk1977d5d56522005-03-16 12:15:20 +0000563 pList->a = a;
564 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000565 }
drh4efc4752004-01-16 15:55:37 +0000566 assert( pList->a!=0 );
567 if( pExpr || pName ){
568 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
569 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000570 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000571 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000572 }
573 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000574
575no_mem:
576 /* Avoid leaking memory if malloc has failed. */
577 sqlite3ExprDelete(pExpr);
578 sqlite3ExprListDelete(pList);
579 return 0;
drha76b5df2002-02-23 02:32:10 +0000580}
581
582/*
583** Delete an entire expression list.
584*/
danielk19774adee202004-05-08 08:23:19 +0000585void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000586 int i;
drhbe5c89a2004-07-26 00:31:09 +0000587 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000588 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000589 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
590 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000591 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
592 sqlite3ExprDelete(pItem->pExpr);
593 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000594 }
595 sqliteFree(pList->a);
596 sqliteFree(pList);
597}
598
599/*
drh626a8792005-01-17 22:08:19 +0000600** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000601**
drh626a8792005-01-17 22:08:19 +0000602** The return value from xFunc determines whether the tree walk continues.
603** 0 means continue walking the tree. 1 means do not walk children
604** of the current node but continue with siblings. 2 means abandon
605** the tree walk completely.
606**
607** The return value from this routine is 1 to abandon the tree walk
608** and 0 to continue.
609*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000610static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000611static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000612 int rc;
613 if( pExpr==0 ) return 0;
614 rc = (*xFunc)(pArg, pExpr);
615 if( rc==0 ){
616 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
617 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000618 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000619 }
620 return rc>1;
621}
622
623/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000624** Call walkExprTree() for every expression in list p.
625*/
626static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
627 int i;
628 struct ExprList_item *pItem;
629 if( !p ) return 0;
630 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
631 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
632 }
633 return 0;
634}
635
636/*
637** Call walkExprTree() for every expression in Select p, not including
638** expressions that are part of sub-selects in any FROM clause or the LIMIT
639** or OFFSET expressions..
640*/
641static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
642 walkExprList(p->pEList, xFunc, pArg);
643 walkExprTree(p->pWhere, xFunc, pArg);
644 walkExprList(p->pGroupBy, xFunc, pArg);
645 walkExprTree(p->pHaving, xFunc, pArg);
646 walkExprList(p->pOrderBy, xFunc, pArg);
647 return 0;
648}
649
650
651/*
drh626a8792005-01-17 22:08:19 +0000652** This routine is designed as an xFunc for walkExprTree().
653**
654** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000655** at pExpr that the expression that contains pExpr is not a constant
656** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
657** If pExpr does does not disqualify the expression from being a constant
658** then do nothing.
659**
660** After walking the whole tree, if no nodes are found that disqualify
661** the expression as constant, then we assume the whole expression
662** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000663*/
664static int exprNodeIsConstant(void *pArg, Expr *pExpr){
665 switch( pExpr->op ){
666 case TK_ID:
667 case TK_COLUMN:
668 case TK_DOT:
669 case TK_AGG_FUNCTION:
670 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000671#ifndef SQLITE_OMIT_SUBQUERY
672 case TK_SELECT:
673 case TK_EXISTS:
674#endif
drh626a8792005-01-17 22:08:19 +0000675 *((int*)pArg) = 0;
676 return 2;
677 default:
678 return 0;
679 }
680}
681
682/*
drhfef52082000-06-06 01:50:43 +0000683** Walk an expression tree. Return 1 if the expression is constant
684** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000685**
686** For the purposes of this function, a double-quoted string (ex: "abc")
687** is considered a variable but a single-quoted string (ex: 'abc') is
688** a constant.
drhfef52082000-06-06 01:50:43 +0000689*/
danielk19774adee202004-05-08 08:23:19 +0000690int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000691 int isConst = 1;
692 walkExprTree(p, exprNodeIsConstant, &isConst);
693 return isConst;
drhfef52082000-06-06 01:50:43 +0000694}
695
696/*
drh73b211a2005-01-18 04:00:42 +0000697** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000698** to fit in a 32-bit integer, return 1 and put the value of the integer
699** in *pValue. If the expression is not an integer or if it is too big
700** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000701*/
danielk19774adee202004-05-08 08:23:19 +0000702int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000703 switch( p->op ){
704 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000705 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000706 return 1;
707 }
708 break;
drhe4de1fe2002-06-02 16:09:01 +0000709 }
drh4b59ab52002-08-24 18:24:51 +0000710 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000711 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000712 }
drhe4de1fe2002-06-02 16:09:01 +0000713 case TK_UMINUS: {
714 int v;
danielk19774adee202004-05-08 08:23:19 +0000715 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000716 *pValue = -v;
717 return 1;
718 }
719 break;
720 }
721 default: break;
722 }
723 return 0;
724}
725
726/*
drhc4a3c772001-04-04 11:48:57 +0000727** Return TRUE if the given string is a row-id column name.
728*/
danielk19774adee202004-05-08 08:23:19 +0000729int sqlite3IsRowid(const char *z){
730 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
731 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
732 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000733 return 0;
734}
735
736/*
drh8141f612004-01-25 22:44:58 +0000737** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
738** that name in the set of source tables in pSrcList and make the pExpr
739** expression node refer back to that source column. The following changes
740** are made to pExpr:
741**
742** pExpr->iDb Set the index in db->aDb[] of the database holding
743** the table.
744** pExpr->iTable Set to the cursor number for the table obtained
745** from pSrcList.
746** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000747** pExpr->op Set to TK_COLUMN.
748** pExpr->pLeft Any expression this points to is deleted
749** pExpr->pRight Any expression this points to is deleted.
750**
751** The pDbToken is the name of the database (the "X"). This value may be
752** NULL meaning that name is of the form Y.Z or Z. Any available database
753** can be used. The pTableToken is the name of the table (the "Y"). This
754** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
755** means that the form of the name is Z and that columns from any table
756** can be used.
757**
758** If the name cannot be resolved unambiguously, leave an error message
759** in pParse and return non-zero. Return zero on success.
760*/
761static int lookupName(
762 Parse *pParse, /* The parsing context */
763 Token *pDbToken, /* Name of the database containing table, or NULL */
764 Token *pTableToken, /* Name of table containing column, or NULL */
765 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000766 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000767 Expr *pExpr /* Make this EXPR node point to the selected column */
768){
769 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
770 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
771 char *zCol = 0; /* Name of the column. The "Z" */
772 int i, j; /* Loop counters */
773 int cnt = 0; /* Number of matching column names */
774 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000775 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000776 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
777 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000778 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000779
780 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000781 zDb = sqlite3NameFromToken(pDbToken);
782 zTab = sqlite3NameFromToken(pTableToken);
783 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000784 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000785 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000786 }
drh8141f612004-01-25 22:44:58 +0000787
788 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000789 while( pNC && cnt==0 ){
790 SrcList *pSrcList = pNC->pSrcList;
791 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000792
drh626a8792005-01-17 22:08:19 +0000793 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000794 if( pSrcList ){
795 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
796 Table *pTab = pItem->pTab;
797 Column *pCol;
798
799 if( pTab==0 ) continue;
800 assert( pTab->nCol>0 );
801 if( zTab ){
802 if( pItem->zAlias ){
803 char *zTabName = pItem->zAlias;
804 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
805 }else{
806 char *zTabName = pTab->zName;
807 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
808 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
809 continue;
810 }
drh626a8792005-01-17 22:08:19 +0000811 }
drh8141f612004-01-25 22:44:58 +0000812 }
danielk1977b3bce662005-01-29 08:32:43 +0000813 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000814 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000815 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000816 pMatch = pItem;
817 }
818 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
819 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
820 cnt++;
821 pExpr->iTable = pItem->iCursor;
822 pMatch = pItem;
823 pExpr->iDb = pTab->iDb;
824 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
825 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
826 pExpr->affinity = pTab->aCol[j].affinity;
827 pExpr->pColl = pTab->aCol[j].pColl;
828 break;
829 }
drh8141f612004-01-25 22:44:58 +0000830 }
831 }
832 }
drh626a8792005-01-17 22:08:19 +0000833
834#ifndef SQLITE_OMIT_TRIGGER
835 /* If we have not already resolved the name, then maybe
836 ** it is a new.* or old.* trigger argument reference
837 */
838 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
839 TriggerStack *pTriggerStack = pParse->trigStack;
840 Table *pTab = 0;
841 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
842 pExpr->iTable = pTriggerStack->newIdx;
843 assert( pTriggerStack->pTab );
844 pTab = pTriggerStack->pTab;
845 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
846 pExpr->iTable = pTriggerStack->oldIdx;
847 assert( pTriggerStack->pTab );
848 pTab = pTriggerStack->pTab;
849 }
850
851 if( pTab ){
852 int j;
853 Column *pCol = pTab->aCol;
854
855 pExpr->iDb = pTab->iDb;
856 cntTab++;
857 for(j=0; j < pTab->nCol; j++, pCol++) {
858 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
859 cnt++;
860 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
861 pExpr->affinity = pTab->aCol[j].affinity;
862 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000863 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000864 break;
865 }
866 }
867 }
868 }
drhb7f91642004-10-31 02:22:47 +0000869#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000870
drh626a8792005-01-17 22:08:19 +0000871 /*
872 ** Perhaps the name is a reference to the ROWID
873 */
874 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
875 cnt = 1;
876 pExpr->iColumn = -1;
877 pExpr->affinity = SQLITE_AFF_INTEGER;
878 }
drh8141f612004-01-25 22:44:58 +0000879
drh626a8792005-01-17 22:08:19 +0000880 /*
881 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
882 ** might refer to an result-set alias. This happens, for example, when
883 ** we are resolving names in the WHERE clause of the following command:
884 **
885 ** SELECT a+b AS x FROM table WHERE x<10;
886 **
887 ** In cases like this, replace pExpr with a copy of the expression that
888 ** forms the result set entry ("a+b" in the example) and return immediately.
889 ** Note that the expression in the result set should have already been
890 ** resolved by the time the WHERE clause is resolved.
891 */
drh79d5f632005-01-18 17:20:10 +0000892 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000893 for(j=0; j<pEList->nExpr; j++){
894 char *zAs = pEList->a[j].zName;
895 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
896 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
897 pExpr->op = TK_AS;
898 pExpr->iColumn = j;
899 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000900 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000901 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000902 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000903 }
904 }
905 }
906
907 /* Advance to the next name context. The loop will exit when either
908 ** we have a match (cnt>0) or when we run out of name contexts.
909 */
910 if( cnt==0 ){
911 pNC = pNC->pNext;
912 }
drh8141f612004-01-25 22:44:58 +0000913 }
914
915 /*
916 ** If X and Y are NULL (in other words if only the column name Z is
917 ** supplied) and the value of Z is enclosed in double-quotes, then
918 ** Z is a string literal if it doesn't match any column names. In that
919 ** case, we need to return right away and not make any changes to
920 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000921 **
922 ** Because no reference was made to outer contexts, the pNC->nRef
923 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000924 */
925 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
926 sqliteFree(zCol);
927 return 0;
928 }
929
930 /*
931 ** cnt==0 means there was not match. cnt>1 means there were two or
932 ** more matches. Either way, we have an error.
933 */
934 if( cnt!=1 ){
935 char *z = 0;
936 char *zErr;
937 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
938 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000939 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000940 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000941 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000942 }else{
943 z = sqliteStrDup(zCol);
944 }
danielk19774adee202004-05-08 08:23:19 +0000945 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000946 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000947 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000948 }
949
drh51669862004-12-18 18:40:26 +0000950 /* If a column from a table in pSrcList is referenced, then record
951 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
952 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
953 ** column number is greater than the number of bits in the bitmask
954 ** then set the high-order bit of the bitmask.
955 */
956 if( pExpr->iColumn>=0 && pMatch!=0 ){
957 int n = pExpr->iColumn;
958 if( n>=sizeof(Bitmask)*8 ){
959 n = sizeof(Bitmask)*8-1;
960 }
961 assert( pMatch->iCursor==pExpr->iTable );
962 pMatch->colUsed |= 1<<n;
963 }
964
danielk1977d5d56522005-03-16 12:15:20 +0000965lookupname_end:
drh8141f612004-01-25 22:44:58 +0000966 /* Clean up and return
967 */
968 sqliteFree(zDb);
969 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +0000970 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000971 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000972 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000973 pExpr->pRight = 0;
974 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +0000975lookupname_end_2:
976 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +0000977 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000978 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000979 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +0000980 if( pMatch && !pMatch->pSelect ){
981 pExpr->pTab = pMatch->pTab;
982 }
drh15ccce12005-05-23 15:06:39 +0000983 /* Increment the nRef value on all name contexts from TopNC up to
984 ** the point where the name matched. */
985 for(;;){
986 assert( pTopNC!=0 );
987 pTopNC->nRef++;
988 if( pTopNC==pNC ) break;
989 pTopNC = pTopNC->pNext;
990 }
991 return 0;
992 } else {
993 return 1;
drh626a8792005-01-17 22:08:19 +0000994 }
drh8141f612004-01-25 22:44:58 +0000995}
996
997/*
drh626a8792005-01-17 22:08:19 +0000998** pExpr is a node that defines a function of some kind. It might
999** be a syntactic function like "count(x)" or it might be a function
1000** that implements an operator, like "a LIKE b".
1001**
1002** This routine makes *pzName point to the name of the function and
1003** *pnName hold the number of characters in the function name.
1004*/
1005static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1006 switch( pExpr->op ){
1007 case TK_FUNCTION: {
1008 *pzName = pExpr->token.z;
1009 *pnName = pExpr->token.n;
1010 break;
1011 }
1012 case TK_LIKE: {
1013 *pzName = "like";
1014 *pnName = 4;
1015 break;
1016 }
1017 case TK_GLOB: {
1018 *pzName = "glob";
1019 *pnName = 4;
1020 break;
1021 }
1022 case TK_CTIME: {
1023 *pzName = "current_time";
1024 *pnName = 12;
1025 break;
1026 }
1027 case TK_CDATE: {
1028 *pzName = "current_date";
1029 *pnName = 12;
1030 break;
1031 }
1032 case TK_CTIMESTAMP: {
1033 *pzName = "current_timestamp";
1034 *pnName = 17;
1035 break;
1036 }
drh626a8792005-01-17 22:08:19 +00001037 }
1038}
1039
1040/*
1041** 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 */
1114 case TK_CTIME:
1115 case TK_CTIMESTAMP:
1116 case TK_CDATE:
drh626a8792005-01-17 22:08:19 +00001117 case TK_GLOB:
1118 case TK_LIKE:
1119 case TK_FUNCTION: {
1120 ExprList *pList = pExpr->pList; /* The argument list */
1121 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1122 int no_such_func = 0; /* True if no such function exists */
1123 int wrong_num_args = 0; /* True if wrong number of arguments */
1124 int is_agg = 0; /* True if is an aggregate function */
1125 int i;
1126 int nId; /* Number of characters in function name */
1127 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001128 FuncDef *pDef; /* Information about the function */
1129 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001130
1131 getFunctionName(pExpr, &zId, &nId);
1132 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1133 if( pDef==0 ){
1134 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1135 if( pDef==0 ){
1136 no_such_func = 1;
1137 }else{
1138 wrong_num_args = 1;
1139 }
1140 }else{
1141 is_agg = pDef->xFunc==0;
1142 }
1143 if( is_agg && !pNC->allowAgg ){
1144 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1145 pNC->nErr++;
1146 is_agg = 0;
1147 }else if( no_such_func ){
1148 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1149 pNC->nErr++;
1150 }else if( wrong_num_args ){
1151 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1152 nId, zId);
1153 pNC->nErr++;
1154 }
1155 if( is_agg ){
1156 pExpr->op = TK_AGG_FUNCTION;
1157 pNC->hasAgg = 1;
1158 }
drh73b211a2005-01-18 04:00:42 +00001159 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001160 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001161 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001162 }
drh73b211a2005-01-18 04:00:42 +00001163 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001164 /* FIX ME: Compute pExpr->affinity based on the expected return
1165 ** type of the function
1166 */
1167 return is_agg;
1168 }
danielk1977b3bce662005-01-29 08:32:43 +00001169#ifndef SQLITE_OMIT_SUBQUERY
1170 case TK_SELECT:
1171 case TK_EXISTS:
1172#endif
1173 case TK_IN: {
1174 if( pExpr->pSelect ){
1175 int nRef = pNC->nRef;
1176 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1177 assert( pNC->nRef>=nRef );
1178 if( nRef!=pNC->nRef ){
1179 ExprSetProperty(pExpr, EP_VarSelect);
1180 }
1181 }
1182 }
drh626a8792005-01-17 22:08:19 +00001183 }
1184 return 0;
1185}
1186
1187/*
drhcce7d172000-05-31 15:34:51 +00001188** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001189** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001190** index to the table in the table list and a column offset. The
1191** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1192** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001193** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001194** VDBE cursor number for a cursor that is pointing into the referenced
1195** table. The Expr.iColumn value is changed to the index of the column
1196** of the referenced table. The Expr.iColumn value for the special
1197** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1198** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001199**
drh626a8792005-01-17 22:08:19 +00001200** Also resolve function names and check the functions for proper
1201** usage. Make sure all function names are recognized and all functions
1202** have the correct number of arguments. Leave an error message
1203** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1204**
drh73b211a2005-01-18 04:00:42 +00001205** If the expression contains aggregate functions then set the EP_Agg
1206** property on the expression.
drh626a8792005-01-17 22:08:19 +00001207*/
1208int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001209 NameContext *pNC, /* Namespace to resolve expressions in. */
1210 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001211){
drh73b211a2005-01-18 04:00:42 +00001212 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001213 walkExprTree(pExpr, nameResolverStep, pNC);
1214 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001215 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001216 }
1217 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001218}
1219
drh1398ad32005-01-19 23:24:50 +00001220/*
1221** A pointer instance of this structure is used to pass information
1222** through walkExprTree into codeSubqueryStep().
1223*/
1224typedef struct QueryCoder QueryCoder;
1225struct QueryCoder {
1226 Parse *pParse; /* The parsing context */
1227 NameContext *pNC; /* Namespace of first enclosing query */
1228};
1229
drh626a8792005-01-17 22:08:19 +00001230
1231/*
1232** Generate code for subqueries and IN operators.
1233**
drh73b211a2005-01-18 04:00:42 +00001234** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001235**
1236** expr IN (exprlist)
1237** and
1238** expr IN (SELECT ...)
1239**
1240** The first form is handled by creating a set holding the list
1241** of allowed values. The second form causes the SELECT to generate
1242** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001243*/
drh51522cd2005-01-20 13:36:19 +00001244#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001245void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1246 int label = 0; /* Address after sub-select code */
1247 Vdbe *v = sqlite3GetVdbe(pParse);
1248 if( v==0 ) return;
1249
1250 /* If this is not a variable (correlated) select, then execute
1251 ** it only once. Unless this is part of a trigger program. In
1252 ** that case re-execute every time (this could be optimized).
1253 */
1254 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1255 int mem = pParse->nMem++;
1256 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1257 label = sqlite3VdbeMakeLabel(v);
1258 sqlite3VdbeAddOp(v, OP_If, 0, label);
1259 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1260 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1261 }
1262
1263 if( pExpr->pSelect ){
1264 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1265 }
drh6a3ea0e2003-05-02 14:32:12 +00001266
drhcce7d172000-05-31 15:34:51 +00001267 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001268 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001269 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001270 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001271 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001272
danielk1977bf3b7212004-05-18 10:06:24 +00001273 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001274
1275 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1276 ** expression it is handled the same way. A temporary table is
1277 ** filled with single-field index keys representing the results
1278 ** from the SELECT or the <exprlist>.
1279 **
1280 ** If the 'x' expression is a column value, or the SELECT...
1281 ** statement returns a column value, then the affinity of that
1282 ** column is used to build the index keys. If both 'x' and the
1283 ** SELECT... statement are columns, then numeric affinity is used
1284 ** if either column has NUMERIC or INTEGER affinity. If neither
1285 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1286 ** is used.
1287 */
1288 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001289 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001290 memset(&keyInfo, 0, sizeof(keyInfo));
1291 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001292 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001293
drhfef52082000-06-06 01:50:43 +00001294 if( pExpr->pSelect ){
1295 /* Case 1: expr IN (SELECT ...)
1296 **
danielk1977e014a832004-05-17 10:48:57 +00001297 ** Generate code to write the results of the select into the temporary
1298 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001299 */
danielk1977e014a832004-05-17 10:48:57 +00001300 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001301 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001302 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001303 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001304 pEList = pExpr->pSelect->pEList;
1305 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001306 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001307 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001308 }
drhfef52082000-06-06 01:50:43 +00001309 }else if( pExpr->pList ){
1310 /* Case 2: expr IN (exprlist)
1311 **
danielk1977e014a832004-05-17 10:48:57 +00001312 ** For each expression, build an index key from the evaluation and
1313 ** store it in the temporary table. If <expr> is a column, then use
1314 ** that columns affinity when building index keys. If <expr> is not
1315 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001316 */
danielk1977e014a832004-05-17 10:48:57 +00001317 int i;
danielk1977e014a832004-05-17 10:48:57 +00001318 if( !affinity ){
1319 affinity = SQLITE_AFF_NUMERIC;
1320 }
danielk19770202b292004-06-09 09:55:16 +00001321 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001322
1323 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001324 for(i=0; i<pExpr->pList->nExpr; i++){
1325 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001326
1327 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001328 if( !sqlite3ExprIsConstant(pE2) ){
1329 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001330 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001331 return;
drh4794b982000-06-06 13:54:14 +00001332 }
danielk1977e014a832004-05-17 10:48:57 +00001333
1334 /* Evaluate the expression and insert it into the temp table */
1335 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001336 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001337 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001338 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001339 }
1340 }
danielk19770202b292004-06-09 09:55:16 +00001341 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001342 break;
drhfef52082000-06-06 01:50:43 +00001343 }
1344
drh51522cd2005-01-20 13:36:19 +00001345 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001346 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001347 /* This has to be a scalar SELECT. Generate code to put the
1348 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001349 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001350 */
drh51522cd2005-01-20 13:36:19 +00001351 int sop;
1352 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001353
drh967e8b72000-06-21 13:59:10 +00001354 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001355 pSel = pExpr->pSelect;
1356 if( pExpr->op==TK_SELECT ){
1357 sop = SRT_Mem;
1358 }else{
1359 static const Token one = { "1", 0, 1 };
1360 sop = SRT_Exists;
1361 sqlite3ExprListDelete(pSel->pEList);
1362 pSel->pEList = sqlite3ExprListAppend(0,
1363 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1364 }
danielk1977b3bce662005-01-29 08:32:43 +00001365 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1366 break;
drhcce7d172000-05-31 15:34:51 +00001367 }
1368 }
danielk1977b3bce662005-01-29 08:32:43 +00001369
1370 if( pExpr->pSelect ){
1371 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1372 }
1373 if( label<0 ){
1374 sqlite3VdbeResolveLabel(v, label);
1375 }
1376 return;
drhcce7d172000-05-31 15:34:51 +00001377}
drh51522cd2005-01-20 13:36:19 +00001378#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001379
drhcce7d172000-05-31 15:34:51 +00001380/*
drhfec19aa2004-05-19 20:41:03 +00001381** Generate an instruction that will put the integer describe by
1382** text z[0..n-1] on the stack.
1383*/
1384static void codeInteger(Vdbe *v, const char *z, int n){
1385 int i;
drh6fec0762004-05-30 01:38:43 +00001386 if( sqlite3GetInt32(z, &i) ){
1387 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1388 }else if( sqlite3FitsIn64Bits(z) ){
1389 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001390 }else{
1391 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1392 }
1393}
1394
1395/*
drhcce7d172000-05-31 15:34:51 +00001396** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001397** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001398**
1399** This code depends on the fact that certain token values (ex: TK_EQ)
1400** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1401** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1402** the make process cause these values to align. Assert()s in the code
1403** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001404*/
danielk19774adee202004-05-08 08:23:19 +00001405void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001406 Vdbe *v = pParse->pVdbe;
1407 int op;
danielk19777977a172004-11-09 12:44:37 +00001408 if( v==0 ) return;
1409 if( pExpr==0 ){
1410 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1411 return;
1412 }
drhf2bc0132004-10-04 13:19:23 +00001413 op = pExpr->op;
1414 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001415 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001416 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1417 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001418 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001419 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001420 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001421 }else{
danielk19774adee202004-05-08 08:23:19 +00001422 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001423 }
drhcce7d172000-05-31 15:34:51 +00001424 break;
1425 }
1426 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001427 codeInteger(v, pExpr->token.z, pExpr->token.n);
1428 break;
1429 }
1430 case TK_FLOAT:
1431 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001432 assert( TK_FLOAT==OP_Real );
1433 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001434 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001435 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001436 break;
1437 }
danielk19775338a5f2005-01-20 13:03:10 +00001438#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001439 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001440 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001441 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1442 sqlite3VdbeDequoteP3(v, -1);
1443 break;
1444 }
danielk19775338a5f2005-01-20 13:03:10 +00001445#endif
drhcce7d172000-05-31 15:34:51 +00001446 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001447 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001448 break;
1449 }
drh50457892003-09-06 01:10:47 +00001450 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001451 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001452 if( pExpr->token.n>1 ){
1453 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1454 }
drh50457892003-09-06 01:10:47 +00001455 break;
1456 }
drh4e0cff62004-11-05 05:10:28 +00001457 case TK_REGISTER: {
1458 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1459 break;
1460 }
drhc9b84a12002-06-20 11:36:48 +00001461 case TK_LT:
1462 case TK_LE:
1463 case TK_GT:
1464 case TK_GE:
1465 case TK_NE:
1466 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001467 assert( TK_LT==OP_Lt );
1468 assert( TK_LE==OP_Le );
1469 assert( TK_GT==OP_Gt );
1470 assert( TK_GE==OP_Ge );
1471 assert( TK_EQ==OP_Eq );
1472 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001473 sqlite3ExprCode(pParse, pExpr->pLeft);
1474 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001475 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001476 break;
drhc9b84a12002-06-20 11:36:48 +00001477 }
drhcce7d172000-05-31 15:34:51 +00001478 case TK_AND:
1479 case TK_OR:
1480 case TK_PLUS:
1481 case TK_STAR:
1482 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001483 case TK_REM:
1484 case TK_BITAND:
1485 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001486 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001487 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001488 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001489 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001490 assert( TK_AND==OP_And );
1491 assert( TK_OR==OP_Or );
1492 assert( TK_PLUS==OP_Add );
1493 assert( TK_MINUS==OP_Subtract );
1494 assert( TK_REM==OP_Remainder );
1495 assert( TK_BITAND==OP_BitAnd );
1496 assert( TK_BITOR==OP_BitOr );
1497 assert( TK_SLASH==OP_Divide );
1498 assert( TK_LSHIFT==OP_ShiftLeft );
1499 assert( TK_RSHIFT==OP_ShiftRight );
1500 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001501 sqlite3ExprCode(pParse, pExpr->pLeft);
1502 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001503 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001504 break;
1505 }
drhcce7d172000-05-31 15:34:51 +00001506 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001507 Expr *pLeft = pExpr->pLeft;
1508 assert( pLeft );
1509 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1510 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001511 char *z = sqliteMalloc( p->n + 2 );
1512 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001513 if( pLeft->op==TK_FLOAT ){
1514 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001515 }else{
drhfec19aa2004-05-19 20:41:03 +00001516 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001517 }
drh6e142f52000-06-08 13:36:40 +00001518 sqliteFree(z);
1519 break;
1520 }
drh1ccde152000-06-17 13:12:39 +00001521 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001522 }
drhbf4133c2001-10-13 02:59:08 +00001523 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001524 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001525 assert( TK_BITNOT==OP_BitNot );
1526 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001527 sqlite3ExprCode(pParse, pExpr->pLeft);
1528 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001529 break;
1530 }
1531 case TK_ISNULL:
1532 case TK_NOTNULL: {
1533 int dest;
drhf2bc0132004-10-04 13:19:23 +00001534 assert( TK_ISNULL==OP_IsNull );
1535 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001536 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1537 sqlite3ExprCode(pParse, pExpr->pLeft);
1538 dest = sqlite3VdbeCurrentAddr(v) + 2;
1539 sqlite3VdbeAddOp(v, op, 1, dest);
1540 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001541 break;
drhcce7d172000-05-31 15:34:51 +00001542 }
drh22827922000-06-06 17:27:05 +00001543 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001544 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001545 break;
1546 }
danielk19777977a172004-11-09 12:44:37 +00001547 case TK_CDATE:
1548 case TK_CTIME:
1549 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001550 case TK_GLOB:
1551 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001552 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001553 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001554 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001555 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001556 int nId;
1557 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001558 int p2 = 0;
1559 int i;
danielk1977d8123362004-06-12 09:25:12 +00001560 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001561 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001562 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001563 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001564 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001565 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001566 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001567 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1568 p2 |= (1<<i);
1569 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001570 if( pDef->needCollSeq && !pColl ){
1571 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1572 }
1573 }
1574 if( pDef->needCollSeq ){
1575 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001576 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001577 }
1578 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001579 break;
1580 }
drhfe2093d2005-01-20 22:48:47 +00001581#ifndef SQLITE_OMIT_SUBQUERY
1582 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001583 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001584 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001585 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001586 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001587 break;
1588 }
drhfef52082000-06-06 01:50:43 +00001589 case TK_IN: {
1590 int addr;
drh94a11212004-09-25 13:12:14 +00001591 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001592 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001593
1594 /* Figure out the affinity to use to create a key from the results
1595 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001596 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001597 */
drh94a11212004-09-25 13:12:14 +00001598 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001599
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001601
1602 /* Code the <expr> from "<expr> IN (...)". The temporary table
1603 ** pExpr->iTable contains the values that make up the (...) set.
1604 */
danielk19774adee202004-05-08 08:23:19 +00001605 sqlite3ExprCode(pParse, pExpr->pLeft);
1606 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001607 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001608 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001609 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001610 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001611 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001612 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1613 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1614
drhfef52082000-06-06 01:50:43 +00001615 break;
1616 }
danielk197793758c82005-01-21 08:13:14 +00001617#endif
drhfef52082000-06-06 01:50:43 +00001618 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001619 Expr *pLeft = pExpr->pLeft;
1620 struct ExprList_item *pLItem = pExpr->pList->a;
1621 Expr *pRight = pLItem->pExpr;
1622 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001623 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001624 sqlite3ExprCode(pParse, pRight);
1625 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001626 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001627 pLItem++;
1628 pRight = pLItem->pExpr;
1629 sqlite3ExprCode(pParse, pRight);
1630 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001631 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001632 break;
1633 }
drh51e9a442004-01-16 16:42:53 +00001634 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001635 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001636 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001637 break;
1638 }
drh17a7f8d2002-03-24 13:13:27 +00001639 case TK_CASE: {
1640 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001641 int jumpInst;
1642 int addr;
1643 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001644 int i;
drhbe5c89a2004-07-26 00:31:09 +00001645 ExprList *pEList;
1646 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001647
1648 assert(pExpr->pList);
1649 assert((pExpr->pList->nExpr % 2) == 0);
1650 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001651 pEList = pExpr->pList;
1652 aListelem = pEList->a;
1653 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001654 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001655 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001657 }
drhf5905aa2002-05-26 20:54:33 +00001658 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001659 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001660 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001661 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001662 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1663 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001664 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001665 }else{
danielk19774adee202004-05-08 08:23:19 +00001666 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001667 }
drhbe5c89a2004-07-26 00:31:09 +00001668 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001669 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1670 addr = sqlite3VdbeCurrentAddr(v);
1671 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001672 }
drhf570f012002-05-31 15:51:25 +00001673 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001674 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001675 }
drh17a7f8d2002-03-24 13:13:27 +00001676 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001677 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001678 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001679 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001680 }
danielk19774adee202004-05-08 08:23:19 +00001681 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001682 break;
1683 }
danielk19775338a5f2005-01-20 13:03:10 +00001684#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001685 case TK_RAISE: {
1686 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001687 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001688 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001689 return;
1690 }
drhad6d9462004-09-19 02:15:24 +00001691 if( pExpr->iColumn!=OE_Ignore ){
1692 assert( pExpr->iColumn==OE_Rollback ||
1693 pExpr->iColumn == OE_Abort ||
1694 pExpr->iColumn == OE_Fail );
1695 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1696 pExpr->token.z, pExpr->token.n);
1697 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001698 } else {
drhad6d9462004-09-19 02:15:24 +00001699 assert( pExpr->iColumn == OE_Ignore );
1700 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1701 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1702 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001703 }
drh17a7f8d2002-03-24 13:13:27 +00001704 }
danielk19775338a5f2005-01-20 13:03:10 +00001705#endif
drh17a7f8d2002-03-24 13:13:27 +00001706 break;
drhcce7d172000-05-31 15:34:51 +00001707 }
drhcce7d172000-05-31 15:34:51 +00001708}
1709
danielk197793758c82005-01-21 08:13:14 +00001710#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001711/*
drh25303782004-12-07 15:41:48 +00001712** Generate code that evalutes the given expression and leaves the result
1713** on the stack. See also sqlite3ExprCode().
1714**
1715** This routine might also cache the result and modify the pExpr tree
1716** so that it will make use of the cached result on subsequent evaluations
1717** rather than evaluate the whole expression again. Trivial expressions are
1718** not cached. If the expression is cached, its result is stored in a
1719** memory location.
1720*/
1721void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1722 Vdbe *v = pParse->pVdbe;
1723 int iMem;
1724 int addr1, addr2;
1725 if( v==0 ) return;
1726 addr1 = sqlite3VdbeCurrentAddr(v);
1727 sqlite3ExprCode(pParse, pExpr);
1728 addr2 = sqlite3VdbeCurrentAddr(v);
1729 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1730 iMem = pExpr->iTable = pParse->nMem++;
1731 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1732 pExpr->op = TK_REGISTER;
1733 }
1734}
danielk197793758c82005-01-21 08:13:14 +00001735#endif
drh25303782004-12-07 15:41:48 +00001736
1737/*
drh268380c2004-02-25 13:47:31 +00001738** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001739** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001740**
1741** Return the number of elements pushed onto the stack.
1742*/
danielk19774adee202004-05-08 08:23:19 +00001743int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001744 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001745 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001746){
1747 struct ExprList_item *pItem;
1748 int i, n;
1749 Vdbe *v;
1750 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001751 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001752 n = pList->nExpr;
1753 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001754 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001755 }
drhf9b596e2004-05-26 16:54:42 +00001756 return n;
drh268380c2004-02-25 13:47:31 +00001757}
1758
1759/*
drhcce7d172000-05-31 15:34:51 +00001760** Generate code for a boolean expression such that a jump is made
1761** to the label "dest" if the expression is true but execution
1762** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001763**
1764** If the expression evaluates to NULL (neither true nor false), then
1765** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001766**
1767** This code depends on the fact that certain token values (ex: TK_EQ)
1768** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1769** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1770** the make process cause these values to align. Assert()s in the code
1771** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001772*/
danielk19774adee202004-05-08 08:23:19 +00001773void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001774 Vdbe *v = pParse->pVdbe;
1775 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001776 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001777 op = pExpr->op;
1778 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001779 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001780 int d2 = sqlite3VdbeMakeLabel(v);
1781 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1782 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1783 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001784 break;
1785 }
1786 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001787 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1788 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001789 break;
1790 }
1791 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001792 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001793 break;
1794 }
1795 case TK_LT:
1796 case TK_LE:
1797 case TK_GT:
1798 case TK_GE:
1799 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001800 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001801 assert( TK_LT==OP_Lt );
1802 assert( TK_LE==OP_Le );
1803 assert( TK_GT==OP_Gt );
1804 assert( TK_GE==OP_Ge );
1805 assert( TK_EQ==OP_Eq );
1806 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001807 sqlite3ExprCode(pParse, pExpr->pLeft);
1808 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001809 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001810 break;
1811 }
1812 case TK_ISNULL:
1813 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001814 assert( TK_ISNULL==OP_IsNull );
1815 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001816 sqlite3ExprCode(pParse, pExpr->pLeft);
1817 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001818 break;
1819 }
drhfef52082000-06-06 01:50:43 +00001820 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001821 /* The expression "x BETWEEN y AND z" is implemented as:
1822 **
1823 ** 1 IF (x < y) GOTO 3
1824 ** 2 IF (x <= z) GOTO <dest>
1825 ** 3 ...
1826 */
drhf5905aa2002-05-26 20:54:33 +00001827 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001828 Expr *pLeft = pExpr->pLeft;
1829 Expr *pRight = pExpr->pList->a[0].pExpr;
1830 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001831 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001832 sqlite3ExprCode(pParse, pRight);
1833 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001834
drhbe5c89a2004-07-26 00:31:09 +00001835 pRight = pExpr->pList->a[1].pExpr;
1836 sqlite3ExprCode(pParse, pRight);
1837 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001838
danielk19774adee202004-05-08 08:23:19 +00001839 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1840 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1841 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001842 break;
1843 }
drhcce7d172000-05-31 15:34:51 +00001844 default: {
danielk19774adee202004-05-08 08:23:19 +00001845 sqlite3ExprCode(pParse, pExpr);
1846 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001847 break;
1848 }
1849 }
1850}
1851
1852/*
drh66b89c82000-11-28 20:47:17 +00001853** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001854** to the label "dest" if the expression is false but execution
1855** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001856**
1857** If the expression evaluates to NULL (neither true nor false) then
1858** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001859*/
danielk19774adee202004-05-08 08:23:19 +00001860void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001861 Vdbe *v = pParse->pVdbe;
1862 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001863 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001864
1865 /* The value of pExpr->op and op are related as follows:
1866 **
1867 ** pExpr->op op
1868 ** --------- ----------
1869 ** TK_ISNULL OP_NotNull
1870 ** TK_NOTNULL OP_IsNull
1871 ** TK_NE OP_Eq
1872 ** TK_EQ OP_Ne
1873 ** TK_GT OP_Le
1874 ** TK_LE OP_Gt
1875 ** TK_GE OP_Lt
1876 ** TK_LT OP_Ge
1877 **
1878 ** For other values of pExpr->op, op is undefined and unused.
1879 ** The value of TK_ and OP_ constants are arranged such that we
1880 ** can compute the mapping above using the following expression.
1881 ** Assert()s verify that the computation is correct.
1882 */
1883 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1884
1885 /* Verify correct alignment of TK_ and OP_ constants
1886 */
1887 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1888 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1889 assert( pExpr->op!=TK_NE || op==OP_Eq );
1890 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1891 assert( pExpr->op!=TK_LT || op==OP_Ge );
1892 assert( pExpr->op!=TK_LE || op==OP_Gt );
1893 assert( pExpr->op!=TK_GT || op==OP_Le );
1894 assert( pExpr->op!=TK_GE || op==OP_Lt );
1895
drhcce7d172000-05-31 15:34:51 +00001896 switch( pExpr->op ){
1897 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001898 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1899 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001900 break;
1901 }
1902 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001903 int d2 = sqlite3VdbeMakeLabel(v);
1904 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1905 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1906 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001907 break;
1908 }
1909 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001910 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001911 break;
1912 }
1913 case TK_LT:
1914 case TK_LE:
1915 case TK_GT:
1916 case TK_GE:
1917 case TK_NE:
1918 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001919 sqlite3ExprCode(pParse, pExpr->pLeft);
1920 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001921 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001922 break;
1923 }
drhcce7d172000-05-31 15:34:51 +00001924 case TK_ISNULL:
1925 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001926 sqlite3ExprCode(pParse, pExpr->pLeft);
1927 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001928 break;
1929 }
drhfef52082000-06-06 01:50:43 +00001930 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001931 /* The expression is "x BETWEEN y AND z". It is implemented as:
1932 **
1933 ** 1 IF (x >= y) GOTO 3
1934 ** 2 GOTO <dest>
1935 ** 3 IF (x > z) GOTO <dest>
1936 */
drhfef52082000-06-06 01:50:43 +00001937 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001938 Expr *pLeft = pExpr->pLeft;
1939 Expr *pRight = pExpr->pList->a[0].pExpr;
1940 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001941 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001942 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001943 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001944 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1945
danielk19774adee202004-05-08 08:23:19 +00001946 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1947 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001948 pRight = pExpr->pList->a[1].pExpr;
1949 sqlite3ExprCode(pParse, pRight);
1950 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001951 break;
1952 }
drhcce7d172000-05-31 15:34:51 +00001953 default: {
danielk19774adee202004-05-08 08:23:19 +00001954 sqlite3ExprCode(pParse, pExpr);
1955 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001956 break;
1957 }
1958 }
1959}
drh22827922000-06-06 17:27:05 +00001960
1961/*
1962** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1963** if they are identical and return FALSE if they differ in any way.
1964*/
danielk19774adee202004-05-08 08:23:19 +00001965int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001966 int i;
1967 if( pA==0 ){
1968 return pB==0;
1969 }else if( pB==0 ){
1970 return 0;
1971 }
1972 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001973 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1974 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001975 if( pA->pList ){
1976 if( pB->pList==0 ) return 0;
1977 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1978 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001979 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001980 return 0;
1981 }
1982 }
1983 }else if( pB->pList ){
1984 return 0;
1985 }
1986 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001987 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001988 if( pA->token.z ){
1989 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001990 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001991 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001992 }
1993 return 1;
1994}
1995
1996/*
1997** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001998** The new element is initialized to zero. The calling function is
1999** expected to fill it in.
drh22827922000-06-06 17:27:05 +00002000*/
2001static int appendAggInfo(Parse *pParse){
2002 if( (pParse->nAgg & 0x7)==0 ){
2003 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00002004 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
2005 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00002006 return -1;
2007 }
drh6d4abfb2001-10-22 02:58:08 +00002008 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00002009 }
2010 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
2011 return pParse->nAgg++;
2012}
2013
2014/*
drh626a8792005-01-17 22:08:19 +00002015** This is an xFunc for walkExprTree() used to implement
2016** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2017** for additional information.
drh22827922000-06-06 17:27:05 +00002018**
drh626a8792005-01-17 22:08:19 +00002019** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002020*/
drh626a8792005-01-17 22:08:19 +00002021static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002022 int i;
2023 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00002024 NameContext *pNC = (NameContext *)pArg;
2025 Parse *pParse = pNC->pParse;
2026 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00002027
drh22827922000-06-06 17:27:05 +00002028 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002029 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002030 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2031 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2032 aAgg = pParse->aAgg;
2033 for(i=0; i<pParse->nAgg; i++){
2034 if( aAgg[i].isAgg ) continue;
2035 if( aAgg[i].pExpr->iTable==pExpr->iTable
2036 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2037 break;
2038 }
2039 }
2040 if( i>=pParse->nAgg ){
2041 i = appendAggInfo(pParse);
2042 if( i<0 ) return 1;
2043 pParse->aAgg[i].isAgg = 0;
2044 pParse->aAgg[i].pExpr = pExpr;
2045 }
2046 pExpr->iAgg = i;
2047 pExpr->iAggCtx = pNC->nDepth;
2048 return 1;
drh22827922000-06-06 17:27:05 +00002049 }
2050 }
drh626a8792005-01-17 22:08:19 +00002051 return 1;
drh22827922000-06-06 17:27:05 +00002052 }
2053 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002054 if( pNC->nDepth==0 ){
2055 aAgg = pParse->aAgg;
2056 for(i=0; i<pParse->nAgg; i++){
2057 if( !aAgg[i].isAgg ) continue;
2058 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2059 break;
2060 }
drh22827922000-06-06 17:27:05 +00002061 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002062 if( i>=pParse->nAgg ){
2063 u8 enc = pParse->db->enc;
2064 i = appendAggInfo(pParse);
2065 if( i<0 ) return 1;
2066 pParse->aAgg[i].isAgg = 1;
2067 pParse->aAgg[i].pExpr = pExpr;
2068 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2069 pExpr->token.z, pExpr->token.n,
2070 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2071 }
2072 pExpr->iAgg = i;
2073 return 1;
drh22827922000-06-06 17:27:05 +00002074 }
drh22827922000-06-06 17:27:05 +00002075 }
2076 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002077 if( pExpr->pSelect ){
2078 pNC->nDepth++;
2079 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2080 pNC->nDepth--;
2081 }
drh626a8792005-01-17 22:08:19 +00002082 return 0;
2083}
2084
2085/*
2086** Analyze the given expression looking for aggregate functions and
2087** for variables that need to be added to the pParse->aAgg[] array.
2088** Make additional entries to the pParse->aAgg[] array as necessary.
2089**
2090** This routine should only be called after the expression has been
2091** analyzed by sqlite3ExprResolveNames().
2092**
2093** If errors are seen, leave an error message in zErrMsg and return
2094** the number of errors.
2095*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002096int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2097 int nErr = pNC->pParse->nErr;
2098 walkExprTree(pExpr, analyzeAggregate, pNC);
2099 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002100}
drh8e0a2f92002-02-23 23:45:45 +00002101
2102/*
danielk1977d02eb1f2004-06-06 09:44:03 +00002103** Locate a user function given a name, a number of arguments and a flag
2104** indicating whether the function prefers UTF-16 over UTF-8. Return a
2105** pointer to the FuncDef structure that defines that function, or return
2106** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00002107**
drh0bce8352002-02-28 00:41:10 +00002108** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00002109** structure is created and liked into the "db" structure if a
2110** no matching function previously existed. When createFlag is true
2111** and the nArg parameter is -1, then only a function that accepts
2112** any number of arguments will be returned.
2113**
2114** If createFlag is false and nArg is -1, then the first valid
2115** function found is returned. A function is valid if either xFunc
2116** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00002117**
2118** If createFlag is false, then a function with the required name and
2119** number of arguments may be returned even if the eTextRep flag does not
2120** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00002121*/
danielk19774adee202004-05-08 08:23:19 +00002122FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00002123 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00002124 const char *zName, /* Name of the function. Not null-terminated */
2125 int nName, /* Number of characters in the name */
2126 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00002127 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00002128 int createFlag /* Create new entry if true and does not otherwise exist */
2129){
danielk1977d02eb1f2004-06-06 09:44:03 +00002130 FuncDef *p; /* Iterator variable */
2131 FuncDef *pFirst; /* First function with this name */
2132 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00002133 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00002134
danielk1977d8123362004-06-12 09:25:12 +00002135
2136 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00002137 if( nArg<-1 ) nArg = -1;
2138
2139 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
2140 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00002141 /* During the search for the best function definition, bestmatch is set
2142 ** as follows to indicate the quality of the match with the definition
2143 ** pointed to by pBest:
2144 **
2145 ** 0: pBest is NULL. No match has been found.
2146 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
2147 ** encoding is requested, or vice versa.
2148 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
2149 ** requested, or vice versa.
2150 ** 3: A variable arguments function using the same text encoding.
2151 ** 4: A function with the exact number of arguments requested that
2152 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
2153 ** 5: A function with the exact number of arguments requested that
2154 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
2155 ** 6: An exact match.
2156 **
2157 ** A larger value of 'matchqual' indicates a more desirable match.
2158 */
danielk1977e12c17b2004-06-23 12:35:14 +00002159 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00002160 int match = 1; /* Quality of this match */
2161 if( p->nArg==nArg || nArg==-1 ){
2162 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00002163 }
danielk1977d8123362004-06-12 09:25:12 +00002164 if( enc==p->iPrefEnc ){
2165 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00002166 }
danielk1977d8123362004-06-12 09:25:12 +00002167 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2168 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2169 match += 1;
2170 }
2171
2172 if( match>bestmatch ){
2173 pBest = p;
2174 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002175 }
2176 }
drh8e0a2f92002-02-23 23:45:45 +00002177 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002178
danielk1977d8123362004-06-12 09:25:12 +00002179 /* If the createFlag parameter is true, and the seach did not reveal an
2180 ** exact match for the name, number of arguments and encoding, then add a
2181 ** new entry to the hash table and return it.
2182 */
2183 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002184 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2185 pBest->nArg = nArg;
2186 pBest->pNext = pFirst;
2187 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002188 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002189 memcpy(pBest->zName, zName, nName);
2190 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002191 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2192 sqliteFree(pBest);
2193 return 0;
2194 }
drh8e0a2f92002-02-23 23:45:45 +00002195 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002196
2197 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2198 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002199 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002200 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002201}