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