blob: 0aaaf0e879860983dadb3f2e0a9ce2de89145ca8 [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**
drh8f619cc2002-09-08 00:04:50 +000015** $Id: expr.c,v 1.81 2002/09/08 00:04:52 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
20/*
drha76b5df2002-02-23 02:32:10 +000021** Construct a new expression node and return a pointer to it. Memory
22** for this node is obtained from sqliteMalloc(). The calling function
23** is responsible for making sure the node eventually gets freed.
24*/
25Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
26 Expr *pNew;
27 pNew = sqliteMalloc( sizeof(Expr) );
28 if( pNew==0 ){
29 sqliteExprDelete(pLeft);
30 sqliteExprDelete(pRight);
31 return 0;
32 }
33 pNew->op = op;
34 pNew->pLeft = pLeft;
35 pNew->pRight = pRight;
36 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000037 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000038 pNew->token = *pToken;
drh4b59ab52002-08-24 18:24:51 +000039 pNew->token.base = 1;
40 }else if( pLeft && pRight ){
41 sqliteExprSpan(pNew, &pLeft->token, &pRight->token);
drha76b5df2002-02-23 02:32:10 +000042 }else{
drh4b59ab52002-08-24 18:24:51 +000043 pNew->token.dyn = 0;
44 pNew->token.base = 1;
drha76b5df2002-02-23 02:32:10 +000045 pNew->token.z = 0;
46 pNew->token.n = 0;
47 }
drha76b5df2002-02-23 02:32:10 +000048 return pNew;
49}
50
51/*
52** Set the Expr.token field of the given expression to span all
53** text between the two given tokens.
54*/
55void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
56 if( pExpr ){
drh4b59ab52002-08-24 18:24:51 +000057 assert( pExpr->token.dyn==0 );
58 if( pLeft->dyn==0 && pRight->dyn==0 ){
59 pExpr->token.z = pLeft->z;
60 pExpr->token.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
61 pExpr->token.base = 0;
62 }else{
63 pExpr->token.z = 0;
64 pExpr->token.n = 0;
65 pExpr->token.dyn = 0;
66 pExpr->token.base = 0;
67 }
drha76b5df2002-02-23 02:32:10 +000068 }
69}
70
71/*
72** Construct a new expression node for a function with multiple
73** arguments.
74*/
75Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
76 Expr *pNew;
77 pNew = sqliteMalloc( sizeof(Expr) );
78 if( pNew==0 ){
79 sqliteExprListDelete(pList);
80 return 0;
81 }
82 pNew->op = TK_FUNCTION;
83 pNew->pList = pList;
drh4b59ab52002-08-24 18:24:51 +000084
85 /* Expr.token.n is the length of the entire function
86 ** call, including the function arguments. The parser
87 ** will extend token.n to cover the either length of the string.
88 ** Expr.nFuncName is the length of just the function name.
89 */
90 pNew->token.dyn = 0;
91 pNew->token.base = 1;
drha76b5df2002-02-23 02:32:10 +000092 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000093 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000094 pNew->token = *pToken;
drh4b59ab52002-08-24 18:24:51 +000095 pNew->nFuncName = pToken->n>255 ? 255 : pToken->n;
drha76b5df2002-02-23 02:32:10 +000096 }else{
97 pNew->token.z = 0;
98 pNew->token.n = 0;
99 }
100 return pNew;
101}
102
103/*
drha2e00042002-01-22 03:13:42 +0000104** Recursively delete an expression tree.
105*/
106void sqliteExprDelete(Expr *p){
107 if( p==0 ) return;
drh4b59ab52002-08-24 18:24:51 +0000108 if( p->token.dyn && p->token.z ) sqliteFree((char*)p->token.z);
drh75148a22002-03-03 03:42:31 +0000109 if( p->pLeft ) sqliteExprDelete(p->pLeft);
110 if( p->pRight ) sqliteExprDelete(p->pRight);
drha2e00042002-01-22 03:13:42 +0000111 if( p->pList ) sqliteExprListDelete(p->pList);
112 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
113 sqliteFree(p);
114}
115
drha76b5df2002-02-23 02:32:10 +0000116
117/*
drhff78bd22002-02-27 01:47:11 +0000118** The following group of routines make deep copies of expressions,
119** expression lists, ID lists, and select statements. The copies can
120** be deleted (by being passed to their respective ...Delete() routines)
121** without effecting the originals.
122**
drhad3cab52002-05-24 02:04:32 +0000123** The expression list, ID, and source lists return by sqliteExprListDup(),
124** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
125** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000126**
drhad3cab52002-05-24 02:04:32 +0000127** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000128*/
129Expr *sqliteExprDup(Expr *p){
130 Expr *pNew;
131 if( p==0 ) return 0;
132 pNew = sqliteMalloc( sizeof(*p) );
133 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000134 memcpy(pNew, p, sizeof(*pNew));
drh4b59ab52002-08-24 18:24:51 +0000135 /* Only make a copy of the token if it is a base token (meaning that
136 ** it covers a single term of an expression - not two or more terms)
137 ** or if it is already dynamically allocated. So, for example, in
138 ** a complex expression like "a+b+c", the token "b" would be duplicated
139 ** but "a+b" would not be. */
140 if( p->token.z!=0 && (p->token.base || p->token.dyn) ){
141 pNew->token.z = sqliteStrDup(p->token.z);
142 pNew->token.dyn = 1;
143 }else{
144 pNew->token.z = 0;
145 pNew->token.n = 0;
146 pNew->token.dyn = 0;
147 }
drhff78bd22002-02-27 01:47:11 +0000148 pNew->pLeft = sqliteExprDup(p->pLeft);
149 pNew->pRight = sqliteExprDup(p->pRight);
150 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000151 pNew->pSelect = sqliteSelectDup(p->pSelect);
152 return pNew;
153}
drh4b59ab52002-08-24 18:24:51 +0000154void sqliteTokenCopy(Token *pTo, Token *pFrom){
155 if( pTo->dyn ) sqliteFree((char*)pTo->z);
156 pTo->base = pFrom->base;
157 if( pFrom->z ){
158 pTo->n = pFrom->n;
159 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
160 pTo->dyn = 1;
161 }else{
162 pTo->n = 0;
163 pTo->z = 0;
164 pTo->dyn = 0;
165 }
166}
drhff78bd22002-02-27 01:47:11 +0000167ExprList *sqliteExprListDup(ExprList *p){
168 ExprList *pNew;
169 int i;
170 if( p==0 ) return 0;
171 pNew = sqliteMalloc( sizeof(*pNew) );
172 if( pNew==0 ) return 0;
173 pNew->nExpr = p->nExpr;
174 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000175 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000176 for(i=0; i<p->nExpr; i++){
drh4b59ab52002-08-24 18:24:51 +0000177 Expr *pNewExpr, *pOldExpr;
178 pNew->a[i].pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
179 if( pOldExpr->token.z!=0 && pNewExpr && pNewExpr->token.z==0 ){
180 /* Always make a copy of the token for top-level expressions in the
181 ** expression list. The logic in SELECT processing that determines
182 ** the names of columns in the result set needs this information */
183 sqliteTokenCopy(&pNew->a[i].pExpr->token, &p->a[i].pExpr->token);
184 }
drhff78bd22002-02-27 01:47:11 +0000185 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
186 pNew->a[i].sortOrder = p->a[i].sortOrder;
187 pNew->a[i].isAgg = p->a[i].isAgg;
188 pNew->a[i].done = 0;
189 }
190 return pNew;
191}
drhad3cab52002-05-24 02:04:32 +0000192SrcList *sqliteSrcListDup(SrcList *p){
193 SrcList *pNew;
194 int i;
195 if( p==0 ) return 0;
196 pNew = sqliteMalloc( sizeof(*pNew) );
197 if( pNew==0 ) return 0;
198 pNew->nSrc = p->nSrc;
199 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
danielk19776f349032002-06-11 02:25:40 +0000200 if( pNew->a==0 && p->nSrc != 0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000201 for(i=0; i<p->nSrc; i++){
202 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
203 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
204 pNew->a[i].jointype = p->a[i].jointype;
205 pNew->a[i].pTab = 0;
206 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
207 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
208 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
209 }
210 return pNew;
211}
drhff78bd22002-02-27 01:47:11 +0000212IdList *sqliteIdListDup(IdList *p){
213 IdList *pNew;
214 int i;
215 if( p==0 ) return 0;
216 pNew = sqliteMalloc( sizeof(*pNew) );
217 if( pNew==0 ) return 0;
218 pNew->nId = p->nId;
219 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000220 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000221 for(i=0; i<p->nId; i++){
222 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000223 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000224 }
225 return pNew;
226}
227Select *sqliteSelectDup(Select *p){
228 Select *pNew;
229 if( p==0 ) return 0;
230 pNew = sqliteMalloc( sizeof(*p) );
231 if( pNew==0 ) return 0;
232 pNew->isDistinct = p->isDistinct;
233 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000234 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000235 pNew->pWhere = sqliteExprDup(p->pWhere);
236 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
237 pNew->pHaving = sqliteExprDup(p->pHaving);
238 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
239 pNew->op = p->op;
240 pNew->pPrior = sqliteSelectDup(p->pPrior);
241 pNew->nLimit = p->nLimit;
242 pNew->nOffset = p->nOffset;
243 pNew->zSelect = 0;
244 return pNew;
245}
246
247
248/*
drha76b5df2002-02-23 02:32:10 +0000249** Add a new element to the end of an expression list. If pList is
250** initially NULL, then create a new expression list.
251*/
252ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
253 int i;
254 if( pList==0 ){
255 pList = sqliteMalloc( sizeof(ExprList) );
256 if( pList==0 ){
257 sqliteExprDelete(pExpr);
258 return 0;
259 }
260 }
261 if( (pList->nExpr & 7)==0 ){
262 int n = pList->nExpr + 8;
263 struct ExprList_item *a;
264 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
265 if( a==0 ){
266 sqliteExprDelete(pExpr);
267 return pList;
268 }
269 pList->a = a;
270 }
271 if( pExpr || pName ){
272 i = pList->nExpr++;
273 pList->a[i].pExpr = pExpr;
274 pList->a[i].zName = 0;
275 if( pName ){
276 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
277 sqliteDequote(pList->a[i].zName);
278 }
279 }
280 return pList;
281}
282
283/*
284** Delete an entire expression list.
285*/
286void sqliteExprListDelete(ExprList *pList){
287 int i;
288 if( pList==0 ) return;
289 for(i=0; i<pList->nExpr; i++){
290 sqliteExprDelete(pList->a[i].pExpr);
291 sqliteFree(pList->a[i].zName);
292 }
293 sqliteFree(pList->a);
294 sqliteFree(pList);
295}
296
297/*
drhfef52082000-06-06 01:50:43 +0000298** Walk an expression tree. Return 1 if the expression is constant
299** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000300**
301** For the purposes of this function, a double-quoted string (ex: "abc")
302** is considered a variable but a single-quoted string (ex: 'abc') is
303** a constant.
drhfef52082000-06-06 01:50:43 +0000304*/
drh92086432002-01-22 14:11:29 +0000305int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000306 switch( p->op ){
307 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000308 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000309 case TK_DOT:
310 return 0;
drh23989372002-05-21 13:43:04 +0000311 case TK_STRING:
312 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000313 case TK_INTEGER:
314 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000315 return 1;
drhfef52082000-06-06 01:50:43 +0000316 default: {
drh92086432002-01-22 14:11:29 +0000317 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
318 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000319 if( p->pList ){
320 int i;
321 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000322 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000323 }
324 }
drh92086432002-01-22 14:11:29 +0000325 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000326 }
327 }
drh92086432002-01-22 14:11:29 +0000328 return 0;
drhfef52082000-06-06 01:50:43 +0000329}
330
331/*
drhe4de1fe2002-06-02 16:09:01 +0000332** If the given expression codes a constant integer, return 1 and put
333** the value of the integer in *pValue. If the expression is not an
334** integer, return 0 and leave *pValue unchanged.
335*/
336int sqliteExprIsInteger(Expr *p, int *pValue){
337 switch( p->op ){
338 case TK_INTEGER: {
339 *pValue = atoi(p->token.z);
340 return 1;
341 }
342 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000343 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000344 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000345 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000346 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
347 if( n==0 ){
348 *pValue = atoi(p->token.z);
349 return 1;
350 }
351 break;
352 }
drh4b59ab52002-08-24 18:24:51 +0000353 case TK_UPLUS: {
354 return sqliteExprIsInteger(p->pLeft, pValue);
355 }
drhe4de1fe2002-06-02 16:09:01 +0000356 case TK_UMINUS: {
357 int v;
358 if( sqliteExprIsInteger(p->pLeft, &v) ){
359 *pValue = -v;
360 return 1;
361 }
362 break;
363 }
364 default: break;
365 }
366 return 0;
367}
368
369/*
drhc4a3c772001-04-04 11:48:57 +0000370** Return TRUE if the given string is a row-id column name.
371*/
drha9f9d1c2002-06-29 02:20:08 +0000372int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000373 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
374 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
375 if( sqliteStrICmp(z, "OID")==0 ) return 1;
376 return 0;
377}
378
379/*
drhcce7d172000-05-31 15:34:51 +0000380** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000381** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000382** index to the table in the table list and a column offset. The
383** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
384** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000385** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000386** VDBE cursor number for a cursor that is pointing into the referenced
387** table. The Expr.iColumn value is changed to the index of the column
388** of the referenced table. The Expr.iColumn value for the special
389** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
390** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000391**
drhfef52082000-06-06 01:50:43 +0000392** We also check for instances of the IN operator. IN comes in two
393** forms:
394**
395** expr IN (exprlist)
396** and
397** expr IN (SELECT ...)
398**
399** The first form is handled by creating a set holding the list
400** of allowed values. The second form causes the SELECT to generate
401** a temporary table.
402**
403** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000404** If it finds any, it generates code to write the value of that select
405** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000406**
drh967e8b72000-06-21 13:59:10 +0000407** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000408** the number of errors seen and leaves an error message on pParse->zErrMsg.
409*/
drha2e00042002-01-22 03:13:42 +0000410int sqliteExprResolveIds(
411 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000412 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000413 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000414 ExprList *pEList, /* List of expressions used to resolve "AS" */
415 Expr *pExpr /* The expression to be analyzed. */
416){
drhdaffd0e2001-04-11 14:28:42 +0000417 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000418 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000419 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000420 /* Double-quoted strings (ex: "abc") are used as identifiers if
421 ** possible. Otherwise they remain as strings. Single-quoted
422 ** strings (ex: 'abc') are always string literals.
423 */
424 case TK_STRING: {
425 if( pExpr->token.z[0]=='\'' ) break;
426 /* Fall thru into the TK_ID case if this is a double-quoted string */
427 }
drha2e00042002-01-22 03:13:42 +0000428 /* A lone identifier. Try and match it as follows:
429 **
430 ** 1. To the name of a column of one of the tables in pTabList
431 **
432 ** 2. To the right side of an AS keyword in the column list of
433 ** a SELECT statement. (For example, match against 'x' in
434 ** "SELECT a+b AS 'x' FROM t1".)
435 **
436 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
437 */
drhcce7d172000-05-31 15:34:51 +0000438 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000439 int cnt = 0; /* Number of matches */
440 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000441 char *z;
442 assert( pExpr->token.z );
443 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000444 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000445 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000446 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000447 int j;
448 Table *pTab = pTabList->a[i].pTab;
449 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000450 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000451 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000452 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000453 cnt++;
drh832508b2002-03-02 17:04:07 +0000454 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000455 if( j==pTab->iPKey ){
456 /* Substitute the record number for the INTEGER PRIMARY KEY */
457 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000458 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000459 }else{
460 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000461 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000462 }
drha2e00042002-01-22 03:13:42 +0000463 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000464 }
465 }
466 }
drha2e00042002-01-22 03:13:42 +0000467 if( cnt==0 && pEList!=0 ){
468 int j;
469 for(j=0; j<pEList->nExpr; j++){
470 char *zAs = pEList->a[j].zName;
471 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
472 cnt++;
473 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
474 pExpr->op = TK_AS;
475 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000476 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000477 }
478 }
479 }
drhc4a3c772001-04-04 11:48:57 +0000480 if( cnt==0 && sqliteIsRowid(z) ){
481 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000482 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000483 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000484 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000485 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000486 }
drhcce7d172000-05-31 15:34:51 +0000487 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000488 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000489 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000490 pExpr->token.z, pExpr->token.n, 0);
491 pParse->nErr++;
492 return 1;
493 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000494 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000495 pExpr->token.z, pExpr->token.n, 0);
496 pParse->nErr++;
497 return 1;
498 }
drhcce7d172000-05-31 15:34:51 +0000499 break;
500 }
501
drh967e8b72000-06-21 13:59:10 +0000502 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000503 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000504 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000505 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000506 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000507 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000508 char *zLeft, *zRight; /* Text of an identifier */
509
510 pLeft = pExpr->pLeft;
511 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000512 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
513 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000514 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
515 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000516 if( zLeft==0 || zRight==0 ){
517 sqliteFree(zLeft);
518 sqliteFree(zRight);
519 return 1;
520 }
drh87c40e82001-07-23 14:33:02 +0000521 sqliteDequote(zLeft);
522 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000523 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000524 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000525 int j;
526 char *zTab;
527 Table *pTab = pTabList->a[i].pTab;
528 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000529 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000530 if( pTabList->a[i].zAlias ){
531 zTab = pTabList->a[i].zAlias;
532 }else{
533 zTab = pTab->zName;
534 }
drh094b2bb2002-03-13 18:54:07 +0000535 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000536 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000537 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000538 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000539 cnt++;
drh832508b2002-03-02 17:04:07 +0000540 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000541 if( j==pTab->iPKey ){
542 /* Substitute the record number for the INTEGER PRIMARY KEY */
543 pExpr->iColumn = -1;
544 }else{
545 pExpr->iColumn = j;
546 }
drhc9b84a12002-06-20 11:36:48 +0000547 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000548 }
549 }
550 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000551
552 /* If we have not already resolved this *.* expression, then maybe
553 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000554 if( cnt == 0 && pParse->trigStack != 0 ){
555 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000556 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000557 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
558 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000559 cntTab++;
560 t = 1;
561 }
danielk1977f29ce552002-05-19 23:43:12 +0000562 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
563 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000564 cntTab++;
565 t = 1;
566 }
567
danielk1977f29ce552002-05-19 23:43:12 +0000568 if( t ){
569 int j;
drhc9b84a12002-06-20 11:36:48 +0000570 Table *pTab = pTriggerStack->pTab;
571 for(j=0; j < pTab->nCol; j++) {
572 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000573 cnt++;
574 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000575 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000576 }
577 }
danielk1977f29ce552002-05-19 23:43:12 +0000578 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000579 }
580
drhc4a3c772001-04-04 11:48:57 +0000581 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
582 cnt = 1;
583 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000584 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000585 }
drhcce7d172000-05-31 15:34:51 +0000586 sqliteFree(zLeft);
587 sqliteFree(zRight);
588 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000589 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000590 pLeft->token.z, pLeft->token.n, ".", 1,
591 pRight->token.z, pRight->token.n, 0);
592 pParse->nErr++;
593 return 1;
594 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000595 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000596 pLeft->token.z, pLeft->token.n, ".", 1,
597 pRight->token.z, pRight->token.n, 0);
598 pParse->nErr++;
599 return 1;
600 }
601 sqliteExprDelete(pLeft);
602 pExpr->pLeft = 0;
603 sqliteExprDelete(pRight);
604 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000605 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000606 break;
607 }
608
drhfef52082000-06-06 01:50:43 +0000609 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000610 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000611 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000612 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000613 return 1;
614 }
drhfef52082000-06-06 01:50:43 +0000615 if( pExpr->pSelect ){
616 /* Case 1: expr IN (SELECT ...)
617 **
618 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000619 ** table. The cursor number of the temporary table has already
620 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000621 */
drh832508b2002-03-02 17:04:07 +0000622 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000623 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000624 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000625 }else if( pExpr->pList ){
626 /* Case 2: expr IN (exprlist)
627 **
628 ** Create a set to put the exprlist values in. The Set id is stored
629 ** in iTable.
630 */
631 int i, iSet;
632 for(i=0; i<pExpr->pList->nExpr; i++){
633 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000634 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000635 sqliteSetString(&pParse->zErrMsg,
636 "right-hand side of IN operator must be constant", 0);
637 pParse->nErr++;
638 return 1;
639 }
drh4794b982000-06-06 13:54:14 +0000640 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
641 return 1;
642 }
drhfef52082000-06-06 01:50:43 +0000643 }
644 iSet = pExpr->iTable = pParse->nSet++;
645 for(i=0; i<pExpr->pList->nExpr; i++){
646 Expr *pE2 = pExpr->pList->a[i].pExpr;
647 switch( pE2->op ){
648 case TK_FLOAT:
649 case TK_INTEGER:
650 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000651 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000652 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000653 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
654 sqliteVdbeDequoteP3(v, addr);
655 break;
656 }
657 default: {
658 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000659 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000660 break;
661 }
662 }
663 }
664 }
drhcfab11b2000-06-06 03:31:22 +0000665 break;
drhfef52082000-06-06 01:50:43 +0000666 }
667
drh19a775c2000-06-05 18:54:46 +0000668 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000669 /* This has to be a scalar SELECT. Generate code to put the
670 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000671 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000672 */
drh967e8b72000-06-21 13:59:10 +0000673 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000674 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000675 return 1;
676 }
677 break;
678 }
679
drhcce7d172000-05-31 15:34:51 +0000680 /* For all else, just recursively walk the tree */
681 default: {
drh4794b982000-06-06 13:54:14 +0000682 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000683 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000684 return 1;
685 }
686 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000687 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000688 return 1;
689 }
690 if( pExpr->pList ){
691 int i;
692 ExprList *pList = pExpr->pList;
693 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000694 Expr *pArg = pList->a[i].pExpr;
695 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000696 return 1;
697 }
698 }
699 }
700 }
701 }
702 return 0;
703}
704
drhcce7d172000-05-31 15:34:51 +0000705/*
drh4b59ab52002-08-24 18:24:51 +0000706** pExpr is a node that defines a function of some kind. It might
707** be a syntactic function like "count(x)" or it might be a function
708** that implements an operator, like "a LIKE b".
709**
710** This routine makes *pzName point to the name of the function and
711** *pnName hold the number of characters in the function name.
712*/
713static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
714 switch( pExpr->op ){
715 case TK_FUNCTION: {
716 *pzName = pExpr->token.z;
717 *pnName = pExpr->nFuncName;
718 break;
719 }
720 case TK_LIKE: {
721 *pzName = "like";
722 *pnName = 4;
723 break;
724 }
725 case TK_GLOB: {
726 *pzName = "glob";
727 *pnName = 4;
728 break;
729 }
730 default: {
731 *pzName = "can't happen";
732 *pnName = 12;
733 break;
734 }
735 }
736}
737
738/*
drhcce7d172000-05-31 15:34:51 +0000739** Error check the functions in an expression. Make sure all
740** function names are recognized and all functions have the correct
741** number of arguments. Leave an error message in pParse->zErrMsg
742** if anything is amiss. Return the number of errors.
743**
744** if pIsAgg is not null and this expression is an aggregate function
745** (like count(*) or max(value)) then write a 1 into *pIsAgg.
746*/
747int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
748 int nErr = 0;
749 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000750 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000751 case TK_GLOB:
752 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000753 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000754 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
755 int no_such_func = 0; /* True if no such function exists */
756 int is_type_of = 0; /* True if is the special TypeOf() function */
757 int wrong_num_args = 0; /* True if wrong number of arguments */
758 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000759 int i;
drh4b59ab52002-08-24 18:24:51 +0000760 int nId; /* Number of characters in function name */
761 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000762 FuncDef *pDef;
763
drh4b59ab52002-08-24 18:24:51 +0000764 getFunctionName(pExpr, &zId, &nId);
765 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000766 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000767 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000768 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000769 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000770 is_type_of = 1;
771 }else {
772 no_such_func = 1;
773 }
drh0bce8352002-02-28 00:41:10 +0000774 }else{
775 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000776 }
drh0bce8352002-02-28 00:41:10 +0000777 }else{
778 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000779 }
drh8e0a2f92002-02-23 23:45:45 +0000780 if( is_agg && !allowAgg ){
781 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000782 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000783 pParse->nErr++;
784 nErr++;
785 is_agg = 0;
786 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000787 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000788 pParse->nErr++;
789 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000790 }else if( wrong_num_args ){
791 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000792 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000793 pParse->nErr++;
794 nErr++;
drhcce7d172000-05-31 15:34:51 +0000795 }
drh22827922000-06-06 17:27:05 +0000796 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000797 if( is_agg && pIsAgg ) *pIsAgg = 1;
798 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000799 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
800 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000801 }
drhc9b84a12002-06-20 11:36:48 +0000802 if( pDef==0 ){
803 if( is_type_of ){
804 pExpr->op = TK_STRING;
805 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
806 pExpr->token.z = "numeric";
807 pExpr->token.n = 7;
808 }else{
809 pExpr->token.z = "text";
810 pExpr->token.n = 4;
811 }
812 }
813 }else if( pDef->dataType>=0 ){
814 if( pDef->dataType<n ){
815 pExpr->dataType =
816 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
817 }else{
818 pExpr->dataType = SQLITE_SO_NUM;
819 }
820 }else if( pDef->dataType==SQLITE_ARGS ){
821 pDef->dataType = SQLITE_SO_TEXT;
822 for(i=0; i<n; i++){
823 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
824 pExpr->dataType = SQLITE_SO_NUM;
825 break;
826 }
827 }
828 }else if( pDef->dataType==SQLITE_NUMERIC ){
829 pExpr->dataType = SQLITE_SO_NUM;
830 }else{
831 pExpr->dataType = SQLITE_SO_TEXT;
832 }
drhcce7d172000-05-31 15:34:51 +0000833 }
834 default: {
835 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000836 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000837 }
838 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000839 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000840 }
drhfef52082000-06-06 01:50:43 +0000841 if( nErr==0 && pExpr->pList ){
842 int n = pExpr->pList->nExpr;
843 int i;
844 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000845 Expr *pE2 = pExpr->pList->a[i].pExpr;
846 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000847 }
848 }
drhcce7d172000-05-31 15:34:51 +0000849 break;
850 }
851 }
852 return nErr;
853}
854
855/*
drhc9b84a12002-06-20 11:36:48 +0000856** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
857** given expression should sort as numeric values or as text.
858**
859** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
860** both been called on the expression before it is passed to this routine.
861*/
862int sqliteExprType(Expr *p){
863 if( p==0 ) return SQLITE_SO_NUM;
864 while( p ) switch( p->op ){
865 case TK_PLUS:
866 case TK_MINUS:
867 case TK_STAR:
868 case TK_SLASH:
869 case TK_AND:
870 case TK_OR:
871 case TK_ISNULL:
872 case TK_NOTNULL:
873 case TK_NOT:
874 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000875 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000876 case TK_BITAND:
877 case TK_BITOR:
878 case TK_BITNOT:
879 case TK_LSHIFT:
880 case TK_RSHIFT:
881 case TK_REM:
882 case TK_INTEGER:
883 case TK_FLOAT:
884 case TK_IN:
885 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000886 case TK_GLOB:
887 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000888 return SQLITE_SO_NUM;
889
890 case TK_STRING:
891 case TK_NULL:
892 case TK_CONCAT:
893 return SQLITE_SO_TEXT;
894
895 case TK_LT:
896 case TK_LE:
897 case TK_GT:
898 case TK_GE:
899 case TK_NE:
900 case TK_EQ:
901 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
902 return SQLITE_SO_NUM;
903 }
904 p = p->pRight;
905 break;
906
907 case TK_AS:
908 p = p->pLeft;
909 break;
910
911 case TK_COLUMN:
912 case TK_FUNCTION:
913 case TK_AGG_FUNCTION:
914 return p->dataType;
915
916 case TK_SELECT:
917 assert( p->pSelect );
918 assert( p->pSelect->pEList );
919 assert( p->pSelect->pEList->nExpr>0 );
920 p = p->pSelect->pEList->a[0].pExpr;
921 break;
922
drhb1363202002-06-26 02:45:03 +0000923 case TK_CASE: {
924 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
925 return SQLITE_SO_NUM;
926 }
927 if( p->pList ){
928 int i;
929 ExprList *pList = p->pList;
930 for(i=1; i<pList->nExpr; i+=2){
931 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
932 return SQLITE_SO_NUM;
933 }
934 }
935 }
936 return SQLITE_SO_TEXT;
937 }
938
drhc9b84a12002-06-20 11:36:48 +0000939 default:
940 assert( p->op==TK_ABORT ); /* Can't Happen */
941 break;
942 }
943 return SQLITE_SO_NUM;
944}
945
946/*
drhcce7d172000-05-31 15:34:51 +0000947** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000948** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000949*/
950void sqliteExprCode(Parse *pParse, Expr *pExpr){
951 Vdbe *v = pParse->pVdbe;
952 int op;
drhdaffd0e2001-04-11 14:28:42 +0000953 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000954 switch( pExpr->op ){
955 case TK_PLUS: op = OP_Add; break;
956 case TK_MINUS: op = OP_Subtract; break;
957 case TK_STAR: op = OP_Multiply; break;
958 case TK_SLASH: op = OP_Divide; break;
959 case TK_AND: op = OP_And; break;
960 case TK_OR: op = OP_Or; break;
961 case TK_LT: op = OP_Lt; break;
962 case TK_LE: op = OP_Le; break;
963 case TK_GT: op = OP_Gt; break;
964 case TK_GE: op = OP_Ge; break;
965 case TK_NE: op = OP_Ne; break;
966 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000967 case TK_ISNULL: op = OP_IsNull; break;
968 case TK_NOTNULL: op = OP_NotNull; break;
969 case TK_NOT: op = OP_Not; break;
970 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000971 case TK_BITAND: op = OP_BitAnd; break;
972 case TK_BITOR: op = OP_BitOr; break;
973 case TK_BITNOT: op = OP_BitNot; break;
974 case TK_LSHIFT: op = OP_ShiftLeft; break;
975 case TK_RSHIFT: op = OP_ShiftRight; break;
976 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000977 default: break;
978 }
979 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000980 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000981 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000982 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000983 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000984 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000985 }else{
drh99fcd712001-10-13 01:06:47 +0000986 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000987 }
drhcce7d172000-05-31 15:34:51 +0000988 break;
989 }
990 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000991 int iVal = atoi(pExpr->token.z);
992 char zBuf[30];
993 sprintf(zBuf,"%d",iVal);
994 if( strlen(zBuf)!=pExpr->token.n
995 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
996 /* If the integer value cannot be represented exactly in 32 bits,
997 ** then code it as a string instead. */
998 sqliteVdbeAddOp(v, OP_String, 0, 0);
999 }else{
1000 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
1001 }
drhe6840902002-03-06 03:08:25 +00001002 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1003 break;
1004 }
1005 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +00001006 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001007 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001008 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001009 break;
1010 }
drhcce7d172000-05-31 15:34:51 +00001011 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001012 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001013 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001014 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1015 sqliteVdbeDequoteP3(v, addr);
1016 break;
1017 }
1018 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001019 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001020 break;
1021 }
drhc9b84a12002-06-20 11:36:48 +00001022 case TK_LT:
1023 case TK_LE:
1024 case TK_GT:
1025 case TK_GE:
1026 case TK_NE:
1027 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001028 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001029 op += 6; /* Convert numeric opcodes to text opcodes */
1030 }
1031 /* Fall through into the next case */
1032 }
drhcce7d172000-05-31 15:34:51 +00001033 case TK_AND:
1034 case TK_OR:
1035 case TK_PLUS:
1036 case TK_STAR:
1037 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001038 case TK_REM:
1039 case TK_BITAND:
1040 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001041 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001042 sqliteExprCode(pParse, pExpr->pLeft);
1043 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001044 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001045 break;
1046 }
drhbf4133c2001-10-13 02:59:08 +00001047 case TK_LSHIFT:
1048 case TK_RSHIFT: {
1049 sqliteExprCode(pParse, pExpr->pRight);
1050 sqliteExprCode(pParse, pExpr->pLeft);
1051 sqliteVdbeAddOp(v, op, 0, 0);
1052 break;
1053 }
drh00400772000-06-16 20:51:26 +00001054 case TK_CONCAT: {
1055 sqliteExprCode(pParse, pExpr->pLeft);
1056 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001057 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001058 break;
1059 }
drh4b59ab52002-08-24 18:24:51 +00001060 case TK_UPLUS: {
1061 Expr *pLeft = pExpr->pLeft;
1062 if( pLeft && pLeft->op==TK_INTEGER ){
1063 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1064 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1065 }else if( pLeft && pLeft->op==TK_FLOAT ){
1066 sqliteVdbeAddOp(v, OP_String, 0, 0);
1067 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1068 }else{
1069 sqliteExprCode(pParse, pExpr->pLeft);
1070 }
1071 break;
1072 }
drhcce7d172000-05-31 15:34:51 +00001073 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001074 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001075 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001076 Token *p = &pExpr->pLeft->token;
1077 char *z = sqliteMalloc( p->n + 2 );
1078 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001079 if( pExpr->pLeft->op==TK_INTEGER ){
1080 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1081 }else{
1082 sqliteVdbeAddOp(v, OP_String, 0, 0);
1083 }
drh99fcd712001-10-13 01:06:47 +00001084 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001085 sqliteFree(z);
1086 break;
1087 }
drh1ccde152000-06-17 13:12:39 +00001088 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001089 }
drhbf4133c2001-10-13 02:59:08 +00001090 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001091 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001092 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001093 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001094 break;
1095 }
1096 case TK_ISNULL:
1097 case TK_NOTNULL: {
1098 int dest;
drh99fcd712001-10-13 01:06:47 +00001099 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001100 sqliteExprCode(pParse, pExpr->pLeft);
1101 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001102 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001103 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001104 break;
1105 }
drh22827922000-06-06 17:27:05 +00001106 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001107 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001108 break;
1109 }
drh4b59ab52002-08-24 18:24:51 +00001110 case TK_GLOB:
1111 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001112 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001113 int i;
1114 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001115 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001116 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001117 int nId;
1118 const char *zId;
1119 getFunctionName(pExpr, &zId, &nId);
1120 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001121 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001122 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001123 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001124 }
drh89425d52002-02-28 03:04:48 +00001125 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001126 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001127 break;
1128 }
drh19a775c2000-06-05 18:54:46 +00001129 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001130 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001131 break;
1132 }
drhfef52082000-06-06 01:50:43 +00001133 case TK_IN: {
1134 int addr;
drh99fcd712001-10-13 01:06:47 +00001135 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001136 sqliteExprCode(pParse, pExpr->pLeft);
1137 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001138 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1139 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1140 sqliteVdbeAddOp(v, OP_String, 0, 0);
1141 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001142 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001143 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001144 }else{
drhf5905aa2002-05-26 20:54:33 +00001145 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001146 }
drh99fcd712001-10-13 01:06:47 +00001147 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001148 break;
1149 }
1150 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001151 sqliteExprCode(pParse, pExpr->pLeft);
1152 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1153 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1154 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1155 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1156 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1157 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1158 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001159 break;
1160 }
drha2e00042002-01-22 03:13:42 +00001161 case TK_AS: {
1162 sqliteExprCode(pParse, pExpr->pLeft);
1163 break;
1164 }
drh17a7f8d2002-03-24 13:13:27 +00001165 case TK_CASE: {
1166 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001167 int jumpInst;
1168 int addr;
1169 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001170 int i;
1171
1172 assert(pExpr->pList);
1173 assert((pExpr->pList->nExpr % 2) == 0);
1174 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001175 nExpr = pExpr->pList->nExpr;
1176 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001177 if( pExpr->pLeft ){
1178 sqliteExprCode(pParse, pExpr->pLeft);
1179 }
drhf5905aa2002-05-26 20:54:33 +00001180 for(i=0; i<nExpr; i=i+2){
1181 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001182 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001183 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001184 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1185 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001186 }else{
drhf570f012002-05-31 15:51:25 +00001187 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001188 }
1189 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001190 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001191 addr = sqliteVdbeCurrentAddr(v);
1192 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001193 }
drhf570f012002-05-31 15:51:25 +00001194 if( pExpr->pLeft ){
1195 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1196 }
drh17a7f8d2002-03-24 13:13:27 +00001197 if( pExpr->pRight ){
1198 sqliteExprCode(pParse, pExpr->pRight);
1199 }else{
drhf5905aa2002-05-26 20:54:33 +00001200 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001201 }
drhf5905aa2002-05-26 20:54:33 +00001202 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001203 break;
1204 }
1205 case TK_RAISE: {
1206 if( !pParse->trigStack ){
1207 sqliteSetNString(&pParse->zErrMsg,
1208 "RAISE() may only be used within a trigger-program", -1, 0);
1209 pParse->nErr++;
1210 return;
1211 }
1212 if( pExpr->iColumn == OE_Rollback ||
1213 pExpr->iColumn == OE_Abort ||
1214 pExpr->iColumn == OE_Fail ){
1215 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1216 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1217 sqliteDequote(msg);
1218 sqliteVdbeChangeP3(v, -1, msg, 0);
1219 sqliteFree(msg);
1220 } else {
1221 assert( pExpr->iColumn == OE_Ignore );
1222 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1223 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
1224 }
drh17a7f8d2002-03-24 13:13:27 +00001225 }
1226 break;
drhcce7d172000-05-31 15:34:51 +00001227 }
drhcce7d172000-05-31 15:34:51 +00001228}
1229
1230/*
1231** Generate code for a boolean expression such that a jump is made
1232** to the label "dest" if the expression is true but execution
1233** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001234**
1235** If the expression evaluates to NULL (neither true nor false), then
1236** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001237*/
drhf5905aa2002-05-26 20:54:33 +00001238void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001239 Vdbe *v = pParse->pVdbe;
1240 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001241 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001242 switch( pExpr->op ){
1243 case TK_LT: op = OP_Lt; break;
1244 case TK_LE: op = OP_Le; break;
1245 case TK_GT: op = OP_Gt; break;
1246 case TK_GE: op = OP_Ge; break;
1247 case TK_NE: op = OP_Ne; break;
1248 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001249 case TK_ISNULL: op = OP_IsNull; break;
1250 case TK_NOTNULL: op = OP_NotNull; break;
1251 default: break;
1252 }
1253 switch( pExpr->op ){
1254 case TK_AND: {
1255 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001256 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1257 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001258 sqliteVdbeResolveLabel(v, d2);
1259 break;
1260 }
1261 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001262 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1263 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001264 break;
1265 }
1266 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001267 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001268 break;
1269 }
1270 case TK_LT:
1271 case TK_LE:
1272 case TK_GT:
1273 case TK_GE:
1274 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001275 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001276 sqliteExprCode(pParse, pExpr->pLeft);
1277 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001278 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001279 op += 6; /* Convert numeric opcodes to text opcodes */
1280 }
drhf5905aa2002-05-26 20:54:33 +00001281 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001282 break;
1283 }
1284 case TK_ISNULL:
1285 case TK_NOTNULL: {
1286 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001287 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001288 break;
1289 }
drhfef52082000-06-06 01:50:43 +00001290 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001291 int addr;
drhcfab11b2000-06-06 03:31:22 +00001292 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001293 addr = sqliteVdbeCurrentAddr(v);
1294 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1295 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1296 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001297 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001298 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001299 }else{
drh99fcd712001-10-13 01:06:47 +00001300 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001301 }
1302 break;
1303 }
1304 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001305 int addr;
drhfef52082000-06-06 01:50:43 +00001306 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001307 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001308 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001309 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001310 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001311 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001312 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001313 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001314 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001315 break;
1316 }
drhcce7d172000-05-31 15:34:51 +00001317 default: {
1318 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001319 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001320 break;
1321 }
1322 }
1323}
1324
1325/*
drh66b89c82000-11-28 20:47:17 +00001326** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001327** to the label "dest" if the expression is false but execution
1328** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001329**
1330** If the expression evaluates to NULL (neither true nor false) then
1331** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001332*/
drhf5905aa2002-05-26 20:54:33 +00001333void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001334 Vdbe *v = pParse->pVdbe;
1335 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001336 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001337 switch( pExpr->op ){
1338 case TK_LT: op = OP_Ge; break;
1339 case TK_LE: op = OP_Gt; break;
1340 case TK_GT: op = OP_Le; break;
1341 case TK_GE: op = OP_Lt; break;
1342 case TK_NE: op = OP_Eq; break;
1343 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001344 case TK_ISNULL: op = OP_NotNull; break;
1345 case TK_NOTNULL: op = OP_IsNull; break;
1346 default: break;
1347 }
1348 switch( pExpr->op ){
1349 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001350 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1351 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001352 break;
1353 }
1354 case TK_OR: {
1355 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001356 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1357 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001358 sqliteVdbeResolveLabel(v, d2);
1359 break;
1360 }
1361 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001362 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001363 break;
1364 }
1365 case TK_LT:
1366 case TK_LE:
1367 case TK_GT:
1368 case TK_GE:
1369 case TK_NE:
1370 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001371 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001372 /* Convert numeric comparison opcodes into text comparison opcodes.
1373 ** This step depends on the fact that the text comparision opcodes are
1374 ** always 6 greater than their corresponding numeric comparison
1375 ** opcodes.
1376 */
1377 assert( OP_Eq+6 == OP_StrEq );
1378 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001379 }
drhcce7d172000-05-31 15:34:51 +00001380 sqliteExprCode(pParse, pExpr->pLeft);
1381 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001382 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001383 break;
1384 }
drhcce7d172000-05-31 15:34:51 +00001385 case TK_ISNULL:
1386 case TK_NOTNULL: {
1387 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001388 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001389 break;
1390 }
drhfef52082000-06-06 01:50:43 +00001391 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001392 int addr;
drhcfab11b2000-06-06 03:31:22 +00001393 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001394 addr = sqliteVdbeCurrentAddr(v);
1395 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1396 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1397 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001398 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001399 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001400 }else{
drh99fcd712001-10-13 01:06:47 +00001401 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001402 }
1403 break;
1404 }
1405 case TK_BETWEEN: {
1406 int addr;
1407 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001408 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001409 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1410 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001411 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001412 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1413 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001414 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001415 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001416 break;
1417 }
drhcce7d172000-05-31 15:34:51 +00001418 default: {
1419 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001420 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001421 break;
1422 }
1423 }
1424}
drh22827922000-06-06 17:27:05 +00001425
1426/*
1427** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1428** if they are identical and return FALSE if they differ in any way.
1429*/
drhd8bc7082000-06-07 23:51:50 +00001430int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001431 int i;
1432 if( pA==0 ){
1433 return pB==0;
1434 }else if( pB==0 ){
1435 return 0;
1436 }
1437 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001438 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1439 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001440 if( pA->pList ){
1441 if( pB->pList==0 ) return 0;
1442 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1443 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001444 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001445 return 0;
1446 }
1447 }
1448 }else if( pB->pList ){
1449 return 0;
1450 }
1451 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001452 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001453 if( pA->token.z ){
drh4b59ab52002-08-24 18:24:51 +00001454 int n;
drh22827922000-06-06 17:27:05 +00001455 if( pB->token.z==0 ) return 0;
drh4b59ab52002-08-24 18:24:51 +00001456 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
1457 n = pA->nFuncName;
1458 if( pB->nFuncName!=n ) return 0;
1459 }else{
1460 n = pA->token.n;
1461 if( pB->token.n!=n ) return 0;
1462 }
1463 if( sqliteStrNICmp(pA->token.z, pB->token.z, n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001464 }
1465 return 1;
1466}
1467
1468/*
1469** Add a new element to the pParse->aAgg[] array and return its index.
1470*/
1471static int appendAggInfo(Parse *pParse){
1472 if( (pParse->nAgg & 0x7)==0 ){
1473 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001474 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1475 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001476 return -1;
1477 }
drh6d4abfb2001-10-22 02:58:08 +00001478 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001479 }
1480 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1481 return pParse->nAgg++;
1482}
1483
1484/*
1485** Analyze the given expression looking for aggregate functions and
1486** for variables that need to be added to the pParse->aAgg[] array.
1487** Make additional entries to the pParse->aAgg[] array as necessary.
1488**
1489** This routine should only be called after the expression has been
1490** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1491**
1492** If errors are seen, leave an error message in zErrMsg and return
1493** the number of errors.
1494*/
1495int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1496 int i;
1497 AggExpr *aAgg;
1498 int nErr = 0;
1499
1500 if( pExpr==0 ) return 0;
1501 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001502 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001503 aAgg = pParse->aAgg;
1504 for(i=0; i<pParse->nAgg; i++){
1505 if( aAgg[i].isAgg ) continue;
1506 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001507 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001508 break;
1509 }
1510 }
1511 if( i>=pParse->nAgg ){
1512 i = appendAggInfo(pParse);
1513 if( i<0 ) return 1;
1514 pParse->aAgg[i].isAgg = 0;
1515 pParse->aAgg[i].pExpr = pExpr;
1516 }
drhaaf88722000-06-08 11:25:00 +00001517 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001518 break;
1519 }
1520 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001521 aAgg = pParse->aAgg;
1522 for(i=0; i<pParse->nAgg; i++){
1523 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001524 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001525 break;
1526 }
1527 }
1528 if( i>=pParse->nAgg ){
1529 i = appendAggInfo(pParse);
1530 if( i<0 ) return 1;
1531 pParse->aAgg[i].isAgg = 1;
1532 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001533 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh4b59ab52002-08-24 18:24:51 +00001534 pExpr->token.z, pExpr->nFuncName,
drhf55f25f2002-02-28 01:46:11 +00001535 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001536 }
1537 pExpr->iAgg = i;
1538 break;
1539 }
1540 default: {
1541 if( pExpr->pLeft ){
1542 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1543 }
1544 if( nErr==0 && pExpr->pRight ){
1545 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1546 }
1547 if( nErr==0 && pExpr->pList ){
1548 int n = pExpr->pList->nExpr;
1549 int i;
1550 for(i=0; nErr==0 && i<n; i++){
1551 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1552 }
1553 }
1554 break;
1555 }
1556 }
1557 return nErr;
1558}
drh8e0a2f92002-02-23 23:45:45 +00001559
1560/*
1561** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001562** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001563** function, or return NULL if the function does not exist.
1564**
drh0bce8352002-02-28 00:41:10 +00001565** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001566** structure is created and liked into the "db" structure if a
1567** no matching function previously existed. When createFlag is true
1568** and the nArg parameter is -1, then only a function that accepts
1569** any number of arguments will be returned.
1570**
1571** If createFlag is false and nArg is -1, then the first valid
1572** function found is returned. A function is valid if either xFunc
1573** or xStep is non-zero.
1574*/
drh0bce8352002-02-28 00:41:10 +00001575FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001576 sqlite *db, /* An open database */
1577 const char *zName, /* Name of the function. Not null-terminated */
1578 int nName, /* Number of characters in the name */
1579 int nArg, /* Number of arguments. -1 means any number */
1580 int createFlag /* Create new entry if true and does not otherwise exist */
1581){
drh0bce8352002-02-28 00:41:10 +00001582 FuncDef *pFirst, *p, *pMaybe;
1583 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001584 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001585 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1586 return p;
1587 }
1588 pMaybe = 0;
1589 while( p && p->nArg!=nArg ){
1590 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1591 p = p->pNext;
1592 }
1593 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1594 return 0;
1595 }
1596 if( p==0 && pMaybe ){
1597 assert( createFlag==0 );
1598 return pMaybe;
1599 }
drh89425d52002-02-28 03:04:48 +00001600 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001601 p->nArg = nArg;
1602 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001603 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001604 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001605 }
1606 return p;
1607}