blob: 48885eac01613f33d9f495e2674d70a8fadb2823 [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**
drhf55f25f2002-02-28 01:46:11 +000015** $Id: expr.c,v 1.50 2002/02/28 01:46:12 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
drhcce7d172000-05-31 15:34:51 +0000634/*
635** Error check the functions in an expression. Make sure all
636** function names are recognized and all functions have the correct
637** number of arguments. Leave an error message in pParse->zErrMsg
638** if anything is amiss. Return the number of errors.
639**
640** if pIsAgg is not null and this expression is an aggregate function
641** (like count(*) or max(value)) then write a 1 into *pIsAgg.
642*/
643int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
644 int nErr = 0;
645 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000646 switch( pExpr->op ){
647 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000648 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
649 int no_such_func = 0;
drh8e0a2f92002-02-23 23:45:45 +0000650 int wrong_num_args = 0;
drhcce7d172000-05-31 15:34:51 +0000651 int is_agg = 0;
652 int i;
drh0bce8352002-02-28 00:41:10 +0000653 FuncDef *pDef;
654
655 pDef = sqliteFindFunction(pParse->db, pExpr->token.z, pExpr->token.n,n,0);
656 if( pDef==0 ){
657 pDef = sqliteFindFunction(pParse->db,
658 pExpr->token.z, pExpr->token.n, -1, 0);
659 if( pDef==0 ){
660 no_such_func = 1;
661 }else{
662 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000663 }
drh0bce8352002-02-28 00:41:10 +0000664 }else{
665 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000666 }
drh8e0a2f92002-02-23 23:45:45 +0000667 if( is_agg && !allowAgg ){
668 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
669 pExpr->token.z, pExpr->token.n, "()", 2, 0);
670 pParse->nErr++;
671 nErr++;
672 is_agg = 0;
673 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000674 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
675 pExpr->token.z, pExpr->token.n, 0);
676 pParse->nErr++;
677 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000678 }else if( wrong_num_args ){
679 sqliteSetNString(&pParse->zErrMsg,
680 "wrong number of arguments to function ",-1,
681 pExpr->token.z, pExpr->token.n, "()", 2, 0);
682 pParse->nErr++;
683 nErr++;
drhcce7d172000-05-31 15:34:51 +0000684 }
drh22827922000-06-06 17:27:05 +0000685 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000686 if( is_agg && pIsAgg ) *pIsAgg = 1;
687 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000688 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
689 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000690 }
691 }
692 default: {
693 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000694 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000695 }
696 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000697 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000698 }
drhfef52082000-06-06 01:50:43 +0000699 if( nErr==0 && pExpr->pList ){
700 int n = pExpr->pList->nExpr;
701 int i;
702 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000703 Expr *pE2 = pExpr->pList->a[i].pExpr;
704 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000705 }
706 }
drhcce7d172000-05-31 15:34:51 +0000707 break;
708 }
709 }
710 return nErr;
711}
712
713/*
714** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000715** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000716*/
717void sqliteExprCode(Parse *pParse, Expr *pExpr){
718 Vdbe *v = pParse->pVdbe;
719 int op;
drhdaffd0e2001-04-11 14:28:42 +0000720 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000721 switch( pExpr->op ){
722 case TK_PLUS: op = OP_Add; break;
723 case TK_MINUS: op = OP_Subtract; break;
724 case TK_STAR: op = OP_Multiply; break;
725 case TK_SLASH: op = OP_Divide; break;
726 case TK_AND: op = OP_And; break;
727 case TK_OR: op = OP_Or; break;
728 case TK_LT: op = OP_Lt; break;
729 case TK_LE: op = OP_Le; break;
730 case TK_GT: op = OP_Gt; break;
731 case TK_GE: op = OP_Ge; break;
732 case TK_NE: op = OP_Ne; break;
733 case TK_EQ: op = OP_Eq; break;
734 case TK_LIKE: op = OP_Like; break;
735 case TK_GLOB: op = OP_Glob; break;
736 case TK_ISNULL: op = OP_IsNull; break;
737 case TK_NOTNULL: op = OP_NotNull; break;
738 case TK_NOT: op = OP_Not; break;
739 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000740 case TK_BITAND: op = OP_BitAnd; break;
741 case TK_BITOR: op = OP_BitOr; break;
742 case TK_BITNOT: op = OP_BitNot; break;
743 case TK_LSHIFT: op = OP_ShiftLeft; break;
744 case TK_RSHIFT: op = OP_ShiftRight; break;
745 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000746 default: break;
747 }
748 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000749 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000750 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000751 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000752 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000753 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000754 }else{
drh99fcd712001-10-13 01:06:47 +0000755 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000756 }
drhcce7d172000-05-31 15:34:51 +0000757 break;
758 }
drhef6764a2002-01-30 04:32:00 +0000759 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +0000760 case TK_INTEGER: {
drh7a7c7392001-11-24 00:31:46 +0000761 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000762 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000763 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000764 break;
765 }
drhcce7d172000-05-31 15:34:51 +0000766 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000767 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000768 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000769 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
770 sqliteVdbeDequoteP3(v, addr);
771 break;
772 }
773 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000774 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000775 break;
776 }
777 case TK_AND:
778 case TK_OR:
779 case TK_PLUS:
780 case TK_STAR:
781 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000782 case TK_REM:
783 case TK_BITAND:
784 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000785 case TK_SLASH: {
786 sqliteExprCode(pParse, pExpr->pLeft);
787 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000788 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000789 break;
790 }
drhbf4133c2001-10-13 02:59:08 +0000791 case TK_LSHIFT:
792 case TK_RSHIFT: {
793 sqliteExprCode(pParse, pExpr->pRight);
794 sqliteExprCode(pParse, pExpr->pLeft);
795 sqliteVdbeAddOp(v, op, 0, 0);
796 break;
797 }
drh00400772000-06-16 20:51:26 +0000798 case TK_CONCAT: {
799 sqliteExprCode(pParse, pExpr->pLeft);
800 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000801 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000802 break;
803 }
drhcce7d172000-05-31 15:34:51 +0000804 case TK_LT:
805 case TK_LE:
806 case TK_GT:
807 case TK_GE:
808 case TK_NE:
809 case TK_EQ:
810 case TK_LIKE:
811 case TK_GLOB: {
812 int dest;
drh99fcd712001-10-13 01:06:47 +0000813 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000814 sqliteExprCode(pParse, pExpr->pLeft);
815 sqliteExprCode(pParse, pExpr->pRight);
816 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000817 sqliteVdbeAddOp(v, op, 0, dest);
818 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000819 break;
820 }
drhcce7d172000-05-31 15:34:51 +0000821 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000822 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000823 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000824 Token *p = &pExpr->pLeft->token;
825 char *z = sqliteMalloc( p->n + 2 );
826 sprintf(z, "-%.*s", p->n, p->z);
drh99fcd712001-10-13 01:06:47 +0000827 sqliteVdbeAddOp(v, OP_String, 0, 0);
828 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000829 sqliteFree(z);
830 break;
831 }
drh1ccde152000-06-17 13:12:39 +0000832 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000833 }
drhbf4133c2001-10-13 02:59:08 +0000834 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000835 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000836 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000837 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000838 break;
839 }
840 case TK_ISNULL:
841 case TK_NOTNULL: {
842 int dest;
drh99fcd712001-10-13 01:06:47 +0000843 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000844 sqliteExprCode(pParse, pExpr->pLeft);
845 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000846 sqliteVdbeAddOp(v, op, 0, dest);
847 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000848 break;
849 }
drh22827922000-06-06 17:27:05 +0000850 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000851 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000852 break;
853 }
drhcce7d172000-05-31 15:34:51 +0000854 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000855 int i;
856 ExprList *pList = pExpr->pList;
drh0bce8352002-02-28 00:41:10 +0000857 FuncDef *pDef;
858 pDef = sqliteFindFunction(pParse->db,
drh8e0a2f92002-02-23 23:45:45 +0000859 pExpr->token.z, pExpr->token.n, pList->nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000860 assert( pDef!=0 );
861 for(i=0; i<pList->nExpr; i++){
862 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000863 }
drh0bce8352002-02-28 00:41:10 +0000864 sqliteVdbeAddOp(v, OP_Function, pList->nExpr, 0);
865 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000866 break;
867 }
drh19a775c2000-06-05 18:54:46 +0000868 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000869 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000870 break;
871 }
drhfef52082000-06-06 01:50:43 +0000872 case TK_IN: {
873 int addr;
drh99fcd712001-10-13 01:06:47 +0000874 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000875 sqliteExprCode(pParse, pExpr->pLeft);
876 addr = sqliteVdbeCurrentAddr(v);
877 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000878 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000879 }else{
drh99fcd712001-10-13 01:06:47 +0000880 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000881 }
drh99fcd712001-10-13 01:06:47 +0000882 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000883 break;
884 }
885 case TK_BETWEEN: {
886 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000887 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000888 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000889 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000890 sqliteVdbeResolveLabel(v, lbl);
891 break;
892 }
drha2e00042002-01-22 03:13:42 +0000893 case TK_AS: {
894 sqliteExprCode(pParse, pExpr->pLeft);
895 break;
896 }
drhcce7d172000-05-31 15:34:51 +0000897 }
898 return;
899}
900
901/*
902** Generate code for a boolean expression such that a jump is made
903** to the label "dest" if the expression is true but execution
904** continues straight thru if the expression is false.
905*/
906void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
907 Vdbe *v = pParse->pVdbe;
908 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000909 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000910 switch( pExpr->op ){
911 case TK_LT: op = OP_Lt; break;
912 case TK_LE: op = OP_Le; break;
913 case TK_GT: op = OP_Gt; break;
914 case TK_GE: op = OP_Ge; break;
915 case TK_NE: op = OP_Ne; break;
916 case TK_EQ: op = OP_Eq; break;
917 case TK_LIKE: op = OP_Like; break;
918 case TK_GLOB: op = OP_Glob; break;
919 case TK_ISNULL: op = OP_IsNull; break;
920 case TK_NOTNULL: op = OP_NotNull; break;
921 default: break;
922 }
923 switch( pExpr->op ){
924 case TK_AND: {
925 int d2 = sqliteVdbeMakeLabel(v);
926 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
927 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
928 sqliteVdbeResolveLabel(v, d2);
929 break;
930 }
931 case TK_OR: {
932 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
933 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
934 break;
935 }
936 case TK_NOT: {
937 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
938 break;
939 }
940 case TK_LT:
941 case TK_LE:
942 case TK_GT:
943 case TK_GE:
944 case TK_NE:
945 case TK_EQ:
946 case TK_LIKE:
947 case TK_GLOB: {
948 sqliteExprCode(pParse, pExpr->pLeft);
949 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000950 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000951 break;
952 }
953 case TK_ISNULL:
954 case TK_NOTNULL: {
955 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000956 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000957 break;
958 }
drhfef52082000-06-06 01:50:43 +0000959 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000960 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000961 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000962 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000963 }else{
drh99fcd712001-10-13 01:06:47 +0000964 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000965 }
966 break;
967 }
968 case TK_BETWEEN: {
969 int lbl = sqliteVdbeMakeLabel(v);
970 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000971 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000972 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000973 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +0000974 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000975 sqliteVdbeAddOp(v, OP_Le, 0, dest);
976 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
977 sqliteVdbeResolveLabel(v, lbl);
978 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +0000979 break;
980 }
drhcce7d172000-05-31 15:34:51 +0000981 default: {
982 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000983 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000984 break;
985 }
986 }
987}
988
989/*
drh66b89c82000-11-28 20:47:17 +0000990** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000991** to the label "dest" if the expression is false but execution
992** continues straight thru if the expression is true.
993*/
994void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
995 Vdbe *v = pParse->pVdbe;
996 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000997 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000998 switch( pExpr->op ){
999 case TK_LT: op = OP_Ge; break;
1000 case TK_LE: op = OP_Gt; break;
1001 case TK_GT: op = OP_Le; break;
1002 case TK_GE: op = OP_Lt; break;
1003 case TK_NE: op = OP_Eq; break;
1004 case TK_EQ: op = OP_Ne; break;
1005 case TK_LIKE: op = OP_Like; break;
1006 case TK_GLOB: op = OP_Glob; break;
1007 case TK_ISNULL: op = OP_NotNull; break;
1008 case TK_NOTNULL: op = OP_IsNull; break;
1009 default: break;
1010 }
1011 switch( pExpr->op ){
1012 case TK_AND: {
1013 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1014 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1015 break;
1016 }
1017 case TK_OR: {
1018 int d2 = sqliteVdbeMakeLabel(v);
1019 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1020 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1021 sqliteVdbeResolveLabel(v, d2);
1022 break;
1023 }
1024 case TK_NOT: {
1025 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1026 break;
1027 }
1028 case TK_LT:
1029 case TK_LE:
1030 case TK_GT:
1031 case TK_GE:
1032 case TK_NE:
1033 case TK_EQ: {
1034 sqliteExprCode(pParse, pExpr->pLeft);
1035 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001036 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001037 break;
1038 }
1039 case TK_LIKE:
1040 case TK_GLOB: {
1041 sqliteExprCode(pParse, pExpr->pLeft);
1042 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001043 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001044 break;
1045 }
1046 case TK_ISNULL:
1047 case TK_NOTNULL: {
1048 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001049 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001050 break;
1051 }
drhfef52082000-06-06 01:50:43 +00001052 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001053 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001054 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001055 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001056 }else{
drh99fcd712001-10-13 01:06:47 +00001057 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001058 }
1059 break;
1060 }
1061 case TK_BETWEEN: {
1062 int addr;
1063 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001064 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001065 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1066 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001067 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1068 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1069 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001070 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001071 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001072 break;
1073 }
drhcce7d172000-05-31 15:34:51 +00001074 default: {
1075 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001076 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1077 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001078 break;
1079 }
1080 }
1081}
drh22827922000-06-06 17:27:05 +00001082
1083/*
1084** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1085** if they are identical and return FALSE if they differ in any way.
1086*/
drhd8bc7082000-06-07 23:51:50 +00001087int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001088 int i;
1089 if( pA==0 ){
1090 return pB==0;
1091 }else if( pB==0 ){
1092 return 0;
1093 }
1094 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001095 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1096 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001097 if( pA->pList ){
1098 if( pB->pList==0 ) return 0;
1099 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1100 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001101 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001102 return 0;
1103 }
1104 }
1105 }else if( pB->pList ){
1106 return 0;
1107 }
1108 if( pA->pSelect || pB->pSelect ) return 0;
1109 if( pA->token.z ){
1110 if( pB->token.z==0 ) return 0;
1111 if( pB->token.n!=pA->token.n ) return 0;
1112 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1113 }
1114 return 1;
1115}
1116
1117/*
1118** Add a new element to the pParse->aAgg[] array and return its index.
1119*/
1120static int appendAggInfo(Parse *pParse){
1121 if( (pParse->nAgg & 0x7)==0 ){
1122 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001123 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1124 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001125 return -1;
1126 }
drh6d4abfb2001-10-22 02:58:08 +00001127 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001128 }
1129 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1130 return pParse->nAgg++;
1131}
1132
1133/*
1134** Analyze the given expression looking for aggregate functions and
1135** for variables that need to be added to the pParse->aAgg[] array.
1136** Make additional entries to the pParse->aAgg[] array as necessary.
1137**
1138** This routine should only be called after the expression has been
1139** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1140**
1141** If errors are seen, leave an error message in zErrMsg and return
1142** the number of errors.
1143*/
1144int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1145 int i;
1146 AggExpr *aAgg;
1147 int nErr = 0;
1148
1149 if( pExpr==0 ) return 0;
1150 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001151 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001152 aAgg = pParse->aAgg;
1153 for(i=0; i<pParse->nAgg; i++){
1154 if( aAgg[i].isAgg ) continue;
1155 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001156 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001157 break;
1158 }
1159 }
1160 if( i>=pParse->nAgg ){
1161 i = appendAggInfo(pParse);
1162 if( i<0 ) return 1;
1163 pParse->aAgg[i].isAgg = 0;
1164 pParse->aAgg[i].pExpr = pExpr;
1165 }
drhaaf88722000-06-08 11:25:00 +00001166 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001167 break;
1168 }
1169 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001170 aAgg = pParse->aAgg;
1171 for(i=0; i<pParse->nAgg; i++){
1172 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001173 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001174 break;
1175 }
1176 }
1177 if( i>=pParse->nAgg ){
1178 i = appendAggInfo(pParse);
1179 if( i<0 ) return 1;
1180 pParse->aAgg[i].isAgg = 1;
1181 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001182 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001183 pExpr->token.z, pExpr->token.n,
1184 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001185 }
1186 pExpr->iAgg = i;
1187 break;
1188 }
1189 default: {
1190 if( pExpr->pLeft ){
1191 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1192 }
1193 if( nErr==0 && pExpr->pRight ){
1194 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1195 }
1196 if( nErr==0 && pExpr->pList ){
1197 int n = pExpr->pList->nExpr;
1198 int i;
1199 for(i=0; nErr==0 && i<n; i++){
1200 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1201 }
1202 }
1203 break;
1204 }
1205 }
1206 return nErr;
1207}
drh8e0a2f92002-02-23 23:45:45 +00001208
1209/*
1210** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001211** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001212** function, or return NULL if the function does not exist.
1213**
drh0bce8352002-02-28 00:41:10 +00001214** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001215** structure is created and liked into the "db" structure if a
1216** no matching function previously existed. When createFlag is true
1217** and the nArg parameter is -1, then only a function that accepts
1218** any number of arguments will be returned.
1219**
1220** If createFlag is false and nArg is -1, then the first valid
1221** function found is returned. A function is valid if either xFunc
1222** or xStep is non-zero.
1223*/
drh0bce8352002-02-28 00:41:10 +00001224FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001225 sqlite *db, /* An open database */
1226 const char *zName, /* Name of the function. Not null-terminated */
1227 int nName, /* Number of characters in the name */
1228 int nArg, /* Number of arguments. -1 means any number */
1229 int createFlag /* Create new entry if true and does not otherwise exist */
1230){
drh0bce8352002-02-28 00:41:10 +00001231 FuncDef *pFirst, *p, *pMaybe;
1232 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001233 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001234 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1235 return p;
1236 }
1237 pMaybe = 0;
1238 while( p && p->nArg!=nArg ){
1239 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1240 p = p->pNext;
1241 }
1242 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1243 return 0;
1244 }
1245 if( p==0 && pMaybe ){
1246 assert( createFlag==0 );
1247 return pMaybe;
1248 }
1249 if( p==0 && createFlag ){
1250 p = sqliteMalloc( sizeof(*p) );
1251 p->nArg = nArg;
1252 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001253 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001254 }
1255 return p;
1256}