blob: c3abbe4e53977acb69d9e57c70f80797637e3d78 [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**
drhc4a3c772001-04-04 11:48:57 +000027** $Id: select.c,v 1.30 2001/04/04 11:48:58 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
drh967e8b72000-06-21 13:59:10 +000089** result row. Except if pEList==NULL, then we just read nColumn
drh82c3d632000-06-06 21:56:07 +000090** 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 */
drh967e8b72000-06-21 13:59:10 +000096 int nColumn, /* Number of columns 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
drh967e8b72000-06-21 13:59:10 +0000107 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000108 */
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 }
drh967e8b72000-06-21 13:59:10 +0000113 nColumn = pEList->nExpr;
drh82c3d632000-06-06 21:56:07 +0000114 }else{
drh967e8b72000-06-21 13:59:10 +0000115 for(i=0; i<nColumn; i++){
drh82c3d632000-06-06 21:56:07 +0000116 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;
drh967e8b72000-06-21 13:59:10 +0000138 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000139 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
140 if( zSortOrder==0 ) return 1;
141 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000142 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000143 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
144 }
145 zSortOrder[pOrderBy->nExpr] = 0;
146 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0);
drh6e142f52000-06-08 13:36:40 +0000147 sqliteFree(zSortOrder);
drh22827922000-06-06 17:27:05 +0000148 sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0);
149 }else
150
drh82c3d632000-06-06 21:56:07 +0000151 /* In this mode, write each query result to the key of the temporary
152 ** table iParm.
drh22827922000-06-06 17:27:05 +0000153 */
drh82c3d632000-06-06 21:56:07 +0000154 if( eDest==SRT_Union ){
drh967e8b72000-06-21 13:59:10 +0000155 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000156 sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0);
157 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
158 }else
159
drh5974a302000-06-07 14:42:26 +0000160 /* Store the result as data using a unique key.
161 */
162 if( eDest==SRT_Table ){
drh967e8b72000-06-21 13:59:10 +0000163 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000164 sqliteVdbeAddOp(v, OP_New, iParm, 0, 0, 0);
165 sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0);
166 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
167 }else
168
drh82c3d632000-06-06 21:56:07 +0000169 /* Construct a record from the query result, but instead of
170 ** saving that record, use it as a key to delete elements from
171 ** the temporary table iParm.
172 */
173 if( eDest==SRT_Except ){
drh967e8b72000-06-21 13:59:10 +0000174 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0);
drh1ecec3c2000-06-06 22:13:55 +0000175 sqliteVdbeAddOp(v, OP_Delete, iParm, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000176 }else
177
178 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
179 ** then there should be a single item on the stack. Write this
180 ** item into the set table with bogus data.
181 */
182 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000183 assert( nColumn==1 );
drh22827922000-06-06 17:27:05 +0000184 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
185 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
186 }else
187
drh82c3d632000-06-06 21:56:07 +0000188
drh22827922000-06-06 17:27:05 +0000189 /* If this is a scalar select that is part of an expression, then
190 ** store the results in the appropriate memory cell and break out
191 ** of the scan loop.
192 */
193 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000194 assert( nColumn==1 );
drh22827922000-06-06 17:27:05 +0000195 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
196 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0);
197 }else
198
199 /* If none of the above, send the data to the callback function.
200 */
201 {
drh967e8b72000-06-21 13:59:10 +0000202 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000203 }
204 return 0;
205}
206
207/*
drhd8bc7082000-06-07 23:51:50 +0000208** If the inner loop was generated using a non-null pOrderBy argument,
209** then the results were placed in a sorter. After the loop is terminated
210** we need to run the sorter and output the results. The following
211** routine generates the code needed to do that.
212*/
drh967e8b72000-06-21 13:59:10 +0000213static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000214 int end = sqliteVdbeMakeLabel(v);
215 int addr;
216 sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0);
217 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000218 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000219 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
220 sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end);
221}
222
223/*
drh82c3d632000-06-06 21:56:07 +0000224** Generate code that will tell the VDBE how many columns there
225** are in the result and the name for each column. This information
226** is used to provide "argc" and "azCol[]" values in the callback.
227*/
drhd8bc7082000-06-07 23:51:50 +0000228static
229void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
230 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000231 int i;
drhd8bc7082000-06-07 23:51:50 +0000232 if( pParse->colNamesSet ) return;
233 pParse->colNamesSet = 1;
drh82c3d632000-06-06 21:56:07 +0000234 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0);
235 for(i=0; i<pEList->nExpr; i++){
236 Expr *p;
drhe1b6a5b2000-07-29 13:06:59 +0000237 int addr;
drh82c3d632000-06-06 21:56:07 +0000238 if( pEList->a[i].zName ){
239 char *zName = pEList->a[i].zName;
drhd8bc7082000-06-07 23:51:50 +0000240 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
drh82c3d632000-06-06 21:56:07 +0000241 continue;
242 }
243 p = pEList->a[i].pExpr;
drhe1b6a5b2000-07-29 13:06:59 +0000244 if( p->span.z && p->span.z[0] ){
245 addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0, 0, 0);
246 sqliteVdbeChangeP3(v, addr, p->span.z, p->span.n);
247 sqliteVdbeCompressSpace(v, addr);
248 }else if( p->op!=TK_COLUMN || pTabList==0 ){
drh82c3d632000-06-06 21:56:07 +0000249 char zName[30];
drh5974a302000-06-07 14:42:26 +0000250 sprintf(zName, "column%d", i+1);
drh82c3d632000-06-06 21:56:07 +0000251 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
252 }else{
253 if( pTabList->nId>1 ){
254 char *zName = 0;
255 Table *pTab = pTabList->a[p->iTable].pTab;
256 char *zTab;
257
258 zTab = pTabList->a[p->iTable].zAlias;
259 if( zTab==0 ) zTab = pTab->zName;
drh967e8b72000-06-21 13:59:10 +0000260 sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iColumn].zName, 0);
drh82c3d632000-06-06 21:56:07 +0000261 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
262 sqliteFree(zName);
263 }else{
264 Table *pTab = pTabList->a[0].pTab;
drh967e8b72000-06-21 13:59:10 +0000265 char *zName = pTab->aCol[p->iColumn].zName;
drh82c3d632000-06-06 21:56:07 +0000266 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
267 }
268 }
269 }
270}
271
272/*
drhd8bc7082000-06-07 23:51:50 +0000273** Name of the connection operator, used for error messages.
274*/
275static const char *selectOpName(int id){
276 char *z;
277 switch( id ){
278 case TK_ALL: z = "UNION ALL"; break;
279 case TK_INTERSECT: z = "INTERSECT"; break;
280 case TK_EXCEPT: z = "EXCEPT"; break;
281 default: z = "UNION"; break;
282 }
283 return z;
284}
285
286/*
287** For the given SELECT statement, do two things.
288**
drh967e8b72000-06-21 13:59:10 +0000289** (1) Fill in the pTabList->a[].pTab fields in the IdList that
290** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000291**
292** (2) If the columns to be extracted variable (pEList) is NULL
293** (meaning that a "*" was used in the SQL statement) then
294** create a fake pEList containing the names of all columns
295** of all tables.
296**
297** Return 0 on success. If there are problems, leave an error message
298** in pParse and return non-zero.
299*/
300static int fillInColumnList(Parse *pParse, Select *p){
301 int i, j;
302 IdList *pTabList = p->pSrc;
303 ExprList *pEList = p->pEList;
304
305 /* Look up every table in the table list.
306 */
307 for(i=0; i<pTabList->nId; i++){
308 if( pTabList->a[i].pTab ){
309 /* This routine has run before! No need to continue */
310 return 0;
311 }
312 pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
313 if( pTabList->a[i].pTab==0 ){
314 sqliteSetString(&pParse->zErrMsg, "no such table: ",
315 pTabList->a[i].zName, 0);
316 pParse->nErr++;
317 return 1;
318 }
319 }
320
321 /* If the list of columns to retrieve is "*" then replace it with
322 ** a list of all columns from all tables.
323 */
324 if( pEList==0 ){
325 for(i=0; i<pTabList->nId; i++){
326 Table *pTab = pTabList->a[i].pTab;
327 for(j=0; j<pTab->nCol; j++){
328 Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0);
329 pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0);
330 pExpr->pLeft->token.z = pTab->zName;
331 pExpr->pLeft->token.n = strlen(pTab->zName);
332 pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0);
333 pExpr->pRight->token.z = pTab->aCol[j].zName;
334 pExpr->pRight->token.n = strlen(pTab->aCol[j].zName);
drhe1b6a5b2000-07-29 13:06:59 +0000335 pExpr->span.z = "";
336 pExpr->span.n = 0;
drhd8bc7082000-06-07 23:51:50 +0000337 pEList = sqliteExprListAppend(pEList, pExpr, 0);
338 }
339 }
340 p->pEList = pEList;
341 }
342 return 0;
343}
344
345/*
346** This routine associates entries in an ORDER BY expression list with
347** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000348** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000349** the top-level node is filled in with column number and the iTable
350** value of the top-level node is filled with iTable parameter.
351**
352** If there are prior SELECT clauses, they are processed first. A match
353** in an earlier SELECT takes precedence over a later SELECT.
354**
355** Any entry that does not match is flagged as an error. The number
356** of errors is returned.
357*/
358static int matchOrderbyToColumn(
359 Parse *pParse, /* A place to leave error messages */
360 Select *pSelect, /* Match to result columns of this SELECT */
361 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
362 int iTable, /* Insert this this value in iTable */
363 int mustComplete /* If TRUE all ORDER BYs must match */
364){
365 int nErr = 0;
366 int i, j;
367 ExprList *pEList;
368
369 assert( pSelect && pOrderBy );
370 if( mustComplete ){
371 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
372 }
373 if( fillInColumnList(pParse, pSelect) ){
374 return 1;
375 }
376 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000377 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
378 return 1;
379 }
drhd8bc7082000-06-07 23:51:50 +0000380 }
381 pEList = pSelect->pEList;
382 for(i=0; i<pOrderBy->nExpr; i++){
383 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000384 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000385 if( pOrderBy->a[i].done ) continue;
386 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000387 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
388 char *zName = pEList->a[j].zName;
drh6e142f52000-06-08 13:36:40 +0000389 char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000390 sqliteDequote(zLabel);
391 if( sqliteStrICmp(zName, zLabel)==0 ){
392 match = 1;
393 }
drh6e142f52000-06-08 13:36:40 +0000394 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000395 }
drh4cfa7932000-06-08 15:10:46 +0000396 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000397 match = 1;
398 }
399 if( match ){
drh967e8b72000-06-21 13:59:10 +0000400 pE->op = TK_COLUMN;
401 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000402 pE->iTable = iTable;
403 pOrderBy->a[i].done = 1;
404 break;
405 }
406 }
drh92cd52f2000-06-08 01:55:29 +0000407 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000408 char zBuf[30];
409 sprintf(zBuf,"%d",i+1);
410 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
411 " does not match any result column", 0);
412 pParse->nErr++;
413 nErr++;
414 break;
415 }
416 }
417 return nErr;
418}
419
420/*
421** Get a VDBE for the given parser context. Create a new one if necessary.
422** If an error occurs, return NULL and leave a message in pParse.
423*/
424Vdbe *sqliteGetVdbe(Parse *pParse){
425 Vdbe *v = pParse->pVdbe;
426 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000427 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000428 }
429 if( v==0 ){
430 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
431 pParse->nErr++;
432 }
433 return v;
434}
435
436
437/*
drh82c3d632000-06-06 21:56:07 +0000438** This routine is called to process a query that is really the union
439** or intersection of two or more separate queries.
440*/
441static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000442 int rc; /* Success code from a subroutine */
443 Select *pPrior; /* Another SELECT immediately to our left */
444 Vdbe *v; /* Generate code to this VDBE */
445 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000446
drhd8bc7082000-06-07 23:51:50 +0000447 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
448 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000449 */
drhd8bc7082000-06-07 23:51:50 +0000450 assert( p->pPrior!=0 );
451 pPrior = p->pPrior;
452 if( pPrior->pOrderBy ){
453 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
454 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000455 pParse->nErr++;
456 return 1;
457 }
458
drhd8bc7082000-06-07 23:51:50 +0000459 /* Make sure we have a valid query engine. If not, create a new one.
460 */
461 v = sqliteGetVdbe(pParse);
462 if( v==0 ) return 1;
463
464 /* Process the UNION or INTERSECTION
465 */
drh10e5e3c2000-06-08 00:19:02 +0000466 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000467 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000468 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000469 case TK_EXCEPT:
470 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000471 int unionTab; /* Cursor number of the temporary table holding result */
472 int op; /* One of the SRT_ operations to apply to self */
473 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000474
drhd8bc7082000-06-07 23:51:50 +0000475 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
476 if( eDest==priorOp ){
477 /* We can reuse a temporary table generated by a SELECT to our
478 ** right. This also means we are not the right-most select and so
479 ** we cannot have an ORDER BY clause
480 */
drh82c3d632000-06-06 21:56:07 +0000481 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000482 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000483 }else{
drhd8bc7082000-06-07 23:51:50 +0000484 /* We will need to create our own temporary table to hold the
485 ** intermediate results.
486 */
487 unionTab = pParse->nTab++;
488 if( p->pOrderBy
489 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
490 return 1;
491 }
drhd8bc7082000-06-07 23:51:50 +0000492 if( p->op!=TK_ALL ){
drh345fda32001-01-15 22:51:08 +0000493 sqliteVdbeAddOp(v, OP_OpenIdx, unionTab, 1, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000494 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0);
drh345fda32001-01-15 22:51:08 +0000495 }else{
496 sqliteVdbeAddOp(v, OP_OpenTbl, unionTab, 1, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000497 }
drh82c3d632000-06-06 21:56:07 +0000498 }
drhd8bc7082000-06-07 23:51:50 +0000499
500 /* Code the SELECT statements to our left
501 */
502 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000503 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000504
505 /* Code the current SELECT statement
506 */
507 switch( p->op ){
508 case TK_EXCEPT: op = SRT_Except; break;
509 case TK_UNION: op = SRT_Union; break;
510 case TK_ALL: op = SRT_Table; break;
511 }
drh82c3d632000-06-06 21:56:07 +0000512 p->pPrior = 0;
513 rc = sqliteSelect(pParse, p, op, unionTab);
514 p->pPrior = pPrior;
515 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000516
517 /* Convert the data in the temporary table into whatever form
518 ** it is that we currently need.
519 */
520 if( eDest!=priorOp ){
drh82c3d632000-06-06 21:56:07 +0000521 int iCont, iBreak;
522 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000523 generateColumnNames(pParse, 0, p->pEList);
drh92dba242000-06-08 00:28:51 +0000524 if( p->pOrderBy ){
525 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
526 }
drh82c3d632000-06-06 21:56:07 +0000527 iBreak = sqliteVdbeMakeLabel(v);
528 iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0);
529 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000530 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000531 iCont, iBreak);
532 if( rc ) return 1;
533 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
534 sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak);
drhd8bc7082000-06-07 23:51:50 +0000535 if( p->pOrderBy ){
536 generateSortTail(v, p->pEList->nExpr);
537 }
drh82c3d632000-06-06 21:56:07 +0000538 }
539 break;
540 }
541 case TK_INTERSECT: {
542 int tab1, tab2;
drh82c3d632000-06-06 21:56:07 +0000543 int iCont, iBreak;
544
drhd8bc7082000-06-07 23:51:50 +0000545 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000546 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000547 ** by allocating the tables we will need.
548 */
drh82c3d632000-06-06 21:56:07 +0000549 tab1 = pParse->nTab++;
550 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000551 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
552 return 1;
553 }
drh345fda32001-01-15 22:51:08 +0000554 sqliteVdbeAddOp(v, OP_OpenIdx, tab1, 1, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000555 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000556
557 /* Code the SELECTs to our left into temporary table "tab1".
558 */
drh82c3d632000-06-06 21:56:07 +0000559 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
560 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000561
562 /* Code the current SELECT into temporary table "tab2"
563 */
drh345fda32001-01-15 22:51:08 +0000564 sqliteVdbeAddOp(v, OP_OpenIdx, tab2, 1, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000565 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0);
566 p->pPrior = 0;
567 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
568 p->pPrior = pPrior;
569 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000570
571 /* Generate code to take the intersection of the two temporary
572 ** tables.
573 */
drh82c3d632000-06-06 21:56:07 +0000574 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000575 generateColumnNames(pParse, 0, p->pEList);
drh92dba242000-06-08 00:28:51 +0000576 if( p->pOrderBy ){
577 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
578 }
drh82c3d632000-06-06 21:56:07 +0000579 iBreak = sqliteVdbeMakeLabel(v);
580 iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0);
drh573bd272001-02-19 23:23:38 +0000581 sqliteVdbeAddOp(v, OP_FullKey, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000582 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0);
583 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000584 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000585 iCont, iBreak);
586 if( rc ) return 1;
587 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
588 sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak);
589 sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000590 if( p->pOrderBy ){
591 generateSortTail(v, p->pEList->nExpr);
592 }
drh82c3d632000-06-06 21:56:07 +0000593 break;
594 }
595 }
596 assert( p->pEList && pPrior->pEList );
597 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000598 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
599 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000600 pParse->nErr++;
601 return 1;
drh22827922000-06-06 17:27:05 +0000602 }
drh10e5e3c2000-06-08 00:19:02 +0000603 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000604 return 0;
605}
606
607/*
drh9bb61fe2000-06-05 16:01:39 +0000608** Generate code for the given SELECT statement.
609**
drhfef52082000-06-06 01:50:43 +0000610** The results are distributed in various ways depending on the
611** value of eDest and iParm.
612**
613** eDest Value Result
614** ------------ -------------------------------------------
615** SRT_Callback Invoke the callback for each row of the result.
616**
617** SRT_Mem Store first result in memory cell iParm
618**
619** SRT_Set Store results as keys of a table with cursor iParm
620**
drh82c3d632000-06-06 21:56:07 +0000621** SRT_Union Store results as a key in a temporary table iParm
622**
drhc4a3c772001-04-04 11:48:57 +0000623** SRT_Except Remove results form the temporary table iParm.
624**
625** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000626**
627** This routine returns the number of errors. If any errors are
628** encountered, then an appropriate error message is left in
629** pParse->zErrMsg.
630**
631** This routine does NOT free the Select structure passed in. The
632** calling function needs to do that.
633*/
634int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000635 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000636 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000637 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000638 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000639){
drhd8bc7082000-06-07 23:51:50 +0000640 int i;
drhcce7d172000-05-31 15:34:51 +0000641 WhereInfo *pWInfo;
642 Vdbe *v;
643 int isAgg = 0; /* True for select lists like "count(*)" */
drh967e8b72000-06-21 13:59:10 +0000644 ExprList *pEList; /* List of columns to extract. NULL means "*" */
drh9bb61fe2000-06-05 16:01:39 +0000645 IdList *pTabList; /* List of tables to select from */
646 Expr *pWhere; /* The WHERE clause. May be NULL */
647 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000648 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
649 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000650 int isDistinct; /* True if the DISTINCT keyword is present */
651 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000652 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000653
drh82c3d632000-06-06 21:56:07 +0000654 /* If there is are a sequence of queries, do the earlier ones first.
655 */
656 if( p->pPrior ){
657 return multiSelect(pParse, p, eDest, iParm);
658 }
659
660 /* Make local copies of the parameters for this query.
661 */
drh9bb61fe2000-06-05 16:01:39 +0000662 pTabList = p->pSrc;
663 pWhere = p->pWhere;
664 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000665 pGroupBy = p->pGroupBy;
666 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000667 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000668
drh10e5e3c2000-06-08 00:19:02 +0000669 /* Save the current value of pParse->nTab. Restore this value before
670 ** we exit.
671 */
672 base = pParse->nTab;
673
drh9bb61fe2000-06-05 16:01:39 +0000674 /*
675 ** Do not even attempt to generate any code if we have already seen
676 ** errors before this routine starts.
677 */
drh10e5e3c2000-06-08 00:19:02 +0000678 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000679 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000680
drhd8bc7082000-06-07 23:51:50 +0000681 /* Look up every table in the table list and create an appropriate
682 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000683 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000684 */
drhd8bc7082000-06-07 23:51:50 +0000685 if( fillInColumnList(pParse, p) ){
686 return 1;
drhcce7d172000-05-31 15:34:51 +0000687 }
drhd8bc7082000-06-07 23:51:50 +0000688 pEList = p->pEList;
drhcce7d172000-05-31 15:34:51 +0000689
drh19a775c2000-06-05 18:54:46 +0000690 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000691 ** necessary. This must be done early to allocate the cursor before
692 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000693 */
694 if( isDistinct ){
695 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000696 }else{
697 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000698 }
699
drh22827922000-06-06 17:27:05 +0000700 /* If writing to memory or generating a set
701 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000702 */
drhfef52082000-06-06 01:50:43 +0000703 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000704 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
705 "a SELECT that is part of an expression", 0);
706 pParse->nErr++;
707 return 1;
708 }
709
drh22827922000-06-06 17:27:05 +0000710 /* ORDER BY is ignored if we are not sending the result to a callback.
711 */
712 if( eDest!=SRT_Callback ){
713 pOrderBy = 0;
714 }
715
716 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000717 */
718 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000719 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
720 }
721 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
722 if( pOrderBy ){
723 for(i=0; i<pOrderBy->nExpr; i++){
724 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
725 }
726 }
drh22827922000-06-06 17:27:05 +0000727 if( pGroupBy ){
728 for(i=0; i<pGroupBy->nExpr; i++){
729 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
730 }
731 }
732 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
733
drh10e5e3c2000-06-08 00:19:02 +0000734 /* At this point, we should have allocated all the cursors that we
735 ** need to handle subquerys and temporary tables. From here on we
736 ** are committed to keeping the same value for pParse->nTab.
737 **
drh967e8b72000-06-21 13:59:10 +0000738 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000739 */
drh4794b982000-06-06 13:54:14 +0000740 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000741 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000742 return 1;
drhcce7d172000-05-31 15:34:51 +0000743 }
drh22827922000-06-06 17:27:05 +0000744 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000745 return 1;
drhcce7d172000-05-31 15:34:51 +0000746 }
747 }
drhcce7d172000-05-31 15:34:51 +0000748 if( pWhere ){
749 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000750 return 1;
drhcce7d172000-05-31 15:34:51 +0000751 }
752 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000753 return 1;
drhcce7d172000-05-31 15:34:51 +0000754 }
755 }
756 if( pOrderBy ){
757 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000758 Expr *pE = pOrderBy->a[i].pExpr;
759 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000760 return 1;
drhcce7d172000-05-31 15:34:51 +0000761 }
drh22827922000-06-06 17:27:05 +0000762 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000763 return 1;
drhcce7d172000-05-31 15:34:51 +0000764 }
765 }
766 }
drh22827922000-06-06 17:27:05 +0000767 if( pGroupBy ){
768 for(i=0; i<pGroupBy->nExpr; i++){
769 Expr *pE = pGroupBy->a[i].pExpr;
770 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
771 return 1;
772 }
773 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
774 return 1;
775 }
776 }
777 }
778 if( pHaving ){
779 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000780 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
781 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000782 pParse->nErr++;
783 return 1;
784 }
785 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
786 return 1;
787 }
drhda932812000-06-06 18:00:15 +0000788 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000789 return 1;
790 }
drhcce7d172000-05-31 15:34:51 +0000791 }
792
drh22827922000-06-06 17:27:05 +0000793 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000794 */
drh22827922000-06-06 17:27:05 +0000795 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000796 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000797 for(i=0; i<pEList->nExpr; i++){
798 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
799 return 1;
800 }
801 }
802 if( pGroupBy ){
803 for(i=0; i<pGroupBy->nExpr; i++){
804 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
805 return 1;
806 }
807 }
808 }
809 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
810 return 1;
811 }
drh191b6902000-06-08 11:13:01 +0000812 if( pOrderBy ){
813 for(i=0; i<pOrderBy->nExpr; i++){
814 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
815 return 1;
816 }
817 }
818 }
drhefb72512000-05-31 20:00:52 +0000819 }
820
drhcce7d172000-05-31 15:34:51 +0000821 /* Begin generating code.
822 */
823 v = pParse->pVdbe;
824 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000825 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhcce7d172000-05-31 15:34:51 +0000826 }
drh9bb61fe2000-06-05 16:01:39 +0000827 if( v==0 ){
828 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
829 pParse->nErr++;
830 return 1;
831 }
drhcce7d172000-05-31 15:34:51 +0000832 if( pOrderBy ){
833 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
834 }
835
drh22827922000-06-06 17:27:05 +0000836 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000837 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000838 */
drhfef52082000-06-06 01:50:43 +0000839 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000840 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000841 }
842
drh22827922000-06-06 17:27:05 +0000843 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000844 */
845 if( isAgg ){
drh22827922000-06-06 17:27:05 +0000846 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000847 }
848
drh19a775c2000-06-05 18:54:46 +0000849 /* Initialize the memory cell to NULL
850 */
drhfef52082000-06-06 01:50:43 +0000851 if( eDest==SRT_Mem ){
drh19a775c2000-06-05 18:54:46 +0000852 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000853 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000854 }
855
drhcce7d172000-05-31 15:34:51 +0000856 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000857 */
drh19a775c2000-06-05 18:54:46 +0000858 if( isDistinct ){
drh345fda32001-01-15 22:51:08 +0000859 sqliteVdbeAddOp(v, OP_OpenIdx, distinct, 1, 0, 0);
drhefb72512000-05-31 20:00:52 +0000860 }
drhcce7d172000-05-31 15:34:51 +0000861 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000862 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000863
drh22827922000-06-06 17:27:05 +0000864 /* Use the standard inner loop if we are not dealing with
865 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000866 */
drhda9d6c42000-05-31 18:20:14 +0000867 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000868 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000869 pWInfo->iContinue, pWInfo->iBreak) ){
870 return 1;
drhda9d6c42000-05-31 18:20:14 +0000871 }
drhcce7d172000-05-31 15:34:51 +0000872 }
drhefb72512000-05-31 20:00:52 +0000873
drh22827922000-06-06 17:27:05 +0000874 /* If we are dealing with aggregates, then to the special aggregate
875 ** processing.
drhefb72512000-05-31 20:00:52 +0000876 */
drh22827922000-06-06 17:27:05 +0000877 else{
878 int doFocus;
879 if( pGroupBy ){
880 for(i=0; i<pGroupBy->nExpr; i++){
881 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
882 }
883 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0);
884 doFocus = 1;
885 }else{
886 doFocus = 0;
887 for(i=0; i<pParse->nAgg; i++){
888 if( !pParse->aAgg[i].isAgg ){
889 doFocus = 1;
890 break;
891 }
892 }
893 if( doFocus ){
894 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
895 }
drhcce7d172000-05-31 15:34:51 +0000896 }
drh22827922000-06-06 17:27:05 +0000897 if( doFocus ){
898 int lbl1 = sqliteVdbeMakeLabel(v);
899 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0);
900 for(i=0; i<pParse->nAgg; i++){
901 if( pParse->aAgg[i].isAgg ) continue;
902 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
903 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000904 }
drh22827922000-06-06 17:27:05 +0000905 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000906 }
drh22827922000-06-06 17:27:05 +0000907 for(i=0; i<pParse->nAgg; i++){
908 Expr *pE;
909 int op;
910 if( !pParse->aAgg[i].isAgg ) continue;
911 pE = pParse->aAgg[i].pExpr;
912 if( pE==0 ){
913 sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0);
914 continue;
915 }
916 assert( pE->op==TK_AGG_FUNCTION );
917 assert( pE->pList!=0 && pE->pList->nExpr==1 );
918 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
919 sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000920 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +0000921 case FN_Min: op = OP_Min; break;
922 case FN_Max: op = OP_Max; break;
923 case FN_Avg: op = OP_Add; break;
924 case FN_Sum: op = OP_Add; break;
925 }
926 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
927 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
928 }
drhcce7d172000-05-31 15:34:51 +0000929 }
930
drh22827922000-06-06 17:27:05 +0000931
drhcce7d172000-05-31 15:34:51 +0000932 /* End the database scan loop.
933 */
934 sqliteWhereEnd(pWInfo);
935
drh22827922000-06-06 17:27:05 +0000936 /* If we are processing aggregates, we need to set up a second loop
937 ** over all of the aggregate values and process them.
938 */
939 if( isAgg ){
940 int endagg = sqliteVdbeMakeLabel(v);
941 int startagg;
942 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0);
943 pParse->useAgg = 1;
944 if( pHaving ){
945 sqliteExprIfFalse(pParse, pHaving, startagg);
946 }
drh82c3d632000-06-06 21:56:07 +0000947 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000948 startagg, endagg) ){
949 return 1;
950 }
951 sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0);
952 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg);
953 pParse->useAgg = 0;
954 }
955
drhcce7d172000-05-31 15:34:51 +0000956 /* If there is an ORDER BY clause, then we need to sort the results
957 ** and send them to the callback one by one.
958 */
959 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +0000960 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +0000961 }
drh10e5e3c2000-06-08 00:19:02 +0000962 pParse->nTab = base;
drh9bb61fe2000-06-05 16:01:39 +0000963 return 0;
drhcce7d172000-05-31 15:34:51 +0000964}