blob: 37f4d28e1bf1505a3abbd3318354c418e716f813 [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**
drh4a324312001-12-21 14:30:42 +000015** $Id: expr.c,v 1.35 2001/12/21 14:30:43 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;
drh4a324312001-12-21 14:30:42 +0000130 if( j==pTab->iPKey ){
131 /* Substitute the record number for the INTEGER PRIMARY KEY */
132 pExpr->iColumn = -1;
133 }else{
134 pExpr->iColumn = j;
135 }
drhcce7d172000-05-31 15:34:51 +0000136 }
137 }
138 }
drhc4a3c772001-04-04 11:48:57 +0000139 if( cnt==0 && sqliteIsRowid(z) ){
140 pExpr->iColumn = -1;
141 pExpr->iTable = pParse->nTab;
142 cnt = 1 + (pTabList->nId>1);
143 }
drhcce7d172000-05-31 15:34:51 +0000144 sqliteFree(z);
145 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000146 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000147 pExpr->token.z, pExpr->token.n, 0);
148 pParse->nErr++;
149 return 1;
150 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000151 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000152 pExpr->token.z, pExpr->token.n, 0);
153 pParse->nErr++;
154 return 1;
155 }
drh967e8b72000-06-21 13:59:10 +0000156 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000157 break;
158 }
159
drh967e8b72000-06-21 13:59:10 +0000160 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000161 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000162 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000163 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000164 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000165 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000166 char *zLeft, *zRight; /* Text of an identifier */
167
168 pLeft = pExpr->pLeft;
169 pRight = pExpr->pRight;
170 assert( pLeft && pLeft->op==TK_ID );
171 assert( pRight && pRight->op==TK_ID );
drh6e142f52000-06-08 13:36:40 +0000172 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
173 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000174 if( zLeft==0 || zRight==0 ){
175 sqliteFree(zLeft);
176 sqliteFree(zRight);
177 return 1;
178 }
drh87c40e82001-07-23 14:33:02 +0000179 sqliteDequote(zLeft);
180 sqliteDequote(zRight);
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;
drh4a324312001-12-21 14:30:42 +0000198 if( j==pTab->iPKey ){
199 /* Substitute the record number for the INTEGER PRIMARY KEY */
200 pExpr->iColumn = -1;
201 }else{
202 pExpr->iColumn = j;
203 }
drhcce7d172000-05-31 15:34:51 +0000204 }
205 }
206 }
drhc4a3c772001-04-04 11:48:57 +0000207 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
208 cnt = 1;
209 pExpr->iColumn = -1;
210 }
drhcce7d172000-05-31 15:34:51 +0000211 sqliteFree(zLeft);
212 sqliteFree(zRight);
213 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000214 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000215 pLeft->token.z, pLeft->token.n, ".", 1,
216 pRight->token.z, pRight->token.n, 0);
217 pParse->nErr++;
218 return 1;
219 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000220 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000221 pLeft->token.z, pLeft->token.n, ".", 1,
222 pRight->token.z, pRight->token.n, 0);
223 pParse->nErr++;
224 return 1;
225 }
226 sqliteExprDelete(pLeft);
227 pExpr->pLeft = 0;
228 sqliteExprDelete(pRight);
229 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000230 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000231 break;
232 }
233
drhfef52082000-06-06 01:50:43 +0000234 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000235 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000236 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000237 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
238 return 1;
239 }
drhfef52082000-06-06 01:50:43 +0000240 if( pExpr->pSelect ){
241 /* Case 1: expr IN (SELECT ...)
242 **
243 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000244 ** table. The cursor number of the temporary table has already
245 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000246 */
drh99fcd712001-10-13 01:06:47 +0000247 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000248 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
249 }else if( pExpr->pList ){
250 /* Case 2: expr IN (exprlist)
251 **
252 ** Create a set to put the exprlist values in. The Set id is stored
253 ** in iTable.
254 */
255 int i, iSet;
256 for(i=0; i<pExpr->pList->nExpr; i++){
257 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000258 if( !isConstant(pE2) ){
259 sqliteSetString(&pParse->zErrMsg,
260 "right-hand side of IN operator must be constant", 0);
261 pParse->nErr++;
262 return 1;
263 }
drh4794b982000-06-06 13:54:14 +0000264 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
265 return 1;
266 }
drhfef52082000-06-06 01:50:43 +0000267 }
268 iSet = pExpr->iTable = pParse->nSet++;
269 for(i=0; i<pExpr->pList->nExpr; i++){
270 Expr *pE2 = pExpr->pList->a[i].pExpr;
271 switch( pE2->op ){
272 case TK_FLOAT:
273 case TK_INTEGER:
274 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000275 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000276 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
277 sqliteVdbeDequoteP3(v, addr);
278 break;
279 }
280 default: {
281 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000282 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000283 break;
284 }
285 }
286 }
287 }
drhcfab11b2000-06-06 03:31:22 +0000288 break;
drhfef52082000-06-06 01:50:43 +0000289 }
290
drh19a775c2000-06-05 18:54:46 +0000291 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000292 /* This has to be a scalar SELECT. Generate code to put the
293 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000294 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000295 */
drh967e8b72000-06-21 13:59:10 +0000296 pExpr->iColumn = pParse->nMem++;
297 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){
drh19a775c2000-06-05 18:54:46 +0000298 return 1;
299 }
300 break;
301 }
302
drhcce7d172000-05-31 15:34:51 +0000303 /* For all else, just recursively walk the tree */
304 default: {
drh4794b982000-06-06 13:54:14 +0000305 if( pExpr->pLeft
306 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000307 return 1;
308 }
309 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000310 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000311 return 1;
312 }
313 if( pExpr->pList ){
314 int i;
315 ExprList *pList = pExpr->pList;
316 for(i=0; i<pList->nExpr; i++){
317 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
318 return 1;
319 }
320 }
321 }
322 }
323 }
324 return 0;
325}
326
327#if 0 /* NOT USED */
328/*
329** Compare a token against a string. Return TRUE if they match.
330*/
331static int sqliteTokenCmp(Token *pToken, const char *zStr){
332 int n = strlen(zStr);
333 if( n!=pToken->n ) return 0;
334 return sqliteStrNICmp(pToken->z, zStr, n)==0;
335}
336#endif
337
338/*
339** Convert a function name into its integer identifier. Return the
340** identifier. Return FN_Unknown if the function name is unknown.
341*/
342int sqliteFuncId(Token *pToken){
343 static const struct {
344 char *zName;
345 int len;
346 int id;
347 } aFunc[] = {
drhbf4133c2001-10-13 02:59:08 +0000348 { "count", 5, FN_Count },
349 { "min", 3, FN_Min },
350 { "max", 3, FN_Max },
351 { "sum", 3, FN_Sum },
352 { "avg", 3, FN_Avg },
drhbf4133c2001-10-13 02:59:08 +0000353 { "length", 6, FN_Length },
354 { "substr", 6, FN_Substr },
355 { "abs", 3, FN_Abs },
356 { "round", 5, FN_Round },
drhcce7d172000-05-31 15:34:51 +0000357 };
358 int i;
359 for(i=0; i<ArraySize(aFunc); i++){
360 if( aFunc[i].len==pToken->n
361 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
362 return aFunc[i].id;
363 }
364 }
365 return FN_Unknown;
366}
367
368/*
369** Error check the functions in an expression. Make sure all
370** function names are recognized and all functions have the correct
371** number of arguments. Leave an error message in pParse->zErrMsg
372** if anything is amiss. Return the number of errors.
373**
374** if pIsAgg is not null and this expression is an aggregate function
375** (like count(*) or max(value)) then write a 1 into *pIsAgg.
376*/
377int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
378 int nErr = 0;
379 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000380 switch( pExpr->op ){
381 case TK_FUNCTION: {
382 int id = sqliteFuncId(&pExpr->token);
383 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
384 int no_such_func = 0;
385 int too_many_args = 0;
386 int too_few_args = 0;
387 int is_agg = 0;
388 int i;
drh967e8b72000-06-21 13:59:10 +0000389 pExpr->iColumn = id;
drhcce7d172000-05-31 15:34:51 +0000390 switch( id ){
391 case FN_Unknown: {
392 no_such_func = 1;
393 break;
394 }
395 case FN_Count: {
396 no_such_func = !allowAgg;
397 too_many_args = n>1;
398 is_agg = 1;
399 break;
400 }
401 case FN_Max:
402 case FN_Min: {
403 too_few_args = allowAgg ? n<1 : n<2;
404 is_agg = n==1;
405 break;
406 }
drh22827922000-06-06 17:27:05 +0000407 case FN_Avg:
drhcce7d172000-05-31 15:34:51 +0000408 case FN_Sum: {
409 no_such_func = !allowAgg;
410 too_many_args = n>1;
411 too_few_args = n<1;
412 is_agg = 1;
413 break;
414 }
drhbf4133c2001-10-13 02:59:08 +0000415 case FN_Abs:
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 }
drhbf4133c2001-10-13 02:59:08 +0000421 case FN_Round: {
422 too_few_args = n<1;
423 too_many_args = n>2;
424 break;
425 }
drh6ec27332000-08-28 15:51:43 +0000426 case FN_Substr: {
427 too_few_args = n<3;
428 too_many_args = n>3;
429 break;
430 }
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;
drhdaffd0e2001-04-11 14:28:42 +0000484 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000485 switch( pExpr->op ){
486 case TK_PLUS: op = OP_Add; break;
487 case TK_MINUS: op = OP_Subtract; break;
488 case TK_STAR: op = OP_Multiply; break;
489 case TK_SLASH: op = OP_Divide; break;
490 case TK_AND: op = OP_And; break;
491 case TK_OR: op = OP_Or; break;
492 case TK_LT: op = OP_Lt; break;
493 case TK_LE: op = OP_Le; break;
494 case TK_GT: op = OP_Gt; break;
495 case TK_GE: op = OP_Ge; break;
496 case TK_NE: op = OP_Ne; break;
497 case TK_EQ: op = OP_Eq; break;
498 case TK_LIKE: op = OP_Like; break;
499 case TK_GLOB: op = OP_Glob; break;
500 case TK_ISNULL: op = OP_IsNull; break;
501 case TK_NOTNULL: op = OP_NotNull; break;
502 case TK_NOT: op = OP_Not; break;
503 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000504 case TK_BITAND: op = OP_BitAnd; break;
505 case TK_BITOR: op = OP_BitOr; break;
506 case TK_BITNOT: op = OP_BitNot; break;
507 case TK_LSHIFT: op = OP_ShiftLeft; break;
508 case TK_RSHIFT: op = OP_ShiftRight; break;
509 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000510 default: break;
511 }
512 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000513 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000514 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000515 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000516 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000517 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000518 }else{
drh99fcd712001-10-13 01:06:47 +0000519 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000520 }
drhcce7d172000-05-31 15:34:51 +0000521 break;
522 }
523 case TK_INTEGER: {
drh7a7c7392001-11-24 00:31:46 +0000524 sqliteVdbeAddOp(v, OP_String, 0, 0);
525 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +0000526 break;
527 }
528 case TK_FLOAT: {
drh99fcd712001-10-13 01:06:47 +0000529 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000530 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
531 break;
532 }
533 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000534 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000535 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
536 sqliteVdbeDequoteP3(v, addr);
537 break;
538 }
539 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +0000540 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000541 break;
542 }
543 case TK_AND:
544 case TK_OR:
545 case TK_PLUS:
546 case TK_STAR:
547 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +0000548 case TK_REM:
549 case TK_BITAND:
550 case TK_BITOR:
drhcce7d172000-05-31 15:34:51 +0000551 case TK_SLASH: {
552 sqliteExprCode(pParse, pExpr->pLeft);
553 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000554 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000555 break;
556 }
drhbf4133c2001-10-13 02:59:08 +0000557 case TK_LSHIFT:
558 case TK_RSHIFT: {
559 sqliteExprCode(pParse, pExpr->pRight);
560 sqliteExprCode(pParse, pExpr->pLeft);
561 sqliteVdbeAddOp(v, op, 0, 0);
562 break;
563 }
drh00400772000-06-16 20:51:26 +0000564 case TK_CONCAT: {
565 sqliteExprCode(pParse, pExpr->pLeft);
566 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000567 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +0000568 break;
569 }
drhcce7d172000-05-31 15:34:51 +0000570 case TK_LT:
571 case TK_LE:
572 case TK_GT:
573 case TK_GE:
574 case TK_NE:
575 case TK_EQ:
576 case TK_LIKE:
577 case TK_GLOB: {
578 int dest;
drh99fcd712001-10-13 01:06:47 +0000579 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000580 sqliteExprCode(pParse, pExpr->pLeft);
581 sqliteExprCode(pParse, pExpr->pRight);
582 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000583 sqliteVdbeAddOp(v, op, 0, dest);
584 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000585 break;
586 }
drhcce7d172000-05-31 15:34:51 +0000587 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +0000588 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +0000589 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +0000590 Token *p = &pExpr->pLeft->token;
591 char *z = sqliteMalloc( p->n + 2 );
592 sprintf(z, "-%.*s", p->n, p->z);
drh99fcd712001-10-13 01:06:47 +0000593 sqliteVdbeAddOp(v, OP_String, 0, 0);
594 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +0000595 sqliteFree(z);
596 break;
597 }
drh1ccde152000-06-17 13:12:39 +0000598 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +0000599 }
drhbf4133c2001-10-13 02:59:08 +0000600 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +0000601 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +0000602 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000603 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000604 break;
605 }
606 case TK_ISNULL:
607 case TK_NOTNULL: {
608 int dest;
drh99fcd712001-10-13 01:06:47 +0000609 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000610 sqliteExprCode(pParse, pExpr->pLeft);
611 dest = sqliteVdbeCurrentAddr(v) + 2;
drh99fcd712001-10-13 01:06:47 +0000612 sqliteVdbeAddOp(v, op, 0, dest);
613 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000614 break;
615 }
drh22827922000-06-06 17:27:05 +0000616 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +0000617 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh967e8b72000-06-21 13:59:10 +0000618 if( pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000619 assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg );
drh99fcd712001-10-13 01:06:47 +0000620 sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount);
621 sqliteVdbeAddOp(v, OP_Divide, 0, 0);
drh22827922000-06-06 17:27:05 +0000622 }
623 break;
624 }
drhcce7d172000-05-31 15:34:51 +0000625 case TK_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000626 int id = pExpr->iColumn;
drhcce7d172000-05-31 15:34:51 +0000627 int op;
628 int i;
629 ExprList *pList = pExpr->pList;
drh6ec27332000-08-28 15:51:43 +0000630 switch( id ){
drh6ec27332000-08-28 15:51:43 +0000631 case FN_Min:
632 case FN_Max: {
633 op = id==FN_Min ? OP_Min : OP_Max;
634 for(i=0; i<pList->nExpr; i++){
635 sqliteExprCode(pParse, pList->a[i].pExpr);
636 if( i>0 ){
drh99fcd712001-10-13 01:06:47 +0000637 sqliteVdbeAddOp(v, op, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000638 }
639 }
640 break;
641 }
drhbf4133c2001-10-13 02:59:08 +0000642 case FN_Abs: {
643 sqliteExprCode(pParse, pList->a[0].pExpr);
644 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
645 break;
646 }
647 case FN_Round: {
648 if( pList->nExpr==2 ){
649 sqliteExprCode(pParse, pList->a[1].pExpr);
650 }else{
651 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
652 }
653 sqliteExprCode(pParse, pList->a[0].pExpr);
654 sqliteVdbeAddOp(v, OP_Precision, 0, 0);
655 break;
656 }
drh6ec27332000-08-28 15:51:43 +0000657 case FN_Length: {
658 sqliteExprCode(pParse, pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000659 sqliteVdbeAddOp(v, OP_Strlen, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000660 break;
661 }
662 case FN_Substr: {
663 for(i=0; i<pList->nExpr; i++){
664 sqliteExprCode(pParse, pList->a[i].pExpr);
665 }
drh99fcd712001-10-13 01:06:47 +0000666 sqliteVdbeAddOp(v, OP_Substr, 0, 0);
drh6ec27332000-08-28 15:51:43 +0000667 break;
668 }
669 default: {
670 /* Can't happen! */
671 break;
drhcce7d172000-05-31 15:34:51 +0000672 }
673 }
674 break;
675 }
drh19a775c2000-06-05 18:54:46 +0000676 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +0000677 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +0000678 break;
679 }
drhfef52082000-06-06 01:50:43 +0000680 case TK_IN: {
681 int addr;
drh99fcd712001-10-13 01:06:47 +0000682 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +0000683 sqliteExprCode(pParse, pExpr->pLeft);
684 addr = sqliteVdbeCurrentAddr(v);
685 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000686 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000687 }else{
drh99fcd712001-10-13 01:06:47 +0000688 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
drhfef52082000-06-06 01:50:43 +0000689 }
drh99fcd712001-10-13 01:06:47 +0000690 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +0000691 break;
692 }
693 case TK_BETWEEN: {
694 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000695 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhfef52082000-06-06 01:50:43 +0000696 sqliteExprIfFalse(pParse, pExpr, lbl);
drh99fcd712001-10-13 01:06:47 +0000697 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drhfef52082000-06-06 01:50:43 +0000698 sqliteVdbeResolveLabel(v, lbl);
699 break;
700 }
drhcce7d172000-05-31 15:34:51 +0000701 }
702 return;
703}
704
705/*
706** Generate code for a boolean expression such that a jump is made
707** to the label "dest" if the expression is true but execution
708** continues straight thru if the expression is false.
709*/
710void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
711 Vdbe *v = pParse->pVdbe;
712 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000713 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000714 switch( pExpr->op ){
715 case TK_LT: op = OP_Lt; break;
716 case TK_LE: op = OP_Le; break;
717 case TK_GT: op = OP_Gt; break;
718 case TK_GE: op = OP_Ge; break;
719 case TK_NE: op = OP_Ne; break;
720 case TK_EQ: op = OP_Eq; break;
721 case TK_LIKE: op = OP_Like; break;
722 case TK_GLOB: op = OP_Glob; break;
723 case TK_ISNULL: op = OP_IsNull; break;
724 case TK_NOTNULL: op = OP_NotNull; break;
725 default: break;
726 }
727 switch( pExpr->op ){
728 case TK_AND: {
729 int d2 = sqliteVdbeMakeLabel(v);
730 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
731 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
732 sqliteVdbeResolveLabel(v, d2);
733 break;
734 }
735 case TK_OR: {
736 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
737 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
738 break;
739 }
740 case TK_NOT: {
741 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
742 break;
743 }
744 case TK_LT:
745 case TK_LE:
746 case TK_GT:
747 case TK_GE:
748 case TK_NE:
749 case TK_EQ:
750 case TK_LIKE:
751 case TK_GLOB: {
752 sqliteExprCode(pParse, pExpr->pLeft);
753 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000754 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000755 break;
756 }
757 case TK_ISNULL:
758 case TK_NOTNULL: {
759 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000760 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000761 break;
762 }
drhfef52082000-06-06 01:50:43 +0000763 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000764 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000765 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000766 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000767 }else{
drh99fcd712001-10-13 01:06:47 +0000768 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000769 }
770 break;
771 }
772 case TK_BETWEEN: {
773 int lbl = sqliteVdbeMakeLabel(v);
774 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000775 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000776 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000777 sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
drhfef52082000-06-06 01:50:43 +0000778 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000779 sqliteVdbeAddOp(v, OP_Le, 0, dest);
780 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
781 sqliteVdbeResolveLabel(v, lbl);
782 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +0000783 break;
784 }
drhcce7d172000-05-31 15:34:51 +0000785 default: {
786 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000787 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000788 break;
789 }
790 }
791}
792
793/*
drh66b89c82000-11-28 20:47:17 +0000794** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +0000795** to the label "dest" if the expression is false but execution
796** continues straight thru if the expression is true.
797*/
798void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
799 Vdbe *v = pParse->pVdbe;
800 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +0000801 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000802 switch( pExpr->op ){
803 case TK_LT: op = OP_Ge; break;
804 case TK_LE: op = OP_Gt; break;
805 case TK_GT: op = OP_Le; break;
806 case TK_GE: op = OP_Lt; break;
807 case TK_NE: op = OP_Eq; break;
808 case TK_EQ: op = OP_Ne; break;
809 case TK_LIKE: op = OP_Like; break;
810 case TK_GLOB: op = OP_Glob; break;
811 case TK_ISNULL: op = OP_NotNull; break;
812 case TK_NOTNULL: op = OP_IsNull; break;
813 default: break;
814 }
815 switch( pExpr->op ){
816 case TK_AND: {
817 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
818 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
819 break;
820 }
821 case TK_OR: {
822 int d2 = sqliteVdbeMakeLabel(v);
823 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
824 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
825 sqliteVdbeResolveLabel(v, d2);
826 break;
827 }
828 case TK_NOT: {
829 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
830 break;
831 }
832 case TK_LT:
833 case TK_LE:
834 case TK_GT:
835 case TK_GE:
836 case TK_NE:
837 case TK_EQ: {
838 sqliteExprCode(pParse, pExpr->pLeft);
839 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000840 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000841 break;
842 }
843 case TK_LIKE:
844 case TK_GLOB: {
845 sqliteExprCode(pParse, pExpr->pLeft);
846 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +0000847 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +0000848 break;
849 }
850 case TK_ISNULL:
851 case TK_NOTNULL: {
852 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000853 sqliteVdbeAddOp(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000854 break;
855 }
drhfef52082000-06-06 01:50:43 +0000856 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000857 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000858 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +0000859 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000860 }else{
drh99fcd712001-10-13 01:06:47 +0000861 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +0000862 }
863 break;
864 }
865 case TK_BETWEEN: {
866 int addr;
867 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +0000868 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +0000869 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
870 addr = sqliteVdbeCurrentAddr(v);
drh99fcd712001-10-13 01:06:47 +0000871 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
872 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
873 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +0000874 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drh99fcd712001-10-13 01:06:47 +0000875 sqliteVdbeAddOp(v, OP_Gt, 0, dest);
drhfef52082000-06-06 01:50:43 +0000876 break;
877 }
drhcce7d172000-05-31 15:34:51 +0000878 default: {
879 sqliteExprCode(pParse, pExpr);
drh99fcd712001-10-13 01:06:47 +0000880 sqliteVdbeAddOp(v, OP_Not, 0, 0);
881 sqliteVdbeAddOp(v, OP_If, 0, dest);
drhcce7d172000-05-31 15:34:51 +0000882 break;
883 }
884 }
885}
drh22827922000-06-06 17:27:05 +0000886
887/*
888** Do a deep comparison of two expression trees. Return TRUE (non-zero)
889** if they are identical and return FALSE if they differ in any way.
890*/
drhd8bc7082000-06-07 23:51:50 +0000891int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +0000892 int i;
893 if( pA==0 ){
894 return pB==0;
895 }else if( pB==0 ){
896 return 0;
897 }
898 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +0000899 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
900 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +0000901 if( pA->pList ){
902 if( pB->pList==0 ) return 0;
903 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
904 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000905 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +0000906 return 0;
907 }
908 }
909 }else if( pB->pList ){
910 return 0;
911 }
912 if( pA->pSelect || pB->pSelect ) return 0;
913 if( pA->token.z ){
914 if( pB->token.z==0 ) return 0;
915 if( pB->token.n!=pA->token.n ) return 0;
916 if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
917 }
918 return 1;
919}
920
921/*
922** Add a new element to the pParse->aAgg[] array and return its index.
923*/
924static int appendAggInfo(Parse *pParse){
925 if( (pParse->nAgg & 0x7)==0 ){
926 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +0000927 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
928 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +0000929 return -1;
930 }
drh6d4abfb2001-10-22 02:58:08 +0000931 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +0000932 }
933 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
934 return pParse->nAgg++;
935}
936
937/*
938** Analyze the given expression looking for aggregate functions and
939** for variables that need to be added to the pParse->aAgg[] array.
940** Make additional entries to the pParse->aAgg[] array as necessary.
941**
942** This routine should only be called after the expression has been
943** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
944**
945** If errors are seen, leave an error message in zErrMsg and return
946** the number of errors.
947*/
948int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
949 int i;
950 AggExpr *aAgg;
951 int nErr = 0;
952
953 if( pExpr==0 ) return 0;
954 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000955 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000956 aAgg = pParse->aAgg;
957 for(i=0; i<pParse->nAgg; i++){
958 if( aAgg[i].isAgg ) continue;
959 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +0000960 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +0000961 break;
962 }
963 }
964 if( i>=pParse->nAgg ){
965 i = appendAggInfo(pParse);
966 if( i<0 ) return 1;
967 pParse->aAgg[i].isAgg = 0;
968 pParse->aAgg[i].pExpr = pExpr;
969 }
drhaaf88722000-06-08 11:25:00 +0000970 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +0000971 break;
972 }
973 case TK_AGG_FUNCTION: {
drh967e8b72000-06-21 13:59:10 +0000974 if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){
drh22827922000-06-06 17:27:05 +0000975 if( pParse->iAggCount>=0 ){
976 i = pParse->iAggCount;
977 }else{
978 i = appendAggInfo(pParse);
979 if( i<0 ) return 1;
980 pParse->aAgg[i].isAgg = 1;
981 pParse->aAgg[i].pExpr = 0;
982 pParse->iAggCount = i;
983 }
drh967e8b72000-06-21 13:59:10 +0000984 if( pExpr->iColumn==FN_Count ){
drh22827922000-06-06 17:27:05 +0000985 pExpr->iAgg = i;
986 break;
987 }
988 }
989 aAgg = pParse->aAgg;
990 for(i=0; i<pParse->nAgg; i++){
991 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +0000992 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +0000993 break;
994 }
995 }
996 if( i>=pParse->nAgg ){
997 i = appendAggInfo(pParse);
998 if( i<0 ) return 1;
999 pParse->aAgg[i].isAgg = 1;
1000 pParse->aAgg[i].pExpr = pExpr;
1001 }
1002 pExpr->iAgg = i;
1003 break;
1004 }
1005 default: {
1006 if( pExpr->pLeft ){
1007 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1008 }
1009 if( nErr==0 && pExpr->pRight ){
1010 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1011 }
1012 if( nErr==0 && pExpr->pList ){
1013 int n = pExpr->pList->nExpr;
1014 int i;
1015 for(i=0; nErr==0 && i<n; i++){
1016 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1017 }
1018 }
1019 break;
1020 }
1021 }
1022 return nErr;
1023}