blob: 0cad95c7c0141d899a78ce9194528834a32541ba [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*************************************************************************
drh1ccde152000-06-17 13:12:39 +000024** This file contains routines used for analyzing expressions and
25** for generating VDBE code that evaluates expressions.
drhcce7d172000-05-31 15:34:51 +000026**
drhff9821a2001-04-04 21:22:14 +000027** $Id: expr.c,v 1.23 2001/04/04 21:22:14 drh Exp $
drhcce7d172000-05-31 15:34:51 +000028*/
29#include "sqliteInt.h"
30
31/*
drhfef52082000-06-06 01:50:43 +000032** Walk an expression tree. Return 1 if the expression is constant
33** and 0 if it involves variables.
34*/
35static int isConstant(Expr *p){
36 switch( p->op ){
37 case TK_ID:
drh967e8b72000-06-21 13:59:10 +000038 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +000039 case TK_DOT:
40 return 0;
41 default: {
42 if( p->pLeft && !isConstant(p->pLeft) ) return 0;
43 if( p->pRight && !isConstant(p->pRight) ) return 0;
44 if( p->pList ){
45 int i;
46 for(i=0; i<p->pList->nExpr; i++){
47 if( !isConstant(p->pList->a[i].pExpr) ) return 0;
48 }
49 }
50 break;
51 }
52 }
53 return 1;
54}
55
56/*
drh4794b982000-06-06 13:54:14 +000057** Walk the expression tree and process operators of the form:
58**
59** expr IN (SELECT ...)
60**
drh967e8b72000-06-21 13:59:10 +000061** These operators have to be processed before column names are
drh4794b982000-06-06 13:54:14 +000062** resolved because each such operator increments pParse->nTab
drh1ccde152000-06-17 13:12:39 +000063** to reserve cursor numbers for its own use. But pParse->nTab
drh967e8b72000-06-21 13:59:10 +000064** needs to be constant once we begin resolving column names.
drh4794b982000-06-06 13:54:14 +000065**
66** Actually, the processing of IN-SELECT is only started by this
67** routine. This routine allocates a cursor number to the IN-SELECT
68** and then moves on. The code generation is done by
69** sqliteExprResolveIds() which must be called afterwards.
70*/
71void sqliteExprResolveInSelect(Parse *pParse, Expr *pExpr){
72 if( pExpr==0 ) return;
73 if( pExpr->op==TK_IN && pExpr->pSelect!=0 ){
74 pExpr->iTable = pParse->nTab++;
75 }else{
76 if( pExpr->pLeft ) sqliteExprResolveInSelect(pParse, pExpr->pLeft);
77 if( pExpr->pRight ) sqliteExprResolveInSelect(pParse, pExpr->pRight);
78 if( pExpr->pList ){
79 int i;
80 ExprList *pList = pExpr->pList;
81 for(i=0; i<pList->nExpr; i++){
82 sqliteExprResolveInSelect(pParse, pList->a[i].pExpr);
83 }
84 }
85 }
86}
87
88/*
drhc4a3c772001-04-04 11:48:57 +000089** Return TRUE if the given string is a row-id column name.
90*/
91static int sqliteIsRowid(const char *z){
92 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
93 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
94 if( sqliteStrICmp(z, "OID")==0 ) return 1;
95 return 0;
96}
97
98/*
drhcce7d172000-05-31 15:34:51 +000099** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000100** table columns. Nodes of the form ID.ID or ID resolve into an
101** index to the table in the table list and a column offset. The opcode
102** for such nodes is changed to TK_COLUMN. The iTable value is changed
drh19a775c2000-06-05 18:54:46 +0000103** to the index of the referenced table in pTabList plus the pParse->nTab
drh967e8b72000-06-21 13:59:10 +0000104** value. The iColumn value is changed to the index of the column of the
drh19a775c2000-06-05 18:54:46 +0000105** referenced table.
106**
drhfef52082000-06-06 01:50:43 +0000107** We also check for instances of the IN operator. IN comes in two
108** forms:
109**
110** expr IN (exprlist)
111** and
112** expr IN (SELECT ...)
113**
114** The first form is handled by creating a set holding the list
115** of allowed values. The second form causes the SELECT to generate
116** a temporary table.
117**
118** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000119** If it finds any, it generates code to write the value of that select
120** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000121**
drh967e8b72000-06-21 13:59:10 +0000122** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000123** the number of errors seen and leaves an error message on pParse->zErrMsg.
124*/
125int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){
126 if( pExpr==0 ) return 0;
127 switch( pExpr->op ){
128 /* A lone identifier */
129 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000130 int cnt = 0; /* Number of matches */
131 int i; /* Loop counter */
drh6e142f52000-06-08 13:36:40 +0000132 char *z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000133 for(i=0; i<pTabList->nId; i++){
134 int j;
135 Table *pTab = pTabList->a[i].pTab;
136 if( pTab==0 ) continue;
137 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000138 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000139 cnt++;
drh19a775c2000-06-05 18:54:46 +0000140 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000141 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000142 }
143 }
144 }
drhc4a3c772001-04-04 11:48:57 +0000145 if( cnt==0 && sqliteIsRowid(z) ){
146 pExpr->iColumn = -1;
147 pExpr->iTable = pParse->nTab;
148 cnt = 1 + (pTabList->nId>1);
149 }
drhcce7d172000-05-31 15:34:51 +0000150 sqliteFree(z);
151 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000152 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000153 pExpr->token.z, pExpr->token.n, 0);
154 pParse->nErr++;
155 return 1;
156 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000157 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000158 pExpr->token.z, pExpr->token.n, 0);
159 pParse->nErr++;
160 return 1;
161 }
drh967e8b72000-06-21 13:59:10 +0000162 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000163 break;
164 }
165
drh967e8b72000-06-21 13:59:10 +0000166 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000167 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000168 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000169 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000170 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000171 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000172 char *zLeft, *zRight; /* Text of an identifier */
173
174 pLeft = pExpr->pLeft;
175 pRight = pExpr->pRight;
176 assert( pLeft && pLeft->op==TK_ID );
177 assert( pRight && pRight->op==TK_ID );
drh6e142f52000-06-08 13:36:40 +0000178 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
179 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhc4a3c772001-04-04 11:48:57 +0000180 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000181 for(i=0; i<pTabList->nId; i++){
182 int j;
183 char *zTab;
184 Table *pTab = pTabList->a[i].pTab;
185 if( pTab==0 ) continue;
186 if( pTabList->a[i].zAlias ){
187 zTab = pTabList->a[i].zAlias;
188 }else{
189 zTab = pTab->zName;
190 }
191 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhc4a3c772001-04-04 11:48:57 +0000192 if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000193 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000194 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000195 cnt++;
drh19a775c2000-06-05 18:54:46 +0000196 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000197 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000198 }
199 }
200 }
drhc4a3c772001-04-04 11:48:57 +0000201 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
202 cnt = 1;
203 pExpr->iColumn = -1;
204 }
drhcce7d172000-05-31 15:34:51 +0000205 sqliteFree(zLeft);
206 sqliteFree(zRight);
207 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000208 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000209 pLeft->token.z, pLeft->token.n, ".", 1,
210 pRight->token.z, pRight->token.n, 0);
211 pParse->nErr++;
212 return 1;
213 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000214 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000215 pLeft->token.z, pLeft->token.n, ".", 1,
216 pRight->token.z, pRight->token.n, 0);
217 pParse->nErr++;
218 return 1;
219 }
220 sqliteExprDelete(pLeft);
221 pExpr->pLeft = 0;
222 sqliteExprDelete(pRight);
223 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000224 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000225 break;
226 }
227
drhfef52082000-06-06 01:50:43 +0000228 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000229 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000230 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000231 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
232 return 1;
233 }
drhfef52082000-06-06 01:50:43 +0000234 if( pExpr->pSelect ){
235 /* Case 1: expr IN (SELECT ...)
236 **
237 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000238 ** table. The cursor number of the temporary table has already
239 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000240 */
drh345fda32001-01-15 22:51:08 +0000241 sqliteVdbeAddOp(v, OP_OpenIdx, pExpr->iTable, 1, 0, 0);
drhfef52082000-06-06 01:50:43 +0000242 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
243 }else if( pExpr->pList ){
244 /* Case 2: expr IN (exprlist)
245 **
246 ** Create a set to put the exprlist values in. The Set id is stored
247 ** in iTable.
248 */
249 int i, iSet;
250 for(i=0; i<pExpr->pList->nExpr; i++){
251 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000252 if( !isConstant(pE2) ){
253 sqliteSetString(&pParse->zErrMsg,
254 "right-hand side of IN operator must be constant", 0);
255 pParse->nErr++;
256 return 1;
257 }
drh4794b982000-06-06 13:54:14 +0000258 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
259 return 1;
260 }
drhfef52082000-06-06 01:50:43 +0000261 }
262 iSet = pExpr->iTable = pParse->nSet++;
263 for(i=0; i<pExpr->pList->nExpr; i++){
264 Expr *pE2 = pExpr->pList->a[i].pExpr;
265 switch( pE2->op ){
266 case TK_FLOAT:
267 case TK_INTEGER:
268 case TK_STRING: {
269 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
270 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
271 sqliteVdbeDequoteP3(v, addr);
272 break;
273 }
274 default: {
275 sqliteExprCode(pParse, pE2);
276 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
277 break;
278 }
279 }
280 }
281 }
drhcfab11b2000-06-06 03:31:22 +0000282 break;
drhfef52082000-06-06 01:50:43 +0000283 }
284
drh19a775c2000-06-05 18:54:46 +0000285 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000286 /* This has to be a scalar SELECT. Generate code to put the
287 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000288 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000289 */
drh967e8b72000-06-21 13:59:10 +0000290 pExpr->iColumn = pParse->nMem++;
291 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000292 return 1;
293 }
294 break;
295 }
296
drhcce7d172000-05-31 15:34:51 +0000297 /* For all else, just recursively walk the tree */
298 default: {
drh4794b982000-06-06 13:54:14 +0000299 if( pExpr->pLeft
300 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000301 return 1;
302 }
303 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000304 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000305 return 1;
306 }
307 if( pExpr->pList ){
308 int i;
309 ExprList *pList = pExpr->pList;
310 for(i=0; i<pList->nExpr; i++){
311 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
312 return 1;
313 }
314 }
315 }
316 }
317 }
318 return 0;
319}
320
321#if 0 /* NOT USED */
322/*
323** Compare a token against a string. Return TRUE if they match.
324*/
325static int sqliteTokenCmp(Token *pToken, const char *zStr){
326 int n = strlen(zStr);
327 if( n!=pToken->n ) return 0;
328 return sqliteStrNICmp(pToken->z, zStr, n)==0;
329}
330#endif
331
332/*
333** Convert a function name into its integer identifier. Return the
334** identifier. Return FN_Unknown if the function name is unknown.
335*/
336int sqliteFuncId(Token *pToken){
337 static const struct {
338 char *zName;
339 int len;
340 int id;
341 } aFunc[] = {
342 { "count", 5, FN_Count },
343 { "min", 3, FN_Min },
344 { "max", 3, FN_Max },
345 { "sum", 3, FN_Sum },
drh22827922000-06-06 17:27:05 +0000346 { "avg", 3, FN_Avg },
drh0bdaf622000-06-11 23:50:13 +0000347 { "fcnt", 4, FN_Fcnt }, /* Used for testing only */
drh6ec27332000-08-28 15:51:43 +0000348 { "length", 6, FN_Length},
349 { "substr", 6, FN_Substr},
drhcce7d172000-05-31 15:34:51 +0000350 };
351 int i;
352 for(i=0; i<ArraySize(aFunc); i++){
353 if( aFunc[i].len==pToken->n
354 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
355 return aFunc[i].id;
356 }
357 }
358 return FN_Unknown;
359}
360
361/*
362** Error check the functions in an expression. Make sure all
363** function names are recognized and all functions have the correct
364** number of arguments. Leave an error message in pParse->zErrMsg
365** if anything is amiss. Return the number of errors.
366**
367** if pIsAgg is not null and this expression is an aggregate function
368** (like count(*) or max(value)) then write a 1 into *pIsAgg.
369*/
370int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
371 int nErr = 0;
372 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000373 switch( pExpr->op ){
374 case TK_FUNCTION: {
375 int id = sqliteFuncId(&pExpr->token);
376 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
377 int no_such_func = 0;
378 int too_many_args = 0;
379 int too_few_args = 0;
380 int is_agg = 0;
381 int i;
drh967e8b72000-06-21 13:59:10 +0000382 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000383 switch( id ){
384 case FN_Unknown: {
385 no_such_func = 1;
386 break;
387 }
388 case FN_Count: {
389 no_such_func = !allowAgg;
390 too_many_args = n>1;
391 is_agg = 1;
392 break;
393 }
394 case FN_Max:
395 case FN_Min: {
396 too_few_args = allowAgg ? n<1 : n<2;
397 is_agg = n==1;
398 break;
399 }
drh22827922000-06-06 17:27:05 +0000400 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000401 case FN_Sum: {
402 no_such_func = !allowAgg;
403 too_many_args = n>1;
404 too_few_args = n<1;
405 is_agg = 1;
406 break;
407 }
drh6ec27332000-08-28 15:51:43 +0000408 case FN_Length: {
409 too_few_args = n<1;
410 too_many_args = n>1;
411 break;
412 }
413 case FN_Substr: {
414 too_few_args = n<3;
415 too_many_args = n>3;
416 break;
417 }
drh0bdaf622000-06-11 23:50:13 +0000418 /* The "fcnt(*)" function always returns the number of fetch
419 ** operations that have occurred so far while processing the
420 ** SQL statement. This information can be used by test procedures
421 ** to verify that indices are being used properly to minimize
422 ** searching. All arguments to fcnt() are ignored. fcnt() has
423 ** no use (other than testing) that we are aware of.
424 */
425 case FN_Fcnt: {
426 n = 0;
427 break;
428 }
drh6ec27332000-08-28 15:51:43 +0000429
drhcce7d172000-05-31 15:34:51 +0000430 default: break;
431 }
432 if( no_such_func ){
433 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
434 pExpr->token.z, pExpr->token.n, 0);
435 pParse->nErr++;
436 nErr++;
437 }else if( too_many_args ){
438 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
439 pExpr->token.z, pExpr->token.n, "()", 2, 0);
440 pParse->nErr++;
441 nErr++;
442 }else if( too_few_args ){
443 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
444 pExpr->token.z, pExpr->token.n, "()", 2, 0);
445 pParse->nErr++;
446 nErr++;
447 }
drh22827922000-06-06 17:27:05 +0000448 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000449 if( is_agg && pIsAgg ) *pIsAgg = 1;
450 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000451 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
452 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000453 }
454 }
455 default: {
456 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000457 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000458 }
459 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000460 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000461 }
drhfef52082000-06-06 01:50:43 +0000462 if( nErr==0 && pExpr->pList ){
463 int n = pExpr->pList->nExpr;
464 int i;
465 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000466 Expr *pE2 = pExpr->pList->a[i].pExpr;
467 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000468 }
469 }
drhcce7d172000-05-31 15:34:51 +0000470 break;
471 }
472 }
473 return nErr;
474}
475
476/*
477** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000478** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000479*/
480void sqliteExprCode(Parse *pParse, Expr *pExpr){
481 Vdbe *v = pParse->pVdbe;
482 int op;
483 switch( pExpr->op ){
484 case TK_PLUS: op = OP_Add; break;
485 case TK_MINUS: op = OP_Subtract; break;
486 case TK_STAR: op = OP_Multiply; break;
487 case TK_SLASH: op = OP_Divide; break;
488 case TK_AND: op = OP_And; break;
489 case TK_OR: op = OP_Or; break;
490 case TK_LT: op = OP_Lt; break;
491 case TK_LE: op = OP_Le; break;
492 case TK_GT: op = OP_Gt; break;
493 case TK_GE: op = OP_Ge; break;
494 case TK_NE: op = OP_Ne; break;
495 case TK_EQ: op = OP_Eq; break;
496 case TK_LIKE: op = OP_Like; break;
497 case TK_GLOB: op = OP_Glob; break;
498 case TK_ISNULL: op = OP_IsNull; break;
499 case TK_NOTNULL: op = OP_NotNull; break;
500 case TK_NOT: op = OP_Not; break;
501 case TK_UMINUS: op = OP_Negative; break;
502 default: break;
503 }
504 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000505 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000506 if( pParse->useAgg ){
507 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000508 }else if( pExpr->iColumn>=0 ){
drh967e8b72000-06-21 13:59:10 +0000509 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iColumn, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000510 }else{
511 sqliteVdbeAddOp(v, OP_Key, pExpr->iTable, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000512 }
drhcce7d172000-05-31 15:34:51 +0000513 break;
514 }
515 case TK_INTEGER: {
516 int i = atoi(pExpr->token.z);
517 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
518 break;
519 }
520 case TK_FLOAT: {
521 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
522 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
523 break;
524 }
525 case TK_STRING: {
526 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
527 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
528 sqliteVdbeDequoteP3(v, addr);
529 break;
530 }
531 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000532 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000533 break;
534 }
535 case TK_AND:
536 case TK_OR:
537 case TK_PLUS:
538 case TK_STAR:
539 case TK_MINUS:
540 case TK_SLASH: {
541 sqliteExprCode(pParse, pExpr->pLeft);
542 sqliteExprCode(pParse, pExpr->pRight);
543 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
544 break;
545 }
drh00400772000-06-16 20:51:26 +0000546 case TK_CONCAT: {
547 sqliteExprCode(pParse, pExpr->pLeft);
548 sqliteExprCode(pParse, pExpr->pRight);
549 sqliteVdbeAddOp(v, OP_Concat, 2, 0, 0, 0);
550 break;
551 }
drhcce7d172000-05-31 15:34:51 +0000552 case TK_LT:
553 case TK_LE:
554 case TK_GT:
555 case TK_GE:
556 case TK_NE:
557 case TK_EQ:
558 case TK_LIKE:
559 case TK_GLOB: {
560 int dest;
561 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
562 sqliteExprCode(pParse, pExpr->pLeft);
563 sqliteExprCode(pParse, pExpr->pRight);
564 dest = sqliteVdbeCurrentAddr(v) + 2;
565 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
566 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
567 break;
568 }
drhcce7d172000-05-31 15:34:51 +0000569 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000570 assert( pExpr->pLeft );
571 if( pExpr->pLeft->op==TK_INTEGER ){
572 int i = atoi(pExpr->pLeft->token.z);
573 sqliteVdbeAddOp(v, OP_Integer, -i, 0, 0, 0);
574 break;
575 }else if( pExpr->pLeft->op==TK_FLOAT ){
576 Token *p = &pExpr->pLeft->token;
577 char *z = sqliteMalloc( p->n + 2 );
578 sprintf(z, "-%.*s", p->n, p->z);
579 sqliteVdbeAddOp(v, OP_String, 0, 0, z, 0);
580 sqliteFree(z);
581 break;
582 }
drh1ccde152000-06-17 13:12:39 +0000583 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000584 }
585 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000586 sqliteExprCode(pParse, pExpr->pLeft);
587 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
588 break;
589 }
590 case TK_ISNULL:
591 case TK_NOTNULL: {
592 int dest;
drh8be51132000-06-03 19:19:41 +0000593 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000594 sqliteExprCode(pParse, pExpr->pLeft);
595 dest = sqliteVdbeCurrentAddr(v) + 2;
596 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000597 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000598 break;
599 }
drh22827922000-06-06 17:27:05 +0000600 case TK_AGG_FUNCTION: {
601 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000602 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000603 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
604 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount, 0, 0);
605 sqliteVdbeAddOp(v, OP_Divide, 0, 0, 0, 0);
606 }
607 break;
608 }
drhcce7d172000-05-31 15:34:51 +0000609 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000610 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000611 int op;
612 int i;
613 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000614 switch( id ){
615 case FN_Fcnt: {
616 sqliteVdbeAddOp(v, OP_Fcnt, 0, 0, 0, 0);
617 break;
618 }
619 case FN_Min:
620 case FN_Max: {
621 op = id==FN_Min ? OP_Min : OP_Max;
622 for(i=0; i<pList->nExpr; i++){
623 sqliteExprCode(pParse, pList->a[i].pExpr);
624 if( i>0 ){
625 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
626 }
627 }
628 break;
629 }
630 case FN_Length: {
631 sqliteExprCode(pParse, pList->a[0].pExpr);
632 sqliteVdbeAddOp(v, OP_Strlen, 0, 0, 0, 0);
633 break;
634 }
635 case FN_Substr: {
636 for(i=0; i<pList->nExpr; i++){
637 sqliteExprCode(pParse, pList->a[i].pExpr);
638 }
639 sqliteVdbeAddOp(v, OP_Substr, 0, 0, 0, 0);
640 break;
641 }
642 default: {
643 /* Can't happen! */
644 break;
drhcce7d172000-05-31 15:34:51 +0000645 }
646 }
647 break;
648 }
drh19a775c2000-06-05 18:54:46 +0000649 case TK_SELECT: {
drh967e8b72000-06-21 13:59:10 +0000650 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000651 break;
652 }
drhfef52082000-06-06 01:50:43 +0000653 case TK_IN: {
654 int addr;
drh4794b982000-06-06 13:54:14 +0000655 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000656 sqliteExprCode(pParse, pExpr->pLeft);
657 addr = sqliteVdbeCurrentAddr(v);
658 if( pExpr->pSelect ){
659 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
660 }else{
661 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
662 }
drh4794b982000-06-06 13:54:14 +0000663 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000664 break;
665 }
666 case TK_BETWEEN: {
667 int lbl = sqliteVdbeMakeLabel(v);
668 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
669 sqliteExprIfFalse(pParse, pExpr, lbl);
670 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
671 sqliteVdbeResolveLabel(v, lbl);
672 break;
673 }
drhcce7d172000-05-31 15:34:51 +0000674 }
675 return;
676}
677
678/*
679** Generate code for a boolean expression such that a jump is made
680** to the label "dest" if the expression is true but execution
681** continues straight thru if the expression is false.
682*/
683void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
684 Vdbe *v = pParse->pVdbe;
685 int op = 0;
686 switch( pExpr->op ){
687 case TK_LT: op = OP_Lt; break;
688 case TK_LE: op = OP_Le; break;
689 case TK_GT: op = OP_Gt; break;
690 case TK_GE: op = OP_Ge; break;
691 case TK_NE: op = OP_Ne; break;
692 case TK_EQ: op = OP_Eq; break;
693 case TK_LIKE: op = OP_Like; break;
694 case TK_GLOB: op = OP_Glob; break;
695 case TK_ISNULL: op = OP_IsNull; break;
696 case TK_NOTNULL: op = OP_NotNull; break;
697 default: break;
698 }
699 switch( pExpr->op ){
700 case TK_AND: {
701 int d2 = sqliteVdbeMakeLabel(v);
702 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
703 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
704 sqliteVdbeResolveLabel(v, d2);
705 break;
706 }
707 case TK_OR: {
708 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
709 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
710 break;
711 }
712 case TK_NOT: {
713 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
714 break;
715 }
716 case TK_LT:
717 case TK_LE:
718 case TK_GT:
719 case TK_GE:
720 case TK_NE:
721 case TK_EQ:
722 case TK_LIKE:
723 case TK_GLOB: {
724 sqliteExprCode(pParse, pExpr->pLeft);
725 sqliteExprCode(pParse, pExpr->pRight);
726 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
727 break;
728 }
729 case TK_ISNULL:
730 case TK_NOTNULL: {
731 sqliteExprCode(pParse, pExpr->pLeft);
732 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
733 break;
734 }
drhfef52082000-06-06 01:50:43 +0000735 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000736 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000737 if( pExpr->pSelect ){
738 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
739 }else{
740 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
741 }
742 break;
743 }
744 case TK_BETWEEN: {
745 int lbl = sqliteVdbeMakeLabel(v);
746 sqliteExprCode(pParse, pExpr->pLeft);
747 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
748 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
749 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
750 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
751 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
752 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
753 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
754 break;
755 }
drhcce7d172000-05-31 15:34:51 +0000756 default: {
757 sqliteExprCode(pParse, pExpr);
758 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
759 break;
760 }
761 }
762}
763
764/*
drh66b89c82000-11-28 20:47:17 +0000765** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000766** to the label "dest" if the expression is false but execution
767** continues straight thru if the expression is true.
768*/
769void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
770 Vdbe *v = pParse->pVdbe;
771 int op = 0;
772 switch( pExpr->op ){
773 case TK_LT: op = OP_Ge; break;
774 case TK_LE: op = OP_Gt; break;
775 case TK_GT: op = OP_Le; break;
776 case TK_GE: op = OP_Lt; break;
777 case TK_NE: op = OP_Eq; break;
778 case TK_EQ: op = OP_Ne; break;
779 case TK_LIKE: op = OP_Like; break;
780 case TK_GLOB: op = OP_Glob; break;
781 case TK_ISNULL: op = OP_NotNull; break;
782 case TK_NOTNULL: op = OP_IsNull; break;
783 default: break;
784 }
785 switch( pExpr->op ){
786 case TK_AND: {
787 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
788 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
789 break;
790 }
791 case TK_OR: {
792 int d2 = sqliteVdbeMakeLabel(v);
793 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
794 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
795 sqliteVdbeResolveLabel(v, d2);
796 break;
797 }
798 case TK_NOT: {
799 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
800 break;
801 }
802 case TK_LT:
803 case TK_LE:
804 case TK_GT:
805 case TK_GE:
806 case TK_NE:
807 case TK_EQ: {
808 sqliteExprCode(pParse, pExpr->pLeft);
809 sqliteExprCode(pParse, pExpr->pRight);
810 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
811 break;
812 }
813 case TK_LIKE:
814 case TK_GLOB: {
815 sqliteExprCode(pParse, pExpr->pLeft);
816 sqliteExprCode(pParse, pExpr->pRight);
817 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
818 break;
819 }
820 case TK_ISNULL:
821 case TK_NOTNULL: {
822 sqliteExprCode(pParse, pExpr->pLeft);
823 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
824 break;
825 }
drhfef52082000-06-06 01:50:43 +0000826 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000827 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000828 if( pExpr->pSelect ){
829 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
830 }else{
831 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
832 }
833 break;
834 }
835 case TK_BETWEEN: {
836 int addr;
837 sqliteExprCode(pParse, pExpr->pLeft);
838 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
839 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
840 addr = sqliteVdbeCurrentAddr(v);
841 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
842 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
843 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
844 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
845 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
846 break;
847 }
drhcce7d172000-05-31 15:34:51 +0000848 default: {
849 sqliteExprCode(pParse, pExpr);
850 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
851 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
852 break;
853 }
854 }
855}
drh22827922000-06-06 17:27:05 +0000856
857/*
858** Do a deep comparison of two expression trees. Return TRUE (non-zero)
859** if they are identical and return FALSE if they differ in any way.
860*/
drhd8bc7082000-06-07 23:51:50 +0000861int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +0000862 int i;
863 if( pA==0 ){
864 return pB==0;
865 }else if( pB==0 ){
866 return 0;
867 }
868 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +0000869 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
870 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +0000871 if( pA->pList ){
872 if( pB->pList==0 ) return 0;
873 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
874 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000875 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +0000876 return 0;
877 }
878 }
879 }else if( pB->pList ){
880 return 0;
881 }
882 if( pA->pSelect || pB->pSelect ) return 0;
883 if( pA->token.z ){
884 if( pB->token.z==0 ) return 0;
885 if( pB->token.n!=pA->token.n ) return 0;
886 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
887 }
888 return 1;
889}
890
891/*
892** Add a new element to the pParse->aAgg[] array and return its index.
893*/
894static int appendAggInfo(Parse *pParse){
895 if( (pParse->nAgg & 0x7)==0 ){
896 int amt = pParse->nAgg + 8;
897 pParse->aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
898 if( pParse->aAgg==0 ){
899 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
900 pParse->nErr++;
901 return -1;
902 }
903 }
904 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
905 return pParse->nAgg++;
906}
907
908/*
909** Analyze the given expression looking for aggregate functions and
910** for variables that need to be added to the pParse->aAgg[] array.
911** Make additional entries to the pParse->aAgg[] array as necessary.
912**
913** This routine should only be called after the expression has been
914** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
915**
916** If errors are seen, leave an error message in zErrMsg and return
917** the number of errors.
918*/
919int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
920 int i;
921 AggExpr *aAgg;
922 int nErr = 0;
923
924 if( pExpr==0 ) return 0;
925 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000926 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000927 aAgg = pParse->aAgg;
928 for(i=0; i<pParse->nAgg; i++){
929 if( aAgg[i].isAgg ) continue;
930 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +0000931 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +0000932 break;
933 }
934 }
935 if( i>=pParse->nAgg ){
936 i = appendAggInfo(pParse);
937 if( i<0 ) return 1;
938 pParse->aAgg[i].isAgg = 0;
939 pParse->aAgg[i].pExpr = pExpr;
940 }
drhaaf88722000-06-08 11:25:00 +0000941 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +0000942 break;
943 }
944 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000945 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000946 if( pParse->iAggCount>=0 ){
947 i = pParse->iAggCount;
948 }else{
949 i = appendAggInfo(pParse);
950 if( i<0 ) return 1;
951 pParse->aAgg[i].isAgg = 1;
952 pParse->aAgg[i].pExpr = 0;
953 pParse->iAggCount = i;
954 }
drh967e8b72000-06-21 13:59:10 +0000955 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +0000956 pExpr->iAgg = i;
957 break;
958 }
959 }
960 aAgg = pParse->aAgg;
961 for(i=0; i<pParse->nAgg; i++){
962 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +0000963 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +0000964 break;
965 }
966 }
967 if( i>=pParse->nAgg ){
968 i = appendAggInfo(pParse);
969 if( i<0 ) return 1;
970 pParse->aAgg[i].isAgg = 1;
971 pParse->aAgg[i].pExpr = pExpr;
972 }
973 pExpr->iAgg = i;
974 break;
975 }
976 default: {
977 if( pExpr->pLeft ){
978 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
979 }
980 if( nErr==0 && pExpr->pRight ){
981 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
982 }
983 if( nErr==0 && pExpr->pList ){
984 int n = pExpr->pList->nExpr;
985 int i;
986 for(i=0; nErr==0 && i<n; i++){
987 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
988 }
989 }
990 break;
991 }
992 }
993 return nErr;
994}