blob: eafd09592e2b30779b48a2c6989e8c8d8b63befa [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**
drh23989372002-05-21 13:43:04 +000015** $Id: expr.c,v 1.61 2002/05/21 13:43: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]) );
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.
drh23989372002-05-21 13:43:04 +0000288**
289** For the purposes of this function, a double-quoted string (ex: "abc")
290** is considered a variable but a single-quoted string (ex: 'abc') is
291** a constant.
drhfef52082000-06-06 01:50:43 +0000292*/
drh92086432002-01-22 14:11:29 +0000293int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000294 switch( p->op ){
295 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000296 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000297 case TK_DOT:
298 return 0;
drh23989372002-05-21 13:43:04 +0000299 case TK_STRING:
300 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000301 case TK_INTEGER:
302 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000303 return 1;
drhfef52082000-06-06 01:50:43 +0000304 default: {
drh92086432002-01-22 14:11:29 +0000305 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
306 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000307 if( p->pList ){
308 int i;
309 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000310 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000311 }
312 }
drh92086432002-01-22 14:11:29 +0000313 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000314 }
315 }
drh92086432002-01-22 14:11:29 +0000316 return 0;
drhfef52082000-06-06 01:50:43 +0000317}
318
319/*
drhc4a3c772001-04-04 11:48:57 +0000320** Return TRUE if the given string is a row-id column name.
321*/
322static int sqliteIsRowid(const char *z){
323 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
324 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
325 if( sqliteStrICmp(z, "OID")==0 ) return 1;
326 return 0;
327}
328
329/*
drhcce7d172000-05-31 15:34:51 +0000330** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000331** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000332** index to the table in the table list and a column offset. The
333** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
334** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000335** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000336** VDBE cursor number for a cursor that is pointing into the referenced
337** table. The Expr.iColumn value is changed to the index of the column
338** of the referenced table. The Expr.iColumn value for the special
339** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
340** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000341**
drhfef52082000-06-06 01:50:43 +0000342** We also check for instances of the IN operator. IN comes in two
343** forms:
344**
345** expr IN (exprlist)
346** and
347** expr IN (SELECT ...)
348**
349** The first form is handled by creating a set holding the list
350** of allowed values. The second form causes the SELECT to generate
351** a temporary table.
352**
353** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000354** If it finds any, it generates code to write the value of that select
355** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000356**
drh967e8b72000-06-21 13:59:10 +0000357** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000358** the number of errors seen and leaves an error message on pParse->zErrMsg.
359*/
drha2e00042002-01-22 03:13:42 +0000360int sqliteExprResolveIds(
361 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000362 int base, /* VDBE cursor number for first entry in pTabList */
drha2e00042002-01-22 03:13:42 +0000363 IdList *pTabList, /* List of tables used to resolve column names */
364 ExprList *pEList, /* List of expressions used to resolve "AS" */
365 Expr *pExpr /* The expression to be analyzed. */
366){
drhdaffd0e2001-04-11 14:28:42 +0000367 if( pExpr==0 || pTabList==0 ) return 0;
drh832508b2002-03-02 17:04:07 +0000368 assert( base+pTabList->nId<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000369 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000370 /* Double-quoted strings (ex: "abc") are used as identifiers if
371 ** possible. Otherwise they remain as strings. Single-quoted
372 ** strings (ex: 'abc') are always string literals.
373 */
374 case TK_STRING: {
375 if( pExpr->token.z[0]=='\'' ) break;
376 /* Fall thru into the TK_ID case if this is a double-quoted string */
377 }
drha2e00042002-01-22 03:13:42 +0000378 /* A lone identifier. Try and match it as follows:
379 **
380 ** 1. To the name of a column of one of the tables in pTabList
381 **
382 ** 2. To the right side of an AS keyword in the column list of
383 ** a SELECT statement. (For example, match against 'x' in
384 ** "SELECT a+b AS 'x' FROM t1".)
385 **
386 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
387 */
drhcce7d172000-05-31 15:34:51 +0000388 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000389 int cnt = 0; /* Number of matches */
390 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000391 char *z;
392 assert( pExpr->token.z );
393 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000394 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000395 if( z==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000396 for(i=0; i<pTabList->nId; i++){
397 int j;
398 Table *pTab = pTabList->a[i].pTab;
399 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000400 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000401 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000402 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000403 cnt++;
drh832508b2002-03-02 17:04:07 +0000404 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000405 if( j==pTab->iPKey ){
406 /* Substitute the record number for the INTEGER PRIMARY KEY */
407 pExpr->iColumn = -1;
408 }else{
409 pExpr->iColumn = j;
410 }
drha2e00042002-01-22 03:13:42 +0000411 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000412 }
413 }
414 }
drha2e00042002-01-22 03:13:42 +0000415 if( cnt==0 && pEList!=0 ){
416 int j;
417 for(j=0; j<pEList->nExpr; j++){
418 char *zAs = pEList->a[j].zName;
419 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
420 cnt++;
421 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
422 pExpr->op = TK_AS;
423 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000424 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000425 }
426 }
427 }
drhc4a3c772001-04-04 11:48:57 +0000428 if( cnt==0 && sqliteIsRowid(z) ){
429 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000430 pExpr->iTable = base;
drhc4a3c772001-04-04 11:48:57 +0000431 cnt = 1 + (pTabList->nId>1);
drha2e00042002-01-22 03:13:42 +0000432 pExpr->op = TK_COLUMN;
drhc4a3c772001-04-04 11:48:57 +0000433 }
drhcce7d172000-05-31 15:34:51 +0000434 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000435 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000436 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000437 pExpr->token.z, pExpr->token.n, 0);
438 pParse->nErr++;
439 return 1;
440 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000441 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000442 pExpr->token.z, pExpr->token.n, 0);
443 pParse->nErr++;
444 return 1;
445 }
drhcce7d172000-05-31 15:34:51 +0000446 break;
447 }
448
drh967e8b72000-06-21 13:59:10 +0000449 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000450 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000451 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000452 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000453 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000454 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000455 char *zLeft, *zRight; /* Text of an identifier */
456
457 pLeft = pExpr->pLeft;
458 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000459 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
460 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000461 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
462 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000463 if( zLeft==0 || zRight==0 ){
464 sqliteFree(zLeft);
465 sqliteFree(zRight);
466 return 1;
467 }
drh87c40e82001-07-23 14:33:02 +0000468 sqliteDequote(zLeft);
469 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000470 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000471 for(i=0; i<pTabList->nId; i++){
472 int j;
473 char *zTab;
474 Table *pTab = pTabList->a[i].pTab;
475 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000476 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000477 if( pTabList->a[i].zAlias ){
478 zTab = pTabList->a[i].zAlias;
479 }else{
480 zTab = pTab->zName;
481 }
drh094b2bb2002-03-13 18:54:07 +0000482 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000483 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000484 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000485 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000486 cnt++;
drh832508b2002-03-02 17:04:07 +0000487 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000488 if( j==pTab->iPKey ){
489 /* Substitute the record number for the INTEGER PRIMARY KEY */
490 pExpr->iColumn = -1;
491 }else{
492 pExpr->iColumn = j;
493 }
drhcce7d172000-05-31 15:34:51 +0000494 }
495 }
496 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000497
498 /* If we have not already resolved this *.* expression, then maybe
499 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000500 if( cnt == 0 && pParse->trigStack != 0 ){
501 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000502 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000503 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
504 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000505 cntTab++;
506 t = 1;
507 }
danielk1977f29ce552002-05-19 23:43:12 +0000508 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
509 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000510 cntTab++;
511 t = 1;
512 }
513
danielk1977f29ce552002-05-19 23:43:12 +0000514 if( t ){
515 int j;
516 for(j=0; j < pTriggerStack->pTab->nCol; j++) {
517 if( sqliteStrICmp(pTriggerStack->pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000518 cnt++;
519 pExpr->iColumn = j;
520 }
521 }
danielk1977f29ce552002-05-19 23:43:12 +0000522 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000523 }
524
drhc4a3c772001-04-04 11:48:57 +0000525 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
526 cnt = 1;
527 pExpr->iColumn = -1;
528 }
drhcce7d172000-05-31 15:34:51 +0000529 sqliteFree(zLeft);
530 sqliteFree(zRight);
531 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000532 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000533 pLeft->token.z, pLeft->token.n, ".", 1,
534 pRight->token.z, pRight->token.n, 0);
535 pParse->nErr++;
536 return 1;
537 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000538 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000539 pLeft->token.z, pLeft->token.n, ".", 1,
540 pRight->token.z, pRight->token.n, 0);
541 pParse->nErr++;
542 return 1;
543 }
544 sqliteExprDelete(pLeft);
545 pExpr->pLeft = 0;
546 sqliteExprDelete(pRight);
547 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000548 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000549 break;
550 }
551
drhfef52082000-06-06 01:50:43 +0000552 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000553 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000554 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000555 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000556 return 1;
557 }
drhfef52082000-06-06 01:50:43 +0000558 if( pExpr->pSelect ){
559 /* Case 1: expr IN (SELECT ...)
560 **
561 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000562 ** table. The cursor number of the temporary table has already
563 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000564 */
drh832508b2002-03-02 17:04:07 +0000565 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000566 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000567 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000568 }else if( pExpr->pList ){
569 /* Case 2: expr IN (exprlist)
570 **
571 ** Create a set to put the exprlist values in. The Set id is stored
572 ** in iTable.
573 */
574 int i, iSet;
575 for(i=0; i<pExpr->pList->nExpr; i++){
576 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000577 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000578 sqliteSetString(&pParse->zErrMsg,
579 "right-hand side of IN operator must be constant", 0);
580 pParse->nErr++;
581 return 1;
582 }
drh4794b982000-06-06 13:54:14 +0000583 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
584 return 1;
585 }
drhfef52082000-06-06 01:50:43 +0000586 }
587 iSet = pExpr->iTable = pParse->nSet++;
588 for(i=0; i<pExpr->pList->nExpr; i++){
589 Expr *pE2 = pExpr->pList->a[i].pExpr;
590 switch( pE2->op ){
591 case TK_FLOAT:
592 case TK_INTEGER:
593 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000594 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000595 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000596 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
597 sqliteVdbeDequoteP3(v, addr);
598 break;
599 }
600 default: {
601 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000602 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000603 break;
604 }
605 }
606 }
607 }
drhcfab11b2000-06-06 03:31:22 +0000608 break;
drhfef52082000-06-06 01:50:43 +0000609 }
610
drh19a775c2000-06-05 18:54:46 +0000611 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000612 /* This has to be a scalar SELECT. Generate code to put the
613 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000614 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000615 */
drh967e8b72000-06-21 13:59:10 +0000616 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000617 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000618 return 1;
619 }
620 break;
621 }
622
drhcce7d172000-05-31 15:34:51 +0000623 /* For all else, just recursively walk the tree */
624 default: {
drh4794b982000-06-06 13:54:14 +0000625 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000626 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000627 return 1;
628 }
629 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000630 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000631 return 1;
632 }
633 if( pExpr->pList ){
634 int i;
635 ExprList *pList = pExpr->pList;
636 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000637 Expr *pArg = pList->a[i].pExpr;
638 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000639 return 1;
640 }
641 }
642 }
643 }
644 }
645 return 0;
646}
647
drhcce7d172000-05-31 15:34:51 +0000648/*
649** Error check the functions in an expression. Make sure all
650** function names are recognized and all functions have the correct
651** number of arguments. Leave an error message in pParse->zErrMsg
652** if anything is amiss. Return the number of errors.
653**
654** if pIsAgg is not null and this expression is an aggregate function
655** (like count(*) or max(value)) then write a 1 into *pIsAgg.
656*/
657int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
658 int nErr = 0;
659 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000660 switch( pExpr->op ){
661 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000662 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
663 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000664 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000665 int is_agg = 0;
666 int i;
drh0bce8352002-02-28 00:41:10 +0000667 FuncDef *pDef;
668
drh89425d52002-02-28 03:04:48 +0000669 pDef = sqliteFindFunction(pParse->db,
670 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000671 if( pDef==0 ){
672 pDef = sqliteFindFunction(pParse->db,
673 pExpr->token.z, pExpr->token.n, -1, 0);
674 if( pDef==0 ){
675 no_such_func = 1;
676 }else{
677 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000678 }
drh0bce8352002-02-28 00:41:10 +0000679 }else{
680 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000681 }
drh8e0a2f92002-02-23 23:45:45 +0000682 if( is_agg && !allowAgg ){
683 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
684 pExpr->token.z, pExpr->token.n, "()", 2, 0);
685 pParse->nErr++;
686 nErr++;
687 is_agg = 0;
688 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000689 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
690 pExpr->token.z, pExpr->token.n, 0);
691 pParse->nErr++;
692 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000693 }else if( wrong_num_args ){
694 sqliteSetNString(&pParse->zErrMsg,
695 "wrong number of arguments to function ",-1,
696 pExpr->token.z, pExpr->token.n, "()", 2, 0);
697 pParse->nErr++;
698 nErr++;
drhcce7d172000-05-31 15:34:51 +0000699 }
drh22827922000-06-06 17:27:05 +0000700 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000701 if( is_agg && pIsAgg ) *pIsAgg = 1;
702 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000703 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
704 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000705 }
706 }
707 default: {
708 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000709 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000710 }
711 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000712 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000713 }
drhfef52082000-06-06 01:50:43 +0000714 if( nErr==0 && pExpr->pList ){
715 int n = pExpr->pList->nExpr;
716 int i;
717 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000718 Expr *pE2 = pExpr->pList->a[i].pExpr;
719 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000720 }
721 }
drhcce7d172000-05-31 15:34:51 +0000722 break;
723 }
724 }
725 return nErr;
726}
727
728/*
729** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000730** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000731*/
732void sqliteExprCode(Parse *pParse, Expr *pExpr){
733 Vdbe *v = pParse->pVdbe;
734 int op;
drhdaffd0e2001-04-11 14:28:42 +0000735 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000736 switch( pExpr->op ){
737 case TK_PLUS: op = OP_Add; break;
738 case TK_MINUS: op = OP_Subtract; break;
739 case TK_STAR: op = OP_Multiply; break;
740 case TK_SLASH: op = OP_Divide; break;
741 case TK_AND: op = OP_And; break;
742 case TK_OR: op = OP_Or; break;
743 case TK_LT: op = OP_Lt; break;
744 case TK_LE: op = OP_Le; break;
745 case TK_GT: op = OP_Gt; break;
746 case TK_GE: op = OP_Ge; break;
747 case TK_NE: op = OP_Ne; break;
748 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000749 case TK_ISNULL: op = OP_IsNull; break;
750 case TK_NOTNULL: op = OP_NotNull; break;
751 case TK_NOT: op = OP_Not; break;
752 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000753 case TK_BITAND: op = OP_BitAnd; break;
754 case TK_BITOR: op = OP_BitOr; break;
755 case TK_BITNOT: op = OP_BitNot; break;
756 case TK_LSHIFT: op = OP_ShiftLeft; break;
757 case TK_RSHIFT: op = OP_ShiftRight; break;
758 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000759 default: break;
760 }
761 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000762 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000763 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000764 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000765 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000766 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000767 }else{
drh99fcd712001-10-13 01:06:47 +0000768 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000769 }
drhcce7d172000-05-31 15:34:51 +0000770 break;
771 }
772 case TK_INTEGER: {
drhe6840902002-03-06 03:08:25 +0000773 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
774 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
775 break;
776 }
777 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000778 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000779 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000780 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000781 break;
782 }
drhcce7d172000-05-31 15:34:51 +0000783 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000784 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000785 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000786 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
787 sqliteVdbeDequoteP3(v, addr);
788 break;
789 }
790 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000791 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000792 break;
793 }
794 case TK_AND:
795 case TK_OR:
796 case TK_PLUS:
797 case TK_STAR:
798 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000799 case TK_REM:
800 case TK_BITAND:
801 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000802 case TK_SLASH: {
803 sqliteExprCode(pParse, pExpr->pLeft);
804 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000805 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000806 break;
807 }
drhbf4133c2001-10-13 02:59:08 +0000808 case TK_LSHIFT:
809 case TK_RSHIFT: {
810 sqliteExprCode(pParse, pExpr->pRight);
811 sqliteExprCode(pParse, pExpr->pLeft);
812 sqliteVdbeAddOp(v, op, 0, 0);
813 break;
814 }
drh00400772000-06-16 20:51:26 +0000815 case TK_CONCAT: {
816 sqliteExprCode(pParse, pExpr->pLeft);
817 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000818 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000819 break;
820 }
drhcce7d172000-05-31 15:34:51 +0000821 case TK_LT:
822 case TK_LE:
823 case TK_GT:
824 case TK_GE:
825 case TK_NE:
drh0ac65892002-04-20 14:24:41 +0000826 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000827 int dest;
drh99fcd712001-10-13 01:06:47 +0000828 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000829 sqliteExprCode(pParse, pExpr->pLeft);
830 sqliteExprCode(pParse, pExpr->pRight);
831 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000832 sqliteVdbeAddOp(v, op, 0, dest);
833 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000834 break;
835 }
drhcce7d172000-05-31 15:34:51 +0000836 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000837 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000838 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000839 Token *p = &pExpr->pLeft->token;
840 char *z = sqliteMalloc( p->n + 2 );
841 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +0000842 if( pExpr->pLeft->op==TK_INTEGER ){
843 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
844 }else{
845 sqliteVdbeAddOp(v, OP_String, 0, 0);
846 }
drh99fcd712001-10-13 01:06:47 +0000847 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000848 sqliteFree(z);
849 break;
850 }
drh1ccde152000-06-17 13:12:39 +0000851 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000852 }
drhbf4133c2001-10-13 02:59:08 +0000853 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000854 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000855 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000856 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000857 break;
858 }
859 case TK_ISNULL:
860 case TK_NOTNULL: {
861 int dest;
drh99fcd712001-10-13 01:06:47 +0000862 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000863 sqliteExprCode(pParse, pExpr->pLeft);
864 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000865 sqliteVdbeAddOp(v, op, 0, dest);
866 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000867 break;
868 }
drh22827922000-06-06 17:27:05 +0000869 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000870 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000871 break;
872 }
drhcce7d172000-05-31 15:34:51 +0000873 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000874 int i;
875 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000876 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000877 FuncDef *pDef;
878 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000879 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000880 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000881 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000882 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000883 }
drh89425d52002-02-28 03:04:48 +0000884 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000885 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000886 break;
887 }
drh19a775c2000-06-05 18:54:46 +0000888 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000889 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000890 break;
891 }
drhfef52082000-06-06 01:50:43 +0000892 case TK_IN: {
893 int addr;
drh99fcd712001-10-13 01:06:47 +0000894 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000895 sqliteExprCode(pParse, pExpr->pLeft);
896 addr = sqliteVdbeCurrentAddr(v);
897 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000898 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000899 }else{
drh99fcd712001-10-13 01:06:47 +0000900 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000901 }
drh99fcd712001-10-13 01:06:47 +0000902 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000903 break;
904 }
905 case TK_BETWEEN: {
906 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000907 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000908 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000909 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000910 sqliteVdbeResolveLabel(v, lbl);
911 break;
912 }
drha2e00042002-01-22 03:13:42 +0000913 case TK_AS: {
914 sqliteExprCode(pParse, pExpr->pLeft);
915 break;
916 }
drh17a7f8d2002-03-24 13:13:27 +0000917 case TK_CASE: {
918 int expr_end_label;
919 int next_when_label;
920 int i;
921
922 assert(pExpr->pList);
923 assert((pExpr->pList->nExpr % 2) == 0);
924 assert(pExpr->pList->nExpr > 0);
925 expr_end_label = sqliteVdbeMakeLabel(pParse->pVdbe);
926 if( pExpr->pLeft ){
927 sqliteExprCode(pParse, pExpr->pLeft);
928 }
929 for(i=0; i<pExpr->pList->nExpr; i=i+2){
930 if( i!=0 ){
931 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
932 }
933 next_when_label = sqliteVdbeMakeLabel(pParse->pVdbe);
934 if( pExpr->pLeft ){
935 sqliteVdbeAddOp(pParse->pVdbe, OP_Dup, 0, 1);
936 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
937 sqliteVdbeAddOp(pParse->pVdbe, OP_Ne, 0, next_when_label);
938 }else{
939 sqliteExprIfFalse(pParse, pExpr->pList->a[i].pExpr, next_when_label);
940 }
941 if( pExpr->pLeft ){
942 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
943 }
944 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
945 sqliteVdbeAddOp(pParse->pVdbe, OP_Goto, 0, expr_end_label);
946 }
947 sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
948 if( pExpr->pLeft ){
949 sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
950 }
951 if( pExpr->pRight ){
952 sqliteExprCode(pParse, pExpr->pRight);
953 }else{
954 sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0);
955 }
956 sqliteVdbeResolveLabel(pParse->pVdbe, expr_end_label);
957 }
958 break;
drhcce7d172000-05-31 15:34:51 +0000959 }
drhcce7d172000-05-31 15:34:51 +0000960}
961
962/*
963** Generate code for a boolean expression such that a jump is made
964** to the label "dest" if the expression is true but execution
965** continues straight thru if the expression is false.
966*/
967void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
968 Vdbe *v = pParse->pVdbe;
969 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000970 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000971 switch( pExpr->op ){
972 case TK_LT: op = OP_Lt; break;
973 case TK_LE: op = OP_Le; break;
974 case TK_GT: op = OP_Gt; break;
975 case TK_GE: op = OP_Ge; break;
976 case TK_NE: op = OP_Ne; break;
977 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000978 case TK_ISNULL: op = OP_IsNull; break;
979 case TK_NOTNULL: op = OP_NotNull; break;
980 default: break;
981 }
982 switch( pExpr->op ){
983 case TK_AND: {
984 int d2 = sqliteVdbeMakeLabel(v);
985 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
986 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
987 sqliteVdbeResolveLabel(v, d2);
988 break;
989 }
990 case TK_OR: {
991 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
992 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
993 break;
994 }
995 case TK_NOT: {
996 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
997 break;
998 }
999 case TK_LT:
1000 case TK_LE:
1001 case TK_GT:
1002 case TK_GE:
1003 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001004 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001005 sqliteExprCode(pParse, pExpr->pLeft);
1006 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001007 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001008 break;
1009 }
1010 case TK_ISNULL:
1011 case TK_NOTNULL: {
1012 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001013 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001014 break;
1015 }
drhfef52082000-06-06 01:50:43 +00001016 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001017 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001018 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001019 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001020 }else{
drh99fcd712001-10-13 01:06:47 +00001021 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001022 }
1023 break;
1024 }
1025 case TK_BETWEEN: {
1026 int lbl = sqliteVdbeMakeLabel(v);
1027 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001028 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001029 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +00001030 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +00001031 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001032 sqliteVdbeAddOp(v, OP_Le, 0, dest);
1033 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1034 sqliteVdbeResolveLabel(v, lbl);
1035 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001036 break;
1037 }
drhcce7d172000-05-31 15:34:51 +00001038 default: {
1039 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001040 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001041 break;
1042 }
1043 }
1044}
1045
1046/*
drh66b89c82000-11-28 20:47:17 +00001047** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001048** to the label "dest" if the expression is false but execution
1049** continues straight thru if the expression is true.
1050*/
1051void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
1052 Vdbe *v = pParse->pVdbe;
1053 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001054 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001055 switch( pExpr->op ){
1056 case TK_LT: op = OP_Ge; break;
1057 case TK_LE: op = OP_Gt; break;
1058 case TK_GT: op = OP_Le; break;
1059 case TK_GE: op = OP_Lt; break;
1060 case TK_NE: op = OP_Eq; break;
1061 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001062 case TK_ISNULL: op = OP_NotNull; break;
1063 case TK_NOTNULL: op = OP_IsNull; break;
1064 default: break;
1065 }
1066 switch( pExpr->op ){
1067 case TK_AND: {
1068 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1069 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1070 break;
1071 }
1072 case TK_OR: {
1073 int d2 = sqliteVdbeMakeLabel(v);
1074 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1075 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1076 sqliteVdbeResolveLabel(v, d2);
1077 break;
1078 }
1079 case TK_NOT: {
1080 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1081 break;
1082 }
1083 case TK_LT:
1084 case TK_LE:
1085 case TK_GT:
1086 case TK_GE:
1087 case TK_NE:
1088 case TK_EQ: {
1089 sqliteExprCode(pParse, pExpr->pLeft);
1090 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001091 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001092 break;
1093 }
drhcce7d172000-05-31 15:34:51 +00001094 case TK_ISNULL:
1095 case TK_NOTNULL: {
1096 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001097 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001098 break;
1099 }
drhfef52082000-06-06 01:50:43 +00001100 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001101 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001102 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001103 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001104 }else{
drh99fcd712001-10-13 01:06:47 +00001105 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001106 }
1107 break;
1108 }
1109 case TK_BETWEEN: {
1110 int addr;
1111 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001112 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001113 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1114 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001115 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1116 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1117 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001118 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001119 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001120 break;
1121 }
drhcce7d172000-05-31 15:34:51 +00001122 default: {
1123 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001124 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1125 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001126 break;
1127 }
1128 }
1129}
drh22827922000-06-06 17:27:05 +00001130
1131/*
1132** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1133** if they are identical and return FALSE if they differ in any way.
1134*/
drhd8bc7082000-06-07 23:51:50 +00001135int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001136 int i;
1137 if( pA==0 ){
1138 return pB==0;
1139 }else if( pB==0 ){
1140 return 0;
1141 }
1142 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001143 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1144 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001145 if( pA->pList ){
1146 if( pB->pList==0 ) return 0;
1147 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1148 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001149 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001150 return 0;
1151 }
1152 }
1153 }else if( pB->pList ){
1154 return 0;
1155 }
1156 if( pA->pSelect || pB->pSelect ) return 0;
1157 if( pA->token.z ){
1158 if( pB->token.z==0 ) return 0;
1159 if( pB->token.n!=pA->token.n ) return 0;
1160 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1161 }
1162 return 1;
1163}
1164
1165/*
1166** Add a new element to the pParse->aAgg[] array and return its index.
1167*/
1168static int appendAggInfo(Parse *pParse){
1169 if( (pParse->nAgg & 0x7)==0 ){
1170 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001171 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1172 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001173 return -1;
1174 }
drh6d4abfb2001-10-22 02:58:08 +00001175 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001176 }
1177 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1178 return pParse->nAgg++;
1179}
1180
1181/*
1182** Analyze the given expression looking for aggregate functions and
1183** for variables that need to be added to the pParse->aAgg[] array.
1184** Make additional entries to the pParse->aAgg[] array as necessary.
1185**
1186** This routine should only be called after the expression has been
1187** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1188**
1189** If errors are seen, leave an error message in zErrMsg and return
1190** the number of errors.
1191*/
1192int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1193 int i;
1194 AggExpr *aAgg;
1195 int nErr = 0;
1196
1197 if( pExpr==0 ) return 0;
1198 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001199 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001200 aAgg = pParse->aAgg;
1201 for(i=0; i<pParse->nAgg; i++){
1202 if( aAgg[i].isAgg ) continue;
1203 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001204 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001205 break;
1206 }
1207 }
1208 if( i>=pParse->nAgg ){
1209 i = appendAggInfo(pParse);
1210 if( i<0 ) return 1;
1211 pParse->aAgg[i].isAgg = 0;
1212 pParse->aAgg[i].pExpr = pExpr;
1213 }
drhaaf88722000-06-08 11:25:00 +00001214 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001215 break;
1216 }
1217 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001218 aAgg = pParse->aAgg;
1219 for(i=0; i<pParse->nAgg; i++){
1220 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001221 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001222 break;
1223 }
1224 }
1225 if( i>=pParse->nAgg ){
1226 i = appendAggInfo(pParse);
1227 if( i<0 ) return 1;
1228 pParse->aAgg[i].isAgg = 1;
1229 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001230 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001231 pExpr->token.z, pExpr->token.n,
1232 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001233 }
1234 pExpr->iAgg = i;
1235 break;
1236 }
1237 default: {
1238 if( pExpr->pLeft ){
1239 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1240 }
1241 if( nErr==0 && pExpr->pRight ){
1242 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1243 }
1244 if( nErr==0 && pExpr->pList ){
1245 int n = pExpr->pList->nExpr;
1246 int i;
1247 for(i=0; nErr==0 && i<n; i++){
1248 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1249 }
1250 }
1251 break;
1252 }
1253 }
1254 return nErr;
1255}
drh8e0a2f92002-02-23 23:45:45 +00001256
1257/*
1258** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001259** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001260** function, or return NULL if the function does not exist.
1261**
drh0bce8352002-02-28 00:41:10 +00001262** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001263** structure is created and liked into the "db" structure if a
1264** no matching function previously existed. When createFlag is true
1265** and the nArg parameter is -1, then only a function that accepts
1266** any number of arguments will be returned.
1267**
1268** If createFlag is false and nArg is -1, then the first valid
1269** function found is returned. A function is valid if either xFunc
1270** or xStep is non-zero.
1271*/
drh0bce8352002-02-28 00:41:10 +00001272FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001273 sqlite *db, /* An open database */
1274 const char *zName, /* Name of the function. Not null-terminated */
1275 int nName, /* Number of characters in the name */
1276 int nArg, /* Number of arguments. -1 means any number */
1277 int createFlag /* Create new entry if true and does not otherwise exist */
1278){
drh0bce8352002-02-28 00:41:10 +00001279 FuncDef *pFirst, *p, *pMaybe;
1280 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001281 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001282 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1283 return p;
1284 }
1285 pMaybe = 0;
1286 while( p && p->nArg!=nArg ){
1287 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1288 p = p->pNext;
1289 }
1290 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1291 return 0;
1292 }
1293 if( p==0 && pMaybe ){
1294 assert( createFlag==0 );
1295 return pMaybe;
1296 }
drh89425d52002-02-28 03:04:48 +00001297 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001298 p->nArg = nArg;
1299 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001300 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001301 }
1302 return p;
1303}