blob: 4798ddb3b80c64bb9702d9721721349b75c8206f [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**
drh0c36cbe2002-07-16 02:05:43 +000015** $Id: expr.c,v 1.78 2002/07/16 02:05:44 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 ){
37 pNew->token = *pToken;
38 }else{
39 pNew->token.z = 0;
40 pNew->token.n = 0;
41 }
42 if( pLeft && pRight ){
43 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
44 }else{
45 pNew->span = pNew->token;
46 }
47 return pNew;
48}
49
50/*
51** Set the Expr.token field of the given expression to span all
52** text between the two given tokens.
53*/
54void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
55 if( pExpr ){
56 pExpr->span.z = pLeft->z;
57 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
58 }
59}
60
61/*
62** Construct a new expression node for a function with multiple
63** arguments.
64*/
65Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
66 Expr *pNew;
67 pNew = sqliteMalloc( sizeof(Expr) );
68 if( pNew==0 ){
69 sqliteExprListDelete(pList);
70 return 0;
71 }
72 pNew->op = TK_FUNCTION;
73 pNew->pList = pList;
74 if( pToken ){
75 pNew->token = *pToken;
76 }else{
77 pNew->token.z = 0;
78 pNew->token.n = 0;
79 }
80 return pNew;
81}
82
83/*
drha2e00042002-01-22 03:13:42 +000084** Recursively delete an expression tree.
85*/
86void sqliteExprDelete(Expr *p){
87 if( p==0 ) return;
drh75148a22002-03-03 03:42:31 +000088 if( p->pLeft ) sqliteExprDelete(p->pLeft);
89 if( p->pRight ) sqliteExprDelete(p->pRight);
drha2e00042002-01-22 03:13:42 +000090 if( p->pList ) sqliteExprListDelete(p->pList);
91 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
92 sqliteFree(p);
93}
94
drhcce7d172000-05-31 15:34:51 +000095/*
drha76b5df2002-02-23 02:32:10 +000096** The following group of functions are used to translate the string
97** pointers of tokens in expression from one buffer to another.
98**
99** Normally, the Expr.token.z and Expr.span.z fields point into the
100** original input buffer of an SQL statement. This is usually OK
101** since the SQL statement is executed and the expression is deleted
102** before the input buffer is freed. Making the tokens point to the
103** original input buffer saves many calls to malloc() and thus helps
104** the library to run faster.
105**
106** But sometimes we need an expression to persist past the time when
107** the input buffer is freed. (Example: The SELECT clause of a
108** CREATE VIEW statement contains expressions that must persist for
109** the life of the view.) When that happens we have to make a
110** persistent copy of the input buffer and translate the Expr.token.z
111** and Expr.span.z fields to point to the copy rather than the
drha2ed5602002-02-26 23:55:31 +0000112** original input buffer. The following group of routines handle that
drha76b5df2002-02-23 02:32:10 +0000113** translation.
114**
115** The "offset" parameter is the distance from the original input buffer
116** to the persistent copy. These routines recursively walk the entire
117** expression tree and shift all tokens by "offset" amount.
118**
119** The work of figuring out the appropriate "offset" and making the
120** presistent copy of the input buffer is done by the calling routine.
121*/
122void sqliteExprMoveStrings(Expr *p, int offset){
123 if( p==0 ) return;
drh3b167c72002-06-28 12:18:47 +0000124 if( !p->staticToken ){
125 if( p->token.z ) p->token.z += offset;
126 if( p->span.z ) p->span.z += offset;
127 }
drha76b5df2002-02-23 02:32:10 +0000128 if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset);
129 if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset);
130 if( p->pList ) sqliteExprListMoveStrings(p->pList, offset);
131 if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset);
132}
133void sqliteExprListMoveStrings(ExprList *pList, int offset){
134 int i;
135 if( pList==0 ) return;
136 for(i=0; i<pList->nExpr; i++){
137 sqliteExprMoveStrings(pList->a[i].pExpr, offset);
138 }
139}
drh0c36cbe2002-07-16 02:05:43 +0000140static void sqliteSrcListMoveStrings(SrcList *pSrc, int offset){
141 int i;
142 if( pSrc==0 ) return;
143 for(i=0; i<pSrc->nSrc; i++){
144 sqliteSelectMoveStrings(pSrc->a[i].pSelect, offset);
145 sqliteExprMoveStrings(pSrc->a[i].pOn, offset);
146 }
147}
drha76b5df2002-02-23 02:32:10 +0000148void sqliteSelectMoveStrings(Select *pSelect, int offset){
149 if( pSelect==0 ) return;
150 sqliteExprListMoveStrings(pSelect->pEList, offset);
drh0c36cbe2002-07-16 02:05:43 +0000151 sqliteSrcListMoveStrings(pSelect->pSrc, offset);
drha76b5df2002-02-23 02:32:10 +0000152 sqliteExprMoveStrings(pSelect->pWhere, offset);
153 sqliteExprListMoveStrings(pSelect->pGroupBy, offset);
154 sqliteExprMoveStrings(pSelect->pHaving, offset);
155 sqliteExprListMoveStrings(pSelect->pOrderBy, offset);
156 sqliteSelectMoveStrings(pSelect->pPrior, offset);
157}
158
159/*
drhff78bd22002-02-27 01:47:11 +0000160** The following group of routines make deep copies of expressions,
161** expression lists, ID lists, and select statements. The copies can
162** be deleted (by being passed to their respective ...Delete() routines)
163** without effecting the originals.
164**
165** Note, however, that the Expr.token.z and Expr.span.z fields point to
166** string space that is allocated separately from the expression tree
167** itself. These routines do NOT duplicate that string space.
168**
drhad3cab52002-05-24 02:04:32 +0000169** The expression list, ID, and source lists return by sqliteExprListDup(),
170** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
171** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000172**
drhad3cab52002-05-24 02:04:32 +0000173** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000174*/
175Expr *sqliteExprDup(Expr *p){
176 Expr *pNew;
177 if( p==0 ) return 0;
178 pNew = sqliteMalloc( sizeof(*p) );
179 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000180 memcpy(pNew, p, sizeof(*pNew));
drhff78bd22002-02-27 01:47:11 +0000181 pNew->pLeft = sqliteExprDup(p->pLeft);
182 pNew->pRight = sqliteExprDup(p->pRight);
183 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000184 pNew->pSelect = sqliteSelectDup(p->pSelect);
185 return pNew;
186}
187ExprList *sqliteExprListDup(ExprList *p){
188 ExprList *pNew;
189 int i;
190 if( p==0 ) return 0;
191 pNew = sqliteMalloc( sizeof(*pNew) );
192 if( pNew==0 ) return 0;
193 pNew->nExpr = p->nExpr;
194 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000195 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000196 for(i=0; i<p->nExpr; i++){
197 pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
198 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
199 pNew->a[i].sortOrder = p->a[i].sortOrder;
200 pNew->a[i].isAgg = p->a[i].isAgg;
201 pNew->a[i].done = 0;
202 }
203 return pNew;
204}
drhad3cab52002-05-24 02:04:32 +0000205SrcList *sqliteSrcListDup(SrcList *p){
206 SrcList *pNew;
207 int i;
208 if( p==0 ) return 0;
209 pNew = sqliteMalloc( sizeof(*pNew) );
210 if( pNew==0 ) return 0;
211 pNew->nSrc = p->nSrc;
212 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
danielk19776f349032002-06-11 02:25:40 +0000213 if( pNew->a==0 && p->nSrc != 0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000214 for(i=0; i<p->nSrc; i++){
215 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
216 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
217 pNew->a[i].jointype = p->a[i].jointype;
218 pNew->a[i].pTab = 0;
219 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
220 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
221 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
222 }
223 return pNew;
224}
drhff78bd22002-02-27 01:47:11 +0000225IdList *sqliteIdListDup(IdList *p){
226 IdList *pNew;
227 int i;
228 if( p==0 ) return 0;
229 pNew = sqliteMalloc( sizeof(*pNew) );
230 if( pNew==0 ) return 0;
231 pNew->nId = p->nId;
232 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000233 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000234 for(i=0; i<p->nId; i++){
235 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000236 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000237 }
238 return pNew;
239}
240Select *sqliteSelectDup(Select *p){
241 Select *pNew;
242 if( p==0 ) return 0;
243 pNew = sqliteMalloc( sizeof(*p) );
244 if( pNew==0 ) return 0;
245 pNew->isDistinct = p->isDistinct;
246 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000247 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000248 pNew->pWhere = sqliteExprDup(p->pWhere);
249 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
250 pNew->pHaving = sqliteExprDup(p->pHaving);
251 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
252 pNew->op = p->op;
253 pNew->pPrior = sqliteSelectDup(p->pPrior);
254 pNew->nLimit = p->nLimit;
255 pNew->nOffset = p->nOffset;
256 pNew->zSelect = 0;
257 return pNew;
258}
259
260
261/*
drha76b5df2002-02-23 02:32:10 +0000262** Add a new element to the end of an expression list. If pList is
263** initially NULL, then create a new expression list.
264*/
265ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
266 int i;
267 if( pList==0 ){
268 pList = sqliteMalloc( sizeof(ExprList) );
269 if( pList==0 ){
270 sqliteExprDelete(pExpr);
271 return 0;
272 }
273 }
274 if( (pList->nExpr & 7)==0 ){
275 int n = pList->nExpr + 8;
276 struct ExprList_item *a;
277 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
278 if( a==0 ){
279 sqliteExprDelete(pExpr);
280 return pList;
281 }
282 pList->a = a;
283 }
284 if( pExpr || pName ){
285 i = pList->nExpr++;
286 pList->a[i].pExpr = pExpr;
287 pList->a[i].zName = 0;
288 if( pName ){
289 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
290 sqliteDequote(pList->a[i].zName);
291 }
292 }
293 return pList;
294}
295
296/*
297** Delete an entire expression list.
298*/
299void sqliteExprListDelete(ExprList *pList){
300 int i;
301 if( pList==0 ) return;
302 for(i=0; i<pList->nExpr; i++){
303 sqliteExprDelete(pList->a[i].pExpr);
304 sqliteFree(pList->a[i].zName);
305 }
306 sqliteFree(pList->a);
307 sqliteFree(pList);
308}
309
310/*
drhfef52082000-06-06 01:50:43 +0000311** Walk an expression tree. Return 1 if the expression is constant
312** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000313**
314** For the purposes of this function, a double-quoted string (ex: "abc")
315** is considered a variable but a single-quoted string (ex: 'abc') is
316** a constant.
drhfef52082000-06-06 01:50:43 +0000317*/
drh92086432002-01-22 14:11:29 +0000318int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000319 switch( p->op ){
320 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000321 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000322 case TK_DOT:
323 return 0;
drh23989372002-05-21 13:43:04 +0000324 case TK_STRING:
325 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000326 case TK_INTEGER:
327 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000328 return 1;
drhfef52082000-06-06 01:50:43 +0000329 default: {
drh92086432002-01-22 14:11:29 +0000330 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
331 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000332 if( p->pList ){
333 int i;
334 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000335 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000336 }
337 }
drh92086432002-01-22 14:11:29 +0000338 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000339 }
340 }
drh92086432002-01-22 14:11:29 +0000341 return 0;
drhfef52082000-06-06 01:50:43 +0000342}
343
344/*
drhe4de1fe2002-06-02 16:09:01 +0000345** If the given expression codes a constant integer, return 1 and put
346** the value of the integer in *pValue. If the expression is not an
347** integer, return 0 and leave *pValue unchanged.
348*/
349int sqliteExprIsInteger(Expr *p, int *pValue){
350 switch( p->op ){
351 case TK_INTEGER: {
352 *pValue = atoi(p->token.z);
353 return 1;
354 }
355 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000356 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000357 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000358 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000359 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
360 if( n==0 ){
361 *pValue = atoi(p->token.z);
362 return 1;
363 }
364 break;
365 }
366 case TK_UMINUS: {
367 int v;
368 if( sqliteExprIsInteger(p->pLeft, &v) ){
369 *pValue = -v;
370 return 1;
371 }
372 break;
373 }
374 default: break;
375 }
376 return 0;
377}
378
379/*
drhc4a3c772001-04-04 11:48:57 +0000380** Return TRUE if the given string is a row-id column name.
381*/
drha9f9d1c2002-06-29 02:20:08 +0000382int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000383 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
384 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
385 if( sqliteStrICmp(z, "OID")==0 ) return 1;
386 return 0;
387}
388
389/*
drhcce7d172000-05-31 15:34:51 +0000390** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000391** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000392** index to the table in the table list and a column offset. The
393** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
394** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000395** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000396** VDBE cursor number for a cursor that is pointing into the referenced
397** table. The Expr.iColumn value is changed to the index of the column
398** of the referenced table. The Expr.iColumn value for the special
399** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
400** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000401**
drhfef52082000-06-06 01:50:43 +0000402** We also check for instances of the IN operator. IN comes in two
403** forms:
404**
405** expr IN (exprlist)
406** and
407** expr IN (SELECT ...)
408**
409** The first form is handled by creating a set holding the list
410** of allowed values. The second form causes the SELECT to generate
411** a temporary table.
412**
413** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000414** If it finds any, it generates code to write the value of that select
415** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000416**
drh967e8b72000-06-21 13:59:10 +0000417** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000418** the number of errors seen and leaves an error message on pParse->zErrMsg.
419*/
drha2e00042002-01-22 03:13:42 +0000420int sqliteExprResolveIds(
421 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000422 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000423 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000424 ExprList *pEList, /* List of expressions used to resolve "AS" */
425 Expr *pExpr /* The expression to be analyzed. */
426){
drhdaffd0e2001-04-11 14:28:42 +0000427 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000428 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000429 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000430 /* Double-quoted strings (ex: "abc") are used as identifiers if
431 ** possible. Otherwise they remain as strings. Single-quoted
432 ** strings (ex: 'abc') are always string literals.
433 */
434 case TK_STRING: {
435 if( pExpr->token.z[0]=='\'' ) break;
436 /* Fall thru into the TK_ID case if this is a double-quoted string */
437 }
drha2e00042002-01-22 03:13:42 +0000438 /* A lone identifier. Try and match it as follows:
439 **
440 ** 1. To the name of a column of one of the tables in pTabList
441 **
442 ** 2. To the right side of an AS keyword in the column list of
443 ** a SELECT statement. (For example, match against 'x' in
444 ** "SELECT a+b AS 'x' FROM t1".)
445 **
446 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
447 */
drhcce7d172000-05-31 15:34:51 +0000448 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000449 int cnt = 0; /* Number of matches */
450 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000451 char *z;
452 assert( pExpr->token.z );
453 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000454 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000455 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000456 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000457 int j;
458 Table *pTab = pTabList->a[i].pTab;
459 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000460 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000461 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000462 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000463 cnt++;
drh832508b2002-03-02 17:04:07 +0000464 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000465 if( j==pTab->iPKey ){
466 /* Substitute the record number for the INTEGER PRIMARY KEY */
467 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000468 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000469 }else{
470 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000471 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000472 }
drha2e00042002-01-22 03:13:42 +0000473 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000474 }
475 }
476 }
drha2e00042002-01-22 03:13:42 +0000477 if( cnt==0 && pEList!=0 ){
478 int j;
479 for(j=0; j<pEList->nExpr; j++){
480 char *zAs = pEList->a[j].zName;
481 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
482 cnt++;
483 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
484 pExpr->op = TK_AS;
485 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000486 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000487 }
488 }
489 }
drhc4a3c772001-04-04 11:48:57 +0000490 if( cnt==0 && sqliteIsRowid(z) ){
491 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000492 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000493 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000494 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000495 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000496 }
drhcce7d172000-05-31 15:34:51 +0000497 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000498 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000499 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000500 pExpr->token.z, pExpr->token.n, 0);
501 pParse->nErr++;
502 return 1;
503 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000504 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000505 pExpr->token.z, pExpr->token.n, 0);
506 pParse->nErr++;
507 return 1;
508 }
drhcce7d172000-05-31 15:34:51 +0000509 break;
510 }
511
drh967e8b72000-06-21 13:59:10 +0000512 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000513 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000514 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000515 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000516 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000517 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000518 char *zLeft, *zRight; /* Text of an identifier */
519
520 pLeft = pExpr->pLeft;
521 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000522 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
523 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000524 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
525 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000526 if( zLeft==0 || zRight==0 ){
527 sqliteFree(zLeft);
528 sqliteFree(zRight);
529 return 1;
530 }
drh87c40e82001-07-23 14:33:02 +0000531 sqliteDequote(zLeft);
532 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000533 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000534 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000535 int j;
536 char *zTab;
537 Table *pTab = pTabList->a[i].pTab;
538 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000539 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000540 if( pTabList->a[i].zAlias ){
541 zTab = pTabList->a[i].zAlias;
542 }else{
543 zTab = pTab->zName;
544 }
drh094b2bb2002-03-13 18:54:07 +0000545 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000546 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000547 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000548 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000549 cnt++;
drh832508b2002-03-02 17:04:07 +0000550 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000551 if( j==pTab->iPKey ){
552 /* Substitute the record number for the INTEGER PRIMARY KEY */
553 pExpr->iColumn = -1;
554 }else{
555 pExpr->iColumn = j;
556 }
drhc9b84a12002-06-20 11:36:48 +0000557 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000558 }
559 }
560 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000561
562 /* If we have not already resolved this *.* expression, then maybe
563 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000564 if( cnt == 0 && pParse->trigStack != 0 ){
565 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000566 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000567 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
568 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000569 cntTab++;
570 t = 1;
571 }
danielk1977f29ce552002-05-19 23:43:12 +0000572 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
573 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000574 cntTab++;
575 t = 1;
576 }
577
danielk1977f29ce552002-05-19 23:43:12 +0000578 if( t ){
579 int j;
drhc9b84a12002-06-20 11:36:48 +0000580 Table *pTab = pTriggerStack->pTab;
581 for(j=0; j < pTab->nCol; j++) {
582 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000583 cnt++;
584 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000585 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000586 }
587 }
danielk1977f29ce552002-05-19 23:43:12 +0000588 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000589 }
590
drhc4a3c772001-04-04 11:48:57 +0000591 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
592 cnt = 1;
593 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000594 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000595 }
drhcce7d172000-05-31 15:34:51 +0000596 sqliteFree(zLeft);
597 sqliteFree(zRight);
598 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000599 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000600 pLeft->token.z, pLeft->token.n, ".", 1,
601 pRight->token.z, pRight->token.n, 0);
602 pParse->nErr++;
603 return 1;
604 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000605 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000606 pLeft->token.z, pLeft->token.n, ".", 1,
607 pRight->token.z, pRight->token.n, 0);
608 pParse->nErr++;
609 return 1;
610 }
611 sqliteExprDelete(pLeft);
612 pExpr->pLeft = 0;
613 sqliteExprDelete(pRight);
614 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000615 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000616 break;
617 }
618
drhfef52082000-06-06 01:50:43 +0000619 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000620 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000621 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000622 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000623 return 1;
624 }
drhfef52082000-06-06 01:50:43 +0000625 if( pExpr->pSelect ){
626 /* Case 1: expr IN (SELECT ...)
627 **
628 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000629 ** table. The cursor number of the temporary table has already
630 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000631 */
drh832508b2002-03-02 17:04:07 +0000632 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000633 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000634 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000635 }else if( pExpr->pList ){
636 /* Case 2: expr IN (exprlist)
637 **
638 ** Create a set to put the exprlist values in. The Set id is stored
639 ** in iTable.
640 */
641 int i, iSet;
642 for(i=0; i<pExpr->pList->nExpr; i++){
643 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000644 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000645 sqliteSetString(&pParse->zErrMsg,
646 "right-hand side of IN operator must be constant", 0);
647 pParse->nErr++;
648 return 1;
649 }
drh4794b982000-06-06 13:54:14 +0000650 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
651 return 1;
652 }
drhfef52082000-06-06 01:50:43 +0000653 }
654 iSet = pExpr->iTable = pParse->nSet++;
655 for(i=0; i<pExpr->pList->nExpr; i++){
656 Expr *pE2 = pExpr->pList->a[i].pExpr;
657 switch( pE2->op ){
658 case TK_FLOAT:
659 case TK_INTEGER:
660 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000661 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000662 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000663 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
664 sqliteVdbeDequoteP3(v, addr);
665 break;
666 }
667 default: {
668 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000669 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000670 break;
671 }
672 }
673 }
674 }
drhcfab11b2000-06-06 03:31:22 +0000675 break;
drhfef52082000-06-06 01:50:43 +0000676 }
677
drh19a775c2000-06-05 18:54:46 +0000678 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000679 /* This has to be a scalar SELECT. Generate code to put the
680 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000681 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000682 */
drh967e8b72000-06-21 13:59:10 +0000683 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000684 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000685 return 1;
686 }
687 break;
688 }
689
drhcce7d172000-05-31 15:34:51 +0000690 /* For all else, just recursively walk the tree */
691 default: {
drh4794b982000-06-06 13:54:14 +0000692 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000693 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000694 return 1;
695 }
696 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000697 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000698 return 1;
699 }
700 if( pExpr->pList ){
701 int i;
702 ExprList *pList = pExpr->pList;
703 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000704 Expr *pArg = pList->a[i].pExpr;
705 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000706 return 1;
707 }
708 }
709 }
710 }
711 }
712 return 0;
713}
714
drhcce7d172000-05-31 15:34:51 +0000715/*
716** Error check the functions in an expression. Make sure all
717** function names are recognized and all functions have the correct
718** number of arguments. Leave an error message in pParse->zErrMsg
719** if anything is amiss. Return the number of errors.
720**
721** if pIsAgg is not null and this expression is an aggregate function
722** (like count(*) or max(value)) then write a 1 into *pIsAgg.
723*/
724int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
725 int nErr = 0;
726 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000727 switch( pExpr->op ){
728 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000729 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
730 int no_such_func = 0; /* True if no such function exists */
731 int is_type_of = 0; /* True if is the special TypeOf() function */
732 int wrong_num_args = 0; /* True if wrong number of arguments */
733 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000734 int i;
drh0bce8352002-02-28 00:41:10 +0000735 FuncDef *pDef;
736
drh89425d52002-02-28 03:04:48 +0000737 pDef = sqliteFindFunction(pParse->db,
738 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000739 if( pDef==0 ){
740 pDef = sqliteFindFunction(pParse->db,
741 pExpr->token.z, pExpr->token.n, -1, 0);
742 if( pDef==0 ){
drhc9b84a12002-06-20 11:36:48 +0000743 if( n==1 && pExpr->token.n==6
744 && sqliteStrNICmp(pExpr->token.z, "typeof", 6)==0 ){
745 is_type_of = 1;
746 }else {
747 no_such_func = 1;
748 }
drh0bce8352002-02-28 00:41:10 +0000749 }else{
750 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000751 }
drh0bce8352002-02-28 00:41:10 +0000752 }else{
753 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000754 }
drh8e0a2f92002-02-23 23:45:45 +0000755 if( is_agg && !allowAgg ){
756 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
757 pExpr->token.z, pExpr->token.n, "()", 2, 0);
758 pParse->nErr++;
759 nErr++;
760 is_agg = 0;
761 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000762 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
763 pExpr->token.z, pExpr->token.n, 0);
764 pParse->nErr++;
765 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000766 }else if( wrong_num_args ){
767 sqliteSetNString(&pParse->zErrMsg,
768 "wrong number of arguments to function ",-1,
769 pExpr->token.z, pExpr->token.n, "()", 2, 0);
770 pParse->nErr++;
771 nErr++;
drhcce7d172000-05-31 15:34:51 +0000772 }
drh22827922000-06-06 17:27:05 +0000773 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000774 if( is_agg && pIsAgg ) *pIsAgg = 1;
775 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000776 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
777 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000778 }
drhc9b84a12002-06-20 11:36:48 +0000779 if( pDef==0 ){
780 if( is_type_of ){
781 pExpr->op = TK_STRING;
782 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
783 pExpr->token.z = "numeric";
784 pExpr->token.n = 7;
785 }else{
786 pExpr->token.z = "text";
787 pExpr->token.n = 4;
788 }
789 }
790 }else if( pDef->dataType>=0 ){
791 if( pDef->dataType<n ){
792 pExpr->dataType =
793 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
794 }else{
795 pExpr->dataType = SQLITE_SO_NUM;
796 }
797 }else if( pDef->dataType==SQLITE_ARGS ){
798 pDef->dataType = SQLITE_SO_TEXT;
799 for(i=0; i<n; i++){
800 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
801 pExpr->dataType = SQLITE_SO_NUM;
802 break;
803 }
804 }
805 }else if( pDef->dataType==SQLITE_NUMERIC ){
806 pExpr->dataType = SQLITE_SO_NUM;
807 }else{
808 pExpr->dataType = SQLITE_SO_TEXT;
809 }
drhcce7d172000-05-31 15:34:51 +0000810 }
811 default: {
812 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000813 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000814 }
815 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000816 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000817 }
drhfef52082000-06-06 01:50:43 +0000818 if( nErr==0 && pExpr->pList ){
819 int n = pExpr->pList->nExpr;
820 int i;
821 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000822 Expr *pE2 = pExpr->pList->a[i].pExpr;
823 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000824 }
825 }
drhcce7d172000-05-31 15:34:51 +0000826 break;
827 }
828 }
829 return nErr;
830}
831
832/*
drhc9b84a12002-06-20 11:36:48 +0000833** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
834** given expression should sort as numeric values or as text.
835**
836** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
837** both been called on the expression before it is passed to this routine.
838*/
839int sqliteExprType(Expr *p){
840 if( p==0 ) return SQLITE_SO_NUM;
841 while( p ) switch( p->op ){
842 case TK_PLUS:
843 case TK_MINUS:
844 case TK_STAR:
845 case TK_SLASH:
846 case TK_AND:
847 case TK_OR:
848 case TK_ISNULL:
849 case TK_NOTNULL:
850 case TK_NOT:
851 case TK_UMINUS:
852 case TK_BITAND:
853 case TK_BITOR:
854 case TK_BITNOT:
855 case TK_LSHIFT:
856 case TK_RSHIFT:
857 case TK_REM:
858 case TK_INTEGER:
859 case TK_FLOAT:
860 case TK_IN:
861 case TK_BETWEEN:
862 return SQLITE_SO_NUM;
863
864 case TK_STRING:
865 case TK_NULL:
866 case TK_CONCAT:
867 return SQLITE_SO_TEXT;
868
869 case TK_LT:
870 case TK_LE:
871 case TK_GT:
872 case TK_GE:
873 case TK_NE:
874 case TK_EQ:
875 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
876 return SQLITE_SO_NUM;
877 }
878 p = p->pRight;
879 break;
880
881 case TK_AS:
882 p = p->pLeft;
883 break;
884
885 case TK_COLUMN:
886 case TK_FUNCTION:
887 case TK_AGG_FUNCTION:
888 return p->dataType;
889
890 case TK_SELECT:
891 assert( p->pSelect );
892 assert( p->pSelect->pEList );
893 assert( p->pSelect->pEList->nExpr>0 );
894 p = p->pSelect->pEList->a[0].pExpr;
895 break;
896
drhb1363202002-06-26 02:45:03 +0000897 case TK_CASE: {
898 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
899 return SQLITE_SO_NUM;
900 }
901 if( p->pList ){
902 int i;
903 ExprList *pList = p->pList;
904 for(i=1; i<pList->nExpr; i+=2){
905 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
906 return SQLITE_SO_NUM;
907 }
908 }
909 }
910 return SQLITE_SO_TEXT;
911 }
912
drhc9b84a12002-06-20 11:36:48 +0000913 default:
914 assert( p->op==TK_ABORT ); /* Can't Happen */
915 break;
916 }
917 return SQLITE_SO_NUM;
918}
919
920/*
drhcce7d172000-05-31 15:34:51 +0000921** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000922** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000923*/
924void sqliteExprCode(Parse *pParse, Expr *pExpr){
925 Vdbe *v = pParse->pVdbe;
926 int op;
drhdaffd0e2001-04-11 14:28:42 +0000927 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000928 switch( pExpr->op ){
929 case TK_PLUS: op = OP_Add; break;
930 case TK_MINUS: op = OP_Subtract; break;
931 case TK_STAR: op = OP_Multiply; break;
932 case TK_SLASH: op = OP_Divide; break;
933 case TK_AND: op = OP_And; break;
934 case TK_OR: op = OP_Or; break;
935 case TK_LT: op = OP_Lt; break;
936 case TK_LE: op = OP_Le; break;
937 case TK_GT: op = OP_Gt; break;
938 case TK_GE: op = OP_Ge; break;
939 case TK_NE: op = OP_Ne; break;
940 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000941 case TK_ISNULL: op = OP_IsNull; break;
942 case TK_NOTNULL: op = OP_NotNull; break;
943 case TK_NOT: op = OP_Not; break;
944 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000945 case TK_BITAND: op = OP_BitAnd; break;
946 case TK_BITOR: op = OP_BitOr; break;
947 case TK_BITNOT: op = OP_BitNot; break;
948 case TK_LSHIFT: op = OP_ShiftLeft; break;
949 case TK_RSHIFT: op = OP_ShiftRight; break;
950 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000951 default: break;
952 }
953 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000954 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000955 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000956 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000957 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000958 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000959 }else{
drh99fcd712001-10-13 01:06:47 +0000960 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000961 }
drhcce7d172000-05-31 15:34:51 +0000962 break;
963 }
964 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000965 int iVal = atoi(pExpr->token.z);
966 char zBuf[30];
967 sprintf(zBuf,"%d",iVal);
968 if( strlen(zBuf)!=pExpr->token.n
969 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
970 /* If the integer value cannot be represented exactly in 32 bits,
971 ** then code it as a string instead. */
972 sqliteVdbeAddOp(v, OP_String, 0, 0);
973 }else{
974 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
975 }
drhe6840902002-03-06 03:08:25 +0000976 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
977 break;
978 }
979 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000980 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000981 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000982 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000983 break;
984 }
drhcce7d172000-05-31 15:34:51 +0000985 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000986 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000987 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000988 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
989 sqliteVdbeDequoteP3(v, addr);
990 break;
991 }
992 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000993 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000994 break;
995 }
drhc9b84a12002-06-20 11:36:48 +0000996 case TK_LT:
997 case TK_LE:
998 case TK_GT:
999 case TK_GE:
1000 case TK_NE:
1001 case TK_EQ: {
1002 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1003 op += 6; /* Convert numeric opcodes to text opcodes */
1004 }
1005 /* Fall through into the next case */
1006 }
drhcce7d172000-05-31 15:34:51 +00001007 case TK_AND:
1008 case TK_OR:
1009 case TK_PLUS:
1010 case TK_STAR:
1011 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001012 case TK_REM:
1013 case TK_BITAND:
1014 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001015 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001016 sqliteExprCode(pParse, pExpr->pLeft);
1017 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001018 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001019 break;
1020 }
drhbf4133c2001-10-13 02:59:08 +00001021 case TK_LSHIFT:
1022 case TK_RSHIFT: {
1023 sqliteExprCode(pParse, pExpr->pRight);
1024 sqliteExprCode(pParse, pExpr->pLeft);
1025 sqliteVdbeAddOp(v, op, 0, 0);
1026 break;
1027 }
drh00400772000-06-16 20:51:26 +00001028 case TK_CONCAT: {
1029 sqliteExprCode(pParse, pExpr->pLeft);
1030 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001031 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001032 break;
1033 }
drhcce7d172000-05-31 15:34:51 +00001034 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001035 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001036 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001037 Token *p = &pExpr->pLeft->token;
1038 char *z = sqliteMalloc( p->n + 2 );
1039 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001040 if( pExpr->pLeft->op==TK_INTEGER ){
1041 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1042 }else{
1043 sqliteVdbeAddOp(v, OP_String, 0, 0);
1044 }
drh99fcd712001-10-13 01:06:47 +00001045 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001046 sqliteFree(z);
1047 break;
1048 }
drh1ccde152000-06-17 13:12:39 +00001049 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001050 }
drhbf4133c2001-10-13 02:59:08 +00001051 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001052 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001053 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001054 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001055 break;
1056 }
1057 case TK_ISNULL:
1058 case TK_NOTNULL: {
1059 int dest;
drh99fcd712001-10-13 01:06:47 +00001060 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001061 sqliteExprCode(pParse, pExpr->pLeft);
1062 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001063 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001064 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001065 break;
1066 }
drh22827922000-06-06 17:27:05 +00001067 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001068 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001069 break;
1070 }
drhcce7d172000-05-31 15:34:51 +00001071 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001072 int i;
1073 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001074 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001075 FuncDef *pDef;
1076 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +00001077 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001078 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001079 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001080 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001081 }
drh89425d52002-02-28 03:04:48 +00001082 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001083 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001084 break;
1085 }
drh19a775c2000-06-05 18:54:46 +00001086 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001087 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001088 break;
1089 }
drhfef52082000-06-06 01:50:43 +00001090 case TK_IN: {
1091 int addr;
drh99fcd712001-10-13 01:06:47 +00001092 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001093 sqliteExprCode(pParse, pExpr->pLeft);
1094 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001095 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1096 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1097 sqliteVdbeAddOp(v, OP_String, 0, 0);
1098 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001099 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001100 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001101 }else{
drhf5905aa2002-05-26 20:54:33 +00001102 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001103 }
drh99fcd712001-10-13 01:06:47 +00001104 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001105 break;
1106 }
1107 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001108 sqliteExprCode(pParse, pExpr->pLeft);
1109 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1110 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1111 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1112 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1113 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1114 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1115 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001116 break;
1117 }
drha2e00042002-01-22 03:13:42 +00001118 case TK_AS: {
1119 sqliteExprCode(pParse, pExpr->pLeft);
1120 break;
1121 }
drh17a7f8d2002-03-24 13:13:27 +00001122 case TK_CASE: {
1123 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001124 int jumpInst;
1125 int addr;
1126 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001127 int i;
1128
1129 assert(pExpr->pList);
1130 assert((pExpr->pList->nExpr % 2) == 0);
1131 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001132 nExpr = pExpr->pList->nExpr;
1133 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001134 if( pExpr->pLeft ){
1135 sqliteExprCode(pParse, pExpr->pLeft);
1136 }
drhf5905aa2002-05-26 20:54:33 +00001137 for(i=0; i<nExpr; i=i+2){
1138 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001139 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001140 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001141 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1142 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001143 }else{
drhf570f012002-05-31 15:51:25 +00001144 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001145 }
1146 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001147 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001148 addr = sqliteVdbeCurrentAddr(v);
1149 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001150 }
drhf570f012002-05-31 15:51:25 +00001151 if( pExpr->pLeft ){
1152 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1153 }
drh17a7f8d2002-03-24 13:13:27 +00001154 if( pExpr->pRight ){
1155 sqliteExprCode(pParse, pExpr->pRight);
1156 }else{
drhf5905aa2002-05-26 20:54:33 +00001157 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001158 }
drhf5905aa2002-05-26 20:54:33 +00001159 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001160 break;
1161 }
1162 case TK_RAISE: {
1163 if( !pParse->trigStack ){
1164 sqliteSetNString(&pParse->zErrMsg,
1165 "RAISE() may only be used within a trigger-program", -1, 0);
1166 pParse->nErr++;
1167 return;
1168 }
1169 if( pExpr->iColumn == OE_Rollback ||
1170 pExpr->iColumn == OE_Abort ||
1171 pExpr->iColumn == OE_Fail ){
1172 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1173 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1174 sqliteDequote(msg);
1175 sqliteVdbeChangeP3(v, -1, msg, 0);
1176 sqliteFree(msg);
1177 } else {
1178 assert( pExpr->iColumn == OE_Ignore );
1179 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1180 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
1181 }
drh17a7f8d2002-03-24 13:13:27 +00001182 }
1183 break;
drhcce7d172000-05-31 15:34:51 +00001184 }
drhcce7d172000-05-31 15:34:51 +00001185}
1186
1187/*
1188** Generate code for a boolean expression such that a jump is made
1189** to the label "dest" if the expression is true but execution
1190** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001191**
1192** If the expression evaluates to NULL (neither true nor false), then
1193** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001194*/
drhf5905aa2002-05-26 20:54:33 +00001195void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001196 Vdbe *v = pParse->pVdbe;
1197 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001198 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001199 switch( pExpr->op ){
1200 case TK_LT: op = OP_Lt; break;
1201 case TK_LE: op = OP_Le; break;
1202 case TK_GT: op = OP_Gt; break;
1203 case TK_GE: op = OP_Ge; break;
1204 case TK_NE: op = OP_Ne; break;
1205 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001206 case TK_ISNULL: op = OP_IsNull; break;
1207 case TK_NOTNULL: op = OP_NotNull; break;
1208 default: break;
1209 }
1210 switch( pExpr->op ){
1211 case TK_AND: {
1212 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001213 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1214 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001215 sqliteVdbeResolveLabel(v, d2);
1216 break;
1217 }
1218 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001219 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1220 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001221 break;
1222 }
1223 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001224 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001225 break;
1226 }
1227 case TK_LT:
1228 case TK_LE:
1229 case TK_GT:
1230 case TK_GE:
1231 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001232 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001233 sqliteExprCode(pParse, pExpr->pLeft);
1234 sqliteExprCode(pParse, pExpr->pRight);
drhc9b84a12002-06-20 11:36:48 +00001235 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1236 op += 6; /* Convert numeric opcodes to text opcodes */
1237 }
drhf5905aa2002-05-26 20:54:33 +00001238 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001239 break;
1240 }
1241 case TK_ISNULL:
1242 case TK_NOTNULL: {
1243 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001244 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001245 break;
1246 }
drhfef52082000-06-06 01:50:43 +00001247 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001248 int addr;
drhcfab11b2000-06-06 03:31:22 +00001249 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001250 addr = sqliteVdbeCurrentAddr(v);
1251 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1252 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1253 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001254 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001255 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001256 }else{
drh99fcd712001-10-13 01:06:47 +00001257 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001258 }
1259 break;
1260 }
1261 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001262 int addr;
drhfef52082000-06-06 01:50:43 +00001263 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001264 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001265 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001266 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001267 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001268 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001269 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001270 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001271 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001272 break;
1273 }
drhcce7d172000-05-31 15:34:51 +00001274 default: {
1275 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001276 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001277 break;
1278 }
1279 }
1280}
1281
1282/*
drh66b89c82000-11-28 20:47:17 +00001283** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001284** to the label "dest" if the expression is false but execution
1285** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001286**
1287** If the expression evaluates to NULL (neither true nor false) then
1288** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001289*/
drhf5905aa2002-05-26 20:54:33 +00001290void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001291 Vdbe *v = pParse->pVdbe;
1292 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001293 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001294 switch( pExpr->op ){
1295 case TK_LT: op = OP_Ge; break;
1296 case TK_LE: op = OP_Gt; break;
1297 case TK_GT: op = OP_Le; break;
1298 case TK_GE: op = OP_Lt; break;
1299 case TK_NE: op = OP_Eq; break;
1300 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001301 case TK_ISNULL: op = OP_NotNull; break;
1302 case TK_NOTNULL: op = OP_IsNull; break;
1303 default: break;
1304 }
1305 switch( pExpr->op ){
1306 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001307 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1308 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001309 break;
1310 }
1311 case TK_OR: {
1312 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001313 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1314 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001315 sqliteVdbeResolveLabel(v, d2);
1316 break;
1317 }
1318 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001319 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001320 break;
1321 }
1322 case TK_LT:
1323 case TK_LE:
1324 case TK_GT:
1325 case TK_GE:
1326 case TK_NE:
1327 case TK_EQ: {
drhc9b84a12002-06-20 11:36:48 +00001328 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1329 op += 6; /* Convert numeric opcodes to text opcodes */
1330 }
drhcce7d172000-05-31 15:34:51 +00001331 sqliteExprCode(pParse, pExpr->pLeft);
1332 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001333 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001334 break;
1335 }
drhcce7d172000-05-31 15:34:51 +00001336 case TK_ISNULL:
1337 case TK_NOTNULL: {
1338 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001339 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001340 break;
1341 }
drhfef52082000-06-06 01:50:43 +00001342 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001343 int addr;
drhcfab11b2000-06-06 03:31:22 +00001344 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001345 addr = sqliteVdbeCurrentAddr(v);
1346 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1347 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1348 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001349 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001350 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001351 }else{
drh99fcd712001-10-13 01:06:47 +00001352 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001353 }
1354 break;
1355 }
1356 case TK_BETWEEN: {
1357 int addr;
1358 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001359 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001360 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1361 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001362 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001363 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1364 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001365 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001366 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001367 break;
1368 }
drhcce7d172000-05-31 15:34:51 +00001369 default: {
1370 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001371 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001372 break;
1373 }
1374 }
1375}
drh22827922000-06-06 17:27:05 +00001376
1377/*
1378** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1379** if they are identical and return FALSE if they differ in any way.
1380*/
drhd8bc7082000-06-07 23:51:50 +00001381int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001382 int i;
1383 if( pA==0 ){
1384 return pB==0;
1385 }else if( pB==0 ){
1386 return 0;
1387 }
1388 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001389 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1390 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001391 if( pA->pList ){
1392 if( pB->pList==0 ) return 0;
1393 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1394 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001395 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001396 return 0;
1397 }
1398 }
1399 }else if( pB->pList ){
1400 return 0;
1401 }
1402 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001403 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001404 if( pA->token.z ){
1405 if( pB->token.z==0 ) return 0;
1406 if( pB->token.n!=pA->token.n ) return 0;
1407 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1408 }
1409 return 1;
1410}
1411
1412/*
1413** Add a new element to the pParse->aAgg[] array and return its index.
1414*/
1415static int appendAggInfo(Parse *pParse){
1416 if( (pParse->nAgg & 0x7)==0 ){
1417 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001418 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1419 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001420 return -1;
1421 }
drh6d4abfb2001-10-22 02:58:08 +00001422 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001423 }
1424 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1425 return pParse->nAgg++;
1426}
1427
1428/*
1429** Analyze the given expression looking for aggregate functions and
1430** for variables that need to be added to the pParse->aAgg[] array.
1431** Make additional entries to the pParse->aAgg[] array as necessary.
1432**
1433** This routine should only be called after the expression has been
1434** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1435**
1436** If errors are seen, leave an error message in zErrMsg and return
1437** the number of errors.
1438*/
1439int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1440 int i;
1441 AggExpr *aAgg;
1442 int nErr = 0;
1443
1444 if( pExpr==0 ) return 0;
1445 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001446 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001447 aAgg = pParse->aAgg;
1448 for(i=0; i<pParse->nAgg; i++){
1449 if( aAgg[i].isAgg ) continue;
1450 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001451 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001452 break;
1453 }
1454 }
1455 if( i>=pParse->nAgg ){
1456 i = appendAggInfo(pParse);
1457 if( i<0 ) return 1;
1458 pParse->aAgg[i].isAgg = 0;
1459 pParse->aAgg[i].pExpr = pExpr;
1460 }
drhaaf88722000-06-08 11:25:00 +00001461 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001462 break;
1463 }
1464 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001465 aAgg = pParse->aAgg;
1466 for(i=0; i<pParse->nAgg; i++){
1467 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001468 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001469 break;
1470 }
1471 }
1472 if( i>=pParse->nAgg ){
1473 i = appendAggInfo(pParse);
1474 if( i<0 ) return 1;
1475 pParse->aAgg[i].isAgg = 1;
1476 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001477 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001478 pExpr->token.z, pExpr->token.n,
1479 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001480 }
1481 pExpr->iAgg = i;
1482 break;
1483 }
1484 default: {
1485 if( pExpr->pLeft ){
1486 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1487 }
1488 if( nErr==0 && pExpr->pRight ){
1489 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1490 }
1491 if( nErr==0 && pExpr->pList ){
1492 int n = pExpr->pList->nExpr;
1493 int i;
1494 for(i=0; nErr==0 && i<n; i++){
1495 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1496 }
1497 }
1498 break;
1499 }
1500 }
1501 return nErr;
1502}
drh8e0a2f92002-02-23 23:45:45 +00001503
1504/*
1505** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001506** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001507** function, or return NULL if the function does not exist.
1508**
drh0bce8352002-02-28 00:41:10 +00001509** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001510** structure is created and liked into the "db" structure if a
1511** no matching function previously existed. When createFlag is true
1512** and the nArg parameter is -1, then only a function that accepts
1513** any number of arguments will be returned.
1514**
1515** If createFlag is false and nArg is -1, then the first valid
1516** function found is returned. A function is valid if either xFunc
1517** or xStep is non-zero.
1518*/
drh0bce8352002-02-28 00:41:10 +00001519FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001520 sqlite *db, /* An open database */
1521 const char *zName, /* Name of the function. Not null-terminated */
1522 int nName, /* Number of characters in the name */
1523 int nArg, /* Number of arguments. -1 means any number */
1524 int createFlag /* Create new entry if true and does not otherwise exist */
1525){
drh0bce8352002-02-28 00:41:10 +00001526 FuncDef *pFirst, *p, *pMaybe;
1527 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001528 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001529 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1530 return p;
1531 }
1532 pMaybe = 0;
1533 while( p && p->nArg!=nArg ){
1534 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1535 p = p->pNext;
1536 }
1537 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1538 return 0;
1539 }
1540 if( p==0 && pMaybe ){
1541 assert( createFlag==0 );
1542 return pMaybe;
1543 }
drh89425d52002-02-28 03:04:48 +00001544 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001545 p->nArg = nArg;
1546 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001547 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001548 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001549 }
1550 return p;
1551}