blob: 487c5395f5f243e99849cdcd717439c2d5ed7a48 [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**
danielk1977a58fdfb2005-02-08 07:50:40 +000015** $Id: expr.c,v 1.193 2005/02/08 07:50:41 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);
drhff78bd22002-02-27 01:47:11 +0000406 return pNew;
407}
danielk19774adee202004-05-08 08:23:19 +0000408void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000409 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000410 if( pFrom->z ){
411 pTo->n = pFrom->n;
412 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
413 pTo->dyn = 1;
414 }else{
drh4b59ab52002-08-24 18:24:51 +0000415 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000416 }
417}
danielk19774adee202004-05-08 08:23:19 +0000418ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000419 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000420 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000421 int i;
422 if( p==0 ) return 0;
423 pNew = sqliteMalloc( sizeof(*pNew) );
424 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000425 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000426 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000427 if( pItem==0 ){
428 sqliteFree(pNew);
429 return 0;
430 }
drh145716b2004-09-24 12:24:06 +0000431 pOldItem = p->a;
432 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000433 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000434 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000435 if( pOldExpr->span.z!=0 && pNewExpr ){
436 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000437 ** expression list. The logic in SELECT processing that determines
438 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000439 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000440 }
drh1f3e9052002-10-31 00:09:39 +0000441 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000442 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000443 pItem->zName = sqliteStrDup(pOldItem->zName);
444 pItem->sortOrder = pOldItem->sortOrder;
445 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000446 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000447 }
448 return pNew;
449}
danielk197793758c82005-01-21 08:13:14 +0000450
451/*
452** If cursors, triggers, views and subqueries are all omitted from
453** the build, then none of the following routines, except for
454** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
455** called with a NULL argument.
456*/
danielk19776a67fe82005-02-04 04:07:16 +0000457#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
458 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000459SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000460 SrcList *pNew;
461 int i;
drh113088e2003-03-20 01:16:58 +0000462 int nByte;
drhad3cab52002-05-24 02:04:32 +0000463 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000464 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000465 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000466 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000467 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000468 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000469 struct SrcList_item *pNewItem = &pNew->a[i];
470 struct SrcList_item *pOldItem = &p->a[i];
471 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
472 pNewItem->zName = sqliteStrDup(pOldItem->zName);
473 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
474 pNewItem->jointype = pOldItem->jointype;
475 pNewItem->iCursor = pOldItem->iCursor;
476 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000477 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
478 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
479 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000480 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000481 }
482 return pNew;
483}
danielk19774adee202004-05-08 08:23:19 +0000484IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000485 IdList *pNew;
486 int i;
487 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000488 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000489 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000490 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000491 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000492 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000493 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000494 struct IdList_item *pNewItem = &pNew->a[i];
495 struct IdList_item *pOldItem = &p->a[i];
496 pNewItem->zName = sqliteStrDup(pOldItem->zName);
497 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000498 }
499 return pNew;
500}
danielk19774adee202004-05-08 08:23:19 +0000501Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000502 Select *pNew;
503 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000504 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000505 if( pNew==0 ) return 0;
506 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000507 pNew->pEList = sqlite3ExprListDup(p->pEList);
508 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
509 pNew->pWhere = sqlite3ExprDup(p->pWhere);
510 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
511 pNew->pHaving = sqlite3ExprDup(p->pHaving);
512 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000513 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000514 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000515 pNew->pLimit = sqlite3ExprDup(p->pLimit);
516 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000517 pNew->iLimit = -1;
518 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000519 pNew->ppOpenTemp = 0;
drhb6c29892004-11-22 19:12:19 +0000520 pNew->pFetch = 0;
danielk1977b3bce662005-01-29 08:32:43 +0000521 pNew->isResolved = 0;
522 pNew->isAgg = 0;
drhff78bd22002-02-27 01:47:11 +0000523 return pNew;
524}
danielk197793758c82005-01-21 08:13:14 +0000525#else
526Select *sqlite3SelectDup(Select *p){
527 assert( p==0 );
528 return 0;
529}
530#endif
drhff78bd22002-02-27 01:47:11 +0000531
532
533/*
drha76b5df2002-02-23 02:32:10 +0000534** Add a new element to the end of an expression list. If pList is
535** initially NULL, then create a new expression list.
536*/
danielk19774adee202004-05-08 08:23:19 +0000537ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000538 if( pList==0 ){
539 pList = sqliteMalloc( sizeof(ExprList) );
540 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000541 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000542 return 0;
543 }
drh4efc4752004-01-16 15:55:37 +0000544 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000545 }
drh4305d102003-07-30 12:34:12 +0000546 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000547 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000548 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
549 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000550 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000551 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000552 return pList;
553 }
drha76b5df2002-02-23 02:32:10 +0000554 }
drh4efc4752004-01-16 15:55:37 +0000555 assert( pList->a!=0 );
556 if( pExpr || pName ){
557 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
558 memset(pItem, 0, sizeof(*pItem));
559 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000560 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000561 }
562 return pList;
563}
564
565/*
566** Delete an entire expression list.
567*/
danielk19774adee202004-05-08 08:23:19 +0000568void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000569 int i;
drhbe5c89a2004-07-26 00:31:09 +0000570 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000571 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000572 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
573 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000574 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
575 sqlite3ExprDelete(pItem->pExpr);
576 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000577 }
578 sqliteFree(pList->a);
579 sqliteFree(pList);
580}
581
582/*
drh626a8792005-01-17 22:08:19 +0000583** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000584**
drh626a8792005-01-17 22:08:19 +0000585** The return value from xFunc determines whether the tree walk continues.
586** 0 means continue walking the tree. 1 means do not walk children
587** of the current node but continue with siblings. 2 means abandon
588** the tree walk completely.
589**
590** The return value from this routine is 1 to abandon the tree walk
591** and 0 to continue.
592*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000593static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000594static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000595 int rc;
596 if( pExpr==0 ) return 0;
597 rc = (*xFunc)(pArg, pExpr);
598 if( rc==0 ){
599 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
600 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000601 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000602 }
603 return rc>1;
604}
605
606/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000607** Call walkExprTree() for every expression in list p.
608*/
609static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
610 int i;
611 struct ExprList_item *pItem;
612 if( !p ) return 0;
613 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
614 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
615 }
616 return 0;
617}
618
619/*
620** Call walkExprTree() for every expression in Select p, not including
621** expressions that are part of sub-selects in any FROM clause or the LIMIT
622** or OFFSET expressions..
623*/
624static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
625 walkExprList(p->pEList, xFunc, pArg);
626 walkExprTree(p->pWhere, xFunc, pArg);
627 walkExprList(p->pGroupBy, xFunc, pArg);
628 walkExprTree(p->pHaving, xFunc, pArg);
629 walkExprList(p->pOrderBy, xFunc, pArg);
630 return 0;
631}
632
633
634/*
drh626a8792005-01-17 22:08:19 +0000635** This routine is designed as an xFunc for walkExprTree().
636**
637** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000638** at pExpr that the expression that contains pExpr is not a constant
639** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
640** If pExpr does does not disqualify the expression from being a constant
641** then do nothing.
642**
643** After walking the whole tree, if no nodes are found that disqualify
644** the expression as constant, then we assume the whole expression
645** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000646*/
647static int exprNodeIsConstant(void *pArg, Expr *pExpr){
648 switch( pExpr->op ){
649 case TK_ID:
650 case TK_COLUMN:
651 case TK_DOT:
652 case TK_AGG_FUNCTION:
653 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000654#ifndef SQLITE_OMIT_SUBQUERY
655 case TK_SELECT:
656 case TK_EXISTS:
657#endif
drh626a8792005-01-17 22:08:19 +0000658 *((int*)pArg) = 0;
659 return 2;
660 default:
661 return 0;
662 }
663}
664
665/*
drhfef52082000-06-06 01:50:43 +0000666** Walk an expression tree. Return 1 if the expression is constant
667** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000668**
669** For the purposes of this function, a double-quoted string (ex: "abc")
670** is considered a variable but a single-quoted string (ex: 'abc') is
671** a constant.
drhfef52082000-06-06 01:50:43 +0000672*/
danielk19774adee202004-05-08 08:23:19 +0000673int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000674 int isConst = 1;
675 walkExprTree(p, exprNodeIsConstant, &isConst);
676 return isConst;
drhfef52082000-06-06 01:50:43 +0000677}
678
679/*
drh73b211a2005-01-18 04:00:42 +0000680** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000681** to fit in a 32-bit integer, return 1 and put the value of the integer
682** in *pValue. If the expression is not an integer or if it is too big
683** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000684*/
danielk19774adee202004-05-08 08:23:19 +0000685int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000686 switch( p->op ){
687 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000688 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000689 return 1;
690 }
691 break;
drhe4de1fe2002-06-02 16:09:01 +0000692 }
drh4b59ab52002-08-24 18:24:51 +0000693 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000694 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000695 }
drhe4de1fe2002-06-02 16:09:01 +0000696 case TK_UMINUS: {
697 int v;
danielk19774adee202004-05-08 08:23:19 +0000698 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000699 *pValue = -v;
700 return 1;
701 }
702 break;
703 }
704 default: break;
705 }
706 return 0;
707}
708
709/*
drhc4a3c772001-04-04 11:48:57 +0000710** Return TRUE if the given string is a row-id column name.
711*/
danielk19774adee202004-05-08 08:23:19 +0000712int sqlite3IsRowid(const char *z){
713 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
714 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
715 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000716 return 0;
717}
718
719/*
drh8141f612004-01-25 22:44:58 +0000720** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
721** that name in the set of source tables in pSrcList and make the pExpr
722** expression node refer back to that source column. The following changes
723** are made to pExpr:
724**
725** pExpr->iDb Set the index in db->aDb[] of the database holding
726** the table.
727** pExpr->iTable Set to the cursor number for the table obtained
728** from pSrcList.
729** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000730** pExpr->op Set to TK_COLUMN.
731** pExpr->pLeft Any expression this points to is deleted
732** pExpr->pRight Any expression this points to is deleted.
733**
734** The pDbToken is the name of the database (the "X"). This value may be
735** NULL meaning that name is of the form Y.Z or Z. Any available database
736** can be used. The pTableToken is the name of the table (the "Y"). This
737** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
738** means that the form of the name is Z and that columns from any table
739** can be used.
740**
741** If the name cannot be resolved unambiguously, leave an error message
742** in pParse and return non-zero. Return zero on success.
743*/
744static int lookupName(
745 Parse *pParse, /* The parsing context */
746 Token *pDbToken, /* Name of the database containing table, or NULL */
747 Token *pTableToken, /* Name of table containing column, or NULL */
748 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000749 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000750 Expr *pExpr /* Make this EXPR node point to the selected column */
751){
752 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
753 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
754 char *zCol = 0; /* Name of the column. The "Z" */
755 int i, j; /* Loop counters */
756 int cnt = 0; /* Number of matching column names */
757 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000758 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000759 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
760 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000761 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000762
763 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000764 zDb = sqlite3NameFromToken(pDbToken);
765 zTab = sqlite3NameFromToken(pTableToken);
766 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000767 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000768 return 1; /* Leak memory (zDb and zTab) if malloc fails */
769 }
drh8141f612004-01-25 22:44:58 +0000770
771 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000772 while( pNC && cnt==0 ){
773 SrcList *pSrcList = pNC->pSrcList;
774 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000775
drh626a8792005-01-17 22:08:19 +0000776 pNC->nRef++;
777 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000778 if( pSrcList ){
779 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
780 Table *pTab = pItem->pTab;
781 Column *pCol;
782
783 if( pTab==0 ) continue;
784 assert( pTab->nCol>0 );
785 if( zTab ){
786 if( pItem->zAlias ){
787 char *zTabName = pItem->zAlias;
788 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
789 }else{
790 char *zTabName = pTab->zName;
791 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
792 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
793 continue;
794 }
drh626a8792005-01-17 22:08:19 +0000795 }
drh8141f612004-01-25 22:44:58 +0000796 }
danielk1977b3bce662005-01-29 08:32:43 +0000797 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000798 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000799 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000800 pMatch = pItem;
801 }
802 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
803 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
804 cnt++;
805 pExpr->iTable = pItem->iCursor;
806 pMatch = pItem;
807 pExpr->iDb = pTab->iDb;
808 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
809 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
810 pExpr->affinity = pTab->aCol[j].affinity;
811 pExpr->pColl = pTab->aCol[j].pColl;
812 break;
813 }
drh8141f612004-01-25 22:44:58 +0000814 }
815 }
816 }
drh626a8792005-01-17 22:08:19 +0000817
818#ifndef SQLITE_OMIT_TRIGGER
819 /* If we have not already resolved the name, then maybe
820 ** it is a new.* or old.* trigger argument reference
821 */
822 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
823 TriggerStack *pTriggerStack = pParse->trigStack;
824 Table *pTab = 0;
825 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
826 pExpr->iTable = pTriggerStack->newIdx;
827 assert( pTriggerStack->pTab );
828 pTab = pTriggerStack->pTab;
829 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
830 pExpr->iTable = pTriggerStack->oldIdx;
831 assert( pTriggerStack->pTab );
832 pTab = pTriggerStack->pTab;
833 }
834
835 if( pTab ){
836 int j;
837 Column *pCol = pTab->aCol;
838
839 pExpr->iDb = pTab->iDb;
840 cntTab++;
841 for(j=0; j < pTab->nCol; j++, pCol++) {
842 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
843 cnt++;
844 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
845 pExpr->affinity = pTab->aCol[j].affinity;
846 pExpr->pColl = pTab->aCol[j].pColl;
847 break;
848 }
849 }
850 }
851 }
drhb7f91642004-10-31 02:22:47 +0000852#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000853
drh626a8792005-01-17 22:08:19 +0000854 /*
855 ** Perhaps the name is a reference to the ROWID
856 */
857 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
858 cnt = 1;
859 pExpr->iColumn = -1;
860 pExpr->affinity = SQLITE_AFF_INTEGER;
861 }
drh8141f612004-01-25 22:44:58 +0000862
drh626a8792005-01-17 22:08:19 +0000863 /*
864 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
865 ** might refer to an result-set alias. This happens, for example, when
866 ** we are resolving names in the WHERE clause of the following command:
867 **
868 ** SELECT a+b AS x FROM table WHERE x<10;
869 **
870 ** In cases like this, replace pExpr with a copy of the expression that
871 ** forms the result set entry ("a+b" in the example) and return immediately.
872 ** Note that the expression in the result set should have already been
873 ** resolved by the time the WHERE clause is resolved.
874 */
drh79d5f632005-01-18 17:20:10 +0000875 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000876 for(j=0; j<pEList->nExpr; j++){
877 char *zAs = pEList->a[j].zName;
878 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
879 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
880 pExpr->op = TK_AS;
881 pExpr->iColumn = j;
882 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
883 sqliteFree(zCol);
884 assert( zTab==0 && zDb==0 );
885 return 0;
886 }
887 }
888 }
889
890 /* Advance to the next name context. The loop will exit when either
891 ** we have a match (cnt>0) or when we run out of name contexts.
892 */
893 if( cnt==0 ){
894 pNC = pNC->pNext;
895 }
drh8141f612004-01-25 22:44:58 +0000896 }
897
898 /*
899 ** If X and Y are NULL (in other words if only the column name Z is
900 ** supplied) and the value of Z is enclosed in double-quotes, then
901 ** Z is a string literal if it doesn't match any column names. In that
902 ** case, we need to return right away and not make any changes to
903 ** pExpr.
904 */
905 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
906 sqliteFree(zCol);
907 return 0;
908 }
909
910 /*
911 ** cnt==0 means there was not match. cnt>1 means there were two or
912 ** more matches. Either way, we have an error.
913 */
914 if( cnt!=1 ){
915 char *z = 0;
916 char *zErr;
917 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
918 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000919 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000920 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000921 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000922 }else{
923 z = sqliteStrDup(zCol);
924 }
danielk19774adee202004-05-08 08:23:19 +0000925 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000926 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000927 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000928 }
929
drh51669862004-12-18 18:40:26 +0000930 /* If a column from a table in pSrcList is referenced, then record
931 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
932 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
933 ** column number is greater than the number of bits in the bitmask
934 ** then set the high-order bit of the bitmask.
935 */
936 if( pExpr->iColumn>=0 && pMatch!=0 ){
937 int n = pExpr->iColumn;
938 if( n>=sizeof(Bitmask)*8 ){
939 n = sizeof(Bitmask)*8-1;
940 }
941 assert( pMatch->iCursor==pExpr->iTable );
942 pMatch->colUsed |= 1<<n;
943 }
944
drh8141f612004-01-25 22:44:58 +0000945 /* Clean up and return
946 */
947 sqliteFree(zDb);
948 sqliteFree(zTab);
949 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000950 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000951 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000952 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000953 pExpr->pRight = 0;
954 pExpr->op = TK_COLUMN;
drh626a8792005-01-17 22:08:19 +0000955 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000956 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000957 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
958 }
drh8141f612004-01-25 22:44:58 +0000959 return cnt!=1;
960}
961
962/*
drh626a8792005-01-17 22:08:19 +0000963** pExpr is a node that defines a function of some kind. It might
964** be a syntactic function like "count(x)" or it might be a function
965** that implements an operator, like "a LIKE b".
966**
967** This routine makes *pzName point to the name of the function and
968** *pnName hold the number of characters in the function name.
969*/
970static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
971 switch( pExpr->op ){
972 case TK_FUNCTION: {
973 *pzName = pExpr->token.z;
974 *pnName = pExpr->token.n;
975 break;
976 }
977 case TK_LIKE: {
978 *pzName = "like";
979 *pnName = 4;
980 break;
981 }
982 case TK_GLOB: {
983 *pzName = "glob";
984 *pnName = 4;
985 break;
986 }
987 case TK_CTIME: {
988 *pzName = "current_time";
989 *pnName = 12;
990 break;
991 }
992 case TK_CDATE: {
993 *pzName = "current_date";
994 *pnName = 12;
995 break;
996 }
997 case TK_CTIMESTAMP: {
998 *pzName = "current_timestamp";
999 *pnName = 17;
1000 break;
1001 }
drh626a8792005-01-17 22:08:19 +00001002 }
1003}
1004
1005/*
1006** This routine is designed as an xFunc for walkExprTree().
1007**
drh73b211a2005-01-18 04:00:42 +00001008** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001009** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001010** the tree or 2 to abort the tree walk.
1011**
1012** This routine also does error checking and name resolution for
1013** function names. The operator for aggregate functions is changed
1014** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001015*/
1016static int nameResolverStep(void *pArg, Expr *pExpr){
1017 NameContext *pNC = (NameContext*)pArg;
1018 SrcList *pSrcList;
1019 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001020
danielk1977b3bce662005-01-29 08:32:43 +00001021 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001022 assert( pNC!=0 );
1023 pSrcList = pNC->pSrcList;
1024 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001025
drh626a8792005-01-17 22:08:19 +00001026 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1027 ExprSetProperty(pExpr, EP_Resolved);
1028#ifndef NDEBUG
1029 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001030 int i;
drh626a8792005-01-17 22:08:19 +00001031 for(i=0; i<pSrcList->nSrc; i++){
1032 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1033 }
1034 }
1035#endif
1036 switch( pExpr->op ){
1037 /* Double-quoted strings (ex: "abc") are used as identifiers if
1038 ** possible. Otherwise they remain as strings. Single-quoted
1039 ** strings (ex: 'abc') are always string literals.
1040 */
1041 case TK_STRING: {
1042 if( pExpr->token.z[0]=='\'' ) break;
1043 /* Fall thru into the TK_ID case if this is a double-quoted string */
1044 }
1045 /* A lone identifier is the name of a column.
1046 */
1047 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001048 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1049 return 1;
1050 }
1051
1052 /* A table name and column name: ID.ID
1053 ** Or a database, table and column: ID.ID.ID
1054 */
1055 case TK_DOT: {
1056 Token *pColumn;
1057 Token *pTable;
1058 Token *pDb;
1059 Expr *pRight;
1060
danielk1977b3bce662005-01-29 08:32:43 +00001061 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001062 pRight = pExpr->pRight;
1063 if( pRight->op==TK_ID ){
1064 pDb = 0;
1065 pTable = &pExpr->pLeft->token;
1066 pColumn = &pRight->token;
1067 }else{
1068 assert( pRight->op==TK_DOT );
1069 pDb = &pExpr->pLeft->token;
1070 pTable = &pRight->pLeft->token;
1071 pColumn = &pRight->pRight->token;
1072 }
1073 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1074 return 1;
1075 }
1076
1077 /* Resolve function names
1078 */
1079 case TK_CTIME:
1080 case TK_CTIMESTAMP:
1081 case TK_CDATE:
drh626a8792005-01-17 22:08:19 +00001082 case TK_GLOB:
1083 case TK_LIKE:
1084 case TK_FUNCTION: {
1085 ExprList *pList = pExpr->pList; /* The argument list */
1086 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1087 int no_such_func = 0; /* True if no such function exists */
1088 int wrong_num_args = 0; /* True if wrong number of arguments */
1089 int is_agg = 0; /* True if is an aggregate function */
1090 int i;
1091 int nId; /* Number of characters in function name */
1092 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001093 FuncDef *pDef; /* Information about the function */
1094 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001095
1096 getFunctionName(pExpr, &zId, &nId);
1097 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1098 if( pDef==0 ){
1099 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1100 if( pDef==0 ){
1101 no_such_func = 1;
1102 }else{
1103 wrong_num_args = 1;
1104 }
1105 }else{
1106 is_agg = pDef->xFunc==0;
1107 }
1108 if( is_agg && !pNC->allowAgg ){
1109 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1110 pNC->nErr++;
1111 is_agg = 0;
1112 }else if( no_such_func ){
1113 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1114 pNC->nErr++;
1115 }else if( wrong_num_args ){
1116 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1117 nId, zId);
1118 pNC->nErr++;
1119 }
1120 if( is_agg ){
1121 pExpr->op = TK_AGG_FUNCTION;
1122 pNC->hasAgg = 1;
1123 }
drh73b211a2005-01-18 04:00:42 +00001124 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001125 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001126 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001127 }
drh73b211a2005-01-18 04:00:42 +00001128 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001129 /* FIX ME: Compute pExpr->affinity based on the expected return
1130 ** type of the function
1131 */
1132 return is_agg;
1133 }
danielk1977b3bce662005-01-29 08:32:43 +00001134#ifndef SQLITE_OMIT_SUBQUERY
1135 case TK_SELECT:
1136 case TK_EXISTS:
1137#endif
1138 case TK_IN: {
1139 if( pExpr->pSelect ){
1140 int nRef = pNC->nRef;
1141 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1142 assert( pNC->nRef>=nRef );
1143 if( nRef!=pNC->nRef ){
1144 ExprSetProperty(pExpr, EP_VarSelect);
1145 }
1146 }
1147 }
drh626a8792005-01-17 22:08:19 +00001148 }
1149 return 0;
1150}
1151
1152/*
drhcce7d172000-05-31 15:34:51 +00001153** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001154** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001155** index to the table in the table list and a column offset. The
1156** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1157** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001158** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001159** VDBE cursor number for a cursor that is pointing into the referenced
1160** table. The Expr.iColumn value is changed to the index of the column
1161** of the referenced table. The Expr.iColumn value for the special
1162** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1163** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001164**
drh626a8792005-01-17 22:08:19 +00001165** Also resolve function names and check the functions for proper
1166** usage. Make sure all function names are recognized and all functions
1167** have the correct number of arguments. Leave an error message
1168** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1169**
drh73b211a2005-01-18 04:00:42 +00001170** If the expression contains aggregate functions then set the EP_Agg
1171** property on the expression.
drh626a8792005-01-17 22:08:19 +00001172*/
1173int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001174 NameContext *pNC, /* Namespace to resolve expressions in. */
1175 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001176){
drh73b211a2005-01-18 04:00:42 +00001177 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001178 walkExprTree(pExpr, nameResolverStep, pNC);
1179 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001180 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001181 }
1182 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001183}
1184
drh1398ad32005-01-19 23:24:50 +00001185/*
1186** A pointer instance of this structure is used to pass information
1187** through walkExprTree into codeSubqueryStep().
1188*/
1189typedef struct QueryCoder QueryCoder;
1190struct QueryCoder {
1191 Parse *pParse; /* The parsing context */
1192 NameContext *pNC; /* Namespace of first enclosing query */
1193};
1194
drh626a8792005-01-17 22:08:19 +00001195
1196/*
1197** Generate code for subqueries and IN operators.
1198**
drh73b211a2005-01-18 04:00:42 +00001199** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001200**
1201** expr IN (exprlist)
1202** and
1203** expr IN (SELECT ...)
1204**
1205** The first form is handled by creating a set holding the list
1206** of allowed values. The second form causes the SELECT to generate
1207** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001208*/
drh51522cd2005-01-20 13:36:19 +00001209#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001210void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1211 int label = 0; /* Address after sub-select code */
1212 Vdbe *v = sqlite3GetVdbe(pParse);
1213 if( v==0 ) return;
1214
1215 /* If this is not a variable (correlated) select, then execute
1216 ** it only once. Unless this is part of a trigger program. In
1217 ** that case re-execute every time (this could be optimized).
1218 */
1219 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1220 int mem = pParse->nMem++;
1221 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1222 label = sqlite3VdbeMakeLabel(v);
1223 sqlite3VdbeAddOp(v, OP_If, 0, label);
1224 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1225 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1226 }
1227
1228 if( pExpr->pSelect ){
1229 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1230 }
drh6a3ea0e2003-05-02 14:32:12 +00001231
drhcce7d172000-05-31 15:34:51 +00001232 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001233 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001234 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001235 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001236 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001237
danielk1977bf3b7212004-05-18 10:06:24 +00001238 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001239
1240 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1241 ** expression it is handled the same way. A temporary table is
1242 ** filled with single-field index keys representing the results
1243 ** from the SELECT or the <exprlist>.
1244 **
1245 ** If the 'x' expression is a column value, or the SELECT...
1246 ** statement returns a column value, then the affinity of that
1247 ** column is used to build the index keys. If both 'x' and the
1248 ** SELECT... statement are columns, then numeric affinity is used
1249 ** if either column has NUMERIC or INTEGER affinity. If neither
1250 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1251 ** is used.
1252 */
1253 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001254 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001255 memset(&keyInfo, 0, sizeof(keyInfo));
1256 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001257 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001258
drhfef52082000-06-06 01:50:43 +00001259 if( pExpr->pSelect ){
1260 /* Case 1: expr IN (SELECT ...)
1261 **
danielk1977e014a832004-05-17 10:48:57 +00001262 ** Generate code to write the results of the select into the temporary
1263 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001264 */
danielk1977e014a832004-05-17 10:48:57 +00001265 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001266 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001267 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001268 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001269 pEList = pExpr->pSelect->pEList;
1270 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001271 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001272 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001273 }
drhfef52082000-06-06 01:50:43 +00001274 }else if( pExpr->pList ){
1275 /* Case 2: expr IN (exprlist)
1276 **
danielk1977e014a832004-05-17 10:48:57 +00001277 ** For each expression, build an index key from the evaluation and
1278 ** store it in the temporary table. If <expr> is a column, then use
1279 ** that columns affinity when building index keys. If <expr> is not
1280 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001281 */
danielk1977e014a832004-05-17 10:48:57 +00001282 int i;
danielk1977e014a832004-05-17 10:48:57 +00001283 if( !affinity ){
1284 affinity = SQLITE_AFF_NUMERIC;
1285 }
danielk19770202b292004-06-09 09:55:16 +00001286 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001287
1288 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001289 for(i=0; i<pExpr->pList->nExpr; i++){
1290 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001291
1292 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001293 if( !sqlite3ExprIsConstant(pE2) ){
1294 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001295 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001296 return;
drh4794b982000-06-06 13:54:14 +00001297 }
danielk1977e014a832004-05-17 10:48:57 +00001298
1299 /* Evaluate the expression and insert it into the temp table */
1300 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001301 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001302 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001303 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001304 }
1305 }
danielk19770202b292004-06-09 09:55:16 +00001306 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001307 break;
drhfef52082000-06-06 01:50:43 +00001308 }
1309
drh51522cd2005-01-20 13:36:19 +00001310 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001311 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001312 /* This has to be a scalar SELECT. Generate code to put the
1313 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001314 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001315 */
drh51522cd2005-01-20 13:36:19 +00001316 int sop;
1317 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001318
drh967e8b72000-06-21 13:59:10 +00001319 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001320 pSel = pExpr->pSelect;
1321 if( pExpr->op==TK_SELECT ){
1322 sop = SRT_Mem;
1323 }else{
1324 static const Token one = { "1", 0, 1 };
1325 sop = SRT_Exists;
1326 sqlite3ExprListDelete(pSel->pEList);
1327 pSel->pEList = sqlite3ExprListAppend(0,
1328 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1329 }
danielk1977b3bce662005-01-29 08:32:43 +00001330 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1331 break;
drhcce7d172000-05-31 15:34:51 +00001332 }
1333 }
danielk1977b3bce662005-01-29 08:32:43 +00001334
1335 if( pExpr->pSelect ){
1336 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1337 }
1338 if( label<0 ){
1339 sqlite3VdbeResolveLabel(v, label);
1340 }
1341 return;
drhcce7d172000-05-31 15:34:51 +00001342}
drh51522cd2005-01-20 13:36:19 +00001343#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001344
drhcce7d172000-05-31 15:34:51 +00001345/*
drhfec19aa2004-05-19 20:41:03 +00001346** Generate an instruction that will put the integer describe by
1347** text z[0..n-1] on the stack.
1348*/
1349static void codeInteger(Vdbe *v, const char *z, int n){
1350 int i;
drh6fec0762004-05-30 01:38:43 +00001351 if( sqlite3GetInt32(z, &i) ){
1352 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1353 }else if( sqlite3FitsIn64Bits(z) ){
1354 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001355 }else{
1356 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1357 }
1358}
1359
1360/*
drhcce7d172000-05-31 15:34:51 +00001361** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001362** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001363**
1364** This code depends on the fact that certain token values (ex: TK_EQ)
1365** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1366** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1367** the make process cause these values to align. Assert()s in the code
1368** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001369*/
danielk19774adee202004-05-08 08:23:19 +00001370void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001371 Vdbe *v = pParse->pVdbe;
1372 int op;
danielk19777977a172004-11-09 12:44:37 +00001373 if( v==0 ) return;
1374 if( pExpr==0 ){
1375 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1376 return;
1377 }
drhf2bc0132004-10-04 13:19:23 +00001378 op = pExpr->op;
1379 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001380 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001381 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1382 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001383 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001384 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001385#ifndef NDEBUG
1386 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1387 VdbeComment((v, "# %T", &pExpr->span));
1388 }
1389#endif
drhc4a3c772001-04-04 11:48:57 +00001390 }else{
danielk19774adee202004-05-08 08:23:19 +00001391 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001392 }
drhcce7d172000-05-31 15:34:51 +00001393 break;
1394 }
1395 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001396 codeInteger(v, pExpr->token.z, pExpr->token.n);
1397 break;
1398 }
1399 case TK_FLOAT:
1400 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001401 assert( TK_FLOAT==OP_Real );
1402 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001403 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001404 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001405 break;
1406 }
danielk19775338a5f2005-01-20 13:03:10 +00001407#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001408 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001409 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001410 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1411 sqlite3VdbeDequoteP3(v, -1);
1412 break;
1413 }
danielk19775338a5f2005-01-20 13:03:10 +00001414#endif
drhcce7d172000-05-31 15:34:51 +00001415 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001416 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001417 break;
1418 }
drh50457892003-09-06 01:10:47 +00001419 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001420 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001421 if( pExpr->token.n>1 ){
1422 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1423 }
drh50457892003-09-06 01:10:47 +00001424 break;
1425 }
drh4e0cff62004-11-05 05:10:28 +00001426 case TK_REGISTER: {
1427 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1428 break;
1429 }
drhc9b84a12002-06-20 11:36:48 +00001430 case TK_LT:
1431 case TK_LE:
1432 case TK_GT:
1433 case TK_GE:
1434 case TK_NE:
1435 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001436 assert( TK_LT==OP_Lt );
1437 assert( TK_LE==OP_Le );
1438 assert( TK_GT==OP_Gt );
1439 assert( TK_GE==OP_Ge );
1440 assert( TK_EQ==OP_Eq );
1441 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001442 sqlite3ExprCode(pParse, pExpr->pLeft);
1443 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001444 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001445 break;
drhc9b84a12002-06-20 11:36:48 +00001446 }
drhcce7d172000-05-31 15:34:51 +00001447 case TK_AND:
1448 case TK_OR:
1449 case TK_PLUS:
1450 case TK_STAR:
1451 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001452 case TK_REM:
1453 case TK_BITAND:
1454 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001455 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001456 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001457 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001458 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001459 assert( TK_AND==OP_And );
1460 assert( TK_OR==OP_Or );
1461 assert( TK_PLUS==OP_Add );
1462 assert( TK_MINUS==OP_Subtract );
1463 assert( TK_REM==OP_Remainder );
1464 assert( TK_BITAND==OP_BitAnd );
1465 assert( TK_BITOR==OP_BitOr );
1466 assert( TK_SLASH==OP_Divide );
1467 assert( TK_LSHIFT==OP_ShiftLeft );
1468 assert( TK_RSHIFT==OP_ShiftRight );
1469 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3ExprCode(pParse, pExpr->pLeft);
1471 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001472 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001473 break;
1474 }
drhcce7d172000-05-31 15:34:51 +00001475 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001476 Expr *pLeft = pExpr->pLeft;
1477 assert( pLeft );
1478 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1479 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001480 char *z = sqliteMalloc( p->n + 2 );
1481 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001482 if( pLeft->op==TK_FLOAT ){
1483 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001484 }else{
drhfec19aa2004-05-19 20:41:03 +00001485 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001486 }
drh6e142f52000-06-08 13:36:40 +00001487 sqliteFree(z);
1488 break;
1489 }
drh1ccde152000-06-17 13:12:39 +00001490 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001491 }
drhbf4133c2001-10-13 02:59:08 +00001492 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001493 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001494 assert( TK_BITNOT==OP_BitNot );
1495 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001496 sqlite3ExprCode(pParse, pExpr->pLeft);
1497 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001498 break;
1499 }
1500 case TK_ISNULL:
1501 case TK_NOTNULL: {
1502 int dest;
drhf2bc0132004-10-04 13:19:23 +00001503 assert( TK_ISNULL==OP_IsNull );
1504 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1506 sqlite3ExprCode(pParse, pExpr->pLeft);
1507 dest = sqlite3VdbeCurrentAddr(v) + 2;
1508 sqlite3VdbeAddOp(v, op, 1, dest);
1509 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001510 break;
drhcce7d172000-05-31 15:34:51 +00001511 }
drh22827922000-06-06 17:27:05 +00001512 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001513 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001514 break;
1515 }
danielk19777977a172004-11-09 12:44:37 +00001516 case TK_CDATE:
1517 case TK_CTIME:
1518 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001519 case TK_GLOB:
1520 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001521 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001522 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001523 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001524 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001525 int nId;
1526 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001527 int p2 = 0;
1528 int i;
danielk1977d8123362004-06-12 09:25:12 +00001529 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001530 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001531 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001532 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001533 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001534 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001535 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001536 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1537 p2 |= (1<<i);
1538 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001539 if( pDef->needCollSeq && !pColl ){
1540 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1541 }
1542 }
1543 if( pDef->needCollSeq ){
1544 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001545 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001546 }
1547 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001548 break;
1549 }
drhfe2093d2005-01-20 22:48:47 +00001550#ifndef SQLITE_OMIT_SUBQUERY
1551 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001552 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001553 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001554 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001555 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001556 break;
1557 }
drhfef52082000-06-06 01:50:43 +00001558 case TK_IN: {
1559 int addr;
drh94a11212004-09-25 13:12:14 +00001560 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001561 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001562
1563 /* Figure out the affinity to use to create a key from the results
1564 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001565 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001566 */
drh94a11212004-09-25 13:12:14 +00001567 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001568
danielk19774adee202004-05-08 08:23:19 +00001569 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001570
1571 /* Code the <expr> from "<expr> IN (...)". The temporary table
1572 ** pExpr->iTable contains the values that make up the (...) set.
1573 */
danielk19774adee202004-05-08 08:23:19 +00001574 sqlite3ExprCode(pParse, pExpr->pLeft);
1575 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001576 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001577 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001578 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001579 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001580 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001581 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1582 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1583
drhfef52082000-06-06 01:50:43 +00001584 break;
1585 }
danielk197793758c82005-01-21 08:13:14 +00001586#endif
drhfef52082000-06-06 01:50:43 +00001587 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001588 Expr *pLeft = pExpr->pLeft;
1589 struct ExprList_item *pLItem = pExpr->pList->a;
1590 Expr *pRight = pLItem->pExpr;
1591 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001593 sqlite3ExprCode(pParse, pRight);
1594 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001595 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001596 pLItem++;
1597 pRight = pLItem->pExpr;
1598 sqlite3ExprCode(pParse, pRight);
1599 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001601 break;
1602 }
drh51e9a442004-01-16 16:42:53 +00001603 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001604 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001605 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001606 break;
1607 }
drh17a7f8d2002-03-24 13:13:27 +00001608 case TK_CASE: {
1609 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001610 int jumpInst;
1611 int addr;
1612 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001613 int i;
drhbe5c89a2004-07-26 00:31:09 +00001614 ExprList *pEList;
1615 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001616
1617 assert(pExpr->pList);
1618 assert((pExpr->pList->nExpr % 2) == 0);
1619 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001620 pEList = pExpr->pList;
1621 aListelem = pEList->a;
1622 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001623 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001624 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001625 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001626 }
drhf5905aa2002-05-26 20:54:33 +00001627 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001628 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001629 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001630 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001631 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1632 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001633 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001634 }else{
danielk19774adee202004-05-08 08:23:19 +00001635 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001636 }
drhbe5c89a2004-07-26 00:31:09 +00001637 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001638 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1639 addr = sqlite3VdbeCurrentAddr(v);
1640 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001641 }
drhf570f012002-05-31 15:51:25 +00001642 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001643 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001644 }
drh17a7f8d2002-03-24 13:13:27 +00001645 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001646 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001647 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001648 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001649 }
danielk19774adee202004-05-08 08:23:19 +00001650 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001651 break;
1652 }
danielk19775338a5f2005-01-20 13:03:10 +00001653#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001654 case TK_RAISE: {
1655 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001657 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001658 return;
1659 }
drhad6d9462004-09-19 02:15:24 +00001660 if( pExpr->iColumn!=OE_Ignore ){
1661 assert( pExpr->iColumn==OE_Rollback ||
1662 pExpr->iColumn == OE_Abort ||
1663 pExpr->iColumn == OE_Fail );
1664 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1665 pExpr->token.z, pExpr->token.n);
1666 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001667 } else {
drhad6d9462004-09-19 02:15:24 +00001668 assert( pExpr->iColumn == OE_Ignore );
1669 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1670 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1671 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001672 }
drh17a7f8d2002-03-24 13:13:27 +00001673 }
danielk19775338a5f2005-01-20 13:03:10 +00001674#endif
drh17a7f8d2002-03-24 13:13:27 +00001675 break;
drhcce7d172000-05-31 15:34:51 +00001676 }
drhcce7d172000-05-31 15:34:51 +00001677}
1678
danielk197793758c82005-01-21 08:13:14 +00001679#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001680/*
drh25303782004-12-07 15:41:48 +00001681** Generate code that evalutes the given expression and leaves the result
1682** on the stack. See also sqlite3ExprCode().
1683**
1684** This routine might also cache the result and modify the pExpr tree
1685** so that it will make use of the cached result on subsequent evaluations
1686** rather than evaluate the whole expression again. Trivial expressions are
1687** not cached. If the expression is cached, its result is stored in a
1688** memory location.
1689*/
1690void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1691 Vdbe *v = pParse->pVdbe;
1692 int iMem;
1693 int addr1, addr2;
1694 if( v==0 ) return;
1695 addr1 = sqlite3VdbeCurrentAddr(v);
1696 sqlite3ExprCode(pParse, pExpr);
1697 addr2 = sqlite3VdbeCurrentAddr(v);
1698 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1699 iMem = pExpr->iTable = pParse->nMem++;
1700 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1701 pExpr->op = TK_REGISTER;
1702 }
1703}
danielk197793758c82005-01-21 08:13:14 +00001704#endif
drh25303782004-12-07 15:41:48 +00001705
1706/*
drh268380c2004-02-25 13:47:31 +00001707** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001708** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001709**
1710** Return the number of elements pushed onto the stack.
1711*/
danielk19774adee202004-05-08 08:23:19 +00001712int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001713 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001714 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001715){
1716 struct ExprList_item *pItem;
1717 int i, n;
1718 Vdbe *v;
1719 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001720 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001721 n = pList->nExpr;
1722 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001723 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001724 }
drhf9b596e2004-05-26 16:54:42 +00001725 return n;
drh268380c2004-02-25 13:47:31 +00001726}
1727
1728/*
drhcce7d172000-05-31 15:34:51 +00001729** Generate code for a boolean expression such that a jump is made
1730** to the label "dest" if the expression is true but execution
1731** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001732**
1733** If the expression evaluates to NULL (neither true nor false), then
1734** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001735**
1736** This code depends on the fact that certain token values (ex: TK_EQ)
1737** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1738** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1739** the make process cause these values to align. Assert()s in the code
1740** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001741*/
danielk19774adee202004-05-08 08:23:19 +00001742void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001743 Vdbe *v = pParse->pVdbe;
1744 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001745 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001746 op = pExpr->op;
1747 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001748 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001749 int d2 = sqlite3VdbeMakeLabel(v);
1750 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1751 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1752 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001753 break;
1754 }
1755 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001756 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1757 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001758 break;
1759 }
1760 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001761 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001762 break;
1763 }
1764 case TK_LT:
1765 case TK_LE:
1766 case TK_GT:
1767 case TK_GE:
1768 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001769 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001770 assert( TK_LT==OP_Lt );
1771 assert( TK_LE==OP_Le );
1772 assert( TK_GT==OP_Gt );
1773 assert( TK_GE==OP_Ge );
1774 assert( TK_EQ==OP_Eq );
1775 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001776 sqlite3ExprCode(pParse, pExpr->pLeft);
1777 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001778 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001779 break;
1780 }
1781 case TK_ISNULL:
1782 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001783 assert( TK_ISNULL==OP_IsNull );
1784 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001785 sqlite3ExprCode(pParse, pExpr->pLeft);
1786 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001787 break;
1788 }
drhfef52082000-06-06 01:50:43 +00001789 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001790 /* The expression "x BETWEEN y AND z" is implemented as:
1791 **
1792 ** 1 IF (x < y) GOTO 3
1793 ** 2 IF (x <= z) GOTO <dest>
1794 ** 3 ...
1795 */
drhf5905aa2002-05-26 20:54:33 +00001796 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001797 Expr *pLeft = pExpr->pLeft;
1798 Expr *pRight = pExpr->pList->a[0].pExpr;
1799 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001800 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001801 sqlite3ExprCode(pParse, pRight);
1802 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001803
drhbe5c89a2004-07-26 00:31:09 +00001804 pRight = pExpr->pList->a[1].pExpr;
1805 sqlite3ExprCode(pParse, pRight);
1806 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001807
danielk19774adee202004-05-08 08:23:19 +00001808 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1809 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1810 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001811 break;
1812 }
drhcce7d172000-05-31 15:34:51 +00001813 default: {
danielk19774adee202004-05-08 08:23:19 +00001814 sqlite3ExprCode(pParse, pExpr);
1815 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001816 break;
1817 }
1818 }
1819}
1820
1821/*
drh66b89c82000-11-28 20:47:17 +00001822** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001823** to the label "dest" if the expression is false but execution
1824** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001825**
1826** If the expression evaluates to NULL (neither true nor false) then
1827** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001828*/
danielk19774adee202004-05-08 08:23:19 +00001829void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001830 Vdbe *v = pParse->pVdbe;
1831 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001832 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001833
1834 /* The value of pExpr->op and op are related as follows:
1835 **
1836 ** pExpr->op op
1837 ** --------- ----------
1838 ** TK_ISNULL OP_NotNull
1839 ** TK_NOTNULL OP_IsNull
1840 ** TK_NE OP_Eq
1841 ** TK_EQ OP_Ne
1842 ** TK_GT OP_Le
1843 ** TK_LE OP_Gt
1844 ** TK_GE OP_Lt
1845 ** TK_LT OP_Ge
1846 **
1847 ** For other values of pExpr->op, op is undefined and unused.
1848 ** The value of TK_ and OP_ constants are arranged such that we
1849 ** can compute the mapping above using the following expression.
1850 ** Assert()s verify that the computation is correct.
1851 */
1852 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1853
1854 /* Verify correct alignment of TK_ and OP_ constants
1855 */
1856 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1857 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1858 assert( pExpr->op!=TK_NE || op==OP_Eq );
1859 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1860 assert( pExpr->op!=TK_LT || op==OP_Ge );
1861 assert( pExpr->op!=TK_LE || op==OP_Gt );
1862 assert( pExpr->op!=TK_GT || op==OP_Le );
1863 assert( pExpr->op!=TK_GE || op==OP_Lt );
1864
drhcce7d172000-05-31 15:34:51 +00001865 switch( pExpr->op ){
1866 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001867 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1868 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001869 break;
1870 }
1871 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001872 int d2 = sqlite3VdbeMakeLabel(v);
1873 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1874 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1875 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001876 break;
1877 }
1878 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001879 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001880 break;
1881 }
1882 case TK_LT:
1883 case TK_LE:
1884 case TK_GT:
1885 case TK_GE:
1886 case TK_NE:
1887 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001888 sqlite3ExprCode(pParse, pExpr->pLeft);
1889 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001890 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001891 break;
1892 }
drhcce7d172000-05-31 15:34:51 +00001893 case TK_ISNULL:
1894 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001895 sqlite3ExprCode(pParse, pExpr->pLeft);
1896 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001897 break;
1898 }
drhfef52082000-06-06 01:50:43 +00001899 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001900 /* The expression is "x BETWEEN y AND z". It is implemented as:
1901 **
1902 ** 1 IF (x >= y) GOTO 3
1903 ** 2 GOTO <dest>
1904 ** 3 IF (x > z) GOTO <dest>
1905 */
drhfef52082000-06-06 01:50:43 +00001906 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001907 Expr *pLeft = pExpr->pLeft;
1908 Expr *pRight = pExpr->pList->a[0].pExpr;
1909 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001910 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001911 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001912 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001913 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1914
danielk19774adee202004-05-08 08:23:19 +00001915 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1916 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001917 pRight = pExpr->pList->a[1].pExpr;
1918 sqlite3ExprCode(pParse, pRight);
1919 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001920 break;
1921 }
drhcce7d172000-05-31 15:34:51 +00001922 default: {
danielk19774adee202004-05-08 08:23:19 +00001923 sqlite3ExprCode(pParse, pExpr);
1924 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001925 break;
1926 }
1927 }
1928}
drh22827922000-06-06 17:27:05 +00001929
1930/*
1931** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1932** if they are identical and return FALSE if they differ in any way.
1933*/
danielk19774adee202004-05-08 08:23:19 +00001934int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001935 int i;
1936 if( pA==0 ){
1937 return pB==0;
1938 }else if( pB==0 ){
1939 return 0;
1940 }
1941 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001942 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1943 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001944 if( pA->pList ){
1945 if( pB->pList==0 ) return 0;
1946 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1947 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001948 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001949 return 0;
1950 }
1951 }
1952 }else if( pB->pList ){
1953 return 0;
1954 }
1955 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001956 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001957 if( pA->token.z ){
1958 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001959 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001960 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001961 }
1962 return 1;
1963}
1964
1965/*
1966** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001967** The new element is initialized to zero. The calling function is
1968** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001969*/
1970static int appendAggInfo(Parse *pParse){
1971 if( (pParse->nAgg & 0x7)==0 ){
1972 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001973 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1974 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001975 return -1;
1976 }
drh6d4abfb2001-10-22 02:58:08 +00001977 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001978 }
1979 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1980 return pParse->nAgg++;
1981}
1982
1983/*
drh626a8792005-01-17 22:08:19 +00001984** This is an xFunc for walkExprTree() used to implement
1985** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1986** for additional information.
drh22827922000-06-06 17:27:05 +00001987**
drh626a8792005-01-17 22:08:19 +00001988** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001989*/
drh626a8792005-01-17 22:08:19 +00001990static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001991 int i;
1992 AggExpr *aAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00001993 NameContext *pNC = (NameContext *)pArg;
1994 Parse *pParse = pNC->pParse;
1995 SrcList *pSrcList = pNC->pSrcList;
drh22827922000-06-06 17:27:05 +00001996
drh22827922000-06-06 17:27:05 +00001997 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001998 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001999 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2000 if( pExpr->iTable==pSrcList->a[i].iCursor ){
2001 aAgg = pParse->aAgg;
2002 for(i=0; i<pParse->nAgg; i++){
2003 if( aAgg[i].isAgg ) continue;
2004 if( aAgg[i].pExpr->iTable==pExpr->iTable
2005 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
2006 break;
2007 }
2008 }
2009 if( i>=pParse->nAgg ){
2010 i = appendAggInfo(pParse);
2011 if( i<0 ) return 1;
2012 pParse->aAgg[i].isAgg = 0;
2013 pParse->aAgg[i].pExpr = pExpr;
2014 }
2015 pExpr->iAgg = i;
2016 pExpr->iAggCtx = pNC->nDepth;
2017 return 1;
drh22827922000-06-06 17:27:05 +00002018 }
2019 }
drh626a8792005-01-17 22:08:19 +00002020 return 1;
drh22827922000-06-06 17:27:05 +00002021 }
2022 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002023 if( pNC->nDepth==0 ){
2024 aAgg = pParse->aAgg;
2025 for(i=0; i<pParse->nAgg; i++){
2026 if( !aAgg[i].isAgg ) continue;
2027 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
2028 break;
2029 }
drh22827922000-06-06 17:27:05 +00002030 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002031 if( i>=pParse->nAgg ){
2032 u8 enc = pParse->db->enc;
2033 i = appendAggInfo(pParse);
2034 if( i<0 ) return 1;
2035 pParse->aAgg[i].isAgg = 1;
2036 pParse->aAgg[i].pExpr = pExpr;
2037 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
2038 pExpr->token.z, pExpr->token.n,
2039 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2040 }
2041 pExpr->iAgg = i;
2042 return 1;
drh22827922000-06-06 17:27:05 +00002043 }
drh22827922000-06-06 17:27:05 +00002044 }
2045 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002046 if( pExpr->pSelect ){
2047 pNC->nDepth++;
2048 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2049 pNC->nDepth--;
2050 }
drh626a8792005-01-17 22:08:19 +00002051 return 0;
2052}
2053
2054/*
2055** Analyze the given expression looking for aggregate functions and
2056** for variables that need to be added to the pParse->aAgg[] array.
2057** Make additional entries to the pParse->aAgg[] array as necessary.
2058**
2059** This routine should only be called after the expression has been
2060** analyzed by sqlite3ExprResolveNames().
2061**
2062** If errors are seen, leave an error message in zErrMsg and return
2063** the number of errors.
2064*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002065int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2066 int nErr = pNC->pParse->nErr;
2067 walkExprTree(pExpr, analyzeAggregate, pNC);
2068 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002069}
drh8e0a2f92002-02-23 23:45:45 +00002070
2071/*
danielk1977d02eb1f2004-06-06 09:44:03 +00002072** Locate a user function given a name, a number of arguments and a flag
2073** indicating whether the function prefers UTF-16 over UTF-8. Return a
2074** pointer to the FuncDef structure that defines that function, or return
2075** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00002076**
drh0bce8352002-02-28 00:41:10 +00002077** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00002078** structure is created and liked into the "db" structure if a
2079** no matching function previously existed. When createFlag is true
2080** and the nArg parameter is -1, then only a function that accepts
2081** any number of arguments will be returned.
2082**
2083** If createFlag is false and nArg is -1, then the first valid
2084** function found is returned. A function is valid if either xFunc
2085** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00002086**
2087** If createFlag is false, then a function with the required name and
2088** number of arguments may be returned even if the eTextRep flag does not
2089** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00002090*/
danielk19774adee202004-05-08 08:23:19 +00002091FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00002092 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00002093 const char *zName, /* Name of the function. Not null-terminated */
2094 int nName, /* Number of characters in the name */
2095 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00002096 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00002097 int createFlag /* Create new entry if true and does not otherwise exist */
2098){
danielk1977d02eb1f2004-06-06 09:44:03 +00002099 FuncDef *p; /* Iterator variable */
2100 FuncDef *pFirst; /* First function with this name */
2101 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00002102 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00002103
danielk1977d8123362004-06-12 09:25:12 +00002104
2105 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00002106 if( nArg<-1 ) nArg = -1;
2107
2108 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
2109 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00002110 /* During the search for the best function definition, bestmatch is set
2111 ** as follows to indicate the quality of the match with the definition
2112 ** pointed to by pBest:
2113 **
2114 ** 0: pBest is NULL. No match has been found.
2115 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
2116 ** encoding is requested, or vice versa.
2117 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
2118 ** requested, or vice versa.
2119 ** 3: A variable arguments function using the same text encoding.
2120 ** 4: A function with the exact number of arguments requested that
2121 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
2122 ** 5: A function with the exact number of arguments requested that
2123 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
2124 ** 6: An exact match.
2125 **
2126 ** A larger value of 'matchqual' indicates a more desirable match.
2127 */
danielk1977e12c17b2004-06-23 12:35:14 +00002128 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00002129 int match = 1; /* Quality of this match */
2130 if( p->nArg==nArg || nArg==-1 ){
2131 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00002132 }
danielk1977d8123362004-06-12 09:25:12 +00002133 if( enc==p->iPrefEnc ){
2134 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00002135 }
danielk1977d8123362004-06-12 09:25:12 +00002136 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2137 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2138 match += 1;
2139 }
2140
2141 if( match>bestmatch ){
2142 pBest = p;
2143 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002144 }
2145 }
drh8e0a2f92002-02-23 23:45:45 +00002146 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002147
danielk1977d8123362004-06-12 09:25:12 +00002148 /* If the createFlag parameter is true, and the seach did not reveal an
2149 ** exact match for the name, number of arguments and encoding, then add a
2150 ** new entry to the hash table and return it.
2151 */
2152 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002153 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2154 pBest->nArg = nArg;
2155 pBest->pNext = pFirst;
2156 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002157 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002158 memcpy(pBest->zName, zName, nName);
2159 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002160 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2161 sqliteFree(pBest);
2162 return 0;
2163 }
drh8e0a2f92002-02-23 23:45:45 +00002164 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002165
2166 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2167 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002168 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002169 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002170}