blob: b41e0ad8c2a2bdc3dea84a43fc11587f688d7f7b [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**
danielk1977c3f9bad2002-05-15 08:30:12 +000015** $Id: expr.c,v 1.59 2002/05/15 08:30:13 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 */
487 if (cnt == 0 && pParse->trigStack != 0) {
488 TriggerStack * tt = pParse->trigStack;
489 int j;
490 int t = 0;
491 if (tt->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0) {
492 pExpr->iTable = tt->newIdx;
493 cntTab++;
494 t = 1;
495 }
496 if (tt->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0) {
497 pExpr->iTable = tt->oldIdx;
498 cntTab++;
499 t = 1;
500 }
501
502 if (t)
503 for(j=0; j<tt->pTab->nCol; j++) {
504 if( sqliteStrICmp(tt->pTab->aCol[j].zName, zRight)==0 ){
505 cnt++;
506 pExpr->iColumn = j;
507 }
508 }
509 }
510
drhc4a3c772001-04-04 11:48:57 +0000511 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
512 cnt = 1;
513 pExpr->iColumn = -1;
514 }
drhcce7d172000-05-31 15:34:51 +0000515 sqliteFree(zLeft);
516 sqliteFree(zRight);
517 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000518 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000519 pLeft->token.z, pLeft->token.n, ".", 1,
520 pRight->token.z, pRight->token.n, 0);
521 pParse->nErr++;
522 return 1;
523 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000524 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000525 pLeft->token.z, pLeft->token.n, ".", 1,
526 pRight->token.z, pRight->token.n, 0);
527 pParse->nErr++;
528 return 1;
529 }
530 sqliteExprDelete(pLeft);
531 pExpr->pLeft = 0;
532 sqliteExprDelete(pRight);
533 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000534 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000535 break;
536 }
537
drhfef52082000-06-06 01:50:43 +0000538 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000539 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000540 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000541 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000542 return 1;
543 }
drhfef52082000-06-06 01:50:43 +0000544 if( pExpr->pSelect ){
545 /* Case 1: expr IN (SELECT ...)
546 **
547 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000548 ** table. The cursor number of the temporary table has already
549 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000550 */
drh832508b2002-03-02 17:04:07 +0000551 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000552 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000553 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000554 }else if( pExpr->pList ){
555 /* Case 2: expr IN (exprlist)
556 **
557 ** Create a set to put the exprlist values in. The Set id is stored
558 ** in iTable.
559 */
560 int i, iSet;
561 for(i=0; i<pExpr->pList->nExpr; i++){
562 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000563 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000564 sqliteSetString(&pParse->zErrMsg,
565 "right-hand side of IN operator must be constant", 0);
566 pParse->nErr++;
567 return 1;
568 }
drh4794b982000-06-06 13:54:14 +0000569 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
570 return 1;
571 }
drhfef52082000-06-06 01:50:43 +0000572 }
573 iSet = pExpr->iTable = pParse->nSet++;
574 for(i=0; i<pExpr->pList->nExpr; i++){
575 Expr *pE2 = pExpr->pList->a[i].pExpr;
576 switch( pE2->op ){
577 case TK_FLOAT:
578 case TK_INTEGER:
579 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000580 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000581 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000582 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
583 sqliteVdbeDequoteP3(v, addr);
584 break;
585 }
586 default: {
587 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000588 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000589 break;
590 }
591 }
592 }
593 }
drhcfab11b2000-06-06 03:31:22 +0000594 break;
drhfef52082000-06-06 01:50:43 +0000595 }
596
drh19a775c2000-06-05 18:54:46 +0000597 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000598 /* This has to be a scalar SELECT. Generate code to put the
599 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000600 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000601 */
drh967e8b72000-06-21 13:59:10 +0000602 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000603 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000604 return 1;
605 }
606 break;
607 }
608
drhcce7d172000-05-31 15:34:51 +0000609 /* For all else, just recursively walk the tree */
610 default: {
drh4794b982000-06-06 13:54:14 +0000611 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000612 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000613 return 1;
614 }
615 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000616 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000617 return 1;
618 }
619 if( pExpr->pList ){
620 int i;
621 ExprList *pList = pExpr->pList;
622 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000623 Expr *pArg = pList->a[i].pExpr;
624 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000625 return 1;
626 }
627 }
628 }
629 }
630 }
631 return 0;
632}
633
drhcce7d172000-05-31 15:34:51 +0000634/*
635** Error check the functions in an expression. Make sure all
636** function names are recognized and all functions have the correct
637** number of arguments. Leave an error message in pParse->zErrMsg
638** if anything is amiss. Return the number of errors.
639**
640** if pIsAgg is not null and this expression is an aggregate function
641** (like count(*) or max(value)) then write a 1 into *pIsAgg.
642*/
643int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
644 int nErr = 0;
645 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000646 switch( pExpr->op ){
647 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000648 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
649 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000650 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000651 int is_agg = 0;
652 int i;
drh0bce8352002-02-28 00:41:10 +0000653 FuncDef *pDef;
654
drh89425d52002-02-28 03:04:48 +0000655 pDef = sqliteFindFunction(pParse->db,
656 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000657 if( pDef==0 ){
658 pDef = sqliteFindFunction(pParse->db,
659 pExpr->token.z, pExpr->token.n, -1, 0);
660 if( pDef==0 ){
661 no_such_func = 1;
662 }else{
663 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000664 }
drh0bce8352002-02-28 00:41:10 +0000665 }else{
666 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000667 }
drh8e0a2f92002-02-23 23:45:45 +0000668 if( is_agg && !allowAgg ){
669 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
670 pExpr->token.z, pExpr->token.n, "()", 2, 0);
671 pParse->nErr++;
672 nErr++;
673 is_agg = 0;
674 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000675 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
676 pExpr->token.z, pExpr->token.n, 0);
677 pParse->nErr++;
678 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000679 }else if( wrong_num_args ){
680 sqliteSetNString(&pParse->zErrMsg,
681 "wrong number of arguments to function ",-1,
682 pExpr->token.z, pExpr->token.n, "()", 2, 0);
683 pParse->nErr++;
684 nErr++;
drhcce7d172000-05-31 15:34:51 +0000685 }
drh22827922000-06-06 17:27:05 +0000686 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000687 if( is_agg && pIsAgg ) *pIsAgg = 1;
688 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000689 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
690 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000691 }
692 }
693 default: {
694 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000695 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000696 }
697 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000698 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000699 }
drhfef52082000-06-06 01:50:43 +0000700 if( nErr==0 && pExpr->pList ){
701 int n = pExpr->pList->nExpr;
702 int i;
703 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000704 Expr *pE2 = pExpr->pList->a[i].pExpr;
705 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000706 }
707 }
drhcce7d172000-05-31 15:34:51 +0000708 break;
709 }
710 }
711 return nErr;
712}
713
714/*
715** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000716** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000717*/
718void sqliteExprCode(Parse *pParse, Expr *pExpr){
719 Vdbe *v = pParse->pVdbe;
720 int op;
drhdaffd0e2001-04-11 14:28:42 +0000721 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000722 switch( pExpr->op ){
723 case TK_PLUS: op = OP_Add; break;
724 case TK_MINUS: op = OP_Subtract; break;
725 case TK_STAR: op = OP_Multiply; break;
726 case TK_SLASH: op = OP_Divide; break;
727 case TK_AND: op = OP_And; break;
728 case TK_OR: op = OP_Or; break;
729 case TK_LT: op = OP_Lt; break;
730 case TK_LE: op = OP_Le; break;
731 case TK_GT: op = OP_Gt; break;
732 case TK_GE: op = OP_Ge; break;
733 case TK_NE: op = OP_Ne; break;
734 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000735 case TK_ISNULL: op = OP_IsNull; break;
736 case TK_NOTNULL: op = OP_NotNull; break;
737 case TK_NOT: op = OP_Not; break;
738 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000739 case TK_BITAND: op = OP_BitAnd; break;
740 case TK_BITOR: op = OP_BitOr; break;
741 case TK_BITNOT: op = OP_BitNot; break;
742 case TK_LSHIFT: op = OP_ShiftLeft; break;
743 case TK_RSHIFT: op = OP_ShiftRight; break;
744 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000745 default: break;
746 }
747 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000748 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000749 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000750 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000751 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000752 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000753 }else{
drh99fcd712001-10-13 01:06:47 +0000754 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000755 }
drhcce7d172000-05-31 15:34:51 +0000756 break;
757 }
758 case TK_INTEGER: {
drhe6840902002-03-06 03:08:25 +0000759 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
760 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
761 break;
762 }
763 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000764 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000765 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000766 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000767 break;
768 }
drhcce7d172000-05-31 15:34:51 +0000769 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000770 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000771 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000772 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
773 sqliteVdbeDequoteP3(v, addr);
774 break;
775 }
776 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000777 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000778 break;
779 }
780 case TK_AND:
781 case TK_OR:
782 case TK_PLUS:
783 case TK_STAR:
784 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000785 case TK_REM:
786 case TK_BITAND:
787 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000788 case TK_SLASH: {
789 sqliteExprCode(pParse, pExpr->pLeft);
790 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000791 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000792 break;
793 }
drhbf4133c2001-10-13 02:59:08 +0000794 case TK_LSHIFT:
795 case TK_RSHIFT: {
796 sqliteExprCode(pParse, pExpr->pRight);
797 sqliteExprCode(pParse, pExpr->pLeft);
798 sqliteVdbeAddOp(v, op, 0, 0);
799 break;
800 }
drh00400772000-06-16 20:51:26 +0000801 case TK_CONCAT: {
802 sqliteExprCode(pParse, pExpr->pLeft);
803 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000804 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000805 break;
806 }
drhcce7d172000-05-31 15:34:51 +0000807 case TK_LT:
808 case TK_LE:
809 case TK_GT:
810 case TK_GE:
811 case TK_NE:
drh0ac65892002-04-20 14:24:41 +0000812 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000813 int dest;
drh99fcd712001-10-13 01:06:47 +0000814 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000815 sqliteExprCode(pParse, pExpr->pLeft);
816 sqliteExprCode(pParse, pExpr->pRight);
817 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000818 sqliteVdbeAddOp(v, op, 0, dest);
819 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000820 break;
821 }
drhcce7d172000-05-31 15:34:51 +0000822 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000823 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000824 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000825 Token *p = &pExpr->pLeft->token;
826 char *z = sqliteMalloc( p->n + 2 );
827 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +0000828 if( pExpr->pLeft->op==TK_INTEGER ){
829 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
830 }else{
831 sqliteVdbeAddOp(v, OP_String, 0, 0);
832 }
drh99fcd712001-10-13 01:06:47 +0000833 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000834 sqliteFree(z);
835 break;
836 }
drh1ccde152000-06-17 13:12:39 +0000837 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000838 }
drhbf4133c2001-10-13 02:59:08 +0000839 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000840 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000841 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000842 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000843 break;
844 }
845 case TK_ISNULL:
846 case TK_NOTNULL: {
847 int dest;
drh99fcd712001-10-13 01:06:47 +0000848 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000849 sqliteExprCode(pParse, pExpr->pLeft);
850 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000851 sqliteVdbeAddOp(v, op, 0, dest);
852 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000853 break;
854 }
drh22827922000-06-06 17:27:05 +0000855 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000856 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000857 break;
858 }
drhcce7d172000-05-31 15:34:51 +0000859 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000860 int i;
861 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000862 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000863 FuncDef *pDef;
864 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000865 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000866 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000867 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000868 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000869 }
drh89425d52002-02-28 03:04:48 +0000870 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000871 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000872 break;
873 }
drh19a775c2000-06-05 18:54:46 +0000874 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000875 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000876 break;
877 }
drhfef52082000-06-06 01:50:43 +0000878 case TK_IN: {
879 int addr;
drh99fcd712001-10-13 01:06:47 +0000880 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000881 sqliteExprCode(pParse, pExpr->pLeft);
882 addr = sqliteVdbeCurrentAddr(v);
883 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000884 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000885 }else{
drh99fcd712001-10-13 01:06:47 +0000886 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000887 }
drh99fcd712001-10-13 01:06:47 +0000888 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000889 break;
890 }
891 case TK_BETWEEN: {
892 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000893 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000894 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000895 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000896 sqliteVdbeResolveLabel(v, lbl);
897 break;
898 }
drha2e00042002-01-22 03:13:42 +0000899 case TK_AS: {
900 sqliteExprCode(pParse, pExpr->pLeft);
901 break;
902 }
drh17a7f8d2002-03-24 13:13:27 +0000903 case TK_CASE: {
904 int expr_end_label;
905 int next_when_label;
906 int i;
907
908 assert(pExpr->pList);
909 assert((pExpr->pList->nExpr % 2) == 0);
910 assert(pExpr->pList->nExpr > 0);
911 expr_end_label = sqliteVdbeMakeLabel(pParse->pVdbe);
912 if( pExpr->pLeft ){
913 sqliteExprCode(pParse, pExpr->pLeft);
914 }
915 for(i=0; i<pExpr->pList->nExpr; i=i+2){
916 if( i!=0 ){
917 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
918 }
919 next_when_label = sqliteVdbeMakeLabel(pParse->pVdbe);
920 if( pExpr->pLeft ){
921 sqliteVdbeAddOp(pParse->pVdbe, OP_Dup, 0, 1);
922 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
923 sqliteVdbeAddOp(pParse->pVdbe, OP_Ne, 0, next_when_label);
924 }else{
925 sqliteExprIfFalse(pParse, pExpr->pList->a[i].pExpr, next_when_label);
926 }
927 if( pExpr->pLeft ){
928 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
929 }
930 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
931 sqliteVdbeAddOp(pParse->pVdbe, OP_Goto, 0, expr_end_label);
932 }
933 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
934 if( pExpr->pLeft ){
935 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
936 }
937 if( pExpr->pRight ){
938 sqliteExprCode(pParse, pExpr->pRight);
939 }else{
940 sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0);
941 }
942 sqliteVdbeResolveLabel(pParse->pVdbe, expr_end_label);
943 }
944 break;
drhcce7d172000-05-31 15:34:51 +0000945 }
drhcce7d172000-05-31 15:34:51 +0000946}
947
948/*
949** Generate code for a boolean expression such that a jump is made
950** to the label "dest" if the expression is true but execution
951** continues straight thru if the expression is false.
952*/
953void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
954 Vdbe *v = pParse->pVdbe;
955 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000956 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000957 switch( pExpr->op ){
958 case TK_LT: op = OP_Lt; break;
959 case TK_LE: op = OP_Le; break;
960 case TK_GT: op = OP_Gt; break;
961 case TK_GE: op = OP_Ge; break;
962 case TK_NE: op = OP_Ne; break;
963 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000964 case TK_ISNULL: op = OP_IsNull; break;
965 case TK_NOTNULL: op = OP_NotNull; break;
966 default: break;
967 }
968 switch( pExpr->op ){
969 case TK_AND: {
970 int d2 = sqliteVdbeMakeLabel(v);
971 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
972 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
973 sqliteVdbeResolveLabel(v, d2);
974 break;
975 }
976 case TK_OR: {
977 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
978 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
979 break;
980 }
981 case TK_NOT: {
982 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
983 break;
984 }
985 case TK_LT:
986 case TK_LE:
987 case TK_GT:
988 case TK_GE:
989 case TK_NE:
drh0ac65892002-04-20 14:24:41 +0000990 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000991 sqliteExprCode(pParse, pExpr->pLeft);
992 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000993 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000994 break;
995 }
996 case TK_ISNULL:
997 case TK_NOTNULL: {
998 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000999 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001000 break;
1001 }
drhfef52082000-06-06 01:50:43 +00001002 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001003 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001004 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001005 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001006 }else{
drh99fcd712001-10-13 01:06:47 +00001007 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001008 }
1009 break;
1010 }
1011 case TK_BETWEEN: {
1012 int lbl = sqliteVdbeMakeLabel(v);
1013 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001014 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001015 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +00001016 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +00001017 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001018 sqliteVdbeAddOp(v, OP_Le, 0, dest);
1019 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1020 sqliteVdbeResolveLabel(v, lbl);
1021 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001022 break;
1023 }
drhcce7d172000-05-31 15:34:51 +00001024 default: {
1025 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001026 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001027 break;
1028 }
1029 }
1030}
1031
1032/*
drh66b89c82000-11-28 20:47:17 +00001033** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001034** to the label "dest" if the expression is false but execution
1035** continues straight thru if the expression is true.
1036*/
1037void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
1038 Vdbe *v = pParse->pVdbe;
1039 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001040 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001041 switch( pExpr->op ){
1042 case TK_LT: op = OP_Ge; break;
1043 case TK_LE: op = OP_Gt; break;
1044 case TK_GT: op = OP_Le; break;
1045 case TK_GE: op = OP_Lt; break;
1046 case TK_NE: op = OP_Eq; break;
1047 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001048 case TK_ISNULL: op = OP_NotNull; break;
1049 case TK_NOTNULL: op = OP_IsNull; break;
1050 default: break;
1051 }
1052 switch( pExpr->op ){
1053 case TK_AND: {
1054 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1055 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1056 break;
1057 }
1058 case TK_OR: {
1059 int d2 = sqliteVdbeMakeLabel(v);
1060 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1061 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1062 sqliteVdbeResolveLabel(v, d2);
1063 break;
1064 }
1065 case TK_NOT: {
1066 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1067 break;
1068 }
1069 case TK_LT:
1070 case TK_LE:
1071 case TK_GT:
1072 case TK_GE:
1073 case TK_NE:
1074 case TK_EQ: {
1075 sqliteExprCode(pParse, pExpr->pLeft);
1076 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001077 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001078 break;
1079 }
drhcce7d172000-05-31 15:34:51 +00001080 case TK_ISNULL:
1081 case TK_NOTNULL: {
1082 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001083 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001084 break;
1085 }
drhfef52082000-06-06 01:50:43 +00001086 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001087 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001088 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001089 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001090 }else{
drh99fcd712001-10-13 01:06:47 +00001091 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001092 }
1093 break;
1094 }
1095 case TK_BETWEEN: {
1096 int addr;
1097 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001098 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001099 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1100 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001101 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1102 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1103 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001104 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001105 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001106 break;
1107 }
drhcce7d172000-05-31 15:34:51 +00001108 default: {
1109 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001110 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1111 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001112 break;
1113 }
1114 }
1115}
drh22827922000-06-06 17:27:05 +00001116
1117/*
1118** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1119** if they are identical and return FALSE if they differ in any way.
1120*/
drhd8bc7082000-06-07 23:51:50 +00001121int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001122 int i;
1123 if( pA==0 ){
1124 return pB==0;
1125 }else if( pB==0 ){
1126 return 0;
1127 }
1128 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001129 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1130 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001131 if( pA->pList ){
1132 if( pB->pList==0 ) return 0;
1133 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1134 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001135 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001136 return 0;
1137 }
1138 }
1139 }else if( pB->pList ){
1140 return 0;
1141 }
1142 if( pA->pSelect || pB->pSelect ) return 0;
1143 if( pA->token.z ){
1144 if( pB->token.z==0 ) return 0;
1145 if( pB->token.n!=pA->token.n ) return 0;
1146 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1147 }
1148 return 1;
1149}
1150
1151/*
1152** Add a new element to the pParse->aAgg[] array and return its index.
1153*/
1154static int appendAggInfo(Parse *pParse){
1155 if( (pParse->nAgg & 0x7)==0 ){
1156 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001157 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1158 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001159 return -1;
1160 }
drh6d4abfb2001-10-22 02:58:08 +00001161 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001162 }
1163 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1164 return pParse->nAgg++;
1165}
1166
1167/*
1168** Analyze the given expression looking for aggregate functions and
1169** for variables that need to be added to the pParse->aAgg[] array.
1170** Make additional entries to the pParse->aAgg[] array as necessary.
1171**
1172** This routine should only be called after the expression has been
1173** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1174**
1175** If errors are seen, leave an error message in zErrMsg and return
1176** the number of errors.
1177*/
1178int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1179 int i;
1180 AggExpr *aAgg;
1181 int nErr = 0;
1182
1183 if( pExpr==0 ) return 0;
1184 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001185 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001186 aAgg = pParse->aAgg;
1187 for(i=0; i<pParse->nAgg; i++){
1188 if( aAgg[i].isAgg ) continue;
1189 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001190 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001191 break;
1192 }
1193 }
1194 if( i>=pParse->nAgg ){
1195 i = appendAggInfo(pParse);
1196 if( i<0 ) return 1;
1197 pParse->aAgg[i].isAgg = 0;
1198 pParse->aAgg[i].pExpr = pExpr;
1199 }
drhaaf88722000-06-08 11:25:00 +00001200 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001201 break;
1202 }
1203 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001204 aAgg = pParse->aAgg;
1205 for(i=0; i<pParse->nAgg; i++){
1206 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001207 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001208 break;
1209 }
1210 }
1211 if( i>=pParse->nAgg ){
1212 i = appendAggInfo(pParse);
1213 if( i<0 ) return 1;
1214 pParse->aAgg[i].isAgg = 1;
1215 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001216 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001217 pExpr->token.z, pExpr->token.n,
1218 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001219 }
1220 pExpr->iAgg = i;
1221 break;
1222 }
1223 default: {
1224 if( pExpr->pLeft ){
1225 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1226 }
1227 if( nErr==0 && pExpr->pRight ){
1228 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1229 }
1230 if( nErr==0 && pExpr->pList ){
1231 int n = pExpr->pList->nExpr;
1232 int i;
1233 for(i=0; nErr==0 && i<n; i++){
1234 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1235 }
1236 }
1237 break;
1238 }
1239 }
1240 return nErr;
1241}
drh8e0a2f92002-02-23 23:45:45 +00001242
1243/*
1244** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001245** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001246** function, or return NULL if the function does not exist.
1247**
drh0bce8352002-02-28 00:41:10 +00001248** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001249** structure is created and liked into the "db" structure if a
1250** no matching function previously existed. When createFlag is true
1251** and the nArg parameter is -1, then only a function that accepts
1252** any number of arguments will be returned.
1253**
1254** If createFlag is false and nArg is -1, then the first valid
1255** function found is returned. A function is valid if either xFunc
1256** or xStep is non-zero.
1257*/
drh0bce8352002-02-28 00:41:10 +00001258FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001259 sqlite *db, /* An open database */
1260 const char *zName, /* Name of the function. Not null-terminated */
1261 int nName, /* Number of characters in the name */
1262 int nArg, /* Number of arguments. -1 means any number */
1263 int createFlag /* Create new entry if true and does not otherwise exist */
1264){
drh0bce8352002-02-28 00:41:10 +00001265 FuncDef *pFirst, *p, *pMaybe;
1266 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001267 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001268 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1269 return p;
1270 }
1271 pMaybe = 0;
1272 while( p && p->nArg!=nArg ){
1273 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1274 p = p->pNext;
1275 }
1276 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1277 return 0;
1278 }
1279 if( p==0 && pMaybe ){
1280 assert( createFlag==0 );
1281 return pMaybe;
1282 }
drh89425d52002-02-28 03:04:48 +00001283 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001284 p->nArg = nArg;
1285 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001286 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001287 }
1288 return p;
1289}