blob: 58ed85a672186e22f5399e0619bb75ad02a5a9b6 [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**
drhe6840902002-03-06 03:08:25 +000015** $Id: expr.c,v 1.55 2002/03/06 03:08:26 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;
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 }
469 if( 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 }
drhc4a3c772001-04-04 11:48:57 +0000484 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
485 cnt = 1;
486 pExpr->iColumn = -1;
487 }
drhcce7d172000-05-31 15:34:51 +0000488 sqliteFree(zLeft);
489 sqliteFree(zRight);
490 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000491 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000492 pLeft->token.z, pLeft->token.n, ".", 1,
493 pRight->token.z, pRight->token.n, 0);
494 pParse->nErr++;
495 return 1;
496 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000497 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000498 pLeft->token.z, pLeft->token.n, ".", 1,
499 pRight->token.z, pRight->token.n, 0);
500 pParse->nErr++;
501 return 1;
502 }
503 sqliteExprDelete(pLeft);
504 pExpr->pLeft = 0;
505 sqliteExprDelete(pRight);
506 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000507 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000508 break;
509 }
510
drhfef52082000-06-06 01:50:43 +0000511 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000512 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000513 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000514 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000515 return 1;
516 }
drhfef52082000-06-06 01:50:43 +0000517 if( pExpr->pSelect ){
518 /* Case 1: expr IN (SELECT ...)
519 **
520 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000521 ** table. The cursor number of the temporary table has already
522 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000523 */
drh832508b2002-03-02 17:04:07 +0000524 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000525 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000526 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000527 }else if( pExpr->pList ){
528 /* Case 2: expr IN (exprlist)
529 **
530 ** Create a set to put the exprlist values in. The Set id is stored
531 ** in iTable.
532 */
533 int i, iSet;
534 for(i=0; i<pExpr->pList->nExpr; i++){
535 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000536 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000537 sqliteSetString(&pParse->zErrMsg,
538 "right-hand side of IN operator must be constant", 0);
539 pParse->nErr++;
540 return 1;
541 }
drh4794b982000-06-06 13:54:14 +0000542 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
543 return 1;
544 }
drhfef52082000-06-06 01:50:43 +0000545 }
546 iSet = pExpr->iTable = pParse->nSet++;
547 for(i=0; i<pExpr->pList->nExpr; i++){
548 Expr *pE2 = pExpr->pList->a[i].pExpr;
549 switch( pE2->op ){
550 case TK_FLOAT:
551 case TK_INTEGER:
552 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000553 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000554 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000555 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
556 sqliteVdbeDequoteP3(v, addr);
557 break;
558 }
559 default: {
560 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000561 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000562 break;
563 }
564 }
565 }
566 }
drhcfab11b2000-06-06 03:31:22 +0000567 break;
drhfef52082000-06-06 01:50:43 +0000568 }
569
drh19a775c2000-06-05 18:54:46 +0000570 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000571 /* This has to be a scalar SELECT. Generate code to put the
572 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000573 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000574 */
drh967e8b72000-06-21 13:59:10 +0000575 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000576 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000577 return 1;
578 }
579 break;
580 }
581
drhcce7d172000-05-31 15:34:51 +0000582 /* For all else, just recursively walk the tree */
583 default: {
drh4794b982000-06-06 13:54:14 +0000584 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000585 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000586 return 1;
587 }
588 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000589 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000590 return 1;
591 }
592 if( pExpr->pList ){
593 int i;
594 ExprList *pList = pExpr->pList;
595 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000596 Expr *pArg = pList->a[i].pExpr;
597 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000598 return 1;
599 }
600 }
601 }
602 }
603 }
604 return 0;
605}
606
drhcce7d172000-05-31 15:34:51 +0000607/*
608** Error check the functions in an expression. Make sure all
609** function names are recognized and all functions have the correct
610** number of arguments. Leave an error message in pParse->zErrMsg
611** if anything is amiss. Return the number of errors.
612**
613** if pIsAgg is not null and this expression is an aggregate function
614** (like count(*) or max(value)) then write a 1 into *pIsAgg.
615*/
616int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
617 int nErr = 0;
618 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000619 switch( pExpr->op ){
620 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000621 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
622 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000623 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000624 int is_agg = 0;
625 int i;
drh0bce8352002-02-28 00:41:10 +0000626 FuncDef *pDef;
627
drh89425d52002-02-28 03:04:48 +0000628 pDef = sqliteFindFunction(pParse->db,
629 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000630 if( pDef==0 ){
631 pDef = sqliteFindFunction(pParse->db,
632 pExpr->token.z, pExpr->token.n, -1, 0);
633 if( pDef==0 ){
634 no_such_func = 1;
635 }else{
636 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000637 }
drh0bce8352002-02-28 00:41:10 +0000638 }else{
639 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000640 }
drh8e0a2f92002-02-23 23:45:45 +0000641 if( is_agg && !allowAgg ){
642 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
643 pExpr->token.z, pExpr->token.n, "()", 2, 0);
644 pParse->nErr++;
645 nErr++;
646 is_agg = 0;
647 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000648 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
649 pExpr->token.z, pExpr->token.n, 0);
650 pParse->nErr++;
651 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000652 }else if( wrong_num_args ){
653 sqliteSetNString(&pParse->zErrMsg,
654 "wrong number of arguments to function ",-1,
655 pExpr->token.z, pExpr->token.n, "()", 2, 0);
656 pParse->nErr++;
657 nErr++;
drhcce7d172000-05-31 15:34:51 +0000658 }
drh22827922000-06-06 17:27:05 +0000659 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000660 if( is_agg && pIsAgg ) *pIsAgg = 1;
661 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000662 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
663 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000664 }
665 }
666 default: {
667 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000668 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000669 }
670 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000671 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000672 }
drhfef52082000-06-06 01:50:43 +0000673 if( nErr==0 && pExpr->pList ){
674 int n = pExpr->pList->nExpr;
675 int i;
676 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000677 Expr *pE2 = pExpr->pList->a[i].pExpr;
678 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000679 }
680 }
drhcce7d172000-05-31 15:34:51 +0000681 break;
682 }
683 }
684 return nErr;
685}
686
687/*
688** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000689** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000690*/
691void sqliteExprCode(Parse *pParse, Expr *pExpr){
692 Vdbe *v = pParse->pVdbe;
693 int op;
drhdaffd0e2001-04-11 14:28:42 +0000694 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000695 switch( pExpr->op ){
696 case TK_PLUS: op = OP_Add; break;
697 case TK_MINUS: op = OP_Subtract; break;
698 case TK_STAR: op = OP_Multiply; break;
699 case TK_SLASH: op = OP_Divide; break;
700 case TK_AND: op = OP_And; break;
701 case TK_OR: op = OP_Or; break;
702 case TK_LT: op = OP_Lt; break;
703 case TK_LE: op = OP_Le; break;
704 case TK_GT: op = OP_Gt; break;
705 case TK_GE: op = OP_Ge; break;
706 case TK_NE: op = OP_Ne; break;
707 case TK_EQ: op = OP_Eq; break;
708 case TK_LIKE: op = OP_Like; break;
709 case TK_GLOB: op = OP_Glob; break;
710 case TK_ISNULL: op = OP_IsNull; break;
711 case TK_NOTNULL: op = OP_NotNull; break;
712 case TK_NOT: op = OP_Not; break;
713 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000714 case TK_BITAND: op = OP_BitAnd; break;
715 case TK_BITOR: op = OP_BitOr; break;
716 case TK_BITNOT: op = OP_BitNot; break;
717 case TK_LSHIFT: op = OP_ShiftLeft; break;
718 case TK_RSHIFT: op = OP_ShiftRight; break;
719 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000720 default: break;
721 }
722 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000723 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000724 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000725 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000726 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000727 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000728 }else{
drh99fcd712001-10-13 01:06:47 +0000729 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000730 }
drhcce7d172000-05-31 15:34:51 +0000731 break;
732 }
733 case TK_INTEGER: {
drhe6840902002-03-06 03:08:25 +0000734 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
735 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
736 break;
737 }
738 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000739 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000740 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000741 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000742 break;
743 }
drhcce7d172000-05-31 15:34:51 +0000744 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000745 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000746 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000747 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
748 sqliteVdbeDequoteP3(v, addr);
749 break;
750 }
751 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000752 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000753 break;
754 }
755 case TK_AND:
756 case TK_OR:
757 case TK_PLUS:
758 case TK_STAR:
759 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000760 case TK_REM:
761 case TK_BITAND:
762 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000763 case TK_SLASH: {
764 sqliteExprCode(pParse, pExpr->pLeft);
765 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000766 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000767 break;
768 }
drhbf4133c2001-10-13 02:59:08 +0000769 case TK_LSHIFT:
770 case TK_RSHIFT: {
771 sqliteExprCode(pParse, pExpr->pRight);
772 sqliteExprCode(pParse, pExpr->pLeft);
773 sqliteVdbeAddOp(v, op, 0, 0);
774 break;
775 }
drh00400772000-06-16 20:51:26 +0000776 case TK_CONCAT: {
777 sqliteExprCode(pParse, pExpr->pLeft);
778 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000779 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000780 break;
781 }
drhcce7d172000-05-31 15:34:51 +0000782 case TK_LT:
783 case TK_LE:
784 case TK_GT:
785 case TK_GE:
786 case TK_NE:
787 case TK_EQ:
788 case TK_LIKE:
789 case TK_GLOB: {
790 int dest;
drh99fcd712001-10-13 01:06:47 +0000791 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000792 sqliteExprCode(pParse, pExpr->pLeft);
793 sqliteExprCode(pParse, pExpr->pRight);
794 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000795 sqliteVdbeAddOp(v, op, 0, dest);
796 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000797 break;
798 }
drhcce7d172000-05-31 15:34:51 +0000799 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000800 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000801 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000802 Token *p = &pExpr->pLeft->token;
803 char *z = sqliteMalloc( p->n + 2 );
804 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +0000805 if( pExpr->pLeft->op==TK_INTEGER ){
806 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
807 }else{
808 sqliteVdbeAddOp(v, OP_String, 0, 0);
809 }
drh99fcd712001-10-13 01:06:47 +0000810 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000811 sqliteFree(z);
812 break;
813 }
drh1ccde152000-06-17 13:12:39 +0000814 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000815 }
drhbf4133c2001-10-13 02:59:08 +0000816 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000817 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000818 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000819 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000820 break;
821 }
822 case TK_ISNULL:
823 case TK_NOTNULL: {
824 int dest;
drh99fcd712001-10-13 01:06:47 +0000825 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000826 sqliteExprCode(pParse, pExpr->pLeft);
827 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000828 sqliteVdbeAddOp(v, op, 0, dest);
829 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000830 break;
831 }
drh22827922000-06-06 17:27:05 +0000832 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000833 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000834 break;
835 }
drhcce7d172000-05-31 15:34:51 +0000836 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000837 int i;
838 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000839 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000840 FuncDef *pDef;
841 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000842 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000843 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000844 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000845 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000846 }
drh89425d52002-02-28 03:04:48 +0000847 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000848 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000849 break;
850 }
drh19a775c2000-06-05 18:54:46 +0000851 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000852 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000853 break;
854 }
drhfef52082000-06-06 01:50:43 +0000855 case TK_IN: {
856 int addr;
drh99fcd712001-10-13 01:06:47 +0000857 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000858 sqliteExprCode(pParse, pExpr->pLeft);
859 addr = sqliteVdbeCurrentAddr(v);
860 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000861 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000862 }else{
drh99fcd712001-10-13 01:06:47 +0000863 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000864 }
drh99fcd712001-10-13 01:06:47 +0000865 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000866 break;
867 }
868 case TK_BETWEEN: {
869 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000870 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000871 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000872 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000873 sqliteVdbeResolveLabel(v, lbl);
874 break;
875 }
drha2e00042002-01-22 03:13:42 +0000876 case TK_AS: {
877 sqliteExprCode(pParse, pExpr->pLeft);
878 break;
879 }
drhcce7d172000-05-31 15:34:51 +0000880 }
881 return;
882}
883
884/*
885** Generate code for a boolean expression such that a jump is made
886** to the label "dest" if the expression is true but execution
887** continues straight thru if the expression is false.
888*/
889void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
890 Vdbe *v = pParse->pVdbe;
891 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000892 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000893 switch( pExpr->op ){
894 case TK_LT: op = OP_Lt; break;
895 case TK_LE: op = OP_Le; break;
896 case TK_GT: op = OP_Gt; break;
897 case TK_GE: op = OP_Ge; break;
898 case TK_NE: op = OP_Ne; break;
899 case TK_EQ: op = OP_Eq; break;
900 case TK_LIKE: op = OP_Like; break;
901 case TK_GLOB: op = OP_Glob; break;
902 case TK_ISNULL: op = OP_IsNull; break;
903 case TK_NOTNULL: op = OP_NotNull; break;
904 default: break;
905 }
906 switch( pExpr->op ){
907 case TK_AND: {
908 int d2 = sqliteVdbeMakeLabel(v);
909 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
910 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
911 sqliteVdbeResolveLabel(v, d2);
912 break;
913 }
914 case TK_OR: {
915 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
916 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
917 break;
918 }
919 case TK_NOT: {
920 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
921 break;
922 }
923 case TK_LT:
924 case TK_LE:
925 case TK_GT:
926 case TK_GE:
927 case TK_NE:
928 case TK_EQ:
929 case TK_LIKE:
930 case TK_GLOB: {
931 sqliteExprCode(pParse, pExpr->pLeft);
932 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000933 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000934 break;
935 }
936 case TK_ISNULL:
937 case TK_NOTNULL: {
938 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000939 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000940 break;
941 }
drhfef52082000-06-06 01:50:43 +0000942 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000943 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000944 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000945 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000946 }else{
drh99fcd712001-10-13 01:06:47 +0000947 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000948 }
949 break;
950 }
951 case TK_BETWEEN: {
952 int lbl = sqliteVdbeMakeLabel(v);
953 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000954 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000955 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000956 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +0000957 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000958 sqliteVdbeAddOp(v, OP_Le, 0, dest);
959 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
960 sqliteVdbeResolveLabel(v, lbl);
961 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +0000962 break;
963 }
drhcce7d172000-05-31 15:34:51 +0000964 default: {
965 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000966 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000967 break;
968 }
969 }
970}
971
972/*
drh66b89c82000-11-28 20:47:17 +0000973** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000974** to the label "dest" if the expression is false but execution
975** continues straight thru if the expression is true.
976*/
977void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
978 Vdbe *v = pParse->pVdbe;
979 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000980 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000981 switch( pExpr->op ){
982 case TK_LT: op = OP_Ge; break;
983 case TK_LE: op = OP_Gt; break;
984 case TK_GT: op = OP_Le; break;
985 case TK_GE: op = OP_Lt; break;
986 case TK_NE: op = OP_Eq; break;
987 case TK_EQ: op = OP_Ne; break;
988 case TK_LIKE: op = OP_Like; break;
989 case TK_GLOB: op = OP_Glob; break;
990 case TK_ISNULL: op = OP_NotNull; break;
991 case TK_NOTNULL: op = OP_IsNull; break;
992 default: break;
993 }
994 switch( pExpr->op ){
995 case TK_AND: {
996 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
997 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
998 break;
999 }
1000 case TK_OR: {
1001 int d2 = sqliteVdbeMakeLabel(v);
1002 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1003 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1004 sqliteVdbeResolveLabel(v, d2);
1005 break;
1006 }
1007 case TK_NOT: {
1008 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1009 break;
1010 }
1011 case TK_LT:
1012 case TK_LE:
1013 case TK_GT:
1014 case TK_GE:
1015 case TK_NE:
1016 case TK_EQ: {
1017 sqliteExprCode(pParse, pExpr->pLeft);
1018 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001019 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001020 break;
1021 }
1022 case TK_LIKE:
1023 case TK_GLOB: {
1024 sqliteExprCode(pParse, pExpr->pLeft);
1025 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001026 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001027 break;
1028 }
1029 case TK_ISNULL:
1030 case TK_NOTNULL: {
1031 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001032 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001033 break;
1034 }
drhfef52082000-06-06 01:50:43 +00001035 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001036 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001037 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001038 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001039 }else{
drh99fcd712001-10-13 01:06:47 +00001040 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001041 }
1042 break;
1043 }
1044 case TK_BETWEEN: {
1045 int addr;
1046 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001047 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001048 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1049 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001050 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1051 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1052 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001053 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001054 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001055 break;
1056 }
drhcce7d172000-05-31 15:34:51 +00001057 default: {
1058 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001059 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1060 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001061 break;
1062 }
1063 }
1064}
drh22827922000-06-06 17:27:05 +00001065
1066/*
1067** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1068** if they are identical and return FALSE if they differ in any way.
1069*/
drhd8bc7082000-06-07 23:51:50 +00001070int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001071 int i;
1072 if( pA==0 ){
1073 return pB==0;
1074 }else if( pB==0 ){
1075 return 0;
1076 }
1077 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001078 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1079 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001080 if( pA->pList ){
1081 if( pB->pList==0 ) return 0;
1082 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1083 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001084 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001085 return 0;
1086 }
1087 }
1088 }else if( pB->pList ){
1089 return 0;
1090 }
1091 if( pA->pSelect || pB->pSelect ) return 0;
1092 if( pA->token.z ){
1093 if( pB->token.z==0 ) return 0;
1094 if( pB->token.n!=pA->token.n ) return 0;
1095 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1096 }
1097 return 1;
1098}
1099
1100/*
1101** Add a new element to the pParse->aAgg[] array and return its index.
1102*/
1103static int appendAggInfo(Parse *pParse){
1104 if( (pParse->nAgg & 0x7)==0 ){
1105 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001106 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1107 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001108 return -1;
1109 }
drh6d4abfb2001-10-22 02:58:08 +00001110 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001111 }
1112 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1113 return pParse->nAgg++;
1114}
1115
1116/*
1117** Analyze the given expression looking for aggregate functions and
1118** for variables that need to be added to the pParse->aAgg[] array.
1119** Make additional entries to the pParse->aAgg[] array as necessary.
1120**
1121** This routine should only be called after the expression has been
1122** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1123**
1124** If errors are seen, leave an error message in zErrMsg and return
1125** the number of errors.
1126*/
1127int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1128 int i;
1129 AggExpr *aAgg;
1130 int nErr = 0;
1131
1132 if( pExpr==0 ) return 0;
1133 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001134 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001135 aAgg = pParse->aAgg;
1136 for(i=0; i<pParse->nAgg; i++){
1137 if( aAgg[i].isAgg ) continue;
1138 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001139 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001140 break;
1141 }
1142 }
1143 if( i>=pParse->nAgg ){
1144 i = appendAggInfo(pParse);
1145 if( i<0 ) return 1;
1146 pParse->aAgg[i].isAgg = 0;
1147 pParse->aAgg[i].pExpr = pExpr;
1148 }
drhaaf88722000-06-08 11:25:00 +00001149 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001150 break;
1151 }
1152 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001153 aAgg = pParse->aAgg;
1154 for(i=0; i<pParse->nAgg; i++){
1155 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001156 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001157 break;
1158 }
1159 }
1160 if( i>=pParse->nAgg ){
1161 i = appendAggInfo(pParse);
1162 if( i<0 ) return 1;
1163 pParse->aAgg[i].isAgg = 1;
1164 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001165 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001166 pExpr->token.z, pExpr->token.n,
1167 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001168 }
1169 pExpr->iAgg = i;
1170 break;
1171 }
1172 default: {
1173 if( pExpr->pLeft ){
1174 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1175 }
1176 if( nErr==0 && pExpr->pRight ){
1177 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1178 }
1179 if( nErr==0 && pExpr->pList ){
1180 int n = pExpr->pList->nExpr;
1181 int i;
1182 for(i=0; nErr==0 && i<n; i++){
1183 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1184 }
1185 }
1186 break;
1187 }
1188 }
1189 return nErr;
1190}
drh8e0a2f92002-02-23 23:45:45 +00001191
1192/*
1193** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001194** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001195** function, or return NULL if the function does not exist.
1196**
drh0bce8352002-02-28 00:41:10 +00001197** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001198** structure is created and liked into the "db" structure if a
1199** no matching function previously existed. When createFlag is true
1200** and the nArg parameter is -1, then only a function that accepts
1201** any number of arguments will be returned.
1202**
1203** If createFlag is false and nArg is -1, then the first valid
1204** function found is returned. A function is valid if either xFunc
1205** or xStep is non-zero.
1206*/
drh0bce8352002-02-28 00:41:10 +00001207FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001208 sqlite *db, /* An open database */
1209 const char *zName, /* Name of the function. Not null-terminated */
1210 int nName, /* Number of characters in the name */
1211 int nArg, /* Number of arguments. -1 means any number */
1212 int createFlag /* Create new entry if true and does not otherwise exist */
1213){
drh0bce8352002-02-28 00:41:10 +00001214 FuncDef *pFirst, *p, *pMaybe;
1215 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001216 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001217 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1218 return p;
1219 }
1220 pMaybe = 0;
1221 while( p && p->nArg!=nArg ){
1222 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1223 p = p->pNext;
1224 }
1225 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1226 return 0;
1227 }
1228 if( p==0 && pMaybe ){
1229 assert( createFlag==0 );
1230 return pMaybe;
1231 }
drh89425d52002-02-28 03:04:48 +00001232 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001233 p->nArg = nArg;
1234 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001235 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001236 }
1237 return p;
1238}