blob: 54b7334245e996ce9d70ac658497dea88bd82acb [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**
drhe4697f52002-05-23 02:09:03 +000015** $Id: expr.c,v 1.62 2002/05/23 02:09:04 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]) );
drhe4697f52002-05-23 02:09:03 +0000189 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000190 for(i=0; i<p->nExpr; i++){
191 pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
192 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
193 pNew->a[i].sortOrder = p->a[i].sortOrder;
194 pNew->a[i].isAgg = p->a[i].isAgg;
195 pNew->a[i].done = 0;
196 }
197 return pNew;
198}
199IdList *sqliteIdListDup(IdList *p){
200 IdList *pNew;
201 int i;
202 if( p==0 ) return 0;
203 pNew = sqliteMalloc( sizeof(*pNew) );
204 if( pNew==0 ) return 0;
205 pNew->nId = p->nId;
206 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000207 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000208 for(i=0; i<p->nId; i++){
209 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
210 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
211 pNew->a[i].idx = p->a[i].idx;
212 pNew->a[i].pTab = 0;
213 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
214 }
215 return pNew;
216}
217Select *sqliteSelectDup(Select *p){
218 Select *pNew;
219 if( p==0 ) return 0;
220 pNew = sqliteMalloc( sizeof(*p) );
221 if( pNew==0 ) return 0;
222 pNew->isDistinct = p->isDistinct;
223 pNew->pEList = sqliteExprListDup(p->pEList);
224 pNew->pSrc = sqliteIdListDup(p->pSrc);
225 pNew->pWhere = sqliteExprDup(p->pWhere);
226 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
227 pNew->pHaving = sqliteExprDup(p->pHaving);
228 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
229 pNew->op = p->op;
230 pNew->pPrior = sqliteSelectDup(p->pPrior);
231 pNew->nLimit = p->nLimit;
232 pNew->nOffset = p->nOffset;
233 pNew->zSelect = 0;
234 return pNew;
235}
236
237
238/*
drha76b5df2002-02-23 02:32:10 +0000239** Add a new element to the end of an expression list. If pList is
240** initially NULL, then create a new expression list.
241*/
242ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
243 int i;
244 if( pList==0 ){
245 pList = sqliteMalloc( sizeof(ExprList) );
246 if( pList==0 ){
247 sqliteExprDelete(pExpr);
248 return 0;
249 }
250 }
251 if( (pList->nExpr & 7)==0 ){
252 int n = pList->nExpr + 8;
253 struct ExprList_item *a;
254 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
255 if( a==0 ){
256 sqliteExprDelete(pExpr);
257 return pList;
258 }
259 pList->a = a;
260 }
261 if( pExpr || pName ){
262 i = pList->nExpr++;
263 pList->a[i].pExpr = pExpr;
264 pList->a[i].zName = 0;
265 if( pName ){
266 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
267 sqliteDequote(pList->a[i].zName);
268 }
269 }
270 return pList;
271}
272
273/*
274** Delete an entire expression list.
275*/
276void sqliteExprListDelete(ExprList *pList){
277 int i;
278 if( pList==0 ) return;
279 for(i=0; i<pList->nExpr; i++){
280 sqliteExprDelete(pList->a[i].pExpr);
281 sqliteFree(pList->a[i].zName);
282 }
283 sqliteFree(pList->a);
284 sqliteFree(pList);
285}
286
287/*
drhfef52082000-06-06 01:50:43 +0000288** Walk an expression tree. Return 1 if the expression is constant
289** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000290**
291** For the purposes of this function, a double-quoted string (ex: "abc")
292** is considered a variable but a single-quoted string (ex: 'abc') is
293** a constant.
drhfef52082000-06-06 01:50:43 +0000294*/
drh92086432002-01-22 14:11:29 +0000295int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000296 switch( p->op ){
297 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000298 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000299 case TK_DOT:
300 return 0;
drh23989372002-05-21 13:43:04 +0000301 case TK_STRING:
302 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000303 case TK_INTEGER:
304 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000305 return 1;
drhfef52082000-06-06 01:50:43 +0000306 default: {
drh92086432002-01-22 14:11:29 +0000307 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
308 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000309 if( p->pList ){
310 int i;
311 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000312 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000313 }
314 }
drh92086432002-01-22 14:11:29 +0000315 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000316 }
317 }
drh92086432002-01-22 14:11:29 +0000318 return 0;
drhfef52082000-06-06 01:50:43 +0000319}
320
321/*
drhc4a3c772001-04-04 11:48:57 +0000322** Return TRUE if the given string is a row-id column name.
323*/
324static int sqliteIsRowid(const char *z){
325 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
326 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
327 if( sqliteStrICmp(z, "OID")==0 ) return 1;
328 return 0;
329}
330
331/*
drhcce7d172000-05-31 15:34:51 +0000332** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000333** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000334** index to the table in the table list and a column offset. The
335** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
336** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000337** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000338** VDBE cursor number for a cursor that is pointing into the referenced
339** table. The Expr.iColumn value is changed to the index of the column
340** of the referenced table. The Expr.iColumn value for the special
341** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
342** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000343**
drhfef52082000-06-06 01:50:43 +0000344** We also check for instances of the IN operator. IN comes in two
345** forms:
346**
347** expr IN (exprlist)
348** and
349** expr IN (SELECT ...)
350**
351** The first form is handled by creating a set holding the list
352** of allowed values. The second form causes the SELECT to generate
353** a temporary table.
354**
355** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000356** If it finds any, it generates code to write the value of that select
357** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000358**
drh967e8b72000-06-21 13:59:10 +0000359** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000360** the number of errors seen and leaves an error message on pParse->zErrMsg.
361*/
drha2e00042002-01-22 03:13:42 +0000362int sqliteExprResolveIds(
363 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000364 int base, /* VDBE cursor number for first entry in pTabList */
drha2e00042002-01-22 03:13:42 +0000365 IdList *pTabList, /* List of tables used to resolve column names */
366 ExprList *pEList, /* List of expressions used to resolve "AS" */
367 Expr *pExpr /* The expression to be analyzed. */
368){
drhdaffd0e2001-04-11 14:28:42 +0000369 if( pExpr==0 || pTabList==0 ) return 0;
drh832508b2002-03-02 17:04:07 +0000370 assert( base+pTabList->nId<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000371 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000372 /* Double-quoted strings (ex: "abc") are used as identifiers if
373 ** possible. Otherwise they remain as strings. Single-quoted
374 ** strings (ex: 'abc') are always string literals.
375 */
376 case TK_STRING: {
377 if( pExpr->token.z[0]=='\'' ) break;
378 /* Fall thru into the TK_ID case if this is a double-quoted string */
379 }
drha2e00042002-01-22 03:13:42 +0000380 /* A lone identifier. Try and match it as follows:
381 **
382 ** 1. To the name of a column of one of the tables in pTabList
383 **
384 ** 2. To the right side of an AS keyword in the column list of
385 ** a SELECT statement. (For example, match against 'x' in
386 ** "SELECT a+b AS 'x' FROM t1".)
387 **
388 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
389 */
drhcce7d172000-05-31 15:34:51 +0000390 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000391 int cnt = 0; /* Number of matches */
392 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000393 char *z;
394 assert( pExpr->token.z );
395 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000396 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000397 if( z==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000398 for(i=0; i<pTabList->nId; i++){
399 int j;
400 Table *pTab = pTabList->a[i].pTab;
401 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000402 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000403 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000404 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000405 cnt++;
drh832508b2002-03-02 17:04:07 +0000406 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000407 if( j==pTab->iPKey ){
408 /* Substitute the record number for the INTEGER PRIMARY KEY */
409 pExpr->iColumn = -1;
410 }else{
411 pExpr->iColumn = j;
412 }
drha2e00042002-01-22 03:13:42 +0000413 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000414 }
415 }
416 }
drha2e00042002-01-22 03:13:42 +0000417 if( cnt==0 && pEList!=0 ){
418 int j;
419 for(j=0; j<pEList->nExpr; j++){
420 char *zAs = pEList->a[j].zName;
421 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
422 cnt++;
423 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
424 pExpr->op = TK_AS;
425 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000426 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000427 }
428 }
429 }
drhc4a3c772001-04-04 11:48:57 +0000430 if( cnt==0 && sqliteIsRowid(z) ){
431 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000432 pExpr->iTable = base;
drhc4a3c772001-04-04 11:48:57 +0000433 cnt = 1 + (pTabList->nId>1);
drha2e00042002-01-22 03:13:42 +0000434 pExpr->op = TK_COLUMN;
drhc4a3c772001-04-04 11:48:57 +0000435 }
drhcce7d172000-05-31 15:34:51 +0000436 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000437 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000438 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000439 pExpr->token.z, pExpr->token.n, 0);
440 pParse->nErr++;
441 return 1;
442 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000443 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000444 pExpr->token.z, pExpr->token.n, 0);
445 pParse->nErr++;
446 return 1;
447 }
drhcce7d172000-05-31 15:34:51 +0000448 break;
449 }
450
drh967e8b72000-06-21 13:59:10 +0000451 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000452 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000453 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000454 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000455 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000456 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000457 char *zLeft, *zRight; /* Text of an identifier */
458
459 pLeft = pExpr->pLeft;
460 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000461 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
462 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000463 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
464 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000465 if( zLeft==0 || zRight==0 ){
466 sqliteFree(zLeft);
467 sqliteFree(zRight);
468 return 1;
469 }
drh87c40e82001-07-23 14:33:02 +0000470 sqliteDequote(zLeft);
471 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000472 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000473 for(i=0; i<pTabList->nId; i++){
474 int j;
475 char *zTab;
476 Table *pTab = pTabList->a[i].pTab;
477 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000478 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000479 if( pTabList->a[i].zAlias ){
480 zTab = pTabList->a[i].zAlias;
481 }else{
482 zTab = pTab->zName;
483 }
drh094b2bb2002-03-13 18:54:07 +0000484 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000485 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000486 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000487 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000488 cnt++;
drh832508b2002-03-02 17:04:07 +0000489 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000490 if( j==pTab->iPKey ){
491 /* Substitute the record number for the INTEGER PRIMARY KEY */
492 pExpr->iColumn = -1;
493 }else{
494 pExpr->iColumn = j;
495 }
drhcce7d172000-05-31 15:34:51 +0000496 }
497 }
498 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000499
500 /* If we have not already resolved this *.* expression, then maybe
501 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000502 if( cnt == 0 && pParse->trigStack != 0 ){
503 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000504 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000505 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
506 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000507 cntTab++;
508 t = 1;
509 }
danielk1977f29ce552002-05-19 23:43:12 +0000510 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
511 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000512 cntTab++;
513 t = 1;
514 }
515
danielk1977f29ce552002-05-19 23:43:12 +0000516 if( t ){
517 int j;
518 for(j=0; j < pTriggerStack->pTab->nCol; j++) {
519 if( sqliteStrICmp(pTriggerStack->pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000520 cnt++;
521 pExpr->iColumn = j;
522 }
523 }
danielk1977f29ce552002-05-19 23:43:12 +0000524 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000525 }
526
drhc4a3c772001-04-04 11:48:57 +0000527 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
528 cnt = 1;
529 pExpr->iColumn = -1;
530 }
drhcce7d172000-05-31 15:34:51 +0000531 sqliteFree(zLeft);
532 sqliteFree(zRight);
533 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000534 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000535 pLeft->token.z, pLeft->token.n, ".", 1,
536 pRight->token.z, pRight->token.n, 0);
537 pParse->nErr++;
538 return 1;
539 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000540 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000541 pLeft->token.z, pLeft->token.n, ".", 1,
542 pRight->token.z, pRight->token.n, 0);
543 pParse->nErr++;
544 return 1;
545 }
546 sqliteExprDelete(pLeft);
547 pExpr->pLeft = 0;
548 sqliteExprDelete(pRight);
549 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000550 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000551 break;
552 }
553
drhfef52082000-06-06 01:50:43 +0000554 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000555 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000556 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000557 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000558 return 1;
559 }
drhfef52082000-06-06 01:50:43 +0000560 if( pExpr->pSelect ){
561 /* Case 1: expr IN (SELECT ...)
562 **
563 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000564 ** table. The cursor number of the temporary table has already
565 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000566 */
drh832508b2002-03-02 17:04:07 +0000567 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000568 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000569 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000570 }else if( pExpr->pList ){
571 /* Case 2: expr IN (exprlist)
572 **
573 ** Create a set to put the exprlist values in. The Set id is stored
574 ** in iTable.
575 */
576 int i, iSet;
577 for(i=0; i<pExpr->pList->nExpr; i++){
578 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000579 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000580 sqliteSetString(&pParse->zErrMsg,
581 "right-hand side of IN operator must be constant", 0);
582 pParse->nErr++;
583 return 1;
584 }
drh4794b982000-06-06 13:54:14 +0000585 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
586 return 1;
587 }
drhfef52082000-06-06 01:50:43 +0000588 }
589 iSet = pExpr->iTable = pParse->nSet++;
590 for(i=0; i<pExpr->pList->nExpr; i++){
591 Expr *pE2 = pExpr->pList->a[i].pExpr;
592 switch( pE2->op ){
593 case TK_FLOAT:
594 case TK_INTEGER:
595 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000596 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000597 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000598 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
599 sqliteVdbeDequoteP3(v, addr);
600 break;
601 }
602 default: {
603 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000604 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000605 break;
606 }
607 }
608 }
609 }
drhcfab11b2000-06-06 03:31:22 +0000610 break;
drhfef52082000-06-06 01:50:43 +0000611 }
612
drh19a775c2000-06-05 18:54:46 +0000613 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000614 /* This has to be a scalar SELECT. Generate code to put the
615 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000616 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000617 */
drh967e8b72000-06-21 13:59:10 +0000618 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000619 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000620 return 1;
621 }
622 break;
623 }
624
drhcce7d172000-05-31 15:34:51 +0000625 /* For all else, just recursively walk the tree */
626 default: {
drh4794b982000-06-06 13:54:14 +0000627 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000628 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000629 return 1;
630 }
631 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000632 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000633 return 1;
634 }
635 if( pExpr->pList ){
636 int i;
637 ExprList *pList = pExpr->pList;
638 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000639 Expr *pArg = pList->a[i].pExpr;
640 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000641 return 1;
642 }
643 }
644 }
645 }
646 }
647 return 0;
648}
649
drhcce7d172000-05-31 15:34:51 +0000650/*
651** Error check the functions in an expression. Make sure all
652** function names are recognized and all functions have the correct
653** number of arguments. Leave an error message in pParse->zErrMsg
654** if anything is amiss. Return the number of errors.
655**
656** if pIsAgg is not null and this expression is an aggregate function
657** (like count(*) or max(value)) then write a 1 into *pIsAgg.
658*/
659int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
660 int nErr = 0;
661 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000662 switch( pExpr->op ){
663 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000664 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
665 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000666 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000667 int is_agg = 0;
668 int i;
drh0bce8352002-02-28 00:41:10 +0000669 FuncDef *pDef;
670
drh89425d52002-02-28 03:04:48 +0000671 pDef = sqliteFindFunction(pParse->db,
672 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000673 if( pDef==0 ){
674 pDef = sqliteFindFunction(pParse->db,
675 pExpr->token.z, pExpr->token.n, -1, 0);
676 if( pDef==0 ){
677 no_such_func = 1;
678 }else{
679 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000680 }
drh0bce8352002-02-28 00:41:10 +0000681 }else{
682 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000683 }
drh8e0a2f92002-02-23 23:45:45 +0000684 if( is_agg && !allowAgg ){
685 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
686 pExpr->token.z, pExpr->token.n, "()", 2, 0);
687 pParse->nErr++;
688 nErr++;
689 is_agg = 0;
690 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000691 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
692 pExpr->token.z, pExpr->token.n, 0);
693 pParse->nErr++;
694 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000695 }else if( wrong_num_args ){
696 sqliteSetNString(&pParse->zErrMsg,
697 "wrong number of arguments to function ",-1,
698 pExpr->token.z, pExpr->token.n, "()", 2, 0);
699 pParse->nErr++;
700 nErr++;
drhcce7d172000-05-31 15:34:51 +0000701 }
drh22827922000-06-06 17:27:05 +0000702 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000703 if( is_agg && pIsAgg ) *pIsAgg = 1;
704 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000705 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
706 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000707 }
708 }
709 default: {
710 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000711 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000712 }
713 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000714 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000715 }
drhfef52082000-06-06 01:50:43 +0000716 if( nErr==0 && pExpr->pList ){
717 int n = pExpr->pList->nExpr;
718 int i;
719 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000720 Expr *pE2 = pExpr->pList->a[i].pExpr;
721 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000722 }
723 }
drhcce7d172000-05-31 15:34:51 +0000724 break;
725 }
726 }
727 return nErr;
728}
729
730/*
731** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000732** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000733*/
734void sqliteExprCode(Parse *pParse, Expr *pExpr){
735 Vdbe *v = pParse->pVdbe;
736 int op;
drhdaffd0e2001-04-11 14:28:42 +0000737 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000738 switch( pExpr->op ){
739 case TK_PLUS: op = OP_Add; break;
740 case TK_MINUS: op = OP_Subtract; break;
741 case TK_STAR: op = OP_Multiply; break;
742 case TK_SLASH: op = OP_Divide; break;
743 case TK_AND: op = OP_And; break;
744 case TK_OR: op = OP_Or; break;
745 case TK_LT: op = OP_Lt; break;
746 case TK_LE: op = OP_Le; break;
747 case TK_GT: op = OP_Gt; break;
748 case TK_GE: op = OP_Ge; break;
749 case TK_NE: op = OP_Ne; break;
750 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000751 case TK_ISNULL: op = OP_IsNull; break;
752 case TK_NOTNULL: op = OP_NotNull; break;
753 case TK_NOT: op = OP_Not; break;
754 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000755 case TK_BITAND: op = OP_BitAnd; break;
756 case TK_BITOR: op = OP_BitOr; break;
757 case TK_BITNOT: op = OP_BitNot; break;
758 case TK_LSHIFT: op = OP_ShiftLeft; break;
759 case TK_RSHIFT: op = OP_ShiftRight; break;
760 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000761 default: break;
762 }
763 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000764 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000765 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000766 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000767 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000768 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000769 }else{
drh99fcd712001-10-13 01:06:47 +0000770 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000771 }
drhcce7d172000-05-31 15:34:51 +0000772 break;
773 }
774 case TK_INTEGER: {
drhe6840902002-03-06 03:08:25 +0000775 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
776 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
777 break;
778 }
779 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000780 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000781 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000782 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000783 break;
784 }
drhcce7d172000-05-31 15:34:51 +0000785 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000786 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000787 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000788 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
789 sqliteVdbeDequoteP3(v, addr);
790 break;
791 }
792 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000793 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000794 break;
795 }
796 case TK_AND:
797 case TK_OR:
798 case TK_PLUS:
799 case TK_STAR:
800 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000801 case TK_REM:
802 case TK_BITAND:
803 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000804 case TK_SLASH: {
805 sqliteExprCode(pParse, pExpr->pLeft);
806 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000807 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000808 break;
809 }
drhbf4133c2001-10-13 02:59:08 +0000810 case TK_LSHIFT:
811 case TK_RSHIFT: {
812 sqliteExprCode(pParse, pExpr->pRight);
813 sqliteExprCode(pParse, pExpr->pLeft);
814 sqliteVdbeAddOp(v, op, 0, 0);
815 break;
816 }
drh00400772000-06-16 20:51:26 +0000817 case TK_CONCAT: {
818 sqliteExprCode(pParse, pExpr->pLeft);
819 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000820 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000821 break;
822 }
drhcce7d172000-05-31 15:34:51 +0000823 case TK_LT:
824 case TK_LE:
825 case TK_GT:
826 case TK_GE:
827 case TK_NE:
drh0ac65892002-04-20 14:24:41 +0000828 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000829 int dest;
drh99fcd712001-10-13 01:06:47 +0000830 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000831 sqliteExprCode(pParse, pExpr->pLeft);
832 sqliteExprCode(pParse, pExpr->pRight);
833 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000834 sqliteVdbeAddOp(v, op, 0, dest);
835 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000836 break;
837 }
drhcce7d172000-05-31 15:34:51 +0000838 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000839 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000840 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000841 Token *p = &pExpr->pLeft->token;
842 char *z = sqliteMalloc( p->n + 2 );
843 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +0000844 if( pExpr->pLeft->op==TK_INTEGER ){
845 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
846 }else{
847 sqliteVdbeAddOp(v, OP_String, 0, 0);
848 }
drh99fcd712001-10-13 01:06:47 +0000849 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000850 sqliteFree(z);
851 break;
852 }
drh1ccde152000-06-17 13:12:39 +0000853 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000854 }
drhbf4133c2001-10-13 02:59:08 +0000855 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000856 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000857 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000858 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000859 break;
860 }
861 case TK_ISNULL:
862 case TK_NOTNULL: {
863 int dest;
drh99fcd712001-10-13 01:06:47 +0000864 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000865 sqliteExprCode(pParse, pExpr->pLeft);
866 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000867 sqliteVdbeAddOp(v, op, 0, dest);
868 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000869 break;
870 }
drh22827922000-06-06 17:27:05 +0000871 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000872 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000873 break;
874 }
drhcce7d172000-05-31 15:34:51 +0000875 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000876 int i;
877 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000878 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000879 FuncDef *pDef;
880 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000881 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000882 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000883 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000884 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000885 }
drh89425d52002-02-28 03:04:48 +0000886 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000887 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000888 break;
889 }
drh19a775c2000-06-05 18:54:46 +0000890 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000891 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000892 break;
893 }
drhfef52082000-06-06 01:50:43 +0000894 case TK_IN: {
895 int addr;
drh99fcd712001-10-13 01:06:47 +0000896 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000897 sqliteExprCode(pParse, pExpr->pLeft);
898 addr = sqliteVdbeCurrentAddr(v);
899 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000900 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000901 }else{
drh99fcd712001-10-13 01:06:47 +0000902 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000903 }
drh99fcd712001-10-13 01:06:47 +0000904 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000905 break;
906 }
907 case TK_BETWEEN: {
908 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000909 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000910 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000911 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000912 sqliteVdbeResolveLabel(v, lbl);
913 break;
914 }
drha2e00042002-01-22 03:13:42 +0000915 case TK_AS: {
916 sqliteExprCode(pParse, pExpr->pLeft);
917 break;
918 }
drh17a7f8d2002-03-24 13:13:27 +0000919 case TK_CASE: {
920 int expr_end_label;
921 int next_when_label;
922 int i;
923
924 assert(pExpr->pList);
925 assert((pExpr->pList->nExpr % 2) == 0);
926 assert(pExpr->pList->nExpr > 0);
927 expr_end_label = sqliteVdbeMakeLabel(pParse->pVdbe);
928 if( pExpr->pLeft ){
929 sqliteExprCode(pParse, pExpr->pLeft);
930 }
931 for(i=0; i<pExpr->pList->nExpr; i=i+2){
932 if( i!=0 ){
933 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
934 }
935 next_when_label = sqliteVdbeMakeLabel(pParse->pVdbe);
936 if( pExpr->pLeft ){
937 sqliteVdbeAddOp(pParse->pVdbe, OP_Dup, 0, 1);
938 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
939 sqliteVdbeAddOp(pParse->pVdbe, OP_Ne, 0, next_when_label);
940 }else{
941 sqliteExprIfFalse(pParse, pExpr->pList->a[i].pExpr, next_when_label);
942 }
943 if( pExpr->pLeft ){
944 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
945 }
946 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
947 sqliteVdbeAddOp(pParse->pVdbe, OP_Goto, 0, expr_end_label);
948 }
949 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
950 if( pExpr->pLeft ){
951 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
952 }
953 if( pExpr->pRight ){
954 sqliteExprCode(pParse, pExpr->pRight);
955 }else{
956 sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0);
957 }
958 sqliteVdbeResolveLabel(pParse->pVdbe, expr_end_label);
959 }
960 break;
drhcce7d172000-05-31 15:34:51 +0000961 }
drhcce7d172000-05-31 15:34:51 +0000962}
963
964/*
965** Generate code for a boolean expression such that a jump is made
966** to the label "dest" if the expression is true but execution
967** continues straight thru if the expression is false.
968*/
969void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
970 Vdbe *v = pParse->pVdbe;
971 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000972 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000973 switch( pExpr->op ){
974 case TK_LT: op = OP_Lt; break;
975 case TK_LE: op = OP_Le; break;
976 case TK_GT: op = OP_Gt; break;
977 case TK_GE: op = OP_Ge; break;
978 case TK_NE: op = OP_Ne; break;
979 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000980 case TK_ISNULL: op = OP_IsNull; break;
981 case TK_NOTNULL: op = OP_NotNull; break;
982 default: break;
983 }
984 switch( pExpr->op ){
985 case TK_AND: {
986 int d2 = sqliteVdbeMakeLabel(v);
987 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
988 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
989 sqliteVdbeResolveLabel(v, d2);
990 break;
991 }
992 case TK_OR: {
993 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
994 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
995 break;
996 }
997 case TK_NOT: {
998 sqliteExprIfFalse(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:
drh0ac65892002-04-20 14:24:41 +00001006 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001007 sqliteExprCode(pParse, pExpr->pLeft);
1008 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001009 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001010 break;
1011 }
1012 case TK_ISNULL:
1013 case TK_NOTNULL: {
1014 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001015 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001016 break;
1017 }
drhfef52082000-06-06 01:50:43 +00001018 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001019 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001020 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001021 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001022 }else{
drh99fcd712001-10-13 01:06:47 +00001023 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001024 }
1025 break;
1026 }
1027 case TK_BETWEEN: {
1028 int lbl = sqliteVdbeMakeLabel(v);
1029 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001030 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001031 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +00001032 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +00001033 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001034 sqliteVdbeAddOp(v, OP_Le, 0, dest);
1035 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1036 sqliteVdbeResolveLabel(v, lbl);
1037 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001038 break;
1039 }
drhcce7d172000-05-31 15:34:51 +00001040 default: {
1041 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001042 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001043 break;
1044 }
1045 }
1046}
1047
1048/*
drh66b89c82000-11-28 20:47:17 +00001049** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001050** to the label "dest" if the expression is false but execution
1051** continues straight thru if the expression is true.
1052*/
1053void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
1054 Vdbe *v = pParse->pVdbe;
1055 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001056 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001057 switch( pExpr->op ){
1058 case TK_LT: op = OP_Ge; break;
1059 case TK_LE: op = OP_Gt; break;
1060 case TK_GT: op = OP_Le; break;
1061 case TK_GE: op = OP_Lt; break;
1062 case TK_NE: op = OP_Eq; break;
1063 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001064 case TK_ISNULL: op = OP_NotNull; break;
1065 case TK_NOTNULL: op = OP_IsNull; break;
1066 default: break;
1067 }
1068 switch( pExpr->op ){
1069 case TK_AND: {
1070 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1071 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1072 break;
1073 }
1074 case TK_OR: {
1075 int d2 = sqliteVdbeMakeLabel(v);
1076 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1077 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1078 sqliteVdbeResolveLabel(v, d2);
1079 break;
1080 }
1081 case TK_NOT: {
1082 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1083 break;
1084 }
1085 case TK_LT:
1086 case TK_LE:
1087 case TK_GT:
1088 case TK_GE:
1089 case TK_NE:
1090 case TK_EQ: {
1091 sqliteExprCode(pParse, pExpr->pLeft);
1092 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001093 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001094 break;
1095 }
drhcce7d172000-05-31 15:34:51 +00001096 case TK_ISNULL:
1097 case TK_NOTNULL: {
1098 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001099 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001100 break;
1101 }
drhfef52082000-06-06 01:50:43 +00001102 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001103 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001104 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001105 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001106 }else{
drh99fcd712001-10-13 01:06:47 +00001107 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001108 }
1109 break;
1110 }
1111 case TK_BETWEEN: {
1112 int addr;
1113 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001114 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001115 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1116 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001117 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1118 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1119 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001120 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001121 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001122 break;
1123 }
drhcce7d172000-05-31 15:34:51 +00001124 default: {
1125 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001126 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1127 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001128 break;
1129 }
1130 }
1131}
drh22827922000-06-06 17:27:05 +00001132
1133/*
1134** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1135** if they are identical and return FALSE if they differ in any way.
1136*/
drhd8bc7082000-06-07 23:51:50 +00001137int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001138 int i;
1139 if( pA==0 ){
1140 return pB==0;
1141 }else if( pB==0 ){
1142 return 0;
1143 }
1144 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001145 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1146 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001147 if( pA->pList ){
1148 if( pB->pList==0 ) return 0;
1149 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1150 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001151 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001152 return 0;
1153 }
1154 }
1155 }else if( pB->pList ){
1156 return 0;
1157 }
1158 if( pA->pSelect || pB->pSelect ) return 0;
1159 if( pA->token.z ){
1160 if( pB->token.z==0 ) return 0;
1161 if( pB->token.n!=pA->token.n ) return 0;
1162 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1163 }
1164 return 1;
1165}
1166
1167/*
1168** Add a new element to the pParse->aAgg[] array and return its index.
1169*/
1170static int appendAggInfo(Parse *pParse){
1171 if( (pParse->nAgg & 0x7)==0 ){
1172 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001173 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1174 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001175 return -1;
1176 }
drh6d4abfb2001-10-22 02:58:08 +00001177 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001178 }
1179 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1180 return pParse->nAgg++;
1181}
1182
1183/*
1184** Analyze the given expression looking for aggregate functions and
1185** for variables that need to be added to the pParse->aAgg[] array.
1186** Make additional entries to the pParse->aAgg[] array as necessary.
1187**
1188** This routine should only be called after the expression has been
1189** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1190**
1191** If errors are seen, leave an error message in zErrMsg and return
1192** the number of errors.
1193*/
1194int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1195 int i;
1196 AggExpr *aAgg;
1197 int nErr = 0;
1198
1199 if( pExpr==0 ) return 0;
1200 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001201 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001202 aAgg = pParse->aAgg;
1203 for(i=0; i<pParse->nAgg; i++){
1204 if( aAgg[i].isAgg ) continue;
1205 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001206 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001207 break;
1208 }
1209 }
1210 if( i>=pParse->nAgg ){
1211 i = appendAggInfo(pParse);
1212 if( i<0 ) return 1;
1213 pParse->aAgg[i].isAgg = 0;
1214 pParse->aAgg[i].pExpr = pExpr;
1215 }
drhaaf88722000-06-08 11:25:00 +00001216 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001217 break;
1218 }
1219 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001220 aAgg = pParse->aAgg;
1221 for(i=0; i<pParse->nAgg; i++){
1222 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001223 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001224 break;
1225 }
1226 }
1227 if( i>=pParse->nAgg ){
1228 i = appendAggInfo(pParse);
1229 if( i<0 ) return 1;
1230 pParse->aAgg[i].isAgg = 1;
1231 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001232 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001233 pExpr->token.z, pExpr->token.n,
1234 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001235 }
1236 pExpr->iAgg = i;
1237 break;
1238 }
1239 default: {
1240 if( pExpr->pLeft ){
1241 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1242 }
1243 if( nErr==0 && pExpr->pRight ){
1244 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1245 }
1246 if( nErr==0 && pExpr->pList ){
1247 int n = pExpr->pList->nExpr;
1248 int i;
1249 for(i=0; nErr==0 && i<n; i++){
1250 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1251 }
1252 }
1253 break;
1254 }
1255 }
1256 return nErr;
1257}
drh8e0a2f92002-02-23 23:45:45 +00001258
1259/*
1260** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001261** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001262** function, or return NULL if the function does not exist.
1263**
drh0bce8352002-02-28 00:41:10 +00001264** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001265** structure is created and liked into the "db" structure if a
1266** no matching function previously existed. When createFlag is true
1267** and the nArg parameter is -1, then only a function that accepts
1268** any number of arguments will be returned.
1269**
1270** If createFlag is false and nArg is -1, then the first valid
1271** function found is returned. A function is valid if either xFunc
1272** or xStep is non-zero.
1273*/
drh0bce8352002-02-28 00:41:10 +00001274FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001275 sqlite *db, /* An open database */
1276 const char *zName, /* Name of the function. Not null-terminated */
1277 int nName, /* Number of characters in the name */
1278 int nArg, /* Number of arguments. -1 means any number */
1279 int createFlag /* Create new entry if true and does not otherwise exist */
1280){
drh0bce8352002-02-28 00:41:10 +00001281 FuncDef *pFirst, *p, *pMaybe;
1282 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001283 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001284 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1285 return p;
1286 }
1287 pMaybe = 0;
1288 while( p && p->nArg!=nArg ){
1289 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1290 p = p->pNext;
1291 }
1292 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1293 return 0;
1294 }
1295 if( p==0 && pMaybe ){
1296 assert( createFlag==0 );
1297 return pMaybe;
1298 }
drh89425d52002-02-28 03:04:48 +00001299 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001300 p->nArg = nArg;
1301 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001302 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001303 }
1304 return p;
1305}