blob: b5bb89c99e016acff857a4c236ffdd4b7d951954 [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**
drhe4de1fe2002-06-02 16:09:01 +000015** $Id: expr.c,v 1.67 2002/06/02 16:09:02 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**
drhad3cab52002-05-24 02:04:32 +0000158** The expression list, ID, and source lists return by sqliteExprListDup(),
159** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
160** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000161**
drhad3cab52002-05-24 02:04:32 +0000162** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000163*/
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}
drhad3cab52002-05-24 02:04:32 +0000199SrcList *sqliteSrcListDup(SrcList *p){
200 SrcList *pNew;
201 int i;
202 if( p==0 ) return 0;
203 pNew = sqliteMalloc( sizeof(*pNew) );
204 if( pNew==0 ) return 0;
205 pNew->nSrc = p->nSrc;
206 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
207 if( pNew->a==0 ) return 0;
208 for(i=0; i<p->nSrc; 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].jointype = p->a[i].jointype;
212 pNew->a[i].pTab = 0;
213 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
214 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
215 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
216 }
217 return pNew;
218}
drhff78bd22002-02-27 01:47:11 +0000219IdList *sqliteIdListDup(IdList *p){
220 IdList *pNew;
221 int i;
222 if( p==0 ) return 0;
223 pNew = sqliteMalloc( sizeof(*pNew) );
224 if( pNew==0 ) return 0;
225 pNew->nId = p->nId;
226 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000227 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000228 for(i=0; i<p->nId; i++){
229 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000230 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000231 }
232 return pNew;
233}
234Select *sqliteSelectDup(Select *p){
235 Select *pNew;
236 if( p==0 ) return 0;
237 pNew = sqliteMalloc( sizeof(*p) );
238 if( pNew==0 ) return 0;
239 pNew->isDistinct = p->isDistinct;
240 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000241 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000242 pNew->pWhere = sqliteExprDup(p->pWhere);
243 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
244 pNew->pHaving = sqliteExprDup(p->pHaving);
245 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
246 pNew->op = p->op;
247 pNew->pPrior = sqliteSelectDup(p->pPrior);
248 pNew->nLimit = p->nLimit;
249 pNew->nOffset = p->nOffset;
250 pNew->zSelect = 0;
251 return pNew;
252}
253
254
255/*
drha76b5df2002-02-23 02:32:10 +0000256** Add a new element to the end of an expression list. If pList is
257** initially NULL, then create a new expression list.
258*/
259ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
260 int i;
261 if( pList==0 ){
262 pList = sqliteMalloc( sizeof(ExprList) );
263 if( pList==0 ){
264 sqliteExprDelete(pExpr);
265 return 0;
266 }
267 }
268 if( (pList->nExpr & 7)==0 ){
269 int n = pList->nExpr + 8;
270 struct ExprList_item *a;
271 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
272 if( a==0 ){
273 sqliteExprDelete(pExpr);
274 return pList;
275 }
276 pList->a = a;
277 }
278 if( pExpr || pName ){
279 i = pList->nExpr++;
280 pList->a[i].pExpr = pExpr;
281 pList->a[i].zName = 0;
282 if( pName ){
283 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
284 sqliteDequote(pList->a[i].zName);
285 }
286 }
287 return pList;
288}
289
290/*
291** Delete an entire expression list.
292*/
293void sqliteExprListDelete(ExprList *pList){
294 int i;
295 if( pList==0 ) return;
296 for(i=0; i<pList->nExpr; i++){
297 sqliteExprDelete(pList->a[i].pExpr);
298 sqliteFree(pList->a[i].zName);
299 }
300 sqliteFree(pList->a);
301 sqliteFree(pList);
302}
303
304/*
drhfef52082000-06-06 01:50:43 +0000305** Walk an expression tree. Return 1 if the expression is constant
306** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000307**
308** For the purposes of this function, a double-quoted string (ex: "abc")
309** is considered a variable but a single-quoted string (ex: 'abc') is
310** a constant.
drhfef52082000-06-06 01:50:43 +0000311*/
drh92086432002-01-22 14:11:29 +0000312int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000313 switch( p->op ){
314 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000315 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000316 case TK_DOT:
317 return 0;
drh23989372002-05-21 13:43:04 +0000318 case TK_STRING:
319 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000320 case TK_INTEGER:
321 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000322 return 1;
drhfef52082000-06-06 01:50:43 +0000323 default: {
drh92086432002-01-22 14:11:29 +0000324 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
325 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000326 if( p->pList ){
327 int i;
328 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000329 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000330 }
331 }
drh92086432002-01-22 14:11:29 +0000332 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000333 }
334 }
drh92086432002-01-22 14:11:29 +0000335 return 0;
drhfef52082000-06-06 01:50:43 +0000336}
337
338/*
drhe4de1fe2002-06-02 16:09:01 +0000339** If the given expression codes a constant integer, return 1 and put
340** the value of the integer in *pValue. If the expression is not an
341** integer, return 0 and leave *pValue unchanged.
342*/
343int sqliteExprIsInteger(Expr *p, int *pValue){
344 switch( p->op ){
345 case TK_INTEGER: {
346 *pValue = atoi(p->token.z);
347 return 1;
348 }
349 case TK_STRING: {
350 char *z = p->token.z;
351 int n = p->token.n;
352 if( n>0 && z=='-' ){ z++; n--; }
353 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
354 if( n==0 ){
355 *pValue = atoi(p->token.z);
356 return 1;
357 }
358 break;
359 }
360 case TK_UMINUS: {
361 int v;
362 if( sqliteExprIsInteger(p->pLeft, &v) ){
363 *pValue = -v;
364 return 1;
365 }
366 break;
367 }
368 default: break;
369 }
370 return 0;
371}
372
373/*
drhc4a3c772001-04-04 11:48:57 +0000374** Return TRUE if the given string is a row-id column name.
375*/
376static int sqliteIsRowid(const char *z){
377 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
378 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
379 if( sqliteStrICmp(z, "OID")==0 ) return 1;
380 return 0;
381}
382
383/*
drhcce7d172000-05-31 15:34:51 +0000384** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000385** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000386** index to the table in the table list and a column offset. The
387** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
388** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000389** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000390** VDBE cursor number for a cursor that is pointing into the referenced
391** table. The Expr.iColumn value is changed to the index of the column
392** of the referenced table. The Expr.iColumn value for the special
393** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
394** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000395**
drhfef52082000-06-06 01:50:43 +0000396** We also check for instances of the IN operator. IN comes in two
397** forms:
398**
399** expr IN (exprlist)
400** and
401** expr IN (SELECT ...)
402**
403** The first form is handled by creating a set holding the list
404** of allowed values. The second form causes the SELECT to generate
405** a temporary table.
406**
407** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000408** If it finds any, it generates code to write the value of that select
409** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000410**
drh967e8b72000-06-21 13:59:10 +0000411** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000412** the number of errors seen and leaves an error message on pParse->zErrMsg.
413*/
drha2e00042002-01-22 03:13:42 +0000414int sqliteExprResolveIds(
415 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000416 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000417 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000418 ExprList *pEList, /* List of expressions used to resolve "AS" */
419 Expr *pExpr /* The expression to be analyzed. */
420){
drhdaffd0e2001-04-11 14:28:42 +0000421 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000422 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000423 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000424 /* Double-quoted strings (ex: "abc") are used as identifiers if
425 ** possible. Otherwise they remain as strings. Single-quoted
426 ** strings (ex: 'abc') are always string literals.
427 */
428 case TK_STRING: {
429 if( pExpr->token.z[0]=='\'' ) break;
430 /* Fall thru into the TK_ID case if this is a double-quoted string */
431 }
drha2e00042002-01-22 03:13:42 +0000432 /* A lone identifier. Try and match it as follows:
433 **
434 ** 1. To the name of a column of one of the tables in pTabList
435 **
436 ** 2. To the right side of an AS keyword in the column list of
437 ** a SELECT statement. (For example, match against 'x' in
438 ** "SELECT a+b AS 'x' FROM t1".)
439 **
440 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
441 */
drhcce7d172000-05-31 15:34:51 +0000442 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000443 int cnt = 0; /* Number of matches */
444 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000445 char *z;
446 assert( pExpr->token.z );
447 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000448 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000449 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000450 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000451 int j;
452 Table *pTab = pTabList->a[i].pTab;
453 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000454 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000455 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000456 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000457 cnt++;
drh832508b2002-03-02 17:04:07 +0000458 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000459 if( j==pTab->iPKey ){
460 /* Substitute the record number for the INTEGER PRIMARY KEY */
461 pExpr->iColumn = -1;
462 }else{
463 pExpr->iColumn = j;
464 }
drha2e00042002-01-22 03:13:42 +0000465 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000466 }
467 }
468 }
drha2e00042002-01-22 03:13:42 +0000469 if( cnt==0 && pEList!=0 ){
470 int j;
471 for(j=0; j<pEList->nExpr; j++){
472 char *zAs = pEList->a[j].zName;
473 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
474 cnt++;
475 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
476 pExpr->op = TK_AS;
477 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000478 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000479 }
480 }
481 }
drhc4a3c772001-04-04 11:48:57 +0000482 if( cnt==0 && sqliteIsRowid(z) ){
483 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000484 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000485 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000486 pExpr->op = TK_COLUMN;
drhc4a3c772001-04-04 11:48:57 +0000487 }
drhcce7d172000-05-31 15:34:51 +0000488 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000489 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000490 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000491 pExpr->token.z, pExpr->token.n, 0);
492 pParse->nErr++;
493 return 1;
494 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000495 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000496 pExpr->token.z, pExpr->token.n, 0);
497 pParse->nErr++;
498 return 1;
499 }
drhcce7d172000-05-31 15:34:51 +0000500 break;
501 }
502
drh967e8b72000-06-21 13:59:10 +0000503 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000504 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000505 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000506 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000507 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000508 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000509 char *zLeft, *zRight; /* Text of an identifier */
510
511 pLeft = pExpr->pLeft;
512 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000513 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
514 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000515 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
516 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000517 if( zLeft==0 || zRight==0 ){
518 sqliteFree(zLeft);
519 sqliteFree(zRight);
520 return 1;
521 }
drh87c40e82001-07-23 14:33:02 +0000522 sqliteDequote(zLeft);
523 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000524 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000525 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000526 int j;
527 char *zTab;
528 Table *pTab = pTabList->a[i].pTab;
529 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000530 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000531 if( pTabList->a[i].zAlias ){
532 zTab = pTabList->a[i].zAlias;
533 }else{
534 zTab = pTab->zName;
535 }
drh094b2bb2002-03-13 18:54:07 +0000536 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000537 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000538 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000539 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000540 cnt++;
drh832508b2002-03-02 17:04:07 +0000541 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000542 if( j==pTab->iPKey ){
543 /* Substitute the record number for the INTEGER PRIMARY KEY */
544 pExpr->iColumn = -1;
545 }else{
546 pExpr->iColumn = j;
547 }
drhcce7d172000-05-31 15:34:51 +0000548 }
549 }
550 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000551
552 /* If we have not already resolved this *.* expression, then maybe
553 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000554 if( cnt == 0 && pParse->trigStack != 0 ){
555 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000556 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000557 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
558 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000559 cntTab++;
560 t = 1;
561 }
danielk1977f29ce552002-05-19 23:43:12 +0000562 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
563 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000564 cntTab++;
565 t = 1;
566 }
567
danielk1977f29ce552002-05-19 23:43:12 +0000568 if( t ){
569 int j;
570 for(j=0; j < pTriggerStack->pTab->nCol; j++) {
571 if( sqliteStrICmp(pTriggerStack->pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000572 cnt++;
573 pExpr->iColumn = j;
574 }
575 }
danielk1977f29ce552002-05-19 23:43:12 +0000576 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }
578
drhc4a3c772001-04-04 11:48:57 +0000579 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
580 cnt = 1;
581 pExpr->iColumn = -1;
582 }
drhcce7d172000-05-31 15:34:51 +0000583 sqliteFree(zLeft);
584 sqliteFree(zRight);
585 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000586 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000587 pLeft->token.z, pLeft->token.n, ".", 1,
588 pRight->token.z, pRight->token.n, 0);
589 pParse->nErr++;
590 return 1;
591 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000592 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000593 pLeft->token.z, pLeft->token.n, ".", 1,
594 pRight->token.z, pRight->token.n, 0);
595 pParse->nErr++;
596 return 1;
597 }
598 sqliteExprDelete(pLeft);
599 pExpr->pLeft = 0;
600 sqliteExprDelete(pRight);
601 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000602 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000603 break;
604 }
605
drhfef52082000-06-06 01:50:43 +0000606 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000607 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000608 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000609 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000610 return 1;
611 }
drhfef52082000-06-06 01:50:43 +0000612 if( pExpr->pSelect ){
613 /* Case 1: expr IN (SELECT ...)
614 **
615 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000616 ** table. The cursor number of the temporary table has already
617 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000618 */
drh832508b2002-03-02 17:04:07 +0000619 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000620 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000621 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000622 }else if( pExpr->pList ){
623 /* Case 2: expr IN (exprlist)
624 **
625 ** Create a set to put the exprlist values in. The Set id is stored
626 ** in iTable.
627 */
628 int i, iSet;
629 for(i=0; i<pExpr->pList->nExpr; i++){
630 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000631 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000632 sqliteSetString(&pParse->zErrMsg,
633 "right-hand side of IN operator must be constant", 0);
634 pParse->nErr++;
635 return 1;
636 }
drh4794b982000-06-06 13:54:14 +0000637 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
638 return 1;
639 }
drhfef52082000-06-06 01:50:43 +0000640 }
641 iSet = pExpr->iTable = pParse->nSet++;
642 for(i=0; i<pExpr->pList->nExpr; i++){
643 Expr *pE2 = pExpr->pList->a[i].pExpr;
644 switch( pE2->op ){
645 case TK_FLOAT:
646 case TK_INTEGER:
647 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000648 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000649 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000650 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
651 sqliteVdbeDequoteP3(v, addr);
652 break;
653 }
654 default: {
655 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000656 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000657 break;
658 }
659 }
660 }
661 }
drhcfab11b2000-06-06 03:31:22 +0000662 break;
drhfef52082000-06-06 01:50:43 +0000663 }
664
drh19a775c2000-06-05 18:54:46 +0000665 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000666 /* This has to be a scalar SELECT. Generate code to put the
667 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000668 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000669 */
drh967e8b72000-06-21 13:59:10 +0000670 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000671 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000672 return 1;
673 }
674 break;
675 }
676
drhcce7d172000-05-31 15:34:51 +0000677 /* For all else, just recursively walk the tree */
678 default: {
drh4794b982000-06-06 13:54:14 +0000679 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000680 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000681 return 1;
682 }
683 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000684 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000685 return 1;
686 }
687 if( pExpr->pList ){
688 int i;
689 ExprList *pList = pExpr->pList;
690 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000691 Expr *pArg = pList->a[i].pExpr;
692 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000693 return 1;
694 }
695 }
696 }
697 }
698 }
699 return 0;
700}
701
drhcce7d172000-05-31 15:34:51 +0000702/*
703** Error check the functions in an expression. Make sure all
704** function names are recognized and all functions have the correct
705** number of arguments. Leave an error message in pParse->zErrMsg
706** if anything is amiss. Return the number of errors.
707**
708** if pIsAgg is not null and this expression is an aggregate function
709** (like count(*) or max(value)) then write a 1 into *pIsAgg.
710*/
711int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
712 int nErr = 0;
713 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000714 switch( pExpr->op ){
715 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000716 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
717 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000718 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000719 int is_agg = 0;
720 int i;
drh0bce8352002-02-28 00:41:10 +0000721 FuncDef *pDef;
722
drh89425d52002-02-28 03:04:48 +0000723 pDef = sqliteFindFunction(pParse->db,
724 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000725 if( pDef==0 ){
726 pDef = sqliteFindFunction(pParse->db,
727 pExpr->token.z, pExpr->token.n, -1, 0);
728 if( pDef==0 ){
729 no_such_func = 1;
730 }else{
731 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000732 }
drh0bce8352002-02-28 00:41:10 +0000733 }else{
734 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000735 }
drh8e0a2f92002-02-23 23:45:45 +0000736 if( is_agg && !allowAgg ){
737 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
738 pExpr->token.z, pExpr->token.n, "()", 2, 0);
739 pParse->nErr++;
740 nErr++;
741 is_agg = 0;
742 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000743 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
744 pExpr->token.z, pExpr->token.n, 0);
745 pParse->nErr++;
746 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000747 }else if( wrong_num_args ){
748 sqliteSetNString(&pParse->zErrMsg,
749 "wrong number of arguments to function ",-1,
750 pExpr->token.z, pExpr->token.n, "()", 2, 0);
751 pParse->nErr++;
752 nErr++;
drhcce7d172000-05-31 15:34:51 +0000753 }
drh22827922000-06-06 17:27:05 +0000754 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000755 if( is_agg && pIsAgg ) *pIsAgg = 1;
756 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000757 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
758 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000759 }
760 }
761 default: {
762 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000763 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000764 }
765 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000766 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000767 }
drhfef52082000-06-06 01:50:43 +0000768 if( nErr==0 && pExpr->pList ){
769 int n = pExpr->pList->nExpr;
770 int i;
771 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000772 Expr *pE2 = pExpr->pList->a[i].pExpr;
773 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000774 }
775 }
drhcce7d172000-05-31 15:34:51 +0000776 break;
777 }
778 }
779 return nErr;
780}
781
782/*
783** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000784** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000785*/
786void sqliteExprCode(Parse *pParse, Expr *pExpr){
787 Vdbe *v = pParse->pVdbe;
788 int op;
drhdaffd0e2001-04-11 14:28:42 +0000789 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000790 switch( pExpr->op ){
791 case TK_PLUS: op = OP_Add; break;
792 case TK_MINUS: op = OP_Subtract; break;
793 case TK_STAR: op = OP_Multiply; break;
794 case TK_SLASH: op = OP_Divide; break;
795 case TK_AND: op = OP_And; break;
796 case TK_OR: op = OP_Or; break;
797 case TK_LT: op = OP_Lt; break;
798 case TK_LE: op = OP_Le; break;
799 case TK_GT: op = OP_Gt; break;
800 case TK_GE: op = OP_Ge; break;
801 case TK_NE: op = OP_Ne; break;
802 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000803 case TK_ISNULL: op = OP_IsNull; break;
804 case TK_NOTNULL: op = OP_NotNull; break;
805 case TK_NOT: op = OP_Not; break;
806 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000807 case TK_BITAND: op = OP_BitAnd; break;
808 case TK_BITOR: op = OP_BitOr; break;
809 case TK_BITNOT: op = OP_BitNot; break;
810 case TK_LSHIFT: op = OP_ShiftLeft; break;
811 case TK_RSHIFT: op = OP_ShiftRight; break;
812 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000813 default: break;
814 }
815 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000816 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000817 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000818 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000819 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000820 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000821 }else{
drh99fcd712001-10-13 01:06:47 +0000822 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000823 }
drhcce7d172000-05-31 15:34:51 +0000824 break;
825 }
826 case TK_INTEGER: {
drhe6840902002-03-06 03:08:25 +0000827 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
828 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
829 break;
830 }
831 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000832 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000833 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000834 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000835 break;
836 }
drhcce7d172000-05-31 15:34:51 +0000837 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000838 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000839 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000840 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
841 sqliteVdbeDequoteP3(v, addr);
842 break;
843 }
844 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000845 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000846 break;
847 }
848 case TK_AND:
849 case TK_OR:
850 case TK_PLUS:
851 case TK_STAR:
852 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000853 case TK_REM:
854 case TK_BITAND:
855 case TK_BITOR:
drhf5905aa2002-05-26 20:54:33 +0000856 case TK_SLASH:
857 case TK_LT:
858 case TK_LE:
859 case TK_GT:
860 case TK_GE:
861 case TK_NE:
862 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +0000863 sqliteExprCode(pParse, pExpr->pLeft);
864 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000865 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000866 break;
867 }
drhbf4133c2001-10-13 02:59:08 +0000868 case TK_LSHIFT:
869 case TK_RSHIFT: {
870 sqliteExprCode(pParse, pExpr->pRight);
871 sqliteExprCode(pParse, pExpr->pLeft);
872 sqliteVdbeAddOp(v, op, 0, 0);
873 break;
874 }
drh00400772000-06-16 20:51:26 +0000875 case TK_CONCAT: {
876 sqliteExprCode(pParse, pExpr->pLeft);
877 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000878 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000879 break;
880 }
drhcce7d172000-05-31 15:34:51 +0000881 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000882 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000883 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000884 Token *p = &pExpr->pLeft->token;
885 char *z = sqliteMalloc( p->n + 2 );
886 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +0000887 if( pExpr->pLeft->op==TK_INTEGER ){
888 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
889 }else{
890 sqliteVdbeAddOp(v, OP_String, 0, 0);
891 }
drh99fcd712001-10-13 01:06:47 +0000892 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000893 sqliteFree(z);
894 break;
895 }
drh1ccde152000-06-17 13:12:39 +0000896 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000897 }
drhbf4133c2001-10-13 02:59:08 +0000898 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000899 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000900 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000901 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000902 break;
903 }
904 case TK_ISNULL:
905 case TK_NOTNULL: {
906 int dest;
drh99fcd712001-10-13 01:06:47 +0000907 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000908 sqliteExprCode(pParse, pExpr->pLeft);
909 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +0000910 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +0000911 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000912 break;
913 }
drh22827922000-06-06 17:27:05 +0000914 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000915 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000916 break;
917 }
drhcce7d172000-05-31 15:34:51 +0000918 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000919 int i;
920 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000921 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000922 FuncDef *pDef;
923 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000924 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000925 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000926 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000927 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000928 }
drh89425d52002-02-28 03:04:48 +0000929 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000930 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000931 break;
932 }
drh19a775c2000-06-05 18:54:46 +0000933 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000934 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000935 break;
936 }
drhfef52082000-06-06 01:50:43 +0000937 case TK_IN: {
938 int addr;
drh99fcd712001-10-13 01:06:47 +0000939 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000940 sqliteExprCode(pParse, pExpr->pLeft);
941 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +0000942 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
943 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
944 sqliteVdbeAddOp(v, OP_String, 0, 0);
945 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +0000946 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +0000947 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +0000948 }else{
drhf5905aa2002-05-26 20:54:33 +0000949 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +0000950 }
drh99fcd712001-10-13 01:06:47 +0000951 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000952 break;
953 }
954 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +0000955 sqliteExprCode(pParse, pExpr->pLeft);
956 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
957 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
958 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
959 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
960 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
961 sqliteVdbeAddOp(v, OP_Le, 0, 0);
962 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +0000963 break;
964 }
drha2e00042002-01-22 03:13:42 +0000965 case TK_AS: {
966 sqliteExprCode(pParse, pExpr->pLeft);
967 break;
968 }
drh17a7f8d2002-03-24 13:13:27 +0000969 case TK_CASE: {
970 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +0000971 int jumpInst;
972 int addr;
973 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +0000974 int i;
975
976 assert(pExpr->pList);
977 assert((pExpr->pList->nExpr % 2) == 0);
978 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +0000979 nExpr = pExpr->pList->nExpr;
980 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +0000981 if( pExpr->pLeft ){
982 sqliteExprCode(pParse, pExpr->pLeft);
983 }
drhf5905aa2002-05-26 20:54:33 +0000984 for(i=0; i<nExpr; i=i+2){
985 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +0000986 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +0000987 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +0000988 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
989 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +0000990 }else{
drhf570f012002-05-31 15:51:25 +0000991 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +0000992 }
993 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +0000994 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +0000995 addr = sqliteVdbeCurrentAddr(v);
996 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +0000997 }
drhf570f012002-05-31 15:51:25 +0000998 if( pExpr->pLeft ){
999 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1000 }
drh17a7f8d2002-03-24 13:13:27 +00001001 if( pExpr->pRight ){
1002 sqliteExprCode(pParse, pExpr->pRight);
1003 }else{
drhf5905aa2002-05-26 20:54:33 +00001004 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001005 }
drhf5905aa2002-05-26 20:54:33 +00001006 sqliteVdbeResolveLabel(v, expr_end_label);
drh17a7f8d2002-03-24 13:13:27 +00001007 }
1008 break;
drhcce7d172000-05-31 15:34:51 +00001009 }
drhcce7d172000-05-31 15:34:51 +00001010}
1011
1012/*
1013** Generate code for a boolean expression such that a jump is made
1014** to the label "dest" if the expression is true but execution
1015** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001016**
1017** If the expression evaluates to NULL (neither true nor false), then
1018** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001019*/
drhf5905aa2002-05-26 20:54:33 +00001020void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001021 Vdbe *v = pParse->pVdbe;
1022 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001023 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001024 switch( pExpr->op ){
1025 case TK_LT: op = OP_Lt; break;
1026 case TK_LE: op = OP_Le; break;
1027 case TK_GT: op = OP_Gt; break;
1028 case TK_GE: op = OP_Ge; break;
1029 case TK_NE: op = OP_Ne; break;
1030 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001031 case TK_ISNULL: op = OP_IsNull; break;
1032 case TK_NOTNULL: op = OP_NotNull; break;
1033 default: break;
1034 }
1035 switch( pExpr->op ){
1036 case TK_AND: {
1037 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001038 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1039 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001040 sqliteVdbeResolveLabel(v, d2);
1041 break;
1042 }
1043 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001044 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1045 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001046 break;
1047 }
1048 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001049 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001050 break;
1051 }
1052 case TK_LT:
1053 case TK_LE:
1054 case TK_GT:
1055 case TK_GE:
1056 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001057 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001058 sqliteExprCode(pParse, pExpr->pLeft);
1059 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001060 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001061 break;
1062 }
1063 case TK_ISNULL:
1064 case TK_NOTNULL: {
1065 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001066 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001067 break;
1068 }
drhfef52082000-06-06 01:50:43 +00001069 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001070 int addr;
drhcfab11b2000-06-06 03:31:22 +00001071 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001072 addr = sqliteVdbeCurrentAddr(v);
1073 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1074 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1075 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001076 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001077 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001078 }else{
drh99fcd712001-10-13 01:06:47 +00001079 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001080 }
1081 break;
1082 }
1083 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001084 int addr;
drhfef52082000-06-06 01:50:43 +00001085 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001086 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001087 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001088 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001089 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001090 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001091 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001092 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001093 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001094 break;
1095 }
drhcce7d172000-05-31 15:34:51 +00001096 default: {
1097 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001098 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001099 break;
1100 }
1101 }
1102}
1103
1104/*
drh66b89c82000-11-28 20:47:17 +00001105** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001106** to the label "dest" if the expression is false but execution
1107** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001108**
1109** If the expression evaluates to NULL (neither true nor false) then
1110** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001111*/
drhf5905aa2002-05-26 20:54:33 +00001112void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001113 Vdbe *v = pParse->pVdbe;
1114 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001115 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001116 switch( pExpr->op ){
1117 case TK_LT: op = OP_Ge; break;
1118 case TK_LE: op = OP_Gt; break;
1119 case TK_GT: op = OP_Le; break;
1120 case TK_GE: op = OP_Lt; break;
1121 case TK_NE: op = OP_Eq; break;
1122 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001123 case TK_ISNULL: op = OP_NotNull; break;
1124 case TK_NOTNULL: op = OP_IsNull; break;
1125 default: break;
1126 }
1127 switch( pExpr->op ){
1128 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001129 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1130 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001131 break;
1132 }
1133 case TK_OR: {
1134 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001135 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1136 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001137 sqliteVdbeResolveLabel(v, d2);
1138 break;
1139 }
1140 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001141 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001142 break;
1143 }
1144 case TK_LT:
1145 case TK_LE:
1146 case TK_GT:
1147 case TK_GE:
1148 case TK_NE:
1149 case TK_EQ: {
1150 sqliteExprCode(pParse, pExpr->pLeft);
1151 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001152 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001153 break;
1154 }
drhcce7d172000-05-31 15:34:51 +00001155 case TK_ISNULL:
1156 case TK_NOTNULL: {
1157 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001158 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001159 break;
1160 }
drhfef52082000-06-06 01:50:43 +00001161 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001162 int addr;
drhcfab11b2000-06-06 03:31:22 +00001163 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001164 addr = sqliteVdbeCurrentAddr(v);
1165 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1166 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1167 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001168 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001169 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001170 }else{
drh99fcd712001-10-13 01:06:47 +00001171 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001172 }
1173 break;
1174 }
1175 case TK_BETWEEN: {
1176 int addr;
1177 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001178 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001179 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1180 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001181 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001182 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1183 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001184 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001185 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001186 break;
1187 }
drhcce7d172000-05-31 15:34:51 +00001188 default: {
1189 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001190 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001191 break;
1192 }
1193 }
1194}
drh22827922000-06-06 17:27:05 +00001195
1196/*
1197** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1198** if they are identical and return FALSE if they differ in any way.
1199*/
drhd8bc7082000-06-07 23:51:50 +00001200int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001201 int i;
1202 if( pA==0 ){
1203 return pB==0;
1204 }else if( pB==0 ){
1205 return 0;
1206 }
1207 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001208 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1209 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001210 if( pA->pList ){
1211 if( pB->pList==0 ) return 0;
1212 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1213 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001214 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001215 return 0;
1216 }
1217 }
1218 }else if( pB->pList ){
1219 return 0;
1220 }
1221 if( pA->pSelect || pB->pSelect ) return 0;
1222 if( pA->token.z ){
1223 if( pB->token.z==0 ) return 0;
1224 if( pB->token.n!=pA->token.n ) return 0;
1225 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1226 }
1227 return 1;
1228}
1229
1230/*
1231** Add a new element to the pParse->aAgg[] array and return its index.
1232*/
1233static int appendAggInfo(Parse *pParse){
1234 if( (pParse->nAgg & 0x7)==0 ){
1235 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001236 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1237 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001238 return -1;
1239 }
drh6d4abfb2001-10-22 02:58:08 +00001240 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001241 }
1242 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1243 return pParse->nAgg++;
1244}
1245
1246/*
1247** Analyze the given expression looking for aggregate functions and
1248** for variables that need to be added to the pParse->aAgg[] array.
1249** Make additional entries to the pParse->aAgg[] array as necessary.
1250**
1251** This routine should only be called after the expression has been
1252** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1253**
1254** If errors are seen, leave an error message in zErrMsg and return
1255** the number of errors.
1256*/
1257int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1258 int i;
1259 AggExpr *aAgg;
1260 int nErr = 0;
1261
1262 if( pExpr==0 ) return 0;
1263 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001264 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001265 aAgg = pParse->aAgg;
1266 for(i=0; i<pParse->nAgg; i++){
1267 if( aAgg[i].isAgg ) continue;
1268 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001269 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001270 break;
1271 }
1272 }
1273 if( i>=pParse->nAgg ){
1274 i = appendAggInfo(pParse);
1275 if( i<0 ) return 1;
1276 pParse->aAgg[i].isAgg = 0;
1277 pParse->aAgg[i].pExpr = pExpr;
1278 }
drhaaf88722000-06-08 11:25:00 +00001279 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001280 break;
1281 }
1282 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001283 aAgg = pParse->aAgg;
1284 for(i=0; i<pParse->nAgg; i++){
1285 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001286 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001287 break;
1288 }
1289 }
1290 if( i>=pParse->nAgg ){
1291 i = appendAggInfo(pParse);
1292 if( i<0 ) return 1;
1293 pParse->aAgg[i].isAgg = 1;
1294 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001295 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001296 pExpr->token.z, pExpr->token.n,
1297 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001298 }
1299 pExpr->iAgg = i;
1300 break;
1301 }
1302 default: {
1303 if( pExpr->pLeft ){
1304 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1305 }
1306 if( nErr==0 && pExpr->pRight ){
1307 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1308 }
1309 if( nErr==0 && pExpr->pList ){
1310 int n = pExpr->pList->nExpr;
1311 int i;
1312 for(i=0; nErr==0 && i<n; i++){
1313 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1314 }
1315 }
1316 break;
1317 }
1318 }
1319 return nErr;
1320}
drh8e0a2f92002-02-23 23:45:45 +00001321
1322/*
1323** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001324** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001325** function, or return NULL if the function does not exist.
1326**
drh0bce8352002-02-28 00:41:10 +00001327** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001328** structure is created and liked into the "db" structure if a
1329** no matching function previously existed. When createFlag is true
1330** and the nArg parameter is -1, then only a function that accepts
1331** any number of arguments will be returned.
1332**
1333** If createFlag is false and nArg is -1, then the first valid
1334** function found is returned. A function is valid if either xFunc
1335** or xStep is non-zero.
1336*/
drh0bce8352002-02-28 00:41:10 +00001337FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001338 sqlite *db, /* An open database */
1339 const char *zName, /* Name of the function. Not null-terminated */
1340 int nName, /* Number of characters in the name */
1341 int nArg, /* Number of arguments. -1 means any number */
1342 int createFlag /* Create new entry if true and does not otherwise exist */
1343){
drh0bce8352002-02-28 00:41:10 +00001344 FuncDef *pFirst, *p, *pMaybe;
1345 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001346 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001347 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1348 return p;
1349 }
1350 pMaybe = 0;
1351 while( p && p->nArg!=nArg ){
1352 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1353 p = p->pNext;
1354 }
1355 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1356 return 0;
1357 }
1358 if( p==0 && pMaybe ){
1359 assert( createFlag==0 );
1360 return pMaybe;
1361 }
drh89425d52002-02-28 03:04:48 +00001362 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001363 p->nArg = nArg;
1364 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001365 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001366 }
1367 return p;
1368}