blob: 73478feb6f347fa16cdab9c9ea4286013f4269d0 [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**
drhbed86902000-06-02 13:27:59 +000026** $Id: expr.c,v 1.2 2000/06/02 13:27:59 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
35** to the index of the referenced table in pTabList, and the iField value
36** is changed to the index of the field of the referenced table.
37**
38** Unknown fields or tables provoke an error. The function returns
39** the number of errors seen and leaves an error message on pParse->zErrMsg.
40*/
41int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){
42 if( pExpr==0 ) return 0;
43 switch( pExpr->op ){
44 /* A lone identifier */
45 case TK_ID: {
46 int cnt = 0; /* Number of matches */
47 int i; /* Loop counter */
48 char *z = 0;
49 sqliteSetNString(&z, pExpr->token.z, pExpr->token.n, 0);
50 for(i=0; i<pTabList->nId; i++){
51 int j;
52 Table *pTab = pTabList->a[i].pTab;
53 if( pTab==0 ) continue;
54 for(j=0; j<pTab->nCol; j++){
55 if( sqliteStrICmp(pTab->azCol[j], z)==0 ){
56 cnt++;
57 pExpr->iTable = i;
58 pExpr->iField = j;
59 }
60 }
61 }
62 sqliteFree(z);
63 if( cnt==0 ){
64 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
65 pExpr->token.z, pExpr->token.n, 0);
66 pParse->nErr++;
67 return 1;
68 }else if( cnt>1 ){
69 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
70 pExpr->token.z, pExpr->token.n, 0);
71 pParse->nErr++;
72 return 1;
73 }
74 pExpr->op = TK_FIELD;
75 break;
76 }
77
78 /* A table name and field name: ID.ID */
79 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +000080 int cnt = 0; /* Number of matches */
81 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +000082 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +000083 char *zLeft, *zRight; /* Text of an identifier */
84
85 pLeft = pExpr->pLeft;
86 pRight = pExpr->pRight;
87 assert( pLeft && pLeft->op==TK_ID );
88 assert( pRight && pRight->op==TK_ID );
89 zLeft = 0;
90 sqliteSetNString(&zLeft, pLeft->token.z, pLeft->token.n, 0);
91 zRight = 0;
92 sqliteSetNString(&zRight, pRight->token.z, pRight->token.n, 0);
93 for(i=0; i<pTabList->nId; i++){
94 int j;
95 char *zTab;
96 Table *pTab = pTabList->a[i].pTab;
97 if( pTab==0 ) continue;
98 if( pTabList->a[i].zAlias ){
99 zTab = pTabList->a[i].zAlias;
100 }else{
101 zTab = pTab->zName;
102 }
103 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
104 for(j=0; j<pTab->nCol; j++){
105 if( sqliteStrICmp(pTab->azCol[j], zRight)==0 ){
106 cnt++;
107 pExpr->iTable = i;
108 pExpr->iField = j;
109 }
110 }
111 }
112 sqliteFree(zLeft);
113 sqliteFree(zRight);
114 if( cnt==0 ){
115 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
116 pLeft->token.z, pLeft->token.n, ".", 1,
117 pRight->token.z, pRight->token.n, 0);
118 pParse->nErr++;
119 return 1;
120 }else if( cnt>1 ){
121 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
122 pLeft->token.z, pLeft->token.n, ".", 1,
123 pRight->token.z, pRight->token.n, 0);
124 pParse->nErr++;
125 return 1;
126 }
127 sqliteExprDelete(pLeft);
128 pExpr->pLeft = 0;
129 sqliteExprDelete(pRight);
130 pExpr->pRight = 0;
131 pExpr->op = TK_FIELD;
132 break;
133 }
134
135 /* For all else, just recursively walk the tree */
136 default: {
137 if( pExpr->pLeft
138 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
139 return 1;
140 }
141 if( pExpr->pRight
142 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
143 return 1;
144 }
145 if( pExpr->pList ){
146 int i;
147 ExprList *pList = pExpr->pList;
148 for(i=0; i<pList->nExpr; i++){
149 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
150 return 1;
151 }
152 }
153 }
154 }
155 }
156 return 0;
157}
158
159#if 0 /* NOT USED */
160/*
161** Compare a token against a string. Return TRUE if they match.
162*/
163static int sqliteTokenCmp(Token *pToken, const char *zStr){
164 int n = strlen(zStr);
165 if( n!=pToken->n ) return 0;
166 return sqliteStrNICmp(pToken->z, zStr, n)==0;
167}
168#endif
169
170/*
171** Convert a function name into its integer identifier. Return the
172** identifier. Return FN_Unknown if the function name is unknown.
173*/
174int sqliteFuncId(Token *pToken){
175 static const struct {
176 char *zName;
177 int len;
178 int id;
179 } aFunc[] = {
180 { "count", 5, FN_Count },
181 { "min", 3, FN_Min },
182 { "max", 3, FN_Max },
183 { "sum", 3, FN_Sum },
184 };
185 int i;
186 for(i=0; i<ArraySize(aFunc); i++){
187 if( aFunc[i].len==pToken->n
188 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
189 return aFunc[i].id;
190 }
191 }
192 return FN_Unknown;
193}
194
195/*
196** Error check the functions in an expression. Make sure all
197** function names are recognized and all functions have the correct
198** number of arguments. Leave an error message in pParse->zErrMsg
199** if anything is amiss. Return the number of errors.
200**
201** if pIsAgg is not null and this expression is an aggregate function
202** (like count(*) or max(value)) then write a 1 into *pIsAgg.
203*/
204int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
205 int nErr = 0;
206 if( pExpr==0 ) return 0;
207 if( pIsAgg ) *pIsAgg = 0;
208 switch( pExpr->op ){
209 case TK_FUNCTION: {
210 int id = sqliteFuncId(&pExpr->token);
211 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
212 int no_such_func = 0;
213 int too_many_args = 0;
214 int too_few_args = 0;
215 int is_agg = 0;
216 int i;
217 switch( id ){
218 case FN_Unknown: {
219 no_such_func = 1;
220 break;
221 }
222 case FN_Count: {
223 no_such_func = !allowAgg;
224 too_many_args = n>1;
225 is_agg = 1;
226 break;
227 }
228 case FN_Max:
229 case FN_Min: {
230 too_few_args = allowAgg ? n<1 : n<2;
231 is_agg = n==1;
232 break;
233 }
234 case FN_Sum: {
235 no_such_func = !allowAgg;
236 too_many_args = n>1;
237 too_few_args = n<1;
238 is_agg = 1;
239 break;
240 }
241 default: break;
242 }
243 if( no_such_func ){
244 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
245 pExpr->token.z, pExpr->token.n, 0);
246 pParse->nErr++;
247 nErr++;
248 }else if( too_many_args ){
249 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
250 pExpr->token.z, pExpr->token.n, "()", 2, 0);
251 pParse->nErr++;
252 nErr++;
253 }else if( too_few_args ){
254 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
255 pExpr->token.z, pExpr->token.n, "()", 2, 0);
256 pParse->nErr++;
257 nErr++;
258 }
259 if( is_agg && pIsAgg ) *pIsAgg = 1;
260 for(i=0; nErr==0 && i<n; i++){
261 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
262 }
263 }
264 default: {
265 if( pExpr->pLeft ){
266 nErr = sqliteExprCheck(pParse, pExpr->pLeft, 0, 0);
267 }
268 if( nErr==0 && pExpr->pRight ){
269 nErr = sqliteExprCheck(pParse, pExpr->pRight, 0, 0);
270 }
271 break;
272 }
273 }
274 return nErr;
275}
276
277/*
278** Generate code into the current Vdbe to evaluate the given
279** expression and leave the result on the stack.
280*/
281void sqliteExprCode(Parse *pParse, Expr *pExpr){
282 Vdbe *v = pParse->pVdbe;
283 int op;
284 switch( pExpr->op ){
285 case TK_PLUS: op = OP_Add; break;
286 case TK_MINUS: op = OP_Subtract; break;
287 case TK_STAR: op = OP_Multiply; break;
288 case TK_SLASH: op = OP_Divide; break;
289 case TK_AND: op = OP_And; break;
290 case TK_OR: op = OP_Or; break;
291 case TK_LT: op = OP_Lt; break;
292 case TK_LE: op = OP_Le; break;
293 case TK_GT: op = OP_Gt; break;
294 case TK_GE: op = OP_Ge; break;
295 case TK_NE: op = OP_Ne; break;
296 case TK_EQ: op = OP_Eq; break;
297 case TK_LIKE: op = OP_Like; break;
298 case TK_GLOB: op = OP_Glob; break;
299 case TK_ISNULL: op = OP_IsNull; break;
300 case TK_NOTNULL: op = OP_NotNull; break;
301 case TK_NOT: op = OP_Not; break;
302 case TK_UMINUS: op = OP_Negative; break;
303 default: break;
304 }
305 switch( pExpr->op ){
306 case TK_FIELD: {
307 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iField, 0, 0);
308 break;
309 }
310 case TK_INTEGER: {
311 int i = atoi(pExpr->token.z);
312 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
313 break;
314 }
315 case TK_FLOAT: {
316 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
317 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
318 break;
319 }
320 case TK_STRING: {
321 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
322 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
323 sqliteVdbeDequoteP3(v, addr);
324 break;
325 }
326 case TK_NULL: {
327 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
328 break;
329 }
330 case TK_AND:
331 case TK_OR:
332 case TK_PLUS:
333 case TK_STAR:
334 case TK_MINUS:
335 case TK_SLASH: {
336 sqliteExprCode(pParse, pExpr->pLeft);
337 sqliteExprCode(pParse, pExpr->pRight);
338 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
339 break;
340 }
341 case TK_LT:
342 case TK_LE:
343 case TK_GT:
344 case TK_GE:
345 case TK_NE:
346 case TK_EQ:
347 case TK_LIKE:
348 case TK_GLOB: {
349 int dest;
350 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
351 sqliteExprCode(pParse, pExpr->pLeft);
352 sqliteExprCode(pParse, pExpr->pRight);
353 dest = sqliteVdbeCurrentAddr(v) + 2;
354 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
355 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
356 break;
357 }
358 case TK_NOT:
359 case TK_UMINUS: {
360 sqliteExprCode(pParse, pExpr->pLeft);
361 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
362 break;
363 }
364 case TK_ISNULL:
365 case TK_NOTNULL: {
366 int dest;
367 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
368 sqliteExprCode(pParse, pExpr->pLeft);
369 dest = sqliteVdbeCurrentAddr(v) + 2;
370 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
371 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
372 break;
373 }
374 case TK_FUNCTION: {
375 int id = sqliteFuncId(&pExpr->token);
376 int op;
377 int i;
378 ExprList *pList = pExpr->pList;
379 op = id==FN_Min ? OP_Min : OP_Max;
380 for(i=0; i<pList->nExpr; i++){
381 sqliteExprCode(pParse, pList->a[i].pExpr);
382 if( i>0 ){
383 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
384 }
385 }
386 break;
387 }
388 }
389 return;
390}
391
392/*
393** Generate code for a boolean expression such that a jump is made
394** to the label "dest" if the expression is true but execution
395** continues straight thru if the expression is false.
396*/
397void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
398 Vdbe *v = pParse->pVdbe;
399 int op = 0;
400 switch( pExpr->op ){
401 case TK_LT: op = OP_Lt; break;
402 case TK_LE: op = OP_Le; break;
403 case TK_GT: op = OP_Gt; break;
404 case TK_GE: op = OP_Ge; break;
405 case TK_NE: op = OP_Ne; break;
406 case TK_EQ: op = OP_Eq; break;
407 case TK_LIKE: op = OP_Like; break;
408 case TK_GLOB: op = OP_Glob; break;
409 case TK_ISNULL: op = OP_IsNull; break;
410 case TK_NOTNULL: op = OP_NotNull; break;
411 default: break;
412 }
413 switch( pExpr->op ){
414 case TK_AND: {
415 int d2 = sqliteVdbeMakeLabel(v);
416 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
417 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
418 sqliteVdbeResolveLabel(v, d2);
419 break;
420 }
421 case TK_OR: {
422 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
423 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
424 break;
425 }
426 case TK_NOT: {
427 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
428 break;
429 }
430 case TK_LT:
431 case TK_LE:
432 case TK_GT:
433 case TK_GE:
434 case TK_NE:
435 case TK_EQ:
436 case TK_LIKE:
437 case TK_GLOB: {
438 sqliteExprCode(pParse, pExpr->pLeft);
439 sqliteExprCode(pParse, pExpr->pRight);
440 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
441 break;
442 }
443 case TK_ISNULL:
444 case TK_NOTNULL: {
445 sqliteExprCode(pParse, pExpr->pLeft);
446 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
447 break;
448 }
449 default: {
450 sqliteExprCode(pParse, pExpr);
451 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
452 break;
453 }
454 }
455}
456
457/*
458** Generate code for boolean expression such that a jump is made
459** to the label "dest" if the expression is false but execution
460** continues straight thru if the expression is true.
461*/
462void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
463 Vdbe *v = pParse->pVdbe;
464 int op = 0;
465 switch( pExpr->op ){
466 case TK_LT: op = OP_Ge; break;
467 case TK_LE: op = OP_Gt; break;
468 case TK_GT: op = OP_Le; break;
469 case TK_GE: op = OP_Lt; break;
470 case TK_NE: op = OP_Eq; break;
471 case TK_EQ: op = OP_Ne; break;
472 case TK_LIKE: op = OP_Like; break;
473 case TK_GLOB: op = OP_Glob; break;
474 case TK_ISNULL: op = OP_NotNull; break;
475 case TK_NOTNULL: op = OP_IsNull; break;
476 default: break;
477 }
478 switch( pExpr->op ){
479 case TK_AND: {
480 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
481 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
482 break;
483 }
484 case TK_OR: {
485 int d2 = sqliteVdbeMakeLabel(v);
486 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
487 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
488 sqliteVdbeResolveLabel(v, d2);
489 break;
490 }
491 case TK_NOT: {
492 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
493 break;
494 }
495 case TK_LT:
496 case TK_LE:
497 case TK_GT:
498 case TK_GE:
499 case TK_NE:
500 case TK_EQ: {
501 sqliteExprCode(pParse, pExpr->pLeft);
502 sqliteExprCode(pParse, pExpr->pRight);
503 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
504 break;
505 }
506 case TK_LIKE:
507 case TK_GLOB: {
508 sqliteExprCode(pParse, pExpr->pLeft);
509 sqliteExprCode(pParse, pExpr->pRight);
510 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
511 break;
512 }
513 case TK_ISNULL:
514 case TK_NOTNULL: {
515 sqliteExprCode(pParse, pExpr->pLeft);
516 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
517 break;
518 }
519 default: {
520 sqliteExprCode(pParse, pExpr);
521 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
522 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
523 break;
524 }
525 }
526}