blob: 5b2d762e6d7fca5216facd89c2742e99ae45ff1e [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**
drha9f9d1c2002-06-29 02:20:08 +000015** $Id: expr.c,v 1.76 2002/06/29 02:20:08 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}
140void sqliteSelectMoveStrings(Select *pSelect, int offset){
141 if( pSelect==0 ) return;
142 sqliteExprListMoveStrings(pSelect->pEList, offset);
143 sqliteExprMoveStrings(pSelect->pWhere, offset);
144 sqliteExprListMoveStrings(pSelect->pGroupBy, offset);
145 sqliteExprMoveStrings(pSelect->pHaving, offset);
146 sqliteExprListMoveStrings(pSelect->pOrderBy, offset);
147 sqliteSelectMoveStrings(pSelect->pPrior, offset);
148}
149
150/*
drhff78bd22002-02-27 01:47:11 +0000151** The following group of routines make deep copies of expressions,
152** expression lists, ID lists, and select statements. The copies can
153** be deleted (by being passed to their respective ...Delete() routines)
154** without effecting the originals.
155**
156** Note, however, that the Expr.token.z and Expr.span.z fields point to
157** string space that is allocated separately from the expression tree
158** itself. These routines do NOT duplicate that string space.
159**
drhad3cab52002-05-24 02:04:32 +0000160** The expression list, ID, and source lists return by sqliteExprListDup(),
161** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
162** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000163**
drhad3cab52002-05-24 02:04:32 +0000164** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000165*/
166Expr *sqliteExprDup(Expr *p){
167 Expr *pNew;
168 if( p==0 ) return 0;
169 pNew = sqliteMalloc( sizeof(*p) );
170 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000171 memcpy(pNew, p, sizeof(*pNew));
drhff78bd22002-02-27 01:47:11 +0000172 pNew->pLeft = sqliteExprDup(p->pLeft);
173 pNew->pRight = sqliteExprDup(p->pRight);
174 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000175 pNew->pSelect = sqliteSelectDup(p->pSelect);
176 return pNew;
177}
178ExprList *sqliteExprListDup(ExprList *p){
179 ExprList *pNew;
180 int i;
181 if( p==0 ) return 0;
182 pNew = sqliteMalloc( sizeof(*pNew) );
183 if( pNew==0 ) return 0;
184 pNew->nExpr = p->nExpr;
185 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000186 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000187 for(i=0; i<p->nExpr; i++){
188 pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
189 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
190 pNew->a[i].sortOrder = p->a[i].sortOrder;
191 pNew->a[i].isAgg = p->a[i].isAgg;
192 pNew->a[i].done = 0;
193 }
194 return pNew;
195}
drhad3cab52002-05-24 02:04:32 +0000196SrcList *sqliteSrcListDup(SrcList *p){
197 SrcList *pNew;
198 int i;
199 if( p==0 ) return 0;
200 pNew = sqliteMalloc( sizeof(*pNew) );
201 if( pNew==0 ) return 0;
202 pNew->nSrc = p->nSrc;
203 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
danielk19776f349032002-06-11 02:25:40 +0000204 if( pNew->a==0 && p->nSrc != 0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000205 for(i=0; i<p->nSrc; i++){
206 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
207 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
208 pNew->a[i].jointype = p->a[i].jointype;
209 pNew->a[i].pTab = 0;
210 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
211 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
212 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
213 }
214 return pNew;
215}
drhff78bd22002-02-27 01:47:11 +0000216IdList *sqliteIdListDup(IdList *p){
217 IdList *pNew;
218 int i;
219 if( p==0 ) return 0;
220 pNew = sqliteMalloc( sizeof(*pNew) );
221 if( pNew==0 ) return 0;
222 pNew->nId = p->nId;
223 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000224 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000225 for(i=0; i<p->nId; i++){
226 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000227 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000228 }
229 return pNew;
230}
231Select *sqliteSelectDup(Select *p){
232 Select *pNew;
233 if( p==0 ) return 0;
234 pNew = sqliteMalloc( sizeof(*p) );
235 if( pNew==0 ) return 0;
236 pNew->isDistinct = p->isDistinct;
237 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000238 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000239 pNew->pWhere = sqliteExprDup(p->pWhere);
240 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
241 pNew->pHaving = sqliteExprDup(p->pHaving);
242 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
243 pNew->op = p->op;
244 pNew->pPrior = sqliteSelectDup(p->pPrior);
245 pNew->nLimit = p->nLimit;
246 pNew->nOffset = p->nOffset;
247 pNew->zSelect = 0;
248 return pNew;
249}
250
251
252/*
drha76b5df2002-02-23 02:32:10 +0000253** Add a new element to the end of an expression list. If pList is
254** initially NULL, then create a new expression list.
255*/
256ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
257 int i;
258 if( pList==0 ){
259 pList = sqliteMalloc( sizeof(ExprList) );
260 if( pList==0 ){
261 sqliteExprDelete(pExpr);
262 return 0;
263 }
264 }
265 if( (pList->nExpr & 7)==0 ){
266 int n = pList->nExpr + 8;
267 struct ExprList_item *a;
268 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
269 if( a==0 ){
270 sqliteExprDelete(pExpr);
271 return pList;
272 }
273 pList->a = a;
274 }
275 if( pExpr || pName ){
276 i = pList->nExpr++;
277 pList->a[i].pExpr = pExpr;
278 pList->a[i].zName = 0;
279 if( pName ){
280 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
281 sqliteDequote(pList->a[i].zName);
282 }
283 }
284 return pList;
285}
286
287/*
288** Delete an entire expression list.
289*/
290void sqliteExprListDelete(ExprList *pList){
291 int i;
292 if( pList==0 ) return;
293 for(i=0; i<pList->nExpr; i++){
294 sqliteExprDelete(pList->a[i].pExpr);
295 sqliteFree(pList->a[i].zName);
296 }
297 sqliteFree(pList->a);
298 sqliteFree(pList);
299}
300
301/*
drhfef52082000-06-06 01:50:43 +0000302** Walk an expression tree. Return 1 if the expression is constant
303** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000304**
305** For the purposes of this function, a double-quoted string (ex: "abc")
306** is considered a variable but a single-quoted string (ex: 'abc') is
307** a constant.
drhfef52082000-06-06 01:50:43 +0000308*/
drh92086432002-01-22 14:11:29 +0000309int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000310 switch( p->op ){
311 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000312 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000313 case TK_DOT:
314 return 0;
drh23989372002-05-21 13:43:04 +0000315 case TK_STRING:
316 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000317 case TK_INTEGER:
318 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000319 return 1;
drhfef52082000-06-06 01:50:43 +0000320 default: {
drh92086432002-01-22 14:11:29 +0000321 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
322 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000323 if( p->pList ){
324 int i;
325 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000326 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000327 }
328 }
drh92086432002-01-22 14:11:29 +0000329 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000330 }
331 }
drh92086432002-01-22 14:11:29 +0000332 return 0;
drhfef52082000-06-06 01:50:43 +0000333}
334
335/*
drhe4de1fe2002-06-02 16:09:01 +0000336** If the given expression codes a constant integer, return 1 and put
337** the value of the integer in *pValue. If the expression is not an
338** integer, return 0 and leave *pValue unchanged.
339*/
340int sqliteExprIsInteger(Expr *p, int *pValue){
341 switch( p->op ){
342 case TK_INTEGER: {
343 *pValue = atoi(p->token.z);
344 return 1;
345 }
346 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000347 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000348 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000349 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000350 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
351 if( n==0 ){
352 *pValue = atoi(p->token.z);
353 return 1;
354 }
355 break;
356 }
357 case TK_UMINUS: {
358 int v;
359 if( sqliteExprIsInteger(p->pLeft, &v) ){
360 *pValue = -v;
361 return 1;
362 }
363 break;
364 }
365 default: break;
366 }
367 return 0;
368}
369
370/*
drhc4a3c772001-04-04 11:48:57 +0000371** Return TRUE if the given string is a row-id column name.
372*/
drha9f9d1c2002-06-29 02:20:08 +0000373int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000374 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
375 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
376 if( sqliteStrICmp(z, "OID")==0 ) return 1;
377 return 0;
378}
379
380/*
drhcce7d172000-05-31 15:34:51 +0000381** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000382** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000383** index to the table in the table list and a column offset. The
384** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
385** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000386** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000387** VDBE cursor number for a cursor that is pointing into the referenced
388** table. The Expr.iColumn value is changed to the index of the column
389** of the referenced table. The Expr.iColumn value for the special
390** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
391** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000392**
drhfef52082000-06-06 01:50:43 +0000393** We also check for instances of the IN operator. IN comes in two
394** forms:
395**
396** expr IN (exprlist)
397** and
398** expr IN (SELECT ...)
399**
400** The first form is handled by creating a set holding the list
401** of allowed values. The second form causes the SELECT to generate
402** a temporary table.
403**
404** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000405** If it finds any, it generates code to write the value of that select
406** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000407**
drh967e8b72000-06-21 13:59:10 +0000408** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000409** the number of errors seen and leaves an error message on pParse->zErrMsg.
410*/
drha2e00042002-01-22 03:13:42 +0000411int sqliteExprResolveIds(
412 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000413 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000414 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000415 ExprList *pEList, /* List of expressions used to resolve "AS" */
416 Expr *pExpr /* The expression to be analyzed. */
417){
drhdaffd0e2001-04-11 14:28:42 +0000418 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000419 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000420 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000421 /* Double-quoted strings (ex: "abc") are used as identifiers if
422 ** possible. Otherwise they remain as strings. Single-quoted
423 ** strings (ex: 'abc') are always string literals.
424 */
425 case TK_STRING: {
426 if( pExpr->token.z[0]=='\'' ) break;
427 /* Fall thru into the TK_ID case if this is a double-quoted string */
428 }
drha2e00042002-01-22 03:13:42 +0000429 /* A lone identifier. Try and match it as follows:
430 **
431 ** 1. To the name of a column of one of the tables in pTabList
432 **
433 ** 2. To the right side of an AS keyword in the column list of
434 ** a SELECT statement. (For example, match against 'x' in
435 ** "SELECT a+b AS 'x' FROM t1".)
436 **
437 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
438 */
drhcce7d172000-05-31 15:34:51 +0000439 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000440 int cnt = 0; /* Number of matches */
441 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000442 char *z;
443 assert( pExpr->token.z );
444 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000445 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000446 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000447 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000448 int j;
449 Table *pTab = pTabList->a[i].pTab;
450 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000451 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000452 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000453 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000454 cnt++;
drh832508b2002-03-02 17:04:07 +0000455 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000456 if( j==pTab->iPKey ){
457 /* Substitute the record number for the INTEGER PRIMARY KEY */
458 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000459 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000460 }else{
461 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000462 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000463 }
drha2e00042002-01-22 03:13:42 +0000464 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000465 }
466 }
467 }
drha2e00042002-01-22 03:13:42 +0000468 if( cnt==0 && pEList!=0 ){
469 int j;
470 for(j=0; j<pEList->nExpr; j++){
471 char *zAs = pEList->a[j].zName;
472 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
473 cnt++;
474 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
475 pExpr->op = TK_AS;
476 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000477 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000478 }
479 }
480 }
drhc4a3c772001-04-04 11:48:57 +0000481 if( cnt==0 && sqliteIsRowid(z) ){
482 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000483 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000484 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000485 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000486 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000487 }
drhcce7d172000-05-31 15:34:51 +0000488 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000489 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000490 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000491 pExpr->token.z, pExpr->token.n, 0);
492 pParse->nErr++;
493 return 1;
494 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000495 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000496 pExpr->token.z, pExpr->token.n, 0);
497 pParse->nErr++;
498 return 1;
499 }
drhcce7d172000-05-31 15:34:51 +0000500 break;
501 }
502
drh967e8b72000-06-21 13:59:10 +0000503 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000504 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000505 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000506 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000507 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000508 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000509 char *zLeft, *zRight; /* Text of an identifier */
510
511 pLeft = pExpr->pLeft;
512 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000513 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
514 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000515 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
516 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000517 if( zLeft==0 || zRight==0 ){
518 sqliteFree(zLeft);
519 sqliteFree(zRight);
520 return 1;
521 }
drh87c40e82001-07-23 14:33:02 +0000522 sqliteDequote(zLeft);
523 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000524 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000525 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000526 int j;
527 char *zTab;
528 Table *pTab = pTabList->a[i].pTab;
529 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000530 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000531 if( pTabList->a[i].zAlias ){
532 zTab = pTabList->a[i].zAlias;
533 }else{
534 zTab = pTab->zName;
535 }
drh094b2bb2002-03-13 18:54:07 +0000536 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000537 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000538 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000539 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000540 cnt++;
drh832508b2002-03-02 17:04:07 +0000541 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000542 if( j==pTab->iPKey ){
543 /* Substitute the record number for the INTEGER PRIMARY KEY */
544 pExpr->iColumn = -1;
545 }else{
546 pExpr->iColumn = j;
547 }
drhc9b84a12002-06-20 11:36:48 +0000548 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000549 }
550 }
551 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000552
553 /* If we have not already resolved this *.* expression, then maybe
554 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000555 if( cnt == 0 && pParse->trigStack != 0 ){
556 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000557 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000558 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
559 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000560 cntTab++;
561 t = 1;
562 }
danielk1977f29ce552002-05-19 23:43:12 +0000563 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
564 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000565 cntTab++;
566 t = 1;
567 }
568
danielk1977f29ce552002-05-19 23:43:12 +0000569 if( t ){
570 int j;
drhc9b84a12002-06-20 11:36:48 +0000571 Table *pTab = pTriggerStack->pTab;
572 for(j=0; j < pTab->nCol; j++) {
573 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000574 cnt++;
575 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000576 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }
578 }
danielk1977f29ce552002-05-19 23:43:12 +0000579 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000580 }
581
drhc4a3c772001-04-04 11:48:57 +0000582 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
583 cnt = 1;
584 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000585 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000586 }
drhcce7d172000-05-31 15:34:51 +0000587 sqliteFree(zLeft);
588 sqliteFree(zRight);
589 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000590 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000591 pLeft->token.z, pLeft->token.n, ".", 1,
592 pRight->token.z, pRight->token.n, 0);
593 pParse->nErr++;
594 return 1;
595 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000596 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000597 pLeft->token.z, pLeft->token.n, ".", 1,
598 pRight->token.z, pRight->token.n, 0);
599 pParse->nErr++;
600 return 1;
601 }
602 sqliteExprDelete(pLeft);
603 pExpr->pLeft = 0;
604 sqliteExprDelete(pRight);
605 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000606 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000607 break;
608 }
609
drhfef52082000-06-06 01:50:43 +0000610 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000611 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000612 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000613 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000614 return 1;
615 }
drhfef52082000-06-06 01:50:43 +0000616 if( pExpr->pSelect ){
617 /* Case 1: expr IN (SELECT ...)
618 **
619 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000620 ** table. The cursor number of the temporary table has already
621 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000622 */
drh832508b2002-03-02 17:04:07 +0000623 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000624 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000625 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000626 }else if( pExpr->pList ){
627 /* Case 2: expr IN (exprlist)
628 **
629 ** Create a set to put the exprlist values in. The Set id is stored
630 ** in iTable.
631 */
632 int i, iSet;
633 for(i=0; i<pExpr->pList->nExpr; i++){
634 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000635 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000636 sqliteSetString(&pParse->zErrMsg,
637 "right-hand side of IN operator must be constant", 0);
638 pParse->nErr++;
639 return 1;
640 }
drh4794b982000-06-06 13:54:14 +0000641 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
642 return 1;
643 }
drhfef52082000-06-06 01:50:43 +0000644 }
645 iSet = pExpr->iTable = pParse->nSet++;
646 for(i=0; i<pExpr->pList->nExpr; i++){
647 Expr *pE2 = pExpr->pList->a[i].pExpr;
648 switch( pE2->op ){
649 case TK_FLOAT:
650 case TK_INTEGER:
651 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000652 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000653 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000654 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
655 sqliteVdbeDequoteP3(v, addr);
656 break;
657 }
658 default: {
659 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000660 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000661 break;
662 }
663 }
664 }
665 }
drhcfab11b2000-06-06 03:31:22 +0000666 break;
drhfef52082000-06-06 01:50:43 +0000667 }
668
drh19a775c2000-06-05 18:54:46 +0000669 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000670 /* This has to be a scalar SELECT. Generate code to put the
671 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000672 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000673 */
drh967e8b72000-06-21 13:59:10 +0000674 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000675 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000676 return 1;
677 }
678 break;
679 }
680
drhcce7d172000-05-31 15:34:51 +0000681 /* For all else, just recursively walk the tree */
682 default: {
drh4794b982000-06-06 13:54:14 +0000683 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000684 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000685 return 1;
686 }
687 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000688 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000689 return 1;
690 }
691 if( pExpr->pList ){
692 int i;
693 ExprList *pList = pExpr->pList;
694 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000695 Expr *pArg = pList->a[i].pExpr;
696 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000697 return 1;
698 }
699 }
700 }
701 }
702 }
703 return 0;
704}
705
drhcce7d172000-05-31 15:34:51 +0000706/*
707** Error check the functions in an expression. Make sure all
708** function names are recognized and all functions have the correct
709** number of arguments. Leave an error message in pParse->zErrMsg
710** if anything is amiss. Return the number of errors.
711**
712** if pIsAgg is not null and this expression is an aggregate function
713** (like count(*) or max(value)) then write a 1 into *pIsAgg.
714*/
715int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
716 int nErr = 0;
717 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000718 switch( pExpr->op ){
719 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000720 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
721 int no_such_func = 0; /* True if no such function exists */
722 int is_type_of = 0; /* True if is the special TypeOf() function */
723 int wrong_num_args = 0; /* True if wrong number of arguments */
724 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000725 int i;
drh0bce8352002-02-28 00:41:10 +0000726 FuncDef *pDef;
727
drh89425d52002-02-28 03:04:48 +0000728 pDef = sqliteFindFunction(pParse->db,
729 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000730 if( pDef==0 ){
731 pDef = sqliteFindFunction(pParse->db,
732 pExpr->token.z, pExpr->token.n, -1, 0);
733 if( pDef==0 ){
drhc9b84a12002-06-20 11:36:48 +0000734 if( n==1 && pExpr->token.n==6
735 && sqliteStrNICmp(pExpr->token.z, "typeof", 6)==0 ){
736 is_type_of = 1;
737 }else {
738 no_such_func = 1;
739 }
drh0bce8352002-02-28 00:41:10 +0000740 }else{
741 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000742 }
drh0bce8352002-02-28 00:41:10 +0000743 }else{
744 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000745 }
drh8e0a2f92002-02-23 23:45:45 +0000746 if( is_agg && !allowAgg ){
747 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
748 pExpr->token.z, pExpr->token.n, "()", 2, 0);
749 pParse->nErr++;
750 nErr++;
751 is_agg = 0;
752 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000753 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
754 pExpr->token.z, pExpr->token.n, 0);
755 pParse->nErr++;
756 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000757 }else if( wrong_num_args ){
758 sqliteSetNString(&pParse->zErrMsg,
759 "wrong number of arguments to function ",-1,
760 pExpr->token.z, pExpr->token.n, "()", 2, 0);
761 pParse->nErr++;
762 nErr++;
drhcce7d172000-05-31 15:34:51 +0000763 }
drh22827922000-06-06 17:27:05 +0000764 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000765 if( is_agg && pIsAgg ) *pIsAgg = 1;
766 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000767 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
768 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000769 }
drhc9b84a12002-06-20 11:36:48 +0000770 if( pDef==0 ){
771 if( is_type_of ){
772 pExpr->op = TK_STRING;
773 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
774 pExpr->token.z = "numeric";
775 pExpr->token.n = 7;
776 }else{
777 pExpr->token.z = "text";
778 pExpr->token.n = 4;
779 }
780 }
781 }else if( pDef->dataType>=0 ){
782 if( pDef->dataType<n ){
783 pExpr->dataType =
784 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
785 }else{
786 pExpr->dataType = SQLITE_SO_NUM;
787 }
788 }else if( pDef->dataType==SQLITE_ARGS ){
789 pDef->dataType = SQLITE_SO_TEXT;
790 for(i=0; i<n; i++){
791 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
792 pExpr->dataType = SQLITE_SO_NUM;
793 break;
794 }
795 }
796 }else if( pDef->dataType==SQLITE_NUMERIC ){
797 pExpr->dataType = SQLITE_SO_NUM;
798 }else{
799 pExpr->dataType = SQLITE_SO_TEXT;
800 }
drhcce7d172000-05-31 15:34:51 +0000801 }
802 default: {
803 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000804 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000805 }
806 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000807 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000808 }
drhfef52082000-06-06 01:50:43 +0000809 if( nErr==0 && pExpr->pList ){
810 int n = pExpr->pList->nExpr;
811 int i;
812 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000813 Expr *pE2 = pExpr->pList->a[i].pExpr;
814 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000815 }
816 }
drhcce7d172000-05-31 15:34:51 +0000817 break;
818 }
819 }
820 return nErr;
821}
822
823/*
drhc9b84a12002-06-20 11:36:48 +0000824** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
825** given expression should sort as numeric values or as text.
826**
827** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
828** both been called on the expression before it is passed to this routine.
829*/
830int sqliteExprType(Expr *p){
831 if( p==0 ) return SQLITE_SO_NUM;
832 while( p ) switch( p->op ){
833 case TK_PLUS:
834 case TK_MINUS:
835 case TK_STAR:
836 case TK_SLASH:
837 case TK_AND:
838 case TK_OR:
839 case TK_ISNULL:
840 case TK_NOTNULL:
841 case TK_NOT:
842 case TK_UMINUS:
843 case TK_BITAND:
844 case TK_BITOR:
845 case TK_BITNOT:
846 case TK_LSHIFT:
847 case TK_RSHIFT:
848 case TK_REM:
849 case TK_INTEGER:
850 case TK_FLOAT:
851 case TK_IN:
852 case TK_BETWEEN:
853 return SQLITE_SO_NUM;
854
855 case TK_STRING:
856 case TK_NULL:
857 case TK_CONCAT:
858 return SQLITE_SO_TEXT;
859
860 case TK_LT:
861 case TK_LE:
862 case TK_GT:
863 case TK_GE:
864 case TK_NE:
865 case TK_EQ:
866 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
867 return SQLITE_SO_NUM;
868 }
869 p = p->pRight;
870 break;
871
872 case TK_AS:
873 p = p->pLeft;
874 break;
875
876 case TK_COLUMN:
877 case TK_FUNCTION:
878 case TK_AGG_FUNCTION:
879 return p->dataType;
880
881 case TK_SELECT:
882 assert( p->pSelect );
883 assert( p->pSelect->pEList );
884 assert( p->pSelect->pEList->nExpr>0 );
885 p = p->pSelect->pEList->a[0].pExpr;
886 break;
887
drhb1363202002-06-26 02:45:03 +0000888 case TK_CASE: {
889 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
890 return SQLITE_SO_NUM;
891 }
892 if( p->pList ){
893 int i;
894 ExprList *pList = p->pList;
895 for(i=1; i<pList->nExpr; i+=2){
896 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
897 return SQLITE_SO_NUM;
898 }
899 }
900 }
901 return SQLITE_SO_TEXT;
902 }
903
drhc9b84a12002-06-20 11:36:48 +0000904 default:
905 assert( p->op==TK_ABORT ); /* Can't Happen */
906 break;
907 }
908 return SQLITE_SO_NUM;
909}
910
911/*
drhcce7d172000-05-31 15:34:51 +0000912** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000913** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000914*/
915void sqliteExprCode(Parse *pParse, Expr *pExpr){
916 Vdbe *v = pParse->pVdbe;
917 int op;
drhdaffd0e2001-04-11 14:28:42 +0000918 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000919 switch( pExpr->op ){
920 case TK_PLUS: op = OP_Add; break;
921 case TK_MINUS: op = OP_Subtract; break;
922 case TK_STAR: op = OP_Multiply; break;
923 case TK_SLASH: op = OP_Divide; break;
924 case TK_AND: op = OP_And; break;
925 case TK_OR: op = OP_Or; break;
926 case TK_LT: op = OP_Lt; break;
927 case TK_LE: op = OP_Le; break;
928 case TK_GT: op = OP_Gt; break;
929 case TK_GE: op = OP_Ge; break;
930 case TK_NE: op = OP_Ne; break;
931 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000932 case TK_ISNULL: op = OP_IsNull; break;
933 case TK_NOTNULL: op = OP_NotNull; break;
934 case TK_NOT: op = OP_Not; break;
935 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000936 case TK_BITAND: op = OP_BitAnd; break;
937 case TK_BITOR: op = OP_BitOr; break;
938 case TK_BITNOT: op = OP_BitNot; break;
939 case TK_LSHIFT: op = OP_ShiftLeft; break;
940 case TK_RSHIFT: op = OP_ShiftRight; break;
941 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000942 default: break;
943 }
944 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000945 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000946 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000947 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000948 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000949 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000950 }else{
drh99fcd712001-10-13 01:06:47 +0000951 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000952 }
drhcce7d172000-05-31 15:34:51 +0000953 break;
954 }
955 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000956 int iVal = atoi(pExpr->token.z);
957 char zBuf[30];
958 sprintf(zBuf,"%d",iVal);
959 if( strlen(zBuf)!=pExpr->token.n
960 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
961 /* If the integer value cannot be represented exactly in 32 bits,
962 ** then code it as a string instead. */
963 sqliteVdbeAddOp(v, OP_String, 0, 0);
964 }else{
965 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
966 }
drhe6840902002-03-06 03:08:25 +0000967 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
968 break;
969 }
970 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000971 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000972 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000973 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000974 break;
975 }
drhcce7d172000-05-31 15:34:51 +0000976 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000977 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000978 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000979 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
980 sqliteVdbeDequoteP3(v, addr);
981 break;
982 }
983 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000984 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000985 break;
986 }
drhc9b84a12002-06-20 11:36:48 +0000987 case TK_LT:
988 case TK_LE:
989 case TK_GT:
990 case TK_GE:
991 case TK_NE:
992 case TK_EQ: {
993 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
994 op += 6; /* Convert numeric opcodes to text opcodes */
995 }
996 /* Fall through into the next case */
997 }
drhcce7d172000-05-31 15:34:51 +0000998 case TK_AND:
999 case TK_OR:
1000 case TK_PLUS:
1001 case TK_STAR:
1002 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001003 case TK_REM:
1004 case TK_BITAND:
1005 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001006 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001007 sqliteExprCode(pParse, pExpr->pLeft);
1008 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001009 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001010 break;
1011 }
drhbf4133c2001-10-13 02:59:08 +00001012 case TK_LSHIFT:
1013 case TK_RSHIFT: {
1014 sqliteExprCode(pParse, pExpr->pRight);
1015 sqliteExprCode(pParse, pExpr->pLeft);
1016 sqliteVdbeAddOp(v, op, 0, 0);
1017 break;
1018 }
drh00400772000-06-16 20:51:26 +00001019 case TK_CONCAT: {
1020 sqliteExprCode(pParse, pExpr->pLeft);
1021 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001022 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001023 break;
1024 }
drhcce7d172000-05-31 15:34:51 +00001025 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001026 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001027 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001028 Token *p = &pExpr->pLeft->token;
1029 char *z = sqliteMalloc( p->n + 2 );
1030 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001031 if( pExpr->pLeft->op==TK_INTEGER ){
1032 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1033 }else{
1034 sqliteVdbeAddOp(v, OP_String, 0, 0);
1035 }
drh99fcd712001-10-13 01:06:47 +00001036 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001037 sqliteFree(z);
1038 break;
1039 }
drh1ccde152000-06-17 13:12:39 +00001040 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001041 }
drhbf4133c2001-10-13 02:59:08 +00001042 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001043 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001044 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001045 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001046 break;
1047 }
1048 case TK_ISNULL:
1049 case TK_NOTNULL: {
1050 int dest;
drh99fcd712001-10-13 01:06:47 +00001051 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001052 sqliteExprCode(pParse, pExpr->pLeft);
1053 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001054 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001055 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001056 break;
1057 }
drh22827922000-06-06 17:27:05 +00001058 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001059 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001060 break;
1061 }
drhcce7d172000-05-31 15:34:51 +00001062 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001063 int i;
1064 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001065 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001066 FuncDef *pDef;
1067 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +00001068 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001069 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001070 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001071 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001072 }
drh89425d52002-02-28 03:04:48 +00001073 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001074 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001075 break;
1076 }
drh19a775c2000-06-05 18:54:46 +00001077 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001078 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001079 break;
1080 }
drhfef52082000-06-06 01:50:43 +00001081 case TK_IN: {
1082 int addr;
drh99fcd712001-10-13 01:06:47 +00001083 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001084 sqliteExprCode(pParse, pExpr->pLeft);
1085 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001086 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1087 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1088 sqliteVdbeAddOp(v, OP_String, 0, 0);
1089 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001090 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001091 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001092 }else{
drhf5905aa2002-05-26 20:54:33 +00001093 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001094 }
drh99fcd712001-10-13 01:06:47 +00001095 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001096 break;
1097 }
1098 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001099 sqliteExprCode(pParse, pExpr->pLeft);
1100 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1101 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1102 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1103 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1104 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1105 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1106 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001107 break;
1108 }
drha2e00042002-01-22 03:13:42 +00001109 case TK_AS: {
1110 sqliteExprCode(pParse, pExpr->pLeft);
1111 break;
1112 }
drh17a7f8d2002-03-24 13:13:27 +00001113 case TK_CASE: {
1114 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001115 int jumpInst;
1116 int addr;
1117 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001118 int i;
1119
1120 assert(pExpr->pList);
1121 assert((pExpr->pList->nExpr % 2) == 0);
1122 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001123 nExpr = pExpr->pList->nExpr;
1124 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001125 if( pExpr->pLeft ){
1126 sqliteExprCode(pParse, pExpr->pLeft);
1127 }
drhf5905aa2002-05-26 20:54:33 +00001128 for(i=0; i<nExpr; i=i+2){
1129 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001130 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001131 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001132 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1133 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001134 }else{
drhf570f012002-05-31 15:51:25 +00001135 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001136 }
1137 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001138 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001139 addr = sqliteVdbeCurrentAddr(v);
1140 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001141 }
drhf570f012002-05-31 15:51:25 +00001142 if( pExpr->pLeft ){
1143 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1144 }
drh17a7f8d2002-03-24 13:13:27 +00001145 if( pExpr->pRight ){
1146 sqliteExprCode(pParse, pExpr->pRight);
1147 }else{
drhf5905aa2002-05-26 20:54:33 +00001148 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001149 }
drhf5905aa2002-05-26 20:54:33 +00001150 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001151 break;
1152 }
1153 case TK_RAISE: {
1154 if( !pParse->trigStack ){
1155 sqliteSetNString(&pParse->zErrMsg,
1156 "RAISE() may only be used within a trigger-program", -1, 0);
1157 pParse->nErr++;
1158 return;
1159 }
1160 if( pExpr->iColumn == OE_Rollback ||
1161 pExpr->iColumn == OE_Abort ||
1162 pExpr->iColumn == OE_Fail ){
1163 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1164 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1165 sqliteDequote(msg);
1166 sqliteVdbeChangeP3(v, -1, msg, 0);
1167 sqliteFree(msg);
1168 } else {
1169 assert( pExpr->iColumn == OE_Ignore );
1170 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1171 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
1172 }
drh17a7f8d2002-03-24 13:13:27 +00001173 }
1174 break;
drhcce7d172000-05-31 15:34:51 +00001175 }
drhcce7d172000-05-31 15:34:51 +00001176}
1177
1178/*
1179** Generate code for a boolean expression such that a jump is made
1180** to the label "dest" if the expression is true but execution
1181** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001182**
1183** If the expression evaluates to NULL (neither true nor false), then
1184** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001185*/
drhf5905aa2002-05-26 20:54:33 +00001186void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001187 Vdbe *v = pParse->pVdbe;
1188 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001189 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001190 switch( pExpr->op ){
1191 case TK_LT: op = OP_Lt; break;
1192 case TK_LE: op = OP_Le; break;
1193 case TK_GT: op = OP_Gt; break;
1194 case TK_GE: op = OP_Ge; break;
1195 case TK_NE: op = OP_Ne; break;
1196 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001197 case TK_ISNULL: op = OP_IsNull; break;
1198 case TK_NOTNULL: op = OP_NotNull; break;
1199 default: break;
1200 }
1201 switch( pExpr->op ){
1202 case TK_AND: {
1203 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001204 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1205 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001206 sqliteVdbeResolveLabel(v, d2);
1207 break;
1208 }
1209 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001210 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1211 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001212 break;
1213 }
1214 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001215 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001216 break;
1217 }
1218 case TK_LT:
1219 case TK_LE:
1220 case TK_GT:
1221 case TK_GE:
1222 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001223 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001224 sqliteExprCode(pParse, pExpr->pLeft);
1225 sqliteExprCode(pParse, pExpr->pRight);
drhc9b84a12002-06-20 11:36:48 +00001226 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1227 op += 6; /* Convert numeric opcodes to text opcodes */
1228 }
drhf5905aa2002-05-26 20:54:33 +00001229 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001230 break;
1231 }
1232 case TK_ISNULL:
1233 case TK_NOTNULL: {
1234 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001235 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001236 break;
1237 }
drhfef52082000-06-06 01:50:43 +00001238 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001239 int addr;
drhcfab11b2000-06-06 03:31:22 +00001240 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001241 addr = sqliteVdbeCurrentAddr(v);
1242 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1243 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1244 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001245 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001246 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001247 }else{
drh99fcd712001-10-13 01:06:47 +00001248 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001249 }
1250 break;
1251 }
1252 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001253 int addr;
drhfef52082000-06-06 01:50:43 +00001254 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001255 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001256 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001257 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001258 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001259 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001260 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001261 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001262 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001263 break;
1264 }
drhcce7d172000-05-31 15:34:51 +00001265 default: {
1266 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001267 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001268 break;
1269 }
1270 }
1271}
1272
1273/*
drh66b89c82000-11-28 20:47:17 +00001274** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001275** to the label "dest" if the expression is false but execution
1276** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001277**
1278** If the expression evaluates to NULL (neither true nor false) then
1279** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001280*/
drhf5905aa2002-05-26 20:54:33 +00001281void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001282 Vdbe *v = pParse->pVdbe;
1283 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001284 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001285 switch( pExpr->op ){
1286 case TK_LT: op = OP_Ge; break;
1287 case TK_LE: op = OP_Gt; break;
1288 case TK_GT: op = OP_Le; break;
1289 case TK_GE: op = OP_Lt; break;
1290 case TK_NE: op = OP_Eq; break;
1291 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001292 case TK_ISNULL: op = OP_NotNull; break;
1293 case TK_NOTNULL: op = OP_IsNull; break;
1294 default: break;
1295 }
1296 switch( pExpr->op ){
1297 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001298 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1299 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001300 break;
1301 }
1302 case TK_OR: {
1303 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001304 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1305 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001306 sqliteVdbeResolveLabel(v, d2);
1307 break;
1308 }
1309 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001310 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001311 break;
1312 }
1313 case TK_LT:
1314 case TK_LE:
1315 case TK_GT:
1316 case TK_GE:
1317 case TK_NE:
1318 case TK_EQ: {
drhc9b84a12002-06-20 11:36:48 +00001319 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1320 op += 6; /* Convert numeric opcodes to text opcodes */
1321 }
drhcce7d172000-05-31 15:34:51 +00001322 sqliteExprCode(pParse, pExpr->pLeft);
1323 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001324 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001325 break;
1326 }
drhcce7d172000-05-31 15:34:51 +00001327 case TK_ISNULL:
1328 case TK_NOTNULL: {
1329 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001330 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001331 break;
1332 }
drhfef52082000-06-06 01:50:43 +00001333 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001334 int addr;
drhcfab11b2000-06-06 03:31:22 +00001335 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001336 addr = sqliteVdbeCurrentAddr(v);
1337 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1338 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1339 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001340 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001341 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001342 }else{
drh99fcd712001-10-13 01:06:47 +00001343 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001344 }
1345 break;
1346 }
1347 case TK_BETWEEN: {
1348 int addr;
1349 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001350 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001351 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1352 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001353 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001354 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1355 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001356 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001357 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001358 break;
1359 }
drhcce7d172000-05-31 15:34:51 +00001360 default: {
1361 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001362 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001363 break;
1364 }
1365 }
1366}
drh22827922000-06-06 17:27:05 +00001367
1368/*
1369** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1370** if they are identical and return FALSE if they differ in any way.
1371*/
drhd8bc7082000-06-07 23:51:50 +00001372int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001373 int i;
1374 if( pA==0 ){
1375 return pB==0;
1376 }else if( pB==0 ){
1377 return 0;
1378 }
1379 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001380 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1381 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001382 if( pA->pList ){
1383 if( pB->pList==0 ) return 0;
1384 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1385 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001386 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001387 return 0;
1388 }
1389 }
1390 }else if( pB->pList ){
1391 return 0;
1392 }
1393 if( pA->pSelect || pB->pSelect ) return 0;
1394 if( pA->token.z ){
1395 if( pB->token.z==0 ) return 0;
1396 if( pB->token.n!=pA->token.n ) return 0;
1397 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1398 }
1399 return 1;
1400}
1401
1402/*
1403** Add a new element to the pParse->aAgg[] array and return its index.
1404*/
1405static int appendAggInfo(Parse *pParse){
1406 if( (pParse->nAgg & 0x7)==0 ){
1407 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001408 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1409 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001410 return -1;
1411 }
drh6d4abfb2001-10-22 02:58:08 +00001412 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001413 }
1414 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1415 return pParse->nAgg++;
1416}
1417
1418/*
1419** Analyze the given expression looking for aggregate functions and
1420** for variables that need to be added to the pParse->aAgg[] array.
1421** Make additional entries to the pParse->aAgg[] array as necessary.
1422**
1423** This routine should only be called after the expression has been
1424** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1425**
1426** If errors are seen, leave an error message in zErrMsg and return
1427** the number of errors.
1428*/
1429int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1430 int i;
1431 AggExpr *aAgg;
1432 int nErr = 0;
1433
1434 if( pExpr==0 ) return 0;
1435 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001436 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001437 aAgg = pParse->aAgg;
1438 for(i=0; i<pParse->nAgg; i++){
1439 if( aAgg[i].isAgg ) continue;
1440 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001441 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001442 break;
1443 }
1444 }
1445 if( i>=pParse->nAgg ){
1446 i = appendAggInfo(pParse);
1447 if( i<0 ) return 1;
1448 pParse->aAgg[i].isAgg = 0;
1449 pParse->aAgg[i].pExpr = pExpr;
1450 }
drhaaf88722000-06-08 11:25:00 +00001451 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001452 break;
1453 }
1454 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001455 aAgg = pParse->aAgg;
1456 for(i=0; i<pParse->nAgg; i++){
1457 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001458 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001459 break;
1460 }
1461 }
1462 if( i>=pParse->nAgg ){
1463 i = appendAggInfo(pParse);
1464 if( i<0 ) return 1;
1465 pParse->aAgg[i].isAgg = 1;
1466 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001467 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001468 pExpr->token.z, pExpr->token.n,
1469 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001470 }
1471 pExpr->iAgg = i;
1472 break;
1473 }
1474 default: {
1475 if( pExpr->pLeft ){
1476 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1477 }
1478 if( nErr==0 && pExpr->pRight ){
1479 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1480 }
1481 if( nErr==0 && pExpr->pList ){
1482 int n = pExpr->pList->nExpr;
1483 int i;
1484 for(i=0; nErr==0 && i<n; i++){
1485 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1486 }
1487 }
1488 break;
1489 }
1490 }
1491 return nErr;
1492}
drh8e0a2f92002-02-23 23:45:45 +00001493
1494/*
1495** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001496** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001497** function, or return NULL if the function does not exist.
1498**
drh0bce8352002-02-28 00:41:10 +00001499** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001500** structure is created and liked into the "db" structure if a
1501** no matching function previously existed. When createFlag is true
1502** and the nArg parameter is -1, then only a function that accepts
1503** any number of arguments will be returned.
1504**
1505** If createFlag is false and nArg is -1, then the first valid
1506** function found is returned. A function is valid if either xFunc
1507** or xStep is non-zero.
1508*/
drh0bce8352002-02-28 00:41:10 +00001509FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001510 sqlite *db, /* An open database */
1511 const char *zName, /* Name of the function. Not null-terminated */
1512 int nName, /* Number of characters in the name */
1513 int nArg, /* Number of arguments. -1 means any number */
1514 int createFlag /* Create new entry if true and does not otherwise exist */
1515){
drh0bce8352002-02-28 00:41:10 +00001516 FuncDef *pFirst, *p, *pMaybe;
1517 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001518 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001519 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1520 return p;
1521 }
1522 pMaybe = 0;
1523 while( p && p->nArg!=nArg ){
1524 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1525 p = p->pNext;
1526 }
1527 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1528 return 0;
1529 }
1530 if( p==0 && pMaybe ){
1531 assert( createFlag==0 );
1532 return pMaybe;
1533 }
drh89425d52002-02-28 03:04:48 +00001534 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001535 p->nArg = nArg;
1536 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001537 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001538 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001539 }
1540 return p;
1541}