blob: bcfcd69bed9359f3af26041295df56ca21bb362a [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*************************************************************************
24** This file contains C code routines that are called by the parser
25** to handle SELECT statements.
26**
drh82c3d632000-06-06 21:56:07 +000027** $Id: select.c,v 1.13 2000/06/06 21:56:08 drh Exp $
drhcce7d172000-05-31 15:34:51 +000028*/
29#include "sqliteInt.h"
30
drhcce7d172000-05-31 15:34:51 +000031/*
drh9bb61fe2000-06-05 16:01:39 +000032** Allocate a new Select structure and return a pointer to that
33** structure.
drhcce7d172000-05-31 15:34:51 +000034*/
drh9bb61fe2000-06-05 16:01:39 +000035Select *sqliteSelectNew(
36 ExprList *pEList,
37 IdList *pSrc,
38 Expr *pWhere,
39 ExprList *pGroupBy,
40 Expr *pHaving,
41 ExprList *pOrderBy,
42 int isDistinct
43){
44 Select *pNew;
45 pNew = sqliteMalloc( sizeof(*pNew) );
46 if( pNew==0 ) return 0;
47 pNew->pEList = pEList;
48 pNew->pSrc = pSrc;
49 pNew->pWhere = pWhere;
50 pNew->pGroupBy = pGroupBy;
51 pNew->pHaving = pHaving;
52 pNew->pOrderBy = pOrderBy;
53 pNew->isDistinct = isDistinct;
drh82c3d632000-06-06 21:56:07 +000054 pNew->op = TK_SELECT;
drh9bb61fe2000-06-05 16:01:39 +000055 return pNew;
56}
57
58/*
59** Delete the given Select structure and all of its substructures.
60*/
61void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +000062 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +000063 sqliteExprListDelete(p->pEList);
64 sqliteIdListDelete(p->pSrc);
65 sqliteExprDelete(p->pWhere);
66 sqliteExprListDelete(p->pGroupBy);
67 sqliteExprDelete(p->pHaving);
68 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +000069 sqliteSelectDelete(p->pPrior);
drh9bb61fe2000-06-05 16:01:39 +000070 sqliteFree(p);
71}
72
73/*
drh22827922000-06-06 17:27:05 +000074** Delete the aggregate information from the parse structure.
75*/
76void sqliteParseInfoReset(Parse *pParse){
77 sqliteFree(pParse->aAgg);
78 pParse->aAgg = 0;
79 pParse->nAgg = 0;
80 pParse->iAggCount = -1;
81 pParse->useAgg = 0;
82}
83
84/*
85** This routine generates the code for the inside of the inner loop
86** of a SELECT.
drh82c3d632000-06-06 21:56:07 +000087**
88** The pEList is used to determine the values for each column in the
89** result row. Except if pEList==NULL, then we just read nField
90** elements from the srcTab table.
drh22827922000-06-06 17:27:05 +000091*/
92static int selectInnerLoop(
93 Parse *pParse, /* The parser context */
94 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +000095 int srcTab, /* Pull data from this table */
96 int nField, /* Number of fields in the source table */
drh22827922000-06-06 17:27:05 +000097 ExprList *pOrderBy, /* If not NULL, sort results using this key */
98 int distinct, /* If >=0, make sure results are distinct */
99 int eDest, /* How to dispose of the results */
100 int iParm, /* An argument to the disposal method */
101 int iContinue, /* Jump here to continue with next row */
102 int iBreak /* Jump here to break out of the inner loop */
103){
104 Vdbe *v = pParse->pVdbe;
105 int i;
106
107 /* Pull the requested fields.
108 */
drh82c3d632000-06-06 21:56:07 +0000109 if( pEList ){
110 for(i=0; i<pEList->nExpr; i++){
111 sqliteExprCode(pParse, pEList->a[i].pExpr);
112 }
113 nField = pEList->nExpr;
114 }else{
115 for(i=0; i<nField; i++){
116 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
117 }
drh22827922000-06-06 17:27:05 +0000118 }
119
120 /* If the current result is not distinct, skip the rest
121 ** of the processing for the current row.
122 */
123 if( distinct>=0 ){
124 int lbl = sqliteVdbeMakeLabel(v);
125 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0);
126 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0);
127 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0);
128 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0);
129 sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl);
130 sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0);
131 }
drh82c3d632000-06-06 21:56:07 +0000132
drh22827922000-06-06 17:27:05 +0000133 /* If there is an ORDER BY clause, then store the results
134 ** in a sorter.
135 */
136 if( pOrderBy ){
137 char *zSortOrder;
138 sqliteVdbeAddOp(v, OP_SortMakeRec, pEList->nExpr, 0, 0, 0);
139 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
140 if( zSortOrder==0 ) return 1;
141 for(i=0; i<pOrderBy->nExpr; i++){
142 zSortOrder[i] = pOrderBy->a[i].idx ? '-' : '+';
143 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
144 }
145 zSortOrder[pOrderBy->nExpr] = 0;
146 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0);
147 sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0);
148 }else
149
drh82c3d632000-06-06 21:56:07 +0000150 /* In this mode, write each query result to the key of the temporary
151 ** table iParm.
drh22827922000-06-06 17:27:05 +0000152 */
drh82c3d632000-06-06 21:56:07 +0000153 if( eDest==SRT_Union ){
154 sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0);
155 sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0);
156 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
157 }else
158
159 /* Construct a record from the query result, but instead of
160 ** saving that record, use it as a key to delete elements from
161 ** the temporary table iParm.
162 */
163 if( eDest==SRT_Except ){
164 assert( pEList->nExpr==1 );
165 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
drh22827922000-06-06 17:27:05 +0000166 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
167 }else
168
169 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
170 ** then there should be a single item on the stack. Write this
171 ** item into the set table with bogus data.
172 */
173 if( eDest==SRT_Set ){
174 assert( pEList->nExpr==1 );
175 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
176 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
177 }else
178
drh82c3d632000-06-06 21:56:07 +0000179
drh22827922000-06-06 17:27:05 +0000180 /* If this is a scalar select that is part of an expression, then
181 ** store the results in the appropriate memory cell and break out
182 ** of the scan loop.
183 */
184 if( eDest==SRT_Mem ){
185 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
186 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0);
187 }else
188
189 /* If none of the above, send the data to the callback function.
190 */
191 {
drh82c3d632000-06-06 21:56:07 +0000192 sqliteVdbeAddOp(v, OP_Callback, nField, 0, 0, 0);
193 }
194 return 0;
195}
196
197/*
198** Generate code that will tell the VDBE how many columns there
199** are in the result and the name for each column. This information
200** is used to provide "argc" and "azCol[]" values in the callback.
201*/
202static void generateColumnNames(Vdbe *v, IdList *pTabList, ExprList *pEList){
203 int i;
204 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0);
205 for(i=0; i<pEList->nExpr; i++){
206 Expr *p;
207 if( pEList->a[i].zName ){
208 char *zName = pEList->a[i].zName;
209 int addr = sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
210 if( zName[0]=='\'' || zName[0]=='"' ){
211 sqliteVdbeDequoteP3(v, addr);
212 }
213 continue;
214 }
215 p = pEList->a[i].pExpr;
216 if( p->op!=TK_FIELD || pTabList==0 ){
217 char zName[30];
218 sprintf(zName, "field%d", i+1);
219 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
220 }else{
221 if( pTabList->nId>1 ){
222 char *zName = 0;
223 Table *pTab = pTabList->a[p->iTable].pTab;
224 char *zTab;
225
226 zTab = pTabList->a[p->iTable].zAlias;
227 if( zTab==0 ) zTab = pTab->zName;
228 sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iField].zName, 0);
229 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
230 sqliteFree(zName);
231 }else{
232 Table *pTab = pTabList->a[0].pTab;
233 char *zName = pTab->aCol[p->iField].zName;
234 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
235 }
236 }
237 }
238}
239
240/*
241** This routine is called to process a query that is really the union
242** or intersection of two or more separate queries.
243*/
244static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
245 int rc;
246 Select *pPrior;
247 Vdbe *v;
248 int i;
249
250 /* Make sure we have a valid query engine. If not, create a new one.
251 */
252 v = pParse->pVdbe;
253 if( v==0 ){
254 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
255 }
256 if( v==0 ){
257 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
258 pParse->nErr++;
259 return 1;
260 }
261
262 assert( p->pPrior!=0 );
263 pPrior = p->pPrior;
264 switch( p->op ){
265 case TK_ALL: {
266 rc = sqliteSelect(pParse, pPrior, eDest, iParm);
267 if( rc ) return rc;
268 p->pPrior = 0;
269 rc = sqliteSelect(pParse, p, eDest, iParm);
270 p->pPrior = pPrior;
271 break;
272 }
273 case TK_EXCEPT:
274 case TK_UNION: {
275 int unionTab;
276 int op;
277
278 if( eDest==SRT_Union ){
279 unionTab = iParm;
280 }else{
281 unionTab = pParse->nTab++;
282 sqliteVdbeAddOp(v, OP_Open, unionTab, 1, 0, 0);
283 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0);
284 }
285 rc = sqliteSelect(pParse, pPrior, SRT_Union, unionTab);
286 if( rc ) return rc;
287 op = p->op==TK_EXCEPT ? SRT_Except : SRT_Union;
288 p->pPrior = 0;
289 rc = sqliteSelect(pParse, p, op, unionTab);
290 p->pPrior = pPrior;
291 if( rc ) return rc;
292 if( eDest!=SRT_Union ){
293 int iCont, iBreak;
294 assert( p->pEList );
295 generateColumnNames(v, 0, p->pEList);
296 iBreak = sqliteVdbeMakeLabel(v);
297 iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0);
298 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
299 0, -1, eDest, iParm,
300 iCont, iBreak);
301 if( rc ) return 1;
302 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
303 sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak);
304 }
305 break;
306 }
307 case TK_INTERSECT: {
308 int tab1, tab2;
309 Select *pPrior;
310 int iCont, iBreak;
311
312 tab1 = pParse->nTab++;
313 tab2 = pParse->nTab++;
314 sqliteVdbeAddOp(v, OP_Open, tab1, 1, 0, 0);
315 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0);
316 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
317 if( rc ) return rc;
318 sqliteVdbeAddOp(v, OP_Open, tab2, 1, 0, 0);
319 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0);
320 p->pPrior = 0;
321 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
322 p->pPrior = pPrior;
323 if( rc ) return rc;
324 assert( p->pEList );
325 generateColumnNames(v, 0, p->pEList);
326 iBreak = sqliteVdbeMakeLabel(v);
327 iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0);
328 sqliteVdbeAddOp(v, OP_Key, tab1, 0, 0, 0);
329 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0);
330 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
331 0, -1, eDest, iParm,
332 iCont, iBreak);
333 if( rc ) return 1;
334 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
335 sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak);
336 sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0);
337 break;
338 }
339 }
340 assert( p->pEList && pPrior->pEList );
341 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
342 sqliteSetString(&pParse->zErrMsg, "SELECTs have different numbers "
343 "of columns and therefore cannot be joined", 0);
344 pParse->nErr++;
345 return 1;
drh22827922000-06-06 17:27:05 +0000346 }
347 return 0;
348}
349
350/*
drh9bb61fe2000-06-05 16:01:39 +0000351** Generate code for the given SELECT statement.
352**
drhfef52082000-06-06 01:50:43 +0000353** The results are distributed in various ways depending on the
354** value of eDest and iParm.
355**
356** eDest Value Result
357** ------------ -------------------------------------------
358** SRT_Callback Invoke the callback for each row of the result.
359**
360** SRT_Mem Store first result in memory cell iParm
361**
362** SRT_Set Store results as keys of a table with cursor iParm
363**
drh82c3d632000-06-06 21:56:07 +0000364** SRT_Union Store results as a key in a temporary table iParm
365**
366** SRT_Except Remove results form the temporary talbe iParm.
drh9bb61fe2000-06-05 16:01:39 +0000367**
368** This routine returns the number of errors. If any errors are
369** encountered, then an appropriate error message is left in
370** pParse->zErrMsg.
371**
372** This routine does NOT free the Select structure passed in. The
373** calling function needs to do that.
374*/
375int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000376 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000377 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000378 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000379 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000380){
381 int i, j;
382 WhereInfo *pWInfo;
383 Vdbe *v;
384 int isAgg = 0; /* True for select lists like "count(*)" */
drh9bb61fe2000-06-05 16:01:39 +0000385 ExprList *pEList; /* List of fields to extract. NULL means "*" */
386 IdList *pTabList; /* List of tables to select from */
387 Expr *pWhere; /* The WHERE clause. May be NULL */
388 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000389 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
390 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000391 int isDistinct; /* True if the DISTINCT keyword is present */
392 int distinct; /* Table to use for the distinct set */
drh9bb61fe2000-06-05 16:01:39 +0000393
drh82c3d632000-06-06 21:56:07 +0000394 /* If there is are a sequence of queries, do the earlier ones first.
395 */
396 if( p->pPrior ){
397 return multiSelect(pParse, p, eDest, iParm);
398 }
399
400 /* Make local copies of the parameters for this query.
401 */
drh9bb61fe2000-06-05 16:01:39 +0000402 pEList = p->pEList;
403 pTabList = p->pSrc;
404 pWhere = p->pWhere;
405 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000406 pGroupBy = p->pGroupBy;
407 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000408 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000409
410 /*
411 ** Do not even attempt to generate any code if we have already seen
412 ** errors before this routine starts.
413 */
414 if( pParse->nErr>0 ) return 0;
drh22827922000-06-06 17:27:05 +0000415 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000416
417 /* Look up every table in the table list.
418 */
419 for(i=0; i<pTabList->nId; i++){
420 pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
421 if( pTabList->a[i].pTab==0 ){
422 sqliteSetString(&pParse->zErrMsg, "no such table: ",
423 pTabList->a[i].zName, 0);
424 pParse->nErr++;
drh9bb61fe2000-06-05 16:01:39 +0000425 return 1;
drhcce7d172000-05-31 15:34:51 +0000426 }
427 }
428
drh19a775c2000-06-05 18:54:46 +0000429 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000430 ** necessary. This must be done early to allocate the cursor before
431 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000432 */
433 if( isDistinct ){
434 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000435 }else{
436 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000437 }
438
drhcce7d172000-05-31 15:34:51 +0000439 /* If the list of fields to retrieve is "*" then replace it with
440 ** a list of all fields from all tables.
441 */
442 if( pEList==0 ){
443 for(i=0; i<pTabList->nId; i++){
444 Table *pTab = pTabList->a[i].pTab;
445 for(j=0; j<pTab->nCol; j++){
446 Expr *pExpr = sqliteExpr(TK_FIELD, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000447 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000448 pExpr->iField = j;
drh82c3d632000-06-06 21:56:07 +0000449 p->pEList = pEList = sqliteExprListAppend(pEList, pExpr, 0);
drhcce7d172000-05-31 15:34:51 +0000450 }
451 }
452 }
453
drh22827922000-06-06 17:27:05 +0000454 /* If writing to memory or generating a set
455 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000456 */
drhfef52082000-06-06 01:50:43 +0000457 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000458 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
459 "a SELECT that is part of an expression", 0);
460 pParse->nErr++;
461 return 1;
462 }
463
drh22827922000-06-06 17:27:05 +0000464 /* ORDER BY is ignored if we are not sending the result to a callback.
465 */
466 if( eDest!=SRT_Callback ){
467 pOrderBy = 0;
468 }
469
470 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000471 */
472 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000473 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
474 }
475 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
476 if( pOrderBy ){
477 for(i=0; i<pOrderBy->nExpr; i++){
478 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
479 }
480 }
drh22827922000-06-06 17:27:05 +0000481 if( pGroupBy ){
482 for(i=0; i<pGroupBy->nExpr; i++){
483 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
484 }
485 }
486 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
487
488 /* Resolve the field names and do a semantics check on all the expressions.
489 */
drh4794b982000-06-06 13:54:14 +0000490 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000491 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000492 return 1;
drhcce7d172000-05-31 15:34:51 +0000493 }
drh22827922000-06-06 17:27:05 +0000494 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000495 return 1;
drhcce7d172000-05-31 15:34:51 +0000496 }
497 }
drhcce7d172000-05-31 15:34:51 +0000498 if( pWhere ){
499 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000500 return 1;
drhcce7d172000-05-31 15:34:51 +0000501 }
502 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000503 return 1;
drhcce7d172000-05-31 15:34:51 +0000504 }
505 }
506 if( pOrderBy ){
507 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000508 Expr *pE = pOrderBy->a[i].pExpr;
509 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000510 return 1;
drhcce7d172000-05-31 15:34:51 +0000511 }
drh22827922000-06-06 17:27:05 +0000512 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000513 return 1;
drhcce7d172000-05-31 15:34:51 +0000514 }
515 }
516 }
drh22827922000-06-06 17:27:05 +0000517 if( pGroupBy ){
518 for(i=0; i<pGroupBy->nExpr; i++){
519 Expr *pE = pGroupBy->a[i].pExpr;
520 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
521 return 1;
522 }
523 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
524 return 1;
525 }
526 }
527 }
528 if( pHaving ){
529 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000530 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
531 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000532 pParse->nErr++;
533 return 1;
534 }
535 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
536 return 1;
537 }
drhda932812000-06-06 18:00:15 +0000538 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000539 return 1;
540 }
drhcce7d172000-05-31 15:34:51 +0000541 }
542
drh22827922000-06-06 17:27:05 +0000543 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000544 */
drh22827922000-06-06 17:27:05 +0000545 if( isAgg ){
546 for(i=0; i<pEList->nExpr; i++){
547 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
548 return 1;
549 }
550 }
551 if( pGroupBy ){
552 for(i=0; i<pGroupBy->nExpr; i++){
553 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
554 return 1;
555 }
556 }
557 }
558 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
559 return 1;
560 }
drhefb72512000-05-31 20:00:52 +0000561 }
562
drhcce7d172000-05-31 15:34:51 +0000563 /* Begin generating code.
564 */
565 v = pParse->pVdbe;
566 if( v==0 ){
567 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
568 }
drh9bb61fe2000-06-05 16:01:39 +0000569 if( v==0 ){
570 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
571 pParse->nErr++;
572 return 1;
573 }
drhcce7d172000-05-31 15:34:51 +0000574 if( pOrderBy ){
575 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
576 }
577
drh22827922000-06-06 17:27:05 +0000578 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000579 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000580 */
drhfef52082000-06-06 01:50:43 +0000581 if( eDest==SRT_Callback ){
drh82c3d632000-06-06 21:56:07 +0000582 generateColumnNames(v, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000583 }
584
drh22827922000-06-06 17:27:05 +0000585 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000586 */
587 if( isAgg ){
drh22827922000-06-06 17:27:05 +0000588 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000589 }
590
drh19a775c2000-06-05 18:54:46 +0000591 /* Initialize the memory cell to NULL
592 */
drhfef52082000-06-06 01:50:43 +0000593 if( eDest==SRT_Mem ){
drh19a775c2000-06-05 18:54:46 +0000594 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000595 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000596 }
597
drhcce7d172000-05-31 15:34:51 +0000598 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000599 */
drh19a775c2000-06-05 18:54:46 +0000600 if( isDistinct ){
drheec553b2000-06-02 01:51:20 +0000601 sqliteVdbeAddOp(v, OP_Open, distinct, 1, 0, 0);
drhefb72512000-05-31 20:00:52 +0000602 }
drhcce7d172000-05-31 15:34:51 +0000603 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000604 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000605
drh22827922000-06-06 17:27:05 +0000606 /* Use the standard inner loop if we are not dealing with
607 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000608 */
drhda9d6c42000-05-31 18:20:14 +0000609 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000610 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000611 pWInfo->iContinue, pWInfo->iBreak) ){
612 return 1;
drhda9d6c42000-05-31 18:20:14 +0000613 }
drhcce7d172000-05-31 15:34:51 +0000614 }
drhefb72512000-05-31 20:00:52 +0000615
drh22827922000-06-06 17:27:05 +0000616 /* If we are dealing with aggregates, then to the special aggregate
617 ** processing.
drhefb72512000-05-31 20:00:52 +0000618 */
drh22827922000-06-06 17:27:05 +0000619 else{
620 int doFocus;
621 if( pGroupBy ){
622 for(i=0; i<pGroupBy->nExpr; i++){
623 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
624 }
625 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0);
626 doFocus = 1;
627 }else{
628 doFocus = 0;
629 for(i=0; i<pParse->nAgg; i++){
630 if( !pParse->aAgg[i].isAgg ){
631 doFocus = 1;
632 break;
633 }
634 }
635 if( doFocus ){
636 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
637 }
drhcce7d172000-05-31 15:34:51 +0000638 }
drh22827922000-06-06 17:27:05 +0000639 if( doFocus ){
640 int lbl1 = sqliteVdbeMakeLabel(v);
641 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0);
642 for(i=0; i<pParse->nAgg; i++){
643 if( pParse->aAgg[i].isAgg ) continue;
644 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
645 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000646 }
drh22827922000-06-06 17:27:05 +0000647 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000648 }
drh22827922000-06-06 17:27:05 +0000649 for(i=0; i<pParse->nAgg; i++){
650 Expr *pE;
651 int op;
652 if( !pParse->aAgg[i].isAgg ) continue;
653 pE = pParse->aAgg[i].pExpr;
654 if( pE==0 ){
655 sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0);
656 continue;
657 }
658 assert( pE->op==TK_AGG_FUNCTION );
659 assert( pE->pList!=0 && pE->pList->nExpr==1 );
660 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
661 sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0);
662 switch( pE->iField ){
663 case FN_Min: op = OP_Min; break;
664 case FN_Max: op = OP_Max; break;
665 case FN_Avg: op = OP_Add; break;
666 case FN_Sum: op = OP_Add; break;
667 }
668 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
669 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
670 }
drhcce7d172000-05-31 15:34:51 +0000671 }
672
drh22827922000-06-06 17:27:05 +0000673
drhcce7d172000-05-31 15:34:51 +0000674 /* End the database scan loop.
675 */
676 sqliteWhereEnd(pWInfo);
677
drh22827922000-06-06 17:27:05 +0000678 /* If we are processing aggregates, we need to set up a second loop
679 ** over all of the aggregate values and process them.
680 */
681 if( isAgg ){
682 int endagg = sqliteVdbeMakeLabel(v);
683 int startagg;
684 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0);
685 pParse->useAgg = 1;
686 if( pHaving ){
687 sqliteExprIfFalse(pParse, pHaving, startagg);
688 }
drh82c3d632000-06-06 21:56:07 +0000689 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000690 startagg, endagg) ){
691 return 1;
692 }
693 sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0);
694 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg);
695 pParse->useAgg = 0;
696 }
697
drhcce7d172000-05-31 15:34:51 +0000698 /* If there is an ORDER BY clause, then we need to sort the results
699 ** and send them to the callback one by one.
700 */
701 if( pOrderBy ){
702 int end = sqliteVdbeMakeLabel(v);
703 int addr;
704 sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0);
705 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0);
706 sqliteVdbeAddOp(v, OP_SortCallback, pEList->nExpr, 0, 0, 0);
707 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000708 sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end);
drhcce7d172000-05-31 15:34:51 +0000709 }
drh9bb61fe2000-06-05 16:01:39 +0000710 return 0;
drhcce7d172000-05-31 15:34:51 +0000711}