blob: 8e2045bf87c161714c67466d22b748813f105bce [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
drh1ccde152000-06-17 13:12:39 +000012** This file contains routines used for analyzing expressions and
drhb19a2bc2001-09-16 00:13:26 +000013** for generating VDBE code that evaluates expressions in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
drh487ab3c2001-11-08 00:45:21 +000015** $Id: expr.c,v 1.33 2001/11/08 00:45:21 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
drhfef52082000-06-06 01:50:43 +000020** Walk an expression tree. Return 1 if the expression is constant
21** and 0 if it involves variables.
22*/
23static int isConstant(Expr *p){
24 switch( p->op ){
25 case TK_ID:
drh967e8b72000-06-21 13:59:10 +000026 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +000027 case TK_DOT:
28 return 0;
29 default: {
30 if( p->pLeft && !isConstant(p->pLeft) ) return 0;
31 if( p->pRight && !isConstant(p->pRight) ) return 0;
32 if( p->pList ){
33 int i;
34 for(i=0; i<p->pList->nExpr; i++){
35 if( !isConstant(p->pList->a[i].pExpr) ) return 0;
36 }
37 }
38 break;
39 }
40 }
41 return 1;
42}
43
44/*
drh4794b982000-06-06 13:54:14 +000045** Walk the expression tree and process operators of the form:
46**
47** expr IN (SELECT ...)
48**
drh967e8b72000-06-21 13:59:10 +000049** These operators have to be processed before column names are
drh4794b982000-06-06 13:54:14 +000050** resolved because each such operator increments pParse->nTab
drh1ccde152000-06-17 13:12:39 +000051** to reserve cursor numbers for its own use. But pParse->nTab
drh967e8b72000-06-21 13:59:10 +000052** needs to be constant once we begin resolving column names.
drh4794b982000-06-06 13:54:14 +000053**
54** Actually, the processing of IN-SELECT is only started by this
55** routine. This routine allocates a cursor number to the IN-SELECT
56** and then moves on. The code generation is done by
57** sqliteExprResolveIds() which must be called afterwards.
58*/
59void sqliteExprResolveInSelect(Parse *pParse, Expr *pExpr){
60 if( pExpr==0 ) return;
61 if( pExpr->op==TK_IN && pExpr->pSelect!=0 ){
62 pExpr->iTable = pParse->nTab++;
63 }else{
64 if( pExpr->pLeft ) sqliteExprResolveInSelect(pParse, pExpr->pLeft);
65 if( pExpr->pRight ) sqliteExprResolveInSelect(pParse, pExpr->pRight);
66 if( pExpr->pList ){
67 int i;
68 ExprList *pList = pExpr->pList;
69 for(i=0; i<pList->nExpr; i++){
70 sqliteExprResolveInSelect(pParse, pList->a[i].pExpr);
71 }
72 }
73 }
74}
75
76/*
drhc4a3c772001-04-04 11:48:57 +000077** Return TRUE if the given string is a row-id column name.
78*/
79static int sqliteIsRowid(const char *z){
80 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
81 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
82 if( sqliteStrICmp(z, "OID")==0 ) return 1;
83 return 0;
84}
85
86/*
drhcce7d172000-05-31 15:34:51 +000087** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +000088** table columns. Nodes of the form ID.ID or ID resolve into an
89** index to the table in the table list and a column offset. The opcode
90** for such nodes is changed to TK_COLUMN. The iTable value is changed
drh19a775c2000-06-05 18:54:46 +000091** to the index of the referenced table in pTabList plus the pParse->nTab
drh967e8b72000-06-21 13:59:10 +000092** value. The iColumn value is changed to the index of the column of the
drh19a775c2000-06-05 18:54:46 +000093** referenced table.
94**
drhfef52082000-06-06 01:50:43 +000095** We also check for instances of the IN operator. IN comes in two
96** forms:
97**
98** expr IN (exprlist)
99** and
100** expr IN (SELECT ...)
101**
102** The first form is handled by creating a set holding the list
103** of allowed values. The second form causes the SELECT to generate
104** a temporary table.
105**
106** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000107** If it finds any, it generates code to write the value of that select
108** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000109**
drh967e8b72000-06-21 13:59:10 +0000110** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000111** the number of errors seen and leaves an error message on pParse->zErrMsg.
112*/
113int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){
drhdaffd0e2001-04-11 14:28:42 +0000114 if( pExpr==0 || pTabList==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000115 switch( pExpr->op ){
116 /* A lone identifier */
117 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000118 int cnt = 0; /* Number of matches */
119 int i; /* Loop counter */
drh6e142f52000-06-08 13:36:40 +0000120 char *z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000121 if( z==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000122 for(i=0; i<pTabList->nId; i++){
123 int j;
124 Table *pTab = pTabList->a[i].pTab;
125 if( pTab==0 ) continue;
126 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000127 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000128 cnt++;
drh19a775c2000-06-05 18:54:46 +0000129 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000130 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000131 }
132 }
133 }
drhc4a3c772001-04-04 11:48:57 +0000134 if( cnt==0 && sqliteIsRowid(z) ){
135 pExpr->iColumn = -1;
136 pExpr->iTable = pParse->nTab;
137 cnt = 1 + (pTabList->nId>1);
138 }
drhcce7d172000-05-31 15:34:51 +0000139 sqliteFree(z);
140 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000141 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000142 pExpr->token.z, pExpr->token.n, 0);
143 pParse->nErr++;
144 return 1;
145 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000146 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000147 pExpr->token.z, pExpr->token.n, 0);
148 pParse->nErr++;
149 return 1;
150 }
drh967e8b72000-06-21 13:59:10 +0000151 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000152 break;
153 }
154
drh967e8b72000-06-21 13:59:10 +0000155 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000156 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000157 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000158 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000159 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000160 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000161 char *zLeft, *zRight; /* Text of an identifier */
162
163 pLeft = pExpr->pLeft;
164 pRight = pExpr->pRight;
165 assert( pLeft && pLeft->op==TK_ID );
166 assert( pRight && pRight->op==TK_ID );
drh6e142f52000-06-08 13:36:40 +0000167 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
168 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000169 if( zLeft==0 || zRight==0 ){
170 sqliteFree(zLeft);
171 sqliteFree(zRight);
172 return 1;
173 }
drh87c40e82001-07-23 14:33:02 +0000174 sqliteDequote(zLeft);
175 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000176 pExpr->iTable = -1;
drhcce7d172000-05-31 15:34:51 +0000177 for(i=0; i<pTabList->nId; i++){
178 int j;
179 char *zTab;
180 Table *pTab = pTabList->a[i].pTab;
181 if( pTab==0 ) continue;
182 if( pTabList->a[i].zAlias ){
183 zTab = pTabList->a[i].zAlias;
184 }else{
185 zTab = pTab->zName;
186 }
187 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhc4a3c772001-04-04 11:48:57 +0000188 if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000189 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000190 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000191 cnt++;
drh19a775c2000-06-05 18:54:46 +0000192 pExpr->iTable = i + pParse->nTab;
drh967e8b72000-06-21 13:59:10 +0000193 pExpr->iColumn = j;
drhcce7d172000-05-31 15:34:51 +0000194 }
195 }
196 }
drhc4a3c772001-04-04 11:48:57 +0000197 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
198 cnt = 1;
199 pExpr->iColumn = -1;
200 }
drhcce7d172000-05-31 15:34:51 +0000201 sqliteFree(zLeft);
202 sqliteFree(zRight);
203 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000204 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000205 pLeft->token.z, pLeft->token.n, ".", 1,
206 pRight->token.z, pRight->token.n, 0);
207 pParse->nErr++;
208 return 1;
209 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000210 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000211 pLeft->token.z, pLeft->token.n, ".", 1,
212 pRight->token.z, pRight->token.n, 0);
213 pParse->nErr++;
214 return 1;
215 }
216 sqliteExprDelete(pLeft);
217 pExpr->pLeft = 0;
218 sqliteExprDelete(pRight);
219 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000220 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000221 break;
222 }
223
drhfef52082000-06-06 01:50:43 +0000224 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000225 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000226 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000227 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
228 return 1;
229 }
drhfef52082000-06-06 01:50:43 +0000230 if( pExpr->pSelect ){
231 /* Case 1: expr IN (SELECT ...)
232 **
233 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000234 ** table. The cursor number of the temporary table has already
235 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000236 */
drh99fcd712001-10-13 01:06:47 +0000237 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000238 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
239 }else if( pExpr->pList ){
240 /* Case 2: expr IN (exprlist)
241 **
242 ** Create a set to put the exprlist values in. The Set id is stored
243 ** in iTable.
244 */
245 int i, iSet;
246 for(i=0; i<pExpr->pList->nExpr; i++){
247 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000248 if( !isConstant(pE2) ){
249 sqliteSetString(&pParse->zErrMsg,
250 "right-hand side of IN operator must be constant", 0);
251 pParse->nErr++;
252 return 1;
253 }
drh4794b982000-06-06 13:54:14 +0000254 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
255 return 1;
256 }
drhfef52082000-06-06 01:50:43 +0000257 }
258 iSet = pExpr->iTable = pParse->nSet++;
259 for(i=0; i<pExpr->pList->nExpr; i++){
260 Expr *pE2 = pExpr->pList->a[i].pExpr;
261 switch( pE2->op ){
262 case TK_FLOAT:
263 case TK_INTEGER:
264 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000265 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000266 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
267 sqliteVdbeDequoteP3(v, addr);
268 break;
269 }
270 default: {
271 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000272 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000273 break;
274 }
275 }
276 }
277 }
drhcfab11b2000-06-06 03:31:22 +0000278 break;
drhfef52082000-06-06 01:50:43 +0000279 }
280
drh19a775c2000-06-05 18:54:46 +0000281 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000282 /* This has to be a scalar SELECT. Generate code to put the
283 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000284 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000285 */
drh967e8b72000-06-21 13:59:10 +0000286 pExpr->iColumn = pParse->nMem++;
287 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000288 return 1;
289 }
290 break;
291 }
292
drhcce7d172000-05-31 15:34:51 +0000293 /* For all else, just recursively walk the tree */
294 default: {
drh4794b982000-06-06 13:54:14 +0000295 if( pExpr->pLeft
296 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000297 return 1;
298 }
299 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000300 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000301 return 1;
302 }
303 if( pExpr->pList ){
304 int i;
305 ExprList *pList = pExpr->pList;
306 for(i=0; i<pList->nExpr; i++){
307 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
308 return 1;
309 }
310 }
311 }
312 }
313 }
314 return 0;
315}
316
317#if 0 /* NOT USED */
318/*
319** Compare a token against a string. Return TRUE if they match.
320*/
321static int sqliteTokenCmp(Token *pToken, const char *zStr){
322 int n = strlen(zStr);
323 if( n!=pToken->n ) return 0;
324 return sqliteStrNICmp(pToken->z, zStr, n)==0;
325}
326#endif
327
328/*
329** Convert a function name into its integer identifier. Return the
330** identifier. Return FN_Unknown if the function name is unknown.
331*/
332int sqliteFuncId(Token *pToken){
333 static const struct {
334 char *zName;
335 int len;
336 int id;
337 } aFunc[] = {
drhbf4133c2001-10-13 02:59:08 +0000338 { "count", 5, FN_Count },
339 { "min", 3, FN_Min },
340 { "max", 3, FN_Max },
341 { "sum", 3, FN_Sum },
342 { "avg", 3, FN_Avg },
drhbf4133c2001-10-13 02:59:08 +0000343 { "length", 6, FN_Length },
344 { "substr", 6, FN_Substr },
345 { "abs", 3, FN_Abs },
346 { "round", 5, FN_Round },
drhcce7d172000-05-31 15:34:51 +0000347 };
348 int i;
349 for(i=0; i<ArraySize(aFunc); i++){
350 if( aFunc[i].len==pToken->n
351 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
352 return aFunc[i].id;
353 }
354 }
355 return FN_Unknown;
356}
357
358/*
359** Error check the functions in an expression. Make sure all
360** function names are recognized and all functions have the correct
361** number of arguments. Leave an error message in pParse->zErrMsg
362** if anything is amiss. Return the number of errors.
363**
364** if pIsAgg is not null and this expression is an aggregate function
365** (like count(*) or max(value)) then write a 1 into *pIsAgg.
366*/
367int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
368 int nErr = 0;
369 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000370 switch( pExpr->op ){
371 case TK_FUNCTION: {
372 int id = sqliteFuncId(&pExpr->token);
373 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
374 int no_such_func = 0;
375 int too_many_args = 0;
376 int too_few_args = 0;
377 int is_agg = 0;
378 int i;
drh967e8b72000-06-21 13:59:10 +0000379 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000380 switch( id ){
381 case FN_Unknown: {
382 no_such_func = 1;
383 break;
384 }
385 case FN_Count: {
386 no_such_func = !allowAgg;
387 too_many_args = n>1;
388 is_agg = 1;
389 break;
390 }
391 case FN_Max:
392 case FN_Min: {
393 too_few_args = allowAgg ? n<1 : n<2;
394 is_agg = n==1;
395 break;
396 }
drh22827922000-06-06 17:27:05 +0000397 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000398 case FN_Sum: {
399 no_such_func = !allowAgg;
400 too_many_args = n>1;
401 too_few_args = n<1;
402 is_agg = 1;
403 break;
404 }
drhbf4133c2001-10-13 02:59:08 +0000405 case FN_Abs:
drh6ec27332000-08-28 15:51:43 +0000406 case FN_Length: {
407 too_few_args = n<1;
408 too_many_args = n>1;
409 break;
410 }
drhbf4133c2001-10-13 02:59:08 +0000411 case FN_Round: {
412 too_few_args = n<1;
413 too_many_args = n>2;
414 break;
415 }
drh6ec27332000-08-28 15:51:43 +0000416 case FN_Substr: {
417 too_few_args = n<3;
418 too_many_args = n>3;
419 break;
420 }
drhcce7d172000-05-31 15:34:51 +0000421 default: break;
422 }
423 if( no_such_func ){
424 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
425 pExpr->token.z, pExpr->token.n, 0);
426 pParse->nErr++;
427 nErr++;
428 }else if( too_many_args ){
429 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
430 pExpr->token.z, pExpr->token.n, "()", 2, 0);
431 pParse->nErr++;
432 nErr++;
433 }else if( too_few_args ){
434 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
435 pExpr->token.z, pExpr->token.n, "()", 2, 0);
436 pParse->nErr++;
437 nErr++;
438 }
drh22827922000-06-06 17:27:05 +0000439 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000440 if( is_agg && pIsAgg ) *pIsAgg = 1;
441 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000442 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
443 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000444 }
445 }
446 default: {
447 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000448 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000449 }
450 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000451 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000452 }
drhfef52082000-06-06 01:50:43 +0000453 if( nErr==0 && pExpr->pList ){
454 int n = pExpr->pList->nExpr;
455 int i;
456 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000457 Expr *pE2 = pExpr->pList->a[i].pExpr;
458 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000459 }
460 }
drhcce7d172000-05-31 15:34:51 +0000461 break;
462 }
463 }
464 return nErr;
465}
466
467/*
468** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000469** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000470*/
471void sqliteExprCode(Parse *pParse, Expr *pExpr){
472 Vdbe *v = pParse->pVdbe;
473 int op;
drhdaffd0e2001-04-11 14:28:42 +0000474 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000475 switch( pExpr->op ){
476 case TK_PLUS: op = OP_Add; break;
477 case TK_MINUS: op = OP_Subtract; break;
478 case TK_STAR: op = OP_Multiply; break;
479 case TK_SLASH: op = OP_Divide; break;
480 case TK_AND: op = OP_And; break;
481 case TK_OR: op = OP_Or; break;
482 case TK_LT: op = OP_Lt; break;
483 case TK_LE: op = OP_Le; break;
484 case TK_GT: op = OP_Gt; break;
485 case TK_GE: op = OP_Ge; break;
486 case TK_NE: op = OP_Ne; break;
487 case TK_EQ: op = OP_Eq; break;
488 case TK_LIKE: op = OP_Like; break;
489 case TK_GLOB: op = OP_Glob; break;
490 case TK_ISNULL: op = OP_IsNull; break;
491 case TK_NOTNULL: op = OP_NotNull; break;
492 case TK_NOT: op = OP_Not; break;
493 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000494 case TK_BITAND: op = OP_BitAnd; break;
495 case TK_BITOR: op = OP_BitOr; break;
496 case TK_BITNOT: op = OP_BitNot; break;
497 case TK_LSHIFT: op = OP_ShiftLeft; break;
498 case TK_RSHIFT: op = OP_ShiftRight; break;
499 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000500 default: break;
501 }
502 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000503 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000504 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000505 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000506 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000507 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000508 }else{
drh99fcd712001-10-13 01:06:47 +0000509 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000510 }
drhcce7d172000-05-31 15:34:51 +0000511 break;
512 }
513 case TK_INTEGER: {
514 int i = atoi(pExpr->token.z);
drh99fcd712001-10-13 01:06:47 +0000515 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhcce7d172000-05-31 15:34:51 +0000516 break;
517 }
518 case TK_FLOAT: {
drh99fcd712001-10-13 01:06:47 +0000519 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000520 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
521 break;
522 }
523 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000524 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000525 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
526 sqliteVdbeDequoteP3(v, addr);
527 break;
528 }
529 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000530 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000531 break;
532 }
533 case TK_AND:
534 case TK_OR:
535 case TK_PLUS:
536 case TK_STAR:
537 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000538 case TK_REM:
539 case TK_BITAND:
540 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000541 case TK_SLASH: {
542 sqliteExprCode(pParse, pExpr->pLeft);
543 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000544 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000545 break;
546 }
drhbf4133c2001-10-13 02:59:08 +0000547 case TK_LSHIFT:
548 case TK_RSHIFT: {
549 sqliteExprCode(pParse, pExpr->pRight);
550 sqliteExprCode(pParse, pExpr->pLeft);
551 sqliteVdbeAddOp(v, op, 0, 0);
552 break;
553 }
drh00400772000-06-16 20:51:26 +0000554 case TK_CONCAT: {
555 sqliteExprCode(pParse, pExpr->pLeft);
556 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000557 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000558 break;
559 }
drhcce7d172000-05-31 15:34:51 +0000560 case TK_LT:
561 case TK_LE:
562 case TK_GT:
563 case TK_GE:
564 case TK_NE:
565 case TK_EQ:
566 case TK_LIKE:
567 case TK_GLOB: {
568 int dest;
drh99fcd712001-10-13 01:06:47 +0000569 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000570 sqliteExprCode(pParse, pExpr->pLeft);
571 sqliteExprCode(pParse, pExpr->pRight);
572 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000573 sqliteVdbeAddOp(v, op, 0, dest);
574 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000575 break;
576 }
drhcce7d172000-05-31 15:34:51 +0000577 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000578 assert( pExpr->pLeft );
579 if( pExpr->pLeft->op==TK_INTEGER ){
580 int i = atoi(pExpr->pLeft->token.z);
drh99fcd712001-10-13 01:06:47 +0000581 sqliteVdbeAddOp(v, OP_Integer, -i, 0);
drh6e142f52000-06-08 13:36:40 +0000582 break;
583 }else if( pExpr->pLeft->op==TK_FLOAT ){
584 Token *p = &pExpr->pLeft->token;
585 char *z = sqliteMalloc( p->n + 2 );
586 sprintf(z, "-%.*s", p->n, p->z);
drh99fcd712001-10-13 01:06:47 +0000587 sqliteVdbeAddOp(v, OP_String, 0, 0);
588 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000589 sqliteFree(z);
590 break;
591 }
drh1ccde152000-06-17 13:12:39 +0000592 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000593 }
drhbf4133c2001-10-13 02:59:08 +0000594 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000595 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000596 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000597 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000598 break;
599 }
600 case TK_ISNULL:
601 case TK_NOTNULL: {
602 int dest;
drh99fcd712001-10-13 01:06:47 +0000603 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000604 sqliteExprCode(pParse, pExpr->pLeft);
605 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000606 sqliteVdbeAddOp(v, op, 0, dest);
607 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000608 break;
609 }
drh22827922000-06-06 17:27:05 +0000610 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000611 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh967e8b72000-06-21 13:59:10 +0000612 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000613 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
drh99fcd712001-10-13 01:06:47 +0000614 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount);
615 sqliteVdbeAddOp(v, OP_Divide, 0, 0);
drh22827922000-06-06 17:27:05 +0000616 }
617 break;
618 }
drhcce7d172000-05-31 15:34:51 +0000619 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000620 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000621 int op;
622 int i;
623 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000624 switch( id ){
drh6ec27332000-08-28 15:51:43 +0000625 case FN_Min:
626 case FN_Max: {
627 op = id==FN_Min ? OP_Min : OP_Max;
628 for(i=0; i<pList->nExpr; i++){
629 sqliteExprCode(pParse, pList->a[i].pExpr);
630 if( i>0 ){
drh99fcd712001-10-13 01:06:47 +0000631 sqliteVdbeAddOp(v, op, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000632 }
633 }
634 break;
635 }
drhbf4133c2001-10-13 02:59:08 +0000636 case FN_Abs: {
637 sqliteExprCode(pParse, pList->a[0].pExpr);
638 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
639 break;
640 }
641 case FN_Round: {
642 if( pList->nExpr==2 ){
643 sqliteExprCode(pParse, pList->a[1].pExpr);
644 }else{
645 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
646 }
647 sqliteExprCode(pParse, pList->a[0].pExpr);
648 sqliteVdbeAddOp(v, OP_Precision, 0, 0);
649 break;
650 }
drh6ec27332000-08-28 15:51:43 +0000651 case FN_Length: {
652 sqliteExprCode(pParse, pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000653 sqliteVdbeAddOp(v, OP_Strlen, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000654 break;
655 }
656 case FN_Substr: {
657 for(i=0; i<pList->nExpr; i++){
658 sqliteExprCode(pParse, pList->a[i].pExpr);
659 }
drh99fcd712001-10-13 01:06:47 +0000660 sqliteVdbeAddOp(v, OP_Substr, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000661 break;
662 }
663 default: {
664 /* Can't happen! */
665 break;
drhcce7d172000-05-31 15:34:51 +0000666 }
667 }
668 break;
669 }
drh19a775c2000-06-05 18:54:46 +0000670 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000671 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000672 break;
673 }
drhfef52082000-06-06 01:50:43 +0000674 case TK_IN: {
675 int addr;
drh99fcd712001-10-13 01:06:47 +0000676 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000677 sqliteExprCode(pParse, pExpr->pLeft);
678 addr = sqliteVdbeCurrentAddr(v);
679 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000680 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000681 }else{
drh99fcd712001-10-13 01:06:47 +0000682 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000683 }
drh99fcd712001-10-13 01:06:47 +0000684 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000685 break;
686 }
687 case TK_BETWEEN: {
688 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000689 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000690 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000691 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000692 sqliteVdbeResolveLabel(v, lbl);
693 break;
694 }
drhcce7d172000-05-31 15:34:51 +0000695 }
696 return;
697}
698
699/*
700** Generate code for a boolean expression such that a jump is made
701** to the label "dest" if the expression is true but execution
702** continues straight thru if the expression is false.
703*/
704void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
705 Vdbe *v = pParse->pVdbe;
706 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000707 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000708 switch( pExpr->op ){
709 case TK_LT: op = OP_Lt; break;
710 case TK_LE: op = OP_Le; break;
711 case TK_GT: op = OP_Gt; break;
712 case TK_GE: op = OP_Ge; break;
713 case TK_NE: op = OP_Ne; break;
714 case TK_EQ: op = OP_Eq; break;
715 case TK_LIKE: op = OP_Like; break;
716 case TK_GLOB: op = OP_Glob; break;
717 case TK_ISNULL: op = OP_IsNull; break;
718 case TK_NOTNULL: op = OP_NotNull; break;
719 default: break;
720 }
721 switch( pExpr->op ){
722 case TK_AND: {
723 int d2 = sqliteVdbeMakeLabel(v);
724 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
725 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
726 sqliteVdbeResolveLabel(v, d2);
727 break;
728 }
729 case TK_OR: {
730 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
731 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
732 break;
733 }
734 case TK_NOT: {
735 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
736 break;
737 }
738 case TK_LT:
739 case TK_LE:
740 case TK_GT:
741 case TK_GE:
742 case TK_NE:
743 case TK_EQ:
744 case TK_LIKE:
745 case TK_GLOB: {
746 sqliteExprCode(pParse, pExpr->pLeft);
747 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000748 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000749 break;
750 }
751 case TK_ISNULL:
752 case TK_NOTNULL: {
753 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000754 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000755 break;
756 }
drhfef52082000-06-06 01:50:43 +0000757 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000758 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000759 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000760 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000761 }else{
drh99fcd712001-10-13 01:06:47 +0000762 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000763 }
764 break;
765 }
766 case TK_BETWEEN: {
767 int lbl = sqliteVdbeMakeLabel(v);
768 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000769 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000770 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000771 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +0000772 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000773 sqliteVdbeAddOp(v, OP_Le, 0, dest);
774 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
775 sqliteVdbeResolveLabel(v, lbl);
776 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +0000777 break;
778 }
drhcce7d172000-05-31 15:34:51 +0000779 default: {
780 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000781 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000782 break;
783 }
784 }
785}
786
787/*
drh66b89c82000-11-28 20:47:17 +0000788** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000789** to the label "dest" if the expression is false but execution
790** continues straight thru if the expression is true.
791*/
792void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
793 Vdbe *v = pParse->pVdbe;
794 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000795 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000796 switch( pExpr->op ){
797 case TK_LT: op = OP_Ge; break;
798 case TK_LE: op = OP_Gt; break;
799 case TK_GT: op = OP_Le; break;
800 case TK_GE: op = OP_Lt; break;
801 case TK_NE: op = OP_Eq; break;
802 case TK_EQ: op = OP_Ne; break;
803 case TK_LIKE: op = OP_Like; break;
804 case TK_GLOB: op = OP_Glob; break;
805 case TK_ISNULL: op = OP_NotNull; break;
806 case TK_NOTNULL: op = OP_IsNull; break;
807 default: break;
808 }
809 switch( pExpr->op ){
810 case TK_AND: {
811 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
812 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
813 break;
814 }
815 case TK_OR: {
816 int d2 = sqliteVdbeMakeLabel(v);
817 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
818 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
819 sqliteVdbeResolveLabel(v, d2);
820 break;
821 }
822 case TK_NOT: {
823 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
824 break;
825 }
826 case TK_LT:
827 case TK_LE:
828 case TK_GT:
829 case TK_GE:
830 case TK_NE:
831 case TK_EQ: {
832 sqliteExprCode(pParse, pExpr->pLeft);
833 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000834 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000835 break;
836 }
837 case TK_LIKE:
838 case TK_GLOB: {
839 sqliteExprCode(pParse, pExpr->pLeft);
840 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000841 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +0000842 break;
843 }
844 case TK_ISNULL:
845 case TK_NOTNULL: {
846 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000847 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000848 break;
849 }
drhfef52082000-06-06 01:50:43 +0000850 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000851 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000852 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000853 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000854 }else{
drh99fcd712001-10-13 01:06:47 +0000855 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000856 }
857 break;
858 }
859 case TK_BETWEEN: {
860 int addr;
861 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000862 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000863 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
864 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +0000865 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
866 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
867 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +0000868 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000869 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +0000870 break;
871 }
drhcce7d172000-05-31 15:34:51 +0000872 default: {
873 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000874 sqliteVdbeAddOp(v, OP_Not, 0, 0);
875 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000876 break;
877 }
878 }
879}
drh22827922000-06-06 17:27:05 +0000880
881/*
882** Do a deep comparison of two expression trees. Return TRUE (non-zero)
883** if they are identical and return FALSE if they differ in any way.
884*/
drhd8bc7082000-06-07 23:51:50 +0000885int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +0000886 int i;
887 if( pA==0 ){
888 return pB==0;
889 }else if( pB==0 ){
890 return 0;
891 }
892 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +0000893 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
894 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +0000895 if( pA->pList ){
896 if( pB->pList==0 ) return 0;
897 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
898 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000899 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +0000900 return 0;
901 }
902 }
903 }else if( pB->pList ){
904 return 0;
905 }
906 if( pA->pSelect || pB->pSelect ) return 0;
907 if( pA->token.z ){
908 if( pB->token.z==0 ) return 0;
909 if( pB->token.n!=pA->token.n ) return 0;
910 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
911 }
912 return 1;
913}
914
915/*
916** Add a new element to the pParse->aAgg[] array and return its index.
917*/
918static int appendAggInfo(Parse *pParse){
919 if( (pParse->nAgg & 0x7)==0 ){
920 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +0000921 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
922 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +0000923 return -1;
924 }
drh6d4abfb2001-10-22 02:58:08 +0000925 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +0000926 }
927 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
928 return pParse->nAgg++;
929}
930
931/*
932** Analyze the given expression looking for aggregate functions and
933** for variables that need to be added to the pParse->aAgg[] array.
934** Make additional entries to the pParse->aAgg[] array as necessary.
935**
936** This routine should only be called after the expression has been
937** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
938**
939** If errors are seen, leave an error message in zErrMsg and return
940** the number of errors.
941*/
942int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
943 int i;
944 AggExpr *aAgg;
945 int nErr = 0;
946
947 if( pExpr==0 ) return 0;
948 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000949 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000950 aAgg = pParse->aAgg;
951 for(i=0; i<pParse->nAgg; i++){
952 if( aAgg[i].isAgg ) continue;
953 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +0000954 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +0000955 break;
956 }
957 }
958 if( i>=pParse->nAgg ){
959 i = appendAggInfo(pParse);
960 if( i<0 ) return 1;
961 pParse->aAgg[i].isAgg = 0;
962 pParse->aAgg[i].pExpr = pExpr;
963 }
drhaaf88722000-06-08 11:25:00 +0000964 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +0000965 break;
966 }
967 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000968 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000969 if( pParse->iAggCount>=0 ){
970 i = pParse->iAggCount;
971 }else{
972 i = appendAggInfo(pParse);
973 if( i<0 ) return 1;
974 pParse->aAgg[i].isAgg = 1;
975 pParse->aAgg[i].pExpr = 0;
976 pParse->iAggCount = i;
977 }
drh967e8b72000-06-21 13:59:10 +0000978 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +0000979 pExpr->iAgg = i;
980 break;
981 }
982 }
983 aAgg = pParse->aAgg;
984 for(i=0; i<pParse->nAgg; i++){
985 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +0000986 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +0000987 break;
988 }
989 }
990 if( i>=pParse->nAgg ){
991 i = appendAggInfo(pParse);
992 if( i<0 ) return 1;
993 pParse->aAgg[i].isAgg = 1;
994 pParse->aAgg[i].pExpr = pExpr;
995 }
996 pExpr->iAgg = i;
997 break;
998 }
999 default: {
1000 if( pExpr->pLeft ){
1001 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1002 }
1003 if( nErr==0 && pExpr->pRight ){
1004 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1005 }
1006 if( nErr==0 && pExpr->pList ){
1007 int n = pExpr->pList->nExpr;
1008 int i;
1009 for(i=0; nErr==0 && i<n; i++){
1010 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1011 }
1012 }
1013 break;
1014 }
1015 }
1016 return nErr;
1017}