blob: 563291c983225f033682ccb5e7ae90a09f4bc578 [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**
drh1350b032002-02-27 19:00:20 +000015** $Id: expr.c,v 1.48 2002/02/27 19:00:21 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;
88 if( p->op!=TK_AS ){
89 if( p->pLeft ) sqliteExprDelete(p->pLeft);
90 if( p->pRight ) sqliteExprDelete(p->pRight);
91 }
92 if( p->pList ) sqliteExprListDelete(p->pList);
93 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
94 sqliteFree(p);
95}
96
drhcce7d172000-05-31 15:34:51 +000097/*
drha76b5df2002-02-23 02:32:10 +000098** The following group of functions are used to translate the string
99** pointers of tokens in expression from one buffer to another.
100**
101** Normally, the Expr.token.z and Expr.span.z fields point into the
102** original input buffer of an SQL statement. This is usually OK
103** since the SQL statement is executed and the expression is deleted
104** before the input buffer is freed. Making the tokens point to the
105** original input buffer saves many calls to malloc() and thus helps
106** the library to run faster.
107**
108** But sometimes we need an expression to persist past the time when
109** the input buffer is freed. (Example: The SELECT clause of a
110** CREATE VIEW statement contains expressions that must persist for
111** the life of the view.) When that happens we have to make a
112** persistent copy of the input buffer and translate the Expr.token.z
113** and Expr.span.z fields to point to the copy rather than the
drha2ed5602002-02-26 23:55:31 +0000114** original input buffer. The following group of routines handle that
drha76b5df2002-02-23 02:32:10 +0000115** translation.
116**
117** The "offset" parameter is the distance from the original input buffer
118** to the persistent copy. These routines recursively walk the entire
119** expression tree and shift all tokens by "offset" amount.
120**
121** The work of figuring out the appropriate "offset" and making the
122** presistent copy of the input buffer is done by the calling routine.
123*/
124void sqliteExprMoveStrings(Expr *p, int offset){
125 if( p==0 ) return;
126 if( p->token.z ) p->token.z += offset;
127 if( p->span.z ) p->span.z += offset;
128 if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset);
129 if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset);
130 if( p->pList ) sqliteExprListMoveStrings(p->pList, offset);
131 if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset);
132}
133void sqliteExprListMoveStrings(ExprList *pList, int offset){
134 int i;
135 if( pList==0 ) return;
136 for(i=0; i<pList->nExpr; i++){
137 sqliteExprMoveStrings(pList->a[i].pExpr, offset);
138 }
139}
140void sqliteSelectMoveStrings(Select *pSelect, int offset){
141 if( pSelect==0 ) return;
142 sqliteExprListMoveStrings(pSelect->pEList, offset);
143 sqliteExprMoveStrings(pSelect->pWhere, offset);
144 sqliteExprListMoveStrings(pSelect->pGroupBy, offset);
145 sqliteExprMoveStrings(pSelect->pHaving, offset);
146 sqliteExprListMoveStrings(pSelect->pOrderBy, offset);
147 sqliteSelectMoveStrings(pSelect->pPrior, offset);
148}
149
150/*
drhff78bd22002-02-27 01:47:11 +0000151** The following group of routines make deep copies of expressions,
152** expression lists, ID lists, and select statements. The copies can
153** be deleted (by being passed to their respective ...Delete() routines)
154** without effecting the originals.
155**
156** Note, however, that the Expr.token.z and Expr.span.z fields point to
157** string space that is allocated separately from the expression tree
158** itself. These routines do NOT duplicate that string space.
159**
160** The expression list and ID list return by sqliteExprListDup() and
161** sqliteIdListDup() can not be further expanded by subsequent calls
162** to sqliteExprListAppend() or sqliteIdListAppend().
163**
164** Any tables that the ID list might point to are not duplicated.
165*/
166Expr *sqliteExprDup(Expr *p){
167 Expr *pNew;
168 if( p==0 ) return 0;
169 pNew = sqliteMalloc( sizeof(*p) );
170 if( pNew==0 ) return 0;
171 pNew->op = p->op;
172 pNew->pLeft = sqliteExprDup(p->pLeft);
173 pNew->pRight = sqliteExprDup(p->pRight);
174 pNew->pList = sqliteExprListDup(p->pList);
175 pNew->token = p->token;
176 pNew->span = p->span;
177 pNew->pSelect = sqliteSelectDup(p->pSelect);
178 return pNew;
179}
180ExprList *sqliteExprListDup(ExprList *p){
181 ExprList *pNew;
182 int i;
183 if( p==0 ) return 0;
184 pNew = sqliteMalloc( sizeof(*pNew) );
185 if( pNew==0 ) return 0;
186 pNew->nExpr = p->nExpr;
187 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
188 for(i=0; i<p->nExpr; i++){
189 pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
190 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
191 pNew->a[i].sortOrder = p->a[i].sortOrder;
192 pNew->a[i].isAgg = p->a[i].isAgg;
193 pNew->a[i].done = 0;
194 }
195 return pNew;
196}
197IdList *sqliteIdListDup(IdList *p){
198 IdList *pNew;
199 int i;
200 if( p==0 ) return 0;
201 pNew = sqliteMalloc( sizeof(*pNew) );
202 if( pNew==0 ) return 0;
203 pNew->nId = p->nId;
204 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
205 for(i=0; i<p->nId; i++){
206 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
207 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
208 pNew->a[i].idx = p->a[i].idx;
209 pNew->a[i].pTab = 0;
210 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
211 }
212 return pNew;
213}
214Select *sqliteSelectDup(Select *p){
215 Select *pNew;
216 if( p==0 ) return 0;
217 pNew = sqliteMalloc( sizeof(*p) );
218 if( pNew==0 ) return 0;
219 pNew->isDistinct = p->isDistinct;
220 pNew->pEList = sqliteExprListDup(p->pEList);
221 pNew->pSrc = sqliteIdListDup(p->pSrc);
222 pNew->pWhere = sqliteExprDup(p->pWhere);
223 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
224 pNew->pHaving = sqliteExprDup(p->pHaving);
225 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
226 pNew->op = p->op;
227 pNew->pPrior = sqliteSelectDup(p->pPrior);
228 pNew->nLimit = p->nLimit;
229 pNew->nOffset = p->nOffset;
230 pNew->zSelect = 0;
231 return pNew;
232}
233
234
235/*
drha76b5df2002-02-23 02:32:10 +0000236** Add a new element to the end of an expression list. If pList is
237** initially NULL, then create a new expression list.
238*/
239ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
240 int i;
241 if( pList==0 ){
242 pList = sqliteMalloc( sizeof(ExprList) );
243 if( pList==0 ){
244 sqliteExprDelete(pExpr);
245 return 0;
246 }
247 }
248 if( (pList->nExpr & 7)==0 ){
249 int n = pList->nExpr + 8;
250 struct ExprList_item *a;
251 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
252 if( a==0 ){
253 sqliteExprDelete(pExpr);
254 return pList;
255 }
256 pList->a = a;
257 }
258 if( pExpr || pName ){
259 i = pList->nExpr++;
260 pList->a[i].pExpr = pExpr;
261 pList->a[i].zName = 0;
262 if( pName ){
263 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
264 sqliteDequote(pList->a[i].zName);
265 }
266 }
267 return pList;
268}
269
270/*
271** Delete an entire expression list.
272*/
273void sqliteExprListDelete(ExprList *pList){
274 int i;
275 if( pList==0 ) return;
276 for(i=0; i<pList->nExpr; i++){
277 sqliteExprDelete(pList->a[i].pExpr);
278 sqliteFree(pList->a[i].zName);
279 }
280 sqliteFree(pList->a);
281 sqliteFree(pList);
282}
283
284/*
drhfef52082000-06-06 01:50:43 +0000285** Walk an expression tree. Return 1 if the expression is constant
286** and 0 if it involves variables.
287*/
drh92086432002-01-22 14:11:29 +0000288int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000289 switch( p->op ){
290 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000291 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000292 case TK_DOT:
293 return 0;
drh92086432002-01-22 14:11:29 +0000294 case TK_INTEGER:
295 case TK_FLOAT:
296 case TK_STRING:
297 return 1;
drhfef52082000-06-06 01:50:43 +0000298 default: {
drh92086432002-01-22 14:11:29 +0000299 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
300 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000301 if( p->pList ){
302 int i;
303 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000304 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000305 }
306 }
drh92086432002-01-22 14:11:29 +0000307 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000308 }
309 }
drh92086432002-01-22 14:11:29 +0000310 return 0;
drhfef52082000-06-06 01:50:43 +0000311}
312
313/*
drh4794b982000-06-06 13:54:14 +0000314** Walk the expression tree and process operators of the form:
315**
316** expr IN (SELECT ...)
317**
drh967e8b72000-06-21 13:59:10 +0000318** These operators have to be processed before column names are
drh4794b982000-06-06 13:54:14 +0000319** resolved because each such operator increments pParse->nTab
drh1ccde152000-06-17 13:12:39 +0000320** to reserve cursor numbers for its own use. But pParse->nTab
drhaacc5432002-01-06 17:07:40 +0000321** needs to be constant once we begin resolving column names. For
322** that reason, this procedure needs to be called on every expression
323** before sqliteExprResolveIds() is called on any expression.
drh4794b982000-06-06 13:54:14 +0000324**
325** Actually, the processing of IN-SELECT is only started by this
326** routine. This routine allocates a cursor number to the IN-SELECT
327** and then moves on. The code generation is done by
328** sqliteExprResolveIds() which must be called afterwards.
329*/
330void sqliteExprResolveInSelect(Parse *pParse, Expr *pExpr){
331 if( pExpr==0 ) return;
332 if( pExpr->op==TK_IN && pExpr->pSelect!=0 ){
333 pExpr->iTable = pParse->nTab++;
334 }else{
335 if( pExpr->pLeft ) sqliteExprResolveInSelect(pParse, pExpr->pLeft);
336 if( pExpr->pRight ) sqliteExprResolveInSelect(pParse, pExpr->pRight);
337 if( pExpr->pList ){
338 int i;
339 ExprList *pList = pExpr->pList;
340 for(i=0; i<pList->nExpr; i++){
341 sqliteExprResolveInSelect(pParse, pList->a[i].pExpr);
342 }
343 }
344 }
345}
346
347/*
drhc4a3c772001-04-04 11:48:57 +0000348** Return TRUE if the given string is a row-id column name.
349*/
350static int sqliteIsRowid(const char *z){
351 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
352 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
353 if( sqliteStrICmp(z, "OID")==0 ) return 1;
354 return 0;
355}
356
357/*
drhcce7d172000-05-31 15:34:51 +0000358** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000359** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000360** index to the table in the table list and a column offset. The
361** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
362** value is changed to the index of the referenced table in pTabList
363** plus the pParse->nTab value. This value will ultimately become the
364** VDBE cursor number for a cursor that is pointing into the referenced
365** table. The Expr.iColumn value is changed to the index of the column
366** of the referenced table. The Expr.iColumn value for the special
367** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
368** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000369**
drhfef52082000-06-06 01:50:43 +0000370** We also check for instances of the IN operator. IN comes in two
371** forms:
372**
373** expr IN (exprlist)
374** and
375** expr IN (SELECT ...)
376**
377** The first form is handled by creating a set holding the list
378** of allowed values. The second form causes the SELECT to generate
379** a temporary table.
380**
381** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000382** If it finds any, it generates code to write the value of that select
383** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000384**
drh967e8b72000-06-21 13:59:10 +0000385** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000386** the number of errors seen and leaves an error message on pParse->zErrMsg.
387*/
drha2e00042002-01-22 03:13:42 +0000388int sqliteExprResolveIds(
389 Parse *pParse, /* The parser context */
390 IdList *pTabList, /* List of tables used to resolve column names */
391 ExprList *pEList, /* List of expressions used to resolve "AS" */
392 Expr *pExpr /* The expression to be analyzed. */
393){
drhdaffd0e2001-04-11 14:28:42 +0000394 if( pExpr==0 || pTabList==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000395 switch( pExpr->op ){
drha2e00042002-01-22 03:13:42 +0000396 /* A lone identifier. Try and match it as follows:
397 **
398 ** 1. To the name of a column of one of the tables in pTabList
399 **
400 ** 2. To the right side of an AS keyword in the column list of
401 ** a SELECT statement. (For example, match against 'x' in
402 ** "SELECT a+b AS 'x' FROM t1".)
403 **
404 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
405 */
drhcce7d172000-05-31 15:34:51 +0000406 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000407 int cnt = 0; /* Number of matches */
408 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000409 char *z;
410 assert( pExpr->token.z );
411 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000412 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000413 if( z==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000414 for(i=0; i<pTabList->nId; i++){
415 int j;
416 Table *pTab = pTabList->a[i].pTab;
417 if( pTab==0 ) continue;
418 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000419 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000420 cnt++;
drh19a775c2000-06-05 18:54:46 +0000421 pExpr->iTable = i + pParse->nTab;
drh4a324312001-12-21 14:30:42 +0000422 if( j==pTab->iPKey ){
423 /* Substitute the record number for the INTEGER PRIMARY KEY */
424 pExpr->iColumn = -1;
425 }else{
426 pExpr->iColumn = j;
427 }
drha2e00042002-01-22 03:13:42 +0000428 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000429 }
430 }
431 }
drha2e00042002-01-22 03:13:42 +0000432 if( cnt==0 && pEList!=0 ){
433 int j;
434 for(j=0; j<pEList->nExpr; j++){
435 char *zAs = pEList->a[j].zName;
436 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
437 cnt++;
438 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
439 pExpr->op = TK_AS;
440 pExpr->iColumn = j;
441 pExpr->pLeft = pEList->a[j].pExpr;
442 }
443 }
444 }
drhc4a3c772001-04-04 11:48:57 +0000445 if( cnt==0 && sqliteIsRowid(z) ){
446 pExpr->iColumn = -1;
447 pExpr->iTable = pParse->nTab;
448 cnt = 1 + (pTabList->nId>1);
drha2e00042002-01-22 03:13:42 +0000449 pExpr->op = TK_COLUMN;
drhc4a3c772001-04-04 11:48:57 +0000450 }
drhcce7d172000-05-31 15:34:51 +0000451 sqliteFree(z);
452 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000453 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000454 pExpr->token.z, pExpr->token.n, 0);
455 pParse->nErr++;
456 return 1;
457 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000458 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000459 pExpr->token.z, pExpr->token.n, 0);
460 pParse->nErr++;
461 return 1;
462 }
drhcce7d172000-05-31 15:34:51 +0000463 break;
464 }
465
drh967e8b72000-06-21 13:59:10 +0000466 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000467 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000468 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000469 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000470 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000471 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000472 char *zLeft, *zRight; /* Text of an identifier */
473
474 pLeft = pExpr->pLeft;
475 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000476 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
477 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000478 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
479 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000480 if( zLeft==0 || zRight==0 ){
481 sqliteFree(zLeft);
482 sqliteFree(zRight);
483 return 1;
484 }
drh87c40e82001-07-23 14:33:02 +0000485 sqliteDequote(zLeft);
486 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000487 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000488 for(i=0; i<pTabList->nId; i++){
489 int j;
490 char *zTab;
491 Table *pTab = pTabList->a[i].pTab;
492 if( pTab==0 ) continue;
493 if( pTabList->a[i].zAlias ){
494 zTab = pTabList->a[i].zAlias;
495 }else{
496 zTab = pTab->zName;
497 }
498 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhc4a3c772001-04-04 11:48:57 +0000499 if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000500 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000501 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000502 cnt++;
drh19a775c2000-06-05 18:54:46 +0000503 pExpr->iTable = i + pParse->nTab;
drh4a324312001-12-21 14:30:42 +0000504 if( j==pTab->iPKey ){
505 /* Substitute the record number for the INTEGER PRIMARY KEY */
506 pExpr->iColumn = -1;
507 }else{
508 pExpr->iColumn = j;
509 }
drhcce7d172000-05-31 15:34:51 +0000510 }
511 }
512 }
drhc4a3c772001-04-04 11:48:57 +0000513 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
514 cnt = 1;
515 pExpr->iColumn = -1;
516 }
drhcce7d172000-05-31 15:34:51 +0000517 sqliteFree(zLeft);
518 sqliteFree(zRight);
519 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000520 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000521 pLeft->token.z, pLeft->token.n, ".", 1,
522 pRight->token.z, pRight->token.n, 0);
523 pParse->nErr++;
524 return 1;
525 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000526 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000527 pLeft->token.z, pLeft->token.n, ".", 1,
528 pRight->token.z, pRight->token.n, 0);
529 pParse->nErr++;
530 return 1;
531 }
532 sqliteExprDelete(pLeft);
533 pExpr->pLeft = 0;
534 sqliteExprDelete(pRight);
535 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000536 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000537 break;
538 }
539
drhfef52082000-06-06 01:50:43 +0000540 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000541 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000542 if( v==0 ) return 1;
drha2e00042002-01-22 03:13:42 +0000543 if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000544 return 1;
545 }
drhfef52082000-06-06 01:50:43 +0000546 if( pExpr->pSelect ){
547 /* Case 1: expr IN (SELECT ...)
548 **
549 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000550 ** table. The cursor number of the temporary table has already
551 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000552 */
drhc6b52df2002-01-04 03:09:29 +0000553 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drhfef52082000-06-06 01:50:43 +0000554 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
555 }else if( pExpr->pList ){
556 /* Case 2: expr IN (exprlist)
557 **
558 ** Create a set to put the exprlist values in. The Set id is stored
559 ** in iTable.
560 */
561 int i, iSet;
562 for(i=0; i<pExpr->pList->nExpr; i++){
563 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000564 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000565 sqliteSetString(&pParse->zErrMsg,
566 "right-hand side of IN operator must be constant", 0);
567 pParse->nErr++;
568 return 1;
569 }
drh4794b982000-06-06 13:54:14 +0000570 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
571 return 1;
572 }
drhfef52082000-06-06 01:50:43 +0000573 }
574 iSet = pExpr->iTable = pParse->nSet++;
575 for(i=0; i<pExpr->pList->nExpr; i++){
576 Expr *pE2 = pExpr->pList->a[i].pExpr;
577 switch( pE2->op ){
578 case TK_FLOAT:
579 case TK_INTEGER:
580 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000581 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000582 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000583 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
584 sqliteVdbeDequoteP3(v, addr);
585 break;
586 }
587 default: {
588 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000589 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000590 break;
591 }
592 }
593 }
594 }
drhcfab11b2000-06-06 03:31:22 +0000595 break;
drhfef52082000-06-06 01:50:43 +0000596 }
597
drh19a775c2000-06-05 18:54:46 +0000598 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000599 /* This has to be a scalar SELECT. Generate code to put the
600 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000601 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000602 */
drh967e8b72000-06-21 13:59:10 +0000603 pExpr->iColumn = pParse->nMem++;
604 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000605 return 1;
606 }
607 break;
608 }
609
drhcce7d172000-05-31 15:34:51 +0000610 /* For all else, just recursively walk the tree */
611 default: {
drh4794b982000-06-06 13:54:14 +0000612 if( pExpr->pLeft
drha2e00042002-01-22 03:13:42 +0000613 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000614 return 1;
615 }
616 if( pExpr->pRight
drha2e00042002-01-22 03:13:42 +0000617 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000618 return 1;
619 }
620 if( pExpr->pList ){
621 int i;
622 ExprList *pList = pExpr->pList;
623 for(i=0; i<pList->nExpr; i++){
drha2e00042002-01-22 03:13:42 +0000624 if( sqliteExprResolveIds(pParse,pTabList,pEList,pList->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000625 return 1;
626 }
627 }
628 }
629 }
630 }
631 return 0;
632}
633
634#if 0 /* NOT USED */
635/*
636** Compare a token against a string. Return TRUE if they match.
637*/
638static int sqliteTokenCmp(Token *pToken, const char *zStr){
639 int n = strlen(zStr);
640 if( n!=pToken->n ) return 0;
641 return sqliteStrNICmp(pToken->z, zStr, n)==0;
642}
643#endif
644
645/*
646** Convert a function name into its integer identifier. Return the
647** identifier. Return FN_Unknown if the function name is unknown.
648*/
649int sqliteFuncId(Token *pToken){
650 static const struct {
651 char *zName;
652 int len;
653 int id;
654 } aFunc[] = {
drhbf4133c2001-10-13 02:59:08 +0000655 { "count", 5, FN_Count },
656 { "min", 3, FN_Min },
657 { "max", 3, FN_Max },
658 { "sum", 3, FN_Sum },
659 { "avg", 3, FN_Avg },
drhbf4133c2001-10-13 02:59:08 +0000660 { "length", 6, FN_Length },
661 { "substr", 6, FN_Substr },
662 { "abs", 3, FN_Abs },
663 { "round", 5, FN_Round },
drhcce7d172000-05-31 15:34:51 +0000664 };
665 int i;
666 for(i=0; i<ArraySize(aFunc); i++){
667 if( aFunc[i].len==pToken->n
668 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
669 return aFunc[i].id;
670 }
671 }
672 return FN_Unknown;
673}
674
675/*
676** Error check the functions in an expression. Make sure all
677** function names are recognized and all functions have the correct
678** number of arguments. Leave an error message in pParse->zErrMsg
679** if anything is amiss. Return the number of errors.
680**
681** if pIsAgg is not null and this expression is an aggregate function
682** (like count(*) or max(value)) then write a 1 into *pIsAgg.
683*/
684int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
685 int nErr = 0;
686 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000687 switch( pExpr->op ){
688 case TK_FUNCTION: {
689 int id = sqliteFuncId(&pExpr->token);
690 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
691 int no_such_func = 0;
692 int too_many_args = 0;
693 int too_few_args = 0;
drh8e0a2f92002-02-23 23:45:45 +0000694 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000695 int is_agg = 0;
696 int i;
drh967e8b72000-06-21 13:59:10 +0000697 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000698 switch( id ){
drh8e0a2f92002-02-23 23:45:45 +0000699 case FN_Unknown: {
700 UserFunc *pUser = sqliteFindUserFunction(pParse->db,
701 pExpr->token.z, pExpr->token.n, n, 0);
702 if( pUser==0 ){
703 pUser = sqliteFindUserFunction(pParse->db,
704 pExpr->token.z, pExpr->token.n, -1, 0);
705 if( pUser==0 ){
706 no_such_func = 1;
707 }else{
708 wrong_num_args = 1;
709 }
710 }else{
711 is_agg = pUser->xFunc==0;
712 }
drhcce7d172000-05-31 15:34:51 +0000713 break;
714 }
715 case FN_Count: {
drhcce7d172000-05-31 15:34:51 +0000716 too_many_args = n>1;
717 is_agg = 1;
718 break;
719 }
720 case FN_Max:
721 case FN_Min: {
drh8e0a2f92002-02-23 23:45:45 +0000722 too_few_args = n<1;
drhcce7d172000-05-31 15:34:51 +0000723 is_agg = n==1;
724 break;
725 }
drh22827922000-06-06 17:27:05 +0000726 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000727 case FN_Sum: {
drhcce7d172000-05-31 15:34:51 +0000728 too_many_args = n>1;
729 too_few_args = n<1;
730 is_agg = 1;
731 break;
732 }
drhbf4133c2001-10-13 02:59:08 +0000733 case FN_Abs:
drh6ec27332000-08-28 15:51:43 +0000734 case FN_Length: {
735 too_few_args = n<1;
736 too_many_args = n>1;
737 break;
738 }
drhbf4133c2001-10-13 02:59:08 +0000739 case FN_Round: {
740 too_few_args = n<1;
741 too_many_args = n>2;
742 break;
743 }
drh6ec27332000-08-28 15:51:43 +0000744 case FN_Substr: {
745 too_few_args = n<3;
746 too_many_args = n>3;
747 break;
748 }
drhcce7d172000-05-31 15:34:51 +0000749 default: break;
750 }
drh8e0a2f92002-02-23 23:45:45 +0000751 if( is_agg && !allowAgg ){
752 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
753 pExpr->token.z, pExpr->token.n, "()", 2, 0);
754 pParse->nErr++;
755 nErr++;
756 is_agg = 0;
757 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000758 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
759 pExpr->token.z, pExpr->token.n, 0);
760 pParse->nErr++;
761 nErr++;
762 }else if( too_many_args ){
763 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
764 pExpr->token.z, pExpr->token.n, "()", 2, 0);
765 pParse->nErr++;
766 nErr++;
767 }else if( too_few_args ){
768 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
769 pExpr->token.z, pExpr->token.n, "()", 2, 0);
770 pParse->nErr++;
771 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000772 }else if( wrong_num_args ){
773 sqliteSetNString(&pParse->zErrMsg,
774 "wrong number of arguments to function ",-1,
775 pExpr->token.z, pExpr->token.n, "()", 2, 0);
776 pParse->nErr++;
777 nErr++;
drhcce7d172000-05-31 15:34:51 +0000778 }
drh22827922000-06-06 17:27:05 +0000779 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000780 if( is_agg && pIsAgg ) *pIsAgg = 1;
781 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000782 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
783 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000784 }
785 }
786 default: {
787 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000788 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000789 }
790 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000791 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000792 }
drhfef52082000-06-06 01:50:43 +0000793 if( nErr==0 && pExpr->pList ){
794 int n = pExpr->pList->nExpr;
795 int i;
796 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000797 Expr *pE2 = pExpr->pList->a[i].pExpr;
798 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000799 }
800 }
drhcce7d172000-05-31 15:34:51 +0000801 break;
802 }
803 }
804 return nErr;
805}
806
807/*
808** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000809** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000810*/
811void sqliteExprCode(Parse *pParse, Expr *pExpr){
812 Vdbe *v = pParse->pVdbe;
813 int op;
drhdaffd0e2001-04-11 14:28:42 +0000814 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000815 switch( pExpr->op ){
816 case TK_PLUS: op = OP_Add; break;
817 case TK_MINUS: op = OP_Subtract; break;
818 case TK_STAR: op = OP_Multiply; break;
819 case TK_SLASH: op = OP_Divide; break;
820 case TK_AND: op = OP_And; break;
821 case TK_OR: op = OP_Or; break;
822 case TK_LT: op = OP_Lt; break;
823 case TK_LE: op = OP_Le; break;
824 case TK_GT: op = OP_Gt; break;
825 case TK_GE: op = OP_Ge; break;
826 case TK_NE: op = OP_Ne; break;
827 case TK_EQ: op = OP_Eq; break;
828 case TK_LIKE: op = OP_Like; break;
829 case TK_GLOB: op = OP_Glob; break;
830 case TK_ISNULL: op = OP_IsNull; break;
831 case TK_NOTNULL: op = OP_NotNull; break;
832 case TK_NOT: op = OP_Not; break;
833 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000834 case TK_BITAND: op = OP_BitAnd; break;
835 case TK_BITOR: op = OP_BitOr; break;
836 case TK_BITNOT: op = OP_BitNot; break;
837 case TK_LSHIFT: op = OP_ShiftLeft; break;
838 case TK_RSHIFT: op = OP_ShiftRight; break;
839 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000840 default: break;
841 }
842 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000843 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000844 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000845 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000846 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000847 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000848 }else{
drh99fcd712001-10-13 01:06:47 +0000849 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000850 }
drhcce7d172000-05-31 15:34:51 +0000851 break;
852 }
drhef6764a2002-01-30 04:32:00 +0000853 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +0000854 case TK_INTEGER: {
drh7a7c7392001-11-24 00:31:46 +0000855 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000856 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000857 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000858 break;
859 }
drhcce7d172000-05-31 15:34:51 +0000860 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000861 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000862 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000863 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
864 sqliteVdbeDequoteP3(v, addr);
865 break;
866 }
867 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000868 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000869 break;
870 }
871 case TK_AND:
872 case TK_OR:
873 case TK_PLUS:
874 case TK_STAR:
875 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000876 case TK_REM:
877 case TK_BITAND:
878 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000879 case TK_SLASH: {
880 sqliteExprCode(pParse, pExpr->pLeft);
881 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000882 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000883 break;
884 }
drhbf4133c2001-10-13 02:59:08 +0000885 case TK_LSHIFT:
886 case TK_RSHIFT: {
887 sqliteExprCode(pParse, pExpr->pRight);
888 sqliteExprCode(pParse, pExpr->pLeft);
889 sqliteVdbeAddOp(v, op, 0, 0);
890 break;
891 }
drh00400772000-06-16 20:51:26 +0000892 case TK_CONCAT: {
893 sqliteExprCode(pParse, pExpr->pLeft);
894 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000895 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000896 break;
897 }
drhcce7d172000-05-31 15:34:51 +0000898 case TK_LT:
899 case TK_LE:
900 case TK_GT:
901 case TK_GE:
902 case TK_NE:
903 case TK_EQ:
904 case TK_LIKE:
905 case TK_GLOB: {
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 sqliteExprCode(pParse, pExpr->pRight);
910 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000911 sqliteVdbeAddOp(v, op, 0, dest);
912 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000913 break;
914 }
drhcce7d172000-05-31 15:34:51 +0000915 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000916 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000917 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000918 Token *p = &pExpr->pLeft->token;
919 char *z = sqliteMalloc( p->n + 2 );
920 sprintf(z, "-%.*s", p->n, p->z);
drh99fcd712001-10-13 01:06:47 +0000921 sqliteVdbeAddOp(v, OP_String, 0, 0);
922 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000923 sqliteFree(z);
924 break;
925 }
drh1ccde152000-06-17 13:12:39 +0000926 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000927 }
drhbf4133c2001-10-13 02:59:08 +0000928 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000929 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000930 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000931 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000932 break;
933 }
934 case TK_ISNULL:
935 case TK_NOTNULL: {
936 int dest;
drh99fcd712001-10-13 01:06:47 +0000937 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000938 sqliteExprCode(pParse, pExpr->pLeft);
939 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000940 sqliteVdbeAddOp(v, op, 0, dest);
941 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000942 break;
943 }
drh22827922000-06-06 17:27:05 +0000944 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000945 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh967e8b72000-06-21 13:59:10 +0000946 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000947 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
drh99fcd712001-10-13 01:06:47 +0000948 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount);
949 sqliteVdbeAddOp(v, OP_Divide, 0, 0);
drh22827922000-06-06 17:27:05 +0000950 }
951 break;
952 }
drhcce7d172000-05-31 15:34:51 +0000953 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000954 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000955 int op;
956 int i;
957 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000958 switch( id ){
drh6ec27332000-08-28 15:51:43 +0000959 case FN_Min:
960 case FN_Max: {
961 op = id==FN_Min ? OP_Min : OP_Max;
962 for(i=0; i<pList->nExpr; i++){
963 sqliteExprCode(pParse, pList->a[i].pExpr);
964 if( i>0 ){
drh99fcd712001-10-13 01:06:47 +0000965 sqliteVdbeAddOp(v, op, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000966 }
967 }
968 break;
969 }
drhbf4133c2001-10-13 02:59:08 +0000970 case FN_Abs: {
971 sqliteExprCode(pParse, pList->a[0].pExpr);
972 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
973 break;
974 }
975 case FN_Round: {
976 if( pList->nExpr==2 ){
977 sqliteExprCode(pParse, pList->a[1].pExpr);
978 }else{
979 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
980 }
981 sqliteExprCode(pParse, pList->a[0].pExpr);
982 sqliteVdbeAddOp(v, OP_Precision, 0, 0);
983 break;
984 }
drh6ec27332000-08-28 15:51:43 +0000985 case FN_Length: {
986 sqliteExprCode(pParse, pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000987 sqliteVdbeAddOp(v, OP_Strlen, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000988 break;
989 }
990 case FN_Substr: {
991 for(i=0; i<pList->nExpr; i++){
992 sqliteExprCode(pParse, pList->a[i].pExpr);
993 }
drh99fcd712001-10-13 01:06:47 +0000994 sqliteVdbeAddOp(v, OP_Substr, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000995 break;
996 }
drh8e0a2f92002-02-23 23:45:45 +0000997 case FN_Unknown: {
998 UserFunc *pUser;
999 pUser = sqliteFindUserFunction(pParse->db,
1000 pExpr->token.z, pExpr->token.n, pList->nExpr, 0);
1001 assert( pUser!=0 );
1002 for(i=0; i<pList->nExpr; i++){
1003 sqliteExprCode(pParse, pList->a[i].pExpr);
1004 }
1005 sqliteVdbeAddOp(v, OP_UserFunc, pList->nExpr, 0);
drh1350b032002-02-27 19:00:20 +00001006 sqliteVdbeChangeP3(v, -1, (char*)pUser, P3_POINTER);
drh8e0a2f92002-02-23 23:45:45 +00001007 break;
1008 }
drh6ec27332000-08-28 15:51:43 +00001009 default: {
1010 /* Can't happen! */
1011 break;
drhcce7d172000-05-31 15:34:51 +00001012 }
1013 }
1014 break;
1015 }
drh19a775c2000-06-05 18:54:46 +00001016 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001017 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001018 break;
1019 }
drhfef52082000-06-06 01:50:43 +00001020 case TK_IN: {
1021 int addr;
drh99fcd712001-10-13 01:06:47 +00001022 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001023 sqliteExprCode(pParse, pExpr->pLeft);
1024 addr = sqliteVdbeCurrentAddr(v);
1025 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001026 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +00001027 }else{
drh99fcd712001-10-13 01:06:47 +00001028 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +00001029 }
drh99fcd712001-10-13 01:06:47 +00001030 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001031 break;
1032 }
1033 case TK_BETWEEN: {
1034 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001035 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +00001036 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +00001037 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +00001038 sqliteVdbeResolveLabel(v, lbl);
1039 break;
1040 }
drha2e00042002-01-22 03:13:42 +00001041 case TK_AS: {
1042 sqliteExprCode(pParse, pExpr->pLeft);
1043 break;
1044 }
drhcce7d172000-05-31 15:34:51 +00001045 }
1046 return;
1047}
1048
1049/*
1050** Generate code for a boolean expression such that a jump is made
1051** to the label "dest" if the expression is true but execution
1052** continues straight thru if the expression is false.
1053*/
1054void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
1055 Vdbe *v = pParse->pVdbe;
1056 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001057 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001058 switch( pExpr->op ){
1059 case TK_LT: op = OP_Lt; break;
1060 case TK_LE: op = OP_Le; break;
1061 case TK_GT: op = OP_Gt; break;
1062 case TK_GE: op = OP_Ge; break;
1063 case TK_NE: op = OP_Ne; break;
1064 case TK_EQ: op = OP_Eq; break;
1065 case TK_LIKE: op = OP_Like; break;
1066 case TK_GLOB: op = OP_Glob; break;
1067 case TK_ISNULL: op = OP_IsNull; break;
1068 case TK_NOTNULL: op = OP_NotNull; break;
1069 default: break;
1070 }
1071 switch( pExpr->op ){
1072 case TK_AND: {
1073 int d2 = sqliteVdbeMakeLabel(v);
1074 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
1075 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
1076 sqliteVdbeResolveLabel(v, d2);
1077 break;
1078 }
1079 case TK_OR: {
1080 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1081 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
1082 break;
1083 }
1084 case TK_NOT: {
1085 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1086 break;
1087 }
1088 case TK_LT:
1089 case TK_LE:
1090 case TK_GT:
1091 case TK_GE:
1092 case TK_NE:
1093 case TK_EQ:
1094 case TK_LIKE:
1095 case TK_GLOB: {
1096 sqliteExprCode(pParse, pExpr->pLeft);
1097 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001098 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001099 break;
1100 }
1101 case TK_ISNULL:
1102 case TK_NOTNULL: {
1103 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001104 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001105 break;
1106 }
drhfef52082000-06-06 01:50:43 +00001107 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001108 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001109 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001110 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001111 }else{
drh99fcd712001-10-13 01:06:47 +00001112 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001113 }
1114 break;
1115 }
1116 case TK_BETWEEN: {
1117 int lbl = sqliteVdbeMakeLabel(v);
1118 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001119 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001120 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +00001121 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +00001122 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001123 sqliteVdbeAddOp(v, OP_Le, 0, dest);
1124 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1125 sqliteVdbeResolveLabel(v, lbl);
1126 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001127 break;
1128 }
drhcce7d172000-05-31 15:34:51 +00001129 default: {
1130 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001131 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001132 break;
1133 }
1134 }
1135}
1136
1137/*
drh66b89c82000-11-28 20:47:17 +00001138** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001139** to the label "dest" if the expression is false but execution
1140** continues straight thru if the expression is true.
1141*/
1142void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
1143 Vdbe *v = pParse->pVdbe;
1144 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001145 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001146 switch( pExpr->op ){
1147 case TK_LT: op = OP_Ge; break;
1148 case TK_LE: op = OP_Gt; break;
1149 case TK_GT: op = OP_Le; break;
1150 case TK_GE: op = OP_Lt; break;
1151 case TK_NE: op = OP_Eq; break;
1152 case TK_EQ: op = OP_Ne; break;
1153 case TK_LIKE: op = OP_Like; break;
1154 case TK_GLOB: op = OP_Glob; break;
1155 case TK_ISNULL: op = OP_NotNull; break;
1156 case TK_NOTNULL: op = OP_IsNull; break;
1157 default: break;
1158 }
1159 switch( pExpr->op ){
1160 case TK_AND: {
1161 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1162 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1163 break;
1164 }
1165 case TK_OR: {
1166 int d2 = sqliteVdbeMakeLabel(v);
1167 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1168 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1169 sqliteVdbeResolveLabel(v, d2);
1170 break;
1171 }
1172 case TK_NOT: {
1173 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1174 break;
1175 }
1176 case TK_LT:
1177 case TK_LE:
1178 case TK_GT:
1179 case TK_GE:
1180 case TK_NE:
1181 case TK_EQ: {
1182 sqliteExprCode(pParse, pExpr->pLeft);
1183 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001184 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001185 break;
1186 }
1187 case TK_LIKE:
1188 case TK_GLOB: {
1189 sqliteExprCode(pParse, pExpr->pLeft);
1190 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001191 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001192 break;
1193 }
1194 case TK_ISNULL:
1195 case TK_NOTNULL: {
1196 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001197 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001198 break;
1199 }
drhfef52082000-06-06 01:50:43 +00001200 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001201 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001202 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001203 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001204 }else{
drh99fcd712001-10-13 01:06:47 +00001205 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001206 }
1207 break;
1208 }
1209 case TK_BETWEEN: {
1210 int addr;
1211 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001212 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001213 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1214 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001215 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1216 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1217 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001218 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001219 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001220 break;
1221 }
drhcce7d172000-05-31 15:34:51 +00001222 default: {
1223 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001224 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1225 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001226 break;
1227 }
1228 }
1229}
drh22827922000-06-06 17:27:05 +00001230
1231/*
1232** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1233** if they are identical and return FALSE if they differ in any way.
1234*/
drhd8bc7082000-06-07 23:51:50 +00001235int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001236 int i;
1237 if( pA==0 ){
1238 return pB==0;
1239 }else if( pB==0 ){
1240 return 0;
1241 }
1242 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001243 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1244 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001245 if( pA->pList ){
1246 if( pB->pList==0 ) return 0;
1247 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1248 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001249 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001250 return 0;
1251 }
1252 }
1253 }else if( pB->pList ){
1254 return 0;
1255 }
1256 if( pA->pSelect || pB->pSelect ) return 0;
1257 if( pA->token.z ){
1258 if( pB->token.z==0 ) return 0;
1259 if( pB->token.n!=pA->token.n ) return 0;
1260 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1261 }
1262 return 1;
1263}
1264
1265/*
1266** Add a new element to the pParse->aAgg[] array and return its index.
1267*/
1268static int appendAggInfo(Parse *pParse){
1269 if( (pParse->nAgg & 0x7)==0 ){
1270 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001271 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1272 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001273 return -1;
1274 }
drh6d4abfb2001-10-22 02:58:08 +00001275 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001276 }
1277 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1278 return pParse->nAgg++;
1279}
1280
1281/*
1282** Analyze the given expression looking for aggregate functions and
1283** for variables that need to be added to the pParse->aAgg[] array.
1284** Make additional entries to the pParse->aAgg[] array as necessary.
1285**
1286** This routine should only be called after the expression has been
1287** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1288**
1289** If errors are seen, leave an error message in zErrMsg and return
1290** the number of errors.
1291*/
1292int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1293 int i;
1294 AggExpr *aAgg;
1295 int nErr = 0;
1296
1297 if( pExpr==0 ) return 0;
1298 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001299 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001300 aAgg = pParse->aAgg;
1301 for(i=0; i<pParse->nAgg; i++){
1302 if( aAgg[i].isAgg ) continue;
1303 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001304 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001305 break;
1306 }
1307 }
1308 if( i>=pParse->nAgg ){
1309 i = appendAggInfo(pParse);
1310 if( i<0 ) return 1;
1311 pParse->aAgg[i].isAgg = 0;
1312 pParse->aAgg[i].pExpr = pExpr;
1313 }
drhaaf88722000-06-08 11:25:00 +00001314 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001315 break;
1316 }
1317 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +00001318 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +00001319 if( pParse->iAggCount>=0 ){
1320 i = pParse->iAggCount;
1321 }else{
1322 i = appendAggInfo(pParse);
1323 if( i<0 ) return 1;
1324 pParse->aAgg[i].isAgg = 1;
1325 pParse->aAgg[i].pExpr = 0;
1326 pParse->iAggCount = i;
1327 }
drh967e8b72000-06-21 13:59:10 +00001328 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +00001329 pExpr->iAgg = i;
1330 break;
1331 }
1332 }
1333 aAgg = pParse->aAgg;
1334 for(i=0; i<pParse->nAgg; i++){
1335 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001336 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001337 break;
1338 }
1339 }
1340 if( i>=pParse->nAgg ){
1341 i = appendAggInfo(pParse);
1342 if( i<0 ) return 1;
1343 pParse->aAgg[i].isAgg = 1;
1344 pParse->aAgg[i].pExpr = pExpr;
drhe5095352002-02-24 03:25:14 +00001345 if( pExpr->iColumn==FN_Unknown ){
1346 pParse->aAgg[i].pUser = sqliteFindUserFunction(pParse->db,
1347 pExpr->token.z, pExpr->token.n, pExpr->pList->nExpr, 0);
1348 }else{
1349 pParse->aAgg[i].pUser = 0;
1350 }
drh22827922000-06-06 17:27:05 +00001351 }
1352 pExpr->iAgg = i;
1353 break;
1354 }
1355 default: {
1356 if( pExpr->pLeft ){
1357 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1358 }
1359 if( nErr==0 && pExpr->pRight ){
1360 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1361 }
1362 if( nErr==0 && pExpr->pList ){
1363 int n = pExpr->pList->nExpr;
1364 int i;
1365 for(i=0; nErr==0 && i<n; i++){
1366 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1367 }
1368 }
1369 break;
1370 }
1371 }
1372 return nErr;
1373}
drh8e0a2f92002-02-23 23:45:45 +00001374
1375/*
1376** Locate a user function given a name and a number of arguments.
1377** Return a pointer to the UserFunc structure that defines that
1378** function, or return NULL if the function does not exist.
1379**
1380** If the createFlag argument is true, then a new (blank) UserFunc
1381** structure is created and liked into the "db" structure if a
1382** no matching function previously existed. When createFlag is true
1383** and the nArg parameter is -1, then only a function that accepts
1384** any number of arguments will be returned.
1385**
1386** If createFlag is false and nArg is -1, then the first valid
1387** function found is returned. A function is valid if either xFunc
1388** or xStep is non-zero.
1389*/
1390UserFunc *sqliteFindUserFunction(
1391 sqlite *db, /* An open database */
1392 const char *zName, /* Name of the function. Not null-terminated */
1393 int nName, /* Number of characters in the name */
1394 int nArg, /* Number of arguments. -1 means any number */
1395 int createFlag /* Create new entry if true and does not otherwise exist */
1396){
1397 UserFunc *pFirst, *p, *pMaybe;
1398 pFirst = p = (UserFunc*)sqliteHashFind(&db->userFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001399 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001400 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1401 return p;
1402 }
1403 pMaybe = 0;
1404 while( p && p->nArg!=nArg ){
1405 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1406 p = p->pNext;
1407 }
1408 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1409 return 0;
1410 }
1411 if( p==0 && pMaybe ){
1412 assert( createFlag==0 );
1413 return pMaybe;
1414 }
1415 if( p==0 && createFlag ){
1416 p = sqliteMalloc( sizeof(*p) );
1417 p->nArg = nArg;
1418 p->pNext = pFirst;
1419 sqliteHashInsert(&db->userFunc, zName, nName, (void*)p);
1420 }
1421 return p;
1422}