blob: abc4963819a8e197bae15abfc4202874961eb52b [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**
drh87c40e82001-07-23 14:33:02 +000027** $Id: expr.c,v 1.25 2001/07/23 14:33:04 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 }
drh87c40e82001-07-23 14:33:02 +0000186 sqliteDequote(zLeft);
187 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000188 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000189 for(i=0; i<pTabList->nId; i++){
190 int j;
191 char *zTab;
192 Table *pTab = pTabList->a[i].pTab;
193 if( pTab==0 ) continue;
194 if( pTabList->a[i].zAlias ){
195 zTab = pTabList->a[i].zAlias;
196 }else{
197 zTab = pTab->zName;
198 }
199 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhc4a3c772001-04-04 11:48:57 +0000200 if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000201 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000202 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000203 cnt++;
drh19a775c2000-06-05 18:54:46 +0000204 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000205 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000206 }
207 }
208 }
drhc4a3c772001-04-04 11:48:57 +0000209 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
210 cnt = 1;
211 pExpr->iColumn = -1;
212 }
drhcce7d172000-05-31 15:34:51 +0000213 sqliteFree(zLeft);
214 sqliteFree(zRight);
215 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000216 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000217 pLeft->token.z, pLeft->token.n, ".", 1,
218 pRight->token.z, pRight->token.n, 0);
219 pParse->nErr++;
220 return 1;
221 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000222 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000223 pLeft->token.z, pLeft->token.n, ".", 1,
224 pRight->token.z, pRight->token.n, 0);
225 pParse->nErr++;
226 return 1;
227 }
228 sqliteExprDelete(pLeft);
229 pExpr->pLeft = 0;
230 sqliteExprDelete(pRight);
231 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000232 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000233 break;
234 }
235
drhfef52082000-06-06 01:50:43 +0000236 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000237 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000238 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000239 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
240 return 1;
241 }
drhfef52082000-06-06 01:50:43 +0000242 if( pExpr->pSelect ){
243 /* Case 1: expr IN (SELECT ...)
244 **
245 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000246 ** table. The cursor number of the temporary table has already
247 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000248 */
drh345fda32001-01-15 22:51:08 +0000249 sqliteVdbeAddOp(v, OP_OpenIdx, pExpr->iTable, 1, 0, 0);
drhfef52082000-06-06 01:50:43 +0000250 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
251 }else if( pExpr->pList ){
252 /* Case 2: expr IN (exprlist)
253 **
254 ** Create a set to put the exprlist values in. The Set id is stored
255 ** in iTable.
256 */
257 int i, iSet;
258 for(i=0; i<pExpr->pList->nExpr; i++){
259 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000260 if( !isConstant(pE2) ){
261 sqliteSetString(&pParse->zErrMsg,
262 "right-hand side of IN operator must be constant", 0);
263 pParse->nErr++;
264 return 1;
265 }
drh4794b982000-06-06 13:54:14 +0000266 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
267 return 1;
268 }
drhfef52082000-06-06 01:50:43 +0000269 }
270 iSet = pExpr->iTable = pParse->nSet++;
271 for(i=0; i<pExpr->pList->nExpr; i++){
272 Expr *pE2 = pExpr->pList->a[i].pExpr;
273 switch( pE2->op ){
274 case TK_FLOAT:
275 case TK_INTEGER:
276 case TK_STRING: {
277 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
278 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
279 sqliteVdbeDequoteP3(v, addr);
280 break;
281 }
282 default: {
283 sqliteExprCode(pParse, pE2);
284 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
285 break;
286 }
287 }
288 }
289 }
drhcfab11b2000-06-06 03:31:22 +0000290 break;
drhfef52082000-06-06 01:50:43 +0000291 }
292
drh19a775c2000-06-05 18:54:46 +0000293 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000294 /* This has to be a scalar SELECT. Generate code to put the
295 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000296 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000297 */
drh967e8b72000-06-21 13:59:10 +0000298 pExpr->iColumn = pParse->nMem++;
299 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000300 return 1;
301 }
302 break;
303 }
304
drhcce7d172000-05-31 15:34:51 +0000305 /* For all else, just recursively walk the tree */
306 default: {
drh4794b982000-06-06 13:54:14 +0000307 if( pExpr->pLeft
308 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000309 return 1;
310 }
311 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000312 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000313 return 1;
314 }
315 if( pExpr->pList ){
316 int i;
317 ExprList *pList = pExpr->pList;
318 for(i=0; i<pList->nExpr; i++){
319 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
320 return 1;
321 }
322 }
323 }
324 }
325 }
326 return 0;
327}
328
329#if 0 /* NOT USED */
330/*
331** Compare a token against a string. Return TRUE if they match.
332*/
333static int sqliteTokenCmp(Token *pToken, const char *zStr){
334 int n = strlen(zStr);
335 if( n!=pToken->n ) return 0;
336 return sqliteStrNICmp(pToken->z, zStr, n)==0;
337}
338#endif
339
340/*
341** Convert a function name into its integer identifier. Return the
342** identifier. Return FN_Unknown if the function name is unknown.
343*/
344int sqliteFuncId(Token *pToken){
345 static const struct {
346 char *zName;
347 int len;
348 int id;
349 } aFunc[] = {
350 { "count", 5, FN_Count },
351 { "min", 3, FN_Min },
352 { "max", 3, FN_Max },
353 { "sum", 3, FN_Sum },
drh22827922000-06-06 17:27:05 +0000354 { "avg", 3, FN_Avg },
drh0bdaf622000-06-11 23:50:13 +0000355 { "fcnt", 4, FN_Fcnt }, /* Used for testing only */
drh6ec27332000-08-28 15:51:43 +0000356 { "length", 6, FN_Length},
357 { "substr", 6, FN_Substr},
drhcce7d172000-05-31 15:34:51 +0000358 };
359 int i;
360 for(i=0; i<ArraySize(aFunc); i++){
361 if( aFunc[i].len==pToken->n
362 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
363 return aFunc[i].id;
364 }
365 }
366 return FN_Unknown;
367}
368
369/*
370** Error check the functions in an expression. Make sure all
371** function names are recognized and all functions have the correct
372** number of arguments. Leave an error message in pParse->zErrMsg
373** if anything is amiss. Return the number of errors.
374**
375** if pIsAgg is not null and this expression is an aggregate function
376** (like count(*) or max(value)) then write a 1 into *pIsAgg.
377*/
378int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
379 int nErr = 0;
380 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000381 switch( pExpr->op ){
382 case TK_FUNCTION: {
383 int id = sqliteFuncId(&pExpr->token);
384 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
385 int no_such_func = 0;
386 int too_many_args = 0;
387 int too_few_args = 0;
388 int is_agg = 0;
389 int i;
drh967e8b72000-06-21 13:59:10 +0000390 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000391 switch( id ){
392 case FN_Unknown: {
393 no_such_func = 1;
394 break;
395 }
396 case FN_Count: {
397 no_such_func = !allowAgg;
398 too_many_args = n>1;
399 is_agg = 1;
400 break;
401 }
402 case FN_Max:
403 case FN_Min: {
404 too_few_args = allowAgg ? n<1 : n<2;
405 is_agg = n==1;
406 break;
407 }
drh22827922000-06-06 17:27:05 +0000408 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000409 case FN_Sum: {
410 no_such_func = !allowAgg;
411 too_many_args = n>1;
412 too_few_args = n<1;
413 is_agg = 1;
414 break;
415 }
drh6ec27332000-08-28 15:51:43 +0000416 case FN_Length: {
417 too_few_args = n<1;
418 too_many_args = n>1;
419 break;
420 }
421 case FN_Substr: {
422 too_few_args = n<3;
423 too_many_args = n>3;
424 break;
425 }
drh0bdaf622000-06-11 23:50:13 +0000426 /* The "fcnt(*)" function always returns the number of fetch
427 ** operations that have occurred so far while processing the
428 ** SQL statement. This information can be used by test procedures
429 ** to verify that indices are being used properly to minimize
430 ** searching. All arguments to fcnt() are ignored. fcnt() has
431 ** no use (other than testing) that we are aware of.
432 */
433 case FN_Fcnt: {
434 n = 0;
435 break;
436 }
drh6ec27332000-08-28 15:51:43 +0000437
drhcce7d172000-05-31 15:34:51 +0000438 default: break;
439 }
440 if( no_such_func ){
441 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
442 pExpr->token.z, pExpr->token.n, 0);
443 pParse->nErr++;
444 nErr++;
445 }else if( too_many_args ){
446 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
447 pExpr->token.z, pExpr->token.n, "()", 2, 0);
448 pParse->nErr++;
449 nErr++;
450 }else if( too_few_args ){
451 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
452 pExpr->token.z, pExpr->token.n, "()", 2, 0);
453 pParse->nErr++;
454 nErr++;
455 }
drh22827922000-06-06 17:27:05 +0000456 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000457 if( is_agg && pIsAgg ) *pIsAgg = 1;
458 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000459 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
460 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000461 }
462 }
463 default: {
464 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000465 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000466 }
467 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000468 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000469 }
drhfef52082000-06-06 01:50:43 +0000470 if( nErr==0 && pExpr->pList ){
471 int n = pExpr->pList->nExpr;
472 int i;
473 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000474 Expr *pE2 = pExpr->pList->a[i].pExpr;
475 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000476 }
477 }
drhcce7d172000-05-31 15:34:51 +0000478 break;
479 }
480 }
481 return nErr;
482}
483
484/*
485** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000486** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000487*/
488void sqliteExprCode(Parse *pParse, Expr *pExpr){
489 Vdbe *v = pParse->pVdbe;
490 int op;
drhdaffd0e2001-04-11 14:28:42 +0000491 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000492 switch( pExpr->op ){
493 case TK_PLUS: op = OP_Add; break;
494 case TK_MINUS: op = OP_Subtract; break;
495 case TK_STAR: op = OP_Multiply; break;
496 case TK_SLASH: op = OP_Divide; break;
497 case TK_AND: op = OP_And; break;
498 case TK_OR: op = OP_Or; break;
499 case TK_LT: op = OP_Lt; break;
500 case TK_LE: op = OP_Le; break;
501 case TK_GT: op = OP_Gt; break;
502 case TK_GE: op = OP_Ge; break;
503 case TK_NE: op = OP_Ne; break;
504 case TK_EQ: op = OP_Eq; break;
505 case TK_LIKE: op = OP_Like; break;
506 case TK_GLOB: op = OP_Glob; break;
507 case TK_ISNULL: op = OP_IsNull; break;
508 case TK_NOTNULL: op = OP_NotNull; break;
509 case TK_NOT: op = OP_Not; break;
510 case TK_UMINUS: op = OP_Negative; break;
511 default: break;
512 }
513 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000514 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000515 if( pParse->useAgg ){
516 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000517 }else if( pExpr->iColumn>=0 ){
drh967e8b72000-06-21 13:59:10 +0000518 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iColumn, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000519 }else{
520 sqliteVdbeAddOp(v, OP_Key, pExpr->iTable, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000521 }
drhcce7d172000-05-31 15:34:51 +0000522 break;
523 }
524 case TK_INTEGER: {
525 int i = atoi(pExpr->token.z);
526 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
527 break;
528 }
529 case TK_FLOAT: {
530 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
531 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
532 break;
533 }
534 case TK_STRING: {
535 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
536 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
537 sqliteVdbeDequoteP3(v, addr);
538 break;
539 }
540 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000541 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000542 break;
543 }
544 case TK_AND:
545 case TK_OR:
546 case TK_PLUS:
547 case TK_STAR:
548 case TK_MINUS:
549 case TK_SLASH: {
550 sqliteExprCode(pParse, pExpr->pLeft);
551 sqliteExprCode(pParse, pExpr->pRight);
552 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
553 break;
554 }
drh00400772000-06-16 20:51:26 +0000555 case TK_CONCAT: {
556 sqliteExprCode(pParse, pExpr->pLeft);
557 sqliteExprCode(pParse, pExpr->pRight);
558 sqliteVdbeAddOp(v, OP_Concat, 2, 0, 0, 0);
559 break;
560 }
drhcce7d172000-05-31 15:34:51 +0000561 case TK_LT:
562 case TK_LE:
563 case TK_GT:
564 case TK_GE:
565 case TK_NE:
566 case TK_EQ:
567 case TK_LIKE:
568 case TK_GLOB: {
569 int dest;
570 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
571 sqliteExprCode(pParse, pExpr->pLeft);
572 sqliteExprCode(pParse, pExpr->pRight);
573 dest = sqliteVdbeCurrentAddr(v) + 2;
574 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
575 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
576 break;
577 }
drhcce7d172000-05-31 15:34:51 +0000578 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000579 assert( pExpr->pLeft );
580 if( pExpr->pLeft->op==TK_INTEGER ){
581 int i = atoi(pExpr->pLeft->token.z);
582 sqliteVdbeAddOp(v, OP_Integer, -i, 0, 0, 0);
583 break;
584 }else if( pExpr->pLeft->op==TK_FLOAT ){
585 Token *p = &pExpr->pLeft->token;
586 char *z = sqliteMalloc( p->n + 2 );
587 sprintf(z, "-%.*s", p->n, p->z);
588 sqliteVdbeAddOp(v, OP_String, 0, 0, z, 0);
589 sqliteFree(z);
590 break;
591 }
drh1ccde152000-06-17 13:12:39 +0000592 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000593 }
594 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000595 sqliteExprCode(pParse, pExpr->pLeft);
596 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
597 break;
598 }
599 case TK_ISNULL:
600 case TK_NOTNULL: {
601 int dest;
drh8be51132000-06-03 19:19:41 +0000602 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000603 sqliteExprCode(pParse, pExpr->pLeft);
604 dest = sqliteVdbeCurrentAddr(v) + 2;
605 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000606 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000607 break;
608 }
drh22827922000-06-06 17:27:05 +0000609 case TK_AGG_FUNCTION: {
610 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000611 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000612 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
613 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount, 0, 0);
614 sqliteVdbeAddOp(v, OP_Divide, 0, 0, 0, 0);
615 }
616 break;
617 }
drhcce7d172000-05-31 15:34:51 +0000618 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000619 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000620 int op;
621 int i;
622 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000623 switch( id ){
624 case FN_Fcnt: {
625 sqliteVdbeAddOp(v, OP_Fcnt, 0, 0, 0, 0);
626 break;
627 }
628 case FN_Min:
629 case FN_Max: {
630 op = id==FN_Min ? OP_Min : OP_Max;
631 for(i=0; i<pList->nExpr; i++){
632 sqliteExprCode(pParse, pList->a[i].pExpr);
633 if( i>0 ){
634 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
635 }
636 }
637 break;
638 }
639 case FN_Length: {
640 sqliteExprCode(pParse, pList->a[0].pExpr);
641 sqliteVdbeAddOp(v, OP_Strlen, 0, 0, 0, 0);
642 break;
643 }
644 case FN_Substr: {
645 for(i=0; i<pList->nExpr; i++){
646 sqliteExprCode(pParse, pList->a[i].pExpr);
647 }
648 sqliteVdbeAddOp(v, OP_Substr, 0, 0, 0, 0);
649 break;
650 }
651 default: {
652 /* Can't happen! */
653 break;
drhcce7d172000-05-31 15:34:51 +0000654 }
655 }
656 break;
657 }
drh19a775c2000-06-05 18:54:46 +0000658 case TK_SELECT: {
drh967e8b72000-06-21 13:59:10 +0000659 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000660 break;
661 }
drhfef52082000-06-06 01:50:43 +0000662 case TK_IN: {
663 int addr;
drh4794b982000-06-06 13:54:14 +0000664 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000665 sqliteExprCode(pParse, pExpr->pLeft);
666 addr = sqliteVdbeCurrentAddr(v);
667 if( pExpr->pSelect ){
668 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
669 }else{
670 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
671 }
drh4794b982000-06-06 13:54:14 +0000672 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000673 break;
674 }
675 case TK_BETWEEN: {
676 int lbl = sqliteVdbeMakeLabel(v);
677 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
678 sqliteExprIfFalse(pParse, pExpr, lbl);
679 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
680 sqliteVdbeResolveLabel(v, lbl);
681 break;
682 }
drhcce7d172000-05-31 15:34:51 +0000683 }
684 return;
685}
686
687/*
688** Generate code for a boolean expression such that a jump is made
689** to the label "dest" if the expression is true but execution
690** continues straight thru if the expression is false.
691*/
692void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
693 Vdbe *v = pParse->pVdbe;
694 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000695 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000696 switch( pExpr->op ){
697 case TK_LT: op = OP_Lt; break;
698 case TK_LE: op = OP_Le; break;
699 case TK_GT: op = OP_Gt; break;
700 case TK_GE: op = OP_Ge; break;
701 case TK_NE: op = OP_Ne; break;
702 case TK_EQ: op = OP_Eq; break;
703 case TK_LIKE: op = OP_Like; break;
704 case TK_GLOB: op = OP_Glob; break;
705 case TK_ISNULL: op = OP_IsNull; break;
706 case TK_NOTNULL: op = OP_NotNull; break;
707 default: break;
708 }
709 switch( pExpr->op ){
710 case TK_AND: {
711 int d2 = sqliteVdbeMakeLabel(v);
712 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
713 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
714 sqliteVdbeResolveLabel(v, d2);
715 break;
716 }
717 case TK_OR: {
718 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
719 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
720 break;
721 }
722 case TK_NOT: {
723 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
724 break;
725 }
726 case TK_LT:
727 case TK_LE:
728 case TK_GT:
729 case TK_GE:
730 case TK_NE:
731 case TK_EQ:
732 case TK_LIKE:
733 case TK_GLOB: {
734 sqliteExprCode(pParse, pExpr->pLeft);
735 sqliteExprCode(pParse, pExpr->pRight);
736 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
737 break;
738 }
739 case TK_ISNULL:
740 case TK_NOTNULL: {
741 sqliteExprCode(pParse, pExpr->pLeft);
742 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
743 break;
744 }
drhfef52082000-06-06 01:50:43 +0000745 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000746 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000747 if( pExpr->pSelect ){
748 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
749 }else{
750 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
751 }
752 break;
753 }
754 case TK_BETWEEN: {
755 int lbl = sqliteVdbeMakeLabel(v);
756 sqliteExprCode(pParse, pExpr->pLeft);
757 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
758 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
759 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
760 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
761 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
762 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
763 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
764 break;
765 }
drhcce7d172000-05-31 15:34:51 +0000766 default: {
767 sqliteExprCode(pParse, pExpr);
768 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
769 break;
770 }
771 }
772}
773
774/*
drh66b89c82000-11-28 20:47:17 +0000775** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000776** to the label "dest" if the expression is false but execution
777** continues straight thru if the expression is true.
778*/
779void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
780 Vdbe *v = pParse->pVdbe;
781 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000782 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000783 switch( pExpr->op ){
784 case TK_LT: op = OP_Ge; break;
785 case TK_LE: op = OP_Gt; break;
786 case TK_GT: op = OP_Le; break;
787 case TK_GE: op = OP_Lt; break;
788 case TK_NE: op = OP_Eq; break;
789 case TK_EQ: op = OP_Ne; break;
790 case TK_LIKE: op = OP_Like; break;
791 case TK_GLOB: op = OP_Glob; break;
792 case TK_ISNULL: op = OP_NotNull; break;
793 case TK_NOTNULL: op = OP_IsNull; break;
794 default: break;
795 }
796 switch( pExpr->op ){
797 case TK_AND: {
798 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
799 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
800 break;
801 }
802 case TK_OR: {
803 int d2 = sqliteVdbeMakeLabel(v);
804 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
805 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
806 sqliteVdbeResolveLabel(v, d2);
807 break;
808 }
809 case TK_NOT: {
810 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
811 break;
812 }
813 case TK_LT:
814 case TK_LE:
815 case TK_GT:
816 case TK_GE:
817 case TK_NE:
818 case TK_EQ: {
819 sqliteExprCode(pParse, pExpr->pLeft);
820 sqliteExprCode(pParse, pExpr->pRight);
821 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
822 break;
823 }
824 case TK_LIKE:
825 case TK_GLOB: {
826 sqliteExprCode(pParse, pExpr->pLeft);
827 sqliteExprCode(pParse, pExpr->pRight);
828 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
829 break;
830 }
831 case TK_ISNULL:
832 case TK_NOTNULL: {
833 sqliteExprCode(pParse, pExpr->pLeft);
834 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
835 break;
836 }
drhfef52082000-06-06 01:50:43 +0000837 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000838 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000839 if( pExpr->pSelect ){
840 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
841 }else{
842 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
843 }
844 break;
845 }
846 case TK_BETWEEN: {
847 int addr;
848 sqliteExprCode(pParse, pExpr->pLeft);
849 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
850 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
851 addr = sqliteVdbeCurrentAddr(v);
852 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
853 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
854 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
855 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
856 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
857 break;
858 }
drhcce7d172000-05-31 15:34:51 +0000859 default: {
860 sqliteExprCode(pParse, pExpr);
861 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
862 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
863 break;
864 }
865 }
866}
drh22827922000-06-06 17:27:05 +0000867
868/*
869** Do a deep comparison of two expression trees. Return TRUE (non-zero)
870** if they are identical and return FALSE if they differ in any way.
871*/
drhd8bc7082000-06-07 23:51:50 +0000872int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +0000873 int i;
874 if( pA==0 ){
875 return pB==0;
876 }else if( pB==0 ){
877 return 0;
878 }
879 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +0000880 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
881 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +0000882 if( pA->pList ){
883 if( pB->pList==0 ) return 0;
884 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
885 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000886 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +0000887 return 0;
888 }
889 }
890 }else if( pB->pList ){
891 return 0;
892 }
893 if( pA->pSelect || pB->pSelect ) return 0;
894 if( pA->token.z ){
895 if( pB->token.z==0 ) return 0;
896 if( pB->token.n!=pA->token.n ) return 0;
897 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
898 }
899 return 1;
900}
901
902/*
903** Add a new element to the pParse->aAgg[] array and return its index.
904*/
905static int appendAggInfo(Parse *pParse){
906 if( (pParse->nAgg & 0x7)==0 ){
907 int amt = pParse->nAgg + 8;
908 pParse->aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
909 if( pParse->aAgg==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000910 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000911 return -1;
912 }
913 }
914 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
915 return pParse->nAgg++;
916}
917
918/*
919** Analyze the given expression looking for aggregate functions and
920** for variables that need to be added to the pParse->aAgg[] array.
921** Make additional entries to the pParse->aAgg[] array as necessary.
922**
923** This routine should only be called after the expression has been
924** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
925**
926** If errors are seen, leave an error message in zErrMsg and return
927** the number of errors.
928*/
929int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
930 int i;
931 AggExpr *aAgg;
932 int nErr = 0;
933
934 if( pExpr==0 ) return 0;
935 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000936 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000937 aAgg = pParse->aAgg;
938 for(i=0; i<pParse->nAgg; i++){
939 if( aAgg[i].isAgg ) continue;
940 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +0000941 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +0000942 break;
943 }
944 }
945 if( i>=pParse->nAgg ){
946 i = appendAggInfo(pParse);
947 if( i<0 ) return 1;
948 pParse->aAgg[i].isAgg = 0;
949 pParse->aAgg[i].pExpr = pExpr;
950 }
drhaaf88722000-06-08 11:25:00 +0000951 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +0000952 break;
953 }
954 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000955 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000956 if( pParse->iAggCount>=0 ){
957 i = pParse->iAggCount;
958 }else{
959 i = appendAggInfo(pParse);
960 if( i<0 ) return 1;
961 pParse->aAgg[i].isAgg = 1;
962 pParse->aAgg[i].pExpr = 0;
963 pParse->iAggCount = i;
964 }
drh967e8b72000-06-21 13:59:10 +0000965 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +0000966 pExpr->iAgg = i;
967 break;
968 }
969 }
970 aAgg = pParse->aAgg;
971 for(i=0; i<pParse->nAgg; i++){
972 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +0000973 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +0000974 break;
975 }
976 }
977 if( i>=pParse->nAgg ){
978 i = appendAggInfo(pParse);
979 if( i<0 ) return 1;
980 pParse->aAgg[i].isAgg = 1;
981 pParse->aAgg[i].pExpr = pExpr;
982 }
983 pExpr->iAgg = i;
984 break;
985 }
986 default: {
987 if( pExpr->pLeft ){
988 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
989 }
990 if( nErr==0 && pExpr->pRight ){
991 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
992 }
993 if( nErr==0 && pExpr->pList ){
994 int n = pExpr->pList->nExpr;
995 int i;
996 for(i=0; nErr==0 && i<n; i++){
997 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
998 }
999 }
1000 break;
1001 }
1002 }
1003 return nErr;
1004}