blob: 008693efa003c7ced75bfb89e20be05b6119fa56 [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**
danielk1977f29ce552002-05-19 23:43:12 +000015** $Id: expr.c,v 1.60 2002/05/19 23:43:14 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
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;
124 if( p->token.z ) p->token.z += offset;
125 if( p->span.z ) p->span.z += offset;
126 if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset);
127 if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset);
128 if( p->pList ) sqliteExprListMoveStrings(p->pList, offset);
129 if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset);
130}
131void sqliteExprListMoveStrings(ExprList *pList, int offset){
132 int i;
133 if( pList==0 ) return;
134 for(i=0; i<pList->nExpr; i++){
135 sqliteExprMoveStrings(pList->a[i].pExpr, offset);
136 }
137}
138void sqliteSelectMoveStrings(Select *pSelect, int offset){
139 if( pSelect==0 ) return;
140 sqliteExprListMoveStrings(pSelect->pEList, offset);
141 sqliteExprMoveStrings(pSelect->pWhere, offset);
142 sqliteExprListMoveStrings(pSelect->pGroupBy, offset);
143 sqliteExprMoveStrings(pSelect->pHaving, offset);
144 sqliteExprListMoveStrings(pSelect->pOrderBy, offset);
145 sqliteSelectMoveStrings(pSelect->pPrior, offset);
146}
147
148/*
drhff78bd22002-02-27 01:47:11 +0000149** The following group of routines make deep copies of expressions,
150** expression lists, ID lists, and select statements. The copies can
151** be deleted (by being passed to their respective ...Delete() routines)
152** without effecting the originals.
153**
154** Note, however, that the Expr.token.z and Expr.span.z fields point to
155** string space that is allocated separately from the expression tree
156** itself. These routines do NOT duplicate that string space.
157**
158** The expression list and ID list return by sqliteExprListDup() and
159** sqliteIdListDup() can not be further expanded by subsequent calls
160** to sqliteExprListAppend() or sqliteIdListAppend().
161**
162** Any tables that the ID list might point to are not duplicated.
163*/
164Expr *sqliteExprDup(Expr *p){
165 Expr *pNew;
166 if( p==0 ) return 0;
167 pNew = sqliteMalloc( sizeof(*p) );
168 if( pNew==0 ) return 0;
169 pNew->op = p->op;
170 pNew->pLeft = sqliteExprDup(p->pLeft);
171 pNew->pRight = sqliteExprDup(p->pRight);
172 pNew->pList = sqliteExprListDup(p->pList);
drh832508b2002-03-02 17:04:07 +0000173 pNew->iTable = p->iTable;
174 pNew->iColumn = p->iColumn;
175 pNew->iAgg = p->iAgg;
drhff78bd22002-02-27 01:47:11 +0000176 pNew->token = p->token;
177 pNew->span = p->span;
178 pNew->pSelect = sqliteSelectDup(p->pSelect);
179 return pNew;
180}
181ExprList *sqliteExprListDup(ExprList *p){
182 ExprList *pNew;
183 int i;
184 if( p==0 ) return 0;
185 pNew = sqliteMalloc( sizeof(*pNew) );
186 if( pNew==0 ) return 0;
187 pNew->nExpr = p->nExpr;
188 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
189 for(i=0; i<p->nExpr; i++){
190 pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
191 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
192 pNew->a[i].sortOrder = p->a[i].sortOrder;
193 pNew->a[i].isAgg = p->a[i].isAgg;
194 pNew->a[i].done = 0;
195 }
196 return pNew;
197}
198IdList *sqliteIdListDup(IdList *p){
199 IdList *pNew;
200 int i;
201 if( p==0 ) return 0;
202 pNew = sqliteMalloc( sizeof(*pNew) );
203 if( pNew==0 ) return 0;
204 pNew->nId = p->nId;
205 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
206 for(i=0; i<p->nId; i++){
207 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
208 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
209 pNew->a[i].idx = p->a[i].idx;
210 pNew->a[i].pTab = 0;
211 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
212 }
213 return pNew;
214}
215Select *sqliteSelectDup(Select *p){
216 Select *pNew;
217 if( p==0 ) return 0;
218 pNew = sqliteMalloc( sizeof(*p) );
219 if( pNew==0 ) return 0;
220 pNew->isDistinct = p->isDistinct;
221 pNew->pEList = sqliteExprListDup(p->pEList);
222 pNew->pSrc = sqliteIdListDup(p->pSrc);
223 pNew->pWhere = sqliteExprDup(p->pWhere);
224 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
225 pNew->pHaving = sqliteExprDup(p->pHaving);
226 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
227 pNew->op = p->op;
228 pNew->pPrior = sqliteSelectDup(p->pPrior);
229 pNew->nLimit = p->nLimit;
230 pNew->nOffset = p->nOffset;
231 pNew->zSelect = 0;
232 return pNew;
233}
234
235
236/*
drha76b5df2002-02-23 02:32:10 +0000237** Add a new element to the end of an expression list. If pList is
238** initially NULL, then create a new expression list.
239*/
240ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
241 int i;
242 if( pList==0 ){
243 pList = sqliteMalloc( sizeof(ExprList) );
244 if( pList==0 ){
245 sqliteExprDelete(pExpr);
246 return 0;
247 }
248 }
249 if( (pList->nExpr & 7)==0 ){
250 int n = pList->nExpr + 8;
251 struct ExprList_item *a;
252 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
253 if( a==0 ){
254 sqliteExprDelete(pExpr);
255 return pList;
256 }
257 pList->a = a;
258 }
259 if( pExpr || pName ){
260 i = pList->nExpr++;
261 pList->a[i].pExpr = pExpr;
262 pList->a[i].zName = 0;
263 if( pName ){
264 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
265 sqliteDequote(pList->a[i].zName);
266 }
267 }
268 return pList;
269}
270
271/*
272** Delete an entire expression list.
273*/
274void sqliteExprListDelete(ExprList *pList){
275 int i;
276 if( pList==0 ) return;
277 for(i=0; i<pList->nExpr; i++){
278 sqliteExprDelete(pList->a[i].pExpr);
279 sqliteFree(pList->a[i].zName);
280 }
281 sqliteFree(pList->a);
282 sqliteFree(pList);
283}
284
285/*
drhfef52082000-06-06 01:50:43 +0000286** Walk an expression tree. Return 1 if the expression is constant
287** and 0 if it involves variables.
288*/
drh92086432002-01-22 14:11:29 +0000289int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000290 switch( p->op ){
291 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000292 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000293 case TK_DOT:
294 return 0;
drh92086432002-01-22 14:11:29 +0000295 case TK_INTEGER:
296 case TK_FLOAT:
297 case TK_STRING:
298 return 1;
drhfef52082000-06-06 01:50:43 +0000299 default: {
drh92086432002-01-22 14:11:29 +0000300 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
301 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000302 if( p->pList ){
303 int i;
304 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000305 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000306 }
307 }
drh92086432002-01-22 14:11:29 +0000308 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000309 }
310 }
drh92086432002-01-22 14:11:29 +0000311 return 0;
drhfef52082000-06-06 01:50:43 +0000312}
313
314/*
drhc4a3c772001-04-04 11:48:57 +0000315** Return TRUE if the given string is a row-id column name.
316*/
317static int sqliteIsRowid(const char *z){
318 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
319 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
320 if( sqliteStrICmp(z, "OID")==0 ) return 1;
321 return 0;
322}
323
324/*
drhcce7d172000-05-31 15:34:51 +0000325** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000326** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000327** index to the table in the table list and a column offset. The
328** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
329** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000330** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000331** VDBE cursor number for a cursor that is pointing into the referenced
332** table. The Expr.iColumn value is changed to the index of the column
333** of the referenced table. The Expr.iColumn value for the special
334** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
335** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000336**
drhfef52082000-06-06 01:50:43 +0000337** We also check for instances of the IN operator. IN comes in two
338** forms:
339**
340** expr IN (exprlist)
341** and
342** expr IN (SELECT ...)
343**
344** The first form is handled by creating a set holding the list
345** of allowed values. The second form causes the SELECT to generate
346** a temporary table.
347**
348** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000349** If it finds any, it generates code to write the value of that select
350** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000351**
drh967e8b72000-06-21 13:59:10 +0000352** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000353** the number of errors seen and leaves an error message on pParse->zErrMsg.
354*/
drha2e00042002-01-22 03:13:42 +0000355int sqliteExprResolveIds(
356 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000357 int base, /* VDBE cursor number for first entry in pTabList */
drha2e00042002-01-22 03:13:42 +0000358 IdList *pTabList, /* List of tables used to resolve column names */
359 ExprList *pEList, /* List of expressions used to resolve "AS" */
360 Expr *pExpr /* The expression to be analyzed. */
361){
drhdaffd0e2001-04-11 14:28:42 +0000362 if( pExpr==0 || pTabList==0 ) return 0;
drh832508b2002-03-02 17:04:07 +0000363 assert( base+pTabList->nId<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000364 switch( pExpr->op ){
drha2e00042002-01-22 03:13:42 +0000365 /* A lone identifier. Try and match it as follows:
366 **
367 ** 1. To the name of a column of one of the tables in pTabList
368 **
369 ** 2. To the right side of an AS keyword in the column list of
370 ** a SELECT statement. (For example, match against 'x' in
371 ** "SELECT a+b AS 'x' FROM t1".)
372 **
373 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
374 */
drhcce7d172000-05-31 15:34:51 +0000375 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000376 int cnt = 0; /* Number of matches */
377 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000378 char *z;
379 assert( pExpr->token.z );
380 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000381 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000382 if( z==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000383 for(i=0; i<pTabList->nId; i++){
384 int j;
385 Table *pTab = pTabList->a[i].pTab;
386 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000387 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000388 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000389 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000390 cnt++;
drh832508b2002-03-02 17:04:07 +0000391 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000392 if( j==pTab->iPKey ){
393 /* Substitute the record number for the INTEGER PRIMARY KEY */
394 pExpr->iColumn = -1;
395 }else{
396 pExpr->iColumn = j;
397 }
drha2e00042002-01-22 03:13:42 +0000398 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000399 }
400 }
401 }
drha2e00042002-01-22 03:13:42 +0000402 if( cnt==0 && pEList!=0 ){
403 int j;
404 for(j=0; j<pEList->nExpr; j++){
405 char *zAs = pEList->a[j].zName;
406 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
407 cnt++;
408 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
409 pExpr->op = TK_AS;
410 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000411 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000412 }
413 }
414 }
drhc4a3c772001-04-04 11:48:57 +0000415 if( cnt==0 && sqliteIsRowid(z) ){
416 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000417 pExpr->iTable = base;
drhc4a3c772001-04-04 11:48:57 +0000418 cnt = 1 + (pTabList->nId>1);
drha2e00042002-01-22 03:13:42 +0000419 pExpr->op = TK_COLUMN;
drhc4a3c772001-04-04 11:48:57 +0000420 }
drhcce7d172000-05-31 15:34:51 +0000421 sqliteFree(z);
422 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000423 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000424 pExpr->token.z, pExpr->token.n, 0);
425 pParse->nErr++;
426 return 1;
427 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000428 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000429 pExpr->token.z, pExpr->token.n, 0);
430 pParse->nErr++;
431 return 1;
432 }
drhcce7d172000-05-31 15:34:51 +0000433 break;
434 }
435
drh967e8b72000-06-21 13:59:10 +0000436 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000437 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000438 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000439 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000440 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000441 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000442 char *zLeft, *zRight; /* Text of an identifier */
443
444 pLeft = pExpr->pLeft;
445 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000446 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
447 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000448 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
449 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000450 if( zLeft==0 || zRight==0 ){
451 sqliteFree(zLeft);
452 sqliteFree(zRight);
453 return 1;
454 }
drh87c40e82001-07-23 14:33:02 +0000455 sqliteDequote(zLeft);
456 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000457 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000458 for(i=0; i<pTabList->nId; i++){
459 int j;
460 char *zTab;
461 Table *pTab = pTabList->a[i].pTab;
462 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000463 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000464 if( pTabList->a[i].zAlias ){
465 zTab = pTabList->a[i].zAlias;
466 }else{
467 zTab = pTab->zName;
468 }
drh094b2bb2002-03-13 18:54:07 +0000469 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000470 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000471 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000472 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000473 cnt++;
drh832508b2002-03-02 17:04:07 +0000474 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000475 if( j==pTab->iPKey ){
476 /* Substitute the record number for the INTEGER PRIMARY KEY */
477 pExpr->iColumn = -1;
478 }else{
479 pExpr->iColumn = j;
480 }
drhcce7d172000-05-31 15:34:51 +0000481 }
482 }
483 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000484
485 /* If we have not already resolved this *.* expression, then maybe
486 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000487 if( cnt == 0 && pParse->trigStack != 0 ){
488 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000489 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000490 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
491 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000492 cntTab++;
493 t = 1;
494 }
danielk1977f29ce552002-05-19 23:43:12 +0000495 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
496 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000497 cntTab++;
498 t = 1;
499 }
500
danielk1977f29ce552002-05-19 23:43:12 +0000501 if( t ){
502 int j;
503 for(j=0; j < pTriggerStack->pTab->nCol; j++) {
504 if( sqliteStrICmp(pTriggerStack->pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000505 cnt++;
506 pExpr->iColumn = j;
507 }
508 }
danielk1977f29ce552002-05-19 23:43:12 +0000509 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000510 }
511
drhc4a3c772001-04-04 11:48:57 +0000512 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
513 cnt = 1;
514 pExpr->iColumn = -1;
515 }
drhcce7d172000-05-31 15:34:51 +0000516 sqliteFree(zLeft);
517 sqliteFree(zRight);
518 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000519 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000520 pLeft->token.z, pLeft->token.n, ".", 1,
521 pRight->token.z, pRight->token.n, 0);
522 pParse->nErr++;
523 return 1;
524 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000525 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000526 pLeft->token.z, pLeft->token.n, ".", 1,
527 pRight->token.z, pRight->token.n, 0);
528 pParse->nErr++;
529 return 1;
530 }
531 sqliteExprDelete(pLeft);
532 pExpr->pLeft = 0;
533 sqliteExprDelete(pRight);
534 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000535 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000536 break;
537 }
538
drhfef52082000-06-06 01:50:43 +0000539 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000540 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000541 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000542 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000543 return 1;
544 }
drhfef52082000-06-06 01:50:43 +0000545 if( pExpr->pSelect ){
546 /* Case 1: expr IN (SELECT ...)
547 **
548 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000549 ** table. The cursor number of the temporary table has already
550 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000551 */
drh832508b2002-03-02 17:04:07 +0000552 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000553 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000554 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000555 }else if( pExpr->pList ){
556 /* Case 2: expr IN (exprlist)
557 **
558 ** Create a set to put the exprlist values in. The Set id is stored
559 ** in iTable.
560 */
561 int i, iSet;
562 for(i=0; i<pExpr->pList->nExpr; i++){
563 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000564 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000565 sqliteSetString(&pParse->zErrMsg,
566 "right-hand side of IN operator must be constant", 0);
567 pParse->nErr++;
568 return 1;
569 }
drh4794b982000-06-06 13:54:14 +0000570 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
571 return 1;
572 }
drhfef52082000-06-06 01:50:43 +0000573 }
574 iSet = pExpr->iTable = pParse->nSet++;
575 for(i=0; i<pExpr->pList->nExpr; i++){
576 Expr *pE2 = pExpr->pList->a[i].pExpr;
577 switch( pE2->op ){
578 case TK_FLOAT:
579 case TK_INTEGER:
580 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000581 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000582 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000583 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
584 sqliteVdbeDequoteP3(v, addr);
585 break;
586 }
587 default: {
588 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000589 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000590 break;
591 }
592 }
593 }
594 }
drhcfab11b2000-06-06 03:31:22 +0000595 break;
drhfef52082000-06-06 01:50:43 +0000596 }
597
drh19a775c2000-06-05 18:54:46 +0000598 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000599 /* This has to be a scalar SELECT. Generate code to put the
600 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000601 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000602 */
drh967e8b72000-06-21 13:59:10 +0000603 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000604 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000605 return 1;
606 }
607 break;
608 }
609
drhcce7d172000-05-31 15:34:51 +0000610 /* For all else, just recursively walk the tree */
611 default: {
drh4794b982000-06-06 13:54:14 +0000612 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000613 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000614 return 1;
615 }
616 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000617 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000618 return 1;
619 }
620 if( pExpr->pList ){
621 int i;
622 ExprList *pList = pExpr->pList;
623 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000624 Expr *pArg = pList->a[i].pExpr;
625 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000626 return 1;
627 }
628 }
629 }
630 }
631 }
632 return 0;
633}
634
drhcce7d172000-05-31 15:34:51 +0000635/*
636** Error check the functions in an expression. Make sure all
637** function names are recognized and all functions have the correct
638** number of arguments. Leave an error message in pParse->zErrMsg
639** if anything is amiss. Return the number of errors.
640**
641** if pIsAgg is not null and this expression is an aggregate function
642** (like count(*) or max(value)) then write a 1 into *pIsAgg.
643*/
644int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
645 int nErr = 0;
646 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000647 switch( pExpr->op ){
648 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000649 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
650 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000651 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000652 int is_agg = 0;
653 int i;
drh0bce8352002-02-28 00:41:10 +0000654 FuncDef *pDef;
655
drh89425d52002-02-28 03:04:48 +0000656 pDef = sqliteFindFunction(pParse->db,
657 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000658 if( pDef==0 ){
659 pDef = sqliteFindFunction(pParse->db,
660 pExpr->token.z, pExpr->token.n, -1, 0);
661 if( pDef==0 ){
662 no_such_func = 1;
663 }else{
664 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000665 }
drh0bce8352002-02-28 00:41:10 +0000666 }else{
667 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000668 }
drh8e0a2f92002-02-23 23:45:45 +0000669 if( is_agg && !allowAgg ){
670 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
671 pExpr->token.z, pExpr->token.n, "()", 2, 0);
672 pParse->nErr++;
673 nErr++;
674 is_agg = 0;
675 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000676 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
677 pExpr->token.z, pExpr->token.n, 0);
678 pParse->nErr++;
679 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000680 }else if( wrong_num_args ){
681 sqliteSetNString(&pParse->zErrMsg,
682 "wrong number of arguments to function ",-1,
683 pExpr->token.z, pExpr->token.n, "()", 2, 0);
684 pParse->nErr++;
685 nErr++;
drhcce7d172000-05-31 15:34:51 +0000686 }
drh22827922000-06-06 17:27:05 +0000687 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000688 if( is_agg && pIsAgg ) *pIsAgg = 1;
689 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000690 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
691 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000692 }
693 }
694 default: {
695 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000696 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000697 }
698 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000699 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000700 }
drhfef52082000-06-06 01:50:43 +0000701 if( nErr==0 && pExpr->pList ){
702 int n = pExpr->pList->nExpr;
703 int i;
704 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000705 Expr *pE2 = pExpr->pList->a[i].pExpr;
706 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000707 }
708 }
drhcce7d172000-05-31 15:34:51 +0000709 break;
710 }
711 }
712 return nErr;
713}
714
715/*
716** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000717** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000718*/
719void sqliteExprCode(Parse *pParse, Expr *pExpr){
720 Vdbe *v = pParse->pVdbe;
721 int op;
drhdaffd0e2001-04-11 14:28:42 +0000722 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000723 switch( pExpr->op ){
724 case TK_PLUS: op = OP_Add; break;
725 case TK_MINUS: op = OP_Subtract; break;
726 case TK_STAR: op = OP_Multiply; break;
727 case TK_SLASH: op = OP_Divide; break;
728 case TK_AND: op = OP_And; break;
729 case TK_OR: op = OP_Or; break;
730 case TK_LT: op = OP_Lt; break;
731 case TK_LE: op = OP_Le; break;
732 case TK_GT: op = OP_Gt; break;
733 case TK_GE: op = OP_Ge; break;
734 case TK_NE: op = OP_Ne; break;
735 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000736 case TK_ISNULL: op = OP_IsNull; break;
737 case TK_NOTNULL: op = OP_NotNull; break;
738 case TK_NOT: op = OP_Not; break;
739 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000740 case TK_BITAND: op = OP_BitAnd; break;
741 case TK_BITOR: op = OP_BitOr; break;
742 case TK_BITNOT: op = OP_BitNot; break;
743 case TK_LSHIFT: op = OP_ShiftLeft; break;
744 case TK_RSHIFT: op = OP_ShiftRight; break;
745 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000746 default: break;
747 }
748 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000749 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000750 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000751 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000752 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000753 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000754 }else{
drh99fcd712001-10-13 01:06:47 +0000755 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000756 }
drhcce7d172000-05-31 15:34:51 +0000757 break;
758 }
759 case TK_INTEGER: {
drhe6840902002-03-06 03:08:25 +0000760 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
761 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
762 break;
763 }
764 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000765 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000766 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000767 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000768 break;
769 }
drhcce7d172000-05-31 15:34:51 +0000770 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000771 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000772 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000773 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
774 sqliteVdbeDequoteP3(v, addr);
775 break;
776 }
777 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000778 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000779 break;
780 }
781 case TK_AND:
782 case TK_OR:
783 case TK_PLUS:
784 case TK_STAR:
785 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000786 case TK_REM:
787 case TK_BITAND:
788 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000789 case TK_SLASH: {
790 sqliteExprCode(pParse, pExpr->pLeft);
791 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000792 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000793 break;
794 }
drhbf4133c2001-10-13 02:59:08 +0000795 case TK_LSHIFT:
796 case TK_RSHIFT: {
797 sqliteExprCode(pParse, pExpr->pRight);
798 sqliteExprCode(pParse, pExpr->pLeft);
799 sqliteVdbeAddOp(v, op, 0, 0);
800 break;
801 }
drh00400772000-06-16 20:51:26 +0000802 case TK_CONCAT: {
803 sqliteExprCode(pParse, pExpr->pLeft);
804 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000805 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000806 break;
807 }
drhcce7d172000-05-31 15:34:51 +0000808 case TK_LT:
809 case TK_LE:
810 case TK_GT:
811 case TK_GE:
812 case TK_NE:
drh0ac65892002-04-20 14:24:41 +0000813 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000814 int dest;
drh99fcd712001-10-13 01:06:47 +0000815 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000816 sqliteExprCode(pParse, pExpr->pLeft);
817 sqliteExprCode(pParse, pExpr->pRight);
818 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000819 sqliteVdbeAddOp(v, op, 0, dest);
820 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000821 break;
822 }
drhcce7d172000-05-31 15:34:51 +0000823 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000824 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000825 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000826 Token *p = &pExpr->pLeft->token;
827 char *z = sqliteMalloc( p->n + 2 );
828 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +0000829 if( pExpr->pLeft->op==TK_INTEGER ){
830 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
831 }else{
832 sqliteVdbeAddOp(v, OP_String, 0, 0);
833 }
drh99fcd712001-10-13 01:06:47 +0000834 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000835 sqliteFree(z);
836 break;
837 }
drh1ccde152000-06-17 13:12:39 +0000838 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000839 }
drhbf4133c2001-10-13 02:59:08 +0000840 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000841 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000842 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000843 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000844 break;
845 }
846 case TK_ISNULL:
847 case TK_NOTNULL: {
848 int dest;
drh99fcd712001-10-13 01:06:47 +0000849 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000850 sqliteExprCode(pParse, pExpr->pLeft);
851 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000852 sqliteVdbeAddOp(v, op, 0, dest);
853 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000854 break;
855 }
drh22827922000-06-06 17:27:05 +0000856 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000857 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000858 break;
859 }
drhcce7d172000-05-31 15:34:51 +0000860 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000861 int i;
862 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000863 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000864 FuncDef *pDef;
865 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000866 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000867 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000868 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000869 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000870 }
drh89425d52002-02-28 03:04:48 +0000871 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000872 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000873 break;
874 }
drh19a775c2000-06-05 18:54:46 +0000875 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000876 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000877 break;
878 }
drhfef52082000-06-06 01:50:43 +0000879 case TK_IN: {
880 int addr;
drh99fcd712001-10-13 01:06:47 +0000881 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000882 sqliteExprCode(pParse, pExpr->pLeft);
883 addr = sqliteVdbeCurrentAddr(v);
884 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000885 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000886 }else{
drh99fcd712001-10-13 01:06:47 +0000887 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000888 }
drh99fcd712001-10-13 01:06:47 +0000889 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000890 break;
891 }
892 case TK_BETWEEN: {
893 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000894 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000895 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000896 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000897 sqliteVdbeResolveLabel(v, lbl);
898 break;
899 }
drha2e00042002-01-22 03:13:42 +0000900 case TK_AS: {
901 sqliteExprCode(pParse, pExpr->pLeft);
902 break;
903 }
drh17a7f8d2002-03-24 13:13:27 +0000904 case TK_CASE: {
905 int expr_end_label;
906 int next_when_label;
907 int i;
908
909 assert(pExpr->pList);
910 assert((pExpr->pList->nExpr % 2) == 0);
911 assert(pExpr->pList->nExpr > 0);
912 expr_end_label = sqliteVdbeMakeLabel(pParse->pVdbe);
913 if( pExpr->pLeft ){
914 sqliteExprCode(pParse, pExpr->pLeft);
915 }
916 for(i=0; i<pExpr->pList->nExpr; i=i+2){
917 if( i!=0 ){
918 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
919 }
920 next_when_label = sqliteVdbeMakeLabel(pParse->pVdbe);
921 if( pExpr->pLeft ){
922 sqliteVdbeAddOp(pParse->pVdbe, OP_Dup, 0, 1);
923 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
924 sqliteVdbeAddOp(pParse->pVdbe, OP_Ne, 0, next_when_label);
925 }else{
926 sqliteExprIfFalse(pParse, pExpr->pList->a[i].pExpr, next_when_label);
927 }
928 if( pExpr->pLeft ){
929 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
930 }
931 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
932 sqliteVdbeAddOp(pParse->pVdbe, OP_Goto, 0, expr_end_label);
933 }
934 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
935 if( pExpr->pLeft ){
936 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
937 }
938 if( pExpr->pRight ){
939 sqliteExprCode(pParse, pExpr->pRight);
940 }else{
941 sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0);
942 }
943 sqliteVdbeResolveLabel(pParse->pVdbe, expr_end_label);
944 }
945 break;
drhcce7d172000-05-31 15:34:51 +0000946 }
drhcce7d172000-05-31 15:34:51 +0000947}
948
949/*
950** Generate code for a boolean expression such that a jump is made
951** to the label "dest" if the expression is true but execution
952** continues straight thru if the expression is false.
953*/
954void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
955 Vdbe *v = pParse->pVdbe;
956 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000957 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000958 switch( pExpr->op ){
959 case TK_LT: op = OP_Lt; break;
960 case TK_LE: op = OP_Le; break;
961 case TK_GT: op = OP_Gt; break;
962 case TK_GE: op = OP_Ge; break;
963 case TK_NE: op = OP_Ne; break;
964 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000965 case TK_ISNULL: op = OP_IsNull; break;
966 case TK_NOTNULL: op = OP_NotNull; break;
967 default: break;
968 }
969 switch( pExpr->op ){
970 case TK_AND: {
971 int d2 = sqliteVdbeMakeLabel(v);
972 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
973 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
974 sqliteVdbeResolveLabel(v, d2);
975 break;
976 }
977 case TK_OR: {
978 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
979 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
980 break;
981 }
982 case TK_NOT: {
983 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
984 break;
985 }
986 case TK_LT:
987 case TK_LE:
988 case TK_GT:
989 case TK_GE:
990 case TK_NE:
drh0ac65892002-04-20 14:24:41 +0000991 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000992 sqliteExprCode(pParse, pExpr->pLeft);
993 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000994 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000995 break;
996 }
997 case TK_ISNULL:
998 case TK_NOTNULL: {
999 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001000 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001001 break;
1002 }
drhfef52082000-06-06 01:50:43 +00001003 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001004 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001005 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001006 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001007 }else{
drh99fcd712001-10-13 01:06:47 +00001008 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001009 }
1010 break;
1011 }
1012 case TK_BETWEEN: {
1013 int lbl = sqliteVdbeMakeLabel(v);
1014 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001015 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001016 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +00001017 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +00001018 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001019 sqliteVdbeAddOp(v, OP_Le, 0, dest);
1020 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1021 sqliteVdbeResolveLabel(v, lbl);
1022 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001023 break;
1024 }
drhcce7d172000-05-31 15:34:51 +00001025 default: {
1026 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001027 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001028 break;
1029 }
1030 }
1031}
1032
1033/*
drh66b89c82000-11-28 20:47:17 +00001034** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001035** to the label "dest" if the expression is false but execution
1036** continues straight thru if the expression is true.
1037*/
1038void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
1039 Vdbe *v = pParse->pVdbe;
1040 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001041 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001042 switch( pExpr->op ){
1043 case TK_LT: op = OP_Ge; break;
1044 case TK_LE: op = OP_Gt; break;
1045 case TK_GT: op = OP_Le; break;
1046 case TK_GE: op = OP_Lt; break;
1047 case TK_NE: op = OP_Eq; break;
1048 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001049 case TK_ISNULL: op = OP_NotNull; break;
1050 case TK_NOTNULL: op = OP_IsNull; break;
1051 default: break;
1052 }
1053 switch( pExpr->op ){
1054 case TK_AND: {
1055 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1056 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1057 break;
1058 }
1059 case TK_OR: {
1060 int d2 = sqliteVdbeMakeLabel(v);
1061 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1062 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1063 sqliteVdbeResolveLabel(v, d2);
1064 break;
1065 }
1066 case TK_NOT: {
1067 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1068 break;
1069 }
1070 case TK_LT:
1071 case TK_LE:
1072 case TK_GT:
1073 case TK_GE:
1074 case TK_NE:
1075 case TK_EQ: {
1076 sqliteExprCode(pParse, pExpr->pLeft);
1077 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001078 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001079 break;
1080 }
drhcce7d172000-05-31 15:34:51 +00001081 case TK_ISNULL:
1082 case TK_NOTNULL: {
1083 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001084 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001085 break;
1086 }
drhfef52082000-06-06 01:50:43 +00001087 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001088 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001089 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001090 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001091 }else{
drh99fcd712001-10-13 01:06:47 +00001092 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001093 }
1094 break;
1095 }
1096 case TK_BETWEEN: {
1097 int addr;
1098 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001099 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001100 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1101 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001102 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1103 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1104 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001105 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001106 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001107 break;
1108 }
drhcce7d172000-05-31 15:34:51 +00001109 default: {
1110 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001111 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1112 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001113 break;
1114 }
1115 }
1116}
drh22827922000-06-06 17:27:05 +00001117
1118/*
1119** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1120** if they are identical and return FALSE if they differ in any way.
1121*/
drhd8bc7082000-06-07 23:51:50 +00001122int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001123 int i;
1124 if( pA==0 ){
1125 return pB==0;
1126 }else if( pB==0 ){
1127 return 0;
1128 }
1129 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001130 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1131 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001132 if( pA->pList ){
1133 if( pB->pList==0 ) return 0;
1134 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1135 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001136 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001137 return 0;
1138 }
1139 }
1140 }else if( pB->pList ){
1141 return 0;
1142 }
1143 if( pA->pSelect || pB->pSelect ) return 0;
1144 if( pA->token.z ){
1145 if( pB->token.z==0 ) return 0;
1146 if( pB->token.n!=pA->token.n ) return 0;
1147 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1148 }
1149 return 1;
1150}
1151
1152/*
1153** Add a new element to the pParse->aAgg[] array and return its index.
1154*/
1155static int appendAggInfo(Parse *pParse){
1156 if( (pParse->nAgg & 0x7)==0 ){
1157 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001158 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1159 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001160 return -1;
1161 }
drh6d4abfb2001-10-22 02:58:08 +00001162 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001163 }
1164 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1165 return pParse->nAgg++;
1166}
1167
1168/*
1169** Analyze the given expression looking for aggregate functions and
1170** for variables that need to be added to the pParse->aAgg[] array.
1171** Make additional entries to the pParse->aAgg[] array as necessary.
1172**
1173** This routine should only be called after the expression has been
1174** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1175**
1176** If errors are seen, leave an error message in zErrMsg and return
1177** the number of errors.
1178*/
1179int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1180 int i;
1181 AggExpr *aAgg;
1182 int nErr = 0;
1183
1184 if( pExpr==0 ) return 0;
1185 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001186 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001187 aAgg = pParse->aAgg;
1188 for(i=0; i<pParse->nAgg; i++){
1189 if( aAgg[i].isAgg ) continue;
1190 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001191 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001192 break;
1193 }
1194 }
1195 if( i>=pParse->nAgg ){
1196 i = appendAggInfo(pParse);
1197 if( i<0 ) return 1;
1198 pParse->aAgg[i].isAgg = 0;
1199 pParse->aAgg[i].pExpr = pExpr;
1200 }
drhaaf88722000-06-08 11:25:00 +00001201 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001202 break;
1203 }
1204 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001205 aAgg = pParse->aAgg;
1206 for(i=0; i<pParse->nAgg; i++){
1207 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001208 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001209 break;
1210 }
1211 }
1212 if( i>=pParse->nAgg ){
1213 i = appendAggInfo(pParse);
1214 if( i<0 ) return 1;
1215 pParse->aAgg[i].isAgg = 1;
1216 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001217 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001218 pExpr->token.z, pExpr->token.n,
1219 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001220 }
1221 pExpr->iAgg = i;
1222 break;
1223 }
1224 default: {
1225 if( pExpr->pLeft ){
1226 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1227 }
1228 if( nErr==0 && pExpr->pRight ){
1229 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1230 }
1231 if( nErr==0 && pExpr->pList ){
1232 int n = pExpr->pList->nExpr;
1233 int i;
1234 for(i=0; nErr==0 && i<n; i++){
1235 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1236 }
1237 }
1238 break;
1239 }
1240 }
1241 return nErr;
1242}
drh8e0a2f92002-02-23 23:45:45 +00001243
1244/*
1245** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001246** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001247** function, or return NULL if the function does not exist.
1248**
drh0bce8352002-02-28 00:41:10 +00001249** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001250** structure is created and liked into the "db" structure if a
1251** no matching function previously existed. When createFlag is true
1252** and the nArg parameter is -1, then only a function that accepts
1253** any number of arguments will be returned.
1254**
1255** If createFlag is false and nArg is -1, then the first valid
1256** function found is returned. A function is valid if either xFunc
1257** or xStep is non-zero.
1258*/
drh0bce8352002-02-28 00:41:10 +00001259FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001260 sqlite *db, /* An open database */
1261 const char *zName, /* Name of the function. Not null-terminated */
1262 int nName, /* Number of characters in the name */
1263 int nArg, /* Number of arguments. -1 means any number */
1264 int createFlag /* Create new entry if true and does not otherwise exist */
1265){
drh0bce8352002-02-28 00:41:10 +00001266 FuncDef *pFirst, *p, *pMaybe;
1267 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001268 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001269 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1270 return p;
1271 }
1272 pMaybe = 0;
1273 while( p && p->nArg!=nArg ){
1274 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1275 p = p->pNext;
1276 }
1277 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1278 return 0;
1279 }
1280 if( p==0 && pMaybe ){
1281 assert( createFlag==0 );
1282 return pMaybe;
1283 }
drh89425d52002-02-28 03:04:48 +00001284 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001285 p->nArg = nArg;
1286 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001287 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001288 }
1289 return p;
1290}