blob: d487188232251ab76f7f1bd34a19182ce02d44c2 [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**
drh75148a22002-03-03 03:42:31 +000015** $Id: expr.c,v 1.53 2002/03/03 03:42:31 drh 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;
387 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000388 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000389 cnt++;
drh832508b2002-03-02 17:04:07 +0000390 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000391 if( j==pTab->iPKey ){
392 /* Substitute the record number for the INTEGER PRIMARY KEY */
393 pExpr->iColumn = -1;
394 }else{
395 pExpr->iColumn = j;
396 }
drha2e00042002-01-22 03:13:42 +0000397 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000398 }
399 }
400 }
drha2e00042002-01-22 03:13:42 +0000401 if( cnt==0 && pEList!=0 ){
402 int j;
403 for(j=0; j<pEList->nExpr; j++){
404 char *zAs = pEList->a[j].zName;
405 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
406 cnt++;
407 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
408 pExpr->op = TK_AS;
409 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000410 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000411 }
412 }
413 }
drhc4a3c772001-04-04 11:48:57 +0000414 if( cnt==0 && sqliteIsRowid(z) ){
415 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000416 pExpr->iTable = base;
drhc4a3c772001-04-04 11:48:57 +0000417 cnt = 1 + (pTabList->nId>1);
drha2e00042002-01-22 03:13:42 +0000418 pExpr->op = TK_COLUMN;
drhc4a3c772001-04-04 11:48:57 +0000419 }
drhcce7d172000-05-31 15:34:51 +0000420 sqliteFree(z);
421 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000422 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000423 pExpr->token.z, pExpr->token.n, 0);
424 pParse->nErr++;
425 return 1;
426 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000427 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000428 pExpr->token.z, pExpr->token.n, 0);
429 pParse->nErr++;
430 return 1;
431 }
drhcce7d172000-05-31 15:34:51 +0000432 break;
433 }
434
drh967e8b72000-06-21 13:59:10 +0000435 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000436 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000437 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000438 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000439 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000440 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000441 char *zLeft, *zRight; /* Text of an identifier */
442
443 pLeft = pExpr->pLeft;
444 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000445 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
446 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000447 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
448 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000449 if( zLeft==0 || zRight==0 ){
450 sqliteFree(zLeft);
451 sqliteFree(zRight);
452 return 1;
453 }
drh87c40e82001-07-23 14:33:02 +0000454 sqliteDequote(zLeft);
455 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000456 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000457 for(i=0; i<pTabList->nId; i++){
458 int j;
459 char *zTab;
460 Table *pTab = pTabList->a[i].pTab;
461 if( pTab==0 ) continue;
462 if( pTabList->a[i].zAlias ){
463 zTab = pTabList->a[i].zAlias;
464 }else{
465 zTab = pTab->zName;
466 }
467 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000468 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000469 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000470 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000471 cnt++;
drh832508b2002-03-02 17:04:07 +0000472 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000473 if( j==pTab->iPKey ){
474 /* Substitute the record number for the INTEGER PRIMARY KEY */
475 pExpr->iColumn = -1;
476 }else{
477 pExpr->iColumn = j;
478 }
drhcce7d172000-05-31 15:34:51 +0000479 }
480 }
481 }
drhc4a3c772001-04-04 11:48:57 +0000482 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
483 cnt = 1;
484 pExpr->iColumn = -1;
485 }
drhcce7d172000-05-31 15:34:51 +0000486 sqliteFree(zLeft);
487 sqliteFree(zRight);
488 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000489 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000490 pLeft->token.z, pLeft->token.n, ".", 1,
491 pRight->token.z, pRight->token.n, 0);
492 pParse->nErr++;
493 return 1;
494 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000495 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000496 pLeft->token.z, pLeft->token.n, ".", 1,
497 pRight->token.z, pRight->token.n, 0);
498 pParse->nErr++;
499 return 1;
500 }
501 sqliteExprDelete(pLeft);
502 pExpr->pLeft = 0;
503 sqliteExprDelete(pRight);
504 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000505 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000506 break;
507 }
508
drhfef52082000-06-06 01:50:43 +0000509 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000510 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000511 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000512 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000513 return 1;
514 }
drhfef52082000-06-06 01:50:43 +0000515 if( pExpr->pSelect ){
516 /* Case 1: expr IN (SELECT ...)
517 **
518 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000519 ** table. The cursor number of the temporary table has already
520 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000521 */
drh832508b2002-03-02 17:04:07 +0000522 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000523 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000524 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000525 }else if( pExpr->pList ){
526 /* Case 2: expr IN (exprlist)
527 **
528 ** Create a set to put the exprlist values in. The Set id is stored
529 ** in iTable.
530 */
531 int i, iSet;
532 for(i=0; i<pExpr->pList->nExpr; i++){
533 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000534 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000535 sqliteSetString(&pParse->zErrMsg,
536 "right-hand side of IN operator must be constant", 0);
537 pParse->nErr++;
538 return 1;
539 }
drh4794b982000-06-06 13:54:14 +0000540 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
541 return 1;
542 }
drhfef52082000-06-06 01:50:43 +0000543 }
544 iSet = pExpr->iTable = pParse->nSet++;
545 for(i=0; i<pExpr->pList->nExpr; i++){
546 Expr *pE2 = pExpr->pList->a[i].pExpr;
547 switch( pE2->op ){
548 case TK_FLOAT:
549 case TK_INTEGER:
550 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000551 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000552 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000553 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
554 sqliteVdbeDequoteP3(v, addr);
555 break;
556 }
557 default: {
558 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000559 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000560 break;
561 }
562 }
563 }
564 }
drhcfab11b2000-06-06 03:31:22 +0000565 break;
drhfef52082000-06-06 01:50:43 +0000566 }
567
drh19a775c2000-06-05 18:54:46 +0000568 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000569 /* This has to be a scalar SELECT. Generate code to put the
570 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000571 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000572 */
drh967e8b72000-06-21 13:59:10 +0000573 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000574 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000575 return 1;
576 }
577 break;
578 }
579
drhcce7d172000-05-31 15:34:51 +0000580 /* For all else, just recursively walk the tree */
581 default: {
drh4794b982000-06-06 13:54:14 +0000582 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000583 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000584 return 1;
585 }
586 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000587 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000588 return 1;
589 }
590 if( pExpr->pList ){
591 int i;
592 ExprList *pList = pExpr->pList;
593 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000594 Expr *pArg = pList->a[i].pExpr;
595 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000596 return 1;
597 }
598 }
599 }
600 }
601 }
602 return 0;
603}
604
drhcce7d172000-05-31 15:34:51 +0000605/*
606** Error check the functions in an expression. Make sure all
607** function names are recognized and all functions have the correct
608** number of arguments. Leave an error message in pParse->zErrMsg
609** if anything is amiss. Return the number of errors.
610**
611** if pIsAgg is not null and this expression is an aggregate function
612** (like count(*) or max(value)) then write a 1 into *pIsAgg.
613*/
614int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
615 int nErr = 0;
616 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000617 switch( pExpr->op ){
618 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000619 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
620 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000621 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000622 int is_agg = 0;
623 int i;
drh0bce8352002-02-28 00:41:10 +0000624 FuncDef *pDef;
625
drh89425d52002-02-28 03:04:48 +0000626 pDef = sqliteFindFunction(pParse->db,
627 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000628 if( pDef==0 ){
629 pDef = sqliteFindFunction(pParse->db,
630 pExpr->token.z, pExpr->token.n, -1, 0);
631 if( pDef==0 ){
632 no_such_func = 1;
633 }else{
634 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000635 }
drh0bce8352002-02-28 00:41:10 +0000636 }else{
637 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000638 }
drh8e0a2f92002-02-23 23:45:45 +0000639 if( is_agg && !allowAgg ){
640 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
641 pExpr->token.z, pExpr->token.n, "()", 2, 0);
642 pParse->nErr++;
643 nErr++;
644 is_agg = 0;
645 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000646 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
647 pExpr->token.z, pExpr->token.n, 0);
648 pParse->nErr++;
649 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000650 }else if( wrong_num_args ){
651 sqliteSetNString(&pParse->zErrMsg,
652 "wrong number of arguments to function ",-1,
653 pExpr->token.z, pExpr->token.n, "()", 2, 0);
654 pParse->nErr++;
655 nErr++;
drhcce7d172000-05-31 15:34:51 +0000656 }
drh22827922000-06-06 17:27:05 +0000657 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000658 if( is_agg && pIsAgg ) *pIsAgg = 1;
659 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000660 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
661 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000662 }
663 }
664 default: {
665 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000666 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000667 }
668 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000669 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000670 }
drhfef52082000-06-06 01:50:43 +0000671 if( nErr==0 && pExpr->pList ){
672 int n = pExpr->pList->nExpr;
673 int i;
674 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000675 Expr *pE2 = pExpr->pList->a[i].pExpr;
676 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000677 }
678 }
drhcce7d172000-05-31 15:34:51 +0000679 break;
680 }
681 }
682 return nErr;
683}
684
685/*
686** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000687** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000688*/
689void sqliteExprCode(Parse *pParse, Expr *pExpr){
690 Vdbe *v = pParse->pVdbe;
691 int op;
drhdaffd0e2001-04-11 14:28:42 +0000692 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000693 switch( pExpr->op ){
694 case TK_PLUS: op = OP_Add; break;
695 case TK_MINUS: op = OP_Subtract; break;
696 case TK_STAR: op = OP_Multiply; break;
697 case TK_SLASH: op = OP_Divide; break;
698 case TK_AND: op = OP_And; break;
699 case TK_OR: op = OP_Or; break;
700 case TK_LT: op = OP_Lt; break;
701 case TK_LE: op = OP_Le; break;
702 case TK_GT: op = OP_Gt; break;
703 case TK_GE: op = OP_Ge; break;
704 case TK_NE: op = OP_Ne; break;
705 case TK_EQ: op = OP_Eq; break;
706 case TK_LIKE: op = OP_Like; break;
707 case TK_GLOB: op = OP_Glob; break;
708 case TK_ISNULL: op = OP_IsNull; break;
709 case TK_NOTNULL: op = OP_NotNull; break;
710 case TK_NOT: op = OP_Not; break;
711 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000712 case TK_BITAND: op = OP_BitAnd; break;
713 case TK_BITOR: op = OP_BitOr; break;
714 case TK_BITNOT: op = OP_BitNot; break;
715 case TK_LSHIFT: op = OP_ShiftLeft; break;
716 case TK_RSHIFT: op = OP_ShiftRight; break;
717 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000718 default: break;
719 }
720 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000721 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000722 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000723 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000724 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000725 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000726 }else{
drh99fcd712001-10-13 01:06:47 +0000727 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000728 }
drhcce7d172000-05-31 15:34:51 +0000729 break;
730 }
drhef6764a2002-01-30 04:32:00 +0000731 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +0000732 case TK_INTEGER: {
drh7a7c7392001-11-24 00:31:46 +0000733 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000734 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000735 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000736 break;
737 }
drhcce7d172000-05-31 15:34:51 +0000738 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000739 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000740 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000741 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
742 sqliteVdbeDequoteP3(v, addr);
743 break;
744 }
745 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000746 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000747 break;
748 }
749 case TK_AND:
750 case TK_OR:
751 case TK_PLUS:
752 case TK_STAR:
753 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000754 case TK_REM:
755 case TK_BITAND:
756 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000757 case TK_SLASH: {
758 sqliteExprCode(pParse, pExpr->pLeft);
759 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000760 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000761 break;
762 }
drhbf4133c2001-10-13 02:59:08 +0000763 case TK_LSHIFT:
764 case TK_RSHIFT: {
765 sqliteExprCode(pParse, pExpr->pRight);
766 sqliteExprCode(pParse, pExpr->pLeft);
767 sqliteVdbeAddOp(v, op, 0, 0);
768 break;
769 }
drh00400772000-06-16 20:51:26 +0000770 case TK_CONCAT: {
771 sqliteExprCode(pParse, pExpr->pLeft);
772 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000773 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000774 break;
775 }
drhcce7d172000-05-31 15:34:51 +0000776 case TK_LT:
777 case TK_LE:
778 case TK_GT:
779 case TK_GE:
780 case TK_NE:
781 case TK_EQ:
782 case TK_LIKE:
783 case TK_GLOB: {
784 int dest;
drh99fcd712001-10-13 01:06:47 +0000785 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000786 sqliteExprCode(pParse, pExpr->pLeft);
787 sqliteExprCode(pParse, pExpr->pRight);
788 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000789 sqliteVdbeAddOp(v, op, 0, dest);
790 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000791 break;
792 }
drhcce7d172000-05-31 15:34:51 +0000793 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000794 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000795 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000796 Token *p = &pExpr->pLeft->token;
797 char *z = sqliteMalloc( p->n + 2 );
798 sprintf(z, "-%.*s", p->n, p->z);
drh99fcd712001-10-13 01:06:47 +0000799 sqliteVdbeAddOp(v, OP_String, 0, 0);
800 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000801 sqliteFree(z);
802 break;
803 }
drh1ccde152000-06-17 13:12:39 +0000804 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000805 }
drhbf4133c2001-10-13 02:59:08 +0000806 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000807 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000808 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000809 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000810 break;
811 }
812 case TK_ISNULL:
813 case TK_NOTNULL: {
814 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 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 }
drh22827922000-06-06 17:27:05 +0000822 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000823 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000824 break;
825 }
drhcce7d172000-05-31 15:34:51 +0000826 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000827 int i;
828 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000829 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000830 FuncDef *pDef;
831 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000832 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000833 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000834 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000835 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000836 }
drh89425d52002-02-28 03:04:48 +0000837 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000838 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000839 break;
840 }
drh19a775c2000-06-05 18:54:46 +0000841 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000842 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000843 break;
844 }
drhfef52082000-06-06 01:50:43 +0000845 case TK_IN: {
846 int addr;
drh99fcd712001-10-13 01:06:47 +0000847 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000848 sqliteExprCode(pParse, pExpr->pLeft);
849 addr = sqliteVdbeCurrentAddr(v);
850 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000851 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000852 }else{
drh99fcd712001-10-13 01:06:47 +0000853 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000854 }
drh99fcd712001-10-13 01:06:47 +0000855 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000856 break;
857 }
858 case TK_BETWEEN: {
859 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000860 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000861 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000862 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000863 sqliteVdbeResolveLabel(v, lbl);
864 break;
865 }
drha2e00042002-01-22 03:13:42 +0000866 case TK_AS: {
867 sqliteExprCode(pParse, pExpr->pLeft);
868 break;
869 }
drhcce7d172000-05-31 15:34:51 +0000870 }
871 return;
872}
873
874/*
875** Generate code for a boolean expression such that a jump is made
876** to the label "dest" if the expression is true but execution
877** continues straight thru if the expression is false.
878*/
879void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
880 Vdbe *v = pParse->pVdbe;
881 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000882 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000883 switch( pExpr->op ){
884 case TK_LT: op = OP_Lt; break;
885 case TK_LE: op = OP_Le; break;
886 case TK_GT: op = OP_Gt; break;
887 case TK_GE: op = OP_Ge; break;
888 case TK_NE: op = OP_Ne; break;
889 case TK_EQ: op = OP_Eq; break;
890 case TK_LIKE: op = OP_Like; break;
891 case TK_GLOB: op = OP_Glob; break;
892 case TK_ISNULL: op = OP_IsNull; break;
893 case TK_NOTNULL: op = OP_NotNull; break;
894 default: break;
895 }
896 switch( pExpr->op ){
897 case TK_AND: {
898 int d2 = sqliteVdbeMakeLabel(v);
899 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
900 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
901 sqliteVdbeResolveLabel(v, d2);
902 break;
903 }
904 case TK_OR: {
905 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
906 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
907 break;
908 }
909 case TK_NOT: {
910 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
911 break;
912 }
913 case TK_LT:
914 case TK_LE:
915 case TK_GT:
916 case TK_GE:
917 case TK_NE:
918 case TK_EQ:
919 case TK_LIKE:
920 case TK_GLOB: {
921 sqliteExprCode(pParse, pExpr->pLeft);
922 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000923 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000924 break;
925 }
926 case TK_ISNULL:
927 case TK_NOTNULL: {
928 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000929 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000930 break;
931 }
drhfef52082000-06-06 01:50:43 +0000932 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000933 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000934 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000935 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000936 }else{
drh99fcd712001-10-13 01:06:47 +0000937 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000938 }
939 break;
940 }
941 case TK_BETWEEN: {
942 int lbl = sqliteVdbeMakeLabel(v);
943 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000944 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000945 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000946 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +0000947 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000948 sqliteVdbeAddOp(v, OP_Le, 0, dest);
949 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
950 sqliteVdbeResolveLabel(v, lbl);
951 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +0000952 break;
953 }
drhcce7d172000-05-31 15:34:51 +0000954 default: {
955 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000956 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000957 break;
958 }
959 }
960}
961
962/*
drh66b89c82000-11-28 20:47:17 +0000963** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000964** to the label "dest" if the expression is false but execution
965** continues straight thru if the expression is true.
966*/
967void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
968 Vdbe *v = pParse->pVdbe;
969 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000970 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000971 switch( pExpr->op ){
972 case TK_LT: op = OP_Ge; break;
973 case TK_LE: op = OP_Gt; break;
974 case TK_GT: op = OP_Le; break;
975 case TK_GE: op = OP_Lt; break;
976 case TK_NE: op = OP_Eq; break;
977 case TK_EQ: op = OP_Ne; break;
978 case TK_LIKE: op = OP_Like; break;
979 case TK_GLOB: op = OP_Glob; break;
980 case TK_ISNULL: op = OP_NotNull; break;
981 case TK_NOTNULL: op = OP_IsNull; break;
982 default: break;
983 }
984 switch( pExpr->op ){
985 case TK_AND: {
986 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
987 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
988 break;
989 }
990 case TK_OR: {
991 int d2 = sqliteVdbeMakeLabel(v);
992 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
993 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
994 sqliteVdbeResolveLabel(v, d2);
995 break;
996 }
997 case TK_NOT: {
998 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
999 break;
1000 }
1001 case TK_LT:
1002 case TK_LE:
1003 case TK_GT:
1004 case TK_GE:
1005 case TK_NE:
1006 case TK_EQ: {
1007 sqliteExprCode(pParse, pExpr->pLeft);
1008 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001009 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001010 break;
1011 }
1012 case TK_LIKE:
1013 case TK_GLOB: {
1014 sqliteExprCode(pParse, pExpr->pLeft);
1015 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001016 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001017 break;
1018 }
1019 case TK_ISNULL:
1020 case TK_NOTNULL: {
1021 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001022 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001023 break;
1024 }
drhfef52082000-06-06 01:50:43 +00001025 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001026 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001027 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001028 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001029 }else{
drh99fcd712001-10-13 01:06:47 +00001030 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001031 }
1032 break;
1033 }
1034 case TK_BETWEEN: {
1035 int addr;
1036 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001037 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001038 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1039 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001040 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1041 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1042 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001043 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001044 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001045 break;
1046 }
drhcce7d172000-05-31 15:34:51 +00001047 default: {
1048 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001049 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1050 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001051 break;
1052 }
1053 }
1054}
drh22827922000-06-06 17:27:05 +00001055
1056/*
1057** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1058** if they are identical and return FALSE if they differ in any way.
1059*/
drhd8bc7082000-06-07 23:51:50 +00001060int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001061 int i;
1062 if( pA==0 ){
1063 return pB==0;
1064 }else if( pB==0 ){
1065 return 0;
1066 }
1067 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001068 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1069 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001070 if( pA->pList ){
1071 if( pB->pList==0 ) return 0;
1072 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1073 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001074 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001075 return 0;
1076 }
1077 }
1078 }else if( pB->pList ){
1079 return 0;
1080 }
1081 if( pA->pSelect || pB->pSelect ) return 0;
1082 if( pA->token.z ){
1083 if( pB->token.z==0 ) return 0;
1084 if( pB->token.n!=pA->token.n ) return 0;
1085 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1086 }
1087 return 1;
1088}
1089
1090/*
1091** Add a new element to the pParse->aAgg[] array and return its index.
1092*/
1093static int appendAggInfo(Parse *pParse){
1094 if( (pParse->nAgg & 0x7)==0 ){
1095 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001096 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1097 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001098 return -1;
1099 }
drh6d4abfb2001-10-22 02:58:08 +00001100 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001101 }
1102 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1103 return pParse->nAgg++;
1104}
1105
1106/*
1107** Analyze the given expression looking for aggregate functions and
1108** for variables that need to be added to the pParse->aAgg[] array.
1109** Make additional entries to the pParse->aAgg[] array as necessary.
1110**
1111** This routine should only be called after the expression has been
1112** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1113**
1114** If errors are seen, leave an error message in zErrMsg and return
1115** the number of errors.
1116*/
1117int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1118 int i;
1119 AggExpr *aAgg;
1120 int nErr = 0;
1121
1122 if( pExpr==0 ) return 0;
1123 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001124 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001125 aAgg = pParse->aAgg;
1126 for(i=0; i<pParse->nAgg; i++){
1127 if( aAgg[i].isAgg ) continue;
1128 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001129 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001130 break;
1131 }
1132 }
1133 if( i>=pParse->nAgg ){
1134 i = appendAggInfo(pParse);
1135 if( i<0 ) return 1;
1136 pParse->aAgg[i].isAgg = 0;
1137 pParse->aAgg[i].pExpr = pExpr;
1138 }
drhaaf88722000-06-08 11:25:00 +00001139 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001140 break;
1141 }
1142 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001143 aAgg = pParse->aAgg;
1144 for(i=0; i<pParse->nAgg; i++){
1145 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001146 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001147 break;
1148 }
1149 }
1150 if( i>=pParse->nAgg ){
1151 i = appendAggInfo(pParse);
1152 if( i<0 ) return 1;
1153 pParse->aAgg[i].isAgg = 1;
1154 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001155 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001156 pExpr->token.z, pExpr->token.n,
1157 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001158 }
1159 pExpr->iAgg = i;
1160 break;
1161 }
1162 default: {
1163 if( pExpr->pLeft ){
1164 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1165 }
1166 if( nErr==0 && pExpr->pRight ){
1167 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1168 }
1169 if( nErr==0 && pExpr->pList ){
1170 int n = pExpr->pList->nExpr;
1171 int i;
1172 for(i=0; nErr==0 && i<n; i++){
1173 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1174 }
1175 }
1176 break;
1177 }
1178 }
1179 return nErr;
1180}
drh8e0a2f92002-02-23 23:45:45 +00001181
1182/*
1183** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001184** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001185** function, or return NULL if the function does not exist.
1186**
drh0bce8352002-02-28 00:41:10 +00001187** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001188** structure is created and liked into the "db" structure if a
1189** no matching function previously existed. When createFlag is true
1190** and the nArg parameter is -1, then only a function that accepts
1191** any number of arguments will be returned.
1192**
1193** If createFlag is false and nArg is -1, then the first valid
1194** function found is returned. A function is valid if either xFunc
1195** or xStep is non-zero.
1196*/
drh0bce8352002-02-28 00:41:10 +00001197FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001198 sqlite *db, /* An open database */
1199 const char *zName, /* Name of the function. Not null-terminated */
1200 int nName, /* Number of characters in the name */
1201 int nArg, /* Number of arguments. -1 means any number */
1202 int createFlag /* Create new entry if true and does not otherwise exist */
1203){
drh0bce8352002-02-28 00:41:10 +00001204 FuncDef *pFirst, *p, *pMaybe;
1205 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001206 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001207 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1208 return p;
1209 }
1210 pMaybe = 0;
1211 while( p && p->nArg!=nArg ){
1212 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1213 p = p->pNext;
1214 }
1215 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1216 return 0;
1217 }
1218 if( p==0 && pMaybe ){
1219 assert( createFlag==0 );
1220 return pMaybe;
1221 }
drh89425d52002-02-28 03:04:48 +00001222 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001223 p->nArg = nArg;
1224 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001225 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001226 }
1227 return p;
1228}