blob: 29ee44c5592673a8c8d9fd138140bbf0bd32232f [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**
drhdaffd0e2001-04-11 14:28:42 +000027** $Id: expr.c,v 1.24 2001/04/11 14:28:42 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){
drhdaffd0e2001-04-11 14:28:42 +0000126 if( pExpr==0 || pTabList==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000127 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);
drhdaffd0e2001-04-11 14:28:42 +0000133 if( z==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000134 for(i=0; i<pTabList->nId; i++){
135 int j;
136 Table *pTab = pTabList->a[i].pTab;
137 if( pTab==0 ) continue;
138 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000139 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000140 cnt++;
drh19a775c2000-06-05 18:54:46 +0000141 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000142 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000143 }
144 }
145 }
drhc4a3c772001-04-04 11:48:57 +0000146 if( cnt==0 && sqliteIsRowid(z) ){
147 pExpr->iColumn = -1;
148 pExpr->iTable = pParse->nTab;
149 cnt = 1 + (pTabList->nId>1);
150 }
drhcce7d172000-05-31 15:34:51 +0000151 sqliteFree(z);
152 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000153 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000154 pExpr->token.z, pExpr->token.n, 0);
155 pParse->nErr++;
156 return 1;
157 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000158 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000159 pExpr->token.z, pExpr->token.n, 0);
160 pParse->nErr++;
161 return 1;
162 }
drh967e8b72000-06-21 13:59:10 +0000163 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000164 break;
165 }
166
drh967e8b72000-06-21 13:59:10 +0000167 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000168 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000169 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000170 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000171 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000172 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000173 char *zLeft, *zRight; /* Text of an identifier */
174
175 pLeft = pExpr->pLeft;
176 pRight = pExpr->pRight;
177 assert( pLeft && pLeft->op==TK_ID );
178 assert( pRight && pRight->op==TK_ID );
drh6e142f52000-06-08 13:36:40 +0000179 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
180 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000181 if( zLeft==0 || zRight==0 ){
182 sqliteFree(zLeft);
183 sqliteFree(zRight);
184 return 1;
185 }
drhc4a3c772001-04-04 11:48:57 +0000186 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000187 for(i=0; i<pTabList->nId; i++){
188 int j;
189 char *zTab;
190 Table *pTab = pTabList->a[i].pTab;
191 if( pTab==0 ) continue;
192 if( pTabList->a[i].zAlias ){
193 zTab = pTabList->a[i].zAlias;
194 }else{
195 zTab = pTab->zName;
196 }
197 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhc4a3c772001-04-04 11:48:57 +0000198 if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000199 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000200 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000201 cnt++;
drh19a775c2000-06-05 18:54:46 +0000202 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000203 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000204 }
205 }
206 }
drhc4a3c772001-04-04 11:48:57 +0000207 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
208 cnt = 1;
209 pExpr->iColumn = -1;
210 }
drhcce7d172000-05-31 15:34:51 +0000211 sqliteFree(zLeft);
212 sqliteFree(zRight);
213 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000214 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -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 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000220 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000221 pLeft->token.z, pLeft->token.n, ".", 1,
222 pRight->token.z, pRight->token.n, 0);
223 pParse->nErr++;
224 return 1;
225 }
226 sqliteExprDelete(pLeft);
227 pExpr->pLeft = 0;
228 sqliteExprDelete(pRight);
229 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000230 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000231 break;
232 }
233
drhfef52082000-06-06 01:50:43 +0000234 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000235 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000236 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000237 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
238 return 1;
239 }
drhfef52082000-06-06 01:50:43 +0000240 if( pExpr->pSelect ){
241 /* Case 1: expr IN (SELECT ...)
242 **
243 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000244 ** table. The cursor number of the temporary table has already
245 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000246 */
drh345fda32001-01-15 22:51:08 +0000247 sqliteVdbeAddOp(v, OP_OpenIdx, pExpr->iTable, 1, 0, 0);
drhfef52082000-06-06 01:50:43 +0000248 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
249 }else if( pExpr->pList ){
250 /* Case 2: expr IN (exprlist)
251 **
252 ** Create a set to put the exprlist values in. The Set id is stored
253 ** in iTable.
254 */
255 int i, iSet;
256 for(i=0; i<pExpr->pList->nExpr; i++){
257 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000258 if( !isConstant(pE2) ){
259 sqliteSetString(&pParse->zErrMsg,
260 "right-hand side of IN operator must be constant", 0);
261 pParse->nErr++;
262 return 1;
263 }
drh4794b982000-06-06 13:54:14 +0000264 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
265 return 1;
266 }
drhfef52082000-06-06 01:50:43 +0000267 }
268 iSet = pExpr->iTable = pParse->nSet++;
269 for(i=0; i<pExpr->pList->nExpr; i++){
270 Expr *pE2 = pExpr->pList->a[i].pExpr;
271 switch( pE2->op ){
272 case TK_FLOAT:
273 case TK_INTEGER:
274 case TK_STRING: {
275 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
276 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
277 sqliteVdbeDequoteP3(v, addr);
278 break;
279 }
280 default: {
281 sqliteExprCode(pParse, pE2);
282 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
283 break;
284 }
285 }
286 }
287 }
drhcfab11b2000-06-06 03:31:22 +0000288 break;
drhfef52082000-06-06 01:50:43 +0000289 }
290
drh19a775c2000-06-05 18:54:46 +0000291 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000292 /* This has to be a scalar SELECT. Generate code to put the
293 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000294 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000295 */
drh967e8b72000-06-21 13:59:10 +0000296 pExpr->iColumn = pParse->nMem++;
297 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000298 return 1;
299 }
300 break;
301 }
302
drhcce7d172000-05-31 15:34:51 +0000303 /* For all else, just recursively walk the tree */
304 default: {
drh4794b982000-06-06 13:54:14 +0000305 if( pExpr->pLeft
306 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000307 return 1;
308 }
309 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000310 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000311 return 1;
312 }
313 if( pExpr->pList ){
314 int i;
315 ExprList *pList = pExpr->pList;
316 for(i=0; i<pList->nExpr; i++){
317 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
318 return 1;
319 }
320 }
321 }
322 }
323 }
324 return 0;
325}
326
327#if 0 /* NOT USED */
328/*
329** Compare a token against a string. Return TRUE if they match.
330*/
331static int sqliteTokenCmp(Token *pToken, const char *zStr){
332 int n = strlen(zStr);
333 if( n!=pToken->n ) return 0;
334 return sqliteStrNICmp(pToken->z, zStr, n)==0;
335}
336#endif
337
338/*
339** Convert a function name into its integer identifier. Return the
340** identifier. Return FN_Unknown if the function name is unknown.
341*/
342int sqliteFuncId(Token *pToken){
343 static const struct {
344 char *zName;
345 int len;
346 int id;
347 } aFunc[] = {
348 { "count", 5, FN_Count },
349 { "min", 3, FN_Min },
350 { "max", 3, FN_Max },
351 { "sum", 3, FN_Sum },
drh22827922000-06-06 17:27:05 +0000352 { "avg", 3, FN_Avg },
drh0bdaf622000-06-11 23:50:13 +0000353 { "fcnt", 4, FN_Fcnt }, /* Used for testing only */
drh6ec27332000-08-28 15:51:43 +0000354 { "length", 6, FN_Length},
355 { "substr", 6, FN_Substr},
drhcce7d172000-05-31 15:34:51 +0000356 };
357 int i;
358 for(i=0; i<ArraySize(aFunc); i++){
359 if( aFunc[i].len==pToken->n
360 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
361 return aFunc[i].id;
362 }
363 }
364 return FN_Unknown;
365}
366
367/*
368** Error check the functions in an expression. Make sure all
369** function names are recognized and all functions have the correct
370** number of arguments. Leave an error message in pParse->zErrMsg
371** if anything is amiss. Return the number of errors.
372**
373** if pIsAgg is not null and this expression is an aggregate function
374** (like count(*) or max(value)) then write a 1 into *pIsAgg.
375*/
376int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
377 int nErr = 0;
378 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000379 switch( pExpr->op ){
380 case TK_FUNCTION: {
381 int id = sqliteFuncId(&pExpr->token);
382 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
383 int no_such_func = 0;
384 int too_many_args = 0;
385 int too_few_args = 0;
386 int is_agg = 0;
387 int i;
drh967e8b72000-06-21 13:59:10 +0000388 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000389 switch( id ){
390 case FN_Unknown: {
391 no_such_func = 1;
392 break;
393 }
394 case FN_Count: {
395 no_such_func = !allowAgg;
396 too_many_args = n>1;
397 is_agg = 1;
398 break;
399 }
400 case FN_Max:
401 case FN_Min: {
402 too_few_args = allowAgg ? n<1 : n<2;
403 is_agg = n==1;
404 break;
405 }
drh22827922000-06-06 17:27:05 +0000406 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000407 case FN_Sum: {
408 no_such_func = !allowAgg;
409 too_many_args = n>1;
410 too_few_args = n<1;
411 is_agg = 1;
412 break;
413 }
drh6ec27332000-08-28 15:51:43 +0000414 case FN_Length: {
415 too_few_args = n<1;
416 too_many_args = n>1;
417 break;
418 }
419 case FN_Substr: {
420 too_few_args = n<3;
421 too_many_args = n>3;
422 break;
423 }
drh0bdaf622000-06-11 23:50:13 +0000424 /* The "fcnt(*)" function always returns the number of fetch
425 ** operations that have occurred so far while processing the
426 ** SQL statement. This information can be used by test procedures
427 ** to verify that indices are being used properly to minimize
428 ** searching. All arguments to fcnt() are ignored. fcnt() has
429 ** no use (other than testing) that we are aware of.
430 */
431 case FN_Fcnt: {
432 n = 0;
433 break;
434 }
drh6ec27332000-08-28 15:51:43 +0000435
drhcce7d172000-05-31 15:34:51 +0000436 default: break;
437 }
438 if( no_such_func ){
439 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
440 pExpr->token.z, pExpr->token.n, 0);
441 pParse->nErr++;
442 nErr++;
443 }else if( too_many_args ){
444 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
445 pExpr->token.z, pExpr->token.n, "()", 2, 0);
446 pParse->nErr++;
447 nErr++;
448 }else if( too_few_args ){
449 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
450 pExpr->token.z, pExpr->token.n, "()", 2, 0);
451 pParse->nErr++;
452 nErr++;
453 }
drh22827922000-06-06 17:27:05 +0000454 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000455 if( is_agg && pIsAgg ) *pIsAgg = 1;
456 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000457 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
458 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000459 }
460 }
461 default: {
462 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000463 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000464 }
465 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000466 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000467 }
drhfef52082000-06-06 01:50:43 +0000468 if( nErr==0 && pExpr->pList ){
469 int n = pExpr->pList->nExpr;
470 int i;
471 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000472 Expr *pE2 = pExpr->pList->a[i].pExpr;
473 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000474 }
475 }
drhcce7d172000-05-31 15:34:51 +0000476 break;
477 }
478 }
479 return nErr;
480}
481
482/*
483** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000484** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000485*/
486void sqliteExprCode(Parse *pParse, Expr *pExpr){
487 Vdbe *v = pParse->pVdbe;
488 int op;
drhdaffd0e2001-04-11 14:28:42 +0000489 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000490 switch( pExpr->op ){
491 case TK_PLUS: op = OP_Add; break;
492 case TK_MINUS: op = OP_Subtract; break;
493 case TK_STAR: op = OP_Multiply; break;
494 case TK_SLASH: op = OP_Divide; break;
495 case TK_AND: op = OP_And; break;
496 case TK_OR: op = OP_Or; break;
497 case TK_LT: op = OP_Lt; break;
498 case TK_LE: op = OP_Le; break;
499 case TK_GT: op = OP_Gt; break;
500 case TK_GE: op = OP_Ge; break;
501 case TK_NE: op = OP_Ne; break;
502 case TK_EQ: op = OP_Eq; break;
503 case TK_LIKE: op = OP_Like; break;
504 case TK_GLOB: op = OP_Glob; break;
505 case TK_ISNULL: op = OP_IsNull; break;
506 case TK_NOTNULL: op = OP_NotNull; break;
507 case TK_NOT: op = OP_Not; break;
508 case TK_UMINUS: op = OP_Negative; break;
509 default: break;
510 }
511 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000512 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000513 if( pParse->useAgg ){
514 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000515 }else if( pExpr->iColumn>=0 ){
drh967e8b72000-06-21 13:59:10 +0000516 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iColumn, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000517 }else{
518 sqliteVdbeAddOp(v, OP_Key, pExpr->iTable, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000519 }
drhcce7d172000-05-31 15:34:51 +0000520 break;
521 }
522 case TK_INTEGER: {
523 int i = atoi(pExpr->token.z);
524 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
525 break;
526 }
527 case TK_FLOAT: {
528 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
529 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
530 break;
531 }
532 case TK_STRING: {
533 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
534 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
535 sqliteVdbeDequoteP3(v, addr);
536 break;
537 }
538 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000539 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000540 break;
541 }
542 case TK_AND:
543 case TK_OR:
544 case TK_PLUS:
545 case TK_STAR:
546 case TK_MINUS:
547 case TK_SLASH: {
548 sqliteExprCode(pParse, pExpr->pLeft);
549 sqliteExprCode(pParse, pExpr->pRight);
550 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
551 break;
552 }
drh00400772000-06-16 20:51:26 +0000553 case TK_CONCAT: {
554 sqliteExprCode(pParse, pExpr->pLeft);
555 sqliteExprCode(pParse, pExpr->pRight);
556 sqliteVdbeAddOp(v, OP_Concat, 2, 0, 0, 0);
557 break;
558 }
drhcce7d172000-05-31 15:34:51 +0000559 case TK_LT:
560 case TK_LE:
561 case TK_GT:
562 case TK_GE:
563 case TK_NE:
564 case TK_EQ:
565 case TK_LIKE:
566 case TK_GLOB: {
567 int dest;
568 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
569 sqliteExprCode(pParse, pExpr->pLeft);
570 sqliteExprCode(pParse, pExpr->pRight);
571 dest = sqliteVdbeCurrentAddr(v) + 2;
572 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
573 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
574 break;
575 }
drhcce7d172000-05-31 15:34:51 +0000576 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000577 assert( pExpr->pLeft );
578 if( pExpr->pLeft->op==TK_INTEGER ){
579 int i = atoi(pExpr->pLeft->token.z);
580 sqliteVdbeAddOp(v, OP_Integer, -i, 0, 0, 0);
581 break;
582 }else if( pExpr->pLeft->op==TK_FLOAT ){
583 Token *p = &pExpr->pLeft->token;
584 char *z = sqliteMalloc( p->n + 2 );
585 sprintf(z, "-%.*s", p->n, p->z);
586 sqliteVdbeAddOp(v, OP_String, 0, 0, z, 0);
587 sqliteFree(z);
588 break;
589 }
drh1ccde152000-06-17 13:12:39 +0000590 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000591 }
592 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000593 sqliteExprCode(pParse, pExpr->pLeft);
594 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
595 break;
596 }
597 case TK_ISNULL:
598 case TK_NOTNULL: {
599 int dest;
drh8be51132000-06-03 19:19:41 +0000600 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000601 sqliteExprCode(pParse, pExpr->pLeft);
602 dest = sqliteVdbeCurrentAddr(v) + 2;
603 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000604 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000605 break;
606 }
drh22827922000-06-06 17:27:05 +0000607 case TK_AGG_FUNCTION: {
608 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000609 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000610 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
611 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount, 0, 0);
612 sqliteVdbeAddOp(v, OP_Divide, 0, 0, 0, 0);
613 }
614 break;
615 }
drhcce7d172000-05-31 15:34:51 +0000616 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000617 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000618 int op;
619 int i;
620 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000621 switch( id ){
622 case FN_Fcnt: {
623 sqliteVdbeAddOp(v, OP_Fcnt, 0, 0, 0, 0);
624 break;
625 }
626 case FN_Min:
627 case FN_Max: {
628 op = id==FN_Min ? OP_Min : OP_Max;
629 for(i=0; i<pList->nExpr; i++){
630 sqliteExprCode(pParse, pList->a[i].pExpr);
631 if( i>0 ){
632 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
633 }
634 }
635 break;
636 }
637 case FN_Length: {
638 sqliteExprCode(pParse, pList->a[0].pExpr);
639 sqliteVdbeAddOp(v, OP_Strlen, 0, 0, 0, 0);
640 break;
641 }
642 case FN_Substr: {
643 for(i=0; i<pList->nExpr; i++){
644 sqliteExprCode(pParse, pList->a[i].pExpr);
645 }
646 sqliteVdbeAddOp(v, OP_Substr, 0, 0, 0, 0);
647 break;
648 }
649 default: {
650 /* Can't happen! */
651 break;
drhcce7d172000-05-31 15:34:51 +0000652 }
653 }
654 break;
655 }
drh19a775c2000-06-05 18:54:46 +0000656 case TK_SELECT: {
drh967e8b72000-06-21 13:59:10 +0000657 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000658 break;
659 }
drhfef52082000-06-06 01:50:43 +0000660 case TK_IN: {
661 int addr;
drh4794b982000-06-06 13:54:14 +0000662 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000663 sqliteExprCode(pParse, pExpr->pLeft);
664 addr = sqliteVdbeCurrentAddr(v);
665 if( pExpr->pSelect ){
666 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
667 }else{
668 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
669 }
drh4794b982000-06-06 13:54:14 +0000670 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000671 break;
672 }
673 case TK_BETWEEN: {
674 int lbl = sqliteVdbeMakeLabel(v);
675 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
676 sqliteExprIfFalse(pParse, pExpr, lbl);
677 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
678 sqliteVdbeResolveLabel(v, lbl);
679 break;
680 }
drhcce7d172000-05-31 15:34:51 +0000681 }
682 return;
683}
684
685/*
686** Generate code for a boolean expression such that a jump is made
687** to the label "dest" if the expression is true but execution
688** continues straight thru if the expression is false.
689*/
690void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
691 Vdbe *v = pParse->pVdbe;
692 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000693 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000694 switch( pExpr->op ){
695 case TK_LT: op = OP_Lt; break;
696 case TK_LE: op = OP_Le; break;
697 case TK_GT: op = OP_Gt; break;
698 case TK_GE: op = OP_Ge; break;
699 case TK_NE: op = OP_Ne; break;
700 case TK_EQ: op = OP_Eq; break;
701 case TK_LIKE: op = OP_Like; break;
702 case TK_GLOB: op = OP_Glob; break;
703 case TK_ISNULL: op = OP_IsNull; break;
704 case TK_NOTNULL: op = OP_NotNull; break;
705 default: break;
706 }
707 switch( pExpr->op ){
708 case TK_AND: {
709 int d2 = sqliteVdbeMakeLabel(v);
710 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
711 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
712 sqliteVdbeResolveLabel(v, d2);
713 break;
714 }
715 case TK_OR: {
716 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
717 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
718 break;
719 }
720 case TK_NOT: {
721 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
722 break;
723 }
724 case TK_LT:
725 case TK_LE:
726 case TK_GT:
727 case TK_GE:
728 case TK_NE:
729 case TK_EQ:
730 case TK_LIKE:
731 case TK_GLOB: {
732 sqliteExprCode(pParse, pExpr->pLeft);
733 sqliteExprCode(pParse, pExpr->pRight);
734 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
735 break;
736 }
737 case TK_ISNULL:
738 case TK_NOTNULL: {
739 sqliteExprCode(pParse, pExpr->pLeft);
740 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
741 break;
742 }
drhfef52082000-06-06 01:50:43 +0000743 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000744 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000745 if( pExpr->pSelect ){
746 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
747 }else{
748 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
749 }
750 break;
751 }
752 case TK_BETWEEN: {
753 int lbl = sqliteVdbeMakeLabel(v);
754 sqliteExprCode(pParse, pExpr->pLeft);
755 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
756 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
757 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
758 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
759 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
760 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
761 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
762 break;
763 }
drhcce7d172000-05-31 15:34:51 +0000764 default: {
765 sqliteExprCode(pParse, pExpr);
766 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
767 break;
768 }
769 }
770}
771
772/*
drh66b89c82000-11-28 20:47:17 +0000773** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000774** to the label "dest" if the expression is false but execution
775** continues straight thru if the expression is true.
776*/
777void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
778 Vdbe *v = pParse->pVdbe;
779 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000780 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000781 switch( pExpr->op ){
782 case TK_LT: op = OP_Ge; break;
783 case TK_LE: op = OP_Gt; break;
784 case TK_GT: op = OP_Le; break;
785 case TK_GE: op = OP_Lt; break;
786 case TK_NE: op = OP_Eq; break;
787 case TK_EQ: op = OP_Ne; break;
788 case TK_LIKE: op = OP_Like; break;
789 case TK_GLOB: op = OP_Glob; break;
790 case TK_ISNULL: op = OP_NotNull; break;
791 case TK_NOTNULL: op = OP_IsNull; break;
792 default: break;
793 }
794 switch( pExpr->op ){
795 case TK_AND: {
796 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
797 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
798 break;
799 }
800 case TK_OR: {
801 int d2 = sqliteVdbeMakeLabel(v);
802 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
803 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
804 sqliteVdbeResolveLabel(v, d2);
805 break;
806 }
807 case TK_NOT: {
808 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
809 break;
810 }
811 case TK_LT:
812 case TK_LE:
813 case TK_GT:
814 case TK_GE:
815 case TK_NE:
816 case TK_EQ: {
817 sqliteExprCode(pParse, pExpr->pLeft);
818 sqliteExprCode(pParse, pExpr->pRight);
819 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
820 break;
821 }
822 case TK_LIKE:
823 case TK_GLOB: {
824 sqliteExprCode(pParse, pExpr->pLeft);
825 sqliteExprCode(pParse, pExpr->pRight);
826 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
827 break;
828 }
829 case TK_ISNULL:
830 case TK_NOTNULL: {
831 sqliteExprCode(pParse, pExpr->pLeft);
832 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
833 break;
834 }
drhfef52082000-06-06 01:50:43 +0000835 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000836 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000837 if( pExpr->pSelect ){
838 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
839 }else{
840 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
841 }
842 break;
843 }
844 case TK_BETWEEN: {
845 int addr;
846 sqliteExprCode(pParse, pExpr->pLeft);
847 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
848 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
849 addr = sqliteVdbeCurrentAddr(v);
850 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
851 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
852 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
853 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
854 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
855 break;
856 }
drhcce7d172000-05-31 15:34:51 +0000857 default: {
858 sqliteExprCode(pParse, pExpr);
859 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
860 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
861 break;
862 }
863 }
864}
drh22827922000-06-06 17:27:05 +0000865
866/*
867** Do a deep comparison of two expression trees. Return TRUE (non-zero)
868** if they are identical and return FALSE if they differ in any way.
869*/
drhd8bc7082000-06-07 23:51:50 +0000870int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +0000871 int i;
872 if( pA==0 ){
873 return pB==0;
874 }else if( pB==0 ){
875 return 0;
876 }
877 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +0000878 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
879 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +0000880 if( pA->pList ){
881 if( pB->pList==0 ) return 0;
882 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
883 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000884 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +0000885 return 0;
886 }
887 }
888 }else if( pB->pList ){
889 return 0;
890 }
891 if( pA->pSelect || pB->pSelect ) return 0;
892 if( pA->token.z ){
893 if( pB->token.z==0 ) return 0;
894 if( pB->token.n!=pA->token.n ) return 0;
895 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
896 }
897 return 1;
898}
899
900/*
901** Add a new element to the pParse->aAgg[] array and return its index.
902*/
903static int appendAggInfo(Parse *pParse){
904 if( (pParse->nAgg & 0x7)==0 ){
905 int amt = pParse->nAgg + 8;
906 pParse->aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
907 if( pParse->aAgg==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000908 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000909 return -1;
910 }
911 }
912 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
913 return pParse->nAgg++;
914}
915
916/*
917** Analyze the given expression looking for aggregate functions and
918** for variables that need to be added to the pParse->aAgg[] array.
919** Make additional entries to the pParse->aAgg[] array as necessary.
920**
921** This routine should only be called after the expression has been
922** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
923**
924** If errors are seen, leave an error message in zErrMsg and return
925** the number of errors.
926*/
927int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
928 int i;
929 AggExpr *aAgg;
930 int nErr = 0;
931
932 if( pExpr==0 ) return 0;
933 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000934 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000935 aAgg = pParse->aAgg;
936 for(i=0; i<pParse->nAgg; i++){
937 if( aAgg[i].isAgg ) continue;
938 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +0000939 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +0000940 break;
941 }
942 }
943 if( i>=pParse->nAgg ){
944 i = appendAggInfo(pParse);
945 if( i<0 ) return 1;
946 pParse->aAgg[i].isAgg = 0;
947 pParse->aAgg[i].pExpr = pExpr;
948 }
drhaaf88722000-06-08 11:25:00 +0000949 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +0000950 break;
951 }
952 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000953 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000954 if( pParse->iAggCount>=0 ){
955 i = pParse->iAggCount;
956 }else{
957 i = appendAggInfo(pParse);
958 if( i<0 ) return 1;
959 pParse->aAgg[i].isAgg = 1;
960 pParse->aAgg[i].pExpr = 0;
961 pParse->iAggCount = i;
962 }
drh967e8b72000-06-21 13:59:10 +0000963 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +0000964 pExpr->iAgg = i;
965 break;
966 }
967 }
968 aAgg = pParse->aAgg;
969 for(i=0; i<pParse->nAgg; i++){
970 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +0000971 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +0000972 break;
973 }
974 }
975 if( i>=pParse->nAgg ){
976 i = appendAggInfo(pParse);
977 if( i<0 ) return 1;
978 pParse->aAgg[i].isAgg = 1;
979 pParse->aAgg[i].pExpr = pExpr;
980 }
981 pExpr->iAgg = i;
982 break;
983 }
984 default: {
985 if( pExpr->pLeft ){
986 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
987 }
988 if( nErr==0 && pExpr->pRight ){
989 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
990 }
991 if( nErr==0 && pExpr->pList ){
992 int n = pExpr->pList->nExpr;
993 int i;
994 for(i=0; nErr==0 && i<n; i++){
995 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
996 }
997 }
998 break;
999 }
1000 }
1001 return nErr;
1002}