blob: 4893d4a86320f684f8ae64ca4e71e08f45d3a1e5 [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**
drhb1363202002-06-26 02:45:03 +000015** $Id: expr.c,v 1.74 2002/06/26 02:45:04 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
20/*
drha76b5df2002-02-23 02:32:10 +000021** Construct a new expression node and return a pointer to it. Memory
22** for this node is obtained from sqliteMalloc(). The calling function
23** is responsible for making sure the node eventually gets freed.
24*/
25Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
26 Expr *pNew;
27 pNew = sqliteMalloc( sizeof(Expr) );
28 if( pNew==0 ){
29 sqliteExprDelete(pLeft);
30 sqliteExprDelete(pRight);
31 return 0;
32 }
33 pNew->op = op;
34 pNew->pLeft = pLeft;
35 pNew->pRight = pRight;
36 if( pToken ){
37 pNew->token = *pToken;
38 }else{
39 pNew->token.z = 0;
40 pNew->token.n = 0;
41 }
42 if( pLeft && pRight ){
43 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
44 }else{
45 pNew->span = pNew->token;
46 }
47 return pNew;
48}
49
50/*
51** Set the Expr.token field of the given expression to span all
52** text between the two given tokens.
53*/
54void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
55 if( pExpr ){
56 pExpr->span.z = pLeft->z;
57 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
58 }
59}
60
61/*
62** Construct a new expression node for a function with multiple
63** arguments.
64*/
65Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
66 Expr *pNew;
67 pNew = sqliteMalloc( sizeof(Expr) );
68 if( pNew==0 ){
69 sqliteExprListDelete(pList);
70 return 0;
71 }
72 pNew->op = TK_FUNCTION;
73 pNew->pList = pList;
74 if( pToken ){
75 pNew->token = *pToken;
76 }else{
77 pNew->token.z = 0;
78 pNew->token.n = 0;
79 }
80 return pNew;
81}
82
83/*
drha2e00042002-01-22 03:13:42 +000084** Recursively delete an expression tree.
85*/
86void sqliteExprDelete(Expr *p){
87 if( p==0 ) return;
drh75148a22002-03-03 03:42:31 +000088 if( p->pLeft ) sqliteExprDelete(p->pLeft);
89 if( p->pRight ) sqliteExprDelete(p->pRight);
drha2e00042002-01-22 03:13:42 +000090 if( p->pList ) sqliteExprListDelete(p->pList);
91 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
92 sqliteFree(p);
93}
94
drhcce7d172000-05-31 15:34:51 +000095/*
drha76b5df2002-02-23 02:32:10 +000096** The following group of functions are used to translate the string
97** pointers of tokens in expression from one buffer to another.
98**
99** Normally, the Expr.token.z and Expr.span.z fields point into the
100** original input buffer of an SQL statement. This is usually OK
101** since the SQL statement is executed and the expression is deleted
102** before the input buffer is freed. Making the tokens point to the
103** original input buffer saves many calls to malloc() and thus helps
104** the library to run faster.
105**
106** But sometimes we need an expression to persist past the time when
107** the input buffer is freed. (Example: The SELECT clause of a
108** CREATE VIEW statement contains expressions that must persist for
109** the life of the view.) When that happens we have to make a
110** persistent copy of the input buffer and translate the Expr.token.z
111** and Expr.span.z fields to point to the copy rather than the
drha2ed5602002-02-26 23:55:31 +0000112** original input buffer. The following group of routines handle that
drha76b5df2002-02-23 02:32:10 +0000113** translation.
114**
115** The "offset" parameter is the distance from the original input buffer
116** to the persistent copy. These routines recursively walk the entire
117** expression tree and shift all tokens by "offset" amount.
118**
119** The work of figuring out the appropriate "offset" and making the
120** presistent copy of the input buffer is done by the calling routine.
121*/
122void sqliteExprMoveStrings(Expr *p, int offset){
123 if( p==0 ) return;
124 if( p->token.z ) p->token.z += offset;
125 if( p->span.z ) p->span.z += offset;
126 if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset);
127 if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset);
128 if( p->pList ) sqliteExprListMoveStrings(p->pList, offset);
129 if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset);
130}
131void sqliteExprListMoveStrings(ExprList *pList, int offset){
132 int i;
133 if( pList==0 ) return;
134 for(i=0; i<pList->nExpr; i++){
135 sqliteExprMoveStrings(pList->a[i].pExpr, offset);
136 }
137}
138void sqliteSelectMoveStrings(Select *pSelect, int offset){
139 if( pSelect==0 ) return;
140 sqliteExprListMoveStrings(pSelect->pEList, offset);
141 sqliteExprMoveStrings(pSelect->pWhere, offset);
142 sqliteExprListMoveStrings(pSelect->pGroupBy, offset);
143 sqliteExprMoveStrings(pSelect->pHaving, offset);
144 sqliteExprListMoveStrings(pSelect->pOrderBy, offset);
145 sqliteSelectMoveStrings(pSelect->pPrior, offset);
146}
147
148/*
drhff78bd22002-02-27 01:47:11 +0000149** The following group of routines make deep copies of expressions,
150** expression lists, ID lists, and select statements. The copies can
151** be deleted (by being passed to their respective ...Delete() routines)
152** without effecting the originals.
153**
154** Note, however, that the Expr.token.z and Expr.span.z fields point to
155** string space that is allocated separately from the expression tree
156** itself. These routines do NOT duplicate that string space.
157**
drhad3cab52002-05-24 02:04:32 +0000158** The expression list, ID, and source lists return by sqliteExprListDup(),
159** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
160** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000161**
drhad3cab52002-05-24 02:04:32 +0000162** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000163*/
164Expr *sqliteExprDup(Expr *p){
165 Expr *pNew;
166 if( p==0 ) return 0;
167 pNew = sqliteMalloc( sizeof(*p) );
168 if( pNew==0 ) return 0;
169 pNew->op = p->op;
drh8e2ca022002-06-17 17:07:19 +0000170 pNew->dataType = p->dataType;
drhff78bd22002-02-27 01:47:11 +0000171 pNew->pLeft = sqliteExprDup(p->pLeft);
172 pNew->pRight = sqliteExprDup(p->pRight);
173 pNew->pList = sqliteExprListDup(p->pList);
drh832508b2002-03-02 17:04:07 +0000174 pNew->iTable = p->iTable;
175 pNew->iColumn = p->iColumn;
176 pNew->iAgg = p->iAgg;
drhff78bd22002-02-27 01:47:11 +0000177 pNew->token = p->token;
178 pNew->span = p->span;
179 pNew->pSelect = sqliteSelectDup(p->pSelect);
180 return pNew;
181}
182ExprList *sqliteExprListDup(ExprList *p){
183 ExprList *pNew;
184 int i;
185 if( p==0 ) return 0;
186 pNew = sqliteMalloc( sizeof(*pNew) );
187 if( pNew==0 ) return 0;
188 pNew->nExpr = p->nExpr;
189 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000190 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000191 for(i=0; i<p->nExpr; i++){
192 pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
193 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
194 pNew->a[i].sortOrder = p->a[i].sortOrder;
195 pNew->a[i].isAgg = p->a[i].isAgg;
196 pNew->a[i].done = 0;
197 }
198 return pNew;
199}
drhad3cab52002-05-24 02:04:32 +0000200SrcList *sqliteSrcListDup(SrcList *p){
201 SrcList *pNew;
202 int i;
203 if( p==0 ) return 0;
204 pNew = sqliteMalloc( sizeof(*pNew) );
205 if( pNew==0 ) return 0;
206 pNew->nSrc = p->nSrc;
207 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
danielk19776f349032002-06-11 02:25:40 +0000208 if( pNew->a==0 && p->nSrc != 0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000209 for(i=0; i<p->nSrc; i++){
210 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
211 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
212 pNew->a[i].jointype = p->a[i].jointype;
213 pNew->a[i].pTab = 0;
214 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
215 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
216 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
217 }
218 return pNew;
219}
drhff78bd22002-02-27 01:47:11 +0000220IdList *sqliteIdListDup(IdList *p){
221 IdList *pNew;
222 int i;
223 if( p==0 ) return 0;
224 pNew = sqliteMalloc( sizeof(*pNew) );
225 if( pNew==0 ) return 0;
226 pNew->nId = p->nId;
227 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000228 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000229 for(i=0; i<p->nId; i++){
230 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000231 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000232 }
233 return pNew;
234}
235Select *sqliteSelectDup(Select *p){
236 Select *pNew;
237 if( p==0 ) return 0;
238 pNew = sqliteMalloc( sizeof(*p) );
239 if( pNew==0 ) return 0;
240 pNew->isDistinct = p->isDistinct;
241 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000242 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000243 pNew->pWhere = sqliteExprDup(p->pWhere);
244 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
245 pNew->pHaving = sqliteExprDup(p->pHaving);
246 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
247 pNew->op = p->op;
248 pNew->pPrior = sqliteSelectDup(p->pPrior);
249 pNew->nLimit = p->nLimit;
250 pNew->nOffset = p->nOffset;
251 pNew->zSelect = 0;
252 return pNew;
253}
254
255
256/*
drha76b5df2002-02-23 02:32:10 +0000257** Add a new element to the end of an expression list. If pList is
258** initially NULL, then create a new expression list.
259*/
260ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
261 int i;
262 if( pList==0 ){
263 pList = sqliteMalloc( sizeof(ExprList) );
264 if( pList==0 ){
265 sqliteExprDelete(pExpr);
266 return 0;
267 }
268 }
269 if( (pList->nExpr & 7)==0 ){
270 int n = pList->nExpr + 8;
271 struct ExprList_item *a;
272 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
273 if( a==0 ){
274 sqliteExprDelete(pExpr);
275 return pList;
276 }
277 pList->a = a;
278 }
279 if( pExpr || pName ){
280 i = pList->nExpr++;
281 pList->a[i].pExpr = pExpr;
282 pList->a[i].zName = 0;
283 if( pName ){
284 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
285 sqliteDequote(pList->a[i].zName);
286 }
287 }
288 return pList;
289}
290
291/*
292** Delete an entire expression list.
293*/
294void sqliteExprListDelete(ExprList *pList){
295 int i;
296 if( pList==0 ) return;
297 for(i=0; i<pList->nExpr; i++){
298 sqliteExprDelete(pList->a[i].pExpr);
299 sqliteFree(pList->a[i].zName);
300 }
301 sqliteFree(pList->a);
302 sqliteFree(pList);
303}
304
305/*
drhfef52082000-06-06 01:50:43 +0000306** Walk an expression tree. Return 1 if the expression is constant
307** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000308**
309** For the purposes of this function, a double-quoted string (ex: "abc")
310** is considered a variable but a single-quoted string (ex: 'abc') is
311** a constant.
drhfef52082000-06-06 01:50:43 +0000312*/
drh92086432002-01-22 14:11:29 +0000313int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000314 switch( p->op ){
315 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000316 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000317 case TK_DOT:
318 return 0;
drh23989372002-05-21 13:43:04 +0000319 case TK_STRING:
320 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000321 case TK_INTEGER:
322 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000323 return 1;
drhfef52082000-06-06 01:50:43 +0000324 default: {
drh92086432002-01-22 14:11:29 +0000325 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
326 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000327 if( p->pList ){
328 int i;
329 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000330 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000331 }
332 }
drh92086432002-01-22 14:11:29 +0000333 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000334 }
335 }
drh92086432002-01-22 14:11:29 +0000336 return 0;
drhfef52082000-06-06 01:50:43 +0000337}
338
339/*
drhe4de1fe2002-06-02 16:09:01 +0000340** If the given expression codes a constant integer, return 1 and put
341** the value of the integer in *pValue. If the expression is not an
342** integer, return 0 and leave *pValue unchanged.
343*/
344int sqliteExprIsInteger(Expr *p, int *pValue){
345 switch( p->op ){
346 case TK_INTEGER: {
347 *pValue = atoi(p->token.z);
348 return 1;
349 }
350 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000351 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000352 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000353 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000354 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
355 if( n==0 ){
356 *pValue = atoi(p->token.z);
357 return 1;
358 }
359 break;
360 }
361 case TK_UMINUS: {
362 int v;
363 if( sqliteExprIsInteger(p->pLeft, &v) ){
364 *pValue = -v;
365 return 1;
366 }
367 break;
368 }
369 default: break;
370 }
371 return 0;
372}
373
374/*
drhc4a3c772001-04-04 11:48:57 +0000375** Return TRUE if the given string is a row-id column name.
376*/
377static int sqliteIsRowid(const char *z){
378 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
379 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
380 if( sqliteStrICmp(z, "OID")==0 ) return 1;
381 return 0;
382}
383
384/*
drhcce7d172000-05-31 15:34:51 +0000385** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000386** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000387** index to the table in the table list and a column offset. The
388** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
389** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000390** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000391** VDBE cursor number for a cursor that is pointing into the referenced
392** table. The Expr.iColumn value is changed to the index of the column
393** of the referenced table. The Expr.iColumn value for the special
394** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
395** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000396**
drhfef52082000-06-06 01:50:43 +0000397** We also check for instances of the IN operator. IN comes in two
398** forms:
399**
400** expr IN (exprlist)
401** and
402** expr IN (SELECT ...)
403**
404** The first form is handled by creating a set holding the list
405** of allowed values. The second form causes the SELECT to generate
406** a temporary table.
407**
408** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000409** If it finds any, it generates code to write the value of that select
410** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000411**
drh967e8b72000-06-21 13:59:10 +0000412** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000413** the number of errors seen and leaves an error message on pParse->zErrMsg.
414*/
drha2e00042002-01-22 03:13:42 +0000415int sqliteExprResolveIds(
416 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000417 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000418 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000419 ExprList *pEList, /* List of expressions used to resolve "AS" */
420 Expr *pExpr /* The expression to be analyzed. */
421){
drhdaffd0e2001-04-11 14:28:42 +0000422 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000423 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000424 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000425 /* Double-quoted strings (ex: "abc") are used as identifiers if
426 ** possible. Otherwise they remain as strings. Single-quoted
427 ** strings (ex: 'abc') are always string literals.
428 */
429 case TK_STRING: {
430 if( pExpr->token.z[0]=='\'' ) break;
431 /* Fall thru into the TK_ID case if this is a double-quoted string */
432 }
drha2e00042002-01-22 03:13:42 +0000433 /* A lone identifier. Try and match it as follows:
434 **
435 ** 1. To the name of a column of one of the tables in pTabList
436 **
437 ** 2. To the right side of an AS keyword in the column list of
438 ** a SELECT statement. (For example, match against 'x' in
439 ** "SELECT a+b AS 'x' FROM t1".)
440 **
441 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
442 */
drhcce7d172000-05-31 15:34:51 +0000443 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000444 int cnt = 0; /* Number of matches */
445 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000446 char *z;
447 assert( pExpr->token.z );
448 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000449 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000450 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000451 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000452 int j;
453 Table *pTab = pTabList->a[i].pTab;
454 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000455 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000456 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000457 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000458 cnt++;
drh832508b2002-03-02 17:04:07 +0000459 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000460 if( j==pTab->iPKey ){
461 /* Substitute the record number for the INTEGER PRIMARY KEY */
462 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000463 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000464 }else{
465 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000466 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000467 }
drha2e00042002-01-22 03:13:42 +0000468 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000469 }
470 }
471 }
drha2e00042002-01-22 03:13:42 +0000472 if( cnt==0 && pEList!=0 ){
473 int j;
474 for(j=0; j<pEList->nExpr; j++){
475 char *zAs = pEList->a[j].zName;
476 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
477 cnt++;
478 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
479 pExpr->op = TK_AS;
480 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000481 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000482 }
483 }
484 }
drhc4a3c772001-04-04 11:48:57 +0000485 if( cnt==0 && sqliteIsRowid(z) ){
486 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000487 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000488 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000489 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000490 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000491 }
drhcce7d172000-05-31 15:34:51 +0000492 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000493 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000494 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000495 pExpr->token.z, pExpr->token.n, 0);
496 pParse->nErr++;
497 return 1;
498 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000499 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000500 pExpr->token.z, pExpr->token.n, 0);
501 pParse->nErr++;
502 return 1;
503 }
drhcce7d172000-05-31 15:34:51 +0000504 break;
505 }
506
drh967e8b72000-06-21 13:59:10 +0000507 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000508 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000509 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000510 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000511 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000512 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000513 char *zLeft, *zRight; /* Text of an identifier */
514
515 pLeft = pExpr->pLeft;
516 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000517 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
518 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000519 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
520 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000521 if( zLeft==0 || zRight==0 ){
522 sqliteFree(zLeft);
523 sqliteFree(zRight);
524 return 1;
525 }
drh87c40e82001-07-23 14:33:02 +0000526 sqliteDequote(zLeft);
527 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000528 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000529 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000530 int j;
531 char *zTab;
532 Table *pTab = pTabList->a[i].pTab;
533 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000534 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000535 if( pTabList->a[i].zAlias ){
536 zTab = pTabList->a[i].zAlias;
537 }else{
538 zTab = pTab->zName;
539 }
drh094b2bb2002-03-13 18:54:07 +0000540 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000541 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000542 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000543 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000544 cnt++;
drh832508b2002-03-02 17:04:07 +0000545 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000546 if( j==pTab->iPKey ){
547 /* Substitute the record number for the INTEGER PRIMARY KEY */
548 pExpr->iColumn = -1;
549 }else{
550 pExpr->iColumn = j;
551 }
drhc9b84a12002-06-20 11:36:48 +0000552 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000553 }
554 }
555 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000556
557 /* If we have not already resolved this *.* expression, then maybe
558 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000559 if( cnt == 0 && pParse->trigStack != 0 ){
560 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000561 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000562 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
563 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000564 cntTab++;
565 t = 1;
566 }
danielk1977f29ce552002-05-19 23:43:12 +0000567 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
568 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000569 cntTab++;
570 t = 1;
571 }
572
danielk1977f29ce552002-05-19 23:43:12 +0000573 if( t ){
574 int j;
drhc9b84a12002-06-20 11:36:48 +0000575 Table *pTab = pTriggerStack->pTab;
576 for(j=0; j < pTab->nCol; j++) {
577 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000578 cnt++;
579 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000580 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000581 }
582 }
danielk1977f29ce552002-05-19 23:43:12 +0000583 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000584 }
585
drhc4a3c772001-04-04 11:48:57 +0000586 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
587 cnt = 1;
588 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000589 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000590 }
drhcce7d172000-05-31 15:34:51 +0000591 sqliteFree(zLeft);
592 sqliteFree(zRight);
593 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000594 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000595 pLeft->token.z, pLeft->token.n, ".", 1,
596 pRight->token.z, pRight->token.n, 0);
597 pParse->nErr++;
598 return 1;
599 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000600 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000601 pLeft->token.z, pLeft->token.n, ".", 1,
602 pRight->token.z, pRight->token.n, 0);
603 pParse->nErr++;
604 return 1;
605 }
606 sqliteExprDelete(pLeft);
607 pExpr->pLeft = 0;
608 sqliteExprDelete(pRight);
609 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000610 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000611 break;
612 }
613
drhfef52082000-06-06 01:50:43 +0000614 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000615 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000616 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000617 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000618 return 1;
619 }
drhfef52082000-06-06 01:50:43 +0000620 if( pExpr->pSelect ){
621 /* Case 1: expr IN (SELECT ...)
622 **
623 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000624 ** table. The cursor number of the temporary table has already
625 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000626 */
drh832508b2002-03-02 17:04:07 +0000627 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000628 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000629 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000630 }else if( pExpr->pList ){
631 /* Case 2: expr IN (exprlist)
632 **
633 ** Create a set to put the exprlist values in. The Set id is stored
634 ** in iTable.
635 */
636 int i, iSet;
637 for(i=0; i<pExpr->pList->nExpr; i++){
638 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000639 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000640 sqliteSetString(&pParse->zErrMsg,
641 "right-hand side of IN operator must be constant", 0);
642 pParse->nErr++;
643 return 1;
644 }
drh4794b982000-06-06 13:54:14 +0000645 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
646 return 1;
647 }
drhfef52082000-06-06 01:50:43 +0000648 }
649 iSet = pExpr->iTable = pParse->nSet++;
650 for(i=0; i<pExpr->pList->nExpr; i++){
651 Expr *pE2 = pExpr->pList->a[i].pExpr;
652 switch( pE2->op ){
653 case TK_FLOAT:
654 case TK_INTEGER:
655 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000656 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000657 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000658 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
659 sqliteVdbeDequoteP3(v, addr);
660 break;
661 }
662 default: {
663 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000664 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000665 break;
666 }
667 }
668 }
669 }
drhcfab11b2000-06-06 03:31:22 +0000670 break;
drhfef52082000-06-06 01:50:43 +0000671 }
672
drh19a775c2000-06-05 18:54:46 +0000673 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000674 /* This has to be a scalar SELECT. Generate code to put the
675 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000676 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000677 */
drh967e8b72000-06-21 13:59:10 +0000678 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000679 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000680 return 1;
681 }
682 break;
683 }
684
drhcce7d172000-05-31 15:34:51 +0000685 /* For all else, just recursively walk the tree */
686 default: {
drh4794b982000-06-06 13:54:14 +0000687 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000688 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000689 return 1;
690 }
691 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000692 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000693 return 1;
694 }
695 if( pExpr->pList ){
696 int i;
697 ExprList *pList = pExpr->pList;
698 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000699 Expr *pArg = pList->a[i].pExpr;
700 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000701 return 1;
702 }
703 }
704 }
705 }
706 }
707 return 0;
708}
709
drhcce7d172000-05-31 15:34:51 +0000710/*
711** Error check the functions in an expression. Make sure all
712** function names are recognized and all functions have the correct
713** number of arguments. Leave an error message in pParse->zErrMsg
714** if anything is amiss. Return the number of errors.
715**
716** if pIsAgg is not null and this expression is an aggregate function
717** (like count(*) or max(value)) then write a 1 into *pIsAgg.
718*/
719int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
720 int nErr = 0;
721 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000722 switch( pExpr->op ){
723 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000724 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
725 int no_such_func = 0; /* True if no such function exists */
726 int is_type_of = 0; /* True if is the special TypeOf() function */
727 int wrong_num_args = 0; /* True if wrong number of arguments */
728 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000729 int i;
drh0bce8352002-02-28 00:41:10 +0000730 FuncDef *pDef;
731
drh89425d52002-02-28 03:04:48 +0000732 pDef = sqliteFindFunction(pParse->db,
733 pExpr->token.z, pExpr->token.n, n, 0);
drh0bce8352002-02-28 00:41:10 +0000734 if( pDef==0 ){
735 pDef = sqliteFindFunction(pParse->db,
736 pExpr->token.z, pExpr->token.n, -1, 0);
737 if( pDef==0 ){
drhc9b84a12002-06-20 11:36:48 +0000738 if( n==1 && pExpr->token.n==6
739 && sqliteStrNICmp(pExpr->token.z, "typeof", 6)==0 ){
740 is_type_of = 1;
741 }else {
742 no_such_func = 1;
743 }
drh0bce8352002-02-28 00:41:10 +0000744 }else{
745 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000746 }
drh0bce8352002-02-28 00:41:10 +0000747 }else{
748 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000749 }
drh8e0a2f92002-02-23 23:45:45 +0000750 if( is_agg && !allowAgg ){
751 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
752 pExpr->token.z, pExpr->token.n, "()", 2, 0);
753 pParse->nErr++;
754 nErr++;
755 is_agg = 0;
756 }else if( no_such_func ){
drhcce7d172000-05-31 15:34:51 +0000757 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
758 pExpr->token.z, pExpr->token.n, 0);
759 pParse->nErr++;
760 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000761 }else if( wrong_num_args ){
762 sqliteSetNString(&pParse->zErrMsg,
763 "wrong number of arguments to function ",-1,
764 pExpr->token.z, pExpr->token.n, "()", 2, 0);
765 pParse->nErr++;
766 nErr++;
drhcce7d172000-05-31 15:34:51 +0000767 }
drh22827922000-06-06 17:27:05 +0000768 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000769 if( is_agg && pIsAgg ) *pIsAgg = 1;
770 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000771 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
772 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000773 }
drhc9b84a12002-06-20 11:36:48 +0000774 if( pDef==0 ){
775 if( is_type_of ){
776 pExpr->op = TK_STRING;
777 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
778 pExpr->token.z = "numeric";
779 pExpr->token.n = 7;
780 }else{
781 pExpr->token.z = "text";
782 pExpr->token.n = 4;
783 }
784 }
785 }else if( pDef->dataType>=0 ){
786 if( pDef->dataType<n ){
787 pExpr->dataType =
788 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
789 }else{
790 pExpr->dataType = SQLITE_SO_NUM;
791 }
792 }else if( pDef->dataType==SQLITE_ARGS ){
793 pDef->dataType = SQLITE_SO_TEXT;
794 for(i=0; i<n; i++){
795 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
796 pExpr->dataType = SQLITE_SO_NUM;
797 break;
798 }
799 }
800 }else if( pDef->dataType==SQLITE_NUMERIC ){
801 pExpr->dataType = SQLITE_SO_NUM;
802 }else{
803 pExpr->dataType = SQLITE_SO_TEXT;
804 }
drhcce7d172000-05-31 15:34:51 +0000805 }
806 default: {
807 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000808 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000809 }
810 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000811 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000812 }
drhfef52082000-06-06 01:50:43 +0000813 if( nErr==0 && pExpr->pList ){
814 int n = pExpr->pList->nExpr;
815 int i;
816 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000817 Expr *pE2 = pExpr->pList->a[i].pExpr;
818 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000819 }
820 }
drhcce7d172000-05-31 15:34:51 +0000821 break;
822 }
823 }
824 return nErr;
825}
826
827/*
drhc9b84a12002-06-20 11:36:48 +0000828** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
829** given expression should sort as numeric values or as text.
830**
831** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
832** both been called on the expression before it is passed to this routine.
833*/
834int sqliteExprType(Expr *p){
835 if( p==0 ) return SQLITE_SO_NUM;
836 while( p ) switch( p->op ){
837 case TK_PLUS:
838 case TK_MINUS:
839 case TK_STAR:
840 case TK_SLASH:
841 case TK_AND:
842 case TK_OR:
843 case TK_ISNULL:
844 case TK_NOTNULL:
845 case TK_NOT:
846 case TK_UMINUS:
847 case TK_BITAND:
848 case TK_BITOR:
849 case TK_BITNOT:
850 case TK_LSHIFT:
851 case TK_RSHIFT:
852 case TK_REM:
853 case TK_INTEGER:
854 case TK_FLOAT:
855 case TK_IN:
856 case TK_BETWEEN:
857 return SQLITE_SO_NUM;
858
859 case TK_STRING:
860 case TK_NULL:
861 case TK_CONCAT:
862 return SQLITE_SO_TEXT;
863
864 case TK_LT:
865 case TK_LE:
866 case TK_GT:
867 case TK_GE:
868 case TK_NE:
869 case TK_EQ:
870 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
871 return SQLITE_SO_NUM;
872 }
873 p = p->pRight;
874 break;
875
876 case TK_AS:
877 p = p->pLeft;
878 break;
879
880 case TK_COLUMN:
881 case TK_FUNCTION:
882 case TK_AGG_FUNCTION:
883 return p->dataType;
884
885 case TK_SELECT:
886 assert( p->pSelect );
887 assert( p->pSelect->pEList );
888 assert( p->pSelect->pEList->nExpr>0 );
889 p = p->pSelect->pEList->a[0].pExpr;
890 break;
891
drhb1363202002-06-26 02:45:03 +0000892 case TK_CASE: {
893 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
894 return SQLITE_SO_NUM;
895 }
896 if( p->pList ){
897 int i;
898 ExprList *pList = p->pList;
899 for(i=1; i<pList->nExpr; i+=2){
900 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
901 return SQLITE_SO_NUM;
902 }
903 }
904 }
905 return SQLITE_SO_TEXT;
906 }
907
drhc9b84a12002-06-20 11:36:48 +0000908 default:
909 assert( p->op==TK_ABORT ); /* Can't Happen */
910 break;
911 }
912 return SQLITE_SO_NUM;
913}
914
915/*
drhcce7d172000-05-31 15:34:51 +0000916** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000917** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000918*/
919void sqliteExprCode(Parse *pParse, Expr *pExpr){
920 Vdbe *v = pParse->pVdbe;
921 int op;
drhdaffd0e2001-04-11 14:28:42 +0000922 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000923 switch( pExpr->op ){
924 case TK_PLUS: op = OP_Add; break;
925 case TK_MINUS: op = OP_Subtract; break;
926 case TK_STAR: op = OP_Multiply; break;
927 case TK_SLASH: op = OP_Divide; break;
928 case TK_AND: op = OP_And; break;
929 case TK_OR: op = OP_Or; break;
930 case TK_LT: op = OP_Lt; break;
931 case TK_LE: op = OP_Le; break;
932 case TK_GT: op = OP_Gt; break;
933 case TK_GE: op = OP_Ge; break;
934 case TK_NE: op = OP_Ne; break;
935 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000936 case TK_ISNULL: op = OP_IsNull; break;
937 case TK_NOTNULL: op = OP_NotNull; break;
938 case TK_NOT: op = OP_Not; break;
939 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000940 case TK_BITAND: op = OP_BitAnd; break;
941 case TK_BITOR: op = OP_BitOr; break;
942 case TK_BITNOT: op = OP_BitNot; break;
943 case TK_LSHIFT: op = OP_ShiftLeft; break;
944 case TK_RSHIFT: op = OP_ShiftRight; break;
945 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000946 default: break;
947 }
948 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000949 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000950 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000951 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000952 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000953 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000954 }else{
drh99fcd712001-10-13 01:06:47 +0000955 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000956 }
drhcce7d172000-05-31 15:34:51 +0000957 break;
958 }
959 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000960 int iVal = atoi(pExpr->token.z);
961 char zBuf[30];
962 sprintf(zBuf,"%d",iVal);
963 if( strlen(zBuf)!=pExpr->token.n
964 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
965 /* If the integer value cannot be represented exactly in 32 bits,
966 ** then code it as a string instead. */
967 sqliteVdbeAddOp(v, OP_String, 0, 0);
968 }else{
969 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
970 }
drhe6840902002-03-06 03:08:25 +0000971 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
972 break;
973 }
974 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000975 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000976 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000977 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000978 break;
979 }
drhcce7d172000-05-31 15:34:51 +0000980 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000981 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000982 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +0000983 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
984 sqliteVdbeDequoteP3(v, addr);
985 break;
986 }
987 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000988 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000989 break;
990 }
drhc9b84a12002-06-20 11:36:48 +0000991 case TK_LT:
992 case TK_LE:
993 case TK_GT:
994 case TK_GE:
995 case TK_NE:
996 case TK_EQ: {
997 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
998 op += 6; /* Convert numeric opcodes to text opcodes */
999 }
1000 /* Fall through into the next case */
1001 }
drhcce7d172000-05-31 15:34:51 +00001002 case TK_AND:
1003 case TK_OR:
1004 case TK_PLUS:
1005 case TK_STAR:
1006 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001007 case TK_REM:
1008 case TK_BITAND:
1009 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001010 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001011 sqliteExprCode(pParse, pExpr->pLeft);
1012 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001013 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001014 break;
1015 }
drhbf4133c2001-10-13 02:59:08 +00001016 case TK_LSHIFT:
1017 case TK_RSHIFT: {
1018 sqliteExprCode(pParse, pExpr->pRight);
1019 sqliteExprCode(pParse, pExpr->pLeft);
1020 sqliteVdbeAddOp(v, op, 0, 0);
1021 break;
1022 }
drh00400772000-06-16 20:51:26 +00001023 case TK_CONCAT: {
1024 sqliteExprCode(pParse, pExpr->pLeft);
1025 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001026 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001027 break;
1028 }
drhcce7d172000-05-31 15:34:51 +00001029 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001030 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001031 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001032 Token *p = &pExpr->pLeft->token;
1033 char *z = sqliteMalloc( p->n + 2 );
1034 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001035 if( pExpr->pLeft->op==TK_INTEGER ){
1036 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1037 }else{
1038 sqliteVdbeAddOp(v, OP_String, 0, 0);
1039 }
drh99fcd712001-10-13 01:06:47 +00001040 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001041 sqliteFree(z);
1042 break;
1043 }
drh1ccde152000-06-17 13:12:39 +00001044 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001045 }
drhbf4133c2001-10-13 02:59:08 +00001046 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001047 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001048 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001049 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001050 break;
1051 }
1052 case TK_ISNULL:
1053 case TK_NOTNULL: {
1054 int dest;
drh99fcd712001-10-13 01:06:47 +00001055 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001056 sqliteExprCode(pParse, pExpr->pLeft);
1057 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001058 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001059 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001060 break;
1061 }
drh22827922000-06-06 17:27:05 +00001062 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001063 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001064 break;
1065 }
drhcce7d172000-05-31 15:34:51 +00001066 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001067 int i;
1068 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001069 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001070 FuncDef *pDef;
1071 pDef = sqliteFindFunction(pParse->db,
drh89425d52002-02-28 03:04:48 +00001072 pExpr->token.z, pExpr->token.n, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001073 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001074 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001075 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001076 }
drh89425d52002-02-28 03:04:48 +00001077 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001078 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001079 break;
1080 }
drh19a775c2000-06-05 18:54:46 +00001081 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001082 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001083 break;
1084 }
drhfef52082000-06-06 01:50:43 +00001085 case TK_IN: {
1086 int addr;
drh99fcd712001-10-13 01:06:47 +00001087 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001088 sqliteExprCode(pParse, pExpr->pLeft);
1089 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001090 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1091 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1092 sqliteVdbeAddOp(v, OP_String, 0, 0);
1093 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001094 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001095 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001096 }else{
drhf5905aa2002-05-26 20:54:33 +00001097 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001098 }
drh99fcd712001-10-13 01:06:47 +00001099 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001100 break;
1101 }
1102 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001103 sqliteExprCode(pParse, pExpr->pLeft);
1104 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1105 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1106 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1107 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1108 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1109 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1110 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001111 break;
1112 }
drha2e00042002-01-22 03:13:42 +00001113 case TK_AS: {
1114 sqliteExprCode(pParse, pExpr->pLeft);
1115 break;
1116 }
drh17a7f8d2002-03-24 13:13:27 +00001117 case TK_CASE: {
1118 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001119 int jumpInst;
1120 int addr;
1121 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001122 int i;
1123
1124 assert(pExpr->pList);
1125 assert((pExpr->pList->nExpr % 2) == 0);
1126 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001127 nExpr = pExpr->pList->nExpr;
1128 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001129 if( pExpr->pLeft ){
1130 sqliteExprCode(pParse, pExpr->pLeft);
1131 }
drhf5905aa2002-05-26 20:54:33 +00001132 for(i=0; i<nExpr; i=i+2){
1133 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001134 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001135 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001136 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1137 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001138 }else{
drhf570f012002-05-31 15:51:25 +00001139 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001140 }
1141 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001142 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001143 addr = sqliteVdbeCurrentAddr(v);
1144 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001145 }
drhf570f012002-05-31 15:51:25 +00001146 if( pExpr->pLeft ){
1147 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1148 }
drh17a7f8d2002-03-24 13:13:27 +00001149 if( pExpr->pRight ){
1150 sqliteExprCode(pParse, pExpr->pRight);
1151 }else{
drhf5905aa2002-05-26 20:54:33 +00001152 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001153 }
drhf5905aa2002-05-26 20:54:33 +00001154 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001155 break;
1156 }
1157 case TK_RAISE: {
1158 if( !pParse->trigStack ){
1159 sqliteSetNString(&pParse->zErrMsg,
1160 "RAISE() may only be used within a trigger-program", -1, 0);
1161 pParse->nErr++;
1162 return;
1163 }
1164 if( pExpr->iColumn == OE_Rollback ||
1165 pExpr->iColumn == OE_Abort ||
1166 pExpr->iColumn == OE_Fail ){
1167 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1168 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1169 sqliteDequote(msg);
1170 sqliteVdbeChangeP3(v, -1, msg, 0);
1171 sqliteFree(msg);
1172 } else {
1173 assert( pExpr->iColumn == OE_Ignore );
1174 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1175 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
1176 }
drh17a7f8d2002-03-24 13:13:27 +00001177 }
1178 break;
drhcce7d172000-05-31 15:34:51 +00001179 }
drhcce7d172000-05-31 15:34:51 +00001180}
1181
1182/*
1183** Generate code for a boolean expression such that a jump is made
1184** to the label "dest" if the expression is true but execution
1185** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001186**
1187** If the expression evaluates to NULL (neither true nor false), then
1188** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001189*/
drhf5905aa2002-05-26 20:54:33 +00001190void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001191 Vdbe *v = pParse->pVdbe;
1192 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001193 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001194 switch( pExpr->op ){
1195 case TK_LT: op = OP_Lt; break;
1196 case TK_LE: op = OP_Le; break;
1197 case TK_GT: op = OP_Gt; break;
1198 case TK_GE: op = OP_Ge; break;
1199 case TK_NE: op = OP_Ne; break;
1200 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001201 case TK_ISNULL: op = OP_IsNull; break;
1202 case TK_NOTNULL: op = OP_NotNull; break;
1203 default: break;
1204 }
1205 switch( pExpr->op ){
1206 case TK_AND: {
1207 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001208 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1209 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001210 sqliteVdbeResolveLabel(v, d2);
1211 break;
1212 }
1213 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001214 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1215 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001216 break;
1217 }
1218 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001219 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001220 break;
1221 }
1222 case TK_LT:
1223 case TK_LE:
1224 case TK_GT:
1225 case TK_GE:
1226 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001227 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001228 sqliteExprCode(pParse, pExpr->pLeft);
1229 sqliteExprCode(pParse, pExpr->pRight);
drhc9b84a12002-06-20 11:36:48 +00001230 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1231 op += 6; /* Convert numeric opcodes to text opcodes */
1232 }
drhf5905aa2002-05-26 20:54:33 +00001233 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001234 break;
1235 }
1236 case TK_ISNULL:
1237 case TK_NOTNULL: {
1238 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001239 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001240 break;
1241 }
drhfef52082000-06-06 01:50:43 +00001242 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001243 int addr;
drhcfab11b2000-06-06 03:31:22 +00001244 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001245 addr = sqliteVdbeCurrentAddr(v);
1246 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1247 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1248 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001249 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001250 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001251 }else{
drh99fcd712001-10-13 01:06:47 +00001252 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001253 }
1254 break;
1255 }
1256 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001257 int addr;
drhfef52082000-06-06 01:50:43 +00001258 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001259 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001260 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001261 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001262 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001263 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001264 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001265 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001266 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001267 break;
1268 }
drhcce7d172000-05-31 15:34:51 +00001269 default: {
1270 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001271 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001272 break;
1273 }
1274 }
1275}
1276
1277/*
drh66b89c82000-11-28 20:47:17 +00001278** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001279** to the label "dest" if the expression is false but execution
1280** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001281**
1282** If the expression evaluates to NULL (neither true nor false) then
1283** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001284*/
drhf5905aa2002-05-26 20:54:33 +00001285void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001286 Vdbe *v = pParse->pVdbe;
1287 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001288 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001289 switch( pExpr->op ){
1290 case TK_LT: op = OP_Ge; break;
1291 case TK_LE: op = OP_Gt; break;
1292 case TK_GT: op = OP_Le; break;
1293 case TK_GE: op = OP_Lt; break;
1294 case TK_NE: op = OP_Eq; break;
1295 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001296 case TK_ISNULL: op = OP_NotNull; break;
1297 case TK_NOTNULL: op = OP_IsNull; break;
1298 default: break;
1299 }
1300 switch( pExpr->op ){
1301 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001302 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1303 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001304 break;
1305 }
1306 case TK_OR: {
1307 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001308 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1309 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001310 sqliteVdbeResolveLabel(v, d2);
1311 break;
1312 }
1313 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001314 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001315 break;
1316 }
1317 case TK_LT:
1318 case TK_LE:
1319 case TK_GT:
1320 case TK_GE:
1321 case TK_NE:
1322 case TK_EQ: {
drhc9b84a12002-06-20 11:36:48 +00001323 if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1324 op += 6; /* Convert numeric opcodes to text opcodes */
1325 }
drhcce7d172000-05-31 15:34:51 +00001326 sqliteExprCode(pParse, pExpr->pLeft);
1327 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001328 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001329 break;
1330 }
drhcce7d172000-05-31 15:34:51 +00001331 case TK_ISNULL:
1332 case TK_NOTNULL: {
1333 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001334 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001335 break;
1336 }
drhfef52082000-06-06 01:50:43 +00001337 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001338 int addr;
drhcfab11b2000-06-06 03:31:22 +00001339 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001340 addr = sqliteVdbeCurrentAddr(v);
1341 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1342 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1343 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001344 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001345 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001346 }else{
drh99fcd712001-10-13 01:06:47 +00001347 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001348 }
1349 break;
1350 }
1351 case TK_BETWEEN: {
1352 int addr;
1353 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001354 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001355 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1356 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001357 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001358 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1359 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001360 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001361 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001362 break;
1363 }
drhcce7d172000-05-31 15:34:51 +00001364 default: {
1365 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001366 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001367 break;
1368 }
1369 }
1370}
drh22827922000-06-06 17:27:05 +00001371
1372/*
1373** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1374** if they are identical and return FALSE if they differ in any way.
1375*/
drhd8bc7082000-06-07 23:51:50 +00001376int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001377 int i;
1378 if( pA==0 ){
1379 return pB==0;
1380 }else if( pB==0 ){
1381 return 0;
1382 }
1383 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001384 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1385 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001386 if( pA->pList ){
1387 if( pB->pList==0 ) return 0;
1388 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1389 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001390 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001391 return 0;
1392 }
1393 }
1394 }else if( pB->pList ){
1395 return 0;
1396 }
1397 if( pA->pSelect || pB->pSelect ) return 0;
1398 if( pA->token.z ){
1399 if( pB->token.z==0 ) return 0;
1400 if( pB->token.n!=pA->token.n ) return 0;
1401 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
1402 }
1403 return 1;
1404}
1405
1406/*
1407** Add a new element to the pParse->aAgg[] array and return its index.
1408*/
1409static int appendAggInfo(Parse *pParse){
1410 if( (pParse->nAgg & 0x7)==0 ){
1411 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001412 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1413 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001414 return -1;
1415 }
drh6d4abfb2001-10-22 02:58:08 +00001416 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001417 }
1418 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1419 return pParse->nAgg++;
1420}
1421
1422/*
1423** Analyze the given expression looking for aggregate functions and
1424** for variables that need to be added to the pParse->aAgg[] array.
1425** Make additional entries to the pParse->aAgg[] array as necessary.
1426**
1427** This routine should only be called after the expression has been
1428** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1429**
1430** If errors are seen, leave an error message in zErrMsg and return
1431** the number of errors.
1432*/
1433int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1434 int i;
1435 AggExpr *aAgg;
1436 int nErr = 0;
1437
1438 if( pExpr==0 ) return 0;
1439 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001440 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001441 aAgg = pParse->aAgg;
1442 for(i=0; i<pParse->nAgg; i++){
1443 if( aAgg[i].isAgg ) continue;
1444 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001445 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001446 break;
1447 }
1448 }
1449 if( i>=pParse->nAgg ){
1450 i = appendAggInfo(pParse);
1451 if( i<0 ) return 1;
1452 pParse->aAgg[i].isAgg = 0;
1453 pParse->aAgg[i].pExpr = pExpr;
1454 }
drhaaf88722000-06-08 11:25:00 +00001455 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001456 break;
1457 }
1458 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001459 aAgg = pParse->aAgg;
1460 for(i=0; i<pParse->nAgg; i++){
1461 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001462 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001463 break;
1464 }
1465 }
1466 if( i>=pParse->nAgg ){
1467 i = appendAggInfo(pParse);
1468 if( i<0 ) return 1;
1469 pParse->aAgg[i].isAgg = 1;
1470 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001471 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drhf55f25f2002-02-28 01:46:11 +00001472 pExpr->token.z, pExpr->token.n,
1473 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001474 }
1475 pExpr->iAgg = i;
1476 break;
1477 }
1478 default: {
1479 if( pExpr->pLeft ){
1480 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1481 }
1482 if( nErr==0 && pExpr->pRight ){
1483 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1484 }
1485 if( nErr==0 && pExpr->pList ){
1486 int n = pExpr->pList->nExpr;
1487 int i;
1488 for(i=0; nErr==0 && i<n; i++){
1489 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1490 }
1491 }
1492 break;
1493 }
1494 }
1495 return nErr;
1496}
drh8e0a2f92002-02-23 23:45:45 +00001497
1498/*
1499** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001500** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001501** function, or return NULL if the function does not exist.
1502**
drh0bce8352002-02-28 00:41:10 +00001503** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001504** structure is created and liked into the "db" structure if a
1505** no matching function previously existed. When createFlag is true
1506** and the nArg parameter is -1, then only a function that accepts
1507** any number of arguments will be returned.
1508**
1509** If createFlag is false and nArg is -1, then the first valid
1510** function found is returned. A function is valid if either xFunc
1511** or xStep is non-zero.
1512*/
drh0bce8352002-02-28 00:41:10 +00001513FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001514 sqlite *db, /* An open database */
1515 const char *zName, /* Name of the function. Not null-terminated */
1516 int nName, /* Number of characters in the name */
1517 int nArg, /* Number of arguments. -1 means any number */
1518 int createFlag /* Create new entry if true and does not otherwise exist */
1519){
drh0bce8352002-02-28 00:41:10 +00001520 FuncDef *pFirst, *p, *pMaybe;
1521 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001522 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001523 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1524 return p;
1525 }
1526 pMaybe = 0;
1527 while( p && p->nArg!=nArg ){
1528 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1529 p = p->pNext;
1530 }
1531 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1532 return 0;
1533 }
1534 if( p==0 && pMaybe ){
1535 assert( createFlag==0 );
1536 return pMaybe;
1537 }
drh89425d52002-02-28 03:04:48 +00001538 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001539 p->nArg = nArg;
1540 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001541 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001542 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001543 }
1544 return p;
1545}