blob: f265f0f4a9c9738f3e6f20fda1ca4fa4dbd00773 [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**
drh89425d52002-02-28 03:04:48 +000015** $Id: expr.c,v 1.51 2002/02/28 03:04:48 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
drh89425d52002-02-28 03:04:48 +0000655 pDef = sqliteFindFunction(pParse->db,
656 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000657 if( pDef==0 ){
658 pDef = sqliteFindFunction(pParse->db,
659 pExpr->token.z, pExpr->token.n, -1, 0);
660 if( pDef==0 ){
661 no_such_func = 1;
662 }else{
663 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000664 }
drh0bce8352002-02-28 00:41:10 +0000665 }else{
666 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000667 }
drh8e0a2f92002-02-23 23:45:45 +0000668 if( is_agg && !allowAgg ){
669 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
670 pExpr->token.z, pExpr->token.n, "()", 2, 0);
671 pParse->nErr++;
672 nErr++;
673 is_agg = 0;
674 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000675 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
676 pExpr->token.z, pExpr->token.n, 0);
677 pParse->nErr++;
678 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000679 }else if( wrong_num_args ){
680 sqliteSetNString(&pParse->zErrMsg,
681 "wrong number of arguments to function ",-1,
682 pExpr->token.z, pExpr->token.n, "()", 2, 0);
683 pParse->nErr++;
684 nErr++;
drhcce7d172000-05-31 15:34:51 +0000685 }
drh22827922000-06-06 17:27:05 +0000686 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000687 if( is_agg && pIsAgg ) *pIsAgg = 1;
688 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000689 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
690 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000691 }
692 }
693 default: {
694 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000695 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000696 }
697 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000698 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000699 }
drhfef52082000-06-06 01:50:43 +0000700 if( nErr==0 && pExpr->pList ){
701 int n = pExpr->pList->nExpr;
702 int i;
703 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000704 Expr *pE2 = pExpr->pList->a[i].pExpr;
705 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000706 }
707 }
drhcce7d172000-05-31 15:34:51 +0000708 break;
709 }
710 }
711 return nErr;
712}
713
714/*
715** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000716** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000717*/
718void sqliteExprCode(Parse *pParse, Expr *pExpr){
719 Vdbe *v = pParse->pVdbe;
720 int op;
drhdaffd0e2001-04-11 14:28:42 +0000721 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000722 switch( pExpr->op ){
723 case TK_PLUS: op = OP_Add; break;
724 case TK_MINUS: op = OP_Subtract; break;
725 case TK_STAR: op = OP_Multiply; break;
726 case TK_SLASH: op = OP_Divide; break;
727 case TK_AND: op = OP_And; break;
728 case TK_OR: op = OP_Or; break;
729 case TK_LT: op = OP_Lt; break;
730 case TK_LE: op = OP_Le; break;
731 case TK_GT: op = OP_Gt; break;
732 case TK_GE: op = OP_Ge; break;
733 case TK_NE: op = OP_Ne; break;
734 case TK_EQ: op = OP_Eq; break;
735 case TK_LIKE: op = OP_Like; break;
736 case TK_GLOB: op = OP_Glob; break;
737 case TK_ISNULL: op = OP_IsNull; break;
738 case TK_NOTNULL: op = OP_NotNull; break;
739 case TK_NOT: op = OP_Not; break;
740 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000741 case TK_BITAND: op = OP_BitAnd; break;
742 case TK_BITOR: op = OP_BitOr; break;
743 case TK_BITNOT: op = OP_BitNot; break;
744 case TK_LSHIFT: op = OP_ShiftLeft; break;
745 case TK_RSHIFT: op = OP_ShiftRight; break;
746 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000747 default: break;
748 }
749 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000750 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000751 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000752 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000753 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000754 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000755 }else{
drh99fcd712001-10-13 01:06:47 +0000756 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000757 }
drhcce7d172000-05-31 15:34:51 +0000758 break;
759 }
drhef6764a2002-01-30 04:32:00 +0000760 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +0000761 case TK_INTEGER: {
drh7a7c7392001-11-24 00:31:46 +0000762 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000763 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000764 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000765 break;
766 }
drhcce7d172000-05-31 15:34:51 +0000767 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000768 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000769 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000770 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
771 sqliteVdbeDequoteP3(v, addr);
772 break;
773 }
774 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000775 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000776 break;
777 }
778 case TK_AND:
779 case TK_OR:
780 case TK_PLUS:
781 case TK_STAR:
782 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000783 case TK_REM:
784 case TK_BITAND:
785 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000786 case TK_SLASH: {
787 sqliteExprCode(pParse, pExpr->pLeft);
788 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000789 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000790 break;
791 }
drhbf4133c2001-10-13 02:59:08 +0000792 case TK_LSHIFT:
793 case TK_RSHIFT: {
794 sqliteExprCode(pParse, pExpr->pRight);
795 sqliteExprCode(pParse, pExpr->pLeft);
796 sqliteVdbeAddOp(v, op, 0, 0);
797 break;
798 }
drh00400772000-06-16 20:51:26 +0000799 case TK_CONCAT: {
800 sqliteExprCode(pParse, pExpr->pLeft);
801 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000802 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000803 break;
804 }
drhcce7d172000-05-31 15:34:51 +0000805 case TK_LT:
806 case TK_LE:
807 case TK_GT:
808 case TK_GE:
809 case TK_NE:
810 case TK_EQ:
811 case TK_LIKE:
812 case TK_GLOB: {
813 int dest;
drh99fcd712001-10-13 01:06:47 +0000814 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000815 sqliteExprCode(pParse, pExpr->pLeft);
816 sqliteExprCode(pParse, pExpr->pRight);
817 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000818 sqliteVdbeAddOp(v, op, 0, dest);
819 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000820 break;
821 }
drhcce7d172000-05-31 15:34:51 +0000822 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000823 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000824 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000825 Token *p = &pExpr->pLeft->token;
826 char *z = sqliteMalloc( p->n + 2 );
827 sprintf(z, "-%.*s", p->n, p->z);
drh99fcd712001-10-13 01:06:47 +0000828 sqliteVdbeAddOp(v, OP_String, 0, 0);
829 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000830 sqliteFree(z);
831 break;
832 }
drh1ccde152000-06-17 13:12:39 +0000833 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000834 }
drhbf4133c2001-10-13 02:59:08 +0000835 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000836 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000837 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000838 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000839 break;
840 }
841 case TK_ISNULL:
842 case TK_NOTNULL: {
843 int dest;
drh99fcd712001-10-13 01:06:47 +0000844 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000845 sqliteExprCode(pParse, pExpr->pLeft);
846 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000847 sqliteVdbeAddOp(v, op, 0, dest);
848 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000849 break;
850 }
drh22827922000-06-06 17:27:05 +0000851 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000852 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +0000853 break;
854 }
drhcce7d172000-05-31 15:34:51 +0000855 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +0000856 int i;
857 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +0000858 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +0000859 FuncDef *pDef;
860 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +0000861 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000862 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +0000863 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +0000864 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +0000865 }
drh89425d52002-02-28 03:04:48 +0000866 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +0000867 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +0000868 break;
869 }
drh19a775c2000-06-05 18:54:46 +0000870 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000871 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000872 break;
873 }
drhfef52082000-06-06 01:50:43 +0000874 case TK_IN: {
875 int addr;
drh99fcd712001-10-13 01:06:47 +0000876 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000877 sqliteExprCode(pParse, pExpr->pLeft);
878 addr = sqliteVdbeCurrentAddr(v);
879 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000880 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000881 }else{
drh99fcd712001-10-13 01:06:47 +0000882 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000883 }
drh99fcd712001-10-13 01:06:47 +0000884 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000885 break;
886 }
887 case TK_BETWEEN: {
888 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000889 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000890 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000891 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000892 sqliteVdbeResolveLabel(v, lbl);
893 break;
894 }
drha2e00042002-01-22 03:13:42 +0000895 case TK_AS: {
896 sqliteExprCode(pParse, pExpr->pLeft);
897 break;
898 }
drhcce7d172000-05-31 15:34:51 +0000899 }
900 return;
901}
902
903/*
904** Generate code for a boolean expression such that a jump is made
905** to the label "dest" if the expression is true but execution
906** continues straight thru if the expression is false.
907*/
908void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
909 Vdbe *v = pParse->pVdbe;
910 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000911 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000912 switch( pExpr->op ){
913 case TK_LT: op = OP_Lt; break;
914 case TK_LE: op = OP_Le; break;
915 case TK_GT: op = OP_Gt; break;
916 case TK_GE: op = OP_Ge; break;
917 case TK_NE: op = OP_Ne; break;
918 case TK_EQ: op = OP_Eq; break;
919 case TK_LIKE: op = OP_Like; break;
920 case TK_GLOB: op = OP_Glob; break;
921 case TK_ISNULL: op = OP_IsNull; break;
922 case TK_NOTNULL: op = OP_NotNull; break;
923 default: break;
924 }
925 switch( pExpr->op ){
926 case TK_AND: {
927 int d2 = sqliteVdbeMakeLabel(v);
928 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
929 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
930 sqliteVdbeResolveLabel(v, d2);
931 break;
932 }
933 case TK_OR: {
934 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
935 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
936 break;
937 }
938 case TK_NOT: {
939 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
940 break;
941 }
942 case TK_LT:
943 case TK_LE:
944 case TK_GT:
945 case TK_GE:
946 case TK_NE:
947 case TK_EQ:
948 case TK_LIKE:
949 case TK_GLOB: {
950 sqliteExprCode(pParse, pExpr->pLeft);
951 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000952 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000953 break;
954 }
955 case TK_ISNULL:
956 case TK_NOTNULL: {
957 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000958 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000959 break;
960 }
drhfef52082000-06-06 01:50:43 +0000961 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000962 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000963 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000964 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000965 }else{
drh99fcd712001-10-13 01:06:47 +0000966 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000967 }
968 break;
969 }
970 case TK_BETWEEN: {
971 int lbl = sqliteVdbeMakeLabel(v);
972 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000973 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000974 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000975 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +0000976 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000977 sqliteVdbeAddOp(v, OP_Le, 0, dest);
978 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
979 sqliteVdbeResolveLabel(v, lbl);
980 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +0000981 break;
982 }
drhcce7d172000-05-31 15:34:51 +0000983 default: {
984 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000985 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000986 break;
987 }
988 }
989}
990
991/*
drh66b89c82000-11-28 20:47:17 +0000992** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000993** to the label "dest" if the expression is false but execution
994** continues straight thru if the expression is true.
995*/
996void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
997 Vdbe *v = pParse->pVdbe;
998 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000999 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001000 switch( pExpr->op ){
1001 case TK_LT: op = OP_Ge; break;
1002 case TK_LE: op = OP_Gt; break;
1003 case TK_GT: op = OP_Le; break;
1004 case TK_GE: op = OP_Lt; break;
1005 case TK_NE: op = OP_Eq; break;
1006 case TK_EQ: op = OP_Ne; break;
1007 case TK_LIKE: op = OP_Like; break;
1008 case TK_GLOB: op = OP_Glob; break;
1009 case TK_ISNULL: op = OP_NotNull; break;
1010 case TK_NOTNULL: op = OP_IsNull; break;
1011 default: break;
1012 }
1013 switch( pExpr->op ){
1014 case TK_AND: {
1015 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
1016 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1017 break;
1018 }
1019 case TK_OR: {
1020 int d2 = sqliteVdbeMakeLabel(v);
1021 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
1022 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
1023 sqliteVdbeResolveLabel(v, d2);
1024 break;
1025 }
1026 case TK_NOT: {
1027 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
1028 break;
1029 }
1030 case TK_LT:
1031 case TK_LE:
1032 case TK_GT:
1033 case TK_GE:
1034 case TK_NE:
1035 case TK_EQ: {
1036 sqliteExprCode(pParse, pExpr->pLeft);
1037 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001038 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001039 break;
1040 }
1041 case TK_LIKE:
1042 case TK_GLOB: {
1043 sqliteExprCode(pParse, pExpr->pLeft);
1044 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001045 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001046 break;
1047 }
1048 case TK_ISNULL:
1049 case TK_NOTNULL: {
1050 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001051 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001052 break;
1053 }
drhfef52082000-06-06 01:50:43 +00001054 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +00001055 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00001056 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001057 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001058 }else{
drh99fcd712001-10-13 01:06:47 +00001059 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001060 }
1061 break;
1062 }
1063 case TK_BETWEEN: {
1064 int addr;
1065 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001066 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001067 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1068 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +00001069 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1070 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1071 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001072 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +00001073 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +00001074 break;
1075 }
drhcce7d172000-05-31 15:34:51 +00001076 default: {
1077 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +00001078 sqliteVdbeAddOp(v, OP_Not, 0, 0);
1079 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +00001080 break;
1081 }
1082 }
1083}
drh22827922000-06-06 17:27:05 +00001084
1085/*
1086** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1087** if they are identical and return FALSE if they differ in any way.
1088*/
drhd8bc7082000-06-07 23:51:50 +00001089int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001090 int i;
1091 if( pA==0 ){
1092 return pB==0;
1093 }else if( pB==0 ){
1094 return 0;
1095 }
1096 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001097 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1098 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001099 if( pA->pList ){
1100 if( pB->pList==0 ) return 0;
1101 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1102 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001103 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001104 return 0;
1105 }
1106 }
1107 }else if( pB->pList ){
1108 return 0;
1109 }
1110 if( pA->pSelect || pB->pSelect ) return 0;
1111 if( pA->token.z ){
1112 if( pB->token.z==0 ) return 0;
1113 if( pB->token.n!=pA->token.n ) return 0;
1114 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1115 }
1116 return 1;
1117}
1118
1119/*
1120** Add a new element to the pParse->aAgg[] array and return its index.
1121*/
1122static int appendAggInfo(Parse *pParse){
1123 if( (pParse->nAgg & 0x7)==0 ){
1124 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001125 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1126 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001127 return -1;
1128 }
drh6d4abfb2001-10-22 02:58:08 +00001129 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001130 }
1131 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1132 return pParse->nAgg++;
1133}
1134
1135/*
1136** Analyze the given expression looking for aggregate functions and
1137** for variables that need to be added to the pParse->aAgg[] array.
1138** Make additional entries to the pParse->aAgg[] array as necessary.
1139**
1140** This routine should only be called after the expression has been
1141** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1142**
1143** If errors are seen, leave an error message in zErrMsg and return
1144** the number of errors.
1145*/
1146int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1147 int i;
1148 AggExpr *aAgg;
1149 int nErr = 0;
1150
1151 if( pExpr==0 ) return 0;
1152 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001153 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001154 aAgg = pParse->aAgg;
1155 for(i=0; i<pParse->nAgg; i++){
1156 if( aAgg[i].isAgg ) continue;
1157 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001158 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001159 break;
1160 }
1161 }
1162 if( i>=pParse->nAgg ){
1163 i = appendAggInfo(pParse);
1164 if( i<0 ) return 1;
1165 pParse->aAgg[i].isAgg = 0;
1166 pParse->aAgg[i].pExpr = pExpr;
1167 }
drhaaf88722000-06-08 11:25:00 +00001168 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001169 break;
1170 }
1171 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001172 aAgg = pParse->aAgg;
1173 for(i=0; i<pParse->nAgg; i++){
1174 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001175 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001176 break;
1177 }
1178 }
1179 if( i>=pParse->nAgg ){
1180 i = appendAggInfo(pParse);
1181 if( i<0 ) return 1;
1182 pParse->aAgg[i].isAgg = 1;
1183 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001184 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001185 pExpr->token.z, pExpr->token.n,
1186 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001187 }
1188 pExpr->iAgg = i;
1189 break;
1190 }
1191 default: {
1192 if( pExpr->pLeft ){
1193 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1194 }
1195 if( nErr==0 && pExpr->pRight ){
1196 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1197 }
1198 if( nErr==0 && pExpr->pList ){
1199 int n = pExpr->pList->nExpr;
1200 int i;
1201 for(i=0; nErr==0 && i<n; i++){
1202 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1203 }
1204 }
1205 break;
1206 }
1207 }
1208 return nErr;
1209}
drh8e0a2f92002-02-23 23:45:45 +00001210
1211/*
1212** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001213** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001214** function, or return NULL if the function does not exist.
1215**
drh0bce8352002-02-28 00:41:10 +00001216** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001217** structure is created and liked into the "db" structure if a
1218** no matching function previously existed. When createFlag is true
1219** and the nArg parameter is -1, then only a function that accepts
1220** any number of arguments will be returned.
1221**
1222** If createFlag is false and nArg is -1, then the first valid
1223** function found is returned. A function is valid if either xFunc
1224** or xStep is non-zero.
1225*/
drh0bce8352002-02-28 00:41:10 +00001226FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001227 sqlite *db, /* An open database */
1228 const char *zName, /* Name of the function. Not null-terminated */
1229 int nName, /* Number of characters in the name */
1230 int nArg, /* Number of arguments. -1 means any number */
1231 int createFlag /* Create new entry if true and does not otherwise exist */
1232){
drh0bce8352002-02-28 00:41:10 +00001233 FuncDef *pFirst, *p, *pMaybe;
1234 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001235 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001236 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1237 return p;
1238 }
1239 pMaybe = 0;
1240 while( p && p->nArg!=nArg ){
1241 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1242 p = p->pNext;
1243 }
1244 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1245 return 0;
1246 }
1247 if( p==0 && pMaybe ){
1248 assert( createFlag==0 );
1249 return pMaybe;
1250 }
drh89425d52002-02-28 03:04:48 +00001251 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001252 p->nArg = nArg;
1253 p->pNext = pFirst;
drh0bce8352002-02-28 00:41:10 +00001254 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001255 }
1256 return p;
1257}