blob: f55fa1893c6b9b627d516575072386ad4021d40f [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**
drhc4a3c772001-04-04 11:48:57 +000027** $Id: expr.c,v 1.22 2001/04/04 11:48:58 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 */
132 int isRowid = 0; /* True if this is the ROWID column */
drh6e142f52000-06-08 13:36:40 +0000133 char *z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
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);
drhc4a3c772001-04-04 11:48:57 +0000181 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000182 for(i=0; i<pTabList->nId; i++){
183 int j;
184 char *zTab;
185 Table *pTab = pTabList->a[i].pTab;
186 if( pTab==0 ) continue;
187 if( pTabList->a[i].zAlias ){
188 zTab = pTabList->a[i].zAlias;
189 }else{
190 zTab = pTab->zName;
191 }
192 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhc4a3c772001-04-04 11:48:57 +0000193 if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000194 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000195 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000196 cnt++;
drh19a775c2000-06-05 18:54:46 +0000197 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000198 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000199 }
200 }
201 }
drhc4a3c772001-04-04 11:48:57 +0000202 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
203 cnt = 1;
204 pExpr->iColumn = -1;
205 }
drhcce7d172000-05-31 15:34:51 +0000206 sqliteFree(zLeft);
207 sqliteFree(zRight);
208 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000209 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000210 pLeft->token.z, pLeft->token.n, ".", 1,
211 pRight->token.z, pRight->token.n, 0);
212 pParse->nErr++;
213 return 1;
214 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000215 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000216 pLeft->token.z, pLeft->token.n, ".", 1,
217 pRight->token.z, pRight->token.n, 0);
218 pParse->nErr++;
219 return 1;
220 }
221 sqliteExprDelete(pLeft);
222 pExpr->pLeft = 0;
223 sqliteExprDelete(pRight);
224 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000225 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000226 break;
227 }
228
drhfef52082000-06-06 01:50:43 +0000229 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000230 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000231 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000232 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
233 return 1;
234 }
drhfef52082000-06-06 01:50:43 +0000235 if( pExpr->pSelect ){
236 /* Case 1: expr IN (SELECT ...)
237 **
238 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000239 ** table. The cursor number of the temporary table has already
240 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000241 */
drh345fda32001-01-15 22:51:08 +0000242 sqliteVdbeAddOp(v, OP_OpenIdx, pExpr->iTable, 1, 0, 0);
drhfef52082000-06-06 01:50:43 +0000243 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
244 }else if( pExpr->pList ){
245 /* Case 2: expr IN (exprlist)
246 **
247 ** Create a set to put the exprlist values in. The Set id is stored
248 ** in iTable.
249 */
250 int i, iSet;
251 for(i=0; i<pExpr->pList->nExpr; i++){
252 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000253 if( !isConstant(pE2) ){
254 sqliteSetString(&pParse->zErrMsg,
255 "right-hand side of IN operator must be constant", 0);
256 pParse->nErr++;
257 return 1;
258 }
drh4794b982000-06-06 13:54:14 +0000259 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
260 return 1;
261 }
drhfef52082000-06-06 01:50:43 +0000262 }
263 iSet = pExpr->iTable = pParse->nSet++;
264 for(i=0; i<pExpr->pList->nExpr; i++){
265 Expr *pE2 = pExpr->pList->a[i].pExpr;
266 switch( pE2->op ){
267 case TK_FLOAT:
268 case TK_INTEGER:
269 case TK_STRING: {
270 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
271 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
272 sqliteVdbeDequoteP3(v, addr);
273 break;
274 }
275 default: {
276 sqliteExprCode(pParse, pE2);
277 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
278 break;
279 }
280 }
281 }
282 }
drhcfab11b2000-06-06 03:31:22 +0000283 break;
drhfef52082000-06-06 01:50:43 +0000284 }
285
drh19a775c2000-06-05 18:54:46 +0000286 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000287 /* This has to be a scalar SELECT. Generate code to put the
288 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000289 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000290 */
drh967e8b72000-06-21 13:59:10 +0000291 pExpr->iColumn = pParse->nMem++;
292 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000293 return 1;
294 }
295 break;
296 }
297
drhcce7d172000-05-31 15:34:51 +0000298 /* For all else, just recursively walk the tree */
299 default: {
drh4794b982000-06-06 13:54:14 +0000300 if( pExpr->pLeft
301 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000302 return 1;
303 }
304 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000305 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000306 return 1;
307 }
308 if( pExpr->pList ){
309 int i;
310 ExprList *pList = pExpr->pList;
311 for(i=0; i<pList->nExpr; i++){
312 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
313 return 1;
314 }
315 }
316 }
317 }
318 }
319 return 0;
320}
321
322#if 0 /* NOT USED */
323/*
324** Compare a token against a string. Return TRUE if they match.
325*/
326static int sqliteTokenCmp(Token *pToken, const char *zStr){
327 int n = strlen(zStr);
328 if( n!=pToken->n ) return 0;
329 return sqliteStrNICmp(pToken->z, zStr, n)==0;
330}
331#endif
332
333/*
334** Convert a function name into its integer identifier. Return the
335** identifier. Return FN_Unknown if the function name is unknown.
336*/
337int sqliteFuncId(Token *pToken){
338 static const struct {
339 char *zName;
340 int len;
341 int id;
342 } aFunc[] = {
343 { "count", 5, FN_Count },
344 { "min", 3, FN_Min },
345 { "max", 3, FN_Max },
346 { "sum", 3, FN_Sum },
drh22827922000-06-06 17:27:05 +0000347 { "avg", 3, FN_Avg },
drh0bdaf622000-06-11 23:50:13 +0000348 { "fcnt", 4, FN_Fcnt }, /* Used for testing only */
drh6ec27332000-08-28 15:51:43 +0000349 { "length", 6, FN_Length},
350 { "substr", 6, FN_Substr},
drhcce7d172000-05-31 15:34:51 +0000351 };
352 int i;
353 for(i=0; i<ArraySize(aFunc); i++){
354 if( aFunc[i].len==pToken->n
355 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
356 return aFunc[i].id;
357 }
358 }
359 return FN_Unknown;
360}
361
362/*
363** Error check the functions in an expression. Make sure all
364** function names are recognized and all functions have the correct
365** number of arguments. Leave an error message in pParse->zErrMsg
366** if anything is amiss. Return the number of errors.
367**
368** if pIsAgg is not null and this expression is an aggregate function
369** (like count(*) or max(value)) then write a 1 into *pIsAgg.
370*/
371int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
372 int nErr = 0;
373 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000374 switch( pExpr->op ){
375 case TK_FUNCTION: {
376 int id = sqliteFuncId(&pExpr->token);
377 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
378 int no_such_func = 0;
379 int too_many_args = 0;
380 int too_few_args = 0;
381 int is_agg = 0;
382 int i;
drh967e8b72000-06-21 13:59:10 +0000383 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000384 switch( id ){
385 case FN_Unknown: {
386 no_such_func = 1;
387 break;
388 }
389 case FN_Count: {
390 no_such_func = !allowAgg;
391 too_many_args = n>1;
392 is_agg = 1;
393 break;
394 }
395 case FN_Max:
396 case FN_Min: {
397 too_few_args = allowAgg ? n<1 : n<2;
398 is_agg = n==1;
399 break;
400 }
drh22827922000-06-06 17:27:05 +0000401 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000402 case FN_Sum: {
403 no_such_func = !allowAgg;
404 too_many_args = n>1;
405 too_few_args = n<1;
406 is_agg = 1;
407 break;
408 }
drh6ec27332000-08-28 15:51:43 +0000409 case FN_Length: {
410 too_few_args = n<1;
411 too_many_args = n>1;
412 break;
413 }
414 case FN_Substr: {
415 too_few_args = n<3;
416 too_many_args = n>3;
417 break;
418 }
drh0bdaf622000-06-11 23:50:13 +0000419 /* The "fcnt(*)" function always returns the number of fetch
420 ** operations that have occurred so far while processing the
421 ** SQL statement. This information can be used by test procedures
422 ** to verify that indices are being used properly to minimize
423 ** searching. All arguments to fcnt() are ignored. fcnt() has
424 ** no use (other than testing) that we are aware of.
425 */
426 case FN_Fcnt: {
427 n = 0;
428 break;
429 }
drh6ec27332000-08-28 15:51:43 +0000430
drhcce7d172000-05-31 15:34:51 +0000431 default: break;
432 }
433 if( no_such_func ){
434 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
435 pExpr->token.z, pExpr->token.n, 0);
436 pParse->nErr++;
437 nErr++;
438 }else if( too_many_args ){
439 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
440 pExpr->token.z, pExpr->token.n, "()", 2, 0);
441 pParse->nErr++;
442 nErr++;
443 }else if( too_few_args ){
444 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
445 pExpr->token.z, pExpr->token.n, "()", 2, 0);
446 pParse->nErr++;
447 nErr++;
448 }
drh22827922000-06-06 17:27:05 +0000449 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000450 if( is_agg && pIsAgg ) *pIsAgg = 1;
451 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000452 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
453 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000454 }
455 }
456 default: {
457 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000458 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000459 }
460 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000461 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000462 }
drhfef52082000-06-06 01:50:43 +0000463 if( nErr==0 && pExpr->pList ){
464 int n = pExpr->pList->nExpr;
465 int i;
466 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000467 Expr *pE2 = pExpr->pList->a[i].pExpr;
468 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000469 }
470 }
drhcce7d172000-05-31 15:34:51 +0000471 break;
472 }
473 }
474 return nErr;
475}
476
477/*
478** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000479** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000480*/
481void sqliteExprCode(Parse *pParse, Expr *pExpr){
482 Vdbe *v = pParse->pVdbe;
483 int op;
484 switch( pExpr->op ){
485 case TK_PLUS: op = OP_Add; break;
486 case TK_MINUS: op = OP_Subtract; break;
487 case TK_STAR: op = OP_Multiply; break;
488 case TK_SLASH: op = OP_Divide; break;
489 case TK_AND: op = OP_And; break;
490 case TK_OR: op = OP_Or; break;
491 case TK_LT: op = OP_Lt; break;
492 case TK_LE: op = OP_Le; break;
493 case TK_GT: op = OP_Gt; break;
494 case TK_GE: op = OP_Ge; break;
495 case TK_NE: op = OP_Ne; break;
496 case TK_EQ: op = OP_Eq; break;
497 case TK_LIKE: op = OP_Like; break;
498 case TK_GLOB: op = OP_Glob; break;
499 case TK_ISNULL: op = OP_IsNull; break;
500 case TK_NOTNULL: op = OP_NotNull; break;
501 case TK_NOT: op = OP_Not; break;
502 case TK_UMINUS: op = OP_Negative; break;
503 default: break;
504 }
505 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000506 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000507 if( pParse->useAgg ){
508 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000509 }else if( pExpr->iColumn>=0 ){
drh967e8b72000-06-21 13:59:10 +0000510 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iColumn, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000511 }else{
512 sqliteVdbeAddOp(v, OP_Key, pExpr->iTable, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000513 }
drhcce7d172000-05-31 15:34:51 +0000514 break;
515 }
516 case TK_INTEGER: {
517 int i = atoi(pExpr->token.z);
518 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
519 break;
520 }
521 case TK_FLOAT: {
522 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
523 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
524 break;
525 }
526 case TK_STRING: {
527 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
528 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
529 sqliteVdbeDequoteP3(v, addr);
530 break;
531 }
532 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000533 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000534 break;
535 }
536 case TK_AND:
537 case TK_OR:
538 case TK_PLUS:
539 case TK_STAR:
540 case TK_MINUS:
541 case TK_SLASH: {
542 sqliteExprCode(pParse, pExpr->pLeft);
543 sqliteExprCode(pParse, pExpr->pRight);
544 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
545 break;
546 }
drh00400772000-06-16 20:51:26 +0000547 case TK_CONCAT: {
548 sqliteExprCode(pParse, pExpr->pLeft);
549 sqliteExprCode(pParse, pExpr->pRight);
550 sqliteVdbeAddOp(v, OP_Concat, 2, 0, 0, 0);
551 break;
552 }
drhcce7d172000-05-31 15:34:51 +0000553 case TK_LT:
554 case TK_LE:
555 case TK_GT:
556 case TK_GE:
557 case TK_NE:
558 case TK_EQ:
559 case TK_LIKE:
560 case TK_GLOB: {
561 int dest;
562 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
563 sqliteExprCode(pParse, pExpr->pLeft);
564 sqliteExprCode(pParse, pExpr->pRight);
565 dest = sqliteVdbeCurrentAddr(v) + 2;
566 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
567 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
568 break;
569 }
drhcce7d172000-05-31 15:34:51 +0000570 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000571 assert( pExpr->pLeft );
572 if( pExpr->pLeft->op==TK_INTEGER ){
573 int i = atoi(pExpr->pLeft->token.z);
574 sqliteVdbeAddOp(v, OP_Integer, -i, 0, 0, 0);
575 break;
576 }else if( pExpr->pLeft->op==TK_FLOAT ){
577 Token *p = &pExpr->pLeft->token;
578 char *z = sqliteMalloc( p->n + 2 );
579 sprintf(z, "-%.*s", p->n, p->z);
580 sqliteVdbeAddOp(v, OP_String, 0, 0, z, 0);
581 sqliteFree(z);
582 break;
583 }
drh1ccde152000-06-17 13:12:39 +0000584 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000585 }
586 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000587 sqliteExprCode(pParse, pExpr->pLeft);
588 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
589 break;
590 }
591 case TK_ISNULL:
592 case TK_NOTNULL: {
593 int dest;
drh8be51132000-06-03 19:19:41 +0000594 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000595 sqliteExprCode(pParse, pExpr->pLeft);
596 dest = sqliteVdbeCurrentAddr(v) + 2;
597 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000598 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000599 break;
600 }
drh22827922000-06-06 17:27:05 +0000601 case TK_AGG_FUNCTION: {
602 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000603 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000604 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
605 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount, 0, 0);
606 sqliteVdbeAddOp(v, OP_Divide, 0, 0, 0, 0);
607 }
608 break;
609 }
drhcce7d172000-05-31 15:34:51 +0000610 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000611 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000612 int op;
613 int i;
614 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000615 switch( id ){
616 case FN_Fcnt: {
617 sqliteVdbeAddOp(v, OP_Fcnt, 0, 0, 0, 0);
618 break;
619 }
620 case FN_Min:
621 case FN_Max: {
622 op = id==FN_Min ? OP_Min : OP_Max;
623 for(i=0; i<pList->nExpr; i++){
624 sqliteExprCode(pParse, pList->a[i].pExpr);
625 if( i>0 ){
626 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
627 }
628 }
629 break;
630 }
631 case FN_Length: {
632 sqliteExprCode(pParse, pList->a[0].pExpr);
633 sqliteVdbeAddOp(v, OP_Strlen, 0, 0, 0, 0);
634 break;
635 }
636 case FN_Substr: {
637 for(i=0; i<pList->nExpr; i++){
638 sqliteExprCode(pParse, pList->a[i].pExpr);
639 }
640 sqliteVdbeAddOp(v, OP_Substr, 0, 0, 0, 0);
641 break;
642 }
643 default: {
644 /* Can't happen! */
645 break;
drhcce7d172000-05-31 15:34:51 +0000646 }
647 }
648 break;
649 }
drh19a775c2000-06-05 18:54:46 +0000650 case TK_SELECT: {
drh967e8b72000-06-21 13:59:10 +0000651 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000652 break;
653 }
drhfef52082000-06-06 01:50:43 +0000654 case TK_IN: {
655 int addr;
drh4794b982000-06-06 13:54:14 +0000656 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000657 sqliteExprCode(pParse, pExpr->pLeft);
658 addr = sqliteVdbeCurrentAddr(v);
659 if( pExpr->pSelect ){
660 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
661 }else{
662 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
663 }
drh4794b982000-06-06 13:54:14 +0000664 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000665 break;
666 }
667 case TK_BETWEEN: {
668 int lbl = sqliteVdbeMakeLabel(v);
669 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
670 sqliteExprIfFalse(pParse, pExpr, lbl);
671 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
672 sqliteVdbeResolveLabel(v, lbl);
673 break;
674 }
drhcce7d172000-05-31 15:34:51 +0000675 }
676 return;
677}
678
679/*
680** Generate code for a boolean expression such that a jump is made
681** to the label "dest" if the expression is true but execution
682** continues straight thru if the expression is false.
683*/
684void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
685 Vdbe *v = pParse->pVdbe;
686 int op = 0;
687 switch( pExpr->op ){
688 case TK_LT: op = OP_Lt; break;
689 case TK_LE: op = OP_Le; break;
690 case TK_GT: op = OP_Gt; break;
691 case TK_GE: op = OP_Ge; break;
692 case TK_NE: op = OP_Ne; break;
693 case TK_EQ: op = OP_Eq; break;
694 case TK_LIKE: op = OP_Like; break;
695 case TK_GLOB: op = OP_Glob; break;
696 case TK_ISNULL: op = OP_IsNull; break;
697 case TK_NOTNULL: op = OP_NotNull; break;
698 default: break;
699 }
700 switch( pExpr->op ){
701 case TK_AND: {
702 int d2 = sqliteVdbeMakeLabel(v);
703 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
704 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
705 sqliteVdbeResolveLabel(v, d2);
706 break;
707 }
708 case TK_OR: {
709 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
710 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
711 break;
712 }
713 case TK_NOT: {
714 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
715 break;
716 }
717 case TK_LT:
718 case TK_LE:
719 case TK_GT:
720 case TK_GE:
721 case TK_NE:
722 case TK_EQ:
723 case TK_LIKE:
724 case TK_GLOB: {
725 sqliteExprCode(pParse, pExpr->pLeft);
726 sqliteExprCode(pParse, pExpr->pRight);
727 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
728 break;
729 }
730 case TK_ISNULL:
731 case TK_NOTNULL: {
732 sqliteExprCode(pParse, pExpr->pLeft);
733 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
734 break;
735 }
drhfef52082000-06-06 01:50:43 +0000736 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000737 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000738 if( pExpr->pSelect ){
739 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
740 }else{
741 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
742 }
743 break;
744 }
745 case TK_BETWEEN: {
746 int lbl = sqliteVdbeMakeLabel(v);
747 sqliteExprCode(pParse, pExpr->pLeft);
748 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
749 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
750 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
751 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
752 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
753 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
754 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
755 break;
756 }
drhcce7d172000-05-31 15:34:51 +0000757 default: {
758 sqliteExprCode(pParse, pExpr);
759 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
760 break;
761 }
762 }
763}
764
765/*
drh66b89c82000-11-28 20:47:17 +0000766** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000767** to the label "dest" if the expression is false but execution
768** continues straight thru if the expression is true.
769*/
770void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
771 Vdbe *v = pParse->pVdbe;
772 int op = 0;
773 switch( pExpr->op ){
774 case TK_LT: op = OP_Ge; break;
775 case TK_LE: op = OP_Gt; break;
776 case TK_GT: op = OP_Le; break;
777 case TK_GE: op = OP_Lt; break;
778 case TK_NE: op = OP_Eq; break;
779 case TK_EQ: op = OP_Ne; break;
780 case TK_LIKE: op = OP_Like; break;
781 case TK_GLOB: op = OP_Glob; break;
782 case TK_ISNULL: op = OP_NotNull; break;
783 case TK_NOTNULL: op = OP_IsNull; break;
784 default: break;
785 }
786 switch( pExpr->op ){
787 case TK_AND: {
788 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
789 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
790 break;
791 }
792 case TK_OR: {
793 int d2 = sqliteVdbeMakeLabel(v);
794 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
795 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
796 sqliteVdbeResolveLabel(v, d2);
797 break;
798 }
799 case TK_NOT: {
800 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
801 break;
802 }
803 case TK_LT:
804 case TK_LE:
805 case TK_GT:
806 case TK_GE:
807 case TK_NE:
808 case TK_EQ: {
809 sqliteExprCode(pParse, pExpr->pLeft);
810 sqliteExprCode(pParse, pExpr->pRight);
811 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
812 break;
813 }
814 case TK_LIKE:
815 case TK_GLOB: {
816 sqliteExprCode(pParse, pExpr->pLeft);
817 sqliteExprCode(pParse, pExpr->pRight);
818 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
819 break;
820 }
821 case TK_ISNULL:
822 case TK_NOTNULL: {
823 sqliteExprCode(pParse, pExpr->pLeft);
824 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
825 break;
826 }
drhfef52082000-06-06 01:50:43 +0000827 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000828 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000829 if( pExpr->pSelect ){
830 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
831 }else{
832 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
833 }
834 break;
835 }
836 case TK_BETWEEN: {
837 int addr;
838 sqliteExprCode(pParse, pExpr->pLeft);
839 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
840 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
841 addr = sqliteVdbeCurrentAddr(v);
842 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
843 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
844 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
845 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
846 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
847 break;
848 }
drhcce7d172000-05-31 15:34:51 +0000849 default: {
850 sqliteExprCode(pParse, pExpr);
851 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
852 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
853 break;
854 }
855 }
856}
drh22827922000-06-06 17:27:05 +0000857
858/*
859** Do a deep comparison of two expression trees. Return TRUE (non-zero)
860** if they are identical and return FALSE if they differ in any way.
861*/
drhd8bc7082000-06-07 23:51:50 +0000862int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +0000863 int i;
864 if( pA==0 ){
865 return pB==0;
866 }else if( pB==0 ){
867 return 0;
868 }
869 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +0000870 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
871 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +0000872 if( pA->pList ){
873 if( pB->pList==0 ) return 0;
874 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
875 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000876 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +0000877 return 0;
878 }
879 }
880 }else if( pB->pList ){
881 return 0;
882 }
883 if( pA->pSelect || pB->pSelect ) return 0;
884 if( pA->token.z ){
885 if( pB->token.z==0 ) return 0;
886 if( pB->token.n!=pA->token.n ) return 0;
887 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
888 }
889 return 1;
890}
891
892/*
893** Add a new element to the pParse->aAgg[] array and return its index.
894*/
895static int appendAggInfo(Parse *pParse){
896 if( (pParse->nAgg & 0x7)==0 ){
897 int amt = pParse->nAgg + 8;
898 pParse->aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
899 if( pParse->aAgg==0 ){
900 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
901 pParse->nErr++;
902 return -1;
903 }
904 }
905 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
906 return pParse->nAgg++;
907}
908
909/*
910** Analyze the given expression looking for aggregate functions and
911** for variables that need to be added to the pParse->aAgg[] array.
912** Make additional entries to the pParse->aAgg[] array as necessary.
913**
914** This routine should only be called after the expression has been
915** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
916**
917** If errors are seen, leave an error message in zErrMsg and return
918** the number of errors.
919*/
920int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
921 int i;
922 AggExpr *aAgg;
923 int nErr = 0;
924
925 if( pExpr==0 ) return 0;
926 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000927 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000928 aAgg = pParse->aAgg;
929 for(i=0; i<pParse->nAgg; i++){
930 if( aAgg[i].isAgg ) continue;
931 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +0000932 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +0000933 break;
934 }
935 }
936 if( i>=pParse->nAgg ){
937 i = appendAggInfo(pParse);
938 if( i<0 ) return 1;
939 pParse->aAgg[i].isAgg = 0;
940 pParse->aAgg[i].pExpr = pExpr;
941 }
drhaaf88722000-06-08 11:25:00 +0000942 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +0000943 break;
944 }
945 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000946 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000947 if( pParse->iAggCount>=0 ){
948 i = pParse->iAggCount;
949 }else{
950 i = appendAggInfo(pParse);
951 if( i<0 ) return 1;
952 pParse->aAgg[i].isAgg = 1;
953 pParse->aAgg[i].pExpr = 0;
954 pParse->iAggCount = i;
955 }
drh967e8b72000-06-21 13:59:10 +0000956 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +0000957 pExpr->iAgg = i;
958 break;
959 }
960 }
961 aAgg = pParse->aAgg;
962 for(i=0; i<pParse->nAgg; i++){
963 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +0000964 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +0000965 break;
966 }
967 }
968 if( i>=pParse->nAgg ){
969 i = appendAggInfo(pParse);
970 if( i<0 ) return 1;
971 pParse->aAgg[i].isAgg = 1;
972 pParse->aAgg[i].pExpr = pExpr;
973 }
974 pExpr->iAgg = i;
975 break;
976 }
977 default: {
978 if( pExpr->pLeft ){
979 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
980 }
981 if( nErr==0 && pExpr->pRight ){
982 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
983 }
984 if( nErr==0 && pExpr->pList ){
985 int n = pExpr->pList->nExpr;
986 int i;
987 for(i=0; nErr==0 && i<n; i++){
988 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
989 }
990 }
991 break;
992 }
993 }
994 return nErr;
995}