blob: 79c7b59e2acbe0a41d9c3bac95f0515b0013127e [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**
drhf8db1bc2005-04-22 02:38:37 +000015** $Id: expr.c,v 1.198 2005/04/22 02:38:38 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 pNC->nRef++;
794 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000795 if( pSrcList ){
796 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
797 Table *pTab = pItem->pTab;
798 Column *pCol;
799
800 if( pTab==0 ) continue;
801 assert( pTab->nCol>0 );
802 if( zTab ){
803 if( pItem->zAlias ){
804 char *zTabName = pItem->zAlias;
805 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
806 }else{
807 char *zTabName = pTab->zName;
808 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
809 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
810 continue;
811 }
drh626a8792005-01-17 22:08:19 +0000812 }
drh8141f612004-01-25 22:44:58 +0000813 }
danielk1977b3bce662005-01-29 08:32:43 +0000814 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000815 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000816 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000817 pMatch = pItem;
818 }
819 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
820 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
821 cnt++;
822 pExpr->iTable = pItem->iCursor;
823 pMatch = pItem;
824 pExpr->iDb = pTab->iDb;
825 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
826 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
827 pExpr->affinity = pTab->aCol[j].affinity;
828 pExpr->pColl = pTab->aCol[j].pColl;
829 break;
830 }
drh8141f612004-01-25 22:44:58 +0000831 }
832 }
833 }
drh626a8792005-01-17 22:08:19 +0000834
835#ifndef SQLITE_OMIT_TRIGGER
836 /* If we have not already resolved the name, then maybe
837 ** it is a new.* or old.* trigger argument reference
838 */
839 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
840 TriggerStack *pTriggerStack = pParse->trigStack;
841 Table *pTab = 0;
842 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
843 pExpr->iTable = pTriggerStack->newIdx;
844 assert( pTriggerStack->pTab );
845 pTab = pTriggerStack->pTab;
846 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
847 pExpr->iTable = pTriggerStack->oldIdx;
848 assert( pTriggerStack->pTab );
849 pTab = pTriggerStack->pTab;
850 }
851
852 if( pTab ){
853 int j;
854 Column *pCol = pTab->aCol;
855
856 pExpr->iDb = pTab->iDb;
857 cntTab++;
858 for(j=0; j < pTab->nCol; j++, pCol++) {
859 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
860 cnt++;
861 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
862 pExpr->affinity = pTab->aCol[j].affinity;
863 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000864 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000865 break;
866 }
867 }
868 }
869 }
drhb7f91642004-10-31 02:22:47 +0000870#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000871
drh626a8792005-01-17 22:08:19 +0000872 /*
873 ** Perhaps the name is a reference to the ROWID
874 */
875 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
876 cnt = 1;
877 pExpr->iColumn = -1;
878 pExpr->affinity = SQLITE_AFF_INTEGER;
879 }
drh8141f612004-01-25 22:44:58 +0000880
drh626a8792005-01-17 22:08:19 +0000881 /*
882 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
883 ** might refer to an result-set alias. This happens, for example, when
884 ** we are resolving names in the WHERE clause of the following command:
885 **
886 ** SELECT a+b AS x FROM table WHERE x<10;
887 **
888 ** In cases like this, replace pExpr with a copy of the expression that
889 ** forms the result set entry ("a+b" in the example) and return immediately.
890 ** Note that the expression in the result set should have already been
891 ** resolved by the time the WHERE clause is resolved.
892 */
drh79d5f632005-01-18 17:20:10 +0000893 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000894 for(j=0; j<pEList->nExpr; j++){
895 char *zAs = pEList->a[j].zName;
896 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
897 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
898 pExpr->op = TK_AS;
899 pExpr->iColumn = j;
900 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
901 sqliteFree(zCol);
902 assert( zTab==0 && zDb==0 );
903 return 0;
904 }
905 }
906 }
907
908 /* Advance to the next name context. The loop will exit when either
909 ** we have a match (cnt>0) or when we run out of name contexts.
910 */
911 if( cnt==0 ){
912 pNC = pNC->pNext;
913 }
drh8141f612004-01-25 22:44:58 +0000914 }
915
916 /*
917 ** If X and Y are NULL (in other words if only the column name Z is
918 ** supplied) and the value of Z is enclosed in double-quotes, then
919 ** Z is a string literal if it doesn't match any column names. In that
920 ** case, we need to return right away and not make any changes to
921 ** pExpr.
922 */
923 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
924 sqliteFree(zCol);
925 return 0;
926 }
927
928 /*
929 ** cnt==0 means there was not match. cnt>1 means there were two or
930 ** more matches. Either way, we have an error.
931 */
932 if( cnt!=1 ){
933 char *z = 0;
934 char *zErr;
935 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
936 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000937 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000938 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000939 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000940 }else{
941 z = sqliteStrDup(zCol);
942 }
danielk19774adee202004-05-08 08:23:19 +0000943 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000944 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000945 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000946 }
947
drh51669862004-12-18 18:40:26 +0000948 /* If a column from a table in pSrcList is referenced, then record
949 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
950 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
951 ** column number is greater than the number of bits in the bitmask
952 ** then set the high-order bit of the bitmask.
953 */
954 if( pExpr->iColumn>=0 && pMatch!=0 ){
955 int n = pExpr->iColumn;
956 if( n>=sizeof(Bitmask)*8 ){
957 n = sizeof(Bitmask)*8-1;
958 }
959 assert( pMatch->iCursor==pExpr->iTable );
960 pMatch->colUsed |= 1<<n;
961 }
962
danielk1977d5d56522005-03-16 12:15:20 +0000963lookupname_end:
drh8141f612004-01-25 22:44:58 +0000964 /* Clean up and return
965 */
966 sqliteFree(zDb);
967 sqliteFree(zTab);
968 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000969 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000970 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000971 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000972 pExpr->pRight = 0;
973 pExpr->op = TK_COLUMN;
drh626a8792005-01-17 22:08:19 +0000974 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000975 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000976 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +0000977 if( pMatch && !pMatch->pSelect ){
978 pExpr->pTab = pMatch->pTab;
979 }
drh626a8792005-01-17 22:08:19 +0000980 }
drh8141f612004-01-25 22:44:58 +0000981 return cnt!=1;
982}
983
984/*
drh626a8792005-01-17 22:08:19 +0000985** pExpr is a node that defines a function of some kind. It might
986** be a syntactic function like "count(x)" or it might be a function
987** that implements an operator, like "a LIKE b".
988**
989** This routine makes *pzName point to the name of the function and
990** *pnName hold the number of characters in the function name.
991*/
992static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
993 switch( pExpr->op ){
994 case TK_FUNCTION: {
995 *pzName = pExpr->token.z;
996 *pnName = pExpr->token.n;
997 break;
998 }
999 case TK_LIKE: {
1000 *pzName = "like";
1001 *pnName = 4;
1002 break;
1003 }
1004 case TK_GLOB: {
1005 *pzName = "glob";
1006 *pnName = 4;
1007 break;
1008 }
1009 case TK_CTIME: {
1010 *pzName = "current_time";
1011 *pnName = 12;
1012 break;
1013 }
1014 case TK_CDATE: {
1015 *pzName = "current_date";
1016 *pnName = 12;
1017 break;
1018 }
1019 case TK_CTIMESTAMP: {
1020 *pzName = "current_timestamp";
1021 *pnName = 17;
1022 break;
1023 }
drh626a8792005-01-17 22:08:19 +00001024 }
1025}
1026
1027/*
1028** This routine is designed as an xFunc for walkExprTree().
1029**
drh73b211a2005-01-18 04:00:42 +00001030** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001031** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001032** the tree or 2 to abort the tree walk.
1033**
1034** This routine also does error checking and name resolution for
1035** function names. The operator for aggregate functions is changed
1036** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001037*/
1038static int nameResolverStep(void *pArg, Expr *pExpr){
1039 NameContext *pNC = (NameContext*)pArg;
1040 SrcList *pSrcList;
1041 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001042
danielk1977b3bce662005-01-29 08:32:43 +00001043 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001044 assert( pNC!=0 );
1045 pSrcList = pNC->pSrcList;
1046 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001047
drh626a8792005-01-17 22:08:19 +00001048 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1049 ExprSetProperty(pExpr, EP_Resolved);
1050#ifndef NDEBUG
1051 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001052 int i;
drh626a8792005-01-17 22:08:19 +00001053 for(i=0; i<pSrcList->nSrc; i++){
1054 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1055 }
1056 }
1057#endif
1058 switch( pExpr->op ){
1059 /* Double-quoted strings (ex: "abc") are used as identifiers if
1060 ** possible. Otherwise they remain as strings. Single-quoted
1061 ** strings (ex: 'abc') are always string literals.
1062 */
1063 case TK_STRING: {
1064 if( pExpr->token.z[0]=='\'' ) break;
1065 /* Fall thru into the TK_ID case if this is a double-quoted string */
1066 }
1067 /* A lone identifier is the name of a column.
1068 */
1069 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001070 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1071 return 1;
1072 }
1073
1074 /* A table name and column name: ID.ID
1075 ** Or a database, table and column: ID.ID.ID
1076 */
1077 case TK_DOT: {
1078 Token *pColumn;
1079 Token *pTable;
1080 Token *pDb;
1081 Expr *pRight;
1082
danielk1977b3bce662005-01-29 08:32:43 +00001083 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001084 pRight = pExpr->pRight;
1085 if( pRight->op==TK_ID ){
1086 pDb = 0;
1087 pTable = &pExpr->pLeft->token;
1088 pColumn = &pRight->token;
1089 }else{
1090 assert( pRight->op==TK_DOT );
1091 pDb = &pExpr->pLeft->token;
1092 pTable = &pRight->pLeft->token;
1093 pColumn = &pRight->pRight->token;
1094 }
1095 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1096 return 1;
1097 }
1098
1099 /* Resolve function names
1100 */
1101 case TK_CTIME:
1102 case TK_CTIMESTAMP:
1103 case TK_CDATE:
drh626a8792005-01-17 22:08:19 +00001104 case TK_GLOB:
1105 case TK_LIKE:
1106 case TK_FUNCTION: {
1107 ExprList *pList = pExpr->pList; /* The argument list */
1108 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1109 int no_such_func = 0; /* True if no such function exists */
1110 int wrong_num_args = 0; /* True if wrong number of arguments */
1111 int is_agg = 0; /* True if is an aggregate function */
1112 int i;
1113 int nId; /* Number of characters in function name */
1114 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001115 FuncDef *pDef; /* Information about the function */
1116 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001117
1118 getFunctionName(pExpr, &zId, &nId);
1119 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1120 if( pDef==0 ){
1121 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1122 if( pDef==0 ){
1123 no_such_func = 1;
1124 }else{
1125 wrong_num_args = 1;
1126 }
1127 }else{
1128 is_agg = pDef->xFunc==0;
1129 }
1130 if( is_agg && !pNC->allowAgg ){
1131 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1132 pNC->nErr++;
1133 is_agg = 0;
1134 }else if( no_such_func ){
1135 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1136 pNC->nErr++;
1137 }else if( wrong_num_args ){
1138 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1139 nId, zId);
1140 pNC->nErr++;
1141 }
1142 if( is_agg ){
1143 pExpr->op = TK_AGG_FUNCTION;
1144 pNC->hasAgg = 1;
1145 }
drh73b211a2005-01-18 04:00:42 +00001146 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001147 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001148 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001149 }
drh73b211a2005-01-18 04:00:42 +00001150 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001151 /* FIX ME: Compute pExpr->affinity based on the expected return
1152 ** type of the function
1153 */
1154 return is_agg;
1155 }
danielk1977b3bce662005-01-29 08:32:43 +00001156#ifndef SQLITE_OMIT_SUBQUERY
1157 case TK_SELECT:
1158 case TK_EXISTS:
1159#endif
1160 case TK_IN: {
1161 if( pExpr->pSelect ){
1162 int nRef = pNC->nRef;
1163 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1164 assert( pNC->nRef>=nRef );
1165 if( nRef!=pNC->nRef ){
1166 ExprSetProperty(pExpr, EP_VarSelect);
1167 }
1168 }
1169 }
drh626a8792005-01-17 22:08:19 +00001170 }
1171 return 0;
1172}
1173
1174/*
drhcce7d172000-05-31 15:34:51 +00001175** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001176** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001177** index to the table in the table list and a column offset. The
1178** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1179** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001180** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001181** VDBE cursor number for a cursor that is pointing into the referenced
1182** table. The Expr.iColumn value is changed to the index of the column
1183** of the referenced table. The Expr.iColumn value for the special
1184** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1185** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001186**
drh626a8792005-01-17 22:08:19 +00001187** Also resolve function names and check the functions for proper
1188** usage. Make sure all function names are recognized and all functions
1189** have the correct number of arguments. Leave an error message
1190** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1191**
drh73b211a2005-01-18 04:00:42 +00001192** If the expression contains aggregate functions then set the EP_Agg
1193** property on the expression.
drh626a8792005-01-17 22:08:19 +00001194*/
1195int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001196 NameContext *pNC, /* Namespace to resolve expressions in. */
1197 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001198){
drh73b211a2005-01-18 04:00:42 +00001199 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001200 walkExprTree(pExpr, nameResolverStep, pNC);
1201 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001202 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001203 }
1204 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001205}
1206
drh1398ad32005-01-19 23:24:50 +00001207/*
1208** A pointer instance of this structure is used to pass information
1209** through walkExprTree into codeSubqueryStep().
1210*/
1211typedef struct QueryCoder QueryCoder;
1212struct QueryCoder {
1213 Parse *pParse; /* The parsing context */
1214 NameContext *pNC; /* Namespace of first enclosing query */
1215};
1216
drh626a8792005-01-17 22:08:19 +00001217
1218/*
1219** Generate code for subqueries and IN operators.
1220**
drh73b211a2005-01-18 04:00:42 +00001221** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001222**
1223** expr IN (exprlist)
1224** and
1225** expr IN (SELECT ...)
1226**
1227** The first form is handled by creating a set holding the list
1228** of allowed values. The second form causes the SELECT to generate
1229** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001230*/
drh51522cd2005-01-20 13:36:19 +00001231#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001232void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1233 int label = 0; /* Address after sub-select code */
1234 Vdbe *v = sqlite3GetVdbe(pParse);
1235 if( v==0 ) return;
1236
1237 /* If this is not a variable (correlated) select, then execute
1238 ** it only once. Unless this is part of a trigger program. In
1239 ** that case re-execute every time (this could be optimized).
1240 */
1241 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1242 int mem = pParse->nMem++;
1243 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1244 label = sqlite3VdbeMakeLabel(v);
1245 sqlite3VdbeAddOp(v, OP_If, 0, label);
1246 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1247 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1248 }
1249
1250 if( pExpr->pSelect ){
1251 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1252 }
drh6a3ea0e2003-05-02 14:32:12 +00001253
drhcce7d172000-05-31 15:34:51 +00001254 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001255 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001256 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001257 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001258 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001259
danielk1977bf3b7212004-05-18 10:06:24 +00001260 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001261
1262 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1263 ** expression it is handled the same way. A temporary table is
1264 ** filled with single-field index keys representing the results
1265 ** from the SELECT or the <exprlist>.
1266 **
1267 ** If the 'x' expression is a column value, or the SELECT...
1268 ** statement returns a column value, then the affinity of that
1269 ** column is used to build the index keys. If both 'x' and the
1270 ** SELECT... statement are columns, then numeric affinity is used
1271 ** if either column has NUMERIC or INTEGER affinity. If neither
1272 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1273 ** is used.
1274 */
1275 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001276 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001277 memset(&keyInfo, 0, sizeof(keyInfo));
1278 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001279 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001280
drhfef52082000-06-06 01:50:43 +00001281 if( pExpr->pSelect ){
1282 /* Case 1: expr IN (SELECT ...)
1283 **
danielk1977e014a832004-05-17 10:48:57 +00001284 ** Generate code to write the results of the select into the temporary
1285 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001286 */
danielk1977e014a832004-05-17 10:48:57 +00001287 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001288 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001289 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001290 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001291 pEList = pExpr->pSelect->pEList;
1292 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001293 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001294 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001295 }
drhfef52082000-06-06 01:50:43 +00001296 }else if( pExpr->pList ){
1297 /* Case 2: expr IN (exprlist)
1298 **
danielk1977e014a832004-05-17 10:48:57 +00001299 ** For each expression, build an index key from the evaluation and
1300 ** store it in the temporary table. If <expr> is a column, then use
1301 ** that columns affinity when building index keys. If <expr> is not
1302 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001303 */
danielk1977e014a832004-05-17 10:48:57 +00001304 int i;
danielk1977e014a832004-05-17 10:48:57 +00001305 if( !affinity ){
1306 affinity = SQLITE_AFF_NUMERIC;
1307 }
danielk19770202b292004-06-09 09:55:16 +00001308 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001309
1310 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001311 for(i=0; i<pExpr->pList->nExpr; i++){
1312 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001313
1314 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001315 if( !sqlite3ExprIsConstant(pE2) ){
1316 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001317 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001318 return;
drh4794b982000-06-06 13:54:14 +00001319 }
danielk1977e014a832004-05-17 10:48:57 +00001320
1321 /* Evaluate the expression and insert it into the temp table */
1322 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001323 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001324 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001325 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001326 }
1327 }
danielk19770202b292004-06-09 09:55:16 +00001328 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001329 break;
drhfef52082000-06-06 01:50:43 +00001330 }
1331
drh51522cd2005-01-20 13:36:19 +00001332 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001333 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001334 /* This has to be a scalar SELECT. Generate code to put the
1335 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001336 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001337 */
drh51522cd2005-01-20 13:36:19 +00001338 int sop;
1339 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001340
drh967e8b72000-06-21 13:59:10 +00001341 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001342 pSel = pExpr->pSelect;
1343 if( pExpr->op==TK_SELECT ){
1344 sop = SRT_Mem;
1345 }else{
1346 static const Token one = { "1", 0, 1 };
1347 sop = SRT_Exists;
1348 sqlite3ExprListDelete(pSel->pEList);
1349 pSel->pEList = sqlite3ExprListAppend(0,
1350 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1351 }
danielk1977b3bce662005-01-29 08:32:43 +00001352 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1353 break;
drhcce7d172000-05-31 15:34:51 +00001354 }
1355 }
danielk1977b3bce662005-01-29 08:32:43 +00001356
1357 if( pExpr->pSelect ){
1358 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1359 }
1360 if( label<0 ){
1361 sqlite3VdbeResolveLabel(v, label);
1362 }
1363 return;
drhcce7d172000-05-31 15:34:51 +00001364}
drh51522cd2005-01-20 13:36:19 +00001365#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001366
drhcce7d172000-05-31 15:34:51 +00001367/*
drhfec19aa2004-05-19 20:41:03 +00001368** Generate an instruction that will put the integer describe by
1369** text z[0..n-1] on the stack.
1370*/
1371static void codeInteger(Vdbe *v, const char *z, int n){
1372 int i;
drh6fec0762004-05-30 01:38:43 +00001373 if( sqlite3GetInt32(z, &i) ){
1374 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1375 }else if( sqlite3FitsIn64Bits(z) ){
1376 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001377 }else{
1378 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1379 }
1380}
1381
1382/*
drhcce7d172000-05-31 15:34:51 +00001383** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001384** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001385**
1386** This code depends on the fact that certain token values (ex: TK_EQ)
1387** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1388** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1389** the make process cause these values to align. Assert()s in the code
1390** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001391*/
danielk19774adee202004-05-08 08:23:19 +00001392void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001393 Vdbe *v = pParse->pVdbe;
1394 int op;
danielk19777977a172004-11-09 12:44:37 +00001395 if( v==0 ) return;
1396 if( pExpr==0 ){
1397 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1398 return;
1399 }
drhf2bc0132004-10-04 13:19:23 +00001400 op = pExpr->op;
1401 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001402 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001403 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1404 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001405 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001406 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001407 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001408 }else{
danielk19774adee202004-05-08 08:23:19 +00001409 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001410 }
drhcce7d172000-05-31 15:34:51 +00001411 break;
1412 }
1413 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001414 codeInteger(v, pExpr->token.z, pExpr->token.n);
1415 break;
1416 }
1417 case TK_FLOAT:
1418 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001419 assert( TK_FLOAT==OP_Real );
1420 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001421 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001422 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001423 break;
1424 }
danielk19775338a5f2005-01-20 13:03:10 +00001425#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001426 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001427 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001428 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1429 sqlite3VdbeDequoteP3(v, -1);
1430 break;
1431 }
danielk19775338a5f2005-01-20 13:03:10 +00001432#endif
drhcce7d172000-05-31 15:34:51 +00001433 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001434 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001435 break;
1436 }
drh50457892003-09-06 01:10:47 +00001437 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001438 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001439 if( pExpr->token.n>1 ){
1440 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1441 }
drh50457892003-09-06 01:10:47 +00001442 break;
1443 }
drh4e0cff62004-11-05 05:10:28 +00001444 case TK_REGISTER: {
1445 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1446 break;
1447 }
drhc9b84a12002-06-20 11:36:48 +00001448 case TK_LT:
1449 case TK_LE:
1450 case TK_GT:
1451 case TK_GE:
1452 case TK_NE:
1453 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001454 assert( TK_LT==OP_Lt );
1455 assert( TK_LE==OP_Le );
1456 assert( TK_GT==OP_Gt );
1457 assert( TK_GE==OP_Ge );
1458 assert( TK_EQ==OP_Eq );
1459 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001460 sqlite3ExprCode(pParse, pExpr->pLeft);
1461 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001462 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001463 break;
drhc9b84a12002-06-20 11:36:48 +00001464 }
drhcce7d172000-05-31 15:34:51 +00001465 case TK_AND:
1466 case TK_OR:
1467 case TK_PLUS:
1468 case TK_STAR:
1469 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001470 case TK_REM:
1471 case TK_BITAND:
1472 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001473 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001474 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001475 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001476 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001477 assert( TK_AND==OP_And );
1478 assert( TK_OR==OP_Or );
1479 assert( TK_PLUS==OP_Add );
1480 assert( TK_MINUS==OP_Subtract );
1481 assert( TK_REM==OP_Remainder );
1482 assert( TK_BITAND==OP_BitAnd );
1483 assert( TK_BITOR==OP_BitOr );
1484 assert( TK_SLASH==OP_Divide );
1485 assert( TK_LSHIFT==OP_ShiftLeft );
1486 assert( TK_RSHIFT==OP_ShiftRight );
1487 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001488 sqlite3ExprCode(pParse, pExpr->pLeft);
1489 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001490 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001491 break;
1492 }
drhcce7d172000-05-31 15:34:51 +00001493 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001494 Expr *pLeft = pExpr->pLeft;
1495 assert( pLeft );
1496 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1497 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001498 char *z = sqliteMalloc( p->n + 2 );
1499 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001500 if( pLeft->op==TK_FLOAT ){
1501 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001502 }else{
drhfec19aa2004-05-19 20:41:03 +00001503 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001504 }
drh6e142f52000-06-08 13:36:40 +00001505 sqliteFree(z);
1506 break;
1507 }
drh1ccde152000-06-17 13:12:39 +00001508 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001509 }
drhbf4133c2001-10-13 02:59:08 +00001510 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001511 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001512 assert( TK_BITNOT==OP_BitNot );
1513 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001514 sqlite3ExprCode(pParse, pExpr->pLeft);
1515 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001516 break;
1517 }
1518 case TK_ISNULL:
1519 case TK_NOTNULL: {
1520 int dest;
drhf2bc0132004-10-04 13:19:23 +00001521 assert( TK_ISNULL==OP_IsNull );
1522 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001523 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1524 sqlite3ExprCode(pParse, pExpr->pLeft);
1525 dest = sqlite3VdbeCurrentAddr(v) + 2;
1526 sqlite3VdbeAddOp(v, op, 1, dest);
1527 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001528 break;
drhcce7d172000-05-31 15:34:51 +00001529 }
drh22827922000-06-06 17:27:05 +00001530 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001531 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001532 break;
1533 }
danielk19777977a172004-11-09 12:44:37 +00001534 case TK_CDATE:
1535 case TK_CTIME:
1536 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001537 case TK_GLOB:
1538 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001539 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001540 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001541 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001542 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001543 int nId;
1544 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001545 int p2 = 0;
1546 int i;
danielk1977d8123362004-06-12 09:25:12 +00001547 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001548 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001549 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001550 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001551 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001552 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001553 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001554 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1555 p2 |= (1<<i);
1556 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001557 if( pDef->needCollSeq && !pColl ){
1558 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1559 }
1560 }
1561 if( pDef->needCollSeq ){
1562 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001563 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001564 }
1565 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001566 break;
1567 }
drhfe2093d2005-01-20 22:48:47 +00001568#ifndef SQLITE_OMIT_SUBQUERY
1569 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001570 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001571 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001572 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001573 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001574 break;
1575 }
drhfef52082000-06-06 01:50:43 +00001576 case TK_IN: {
1577 int addr;
drh94a11212004-09-25 13:12:14 +00001578 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001579 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001580
1581 /* Figure out the affinity to use to create a key from the results
1582 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001583 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001584 */
drh94a11212004-09-25 13:12:14 +00001585 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001586
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001588
1589 /* Code the <expr> from "<expr> IN (...)". The temporary table
1590 ** pExpr->iTable contains the values that make up the (...) set.
1591 */
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3ExprCode(pParse, pExpr->pLeft);
1593 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001594 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001595 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001596 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001597 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001598 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001599 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1600 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1601
drhfef52082000-06-06 01:50:43 +00001602 break;
1603 }
danielk197793758c82005-01-21 08:13:14 +00001604#endif
drhfef52082000-06-06 01:50:43 +00001605 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001606 Expr *pLeft = pExpr->pLeft;
1607 struct ExprList_item *pLItem = pExpr->pList->a;
1608 Expr *pRight = pLItem->pExpr;
1609 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001610 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001611 sqlite3ExprCode(pParse, pRight);
1612 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001613 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001614 pLItem++;
1615 pRight = pLItem->pExpr;
1616 sqlite3ExprCode(pParse, pRight);
1617 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001618 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001619 break;
1620 }
drh51e9a442004-01-16 16:42:53 +00001621 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001622 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001623 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001624 break;
1625 }
drh17a7f8d2002-03-24 13:13:27 +00001626 case TK_CASE: {
1627 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001628 int jumpInst;
1629 int addr;
1630 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001631 int i;
drhbe5c89a2004-07-26 00:31:09 +00001632 ExprList *pEList;
1633 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001634
1635 assert(pExpr->pList);
1636 assert((pExpr->pList->nExpr % 2) == 0);
1637 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001638 pEList = pExpr->pList;
1639 aListelem = pEList->a;
1640 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001641 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001642 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001643 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001644 }
drhf5905aa2002-05-26 20:54:33 +00001645 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001646 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001647 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001648 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001649 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1650 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001651 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001652 }else{
danielk19774adee202004-05-08 08:23:19 +00001653 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001654 }
drhbe5c89a2004-07-26 00:31:09 +00001655 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1657 addr = sqlite3VdbeCurrentAddr(v);
1658 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001659 }
drhf570f012002-05-31 15:51:25 +00001660 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001661 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001662 }
drh17a7f8d2002-03-24 13:13:27 +00001663 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001664 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001665 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001666 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001667 }
danielk19774adee202004-05-08 08:23:19 +00001668 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001669 break;
1670 }
danielk19775338a5f2005-01-20 13:03:10 +00001671#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001672 case TK_RAISE: {
1673 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001674 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001675 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001676 return;
1677 }
drhad6d9462004-09-19 02:15:24 +00001678 if( pExpr->iColumn!=OE_Ignore ){
1679 assert( pExpr->iColumn==OE_Rollback ||
1680 pExpr->iColumn == OE_Abort ||
1681 pExpr->iColumn == OE_Fail );
1682 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1683 pExpr->token.z, pExpr->token.n);
1684 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001685 } else {
drhad6d9462004-09-19 02:15:24 +00001686 assert( pExpr->iColumn == OE_Ignore );
1687 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1688 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1689 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001690 }
drh17a7f8d2002-03-24 13:13:27 +00001691 }
danielk19775338a5f2005-01-20 13:03:10 +00001692#endif
drh17a7f8d2002-03-24 13:13:27 +00001693 break;
drhcce7d172000-05-31 15:34:51 +00001694 }
drhcce7d172000-05-31 15:34:51 +00001695}
1696
danielk197793758c82005-01-21 08:13:14 +00001697#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001698/*
drh25303782004-12-07 15:41:48 +00001699** Generate code that evalutes the given expression and leaves the result
1700** on the stack. See also sqlite3ExprCode().
1701**
1702** This routine might also cache the result and modify the pExpr tree
1703** so that it will make use of the cached result on subsequent evaluations
1704** rather than evaluate the whole expression again. Trivial expressions are
1705** not cached. If the expression is cached, its result is stored in a
1706** memory location.
1707*/
1708void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1709 Vdbe *v = pParse->pVdbe;
1710 int iMem;
1711 int addr1, addr2;
1712 if( v==0 ) return;
1713 addr1 = sqlite3VdbeCurrentAddr(v);
1714 sqlite3ExprCode(pParse, pExpr);
1715 addr2 = sqlite3VdbeCurrentAddr(v);
1716 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1717 iMem = pExpr->iTable = pParse->nMem++;
1718 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1719 pExpr->op = TK_REGISTER;
1720 }
1721}
danielk197793758c82005-01-21 08:13:14 +00001722#endif
drh25303782004-12-07 15:41:48 +00001723
1724/*
drh268380c2004-02-25 13:47:31 +00001725** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001726** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001727**
1728** Return the number of elements pushed onto the stack.
1729*/
danielk19774adee202004-05-08 08:23:19 +00001730int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001731 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001732 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001733){
1734 struct ExprList_item *pItem;
1735 int i, n;
1736 Vdbe *v;
1737 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001738 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001739 n = pList->nExpr;
1740 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001741 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001742 }
drhf9b596e2004-05-26 16:54:42 +00001743 return n;
drh268380c2004-02-25 13:47:31 +00001744}
1745
1746/*
drhcce7d172000-05-31 15:34:51 +00001747** Generate code for a boolean expression such that a jump is made
1748** to the label "dest" if the expression is true but execution
1749** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001750**
1751** If the expression evaluates to NULL (neither true nor false), then
1752** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001753**
1754** This code depends on the fact that certain token values (ex: TK_EQ)
1755** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1756** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1757** the make process cause these values to align. Assert()s in the code
1758** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001759*/
danielk19774adee202004-05-08 08:23:19 +00001760void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001761 Vdbe *v = pParse->pVdbe;
1762 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001763 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001764 op = pExpr->op;
1765 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001766 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001767 int d2 = sqlite3VdbeMakeLabel(v);
1768 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1769 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1770 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001771 break;
1772 }
1773 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001774 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1775 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001776 break;
1777 }
1778 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001780 break;
1781 }
1782 case TK_LT:
1783 case TK_LE:
1784 case TK_GT:
1785 case TK_GE:
1786 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001787 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001788 assert( TK_LT==OP_Lt );
1789 assert( TK_LE==OP_Le );
1790 assert( TK_GT==OP_Gt );
1791 assert( TK_GE==OP_Ge );
1792 assert( TK_EQ==OP_Eq );
1793 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001794 sqlite3ExprCode(pParse, pExpr->pLeft);
1795 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001796 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001797 break;
1798 }
1799 case TK_ISNULL:
1800 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001801 assert( TK_ISNULL==OP_IsNull );
1802 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001803 sqlite3ExprCode(pParse, pExpr->pLeft);
1804 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001805 break;
1806 }
drhfef52082000-06-06 01:50:43 +00001807 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001808 /* The expression "x BETWEEN y AND z" is implemented as:
1809 **
1810 ** 1 IF (x < y) GOTO 3
1811 ** 2 IF (x <= z) GOTO <dest>
1812 ** 3 ...
1813 */
drhf5905aa2002-05-26 20:54:33 +00001814 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001815 Expr *pLeft = pExpr->pLeft;
1816 Expr *pRight = pExpr->pList->a[0].pExpr;
1817 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001818 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001819 sqlite3ExprCode(pParse, pRight);
1820 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001821
drhbe5c89a2004-07-26 00:31:09 +00001822 pRight = pExpr->pList->a[1].pExpr;
1823 sqlite3ExprCode(pParse, pRight);
1824 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001825
danielk19774adee202004-05-08 08:23:19 +00001826 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1827 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1828 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001829 break;
1830 }
drhcce7d172000-05-31 15:34:51 +00001831 default: {
danielk19774adee202004-05-08 08:23:19 +00001832 sqlite3ExprCode(pParse, pExpr);
1833 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001834 break;
1835 }
1836 }
1837}
1838
1839/*
drh66b89c82000-11-28 20:47:17 +00001840** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001841** to the label "dest" if the expression is false but execution
1842** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001843**
1844** If the expression evaluates to NULL (neither true nor false) then
1845** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001846*/
danielk19774adee202004-05-08 08:23:19 +00001847void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001848 Vdbe *v = pParse->pVdbe;
1849 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001850 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001851
1852 /* The value of pExpr->op and op are related as follows:
1853 **
1854 ** pExpr->op op
1855 ** --------- ----------
1856 ** TK_ISNULL OP_NotNull
1857 ** TK_NOTNULL OP_IsNull
1858 ** TK_NE OP_Eq
1859 ** TK_EQ OP_Ne
1860 ** TK_GT OP_Le
1861 ** TK_LE OP_Gt
1862 ** TK_GE OP_Lt
1863 ** TK_LT OP_Ge
1864 **
1865 ** For other values of pExpr->op, op is undefined and unused.
1866 ** The value of TK_ and OP_ constants are arranged such that we
1867 ** can compute the mapping above using the following expression.
1868 ** Assert()s verify that the computation is correct.
1869 */
1870 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1871
1872 /* Verify correct alignment of TK_ and OP_ constants
1873 */
1874 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1875 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1876 assert( pExpr->op!=TK_NE || op==OP_Eq );
1877 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1878 assert( pExpr->op!=TK_LT || op==OP_Ge );
1879 assert( pExpr->op!=TK_LE || op==OP_Gt );
1880 assert( pExpr->op!=TK_GT || op==OP_Le );
1881 assert( pExpr->op!=TK_GE || op==OP_Lt );
1882
drhcce7d172000-05-31 15:34:51 +00001883 switch( pExpr->op ){
1884 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001885 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1886 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001887 break;
1888 }
1889 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001890 int d2 = sqlite3VdbeMakeLabel(v);
1891 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1892 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1893 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001894 break;
1895 }
1896 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001897 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001898 break;
1899 }
1900 case TK_LT:
1901 case TK_LE:
1902 case TK_GT:
1903 case TK_GE:
1904 case TK_NE:
1905 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001906 sqlite3ExprCode(pParse, pExpr->pLeft);
1907 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001908 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001909 break;
1910 }
drhcce7d172000-05-31 15:34:51 +00001911 case TK_ISNULL:
1912 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001913 sqlite3ExprCode(pParse, pExpr->pLeft);
1914 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001915 break;
1916 }
drhfef52082000-06-06 01:50:43 +00001917 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001918 /* The expression is "x BETWEEN y AND z". It is implemented as:
1919 **
1920 ** 1 IF (x >= y) GOTO 3
1921 ** 2 GOTO <dest>
1922 ** 3 IF (x > z) GOTO <dest>
1923 */
drhfef52082000-06-06 01:50:43 +00001924 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001925 Expr *pLeft = pExpr->pLeft;
1926 Expr *pRight = pExpr->pList->a[0].pExpr;
1927 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001928 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001929 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001930 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001931 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1932
danielk19774adee202004-05-08 08:23:19 +00001933 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1934 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001935 pRight = pExpr->pList->a[1].pExpr;
1936 sqlite3ExprCode(pParse, pRight);
1937 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001938 break;
1939 }
drhcce7d172000-05-31 15:34:51 +00001940 default: {
danielk19774adee202004-05-08 08:23:19 +00001941 sqlite3ExprCode(pParse, pExpr);
1942 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001943 break;
1944 }
1945 }
1946}
drh22827922000-06-06 17:27:05 +00001947
1948/*
1949** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1950** if they are identical and return FALSE if they differ in any way.
1951*/
danielk19774adee202004-05-08 08:23:19 +00001952int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001953 int i;
1954 if( pA==0 ){
1955 return pB==0;
1956 }else if( pB==0 ){
1957 return 0;
1958 }
1959 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001960 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1961 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001962 if( pA->pList ){
1963 if( pB->pList==0 ) return 0;
1964 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1965 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001966 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001967 return 0;
1968 }
1969 }
1970 }else if( pB->pList ){
1971 return 0;
1972 }
1973 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001974 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001975 if( pA->token.z ){
1976 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001977 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001978 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001979 }
1980 return 1;
1981}
1982
1983/*
1984** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001985** The new element is initialized to zero. The calling function is
1986** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001987*/
1988static int appendAggInfo(Parse *pParse){
1989 if( (pParse->nAgg & 0x7)==0 ){
1990 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001991 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1992 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001993 return -1;
1994 }
drh6d4abfb2001-10-22 02:58:08 +00001995 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001996 }
1997 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1998 return pParse->nAgg++;
1999}
2000
2001/*
drh626a8792005-01-17 22:08:19 +00002002** This is an xFunc for walkExprTree() used to implement
2003** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2004** for additional information.
drh22827922000-06-06 17:27:05 +00002005**
drh626a8792005-01-17 22:08:19 +00002006** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002007*/
drh626a8792005-01-17 22:08:19 +00002008static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002009 int i;
2010 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00002011 NameContext *pNC = (NameContext *)pArg;
2012 Parse *pParse = pNC->pParse;
2013 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00002014
drh22827922000-06-06 17:27:05 +00002015 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002016 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002017 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2018 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2019 aAgg = pParse->aAgg;
2020 for(i=0; i<pParse->nAgg; i++){
2021 if( aAgg[i].isAgg ) continue;
2022 if( aAgg[i].pExpr->iTable==pExpr->iTable
2023 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2024 break;
2025 }
2026 }
2027 if( i>=pParse->nAgg ){
2028 i = appendAggInfo(pParse);
2029 if( i<0 ) return 1;
2030 pParse->aAgg[i].isAgg = 0;
2031 pParse->aAgg[i].pExpr = pExpr;
2032 }
2033 pExpr->iAgg = i;
2034 pExpr->iAggCtx = pNC->nDepth;
2035 return 1;
drh22827922000-06-06 17:27:05 +00002036 }
2037 }
drh626a8792005-01-17 22:08:19 +00002038 return 1;
drh22827922000-06-06 17:27:05 +00002039 }
2040 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002041 if( pNC->nDepth==0 ){
2042 aAgg = pParse->aAgg;
2043 for(i=0; i<pParse->nAgg; i++){
2044 if( !aAgg[i].isAgg ) continue;
2045 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2046 break;
2047 }
drh22827922000-06-06 17:27:05 +00002048 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002049 if( i>=pParse->nAgg ){
2050 u8 enc = pParse->db->enc;
2051 i = appendAggInfo(pParse);
2052 if( i<0 ) return 1;
2053 pParse->aAgg[i].isAgg = 1;
2054 pParse->aAgg[i].pExpr = pExpr;
2055 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2056 pExpr->token.z, pExpr->token.n,
2057 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2058 }
2059 pExpr->iAgg = i;
2060 return 1;
drh22827922000-06-06 17:27:05 +00002061 }
drh22827922000-06-06 17:27:05 +00002062 }
2063 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002064 if( pExpr->pSelect ){
2065 pNC->nDepth++;
2066 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2067 pNC->nDepth--;
2068 }
drh626a8792005-01-17 22:08:19 +00002069 return 0;
2070}
2071
2072/*
2073** Analyze the given expression looking for aggregate functions and
2074** for variables that need to be added to the pParse->aAgg[] array.
2075** Make additional entries to the pParse->aAgg[] array as necessary.
2076**
2077** This routine should only be called after the expression has been
2078** analyzed by sqlite3ExprResolveNames().
2079**
2080** If errors are seen, leave an error message in zErrMsg and return
2081** the number of errors.
2082*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002083int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2084 int nErr = pNC->pParse->nErr;
2085 walkExprTree(pExpr, analyzeAggregate, pNC);
2086 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002087}
drh8e0a2f92002-02-23 23:45:45 +00002088
2089/*
danielk1977d02eb1f2004-06-06 09:44:03 +00002090** Locate a user function given a name, a number of arguments and a flag
2091** indicating whether the function prefers UTF-16 over UTF-8. Return a
2092** pointer to the FuncDef structure that defines that function, or return
2093** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00002094**
drh0bce8352002-02-28 00:41:10 +00002095** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00002096** structure is created and liked into the "db" structure if a
2097** no matching function previously existed. When createFlag is true
2098** and the nArg parameter is -1, then only a function that accepts
2099** any number of arguments will be returned.
2100**
2101** If createFlag is false and nArg is -1, then the first valid
2102** function found is returned. A function is valid if either xFunc
2103** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00002104**
2105** If createFlag is false, then a function with the required name and
2106** number of arguments may be returned even if the eTextRep flag does not
2107** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00002108*/
danielk19774adee202004-05-08 08:23:19 +00002109FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00002110 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00002111 const char *zName, /* Name of the function. Not null-terminated */
2112 int nName, /* Number of characters in the name */
2113 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00002114 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00002115 int createFlag /* Create new entry if true and does not otherwise exist */
2116){
danielk1977d02eb1f2004-06-06 09:44:03 +00002117 FuncDef *p; /* Iterator variable */
2118 FuncDef *pFirst; /* First function with this name */
2119 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00002120 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00002121
danielk1977d8123362004-06-12 09:25:12 +00002122
2123 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00002124 if( nArg<-1 ) nArg = -1;
2125
2126 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
2127 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00002128 /* During the search for the best function definition, bestmatch is set
2129 ** as follows to indicate the quality of the match with the definition
2130 ** pointed to by pBest:
2131 **
2132 ** 0: pBest is NULL. No match has been found.
2133 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
2134 ** encoding is requested, or vice versa.
2135 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
2136 ** requested, or vice versa.
2137 ** 3: A variable arguments function using the same text encoding.
2138 ** 4: A function with the exact number of arguments requested that
2139 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
2140 ** 5: A function with the exact number of arguments requested that
2141 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
2142 ** 6: An exact match.
2143 **
2144 ** A larger value of 'matchqual' indicates a more desirable match.
2145 */
danielk1977e12c17b2004-06-23 12:35:14 +00002146 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00002147 int match = 1; /* Quality of this match */
2148 if( p->nArg==nArg || nArg==-1 ){
2149 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00002150 }
danielk1977d8123362004-06-12 09:25:12 +00002151 if( enc==p->iPrefEnc ){
2152 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00002153 }
danielk1977d8123362004-06-12 09:25:12 +00002154 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2155 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2156 match += 1;
2157 }
2158
2159 if( match>bestmatch ){
2160 pBest = p;
2161 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002162 }
2163 }
drh8e0a2f92002-02-23 23:45:45 +00002164 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002165
danielk1977d8123362004-06-12 09:25:12 +00002166 /* If the createFlag parameter is true, and the seach did not reveal an
2167 ** exact match for the name, number of arguments and encoding, then add a
2168 ** new entry to the hash table and return it.
2169 */
2170 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002171 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2172 pBest->nArg = nArg;
2173 pBest->pNext = pFirst;
2174 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002175 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002176 memcpy(pBest->zName, zName, nName);
2177 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002178 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2179 sqliteFree(pBest);
2180 return 0;
2181 }
drh8e0a2f92002-02-23 23:45:45 +00002182 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002183
2184 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2185 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002186 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002187 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002188}