blob: 7b9c4794257ea08f90e9cdb5b96bbdd8090c292f [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*************************************************************************
12** This file contains C code routines that are called by the parser
drhb19a2bc2001-09-16 00:13:26 +000013** to handle SELECT statements in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
drhb19a2bc2001-09-16 00:13:26 +000015** $Id: select.c,v 1.37 2001/09/16 00:13:27 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drhcce7d172000-05-31 15:34:51 +000019/*
drh9bb61fe2000-06-05 16:01:39 +000020** Allocate a new Select structure and return a pointer to that
21** structure.
drhcce7d172000-05-31 15:34:51 +000022*/
drh9bb61fe2000-06-05 16:01:39 +000023Select *sqliteSelectNew(
drhdaffd0e2001-04-11 14:28:42 +000024 ExprList *pEList, /* which columns to include in the result */
25 IdList *pSrc, /* the FROM clause -- which tables to scan */
26 Expr *pWhere, /* the WHERE clause */
27 ExprList *pGroupBy, /* the GROUP BY clause */
28 Expr *pHaving, /* the HAVING clause */
29 ExprList *pOrderBy, /* the ORDER BY clause */
30 int isDistinct /* true if the DISTINCT keyword is present */
drh9bb61fe2000-06-05 16:01:39 +000031){
32 Select *pNew;
33 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000034 if( pNew==0 ){
35 sqliteExprListDelete(pEList);
36 sqliteIdListDelete(pSrc);
37 sqliteExprDelete(pWhere);
38 sqliteExprListDelete(pGroupBy);
39 sqliteExprDelete(pHaving);
40 sqliteExprListDelete(pOrderBy);
41 }else{
42 pNew->pEList = pEList;
43 pNew->pSrc = pSrc;
44 pNew->pWhere = pWhere;
45 pNew->pGroupBy = pGroupBy;
46 pNew->pHaving = pHaving;
47 pNew->pOrderBy = pOrderBy;
48 pNew->isDistinct = isDistinct;
49 pNew->op = TK_SELECT;
50 }
drh9bb61fe2000-06-05 16:01:39 +000051 return pNew;
52}
53
54/*
55** Delete the given Select structure and all of its substructures.
56*/
57void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +000058 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +000059 sqliteExprListDelete(p->pEList);
60 sqliteIdListDelete(p->pSrc);
61 sqliteExprDelete(p->pWhere);
62 sqliteExprListDelete(p->pGroupBy);
63 sqliteExprDelete(p->pHaving);
64 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +000065 sqliteSelectDelete(p->pPrior);
drh9bb61fe2000-06-05 16:01:39 +000066 sqliteFree(p);
67}
68
69/*
drh22827922000-06-06 17:27:05 +000070** Delete the aggregate information from the parse structure.
71*/
72void sqliteParseInfoReset(Parse *pParse){
73 sqliteFree(pParse->aAgg);
74 pParse->aAgg = 0;
75 pParse->nAgg = 0;
76 pParse->iAggCount = -1;
77 pParse->useAgg = 0;
78}
79
80/*
81** This routine generates the code for the inside of the inner loop
82** of a SELECT.
drh82c3d632000-06-06 21:56:07 +000083**
84** The pEList is used to determine the values for each column in the
drh967e8b72000-06-21 13:59:10 +000085** result row. Except if pEList==NULL, then we just read nColumn
drh82c3d632000-06-06 21:56:07 +000086** elements from the srcTab table.
drh22827922000-06-06 17:27:05 +000087*/
88static int selectInnerLoop(
89 Parse *pParse, /* The parser context */
90 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +000091 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +000092 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +000093 ExprList *pOrderBy, /* If not NULL, sort results using this key */
94 int distinct, /* If >=0, make sure results are distinct */
95 int eDest, /* How to dispose of the results */
96 int iParm, /* An argument to the disposal method */
97 int iContinue, /* Jump here to continue with next row */
98 int iBreak /* Jump here to break out of the inner loop */
99){
100 Vdbe *v = pParse->pVdbe;
101 int i;
drhdaffd0e2001-04-11 14:28:42 +0000102 if( v==0 ) return 0;
drh22827922000-06-06 17:27:05 +0000103
drh967e8b72000-06-21 13:59:10 +0000104 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000105 */
drh82c3d632000-06-06 21:56:07 +0000106 if( pEList ){
107 for(i=0; i<pEList->nExpr; i++){
108 sqliteExprCode(pParse, pEList->a[i].pExpr);
109 }
drh967e8b72000-06-21 13:59:10 +0000110 nColumn = pEList->nExpr;
drh82c3d632000-06-06 21:56:07 +0000111 }else{
drh967e8b72000-06-21 13:59:10 +0000112 for(i=0; i<nColumn; i++){
drhbe0072d2001-09-13 14:46:09 +0000113 sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000114 }
drh22827922000-06-06 17:27:05 +0000115 }
116
drhdaffd0e2001-04-11 14:28:42 +0000117 /* If the DISTINCT keyword was present on the SELECT statement
118 ** and this row has been seen before, then do not make this row
119 ** part of the result.
drh22827922000-06-06 17:27:05 +0000120 */
121 if( distinct>=0 ){
122 int lbl = sqliteVdbeMakeLabel(v);
123 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0);
124 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0);
125 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0);
126 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0);
127 sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl);
128 sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0);
129 }
drh82c3d632000-06-06 21:56:07 +0000130
drh22827922000-06-06 17:27:05 +0000131 /* If there is an ORDER BY clause, then store the results
132 ** in a sorter.
133 */
134 if( pOrderBy ){
135 char *zSortOrder;
drh967e8b72000-06-21 13:59:10 +0000136 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000137 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
138 if( zSortOrder==0 ) return 1;
139 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000140 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000141 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
142 }
143 zSortOrder[pOrderBy->nExpr] = 0;
144 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0);
drh6e142f52000-06-08 13:36:40 +0000145 sqliteFree(zSortOrder);
drh22827922000-06-06 17:27:05 +0000146 sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0);
147 }else
148
drh82c3d632000-06-06 21:56:07 +0000149 /* In this mode, write each query result to the key of the temporary
150 ** table iParm.
drh22827922000-06-06 17:27:05 +0000151 */
drh82c3d632000-06-06 21:56:07 +0000152 if( eDest==SRT_Union ){
drh967e8b72000-06-21 13:59:10 +0000153 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000154 sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0);
155 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
156 }else
157
drh5974a302000-06-07 14:42:26 +0000158 /* Store the result as data using a unique key.
159 */
160 if( eDest==SRT_Table ){
drh967e8b72000-06-21 13:59:10 +0000161 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0);
drhbe0072d2001-09-13 14:46:09 +0000162 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000163 sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0);
164 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
165 }else
166
drh82c3d632000-06-06 21:56:07 +0000167 /* Construct a record from the query result, but instead of
168 ** saving that record, use it as a key to delete elements from
169 ** the temporary table iParm.
170 */
171 if( eDest==SRT_Except ){
drh3fc190c2001-09-14 03:24:23 +0000172 int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0);
173 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3, 0, 0);
drh1ecec3c2000-06-06 22:13:55 +0000174 sqliteVdbeAddOp(v, OP_Delete, iParm, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000175 }else
176
177 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
178 ** then there should be a single item on the stack. Write this
179 ** item into the set table with bogus data.
180 */
181 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000182 assert( nColumn==1 );
drh22827922000-06-06 17:27:05 +0000183 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
184 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
185 }else
186
drh82c3d632000-06-06 21:56:07 +0000187
drh22827922000-06-06 17:27:05 +0000188 /* If this is a scalar select that is part of an expression, then
189 ** store the results in the appropriate memory cell and break out
190 ** of the scan loop.
191 */
192 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000193 assert( nColumn==1 );
drh22827922000-06-06 17:27:05 +0000194 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
195 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0);
196 }else
197
198 /* If none of the above, send the data to the callback function.
199 */
200 {
drh967e8b72000-06-21 13:59:10 +0000201 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000202 }
203 return 0;
204}
205
206/*
drhd8bc7082000-06-07 23:51:50 +0000207** If the inner loop was generated using a non-null pOrderBy argument,
208** then the results were placed in a sorter. After the loop is terminated
209** we need to run the sorter and output the results. The following
210** routine generates the code needed to do that.
211*/
drh967e8b72000-06-21 13:59:10 +0000212static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000213 int end = sqliteVdbeMakeLabel(v);
214 int addr;
215 sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0);
216 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000217 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000218 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
219 sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end);
220}
221
222/*
drh82c3d632000-06-06 21:56:07 +0000223** Generate code that will tell the VDBE how many columns there
224** are in the result and the name for each column. This information
225** is used to provide "argc" and "azCol[]" values in the callback.
226*/
drhd8bc7082000-06-07 23:51:50 +0000227static
228void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
229 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000230 int i;
drhdaffd0e2001-04-11 14:28:42 +0000231 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000232 pParse->colNamesSet = 1;
drh82c3d632000-06-06 21:56:07 +0000233 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0);
234 for(i=0; i<pEList->nExpr; i++){
235 Expr *p;
drhe1b6a5b2000-07-29 13:06:59 +0000236 int addr;
drh82c3d632000-06-06 21:56:07 +0000237 if( pEList->a[i].zName ){
238 char *zName = pEList->a[i].zName;
drhd8bc7082000-06-07 23:51:50 +0000239 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
drh82c3d632000-06-06 21:56:07 +0000240 continue;
241 }
242 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000243 if( p==0 ) continue;
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;
drhdaffd0e2001-04-11 14:28:42 +0000302 IdList *pTabList;
303 ExprList *pEList;
304
305 if( p==0 || p->pSrc==0 ) return 1;
306 pTabList = p->pSrc;
307 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000308
309 /* Look up every table in the table list.
310 */
311 for(i=0; i<pTabList->nId; i++){
312 if( pTabList->a[i].pTab ){
313 /* This routine has run before! No need to continue */
314 return 0;
315 }
drhdaffd0e2001-04-11 14:28:42 +0000316 if( pTabList->a[i].zName==0 ){
317 /* No table name is given. Instead, there is a (SELECT ...) statement
318 ** the results of which should be used in place of the table. The
319 ** was this is implemented is that the (SELECT ...) writes its results
320 ** into a temporary table which is then scanned like any other table.
321 */
322 sqliteSetString(&pParse->zErrMsg,
323 "(SELECT...) in a FROM clause is not yet implemented.", 0);
324 pParse->nErr++;
325 return 1;
326 }
drhd8bc7082000-06-07 23:51:50 +0000327 pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
328 if( pTabList->a[i].pTab==0 ){
329 sqliteSetString(&pParse->zErrMsg, "no such table: ",
330 pTabList->a[i].zName, 0);
331 pParse->nErr++;
332 return 1;
333 }
334 }
335
336 /* If the list of columns to retrieve is "*" then replace it with
337 ** a list of all columns from all tables.
338 */
339 if( pEList==0 ){
340 for(i=0; i<pTabList->nId; i++){
341 Table *pTab = pTabList->a[i].pTab;
342 for(j=0; j<pTab->nCol; j++){
343 Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000344 if( pExpr==0 ) break;
drhd8bc7082000-06-07 23:51:50 +0000345 pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000346 if( pExpr->pLeft==0 ) break;
drhd8bc7082000-06-07 23:51:50 +0000347 pExpr->pLeft->token.z = pTab->zName;
348 pExpr->pLeft->token.n = strlen(pTab->zName);
349 pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000350 if( pExpr->pRight==0 ) break;
drhd8bc7082000-06-07 23:51:50 +0000351 pExpr->pRight->token.z = pTab->aCol[j].zName;
352 pExpr->pRight->token.n = strlen(pTab->aCol[j].zName);
drhe1b6a5b2000-07-29 13:06:59 +0000353 pExpr->span.z = "";
354 pExpr->span.n = 0;
drhd8bc7082000-06-07 23:51:50 +0000355 pEList = sqliteExprListAppend(pEList, pExpr, 0);
356 }
357 }
358 p->pEList = pEList;
359 }
360 return 0;
361}
362
363/*
364** This routine associates entries in an ORDER BY expression list with
365** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000366** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000367** the top-level node is filled in with column number and the iTable
368** value of the top-level node is filled with iTable parameter.
369**
370** If there are prior SELECT clauses, they are processed first. A match
371** in an earlier SELECT takes precedence over a later SELECT.
372**
373** Any entry that does not match is flagged as an error. The number
374** of errors is returned.
375*/
376static int matchOrderbyToColumn(
377 Parse *pParse, /* A place to leave error messages */
378 Select *pSelect, /* Match to result columns of this SELECT */
379 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
380 int iTable, /* Insert this this value in iTable */
381 int mustComplete /* If TRUE all ORDER BYs must match */
382){
383 int nErr = 0;
384 int i, j;
385 ExprList *pEList;
386
drhdaffd0e2001-04-11 14:28:42 +0000387 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000388 if( mustComplete ){
389 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
390 }
391 if( fillInColumnList(pParse, pSelect) ){
392 return 1;
393 }
394 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000395 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
396 return 1;
397 }
drhd8bc7082000-06-07 23:51:50 +0000398 }
399 pEList = pSelect->pEList;
400 for(i=0; i<pOrderBy->nExpr; i++){
401 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000402 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000403 if( pOrderBy->a[i].done ) continue;
404 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000405 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
406 char *zName = pEList->a[j].zName;
drh6e142f52000-06-08 13:36:40 +0000407 char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000408 sqliteDequote(zLabel);
409 if( sqliteStrICmp(zName, zLabel)==0 ){
410 match = 1;
411 }
drh6e142f52000-06-08 13:36:40 +0000412 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000413 }
drh4cfa7932000-06-08 15:10:46 +0000414 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000415 match = 1;
416 }
417 if( match ){
drh967e8b72000-06-21 13:59:10 +0000418 pE->op = TK_COLUMN;
419 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000420 pE->iTable = iTable;
421 pOrderBy->a[i].done = 1;
422 break;
423 }
424 }
drh92cd52f2000-06-08 01:55:29 +0000425 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000426 char zBuf[30];
427 sprintf(zBuf,"%d",i+1);
428 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
429 " does not match any result column", 0);
430 pParse->nErr++;
431 nErr++;
432 break;
433 }
434 }
435 return nErr;
436}
437
438/*
439** Get a VDBE for the given parser context. Create a new one if necessary.
440** If an error occurs, return NULL and leave a message in pParse.
441*/
442Vdbe *sqliteGetVdbe(Parse *pParse){
443 Vdbe *v = pParse->pVdbe;
444 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000445 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000446 }
drhd8bc7082000-06-07 23:51:50 +0000447 return v;
448}
449
450
451/*
drh82c3d632000-06-06 21:56:07 +0000452** This routine is called to process a query that is really the union
453** or intersection of two or more separate queries.
454*/
455static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000456 int rc; /* Success code from a subroutine */
457 Select *pPrior; /* Another SELECT immediately to our left */
458 Vdbe *v; /* Generate code to this VDBE */
459 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000460
drhd8bc7082000-06-07 23:51:50 +0000461 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
462 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000463 */
drhdaffd0e2001-04-11 14:28:42 +0000464 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000465 pPrior = p->pPrior;
466 if( pPrior->pOrderBy ){
467 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
468 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000469 pParse->nErr++;
470 return 1;
471 }
472
drhd8bc7082000-06-07 23:51:50 +0000473 /* Make sure we have a valid query engine. If not, create a new one.
474 */
475 v = sqliteGetVdbe(pParse);
476 if( v==0 ) return 1;
477
478 /* Process the UNION or INTERSECTION
479 */
drh10e5e3c2000-06-08 00:19:02 +0000480 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000481 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000482 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000483 case TK_EXCEPT:
484 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000485 int unionTab; /* Cursor number of the temporary table holding result */
486 int op; /* One of the SRT_ operations to apply to self */
487 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000488
drhd8bc7082000-06-07 23:51:50 +0000489 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
490 if( eDest==priorOp ){
491 /* We can reuse a temporary table generated by a SELECT to our
492 ** right. This also means we are not the right-most select and so
493 ** we cannot have an ORDER BY clause
494 */
drh82c3d632000-06-06 21:56:07 +0000495 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000496 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000497 }else{
drhd8bc7082000-06-07 23:51:50 +0000498 /* We will need to create our own temporary table to hold the
499 ** intermediate results.
500 */
501 unionTab = pParse->nTab++;
502 if( p->pOrderBy
503 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
504 return 1;
505 }
drhd8bc7082000-06-07 23:51:50 +0000506 if( p->op!=TK_ALL ){
drh5e00f6c2001-09-13 13:46:56 +0000507 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000508 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0);
drh345fda32001-01-15 22:51:08 +0000509 }else{
drh5e00f6c2001-09-13 13:46:56 +0000510 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000511 }
drh82c3d632000-06-06 21:56:07 +0000512 }
drhd8bc7082000-06-07 23:51:50 +0000513
514 /* Code the SELECT statements to our left
515 */
516 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000517 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000518
519 /* Code the current SELECT statement
520 */
521 switch( p->op ){
522 case TK_EXCEPT: op = SRT_Except; break;
523 case TK_UNION: op = SRT_Union; break;
524 case TK_ALL: op = SRT_Table; break;
525 }
drh82c3d632000-06-06 21:56:07 +0000526 p->pPrior = 0;
527 rc = sqliteSelect(pParse, p, op, unionTab);
528 p->pPrior = pPrior;
529 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000530
531 /* Convert the data in the temporary table into whatever form
532 ** it is that we currently need.
533 */
534 if( eDest!=priorOp ){
drh82c3d632000-06-06 21:56:07 +0000535 int iCont, iBreak;
536 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000537 generateColumnNames(pParse, 0, p->pEList);
drh92dba242000-06-08 00:28:51 +0000538 if( p->pOrderBy ){
539 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
540 }
drhd78eeee2001-09-13 16:18:53 +0000541 sqliteVdbeAddOp(v, OP_Rewind, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000542 iBreak = sqliteVdbeMakeLabel(v);
543 iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0);
544 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000545 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000546 iCont, iBreak);
547 if( rc ) return 1;
548 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
549 sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak);
drhd8bc7082000-06-07 23:51:50 +0000550 if( p->pOrderBy ){
551 generateSortTail(v, p->pEList->nExpr);
552 }
drh82c3d632000-06-06 21:56:07 +0000553 }
554 break;
555 }
556 case TK_INTERSECT: {
557 int tab1, tab2;
drh82c3d632000-06-06 21:56:07 +0000558 int iCont, iBreak;
559
drhd8bc7082000-06-07 23:51:50 +0000560 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000561 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000562 ** by allocating the tables we will need.
563 */
drh82c3d632000-06-06 21:56:07 +0000564 tab1 = pParse->nTab++;
565 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000566 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
567 return 1;
568 }
drh5e00f6c2001-09-13 13:46:56 +0000569 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000570 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000571
572 /* Code the SELECTs to our left into temporary table "tab1".
573 */
drh82c3d632000-06-06 21:56:07 +0000574 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
575 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000576
577 /* Code the current SELECT into temporary table "tab2"
578 */
drh5e00f6c2001-09-13 13:46:56 +0000579 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000580 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0);
581 p->pPrior = 0;
582 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
583 p->pPrior = pPrior;
584 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000585
586 /* Generate code to take the intersection of the two temporary
587 ** tables.
588 */
drh82c3d632000-06-06 21:56:07 +0000589 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000590 generateColumnNames(pParse, 0, p->pEList);
drh92dba242000-06-08 00:28:51 +0000591 if( p->pOrderBy ){
592 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
593 }
drhd78eeee2001-09-13 16:18:53 +0000594 sqliteVdbeAddOp(v, OP_Rewind, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000595 iBreak = sqliteVdbeMakeLabel(v);
596 iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0);
drh573bd272001-02-19 23:23:38 +0000597 sqliteVdbeAddOp(v, OP_FullKey, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000598 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0);
599 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000600 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000601 iCont, iBreak);
602 if( rc ) return 1;
603 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
604 sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak);
605 sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000606 if( p->pOrderBy ){
607 generateSortTail(v, p->pEList->nExpr);
608 }
drh82c3d632000-06-06 21:56:07 +0000609 break;
610 }
611 }
612 assert( p->pEList && pPrior->pEList );
613 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000614 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
615 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000616 pParse->nErr++;
617 return 1;
drh22827922000-06-06 17:27:05 +0000618 }
drh10e5e3c2000-06-08 00:19:02 +0000619 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000620 return 0;
621}
622
623/*
drh9bb61fe2000-06-05 16:01:39 +0000624** Generate code for the given SELECT statement.
625**
drhfef52082000-06-06 01:50:43 +0000626** The results are distributed in various ways depending on the
627** value of eDest and iParm.
628**
629** eDest Value Result
630** ------------ -------------------------------------------
631** SRT_Callback Invoke the callback for each row of the result.
632**
633** SRT_Mem Store first result in memory cell iParm
634**
635** SRT_Set Store results as keys of a table with cursor iParm
636**
drh82c3d632000-06-06 21:56:07 +0000637** SRT_Union Store results as a key in a temporary table iParm
638**
drhc4a3c772001-04-04 11:48:57 +0000639** SRT_Except Remove results form the temporary table iParm.
640**
641** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000642**
643** This routine returns the number of errors. If any errors are
644** encountered, then an appropriate error message is left in
645** pParse->zErrMsg.
646**
647** This routine does NOT free the Select structure passed in. The
648** calling function needs to do that.
649*/
650int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000651 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000652 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000653 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000654 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000655){
drhd8bc7082000-06-07 23:51:50 +0000656 int i;
drhcce7d172000-05-31 15:34:51 +0000657 WhereInfo *pWInfo;
658 Vdbe *v;
659 int isAgg = 0; /* True for select lists like "count(*)" */
drh967e8b72000-06-21 13:59:10 +0000660 ExprList *pEList; /* List of columns to extract. NULL means "*" */
drh9bb61fe2000-06-05 16:01:39 +0000661 IdList *pTabList; /* List of tables to select from */
662 Expr *pWhere; /* The WHERE clause. May be NULL */
663 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000664 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
665 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000666 int isDistinct; /* True if the DISTINCT keyword is present */
667 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000668 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000669
drhdaffd0e2001-04-11 14:28:42 +0000670 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
671
drh82c3d632000-06-06 21:56:07 +0000672 /* If there is are a sequence of queries, do the earlier ones first.
673 */
674 if( p->pPrior ){
675 return multiSelect(pParse, p, eDest, iParm);
676 }
677
678 /* Make local copies of the parameters for this query.
679 */
drh9bb61fe2000-06-05 16:01:39 +0000680 pTabList = p->pSrc;
681 pWhere = p->pWhere;
682 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000683 pGroupBy = p->pGroupBy;
684 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000685 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000686
drh10e5e3c2000-06-08 00:19:02 +0000687 /* Save the current value of pParse->nTab. Restore this value before
688 ** we exit.
689 */
690 base = pParse->nTab;
691
drh9bb61fe2000-06-05 16:01:39 +0000692 /*
693 ** Do not even attempt to generate any code if we have already seen
694 ** errors before this routine starts.
695 */
drh10e5e3c2000-06-08 00:19:02 +0000696 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000697 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000698
drhd8bc7082000-06-07 23:51:50 +0000699 /* Look up every table in the table list and create an appropriate
700 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000701 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000702 */
drhd8bc7082000-06-07 23:51:50 +0000703 if( fillInColumnList(pParse, p) ){
704 return 1;
drhcce7d172000-05-31 15:34:51 +0000705 }
drhd8bc7082000-06-07 23:51:50 +0000706 pEList = p->pEList;
drhdaffd0e2001-04-11 14:28:42 +0000707 if( pEList==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000708
drh19a775c2000-06-05 18:54:46 +0000709 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000710 ** necessary. This must be done early to allocate the cursor before
711 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000712 */
713 if( isDistinct ){
714 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000715 }else{
716 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000717 }
718
drh22827922000-06-06 17:27:05 +0000719 /* If writing to memory or generating a set
720 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000721 */
drhfef52082000-06-06 01:50:43 +0000722 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000723 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
724 "a SELECT that is part of an expression", 0);
725 pParse->nErr++;
726 return 1;
727 }
728
drh22827922000-06-06 17:27:05 +0000729 /* ORDER BY is ignored if we are not sending the result to a callback.
730 */
731 if( eDest!=SRT_Callback ){
732 pOrderBy = 0;
733 }
734
735 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000736 */
737 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000738 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
739 }
740 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
741 if( pOrderBy ){
742 for(i=0; i<pOrderBy->nExpr; i++){
743 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
744 }
745 }
drh22827922000-06-06 17:27:05 +0000746 if( pGroupBy ){
747 for(i=0; i<pGroupBy->nExpr; i++){
748 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
749 }
750 }
751 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
752
drh10e5e3c2000-06-08 00:19:02 +0000753 /* At this point, we should have allocated all the cursors that we
754 ** need to handle subquerys and temporary tables. From here on we
755 ** are committed to keeping the same value for pParse->nTab.
756 **
drh967e8b72000-06-21 13:59:10 +0000757 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000758 */
drh4794b982000-06-06 13:54:14 +0000759 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000760 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000761 return 1;
drhcce7d172000-05-31 15:34:51 +0000762 }
drh22827922000-06-06 17:27:05 +0000763 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000764 return 1;
drhcce7d172000-05-31 15:34:51 +0000765 }
766 }
drhcce7d172000-05-31 15:34:51 +0000767 if( pWhere ){
768 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000769 return 1;
drhcce7d172000-05-31 15:34:51 +0000770 }
771 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000772 return 1;
drhcce7d172000-05-31 15:34:51 +0000773 }
774 }
775 if( pOrderBy ){
776 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000777 Expr *pE = pOrderBy->a[i].pExpr;
778 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000779 return 1;
drhcce7d172000-05-31 15:34:51 +0000780 }
drh22827922000-06-06 17:27:05 +0000781 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000782 return 1;
drhcce7d172000-05-31 15:34:51 +0000783 }
784 }
785 }
drh22827922000-06-06 17:27:05 +0000786 if( pGroupBy ){
787 for(i=0; i<pGroupBy->nExpr; i++){
788 Expr *pE = pGroupBy->a[i].pExpr;
789 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
790 return 1;
791 }
792 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
793 return 1;
794 }
795 }
796 }
797 if( pHaving ){
798 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000799 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
800 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000801 pParse->nErr++;
802 return 1;
803 }
804 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
805 return 1;
806 }
drhda932812000-06-06 18:00:15 +0000807 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000808 return 1;
809 }
drhcce7d172000-05-31 15:34:51 +0000810 }
811
drh22827922000-06-06 17:27:05 +0000812 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000813 */
drh22827922000-06-06 17:27:05 +0000814 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000815 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000816 for(i=0; i<pEList->nExpr; i++){
817 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
818 return 1;
819 }
820 }
821 if( pGroupBy ){
822 for(i=0; i<pGroupBy->nExpr; i++){
823 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
824 return 1;
825 }
826 }
827 }
828 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
829 return 1;
830 }
drh191b6902000-06-08 11:13:01 +0000831 if( pOrderBy ){
832 for(i=0; i<pOrderBy->nExpr; i++){
833 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
834 return 1;
835 }
836 }
837 }
drhefb72512000-05-31 20:00:52 +0000838 }
839
drhcce7d172000-05-31 15:34:51 +0000840 /* Begin generating code.
841 */
drhdaffd0e2001-04-11 14:28:42 +0000842 v = sqliteGetVdbe(pParse);
843 if( v==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000844 if( pOrderBy ){
845 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
846 }
847
drh22827922000-06-06 17:27:05 +0000848 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000849 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000850 */
drhfef52082000-06-06 01:50:43 +0000851 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000852 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000853 }
854
drh22827922000-06-06 17:27:05 +0000855 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000856 */
857 if( isAgg ){
drh22827922000-06-06 17:27:05 +0000858 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000859 }
860
drh19a775c2000-06-05 18:54:46 +0000861 /* Initialize the memory cell to NULL
862 */
drhfef52082000-06-06 01:50:43 +0000863 if( eDest==SRT_Mem ){
drh19a775c2000-06-05 18:54:46 +0000864 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000865 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000866 }
867
drhcce7d172000-05-31 15:34:51 +0000868 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000869 */
drh19a775c2000-06-05 18:54:46 +0000870 if( isDistinct ){
drh5e00f6c2001-09-13 13:46:56 +0000871 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0, 0, 0);
drhefb72512000-05-31 20:00:52 +0000872 }
drhcce7d172000-05-31 15:34:51 +0000873 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000874 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000875
drh22827922000-06-06 17:27:05 +0000876 /* Use the standard inner loop if we are not dealing with
877 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000878 */
drhda9d6c42000-05-31 18:20:14 +0000879 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000880 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000881 pWInfo->iContinue, pWInfo->iBreak) ){
882 return 1;
drhda9d6c42000-05-31 18:20:14 +0000883 }
drhcce7d172000-05-31 15:34:51 +0000884 }
drhefb72512000-05-31 20:00:52 +0000885
drh22827922000-06-06 17:27:05 +0000886 /* If we are dealing with aggregates, then to the special aggregate
887 ** processing.
drhefb72512000-05-31 20:00:52 +0000888 */
drh22827922000-06-06 17:27:05 +0000889 else{
890 int doFocus;
891 if( pGroupBy ){
892 for(i=0; i<pGroupBy->nExpr; i++){
893 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
894 }
895 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0);
896 doFocus = 1;
897 }else{
898 doFocus = 0;
899 for(i=0; i<pParse->nAgg; i++){
900 if( !pParse->aAgg[i].isAgg ){
901 doFocus = 1;
902 break;
903 }
904 }
905 if( doFocus ){
906 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
907 }
drhcce7d172000-05-31 15:34:51 +0000908 }
drh22827922000-06-06 17:27:05 +0000909 if( doFocus ){
910 int lbl1 = sqliteVdbeMakeLabel(v);
911 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0);
912 for(i=0; i<pParse->nAgg; i++){
913 if( pParse->aAgg[i].isAgg ) continue;
914 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
915 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000916 }
drh22827922000-06-06 17:27:05 +0000917 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000918 }
drh22827922000-06-06 17:27:05 +0000919 for(i=0; i<pParse->nAgg; i++){
920 Expr *pE;
921 int op;
922 if( !pParse->aAgg[i].isAgg ) continue;
923 pE = pParse->aAgg[i].pExpr;
924 if( pE==0 ){
925 sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0);
926 continue;
927 }
928 assert( pE->op==TK_AGG_FUNCTION );
929 assert( pE->pList!=0 && pE->pList->nExpr==1 );
930 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
931 sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000932 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +0000933 case FN_Min: op = OP_Min; break;
934 case FN_Max: op = OP_Max; break;
935 case FN_Avg: op = OP_Add; break;
936 case FN_Sum: op = OP_Add; break;
937 }
938 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
939 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
940 }
drhcce7d172000-05-31 15:34:51 +0000941 }
942
drh22827922000-06-06 17:27:05 +0000943
drhcce7d172000-05-31 15:34:51 +0000944 /* End the database scan loop.
945 */
946 sqliteWhereEnd(pWInfo);
947
drh22827922000-06-06 17:27:05 +0000948 /* If we are processing aggregates, we need to set up a second loop
949 ** over all of the aggregate values and process them.
950 */
951 if( isAgg ){
952 int endagg = sqliteVdbeMakeLabel(v);
953 int startagg;
954 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0);
955 pParse->useAgg = 1;
956 if( pHaving ){
957 sqliteExprIfFalse(pParse, pHaving, startagg);
958 }
drh82c3d632000-06-06 21:56:07 +0000959 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000960 startagg, endagg) ){
961 return 1;
962 }
963 sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0);
964 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg);
965 pParse->useAgg = 0;
966 }
967
drhcce7d172000-05-31 15:34:51 +0000968 /* If there is an ORDER BY clause, then we need to sort the results
969 ** and send them to the callback one by one.
970 */
971 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +0000972 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +0000973 }
drh10e5e3c2000-06-08 00:19:02 +0000974 pParse->nTab = base;
drh9bb61fe2000-06-05 16:01:39 +0000975 return 0;
drhcce7d172000-05-31 15:34:51 +0000976}