blob: bf17fa920dd75c42ef7979352833836d7c3e17be [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**
drha8b38d22001-11-01 14:41:34 +000015** $Id: select.c,v 1.45 2001/11/01 14:41:34 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++){
drh99fcd712001-10-13 01:06:47 +0000113 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
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);
drh99fcd712001-10-13 01:06:47 +0000123 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
124 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl);
125 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
126 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
127 sqliteVdbeResolveLabel(v, lbl);
128 sqliteVdbeAddOp(v, OP_String, 0, 0);
129 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
130 sqliteVdbeAddOp(v, OP_Put, distinct, 0);
drh22827922000-06-06 17:27:05 +0000131 }
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;
drh99fcd712001-10-13 01:06:47 +0000138 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 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;
drh99fcd712001-10-13 01:06:47 +0000146 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
147 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
drh6e142f52000-06-08 13:36:40 +0000148 sqliteFree(zSortOrder);
drh99fcd712001-10-13 01:06:47 +0000149 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
drh22827922000-06-06 17:27:05 +0000150 }else
151
drh82c3d632000-06-06 21:56:07 +0000152 /* In this mode, write each query result to the key of the temporary
153 ** table iParm.
drh22827922000-06-06 17:27:05 +0000154 */
drh82c3d632000-06-06 21:56:07 +0000155 if( eDest==SRT_Union ){
drh99fcd712001-10-13 01:06:47 +0000156 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
157 sqliteVdbeAddOp(v, OP_String, iParm, 0);
158 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
159 sqliteVdbeAddOp(v, OP_Put, iParm, 0);
drh82c3d632000-06-06 21:56:07 +0000160 }else
161
drh5974a302000-06-07 14:42:26 +0000162 /* Store the result as data using a unique key.
163 */
164 if( eDest==SRT_Table ){
drh99fcd712001-10-13 01:06:47 +0000165 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
166 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
167 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
168 sqliteVdbeAddOp(v, OP_Put, iParm, 0);
drh5974a302000-06-07 14:42:26 +0000169 }else
170
drh82c3d632000-06-06 21:56:07 +0000171 /* Construct a record from the query result, but instead of
172 ** saving that record, use it as a key to delete elements from
173 ** the temporary table iParm.
174 */
175 if( eDest==SRT_Except ){
drh99fcd712001-10-13 01:06:47 +0000176 int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
177 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
178 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
drh22827922000-06-06 17:27:05 +0000179 }else
180
181 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
182 ** then there should be a single item on the stack. Write this
183 ** item into the set table with bogus data.
184 */
185 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000186 assert( nColumn==1 );
drh99fcd712001-10-13 01:06:47 +0000187 sqliteVdbeAddOp(v, OP_String, 0, 0);
188 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
189 sqliteVdbeAddOp(v, OP_Put, iParm, 0);
drh22827922000-06-06 17:27:05 +0000190 }else
191
drh82c3d632000-06-06 21:56:07 +0000192
drh22827922000-06-06 17:27:05 +0000193 /* If this is a scalar select that is part of an expression, then
194 ** store the results in the appropriate memory cell and break out
195 ** of the scan loop.
196 */
197 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000198 assert( nColumn==1 );
drh99fcd712001-10-13 01:06:47 +0000199 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0);
200 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
drh22827922000-06-06 17:27:05 +0000201 }else
202
203 /* If none of the above, send the data to the callback function.
204 */
205 {
drh99fcd712001-10-13 01:06:47 +0000206 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0);
drh82c3d632000-06-06 21:56:07 +0000207 }
208 return 0;
209}
210
211/*
drhd8bc7082000-06-07 23:51:50 +0000212** If the inner loop was generated using a non-null pOrderBy argument,
213** then the results were placed in a sorter. After the loop is terminated
214** we need to run the sorter and output the results. The following
215** routine generates the code needed to do that.
216*/
drh967e8b72000-06-21 13:59:10 +0000217static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000218 int end = sqliteVdbeMakeLabel(v);
219 int addr;
drh99fcd712001-10-13 01:06:47 +0000220 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
221 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
222 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
223 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
224 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000225 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000226}
227
228/*
drh82c3d632000-06-06 21:56:07 +0000229** Generate code that will tell the VDBE how many columns there
230** are in the result and the name for each column. This information
231** is used to provide "argc" and "azCol[]" values in the callback.
232*/
drhd8bc7082000-06-07 23:51:50 +0000233static
234void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
235 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000236 int i;
drhdaffd0e2001-04-11 14:28:42 +0000237 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000238 pParse->colNamesSet = 1;
drh99fcd712001-10-13 01:06:47 +0000239 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
drh82c3d632000-06-06 21:56:07 +0000240 for(i=0; i<pEList->nExpr; i++){
241 Expr *p;
drh1bee3d72001-10-15 00:44:35 +0000242 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000243 if( pEList->a[i].zName ){
244 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000245 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
246 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000247 continue;
248 }
249 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000250 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000251 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
252 if( p->span.z && p->span.z[0] && !showFullNames ){
drh99fcd712001-10-13 01:06:47 +0000253 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
254 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhe1b6a5b2000-07-29 13:06:59 +0000255 sqliteVdbeCompressSpace(v, addr);
drh1bee3d72001-10-15 00:44:35 +0000256 }else if( p->op==TK_COLUMN && pTabList ){
257 if( pTabList->nId>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000258 char *zName = 0;
drh98808ba2001-10-18 12:34:46 +0000259 Table *pTab = pTabList->a[p->iTable - pParse->nTab].pTab;
drh82c3d632000-06-06 21:56:07 +0000260 char *zTab;
261
drh98808ba2001-10-18 12:34:46 +0000262 zTab = pTabList->a[p->iTable - pParse->nTab].zAlias;
drh01a34662001-10-20 12:30:10 +0000263 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh967e8b72000-06-21 13:59:10 +0000264 sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iColumn].zName, 0);
drh99fcd712001-10-13 01:06:47 +0000265 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
266 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000267 sqliteFree(zName);
268 }else{
269 Table *pTab = pTabList->a[0].pTab;
drh967e8b72000-06-21 13:59:10 +0000270 char *zName = pTab->aCol[p->iColumn].zName;
drh99fcd712001-10-13 01:06:47 +0000271 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
272 sqliteVdbeChangeP3(v, -1, zName, P3_STATIC);
drh82c3d632000-06-06 21:56:07 +0000273 }
drh1bee3d72001-10-15 00:44:35 +0000274 }else if( p->span.z && p->span.z[0] ){
275 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
276 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
277 sqliteVdbeCompressSpace(v, addr);
278 }else{
279 char zName[30];
280 assert( p->op!=TK_COLUMN || pTabList==0 );
281 sprintf(zName, "column%d", i+1);
282 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
283 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000284 }
285 }
286}
287
288/*
drhd8bc7082000-06-07 23:51:50 +0000289** Name of the connection operator, used for error messages.
290*/
291static const char *selectOpName(int id){
292 char *z;
293 switch( id ){
294 case TK_ALL: z = "UNION ALL"; break;
295 case TK_INTERSECT: z = "INTERSECT"; break;
296 case TK_EXCEPT: z = "EXCEPT"; break;
297 default: z = "UNION"; break;
298 }
299 return z;
300}
301
302/*
303** For the given SELECT statement, do two things.
304**
drh967e8b72000-06-21 13:59:10 +0000305** (1) Fill in the pTabList->a[].pTab fields in the IdList that
306** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000307**
308** (2) If the columns to be extracted variable (pEList) is NULL
309** (meaning that a "*" was used in the SQL statement) then
310** create a fake pEList containing the names of all columns
311** of all tables.
312**
313** Return 0 on success. If there are problems, leave an error message
314** in pParse and return non-zero.
315*/
316static int fillInColumnList(Parse *pParse, Select *p){
317 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000318 IdList *pTabList;
319 ExprList *pEList;
320
321 if( p==0 || p->pSrc==0 ) return 1;
322 pTabList = p->pSrc;
323 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000324
325 /* Look up every table in the table list.
326 */
327 for(i=0; i<pTabList->nId; i++){
328 if( pTabList->a[i].pTab ){
329 /* This routine has run before! No need to continue */
330 return 0;
331 }
drhdaffd0e2001-04-11 14:28:42 +0000332 if( pTabList->a[i].zName==0 ){
333 /* No table name is given. Instead, there is a (SELECT ...) statement
334 ** the results of which should be used in place of the table. The
335 ** was this is implemented is that the (SELECT ...) writes its results
336 ** into a temporary table which is then scanned like any other table.
337 */
338 sqliteSetString(&pParse->zErrMsg,
339 "(SELECT...) in a FROM clause is not yet implemented.", 0);
340 pParse->nErr++;
341 return 1;
342 }
drhd8bc7082000-06-07 23:51:50 +0000343 pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
344 if( pTabList->a[i].pTab==0 ){
345 sqliteSetString(&pParse->zErrMsg, "no such table: ",
346 pTabList->a[i].zName, 0);
347 pParse->nErr++;
348 return 1;
349 }
350 }
351
352 /* If the list of columns to retrieve is "*" then replace it with
353 ** a list of all columns from all tables.
354 */
355 if( pEList==0 ){
356 for(i=0; i<pTabList->nId; i++){
357 Table *pTab = pTabList->a[i].pTab;
358 for(j=0; j<pTab->nCol; j++){
359 Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000360 if( pExpr==0 ) break;
drhd8bc7082000-06-07 23:51:50 +0000361 pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000362 if( pExpr->pLeft==0 ){ sqliteExprDelete(pExpr); break; }
drhd8bc7082000-06-07 23:51:50 +0000363 pExpr->pLeft->token.z = pTab->zName;
364 pExpr->pLeft->token.n = strlen(pTab->zName);
365 pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000366 if( pExpr->pRight==0 ){ sqliteExprDelete(pExpr); break; }
drhd8bc7082000-06-07 23:51:50 +0000367 pExpr->pRight->token.z = pTab->aCol[j].zName;
368 pExpr->pRight->token.n = strlen(pTab->aCol[j].zName);
drhe1b6a5b2000-07-29 13:06:59 +0000369 pExpr->span.z = "";
370 pExpr->span.n = 0;
drhd8bc7082000-06-07 23:51:50 +0000371 pEList = sqliteExprListAppend(pEList, pExpr, 0);
372 }
373 }
374 p->pEList = pEList;
375 }
376 return 0;
377}
378
379/*
380** This routine associates entries in an ORDER BY expression list with
381** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000382** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000383** the top-level node is filled in with column number and the iTable
384** value of the top-level node is filled with iTable parameter.
385**
386** If there are prior SELECT clauses, they are processed first. A match
387** in an earlier SELECT takes precedence over a later SELECT.
388**
389** Any entry that does not match is flagged as an error. The number
390** of errors is returned.
391*/
392static int matchOrderbyToColumn(
393 Parse *pParse, /* A place to leave error messages */
394 Select *pSelect, /* Match to result columns of this SELECT */
395 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
396 int iTable, /* Insert this this value in iTable */
397 int mustComplete /* If TRUE all ORDER BYs must match */
398){
399 int nErr = 0;
400 int i, j;
401 ExprList *pEList;
402
drhdaffd0e2001-04-11 14:28:42 +0000403 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000404 if( mustComplete ){
405 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
406 }
407 if( fillInColumnList(pParse, pSelect) ){
408 return 1;
409 }
410 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000411 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
412 return 1;
413 }
drhd8bc7082000-06-07 23:51:50 +0000414 }
415 pEList = pSelect->pEList;
416 for(i=0; i<pOrderBy->nExpr; i++){
417 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000418 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000419 if( pOrderBy->a[i].done ) continue;
420 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000421 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
422 char *zName = pEList->a[j].zName;
drh6e142f52000-06-08 13:36:40 +0000423 char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000424 sqliteDequote(zLabel);
425 if( sqliteStrICmp(zName, zLabel)==0 ){
426 match = 1;
427 }
drh6e142f52000-06-08 13:36:40 +0000428 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000429 }
drh4cfa7932000-06-08 15:10:46 +0000430 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000431 match = 1;
432 }
433 if( match ){
drh967e8b72000-06-21 13:59:10 +0000434 pE->op = TK_COLUMN;
435 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000436 pE->iTable = iTable;
437 pOrderBy->a[i].done = 1;
438 break;
439 }
440 }
drh92cd52f2000-06-08 01:55:29 +0000441 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000442 char zBuf[30];
443 sprintf(zBuf,"%d",i+1);
444 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
445 " does not match any result column", 0);
446 pParse->nErr++;
447 nErr++;
448 break;
449 }
450 }
451 return nErr;
452}
453
454/*
455** Get a VDBE for the given parser context. Create a new one if necessary.
456** If an error occurs, return NULL and leave a message in pParse.
457*/
458Vdbe *sqliteGetVdbe(Parse *pParse){
459 Vdbe *v = pParse->pVdbe;
460 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000461 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000462 }
drhd8bc7082000-06-07 23:51:50 +0000463 return v;
464}
465
466
467/*
drh82c3d632000-06-06 21:56:07 +0000468** This routine is called to process a query that is really the union
469** or intersection of two or more separate queries.
470*/
471static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000472 int rc; /* Success code from a subroutine */
473 Select *pPrior; /* Another SELECT immediately to our left */
474 Vdbe *v; /* Generate code to this VDBE */
475 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000476
drhd8bc7082000-06-07 23:51:50 +0000477 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
478 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000479 */
drhdaffd0e2001-04-11 14:28:42 +0000480 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000481 pPrior = p->pPrior;
482 if( pPrior->pOrderBy ){
483 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
484 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000485 pParse->nErr++;
486 return 1;
487 }
488
drhd8bc7082000-06-07 23:51:50 +0000489 /* Make sure we have a valid query engine. If not, create a new one.
490 */
491 v = sqliteGetVdbe(pParse);
492 if( v==0 ) return 1;
493
494 /* Process the UNION or INTERSECTION
495 */
drh10e5e3c2000-06-08 00:19:02 +0000496 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000497 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000498 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000499 case TK_EXCEPT:
500 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000501 int unionTab; /* Cursor number of the temporary table holding result */
502 int op; /* One of the SRT_ operations to apply to self */
503 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000504
drhd8bc7082000-06-07 23:51:50 +0000505 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
506 if( eDest==priorOp ){
507 /* We can reuse a temporary table generated by a SELECT to our
508 ** right. This also means we are not the right-most select and so
509 ** we cannot have an ORDER BY clause
510 */
drh82c3d632000-06-06 21:56:07 +0000511 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000512 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000513 }else{
drhd8bc7082000-06-07 23:51:50 +0000514 /* We will need to create our own temporary table to hold the
515 ** intermediate results.
516 */
517 unionTab = pParse->nTab++;
518 if( p->pOrderBy
519 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
520 return 1;
521 }
drhd8bc7082000-06-07 23:51:50 +0000522 if( p->op!=TK_ALL ){
drh99fcd712001-10-13 01:06:47 +0000523 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
524 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000525 }else{
drh99fcd712001-10-13 01:06:47 +0000526 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000527 }
drh82c3d632000-06-06 21:56:07 +0000528 }
drhd8bc7082000-06-07 23:51:50 +0000529
530 /* Code the SELECT statements to our left
531 */
532 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000533 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000534
535 /* Code the current SELECT statement
536 */
537 switch( p->op ){
538 case TK_EXCEPT: op = SRT_Except; break;
539 case TK_UNION: op = SRT_Union; break;
540 case TK_ALL: op = SRT_Table; break;
541 }
drh82c3d632000-06-06 21:56:07 +0000542 p->pPrior = 0;
543 rc = sqliteSelect(pParse, p, op, unionTab);
544 p->pPrior = pPrior;
545 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000546
547 /* Convert the data in the temporary table into whatever form
548 ** it is that we currently need.
549 */
550 if( eDest!=priorOp ){
drh82c3d632000-06-06 21:56:07 +0000551 int iCont, iBreak;
552 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000553 generateColumnNames(pParse, 0, p->pEList);
drh99fcd712001-10-13 01:06:47 +0000554 sqliteVdbeAddOp(v, OP_Rewind, unionTab, 0);
drh82c3d632000-06-06 21:56:07 +0000555 iBreak = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000556 iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak);
drh82c3d632000-06-06 21:56:07 +0000557 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000558 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000559 iCont, iBreak);
560 if( rc ) return 1;
drh99fcd712001-10-13 01:06:47 +0000561 sqliteVdbeAddOp(v, OP_Goto, 0, iCont);
562 sqliteVdbeResolveLabel(v, iBreak);
563 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000564 if( p->pOrderBy ){
565 generateSortTail(v, p->pEList->nExpr);
566 }
drh82c3d632000-06-06 21:56:07 +0000567 }
568 break;
569 }
570 case TK_INTERSECT: {
571 int tab1, tab2;
drh82c3d632000-06-06 21:56:07 +0000572 int iCont, iBreak;
573
drhd8bc7082000-06-07 23:51:50 +0000574 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000575 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000576 ** by allocating the tables we will need.
577 */
drh82c3d632000-06-06 21:56:07 +0000578 tab1 = pParse->nTab++;
579 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000580 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
581 return 1;
582 }
drh99fcd712001-10-13 01:06:47 +0000583 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0);
584 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000585
586 /* Code the SELECTs to our left into temporary table "tab1".
587 */
drh82c3d632000-06-06 21:56:07 +0000588 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
589 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000590
591 /* Code the current SELECT into temporary table "tab2"
592 */
drh99fcd712001-10-13 01:06:47 +0000593 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0);
594 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000595 p->pPrior = 0;
596 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
597 p->pPrior = pPrior;
598 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000599
600 /* Generate code to take the intersection of the two temporary
601 ** tables.
602 */
drh82c3d632000-06-06 21:56:07 +0000603 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000604 generateColumnNames(pParse, 0, p->pEList);
drh99fcd712001-10-13 01:06:47 +0000605 sqliteVdbeAddOp(v, OP_Rewind, tab1, 0);
drh82c3d632000-06-06 21:56:07 +0000606 iBreak = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000607 iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak);
608 sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
609 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000610 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000611 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000612 iCont, iBreak);
613 if( rc ) return 1;
drh99fcd712001-10-13 01:06:47 +0000614 sqliteVdbeAddOp(v, OP_Goto, 0, iCont);
615 sqliteVdbeResolveLabel(v, iBreak);
616 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
617 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000618 if( p->pOrderBy ){
619 generateSortTail(v, p->pEList->nExpr);
620 }
drh82c3d632000-06-06 21:56:07 +0000621 break;
622 }
623 }
624 assert( p->pEList && pPrior->pEList );
625 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000626 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
627 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000628 pParse->nErr++;
629 return 1;
drh22827922000-06-06 17:27:05 +0000630 }
drh10e5e3c2000-06-08 00:19:02 +0000631 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000632 return 0;
633}
634
635/*
drh9bb61fe2000-06-05 16:01:39 +0000636** Generate code for the given SELECT statement.
637**
drhfef52082000-06-06 01:50:43 +0000638** The results are distributed in various ways depending on the
639** value of eDest and iParm.
640**
641** eDest Value Result
642** ------------ -------------------------------------------
643** SRT_Callback Invoke the callback for each row of the result.
644**
645** SRT_Mem Store first result in memory cell iParm
646**
647** SRT_Set Store results as keys of a table with cursor iParm
648**
drh82c3d632000-06-06 21:56:07 +0000649** SRT_Union Store results as a key in a temporary table iParm
650**
drhc4a3c772001-04-04 11:48:57 +0000651** SRT_Except Remove results form the temporary table iParm.
652**
653** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000654**
655** This routine returns the number of errors. If any errors are
656** encountered, then an appropriate error message is left in
657** pParse->zErrMsg.
658**
659** This routine does NOT free the Select structure passed in. The
660** calling function needs to do that.
661*/
662int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000663 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000664 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000665 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000666 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000667){
drhd8bc7082000-06-07 23:51:50 +0000668 int i;
drhcce7d172000-05-31 15:34:51 +0000669 WhereInfo *pWInfo;
670 Vdbe *v;
671 int isAgg = 0; /* True for select lists like "count(*)" */
drh967e8b72000-06-21 13:59:10 +0000672 ExprList *pEList; /* List of columns to extract. NULL means "*" */
drh9bb61fe2000-06-05 16:01:39 +0000673 IdList *pTabList; /* List of tables to select from */
674 Expr *pWhere; /* The WHERE clause. May be NULL */
675 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000676 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
677 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000678 int isDistinct; /* True if the DISTINCT keyword is present */
679 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000680 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000681
drhdaffd0e2001-04-11 14:28:42 +0000682 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
683
drh82c3d632000-06-06 21:56:07 +0000684 /* If there is are a sequence of queries, do the earlier ones first.
685 */
686 if( p->pPrior ){
687 return multiSelect(pParse, p, eDest, iParm);
688 }
689
690 /* Make local copies of the parameters for this query.
691 */
drh9bb61fe2000-06-05 16:01:39 +0000692 pTabList = p->pSrc;
693 pWhere = p->pWhere;
694 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000695 pGroupBy = p->pGroupBy;
696 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000697 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000698
drh10e5e3c2000-06-08 00:19:02 +0000699 /* Save the current value of pParse->nTab. Restore this value before
700 ** we exit.
701 */
702 base = pParse->nTab;
703
drh9bb61fe2000-06-05 16:01:39 +0000704 /*
705 ** Do not even attempt to generate any code if we have already seen
706 ** errors before this routine starts.
707 */
drh10e5e3c2000-06-08 00:19:02 +0000708 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000709 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000710
drhd8bc7082000-06-07 23:51:50 +0000711 /* Look up every table in the table list and create an appropriate
712 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000713 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000714 */
drhd8bc7082000-06-07 23:51:50 +0000715 if( fillInColumnList(pParse, p) ){
716 return 1;
drhcce7d172000-05-31 15:34:51 +0000717 }
drhd8bc7082000-06-07 23:51:50 +0000718 pEList = p->pEList;
drhdaffd0e2001-04-11 14:28:42 +0000719 if( pEList==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000720
drh19a775c2000-06-05 18:54:46 +0000721 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000722 ** necessary. This must be done early to allocate the cursor before
723 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000724 */
725 if( isDistinct ){
726 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000727 }else{
728 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000729 }
730
drh22827922000-06-06 17:27:05 +0000731 /* If writing to memory or generating a set
732 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000733 */
drhfef52082000-06-06 01:50:43 +0000734 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000735 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
736 "a SELECT that is part of an expression", 0);
737 pParse->nErr++;
738 return 1;
739 }
740
drh22827922000-06-06 17:27:05 +0000741 /* ORDER BY is ignored if we are not sending the result to a callback.
742 */
743 if( eDest!=SRT_Callback ){
744 pOrderBy = 0;
745 }
746
747 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000748 */
749 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000750 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
751 }
752 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
753 if( pOrderBy ){
754 for(i=0; i<pOrderBy->nExpr; i++){
755 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
756 }
757 }
drh22827922000-06-06 17:27:05 +0000758 if( pGroupBy ){
759 for(i=0; i<pGroupBy->nExpr; i++){
760 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
761 }
762 }
763 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
764
drh10e5e3c2000-06-08 00:19:02 +0000765 /* At this point, we should have allocated all the cursors that we
766 ** need to handle subquerys and temporary tables. From here on we
767 ** are committed to keeping the same value for pParse->nTab.
768 **
drh967e8b72000-06-21 13:59:10 +0000769 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000770 */
drh4794b982000-06-06 13:54:14 +0000771 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000772 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000773 return 1;
drhcce7d172000-05-31 15:34:51 +0000774 }
drh22827922000-06-06 17:27:05 +0000775 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000776 return 1;
drhcce7d172000-05-31 15:34:51 +0000777 }
778 }
drhcce7d172000-05-31 15:34:51 +0000779 if( pWhere ){
780 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000781 return 1;
drhcce7d172000-05-31 15:34:51 +0000782 }
783 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000784 return 1;
drhcce7d172000-05-31 15:34:51 +0000785 }
786 }
787 if( pOrderBy ){
788 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000789 Expr *pE = pOrderBy->a[i].pExpr;
790 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000791 return 1;
drhcce7d172000-05-31 15:34:51 +0000792 }
drh22827922000-06-06 17:27:05 +0000793 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000794 return 1;
drhcce7d172000-05-31 15:34:51 +0000795 }
796 }
797 }
drh22827922000-06-06 17:27:05 +0000798 if( pGroupBy ){
799 for(i=0; i<pGroupBy->nExpr; i++){
800 Expr *pE = pGroupBy->a[i].pExpr;
801 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
802 return 1;
803 }
804 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
805 return 1;
806 }
807 }
808 }
809 if( pHaving ){
810 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000811 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
812 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000813 pParse->nErr++;
814 return 1;
815 }
816 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
817 return 1;
818 }
drhda932812000-06-06 18:00:15 +0000819 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000820 return 1;
821 }
drhcce7d172000-05-31 15:34:51 +0000822 }
823
drh22827922000-06-06 17:27:05 +0000824 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000825 */
drh22827922000-06-06 17:27:05 +0000826 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000827 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000828 for(i=0; i<pEList->nExpr; i++){
829 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
830 return 1;
831 }
832 }
833 if( pGroupBy ){
834 for(i=0; i<pGroupBy->nExpr; i++){
835 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
836 return 1;
837 }
838 }
839 }
840 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
841 return 1;
842 }
drh191b6902000-06-08 11:13:01 +0000843 if( pOrderBy ){
844 for(i=0; i<pOrderBy->nExpr; i++){
845 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
846 return 1;
847 }
848 }
849 }
drhefb72512000-05-31 20:00:52 +0000850 }
851
drhcce7d172000-05-31 15:34:51 +0000852 /* Begin generating code.
853 */
drhdaffd0e2001-04-11 14:28:42 +0000854 v = sqliteGetVdbe(pParse);
855 if( v==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000856
drh22827922000-06-06 17:27:05 +0000857 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000858 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000859 */
drhfef52082000-06-06 01:50:43 +0000860 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000861 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000862 }
863
drh22827922000-06-06 17:27:05 +0000864 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000865 */
866 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +0000867 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drh1bee3d72001-10-15 00:44:35 +0000868 if( pGroupBy==0 ){
869 sqliteVdbeAddOp(v, OP_String, 0, 0);
870 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
871 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
872 for(i=0; i<pParse->nAgg; i++){
873 Expr *pE;
874 if( !pParse->aAgg[i].isAgg ) continue;
875 pE = pParse->aAgg[i].pExpr;
876 assert( pE==0 || pE->op==TK_AGG_FUNCTION );
877 assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) );
878 if( pE==0 || pE->iColumn==FN_Sum ){
879 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
880 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
881 continue;
882 }
883 }
884 }
drhcce7d172000-05-31 15:34:51 +0000885 }
886
drh19a775c2000-06-05 18:54:46 +0000887 /* Initialize the memory cell to NULL
888 */
drhfef52082000-06-06 01:50:43 +0000889 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +0000890 sqliteVdbeAddOp(v, OP_String, 0, 0);
891 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0);
drh19a775c2000-06-05 18:54:46 +0000892 }
893
drhcce7d172000-05-31 15:34:51 +0000894 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000895 */
drh19a775c2000-06-05 18:54:46 +0000896 if( isDistinct ){
drh99fcd712001-10-13 01:06:47 +0000897 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0);
drhefb72512000-05-31 20:00:52 +0000898 }
drhcce7d172000-05-31 15:34:51 +0000899 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000900 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000901
drh22827922000-06-06 17:27:05 +0000902 /* Use the standard inner loop if we are not dealing with
903 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000904 */
drhda9d6c42000-05-31 18:20:14 +0000905 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000906 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000907 pWInfo->iContinue, pWInfo->iBreak) ){
908 return 1;
drhda9d6c42000-05-31 18:20:14 +0000909 }
drhcce7d172000-05-31 15:34:51 +0000910 }
drhefb72512000-05-31 20:00:52 +0000911
drh22827922000-06-06 17:27:05 +0000912 /* If we are dealing with aggregates, then to the special aggregate
913 ** processing.
drhefb72512000-05-31 20:00:52 +0000914 */
drh22827922000-06-06 17:27:05 +0000915 else{
drh22827922000-06-06 17:27:05 +0000916 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +0000917 int lbl1;
drh22827922000-06-06 17:27:05 +0000918 for(i=0; i<pGroupBy->nExpr; i++){
919 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
920 }
drh99fcd712001-10-13 01:06:47 +0000921 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +0000922 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000923 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +0000924 for(i=0; i<pParse->nAgg; i++){
925 if( pParse->aAgg[i].isAgg ) continue;
926 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +0000927 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +0000928 }
drh22827922000-06-06 17:27:05 +0000929 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000930 }
drh22827922000-06-06 17:27:05 +0000931 for(i=0; i<pParse->nAgg; i++){
932 Expr *pE;
933 int op;
934 if( !pParse->aAgg[i].isAgg ) continue;
935 pE = pParse->aAgg[i].pExpr;
936 if( pE==0 ){
drh99fcd712001-10-13 01:06:47 +0000937 sqliteVdbeAddOp(v, OP_AggIncr, 1, i);
drh22827922000-06-06 17:27:05 +0000938 continue;
939 }
940 assert( pE->op==TK_AGG_FUNCTION );
941 assert( pE->pList!=0 && pE->pList->nExpr==1 );
942 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000943 sqliteVdbeAddOp(v, OP_AggGet, 0, i);
drh967e8b72000-06-21 13:59:10 +0000944 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +0000945 case FN_Min: op = OP_Min; break;
946 case FN_Max: op = OP_Max; break;
947 case FN_Avg: op = OP_Add; break;
948 case FN_Sum: op = OP_Add; break;
949 }
drh99fcd712001-10-13 01:06:47 +0000950 sqliteVdbeAddOp(v, op, 0, 0);
951 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drh22827922000-06-06 17:27:05 +0000952 }
drhcce7d172000-05-31 15:34:51 +0000953 }
954
drh22827922000-06-06 17:27:05 +0000955
drhcce7d172000-05-31 15:34:51 +0000956 /* End the database scan loop.
957 */
958 sqliteWhereEnd(pWInfo);
959
drh22827922000-06-06 17:27:05 +0000960 /* If we are processing aggregates, we need to set up a second loop
961 ** over all of the aggregate values and process them.
962 */
963 if( isAgg ){
964 int endagg = sqliteVdbeMakeLabel(v);
965 int startagg;
drh99fcd712001-10-13 01:06:47 +0000966 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +0000967 pParse->useAgg = 1;
968 if( pHaving ){
969 sqliteExprIfFalse(pParse, pHaving, startagg);
970 }
drh82c3d632000-06-06 21:56:07 +0000971 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000972 startagg, endagg) ){
973 return 1;
974 }
drh99fcd712001-10-13 01:06:47 +0000975 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
976 sqliteVdbeResolveLabel(v, endagg);
977 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +0000978 pParse->useAgg = 0;
979 }
980
drhcce7d172000-05-31 15:34:51 +0000981 /* If there is an ORDER BY clause, then we need to sort the results
982 ** and send them to the callback one by one.
983 */
984 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +0000985 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +0000986 }
drh10e5e3c2000-06-08 00:19:02 +0000987 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +0000988
989
990 /* Issue a null callback if that is what the user wants.
991 */
992 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
993 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
994 }
995
drh9bb61fe2000-06-05 16:01:39 +0000996 return 0;
drhcce7d172000-05-31 15:34:51 +0000997}