blob: 5fd5bbb3468fbf5218f37f9492b08e627fa927f7 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This file contains C code routines used for processing expressions
25**
drh19a775c2000-06-05 18:54:46 +000026** $Id: expr.c,v 1.6 2000/06/05 18:54:46 drh Exp $
drhcce7d172000-05-31 15:34:51 +000027*/
28#include "sqliteInt.h"
29
30/*
31** This routine walks an expression tree and resolves references to
32** table fields. Nodes of the form ID.ID or ID resolve into an
33** index to the table in the table list and a field offset. The opcode
34** for such nodes is changed to TK_FIELD. The iTable value is changed
drh19a775c2000-06-05 18:54:46 +000035** to the index of the referenced table in pTabList plus the pParse->nTab
36** value. The iField value is changed to the index of the field of the
37** referenced table.
38**
39** This routine also looks for SELECTs that are part of an expression.
40** If it finds any, it generates code to write the value of that select
41** into a memory cell.
drhcce7d172000-05-31 15:34:51 +000042**
43** Unknown fields or tables provoke an error. The function returns
44** the number of errors seen and leaves an error message on pParse->zErrMsg.
45*/
46int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){
47 if( pExpr==0 ) return 0;
48 switch( pExpr->op ){
49 /* A lone identifier */
50 case TK_ID: {
51 int cnt = 0; /* Number of matches */
52 int i; /* Loop counter */
53 char *z = 0;
54 sqliteSetNString(&z, pExpr->token.z, pExpr->token.n, 0);
55 for(i=0; i<pTabList->nId; i++){
56 int j;
57 Table *pTab = pTabList->a[i].pTab;
58 if( pTab==0 ) continue;
59 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +000060 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +000061 cnt++;
drh19a775c2000-06-05 18:54:46 +000062 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +000063 pExpr->iField = j;
64 }
65 }
66 }
67 sqliteFree(z);
68 if( cnt==0 ){
69 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
70 pExpr->token.z, pExpr->token.n, 0);
71 pParse->nErr++;
72 return 1;
73 }else if( cnt>1 ){
74 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
75 pExpr->token.z, pExpr->token.n, 0);
76 pParse->nErr++;
77 return 1;
78 }
79 pExpr->op = TK_FIELD;
80 break;
81 }
82
83 /* A table name and field name: ID.ID */
84 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +000085 int cnt = 0; /* Number of matches */
86 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +000087 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +000088 char *zLeft, *zRight; /* Text of an identifier */
89
90 pLeft = pExpr->pLeft;
91 pRight = pExpr->pRight;
92 assert( pLeft && pLeft->op==TK_ID );
93 assert( pRight && pRight->op==TK_ID );
94 zLeft = 0;
95 sqliteSetNString(&zLeft, pLeft->token.z, pLeft->token.n, 0);
96 zRight = 0;
97 sqliteSetNString(&zRight, pRight->token.z, pRight->token.n, 0);
98 for(i=0; i<pTabList->nId; i++){
99 int j;
100 char *zTab;
101 Table *pTab = pTabList->a[i].pTab;
102 if( pTab==0 ) continue;
103 if( pTabList->a[i].zAlias ){
104 zTab = pTabList->a[i].zAlias;
105 }else{
106 zTab = pTab->zName;
107 }
108 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
109 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000110 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000111 cnt++;
drh19a775c2000-06-05 18:54:46 +0000112 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000113 pExpr->iField = j;
114 }
115 }
116 }
117 sqliteFree(zLeft);
118 sqliteFree(zRight);
119 if( cnt==0 ){
120 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
121 pLeft->token.z, pLeft->token.n, ".", 1,
122 pRight->token.z, pRight->token.n, 0);
123 pParse->nErr++;
124 return 1;
125 }else if( cnt>1 ){
126 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
127 pLeft->token.z, pLeft->token.n, ".", 1,
128 pRight->token.z, pRight->token.n, 0);
129 pParse->nErr++;
130 return 1;
131 }
132 sqliteExprDelete(pLeft);
133 pExpr->pLeft = 0;
134 sqliteExprDelete(pRight);
135 pExpr->pRight = 0;
136 pExpr->op = TK_FIELD;
137 break;
138 }
139
drh19a775c2000-06-05 18:54:46 +0000140 case TK_SELECT: {
141 pExpr->iField = pParse->nMem++;
142 if( sqliteSelect(pParse, pExpr->pSelect, -1, pExpr->iField) ){
143 return 1;
144 }
145 break;
146 }
147
drhcce7d172000-05-31 15:34:51 +0000148 /* For all else, just recursively walk the tree */
149 default: {
150 if( pExpr->pLeft
151 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
152 return 1;
153 }
154 if( pExpr->pRight
155 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
156 return 1;
157 }
158 if( pExpr->pList ){
159 int i;
160 ExprList *pList = pExpr->pList;
161 for(i=0; i<pList->nExpr; i++){
162 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
163 return 1;
164 }
165 }
166 }
167 }
168 }
169 return 0;
170}
171
172#if 0 /* NOT USED */
173/*
174** Compare a token against a string. Return TRUE if they match.
175*/
176static int sqliteTokenCmp(Token *pToken, const char *zStr){
177 int n = strlen(zStr);
178 if( n!=pToken->n ) return 0;
179 return sqliteStrNICmp(pToken->z, zStr, n)==0;
180}
181#endif
182
183/*
184** Convert a function name into its integer identifier. Return the
185** identifier. Return FN_Unknown if the function name is unknown.
186*/
187int sqliteFuncId(Token *pToken){
188 static const struct {
189 char *zName;
190 int len;
191 int id;
192 } aFunc[] = {
193 { "count", 5, FN_Count },
194 { "min", 3, FN_Min },
195 { "max", 3, FN_Max },
196 { "sum", 3, FN_Sum },
197 };
198 int i;
199 for(i=0; i<ArraySize(aFunc); i++){
200 if( aFunc[i].len==pToken->n
201 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
202 return aFunc[i].id;
203 }
204 }
205 return FN_Unknown;
206}
207
208/*
209** Error check the functions in an expression. Make sure all
210** function names are recognized and all functions have the correct
211** number of arguments. Leave an error message in pParse->zErrMsg
212** if anything is amiss. Return the number of errors.
213**
214** if pIsAgg is not null and this expression is an aggregate function
215** (like count(*) or max(value)) then write a 1 into *pIsAgg.
216*/
217int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
218 int nErr = 0;
219 if( pExpr==0 ) return 0;
220 if( pIsAgg ) *pIsAgg = 0;
221 switch( pExpr->op ){
222 case TK_FUNCTION: {
223 int id = sqliteFuncId(&pExpr->token);
224 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
225 int no_such_func = 0;
226 int too_many_args = 0;
227 int too_few_args = 0;
228 int is_agg = 0;
229 int i;
230 switch( id ){
231 case FN_Unknown: {
232 no_such_func = 1;
233 break;
234 }
235 case FN_Count: {
236 no_such_func = !allowAgg;
237 too_many_args = n>1;
238 is_agg = 1;
239 break;
240 }
241 case FN_Max:
242 case FN_Min: {
243 too_few_args = allowAgg ? n<1 : n<2;
244 is_agg = n==1;
245 break;
246 }
247 case FN_Sum: {
248 no_such_func = !allowAgg;
249 too_many_args = n>1;
250 too_few_args = n<1;
251 is_agg = 1;
252 break;
253 }
254 default: break;
255 }
256 if( no_such_func ){
257 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
258 pExpr->token.z, pExpr->token.n, 0);
259 pParse->nErr++;
260 nErr++;
261 }else if( too_many_args ){
262 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
263 pExpr->token.z, pExpr->token.n, "()", 2, 0);
264 pParse->nErr++;
265 nErr++;
266 }else if( too_few_args ){
267 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
268 pExpr->token.z, pExpr->token.n, "()", 2, 0);
269 pParse->nErr++;
270 nErr++;
271 }
272 if( is_agg && pIsAgg ) *pIsAgg = 1;
273 for(i=0; nErr==0 && i<n; i++){
274 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
275 }
276 }
277 default: {
278 if( pExpr->pLeft ){
279 nErr = sqliteExprCheck(pParse, pExpr->pLeft, 0, 0);
280 }
281 if( nErr==0 && pExpr->pRight ){
282 nErr = sqliteExprCheck(pParse, pExpr->pRight, 0, 0);
283 }
284 break;
285 }
286 }
287 return nErr;
288}
289
290/*
291** Generate code into the current Vdbe to evaluate the given
292** expression and leave the result on the stack.
293*/
294void sqliteExprCode(Parse *pParse, Expr *pExpr){
295 Vdbe *v = pParse->pVdbe;
296 int op;
297 switch( pExpr->op ){
298 case TK_PLUS: op = OP_Add; break;
299 case TK_MINUS: op = OP_Subtract; break;
300 case TK_STAR: op = OP_Multiply; break;
301 case TK_SLASH: op = OP_Divide; break;
302 case TK_AND: op = OP_And; break;
303 case TK_OR: op = OP_Or; break;
304 case TK_LT: op = OP_Lt; break;
305 case TK_LE: op = OP_Le; break;
306 case TK_GT: op = OP_Gt; break;
307 case TK_GE: op = OP_Ge; break;
308 case TK_NE: op = OP_Ne; break;
309 case TK_EQ: op = OP_Eq; break;
310 case TK_LIKE: op = OP_Like; break;
311 case TK_GLOB: op = OP_Glob; break;
312 case TK_ISNULL: op = OP_IsNull; break;
313 case TK_NOTNULL: op = OP_NotNull; break;
314 case TK_NOT: op = OP_Not; break;
315 case TK_UMINUS: op = OP_Negative; break;
316 default: break;
317 }
318 switch( pExpr->op ){
319 case TK_FIELD: {
320 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iField, 0, 0);
321 break;
322 }
323 case TK_INTEGER: {
324 int i = atoi(pExpr->token.z);
325 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
326 break;
327 }
328 case TK_FLOAT: {
329 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
330 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
331 break;
332 }
333 case TK_STRING: {
334 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
335 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
336 sqliteVdbeDequoteP3(v, addr);
337 break;
338 }
339 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000340 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000341 break;
342 }
343 case TK_AND:
344 case TK_OR:
345 case TK_PLUS:
346 case TK_STAR:
347 case TK_MINUS:
348 case TK_SLASH: {
349 sqliteExprCode(pParse, pExpr->pLeft);
350 sqliteExprCode(pParse, pExpr->pRight);
351 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
352 break;
353 }
354 case TK_LT:
355 case TK_LE:
356 case TK_GT:
357 case TK_GE:
358 case TK_NE:
359 case TK_EQ:
360 case TK_LIKE:
361 case TK_GLOB: {
362 int dest;
363 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
364 sqliteExprCode(pParse, pExpr->pLeft);
365 sqliteExprCode(pParse, pExpr->pRight);
366 dest = sqliteVdbeCurrentAddr(v) + 2;
367 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
368 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
369 break;
370 }
371 case TK_NOT:
372 case TK_UMINUS: {
373 sqliteExprCode(pParse, pExpr->pLeft);
374 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
375 break;
376 }
377 case TK_ISNULL:
378 case TK_NOTNULL: {
379 int dest;
drh8be51132000-06-03 19:19:41 +0000380 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000381 sqliteExprCode(pParse, pExpr->pLeft);
382 dest = sqliteVdbeCurrentAddr(v) + 2;
383 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000384 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000385 break;
386 }
387 case TK_FUNCTION: {
388 int id = sqliteFuncId(&pExpr->token);
389 int op;
390 int i;
391 ExprList *pList = pExpr->pList;
392 op = id==FN_Min ? OP_Min : OP_Max;
393 for(i=0; i<pList->nExpr; i++){
394 sqliteExprCode(pParse, pList->a[i].pExpr);
395 if( i>0 ){
396 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
397 }
398 }
399 break;
400 }
drh19a775c2000-06-05 18:54:46 +0000401 case TK_SELECT: {
402 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iField, 0, 0, 0);
403 break;
404 }
drhcce7d172000-05-31 15:34:51 +0000405 }
406 return;
407}
408
409/*
410** Generate code for a boolean expression such that a jump is made
411** to the label "dest" if the expression is true but execution
412** continues straight thru if the expression is false.
413*/
414void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
415 Vdbe *v = pParse->pVdbe;
416 int op = 0;
417 switch( pExpr->op ){
418 case TK_LT: op = OP_Lt; break;
419 case TK_LE: op = OP_Le; break;
420 case TK_GT: op = OP_Gt; break;
421 case TK_GE: op = OP_Ge; break;
422 case TK_NE: op = OP_Ne; break;
423 case TK_EQ: op = OP_Eq; break;
424 case TK_LIKE: op = OP_Like; break;
425 case TK_GLOB: op = OP_Glob; break;
426 case TK_ISNULL: op = OP_IsNull; break;
427 case TK_NOTNULL: op = OP_NotNull; break;
428 default: break;
429 }
430 switch( pExpr->op ){
431 case TK_AND: {
432 int d2 = sqliteVdbeMakeLabel(v);
433 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
434 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
435 sqliteVdbeResolveLabel(v, d2);
436 break;
437 }
438 case TK_OR: {
439 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
440 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
441 break;
442 }
443 case TK_NOT: {
444 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
445 break;
446 }
447 case TK_LT:
448 case TK_LE:
449 case TK_GT:
450 case TK_GE:
451 case TK_NE:
452 case TK_EQ:
453 case TK_LIKE:
454 case TK_GLOB: {
455 sqliteExprCode(pParse, pExpr->pLeft);
456 sqliteExprCode(pParse, pExpr->pRight);
457 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
458 break;
459 }
460 case TK_ISNULL:
461 case TK_NOTNULL: {
462 sqliteExprCode(pParse, pExpr->pLeft);
463 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
464 break;
465 }
466 default: {
467 sqliteExprCode(pParse, pExpr);
468 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
469 break;
470 }
471 }
472}
473
474/*
475** Generate code for boolean expression such that a jump is made
476** to the label "dest" if the expression is false but execution
477** continues straight thru if the expression is true.
478*/
479void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
480 Vdbe *v = pParse->pVdbe;
481 int op = 0;
482 switch( pExpr->op ){
483 case TK_LT: op = OP_Ge; break;
484 case TK_LE: op = OP_Gt; break;
485 case TK_GT: op = OP_Le; break;
486 case TK_GE: op = OP_Lt; break;
487 case TK_NE: op = OP_Eq; break;
488 case TK_EQ: op = OP_Ne; break;
489 case TK_LIKE: op = OP_Like; break;
490 case TK_GLOB: op = OP_Glob; break;
491 case TK_ISNULL: op = OP_NotNull; break;
492 case TK_NOTNULL: op = OP_IsNull; break;
493 default: break;
494 }
495 switch( pExpr->op ){
496 case TK_AND: {
497 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
498 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
499 break;
500 }
501 case TK_OR: {
502 int d2 = sqliteVdbeMakeLabel(v);
503 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
504 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
505 sqliteVdbeResolveLabel(v, d2);
506 break;
507 }
508 case TK_NOT: {
509 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
510 break;
511 }
512 case TK_LT:
513 case TK_LE:
514 case TK_GT:
515 case TK_GE:
516 case TK_NE:
517 case TK_EQ: {
518 sqliteExprCode(pParse, pExpr->pLeft);
519 sqliteExprCode(pParse, pExpr->pRight);
520 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
521 break;
522 }
523 case TK_LIKE:
524 case TK_GLOB: {
525 sqliteExprCode(pParse, pExpr->pLeft);
526 sqliteExprCode(pParse, pExpr->pRight);
527 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
528 break;
529 }
530 case TK_ISNULL:
531 case TK_NOTNULL: {
532 sqliteExprCode(pParse, pExpr->pLeft);
533 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
534 break;
535 }
536 default: {
537 sqliteExprCode(pParse, pExpr);
538 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
539 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
540 break;
541 }
542 }
543}