blob: 1723e60c7dc074f3ed0312e4e7591453e51fa3fd [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**
danielk1977aee18ef2005-03-09 12:26:50 +000015** $Id: expr.c,v 1.195 2005/03/09 12:26:51 danielk1977 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 ){
drh4efc4752004-01-16 15:55:37 +0000186 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000187 return 0;
188 }
189 pNew->op = op;
190 pNew->pLeft = pLeft;
191 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000192 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000193 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000194 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000195 pNew->span = pNew->token = *pToken;
196 }else if( pLeft && pRight ){
197 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000198 }
drha76b5df2002-02-23 02:32:10 +0000199 return pNew;
200}
201
202/*
drh4e0cff62004-11-05 05:10:28 +0000203** When doing a nested parse, you can include terms in an expression
204** that look like this: #0 #1 #2 ... These terms refer to elements
205** on the stack. "#0" (or just "#") means the top of the stack.
drh2958a4e2004-11-12 03:56:15 +0000206** "#1" means the next down on the stack. And so forth. #-1 means
207** memory location 0. #-2 means memory location 1. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000208**
209** This routine is called by the parser to deal with on of those terms.
210** It immediately generates code to store the value in a memory location.
211** The returns an expression that will code to extract the value from
212** that memory location as needed.
213*/
214Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
215 Vdbe *v = pParse->pVdbe;
216 Expr *p;
217 int depth;
218 if( v==0 ) return 0;
219 if( pParse->nested==0 ){
220 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
221 return 0;
222 }
223 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000224 if( p==0 ){
225 return 0; /* Malloc failed */
226 }
drh4e0cff62004-11-05 05:10:28 +0000227 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000228 if( depth>=0 ){
229 p->iTable = pParse->nMem++;
230 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
231 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
232 }else{
233 p->iTable = -1-depth;
234 }
drh4e0cff62004-11-05 05:10:28 +0000235 return p;
236}
237
238/*
drh91bb0ee2004-09-01 03:06:34 +0000239** Join two expressions using an AND operator. If either expression is
240** NULL, then just return the other expression.
241*/
242Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
243 if( pLeft==0 ){
244 return pRight;
245 }else if( pRight==0 ){
246 return pLeft;
247 }else{
248 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
249 }
250}
251
252/*
drh6977fea2002-10-22 23:38:04 +0000253** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000254** text between the two given tokens.
255*/
danielk19774adee202004-05-08 08:23:19 +0000256void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000257 assert( pRight!=0 );
258 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000259 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000260 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000261 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000262 pExpr->span.z = pLeft->z;
263 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000264 }else{
drh6977fea2002-10-22 23:38:04 +0000265 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000266 }
drha76b5df2002-02-23 02:32:10 +0000267 }
268}
269
270/*
271** Construct a new expression node for a function with multiple
272** arguments.
273*/
danielk19774adee202004-05-08 08:23:19 +0000274Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000275 Expr *pNew;
276 pNew = sqliteMalloc( sizeof(Expr) );
277 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000278 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000279 return 0;
280 }
281 pNew->op = TK_FUNCTION;
282 pNew->pList = pList;
283 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000284 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000285 pNew->token = *pToken;
286 }else{
287 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000288 }
drh6977fea2002-10-22 23:38:04 +0000289 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000290 return pNew;
291}
292
293/*
drhfa6bc002004-09-07 16:19:52 +0000294** Assign a variable number to an expression that encodes a wildcard
295** in the original SQL statement.
296**
297** Wildcards consisting of a single "?" are assigned the next sequential
298** variable number.
299**
300** Wildcards of the form "?nnn" are assigned the number "nnn". We make
301** sure "nnn" is not too be to avoid a denial of service attack when
302** the SQL statement comes from an external source.
303**
304** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
305** as the previous instance of the same wildcard. Or if this is the first
306** instance of the wildcard, the next sequenial variable number is
307** assigned.
308*/
309void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
310 Token *pToken;
311 if( pExpr==0 ) return;
312 pToken = &pExpr->token;
313 assert( pToken->n>=1 );
314 assert( pToken->z!=0 );
315 assert( pToken->z[0]!=0 );
316 if( pToken->n==1 ){
317 /* Wildcard of the form "?". Assign the next variable number */
318 pExpr->iTable = ++pParse->nVar;
319 }else if( pToken->z[0]=='?' ){
320 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
321 ** use it as the variable number */
322 int i;
323 pExpr->iTable = i = atoi(&pToken->z[1]);
324 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
325 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
326 SQLITE_MAX_VARIABLE_NUMBER);
327 }
328 if( i>pParse->nVar ){
329 pParse->nVar = i;
330 }
331 }else{
332 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
333 ** number as the prior appearance of the same name, or if the name
334 ** has never appeared before, reuse the same variable number
335 */
336 int i, n;
337 n = pToken->n;
338 for(i=0; i<pParse->nVarExpr; i++){
339 Expr *pE;
340 if( (pE = pParse->apVarExpr[i])!=0
341 && pE->token.n==n
342 && memcmp(pE->token.z, pToken->z, n)==0 ){
343 pExpr->iTable = pE->iTable;
344 break;
345 }
346 }
347 if( i>=pParse->nVarExpr ){
348 pExpr->iTable = ++pParse->nVar;
349 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
350 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
351 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
352 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
353 }
354 if( !sqlite3_malloc_failed ){
355 assert( pParse->apVarExpr!=0 );
356 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
357 }
358 }
359 }
360}
361
362/*
drha2e00042002-01-22 03:13:42 +0000363** Recursively delete an expression tree.
364*/
danielk19774adee202004-05-08 08:23:19 +0000365void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000366 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000367 if( p->span.dyn ) sqliteFree((char*)p->span.z);
368 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3ExprDelete(p->pLeft);
370 sqlite3ExprDelete(p->pRight);
371 sqlite3ExprListDelete(p->pList);
372 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000373 sqliteFree(p);
374}
375
drha76b5df2002-02-23 02:32:10 +0000376
377/*
drhff78bd22002-02-27 01:47:11 +0000378** The following group of routines make deep copies of expressions,
379** expression lists, ID lists, and select statements. The copies can
380** be deleted (by being passed to their respective ...Delete() routines)
381** without effecting the originals.
382**
danielk19774adee202004-05-08 08:23:19 +0000383** The expression list, ID, and source lists return by sqlite3ExprListDup(),
384** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000385** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000386**
drhad3cab52002-05-24 02:04:32 +0000387** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000388*/
danielk19774adee202004-05-08 08:23:19 +0000389Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000390 Expr *pNew;
391 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000392 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000393 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000394 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000395 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000396 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000397 pNew->token.dyn = 1;
398 }else{
drh4efc4752004-01-16 15:55:37 +0000399 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000400 }
drh6977fea2002-10-22 23:38:04 +0000401 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000402 pNew->pLeft = sqlite3ExprDup(p->pLeft);
403 pNew->pRight = sqlite3ExprDup(p->pRight);
404 pNew->pList = sqlite3ExprListDup(p->pList);
405 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000406 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000407 return pNew;
408}
danielk19774adee202004-05-08 08:23:19 +0000409void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000410 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000411 if( pFrom->z ){
412 pTo->n = pFrom->n;
413 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
414 pTo->dyn = 1;
415 }else{
drh4b59ab52002-08-24 18:24:51 +0000416 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000417 }
418}
danielk19774adee202004-05-08 08:23:19 +0000419ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000420 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000421 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000422 int i;
423 if( p==0 ) return 0;
424 pNew = sqliteMalloc( sizeof(*pNew) );
425 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000426 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000427 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000428 if( pItem==0 ){
429 sqliteFree(pNew);
430 return 0;
431 }
drh145716b2004-09-24 12:24:06 +0000432 pOldItem = p->a;
433 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000434 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000435 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000436 if( pOldExpr->span.z!=0 && pNewExpr ){
437 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000438 ** expression list. The logic in SELECT processing that determines
439 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000440 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000441 }
drh1f3e9052002-10-31 00:09:39 +0000442 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000443 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000444 pItem->zName = sqliteStrDup(pOldItem->zName);
445 pItem->sortOrder = pOldItem->sortOrder;
446 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000447 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000448 }
449 return pNew;
450}
danielk197793758c82005-01-21 08:13:14 +0000451
452/*
453** If cursors, triggers, views and subqueries are all omitted from
454** the build, then none of the following routines, except for
455** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
456** called with a NULL argument.
457*/
danielk19776a67fe82005-02-04 04:07:16 +0000458#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
459 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000460SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000461 SrcList *pNew;
462 int i;
drh113088e2003-03-20 01:16:58 +0000463 int nByte;
drhad3cab52002-05-24 02:04:32 +0000464 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000465 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000466 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000467 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000468 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000469 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000470 struct SrcList_item *pNewItem = &pNew->a[i];
471 struct SrcList_item *pOldItem = &p->a[i];
472 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
473 pNewItem->zName = sqliteStrDup(pOldItem->zName);
474 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
475 pNewItem->jointype = pOldItem->jointype;
476 pNewItem->iCursor = pOldItem->iCursor;
danielk1977a1cb1832005-02-12 08:59:55 +0000477 pNewItem->pTab = pOldItem->pTab;
478 if( pNewItem->pTab ){
479 pNewItem->pTab->isTransient = 0;
480 }
danielk19774adee202004-05-08 08:23:19 +0000481 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
482 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
483 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000484 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000485 }
486 return pNew;
487}
danielk19774adee202004-05-08 08:23:19 +0000488IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000489 IdList *pNew;
490 int i;
491 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000492 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000493 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000494 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000495 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000496 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000497 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000498 struct IdList_item *pNewItem = &pNew->a[i];
499 struct IdList_item *pOldItem = &p->a[i];
500 pNewItem->zName = sqliteStrDup(pOldItem->zName);
501 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000502 }
503 return pNew;
504}
danielk19774adee202004-05-08 08:23:19 +0000505Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000506 Select *pNew;
507 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000508 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000509 if( pNew==0 ) return 0;
510 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000511 pNew->pEList = sqlite3ExprListDup(p->pEList);
512 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
513 pNew->pWhere = sqlite3ExprDup(p->pWhere);
514 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
515 pNew->pHaving = sqlite3ExprDup(p->pHaving);
516 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000517 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000518 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000519 pNew->pLimit = sqlite3ExprDup(p->pLimit);
520 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000521 pNew->iLimit = -1;
522 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000523 pNew->ppOpenTemp = 0;
drhb6c29892004-11-22 19:12:19 +0000524 pNew->pFetch = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000525 pNew->isResolved = p->isResolved;
526 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000527 return pNew;
528}
danielk197793758c82005-01-21 08:13:14 +0000529#else
530Select *sqlite3SelectDup(Select *p){
531 assert( p==0 );
532 return 0;
533}
534#endif
drhff78bd22002-02-27 01:47:11 +0000535
536
537/*
drha76b5df2002-02-23 02:32:10 +0000538** Add a new element to the end of an expression list. If pList is
539** initially NULL, then create a new expression list.
540*/
danielk19774adee202004-05-08 08:23:19 +0000541ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000542 if( pList==0 ){
543 pList = sqliteMalloc( sizeof(ExprList) );
544 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000545 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000546 return 0;
547 }
drh4efc4752004-01-16 15:55:37 +0000548 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000549 }
drh4305d102003-07-30 12:34:12 +0000550 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000551 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000552 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
553 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000554 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000555 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000556 return pList;
557 }
drha76b5df2002-02-23 02:32:10 +0000558 }
drh4efc4752004-01-16 15:55:37 +0000559 assert( pList->a!=0 );
560 if( pExpr || pName ){
561 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
562 memset(pItem, 0, sizeof(*pItem));
563 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000564 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000565 }
566 return pList;
567}
568
569/*
570** Delete an entire expression list.
571*/
danielk19774adee202004-05-08 08:23:19 +0000572void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000573 int i;
drhbe5c89a2004-07-26 00:31:09 +0000574 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000575 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000576 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
577 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000578 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
579 sqlite3ExprDelete(pItem->pExpr);
580 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000581 }
582 sqliteFree(pList->a);
583 sqliteFree(pList);
584}
585
586/*
drh626a8792005-01-17 22:08:19 +0000587** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000588**
drh626a8792005-01-17 22:08:19 +0000589** The return value from xFunc determines whether the tree walk continues.
590** 0 means continue walking the tree. 1 means do not walk children
591** of the current node but continue with siblings. 2 means abandon
592** the tree walk completely.
593**
594** The return value from this routine is 1 to abandon the tree walk
595** and 0 to continue.
596*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000597static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000598static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000599 int rc;
600 if( pExpr==0 ) return 0;
601 rc = (*xFunc)(pArg, pExpr);
602 if( rc==0 ){
603 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
604 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000605 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000606 }
607 return rc>1;
608}
609
610/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000611** Call walkExprTree() for every expression in list p.
612*/
613static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
614 int i;
615 struct ExprList_item *pItem;
616 if( !p ) return 0;
617 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
618 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
619 }
620 return 0;
621}
622
623/*
624** Call walkExprTree() for every expression in Select p, not including
625** expressions that are part of sub-selects in any FROM clause or the LIMIT
626** or OFFSET expressions..
627*/
628static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
629 walkExprList(p->pEList, xFunc, pArg);
630 walkExprTree(p->pWhere, xFunc, pArg);
631 walkExprList(p->pGroupBy, xFunc, pArg);
632 walkExprTree(p->pHaving, xFunc, pArg);
633 walkExprList(p->pOrderBy, xFunc, pArg);
634 return 0;
635}
636
637
638/*
drh626a8792005-01-17 22:08:19 +0000639** This routine is designed as an xFunc for walkExprTree().
640**
641** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000642** at pExpr that the expression that contains pExpr is not a constant
643** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
644** If pExpr does does not disqualify the expression from being a constant
645** then do nothing.
646**
647** After walking the whole tree, if no nodes are found that disqualify
648** the expression as constant, then we assume the whole expression
649** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000650*/
651static int exprNodeIsConstant(void *pArg, Expr *pExpr){
652 switch( pExpr->op ){
653 case TK_ID:
654 case TK_COLUMN:
655 case TK_DOT:
656 case TK_AGG_FUNCTION:
657 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000658#ifndef SQLITE_OMIT_SUBQUERY
659 case TK_SELECT:
660 case TK_EXISTS:
661#endif
drh626a8792005-01-17 22:08:19 +0000662 *((int*)pArg) = 0;
663 return 2;
664 default:
665 return 0;
666 }
667}
668
669/*
drhfef52082000-06-06 01:50:43 +0000670** Walk an expression tree. Return 1 if the expression is constant
671** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000672**
673** For the purposes of this function, a double-quoted string (ex: "abc")
674** is considered a variable but a single-quoted string (ex: 'abc') is
675** a constant.
drhfef52082000-06-06 01:50:43 +0000676*/
danielk19774adee202004-05-08 08:23:19 +0000677int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000678 int isConst = 1;
679 walkExprTree(p, exprNodeIsConstant, &isConst);
680 return isConst;
drhfef52082000-06-06 01:50:43 +0000681}
682
683/*
drh73b211a2005-01-18 04:00:42 +0000684** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000685** to fit in a 32-bit integer, return 1 and put the value of the integer
686** in *pValue. If the expression is not an integer or if it is too big
687** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000688*/
danielk19774adee202004-05-08 08:23:19 +0000689int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000690 switch( p->op ){
691 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000692 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000693 return 1;
694 }
695 break;
drhe4de1fe2002-06-02 16:09:01 +0000696 }
drh4b59ab52002-08-24 18:24:51 +0000697 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000698 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000699 }
drhe4de1fe2002-06-02 16:09:01 +0000700 case TK_UMINUS: {
701 int v;
danielk19774adee202004-05-08 08:23:19 +0000702 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000703 *pValue = -v;
704 return 1;
705 }
706 break;
707 }
708 default: break;
709 }
710 return 0;
711}
712
713/*
drhc4a3c772001-04-04 11:48:57 +0000714** Return TRUE if the given string is a row-id column name.
715*/
danielk19774adee202004-05-08 08:23:19 +0000716int sqlite3IsRowid(const char *z){
717 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
718 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
719 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000720 return 0;
721}
722
723/*
drh8141f612004-01-25 22:44:58 +0000724** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
725** that name in the set of source tables in pSrcList and make the pExpr
726** expression node refer back to that source column. The following changes
727** are made to pExpr:
728**
729** pExpr->iDb Set the index in db->aDb[] of the database holding
730** the table.
731** pExpr->iTable Set to the cursor number for the table obtained
732** from pSrcList.
733** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000734** pExpr->op Set to TK_COLUMN.
735** pExpr->pLeft Any expression this points to is deleted
736** pExpr->pRight Any expression this points to is deleted.
737**
738** The pDbToken is the name of the database (the "X"). This value may be
739** NULL meaning that name is of the form Y.Z or Z. Any available database
740** can be used. The pTableToken is the name of the table (the "Y"). This
741** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
742** means that the form of the name is Z and that columns from any table
743** can be used.
744**
745** If the name cannot be resolved unambiguously, leave an error message
746** in pParse and return non-zero. Return zero on success.
747*/
748static int lookupName(
749 Parse *pParse, /* The parsing context */
750 Token *pDbToken, /* Name of the database containing table, or NULL */
751 Token *pTableToken, /* Name of table containing column, or NULL */
752 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000753 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000754 Expr *pExpr /* Make this EXPR node point to the selected column */
755){
756 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
757 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
758 char *zCol = 0; /* Name of the column. The "Z" */
759 int i, j; /* Loop counters */
760 int cnt = 0; /* Number of matching column names */
761 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000762 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000763 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
764 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000765 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000766
767 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000768 zDb = sqlite3NameFromToken(pDbToken);
769 zTab = sqlite3NameFromToken(pTableToken);
770 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000771 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000772 return 1; /* Leak memory (zDb and zTab) if malloc fails */
773 }
drh8141f612004-01-25 22:44:58 +0000774
775 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000776 while( pNC && cnt==0 ){
777 SrcList *pSrcList = pNC->pSrcList;
778 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000779
drh626a8792005-01-17 22:08:19 +0000780 pNC->nRef++;
781 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000782 if( pSrcList ){
783 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
784 Table *pTab = pItem->pTab;
785 Column *pCol;
786
787 if( pTab==0 ) continue;
788 assert( pTab->nCol>0 );
789 if( zTab ){
790 if( pItem->zAlias ){
791 char *zTabName = pItem->zAlias;
792 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
793 }else{
794 char *zTabName = pTab->zName;
795 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
796 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
797 continue;
798 }
drh626a8792005-01-17 22:08:19 +0000799 }
drh8141f612004-01-25 22:44:58 +0000800 }
danielk1977b3bce662005-01-29 08:32:43 +0000801 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000802 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000803 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000804 pMatch = pItem;
805 }
806 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
807 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
808 cnt++;
809 pExpr->iTable = pItem->iCursor;
810 pMatch = pItem;
811 pExpr->iDb = pTab->iDb;
812 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
813 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
814 pExpr->affinity = pTab->aCol[j].affinity;
815 pExpr->pColl = pTab->aCol[j].pColl;
816 break;
817 }
drh8141f612004-01-25 22:44:58 +0000818 }
819 }
820 }
drh626a8792005-01-17 22:08:19 +0000821
822#ifndef SQLITE_OMIT_TRIGGER
823 /* If we have not already resolved the name, then maybe
824 ** it is a new.* or old.* trigger argument reference
825 */
826 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
827 TriggerStack *pTriggerStack = pParse->trigStack;
828 Table *pTab = 0;
829 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
830 pExpr->iTable = pTriggerStack->newIdx;
831 assert( pTriggerStack->pTab );
832 pTab = pTriggerStack->pTab;
833 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
834 pExpr->iTable = pTriggerStack->oldIdx;
835 assert( pTriggerStack->pTab );
836 pTab = pTriggerStack->pTab;
837 }
838
839 if( pTab ){
840 int j;
841 Column *pCol = pTab->aCol;
842
843 pExpr->iDb = pTab->iDb;
844 cntTab++;
845 for(j=0; j < pTab->nCol; j++, pCol++) {
846 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
847 cnt++;
848 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
849 pExpr->affinity = pTab->aCol[j].affinity;
850 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000851 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000852 break;
853 }
854 }
855 }
856 }
drhb7f91642004-10-31 02:22:47 +0000857#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000858
drh626a8792005-01-17 22:08:19 +0000859 /*
860 ** Perhaps the name is a reference to the ROWID
861 */
862 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
863 cnt = 1;
864 pExpr->iColumn = -1;
865 pExpr->affinity = SQLITE_AFF_INTEGER;
866 }
drh8141f612004-01-25 22:44:58 +0000867
drh626a8792005-01-17 22:08:19 +0000868 /*
869 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
870 ** might refer to an result-set alias. This happens, for example, when
871 ** we are resolving names in the WHERE clause of the following command:
872 **
873 ** SELECT a+b AS x FROM table WHERE x<10;
874 **
875 ** In cases like this, replace pExpr with a copy of the expression that
876 ** forms the result set entry ("a+b" in the example) and return immediately.
877 ** Note that the expression in the result set should have already been
878 ** resolved by the time the WHERE clause is resolved.
879 */
drh79d5f632005-01-18 17:20:10 +0000880 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000881 for(j=0; j<pEList->nExpr; j++){
882 char *zAs = pEList->a[j].zName;
883 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
884 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
885 pExpr->op = TK_AS;
886 pExpr->iColumn = j;
887 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
888 sqliteFree(zCol);
889 assert( zTab==0 && zDb==0 );
890 return 0;
891 }
892 }
893 }
894
895 /* Advance to the next name context. The loop will exit when either
896 ** we have a match (cnt>0) or when we run out of name contexts.
897 */
898 if( cnt==0 ){
899 pNC = pNC->pNext;
900 }
drh8141f612004-01-25 22:44:58 +0000901 }
902
903 /*
904 ** If X and Y are NULL (in other words if only the column name Z is
905 ** supplied) and the value of Z is enclosed in double-quotes, then
906 ** Z is a string literal if it doesn't match any column names. In that
907 ** case, we need to return right away and not make any changes to
908 ** pExpr.
909 */
910 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
911 sqliteFree(zCol);
912 return 0;
913 }
914
915 /*
916 ** cnt==0 means there was not match. cnt>1 means there were two or
917 ** more matches. Either way, we have an error.
918 */
919 if( cnt!=1 ){
920 char *z = 0;
921 char *zErr;
922 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
923 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000924 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000925 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000926 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000927 }else{
928 z = sqliteStrDup(zCol);
929 }
danielk19774adee202004-05-08 08:23:19 +0000930 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000931 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000932 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000933 }
934
drh51669862004-12-18 18:40:26 +0000935 /* If a column from a table in pSrcList is referenced, then record
936 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
937 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
938 ** column number is greater than the number of bits in the bitmask
939 ** then set the high-order bit of the bitmask.
940 */
941 if( pExpr->iColumn>=0 && pMatch!=0 ){
942 int n = pExpr->iColumn;
943 if( n>=sizeof(Bitmask)*8 ){
944 n = sizeof(Bitmask)*8-1;
945 }
946 assert( pMatch->iCursor==pExpr->iTable );
947 pMatch->colUsed |= 1<<n;
948 }
949
drh8141f612004-01-25 22:44:58 +0000950 /* Clean up and return
951 */
952 sqliteFree(zDb);
953 sqliteFree(zTab);
954 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000955 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000956 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000957 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000958 pExpr->pRight = 0;
959 pExpr->op = TK_COLUMN;
drh626a8792005-01-17 22:08:19 +0000960 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000961 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000962 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +0000963 if( pMatch && !pMatch->pSelect ){
964 pExpr->pTab = pMatch->pTab;
965 }
drh626a8792005-01-17 22:08:19 +0000966 }
drh8141f612004-01-25 22:44:58 +0000967 return cnt!=1;
968}
969
970/*
drh626a8792005-01-17 22:08:19 +0000971** pExpr is a node that defines a function of some kind. It might
972** be a syntactic function like "count(x)" or it might be a function
973** that implements an operator, like "a LIKE b".
974**
975** This routine makes *pzName point to the name of the function and
976** *pnName hold the number of characters in the function name.
977*/
978static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
979 switch( pExpr->op ){
980 case TK_FUNCTION: {
981 *pzName = pExpr->token.z;
982 *pnName = pExpr->token.n;
983 break;
984 }
985 case TK_LIKE: {
986 *pzName = "like";
987 *pnName = 4;
988 break;
989 }
990 case TK_GLOB: {
991 *pzName = "glob";
992 *pnName = 4;
993 break;
994 }
995 case TK_CTIME: {
996 *pzName = "current_time";
997 *pnName = 12;
998 break;
999 }
1000 case TK_CDATE: {
1001 *pzName = "current_date";
1002 *pnName = 12;
1003 break;
1004 }
1005 case TK_CTIMESTAMP: {
1006 *pzName = "current_timestamp";
1007 *pnName = 17;
1008 break;
1009 }
drh626a8792005-01-17 22:08:19 +00001010 }
1011}
1012
1013/*
1014** This routine is designed as an xFunc for walkExprTree().
1015**
drh73b211a2005-01-18 04:00:42 +00001016** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001017** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001018** the tree or 2 to abort the tree walk.
1019**
1020** This routine also does error checking and name resolution for
1021** function names. The operator for aggregate functions is changed
1022** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001023*/
1024static int nameResolverStep(void *pArg, Expr *pExpr){
1025 NameContext *pNC = (NameContext*)pArg;
1026 SrcList *pSrcList;
1027 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001028
danielk1977b3bce662005-01-29 08:32:43 +00001029 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001030 assert( pNC!=0 );
1031 pSrcList = pNC->pSrcList;
1032 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001033
drh626a8792005-01-17 22:08:19 +00001034 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1035 ExprSetProperty(pExpr, EP_Resolved);
1036#ifndef NDEBUG
1037 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001038 int i;
drh626a8792005-01-17 22:08:19 +00001039 for(i=0; i<pSrcList->nSrc; i++){
1040 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1041 }
1042 }
1043#endif
1044 switch( pExpr->op ){
1045 /* Double-quoted strings (ex: "abc") are used as identifiers if
1046 ** possible. Otherwise they remain as strings. Single-quoted
1047 ** strings (ex: 'abc') are always string literals.
1048 */
1049 case TK_STRING: {
1050 if( pExpr->token.z[0]=='\'' ) break;
1051 /* Fall thru into the TK_ID case if this is a double-quoted string */
1052 }
1053 /* A lone identifier is the name of a column.
1054 */
1055 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001056 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1057 return 1;
1058 }
1059
1060 /* A table name and column name: ID.ID
1061 ** Or a database, table and column: ID.ID.ID
1062 */
1063 case TK_DOT: {
1064 Token *pColumn;
1065 Token *pTable;
1066 Token *pDb;
1067 Expr *pRight;
1068
danielk1977b3bce662005-01-29 08:32:43 +00001069 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001070 pRight = pExpr->pRight;
1071 if( pRight->op==TK_ID ){
1072 pDb = 0;
1073 pTable = &pExpr->pLeft->token;
1074 pColumn = &pRight->token;
1075 }else{
1076 assert( pRight->op==TK_DOT );
1077 pDb = &pExpr->pLeft->token;
1078 pTable = &pRight->pLeft->token;
1079 pColumn = &pRight->pRight->token;
1080 }
1081 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1082 return 1;
1083 }
1084
1085 /* Resolve function names
1086 */
1087 case TK_CTIME:
1088 case TK_CTIMESTAMP:
1089 case TK_CDATE:
drh626a8792005-01-17 22:08:19 +00001090 case TK_GLOB:
1091 case TK_LIKE:
1092 case TK_FUNCTION: {
1093 ExprList *pList = pExpr->pList; /* The argument list */
1094 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1095 int no_such_func = 0; /* True if no such function exists */
1096 int wrong_num_args = 0; /* True if wrong number of arguments */
1097 int is_agg = 0; /* True if is an aggregate function */
1098 int i;
1099 int nId; /* Number of characters in function name */
1100 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001101 FuncDef *pDef; /* Information about the function */
1102 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001103
1104 getFunctionName(pExpr, &zId, &nId);
1105 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1106 if( pDef==0 ){
1107 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1108 if( pDef==0 ){
1109 no_such_func = 1;
1110 }else{
1111 wrong_num_args = 1;
1112 }
1113 }else{
1114 is_agg = pDef->xFunc==0;
1115 }
1116 if( is_agg && !pNC->allowAgg ){
1117 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1118 pNC->nErr++;
1119 is_agg = 0;
1120 }else if( no_such_func ){
1121 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1122 pNC->nErr++;
1123 }else if( wrong_num_args ){
1124 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1125 nId, zId);
1126 pNC->nErr++;
1127 }
1128 if( is_agg ){
1129 pExpr->op = TK_AGG_FUNCTION;
1130 pNC->hasAgg = 1;
1131 }
drh73b211a2005-01-18 04:00:42 +00001132 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001133 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001134 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001135 }
drh73b211a2005-01-18 04:00:42 +00001136 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001137 /* FIX ME: Compute pExpr->affinity based on the expected return
1138 ** type of the function
1139 */
1140 return is_agg;
1141 }
danielk1977b3bce662005-01-29 08:32:43 +00001142#ifndef SQLITE_OMIT_SUBQUERY
1143 case TK_SELECT:
1144 case TK_EXISTS:
1145#endif
1146 case TK_IN: {
1147 if( pExpr->pSelect ){
1148 int nRef = pNC->nRef;
1149 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1150 assert( pNC->nRef>=nRef );
1151 if( nRef!=pNC->nRef ){
1152 ExprSetProperty(pExpr, EP_VarSelect);
1153 }
1154 }
1155 }
drh626a8792005-01-17 22:08:19 +00001156 }
1157 return 0;
1158}
1159
1160/*
drhcce7d172000-05-31 15:34:51 +00001161** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001162** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001163** index to the table in the table list and a column offset. The
1164** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1165** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001166** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001167** VDBE cursor number for a cursor that is pointing into the referenced
1168** table. The Expr.iColumn value is changed to the index of the column
1169** of the referenced table. The Expr.iColumn value for the special
1170** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1171** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001172**
drh626a8792005-01-17 22:08:19 +00001173** Also resolve function names and check the functions for proper
1174** usage. Make sure all function names are recognized and all functions
1175** have the correct number of arguments. Leave an error message
1176** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1177**
drh73b211a2005-01-18 04:00:42 +00001178** If the expression contains aggregate functions then set the EP_Agg
1179** property on the expression.
drh626a8792005-01-17 22:08:19 +00001180*/
1181int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001182 NameContext *pNC, /* Namespace to resolve expressions in. */
1183 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001184){
drh73b211a2005-01-18 04:00:42 +00001185 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001186 walkExprTree(pExpr, nameResolverStep, pNC);
1187 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001188 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001189 }
1190 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001191}
1192
drh1398ad32005-01-19 23:24:50 +00001193/*
1194** A pointer instance of this structure is used to pass information
1195** through walkExprTree into codeSubqueryStep().
1196*/
1197typedef struct QueryCoder QueryCoder;
1198struct QueryCoder {
1199 Parse *pParse; /* The parsing context */
1200 NameContext *pNC; /* Namespace of first enclosing query */
1201};
1202
drh626a8792005-01-17 22:08:19 +00001203
1204/*
1205** Generate code for subqueries and IN operators.
1206**
drh73b211a2005-01-18 04:00:42 +00001207** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001208**
1209** expr IN (exprlist)
1210** and
1211** expr IN (SELECT ...)
1212**
1213** The first form is handled by creating a set holding the list
1214** of allowed values. The second form causes the SELECT to generate
1215** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001216*/
drh51522cd2005-01-20 13:36:19 +00001217#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001218void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1219 int label = 0; /* Address after sub-select code */
1220 Vdbe *v = sqlite3GetVdbe(pParse);
1221 if( v==0 ) return;
1222
1223 /* If this is not a variable (correlated) select, then execute
1224 ** it only once. Unless this is part of a trigger program. In
1225 ** that case re-execute every time (this could be optimized).
1226 */
1227 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1228 int mem = pParse->nMem++;
1229 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1230 label = sqlite3VdbeMakeLabel(v);
1231 sqlite3VdbeAddOp(v, OP_If, 0, label);
1232 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1233 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1234 }
1235
1236 if( pExpr->pSelect ){
1237 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1238 }
drh6a3ea0e2003-05-02 14:32:12 +00001239
drhcce7d172000-05-31 15:34:51 +00001240 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001241 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001242 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001243 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001244 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001245
danielk1977bf3b7212004-05-18 10:06:24 +00001246 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001247
1248 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1249 ** expression it is handled the same way. A temporary table is
1250 ** filled with single-field index keys representing the results
1251 ** from the SELECT or the <exprlist>.
1252 **
1253 ** If the 'x' expression is a column value, or the SELECT...
1254 ** statement returns a column value, then the affinity of that
1255 ** column is used to build the index keys. If both 'x' and the
1256 ** SELECT... statement are columns, then numeric affinity is used
1257 ** if either column has NUMERIC or INTEGER affinity. If neither
1258 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1259 ** is used.
1260 */
1261 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001262 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001263 memset(&keyInfo, 0, sizeof(keyInfo));
1264 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001265 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001266
drhfef52082000-06-06 01:50:43 +00001267 if( pExpr->pSelect ){
1268 /* Case 1: expr IN (SELECT ...)
1269 **
danielk1977e014a832004-05-17 10:48:57 +00001270 ** Generate code to write the results of the select into the temporary
1271 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001272 */
danielk1977e014a832004-05-17 10:48:57 +00001273 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001274 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001275 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001276 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001277 pEList = pExpr->pSelect->pEList;
1278 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001279 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001280 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001281 }
drhfef52082000-06-06 01:50:43 +00001282 }else if( pExpr->pList ){
1283 /* Case 2: expr IN (exprlist)
1284 **
danielk1977e014a832004-05-17 10:48:57 +00001285 ** For each expression, build an index key from the evaluation and
1286 ** store it in the temporary table. If <expr> is a column, then use
1287 ** that columns affinity when building index keys. If <expr> is not
1288 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001289 */
danielk1977e014a832004-05-17 10:48:57 +00001290 int i;
danielk1977e014a832004-05-17 10:48:57 +00001291 if( !affinity ){
1292 affinity = SQLITE_AFF_NUMERIC;
1293 }
danielk19770202b292004-06-09 09:55:16 +00001294 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001295
1296 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001297 for(i=0; i<pExpr->pList->nExpr; i++){
1298 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001299
1300 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001301 if( !sqlite3ExprIsConstant(pE2) ){
1302 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001303 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001304 return;
drh4794b982000-06-06 13:54:14 +00001305 }
danielk1977e014a832004-05-17 10:48:57 +00001306
1307 /* Evaluate the expression and insert it into the temp table */
1308 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001309 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001310 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001311 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001312 }
1313 }
danielk19770202b292004-06-09 09:55:16 +00001314 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001315 break;
drhfef52082000-06-06 01:50:43 +00001316 }
1317
drh51522cd2005-01-20 13:36:19 +00001318 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001319 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001320 /* This has to be a scalar SELECT. Generate code to put the
1321 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001322 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001323 */
drh51522cd2005-01-20 13:36:19 +00001324 int sop;
1325 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001326
drh967e8b72000-06-21 13:59:10 +00001327 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001328 pSel = pExpr->pSelect;
1329 if( pExpr->op==TK_SELECT ){
1330 sop = SRT_Mem;
1331 }else{
1332 static const Token one = { "1", 0, 1 };
1333 sop = SRT_Exists;
1334 sqlite3ExprListDelete(pSel->pEList);
1335 pSel->pEList = sqlite3ExprListAppend(0,
1336 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1337 }
danielk1977b3bce662005-01-29 08:32:43 +00001338 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1339 break;
drhcce7d172000-05-31 15:34:51 +00001340 }
1341 }
danielk1977b3bce662005-01-29 08:32:43 +00001342
1343 if( pExpr->pSelect ){
1344 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1345 }
1346 if( label<0 ){
1347 sqlite3VdbeResolveLabel(v, label);
1348 }
1349 return;
drhcce7d172000-05-31 15:34:51 +00001350}
drh51522cd2005-01-20 13:36:19 +00001351#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001352
drhcce7d172000-05-31 15:34:51 +00001353/*
drhfec19aa2004-05-19 20:41:03 +00001354** Generate an instruction that will put the integer describe by
1355** text z[0..n-1] on the stack.
1356*/
1357static void codeInteger(Vdbe *v, const char *z, int n){
1358 int i;
drh6fec0762004-05-30 01:38:43 +00001359 if( sqlite3GetInt32(z, &i) ){
1360 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1361 }else if( sqlite3FitsIn64Bits(z) ){
1362 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001363 }else{
1364 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1365 }
1366}
1367
1368/*
drhcce7d172000-05-31 15:34:51 +00001369** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001370** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001371**
1372** This code depends on the fact that certain token values (ex: TK_EQ)
1373** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1374** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1375** the make process cause these values to align. Assert()s in the code
1376** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001377*/
danielk19774adee202004-05-08 08:23:19 +00001378void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001379 Vdbe *v = pParse->pVdbe;
1380 int op;
danielk19777977a172004-11-09 12:44:37 +00001381 if( v==0 ) return;
1382 if( pExpr==0 ){
1383 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1384 return;
1385 }
drhf2bc0132004-10-04 13:19:23 +00001386 op = pExpr->op;
1387 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001388 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001389 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1390 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001391 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001392 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001393 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001394 }else{
danielk19774adee202004-05-08 08:23:19 +00001395 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001396 }
drhcce7d172000-05-31 15:34:51 +00001397 break;
1398 }
1399 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001400 codeInteger(v, pExpr->token.z, pExpr->token.n);
1401 break;
1402 }
1403 case TK_FLOAT:
1404 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001405 assert( TK_FLOAT==OP_Real );
1406 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001407 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001408 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001409 break;
1410 }
danielk19775338a5f2005-01-20 13:03:10 +00001411#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001412 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001413 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001414 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1415 sqlite3VdbeDequoteP3(v, -1);
1416 break;
1417 }
danielk19775338a5f2005-01-20 13:03:10 +00001418#endif
drhcce7d172000-05-31 15:34:51 +00001419 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001420 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001421 break;
1422 }
drh50457892003-09-06 01:10:47 +00001423 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001424 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001425 if( pExpr->token.n>1 ){
1426 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1427 }
drh50457892003-09-06 01:10:47 +00001428 break;
1429 }
drh4e0cff62004-11-05 05:10:28 +00001430 case TK_REGISTER: {
1431 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1432 break;
1433 }
drhc9b84a12002-06-20 11:36:48 +00001434 case TK_LT:
1435 case TK_LE:
1436 case TK_GT:
1437 case TK_GE:
1438 case TK_NE:
1439 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001440 assert( TK_LT==OP_Lt );
1441 assert( TK_LE==OP_Le );
1442 assert( TK_GT==OP_Gt );
1443 assert( TK_GE==OP_Ge );
1444 assert( TK_EQ==OP_Eq );
1445 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001446 sqlite3ExprCode(pParse, pExpr->pLeft);
1447 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001448 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001449 break;
drhc9b84a12002-06-20 11:36:48 +00001450 }
drhcce7d172000-05-31 15:34:51 +00001451 case TK_AND:
1452 case TK_OR:
1453 case TK_PLUS:
1454 case TK_STAR:
1455 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001456 case TK_REM:
1457 case TK_BITAND:
1458 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001459 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001460 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001461 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001462 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001463 assert( TK_AND==OP_And );
1464 assert( TK_OR==OP_Or );
1465 assert( TK_PLUS==OP_Add );
1466 assert( TK_MINUS==OP_Subtract );
1467 assert( TK_REM==OP_Remainder );
1468 assert( TK_BITAND==OP_BitAnd );
1469 assert( TK_BITOR==OP_BitOr );
1470 assert( TK_SLASH==OP_Divide );
1471 assert( TK_LSHIFT==OP_ShiftLeft );
1472 assert( TK_RSHIFT==OP_ShiftRight );
1473 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001474 sqlite3ExprCode(pParse, pExpr->pLeft);
1475 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001476 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001477 break;
1478 }
drhcce7d172000-05-31 15:34:51 +00001479 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001480 Expr *pLeft = pExpr->pLeft;
1481 assert( pLeft );
1482 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1483 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001484 char *z = sqliteMalloc( p->n + 2 );
1485 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001486 if( pLeft->op==TK_FLOAT ){
1487 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001488 }else{
drhfec19aa2004-05-19 20:41:03 +00001489 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001490 }
drh6e142f52000-06-08 13:36:40 +00001491 sqliteFree(z);
1492 break;
1493 }
drh1ccde152000-06-17 13:12:39 +00001494 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001495 }
drhbf4133c2001-10-13 02:59:08 +00001496 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001497 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001498 assert( TK_BITNOT==OP_BitNot );
1499 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001500 sqlite3ExprCode(pParse, pExpr->pLeft);
1501 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001502 break;
1503 }
1504 case TK_ISNULL:
1505 case TK_NOTNULL: {
1506 int dest;
drhf2bc0132004-10-04 13:19:23 +00001507 assert( TK_ISNULL==OP_IsNull );
1508 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001509 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1510 sqlite3ExprCode(pParse, pExpr->pLeft);
1511 dest = sqlite3VdbeCurrentAddr(v) + 2;
1512 sqlite3VdbeAddOp(v, op, 1, dest);
1513 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001514 break;
drhcce7d172000-05-31 15:34:51 +00001515 }
drh22827922000-06-06 17:27:05 +00001516 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001517 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001518 break;
1519 }
danielk19777977a172004-11-09 12:44:37 +00001520 case TK_CDATE:
1521 case TK_CTIME:
1522 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001523 case TK_GLOB:
1524 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001525 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001526 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001527 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001528 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001529 int nId;
1530 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001531 int p2 = 0;
1532 int i;
danielk1977d8123362004-06-12 09:25:12 +00001533 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001534 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001535 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001536 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001537 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001538 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001539 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001540 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1541 p2 |= (1<<i);
1542 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001543 if( pDef->needCollSeq && !pColl ){
1544 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1545 }
1546 }
1547 if( pDef->needCollSeq ){
1548 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001549 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001550 }
1551 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001552 break;
1553 }
drhfe2093d2005-01-20 22:48:47 +00001554#ifndef SQLITE_OMIT_SUBQUERY
1555 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001556 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001557 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001558 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001559 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001560 break;
1561 }
drhfef52082000-06-06 01:50:43 +00001562 case TK_IN: {
1563 int addr;
drh94a11212004-09-25 13:12:14 +00001564 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001565 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001566
1567 /* Figure out the affinity to use to create a key from the results
1568 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001569 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001570 */
drh94a11212004-09-25 13:12:14 +00001571 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001572
danielk19774adee202004-05-08 08:23:19 +00001573 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001574
1575 /* Code the <expr> from "<expr> IN (...)". The temporary table
1576 ** pExpr->iTable contains the values that make up the (...) set.
1577 */
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3ExprCode(pParse, pExpr->pLeft);
1579 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001580 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001582 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001583 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001584 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001585 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1586 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1587
drhfef52082000-06-06 01:50:43 +00001588 break;
1589 }
danielk197793758c82005-01-21 08:13:14 +00001590#endif
drhfef52082000-06-06 01:50:43 +00001591 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001592 Expr *pLeft = pExpr->pLeft;
1593 struct ExprList_item *pLItem = pExpr->pList->a;
1594 Expr *pRight = pLItem->pExpr;
1595 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001596 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001597 sqlite3ExprCode(pParse, pRight);
1598 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001599 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001600 pLItem++;
1601 pRight = pLItem->pExpr;
1602 sqlite3ExprCode(pParse, pRight);
1603 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001604 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001605 break;
1606 }
drh51e9a442004-01-16 16:42:53 +00001607 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001608 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001609 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001610 break;
1611 }
drh17a7f8d2002-03-24 13:13:27 +00001612 case TK_CASE: {
1613 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001614 int jumpInst;
1615 int addr;
1616 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001617 int i;
drhbe5c89a2004-07-26 00:31:09 +00001618 ExprList *pEList;
1619 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001620
1621 assert(pExpr->pList);
1622 assert((pExpr->pList->nExpr % 2) == 0);
1623 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001624 pEList = pExpr->pList;
1625 aListelem = pEList->a;
1626 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001627 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001628 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001629 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001630 }
drhf5905aa2002-05-26 20:54:33 +00001631 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001632 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001633 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001634 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001635 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1636 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001637 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001638 }else{
danielk19774adee202004-05-08 08:23:19 +00001639 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001640 }
drhbe5c89a2004-07-26 00:31:09 +00001641 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001642 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1643 addr = sqlite3VdbeCurrentAddr(v);
1644 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001645 }
drhf570f012002-05-31 15:51:25 +00001646 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001647 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001648 }
drh17a7f8d2002-03-24 13:13:27 +00001649 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001650 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001651 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001652 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001653 }
danielk19774adee202004-05-08 08:23:19 +00001654 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001655 break;
1656 }
danielk19775338a5f2005-01-20 13:03:10 +00001657#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001658 case TK_RAISE: {
1659 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001660 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001661 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001662 return;
1663 }
drhad6d9462004-09-19 02:15:24 +00001664 if( pExpr->iColumn!=OE_Ignore ){
1665 assert( pExpr->iColumn==OE_Rollback ||
1666 pExpr->iColumn == OE_Abort ||
1667 pExpr->iColumn == OE_Fail );
1668 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1669 pExpr->token.z, pExpr->token.n);
1670 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001671 } else {
drhad6d9462004-09-19 02:15:24 +00001672 assert( pExpr->iColumn == OE_Ignore );
1673 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1674 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1675 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001676 }
drh17a7f8d2002-03-24 13:13:27 +00001677 }
danielk19775338a5f2005-01-20 13:03:10 +00001678#endif
drh17a7f8d2002-03-24 13:13:27 +00001679 break;
drhcce7d172000-05-31 15:34:51 +00001680 }
drhcce7d172000-05-31 15:34:51 +00001681}
1682
danielk197793758c82005-01-21 08:13:14 +00001683#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001684/*
drh25303782004-12-07 15:41:48 +00001685** Generate code that evalutes the given expression and leaves the result
1686** on the stack. See also sqlite3ExprCode().
1687**
1688** This routine might also cache the result and modify the pExpr tree
1689** so that it will make use of the cached result on subsequent evaluations
1690** rather than evaluate the whole expression again. Trivial expressions are
1691** not cached. If the expression is cached, its result is stored in a
1692** memory location.
1693*/
1694void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1695 Vdbe *v = pParse->pVdbe;
1696 int iMem;
1697 int addr1, addr2;
1698 if( v==0 ) return;
1699 addr1 = sqlite3VdbeCurrentAddr(v);
1700 sqlite3ExprCode(pParse, pExpr);
1701 addr2 = sqlite3VdbeCurrentAddr(v);
1702 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1703 iMem = pExpr->iTable = pParse->nMem++;
1704 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1705 pExpr->op = TK_REGISTER;
1706 }
1707}
danielk197793758c82005-01-21 08:13:14 +00001708#endif
drh25303782004-12-07 15:41:48 +00001709
1710/*
drh268380c2004-02-25 13:47:31 +00001711** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001712** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001713**
1714** Return the number of elements pushed onto the stack.
1715*/
danielk19774adee202004-05-08 08:23:19 +00001716int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001717 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001718 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001719){
1720 struct ExprList_item *pItem;
1721 int i, n;
1722 Vdbe *v;
1723 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001724 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001725 n = pList->nExpr;
1726 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001727 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001728 }
drhf9b596e2004-05-26 16:54:42 +00001729 return n;
drh268380c2004-02-25 13:47:31 +00001730}
1731
1732/*
drhcce7d172000-05-31 15:34:51 +00001733** Generate code for a boolean expression such that a jump is made
1734** to the label "dest" if the expression is true but execution
1735** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001736**
1737** If the expression evaluates to NULL (neither true nor false), then
1738** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001739**
1740** This code depends on the fact that certain token values (ex: TK_EQ)
1741** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1742** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1743** the make process cause these values to align. Assert()s in the code
1744** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001745*/
danielk19774adee202004-05-08 08:23:19 +00001746void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001747 Vdbe *v = pParse->pVdbe;
1748 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001749 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001750 op = pExpr->op;
1751 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001752 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001753 int d2 = sqlite3VdbeMakeLabel(v);
1754 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1755 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1756 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001757 break;
1758 }
1759 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001760 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1761 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001762 break;
1763 }
1764 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001765 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001766 break;
1767 }
1768 case TK_LT:
1769 case TK_LE:
1770 case TK_GT:
1771 case TK_GE:
1772 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001773 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001774 assert( TK_LT==OP_Lt );
1775 assert( TK_LE==OP_Le );
1776 assert( TK_GT==OP_Gt );
1777 assert( TK_GE==OP_Ge );
1778 assert( TK_EQ==OP_Eq );
1779 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001780 sqlite3ExprCode(pParse, pExpr->pLeft);
1781 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001782 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001783 break;
1784 }
1785 case TK_ISNULL:
1786 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001787 assert( TK_ISNULL==OP_IsNull );
1788 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001789 sqlite3ExprCode(pParse, pExpr->pLeft);
1790 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001791 break;
1792 }
drhfef52082000-06-06 01:50:43 +00001793 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001794 /* The expression "x BETWEEN y AND z" is implemented as:
1795 **
1796 ** 1 IF (x < y) GOTO 3
1797 ** 2 IF (x <= z) GOTO <dest>
1798 ** 3 ...
1799 */
drhf5905aa2002-05-26 20:54:33 +00001800 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001801 Expr *pLeft = pExpr->pLeft;
1802 Expr *pRight = pExpr->pList->a[0].pExpr;
1803 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001804 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001805 sqlite3ExprCode(pParse, pRight);
1806 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001807
drhbe5c89a2004-07-26 00:31:09 +00001808 pRight = pExpr->pList->a[1].pExpr;
1809 sqlite3ExprCode(pParse, pRight);
1810 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001811
danielk19774adee202004-05-08 08:23:19 +00001812 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1813 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1814 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001815 break;
1816 }
drhcce7d172000-05-31 15:34:51 +00001817 default: {
danielk19774adee202004-05-08 08:23:19 +00001818 sqlite3ExprCode(pParse, pExpr);
1819 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001820 break;
1821 }
1822 }
1823}
1824
1825/*
drh66b89c82000-11-28 20:47:17 +00001826** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001827** to the label "dest" if the expression is false but execution
1828** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001829**
1830** If the expression evaluates to NULL (neither true nor false) then
1831** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001832*/
danielk19774adee202004-05-08 08:23:19 +00001833void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001834 Vdbe *v = pParse->pVdbe;
1835 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001836 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001837
1838 /* The value of pExpr->op and op are related as follows:
1839 **
1840 ** pExpr->op op
1841 ** --------- ----------
1842 ** TK_ISNULL OP_NotNull
1843 ** TK_NOTNULL OP_IsNull
1844 ** TK_NE OP_Eq
1845 ** TK_EQ OP_Ne
1846 ** TK_GT OP_Le
1847 ** TK_LE OP_Gt
1848 ** TK_GE OP_Lt
1849 ** TK_LT OP_Ge
1850 **
1851 ** For other values of pExpr->op, op is undefined and unused.
1852 ** The value of TK_ and OP_ constants are arranged such that we
1853 ** can compute the mapping above using the following expression.
1854 ** Assert()s verify that the computation is correct.
1855 */
1856 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1857
1858 /* Verify correct alignment of TK_ and OP_ constants
1859 */
1860 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1861 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1862 assert( pExpr->op!=TK_NE || op==OP_Eq );
1863 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1864 assert( pExpr->op!=TK_LT || op==OP_Ge );
1865 assert( pExpr->op!=TK_LE || op==OP_Gt );
1866 assert( pExpr->op!=TK_GT || op==OP_Le );
1867 assert( pExpr->op!=TK_GE || op==OP_Lt );
1868
drhcce7d172000-05-31 15:34:51 +00001869 switch( pExpr->op ){
1870 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001871 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1872 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001873 break;
1874 }
1875 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001876 int d2 = sqlite3VdbeMakeLabel(v);
1877 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1878 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1879 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001880 break;
1881 }
1882 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001883 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001884 break;
1885 }
1886 case TK_LT:
1887 case TK_LE:
1888 case TK_GT:
1889 case TK_GE:
1890 case TK_NE:
1891 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001892 sqlite3ExprCode(pParse, pExpr->pLeft);
1893 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001894 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001895 break;
1896 }
drhcce7d172000-05-31 15:34:51 +00001897 case TK_ISNULL:
1898 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001899 sqlite3ExprCode(pParse, pExpr->pLeft);
1900 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001901 break;
1902 }
drhfef52082000-06-06 01:50:43 +00001903 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001904 /* The expression is "x BETWEEN y AND z". It is implemented as:
1905 **
1906 ** 1 IF (x >= y) GOTO 3
1907 ** 2 GOTO <dest>
1908 ** 3 IF (x > z) GOTO <dest>
1909 */
drhfef52082000-06-06 01:50:43 +00001910 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001911 Expr *pLeft = pExpr->pLeft;
1912 Expr *pRight = pExpr->pList->a[0].pExpr;
1913 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001914 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001915 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001916 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001917 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1918
danielk19774adee202004-05-08 08:23:19 +00001919 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1920 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001921 pRight = pExpr->pList->a[1].pExpr;
1922 sqlite3ExprCode(pParse, pRight);
1923 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001924 break;
1925 }
drhcce7d172000-05-31 15:34:51 +00001926 default: {
danielk19774adee202004-05-08 08:23:19 +00001927 sqlite3ExprCode(pParse, pExpr);
1928 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001929 break;
1930 }
1931 }
1932}
drh22827922000-06-06 17:27:05 +00001933
1934/*
1935** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1936** if they are identical and return FALSE if they differ in any way.
1937*/
danielk19774adee202004-05-08 08:23:19 +00001938int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001939 int i;
1940 if( pA==0 ){
1941 return pB==0;
1942 }else if( pB==0 ){
1943 return 0;
1944 }
1945 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001946 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1947 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001948 if( pA->pList ){
1949 if( pB->pList==0 ) return 0;
1950 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1951 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001952 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001953 return 0;
1954 }
1955 }
1956 }else if( pB->pList ){
1957 return 0;
1958 }
1959 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001960 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001961 if( pA->token.z ){
1962 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001963 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001964 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001965 }
1966 return 1;
1967}
1968
1969/*
1970** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001971** The new element is initialized to zero. The calling function is
1972** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001973*/
1974static int appendAggInfo(Parse *pParse){
1975 if( (pParse->nAgg & 0x7)==0 ){
1976 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001977 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1978 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001979 return -1;
1980 }
drh6d4abfb2001-10-22 02:58:08 +00001981 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001982 }
1983 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1984 return pParse->nAgg++;
1985}
1986
1987/*
drh626a8792005-01-17 22:08:19 +00001988** This is an xFunc for walkExprTree() used to implement
1989** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1990** for additional information.
drh22827922000-06-06 17:27:05 +00001991**
drh626a8792005-01-17 22:08:19 +00001992** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001993*/
drh626a8792005-01-17 22:08:19 +00001994static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001995 int i;
1996 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00001997 NameContext *pNC = (NameContext *)pArg;
1998 Parse *pParse = pNC->pParse;
1999 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00002000
drh22827922000-06-06 17:27:05 +00002001 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002002 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002003 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2004 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2005 aAgg = pParse->aAgg;
2006 for(i=0; i<pParse->nAgg; i++){
2007 if( aAgg[i].isAgg ) continue;
2008 if( aAgg[i].pExpr->iTable==pExpr->iTable
2009 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2010 break;
2011 }
2012 }
2013 if( i>=pParse->nAgg ){
2014 i = appendAggInfo(pParse);
2015 if( i<0 ) return 1;
2016 pParse->aAgg[i].isAgg = 0;
2017 pParse->aAgg[i].pExpr = pExpr;
2018 }
2019 pExpr->iAgg = i;
2020 pExpr->iAggCtx = pNC->nDepth;
2021 return 1;
drh22827922000-06-06 17:27:05 +00002022 }
2023 }
drh626a8792005-01-17 22:08:19 +00002024 return 1;
drh22827922000-06-06 17:27:05 +00002025 }
2026 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002027 if( pNC->nDepth==0 ){
2028 aAgg = pParse->aAgg;
2029 for(i=0; i<pParse->nAgg; i++){
2030 if( !aAgg[i].isAgg ) continue;
2031 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2032 break;
2033 }
drh22827922000-06-06 17:27:05 +00002034 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002035 if( i>=pParse->nAgg ){
2036 u8 enc = pParse->db->enc;
2037 i = appendAggInfo(pParse);
2038 if( i<0 ) return 1;
2039 pParse->aAgg[i].isAgg = 1;
2040 pParse->aAgg[i].pExpr = pExpr;
2041 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2042 pExpr->token.z, pExpr->token.n,
2043 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2044 }
2045 pExpr->iAgg = i;
2046 return 1;
drh22827922000-06-06 17:27:05 +00002047 }
drh22827922000-06-06 17:27:05 +00002048 }
2049 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002050 if( pExpr->pSelect ){
2051 pNC->nDepth++;
2052 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2053 pNC->nDepth--;
2054 }
drh626a8792005-01-17 22:08:19 +00002055 return 0;
2056}
2057
2058/*
2059** Analyze the given expression looking for aggregate functions and
2060** for variables that need to be added to the pParse->aAgg[] array.
2061** Make additional entries to the pParse->aAgg[] array as necessary.
2062**
2063** This routine should only be called after the expression has been
2064** analyzed by sqlite3ExprResolveNames().
2065**
2066** If errors are seen, leave an error message in zErrMsg and return
2067** the number of errors.
2068*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002069int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2070 int nErr = pNC->pParse->nErr;
2071 walkExprTree(pExpr, analyzeAggregate, pNC);
2072 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002073}
drh8e0a2f92002-02-23 23:45:45 +00002074
2075/*
danielk1977d02eb1f2004-06-06 09:44:03 +00002076** Locate a user function given a name, a number of arguments and a flag
2077** indicating whether the function prefers UTF-16 over UTF-8. Return a
2078** pointer to the FuncDef structure that defines that function, or return
2079** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00002080**
drh0bce8352002-02-28 00:41:10 +00002081** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00002082** structure is created and liked into the "db" structure if a
2083** no matching function previously existed. When createFlag is true
2084** and the nArg parameter is -1, then only a function that accepts
2085** any number of arguments will be returned.
2086**
2087** If createFlag is false and nArg is -1, then the first valid
2088** function found is returned. A function is valid if either xFunc
2089** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00002090**
2091** If createFlag is false, then a function with the required name and
2092** number of arguments may be returned even if the eTextRep flag does not
2093** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00002094*/
danielk19774adee202004-05-08 08:23:19 +00002095FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00002096 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00002097 const char *zName, /* Name of the function. Not null-terminated */
2098 int nName, /* Number of characters in the name */
2099 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00002100 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00002101 int createFlag /* Create new entry if true and does not otherwise exist */
2102){
danielk1977d02eb1f2004-06-06 09:44:03 +00002103 FuncDef *p; /* Iterator variable */
2104 FuncDef *pFirst; /* First function with this name */
2105 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00002106 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00002107
danielk1977d8123362004-06-12 09:25:12 +00002108
2109 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00002110 if( nArg<-1 ) nArg = -1;
2111
2112 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
2113 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00002114 /* During the search for the best function definition, bestmatch is set
2115 ** as follows to indicate the quality of the match with the definition
2116 ** pointed to by pBest:
2117 **
2118 ** 0: pBest is NULL. No match has been found.
2119 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
2120 ** encoding is requested, or vice versa.
2121 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
2122 ** requested, or vice versa.
2123 ** 3: A variable arguments function using the same text encoding.
2124 ** 4: A function with the exact number of arguments requested that
2125 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
2126 ** 5: A function with the exact number of arguments requested that
2127 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
2128 ** 6: An exact match.
2129 **
2130 ** A larger value of 'matchqual' indicates a more desirable match.
2131 */
danielk1977e12c17b2004-06-23 12:35:14 +00002132 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00002133 int match = 1; /* Quality of this match */
2134 if( p->nArg==nArg || nArg==-1 ){
2135 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00002136 }
danielk1977d8123362004-06-12 09:25:12 +00002137 if( enc==p->iPrefEnc ){
2138 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00002139 }
danielk1977d8123362004-06-12 09:25:12 +00002140 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2141 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2142 match += 1;
2143 }
2144
2145 if( match>bestmatch ){
2146 pBest = p;
2147 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002148 }
2149 }
drh8e0a2f92002-02-23 23:45:45 +00002150 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002151
danielk1977d8123362004-06-12 09:25:12 +00002152 /* If the createFlag parameter is true, and the seach did not reveal an
2153 ** exact match for the name, number of arguments and encoding, then add a
2154 ** new entry to the hash table and return it.
2155 */
2156 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002157 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2158 pBest->nArg = nArg;
2159 pBest->pNext = pFirst;
2160 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002161 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002162 memcpy(pBest->zName, zName, nName);
2163 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002164 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2165 sqliteFree(pBest);
2166 return 0;
2167 }
drh8e0a2f92002-02-23 23:45:45 +00002168 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002169
2170 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2171 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002172 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002173 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002174}