blob: f71016524e64dd804b3d4ac945bbd9a6fd65838b [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**
drhf55f25f2002-02-28 01:46:11 +000015** $Id: select.c,v 1.70 2002/02/28 01:46:13 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 */
drh9bbca4c2001-11-06 04:00:18 +000030 int isDistinct, /* true if the DISTINCT keyword is present */
31 int nLimit, /* LIMIT value. -1 means not used */
32 int nOffset /* OFFSET value. -1 means not used */
drh9bb61fe2000-06-05 16:01:39 +000033){
34 Select *pNew;
35 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000036 if( pNew==0 ){
37 sqliteExprListDelete(pEList);
38 sqliteIdListDelete(pSrc);
39 sqliteExprDelete(pWhere);
40 sqliteExprListDelete(pGroupBy);
41 sqliteExprDelete(pHaving);
42 sqliteExprListDelete(pOrderBy);
43 }else{
44 pNew->pEList = pEList;
45 pNew->pSrc = pSrc;
46 pNew->pWhere = pWhere;
47 pNew->pGroupBy = pGroupBy;
48 pNew->pHaving = pHaving;
49 pNew->pOrderBy = pOrderBy;
50 pNew->isDistinct = isDistinct;
51 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000052 pNew->nLimit = nLimit;
53 pNew->nOffset = nOffset;
drhdaffd0e2001-04-11 14:28:42 +000054 }
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);
drha76b5df2002-02-23 02:32:10 +000070 sqliteFree(p->zSelect);
drh9bb61fe2000-06-05 16:01:39 +000071 sqliteFree(p);
72}
73
74/*
drh22827922000-06-06 17:27:05 +000075** Delete the aggregate information from the parse structure.
76*/
drh1d83f052002-02-17 00:30:36 +000077static void sqliteAggregateInfoReset(Parse *pParse){
drh22827922000-06-06 17:27:05 +000078 sqliteFree(pParse->aAgg);
79 pParse->aAgg = 0;
80 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +000081 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;
drhdaffd0e2001-04-11 14:28:42 +0000106 if( v==0 ) return 0;
drh22827922000-06-06 17:27:05 +0000107
drh967e8b72000-06-21 13:59:10 +0000108 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000109 */
drh82c3d632000-06-06 21:56:07 +0000110 if( pEList ){
111 for(i=0; i<pEList->nExpr; i++){
112 sqliteExprCode(pParse, pEList->a[i].pExpr);
113 }
drh967e8b72000-06-21 13:59:10 +0000114 nColumn = pEList->nExpr;
drh82c3d632000-06-06 21:56:07 +0000115 }else{
drh967e8b72000-06-21 13:59:10 +0000116 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000117 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000118 }
drh22827922000-06-06 17:27:05 +0000119 }
120
drhdaffd0e2001-04-11 14:28:42 +0000121 /* If the DISTINCT keyword was present on the SELECT statement
122 ** and this row has been seen before, then do not make this row
123 ** part of the result.
drh22827922000-06-06 17:27:05 +0000124 */
125 if( distinct>=0 ){
126 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000127 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
128 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl);
129 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
130 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
131 sqliteVdbeResolveLabel(v, lbl);
132 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000133 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
drh22827922000-06-06 17:27:05 +0000134 }
drh82c3d632000-06-06 21:56:07 +0000135
drh22827922000-06-06 17:27:05 +0000136 /* If there is an ORDER BY clause, then store the results
137 ** in a sorter.
138 */
139 if( pOrderBy ){
140 char *zSortOrder;
drh99fcd712001-10-13 01:06:47 +0000141 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
drh22827922000-06-06 17:27:05 +0000142 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
143 if( zSortOrder==0 ) return 1;
144 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000145 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000146 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
147 }
148 zSortOrder[pOrderBy->nExpr] = 0;
drh99fcd712001-10-13 01:06:47 +0000149 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
150 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
drh6e142f52000-06-08 13:36:40 +0000151 sqliteFree(zSortOrder);
drh99fcd712001-10-13 01:06:47 +0000152 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
drh22827922000-06-06 17:27:05 +0000153 }else
154
drh82c3d632000-06-06 21:56:07 +0000155 /* In this mode, write each query result to the key of the temporary
156 ** table iParm.
drh22827922000-06-06 17:27:05 +0000157 */
drh82c3d632000-06-06 21:56:07 +0000158 if( eDest==SRT_Union ){
drh99fcd712001-10-13 01:06:47 +0000159 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
160 sqliteVdbeAddOp(v, OP_String, iParm, 0);
drh6b125452002-01-28 15:53:03 +0000161 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
drh82c3d632000-06-06 21:56:07 +0000162 }else
163
drh5974a302000-06-07 14:42:26 +0000164 /* Store the result as data using a unique key.
165 */
166 if( eDest==SRT_Table ){
drh99fcd712001-10-13 01:06:47 +0000167 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
168 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
169 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh6b125452002-01-28 15:53:03 +0000170 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
drh5974a302000-06-07 14:42:26 +0000171 }else
172
drh82c3d632000-06-06 21:56:07 +0000173 /* Construct a record from the query result, but instead of
174 ** saving that record, use it as a key to delete elements from
175 ** the temporary table iParm.
176 */
177 if( eDest==SRT_Except ){
drh99fcd712001-10-13 01:06:47 +0000178 int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
179 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
180 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
drh22827922000-06-06 17:27:05 +0000181 }else
182
183 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
184 ** then there should be a single item on the stack. Write this
185 ** item into the set table with bogus data.
186 */
187 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000188 assert( nColumn==1 );
drh99fcd712001-10-13 01:06:47 +0000189 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000190 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
drh22827922000-06-06 17:27:05 +0000191 }else
192
drh82c3d632000-06-06 21:56:07 +0000193
drh22827922000-06-06 17:27:05 +0000194 /* If this is a scalar select that is part of an expression, then
195 ** store the results in the appropriate memory cell and break out
196 ** of the scan loop.
197 */
198 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000199 assert( nColumn==1 );
drh8721ce42001-11-07 14:22:00 +0000200 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh99fcd712001-10-13 01:06:47 +0000201 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
drh22827922000-06-06 17:27:05 +0000202 }else
203
204 /* If none of the above, send the data to the callback function.
205 */
206 {
drh9bbca4c2001-11-06 04:00:18 +0000207 sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak);
drh82c3d632000-06-06 21:56:07 +0000208 }
209 return 0;
210}
211
212/*
drhd8bc7082000-06-07 23:51:50 +0000213** If the inner loop was generated using a non-null pOrderBy argument,
214** then the results were placed in a sorter. After the loop is terminated
215** we need to run the sorter and output the results. The following
216** routine generates the code needed to do that.
217*/
drh967e8b72000-06-21 13:59:10 +0000218static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000219 int end = sqliteVdbeMakeLabel(v);
220 int addr;
drh99fcd712001-10-13 01:06:47 +0000221 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
222 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drh9bbca4c2001-11-06 04:00:18 +0000223 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end);
drh99fcd712001-10-13 01:06:47 +0000224 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
225 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000226 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000227}
228
229/*
drh82c3d632000-06-06 21:56:07 +0000230** Generate code that will tell the VDBE how many columns there
231** are in the result and the name for each column. This information
232** is used to provide "argc" and "azCol[]" values in the callback.
233*/
drhd8bc7082000-06-07 23:51:50 +0000234static
235void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
236 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000237 int i;
drhdaffd0e2001-04-11 14:28:42 +0000238 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000239 pParse->colNamesSet = 1;
drh99fcd712001-10-13 01:06:47 +0000240 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
drh82c3d632000-06-06 21:56:07 +0000241 for(i=0; i<pEList->nExpr; i++){
242 Expr *p;
drh1bee3d72001-10-15 00:44:35 +0000243 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000244 if( pEList->a[i].zName ){
245 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000246 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
247 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000248 continue;
249 }
250 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000251 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000252 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
253 if( p->span.z && p->span.z[0] && !showFullNames ){
drh99fcd712001-10-13 01:06:47 +0000254 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
255 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhe1b6a5b2000-07-29 13:06:59 +0000256 sqliteVdbeCompressSpace(v, addr);
drh1bee3d72001-10-15 00:44:35 +0000257 }else if( p->op==TK_COLUMN && pTabList ){
drh8aff1012001-12-22 14:49:24 +0000258 Table *pTab = pTabList->a[p->iTable - pParse->nTab].pTab;
drh97665872002-02-13 23:22:53 +0000259 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000260 int iCol = p->iColumn;
261 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000262 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
263 zCol = iCol<0 ? "_ROWID_" : pTab->aCol[iCol].zName;
drh1bee3d72001-10-15 00:44:35 +0000264 if( pTabList->nId>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000265 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000266 char *zTab;
267
drh98808ba2001-10-18 12:34:46 +0000268 zTab = pTabList->a[p->iTable - pParse->nTab].zAlias;
drh01a34662001-10-20 12:30:10 +0000269 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000270 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000271 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
272 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000273 sqliteFree(zName);
274 }else{
drh99fcd712001-10-13 01:06:47 +0000275 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000276 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000277 }
drh1bee3d72001-10-15 00:44:35 +0000278 }else if( p->span.z && p->span.z[0] ){
279 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
280 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
281 sqliteVdbeCompressSpace(v, addr);
282 }else{
283 char zName[30];
284 assert( p->op!=TK_COLUMN || pTabList==0 );
285 sprintf(zName, "column%d", i+1);
286 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
287 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000288 }
289 }
290}
291
292/*
drhd8bc7082000-06-07 23:51:50 +0000293** Name of the connection operator, used for error messages.
294*/
295static const char *selectOpName(int id){
296 char *z;
297 switch( id ){
298 case TK_ALL: z = "UNION ALL"; break;
299 case TK_INTERSECT: z = "INTERSECT"; break;
300 case TK_EXCEPT: z = "EXCEPT"; break;
301 default: z = "UNION"; break;
302 }
303 return z;
304}
305
306/*
drh22f70c32002-02-18 01:17:00 +0000307** Given a SELECT statement, generate a Table structure that describes
308** the result set of that SELECT.
309*/
310Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
311 Table *pTab;
312 int i;
313 ExprList *pEList;
314 static int fillInColumnList(Parse*, Select*);
315
316 if( fillInColumnList(pParse, pSelect) ){
317 return 0;
318 }
319 pTab = sqliteMalloc( sizeof(Table) );
320 if( pTab==0 ){
321 return 0;
322 }
323 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
324 pEList = pSelect->pEList;
325 pTab->nCol = pEList->nExpr;
326 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
327 for(i=0; i<pTab->nCol; i++){
328 Expr *p;
329 if( pEList->a[i].zName ){
330 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
331 }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
332 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
drhd820cb12002-02-18 03:21:45 +0000333 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
334 p->pRight->token.z[0] ){
335 sqliteSetNString(&pTab->aCol[i].zName,
336 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000337 }else{
338 char zBuf[30];
339 sprintf(zBuf, "column%d", i+1);
340 pTab->aCol[i].zName = sqliteStrDup(zBuf);
341 }
342 }
343 pTab->iPKey = -1;
344 return pTab;
345}
346
347/*
drhd8bc7082000-06-07 23:51:50 +0000348** For the given SELECT statement, do two things.
349**
drh967e8b72000-06-21 13:59:10 +0000350** (1) Fill in the pTabList->a[].pTab fields in the IdList that
drh22f70c32002-02-18 01:17:00 +0000351** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000352**
353** (2) If the columns to be extracted variable (pEList) is NULL
354** (meaning that a "*" was used in the SQL statement) then
355** create a fake pEList containing the names of all columns
356** of all tables.
357**
358** Return 0 on success. If there are problems, leave an error message
359** in pParse and return non-zero.
360*/
361static int fillInColumnList(Parse *pParse, Select *p){
drh7c917d12001-12-16 20:05:05 +0000362 int i, j, k;
drhdaffd0e2001-04-11 14:28:42 +0000363 IdList *pTabList;
364 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000365 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000366
367 if( p==0 || p->pSrc==0 ) return 1;
368 pTabList = p->pSrc;
369 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000370
371 /* Look up every table in the table list.
372 */
373 for(i=0; i<pTabList->nId; i++){
374 if( pTabList->a[i].pTab ){
375 /* This routine has run before! No need to continue */
376 return 0;
377 }
drhdaffd0e2001-04-11 14:28:42 +0000378 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000379 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000380 assert( pTabList->a[i].pSelect!=0 );
381 pTabList->a[i].pTab = pTab =
382 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
383 pTabList->a[i].pSelect);
384 if( pTab==0 ){
385 return 1;
386 }
387 pTab->isTransient = 1;
388 }else{
drha76b5df2002-02-23 02:32:10 +0000389 /* An ordinary table or view name in the FROM clause */
390 pTabList->a[i].pTab = pTab =
391 sqliteFindTable(pParse->db, pTabList->a[i].zName);
392 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000393 sqliteSetString(&pParse->zErrMsg, "no such table: ",
394 pTabList->a[i].zName, 0);
395 pParse->nErr++;
396 return 1;
397 }
drha76b5df2002-02-23 02:32:10 +0000398 if( pTab->pSelect ){
drhff78bd22002-02-27 01:47:11 +0000399 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000400 }
drhd8bc7082000-06-07 23:51:50 +0000401 }
402 }
403
drh7c917d12001-12-16 20:05:05 +0000404 /* For every "*" that occurs in the column list, insert the names of
405 ** all columns in all tables. The parser inserted a special expression
406 ** with the TK_ALL operator for each "*" that it found in the column list.
407 ** The following code just has to locate the TK_ALL expressions and expand
408 ** each one to the list of all columns in all tables.
drhd8bc7082000-06-07 23:51:50 +0000409 */
drh7c917d12001-12-16 20:05:05 +0000410 for(k=0; k<pEList->nExpr; k++){
411 if( pEList->a[k].pExpr->op==TK_ALL ) break;
412 }
413 if( k<pEList->nExpr ){
414 struct ExprList_item *a = pEList->a;
415 ExprList *pNew = 0;
416 for(k=0; k<pEList->nExpr; k++){
417 if( a[k].pExpr->op!=TK_ALL ){
418 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
419 pNew->a[pNew->nExpr-1].zName = a[k].zName;
420 a[k].pExpr = 0;
421 a[k].zName = 0;
422 }else{
423 for(i=0; i<pTabList->nId; i++){
424 Table *pTab = pTabList->a[i].pTab;
425 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000426 Expr *pExpr, *pLeft, *pRight;
427 pRight = sqliteExpr(TK_ID, 0, 0, 0);
428 if( pRight==0 ) break;
429 pRight->token.z = pTab->aCol[j].zName;
430 pRight->token.n = strlen(pTab->aCol[j].zName);
431 if( pTab->zName ){
432 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
433 if( pLeft==0 ) break;
434 if( pTabList->a[i].zAlias && pTabList->a[i].zAlias[0] ){
435 pLeft->token.z = pTabList->a[i].zAlias;
436 pLeft->token.n = strlen(pTabList->a[i].zAlias);
437 }else{
438 pLeft->token.z = pTab->zName;
439 pLeft->token.n = strlen(pTab->zName);
440 }
441 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
442 if( pExpr==0 ) break;
drh7c917d12001-12-16 20:05:05 +0000443 }else{
drh22f70c32002-02-18 01:17:00 +0000444 pExpr = pRight;
445 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +0000446 }
drh7c917d12001-12-16 20:05:05 +0000447 pNew = sqliteExprListAppend(pNew, pExpr, 0);
448 }
drh17e24df2001-11-06 14:10:41 +0000449 }
drhd8bc7082000-06-07 23:51:50 +0000450 }
451 }
drh7c917d12001-12-16 20:05:05 +0000452 sqliteExprListDelete(pEList);
453 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000454 }
455 return 0;
456}
457
458/*
drhff78bd22002-02-27 01:47:11 +0000459** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
460** in a select structure. It just sets the pointers to NULL. This
461** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
462** pointer is not NULL, this routine is called recursively on that pointer.
463**
464** This routine is called on the Select structure that defines a
465** VIEW in order to undo any bindings to tables. This is necessary
466** because those tables might be DROPed by a subsequent SQL command.
467*/
468void sqliteSelectUnbind(Select *p){
469 int i;
470 IdList *pSrc = p->pSrc;
471 Table *pTab;
472 if( p==0 ) return;
473 for(i=0; i<pSrc->nId; i++){
474 if( (pTab = pSrc->a[i].pTab)!=0 ){
475 if( pTab->isTransient ){
476 sqliteDeleteTable(0, pTab);
477 sqliteSelectDelete(pSrc->a[i].pSelect);
478 pSrc->a[i].pSelect = 0;
479 }
480 pSrc->a[i].pTab = 0;
481 if( pSrc->a[i].pSelect ){
482 sqliteSelectUnbind(pSrc->a[i].pSelect);
483 }
484 }
485 }
486}
487
488/*
drhd8bc7082000-06-07 23:51:50 +0000489** This routine associates entries in an ORDER BY expression list with
490** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000491** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000492** the top-level node is filled in with column number and the iTable
493** value of the top-level node is filled with iTable parameter.
494**
495** If there are prior SELECT clauses, they are processed first. A match
496** in an earlier SELECT takes precedence over a later SELECT.
497**
498** Any entry that does not match is flagged as an error. The number
499** of errors is returned.
500*/
501static int matchOrderbyToColumn(
502 Parse *pParse, /* A place to leave error messages */
503 Select *pSelect, /* Match to result columns of this SELECT */
504 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
505 int iTable, /* Insert this this value in iTable */
506 int mustComplete /* If TRUE all ORDER BYs must match */
507){
508 int nErr = 0;
509 int i, j;
510 ExprList *pEList;
511
drhdaffd0e2001-04-11 14:28:42 +0000512 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000513 if( mustComplete ){
514 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
515 }
516 if( fillInColumnList(pParse, pSelect) ){
517 return 1;
518 }
519 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000520 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
521 return 1;
522 }
drhd8bc7082000-06-07 23:51:50 +0000523 }
524 pEList = pSelect->pEList;
525 for(i=0; i<pOrderBy->nExpr; i++){
526 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000527 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000528 if( pOrderBy->a[i].done ) continue;
529 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000530 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +0000531 char *zName, *zLabel;
532 zName = pEList->a[j].zName;
533 assert( pE->token.z );
534 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000535 sqliteDequote(zLabel);
536 if( sqliteStrICmp(zName, zLabel)==0 ){
537 match = 1;
538 }
drh6e142f52000-06-08 13:36:40 +0000539 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000540 }
drh4cfa7932000-06-08 15:10:46 +0000541 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000542 match = 1;
543 }
544 if( match ){
drh967e8b72000-06-21 13:59:10 +0000545 pE->op = TK_COLUMN;
546 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000547 pE->iTable = iTable;
548 pOrderBy->a[i].done = 1;
549 break;
550 }
551 }
drh92cd52f2000-06-08 01:55:29 +0000552 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000553 char zBuf[30];
554 sprintf(zBuf,"%d",i+1);
555 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
556 " does not match any result column", 0);
557 pParse->nErr++;
558 nErr++;
559 break;
560 }
561 }
562 return nErr;
563}
564
565/*
566** Get a VDBE for the given parser context. Create a new one if necessary.
567** If an error occurs, return NULL and leave a message in pParse.
568*/
569Vdbe *sqliteGetVdbe(Parse *pParse){
570 Vdbe *v = pParse->pVdbe;
571 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000572 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000573 }
drhd8bc7082000-06-07 23:51:50 +0000574 return v;
575}
576
577
578/*
drh82c3d632000-06-06 21:56:07 +0000579** This routine is called to process a query that is really the union
580** or intersection of two or more separate queries.
581*/
582static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000583 int rc; /* Success code from a subroutine */
584 Select *pPrior; /* Another SELECT immediately to our left */
585 Vdbe *v; /* Generate code to this VDBE */
586 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000587
drhd8bc7082000-06-07 23:51:50 +0000588 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
589 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000590 */
drhdaffd0e2001-04-11 14:28:42 +0000591 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000592 pPrior = p->pPrior;
593 if( pPrior->pOrderBy ){
594 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
595 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000596 pParse->nErr++;
597 return 1;
598 }
599
drhd8bc7082000-06-07 23:51:50 +0000600 /* Make sure we have a valid query engine. If not, create a new one.
601 */
602 v = sqliteGetVdbe(pParse);
603 if( v==0 ) return 1;
604
605 /* Process the UNION or INTERSECTION
606 */
drh10e5e3c2000-06-08 00:19:02 +0000607 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000608 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000609 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000610 case TK_EXCEPT:
611 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000612 int unionTab; /* Cursor number of the temporary table holding result */
613 int op; /* One of the SRT_ operations to apply to self */
614 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000615
drhd8bc7082000-06-07 23:51:50 +0000616 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
617 if( eDest==priorOp ){
618 /* We can reuse a temporary table generated by a SELECT to our
619 ** right. This also means we are not the right-most select and so
620 ** we cannot have an ORDER BY clause
621 */
drh82c3d632000-06-06 21:56:07 +0000622 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000623 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000624 }else{
drhd8bc7082000-06-07 23:51:50 +0000625 /* We will need to create our own temporary table to hold the
626 ** intermediate results.
627 */
628 unionTab = pParse->nTab++;
629 if( p->pOrderBy
630 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
631 return 1;
632 }
drhd8bc7082000-06-07 23:51:50 +0000633 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +0000634 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +0000635 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000636 }else{
drh99fcd712001-10-13 01:06:47 +0000637 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000638 }
drh82c3d632000-06-06 21:56:07 +0000639 }
drhd8bc7082000-06-07 23:51:50 +0000640
641 /* Code the SELECT statements to our left
642 */
643 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000644 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000645
646 /* Code the current SELECT statement
647 */
648 switch( p->op ){
649 case TK_EXCEPT: op = SRT_Except; break;
650 case TK_UNION: op = SRT_Union; break;
651 case TK_ALL: op = SRT_Table; break;
652 }
drh82c3d632000-06-06 21:56:07 +0000653 p->pPrior = 0;
654 rc = sqliteSelect(pParse, p, op, unionTab);
655 p->pPrior = pPrior;
656 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000657
658 /* Convert the data in the temporary table into whatever form
659 ** it is that we currently need.
660 */
661 if( eDest!=priorOp ){
drh6b563442001-11-07 16:48:26 +0000662 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000663 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000664 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000665 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000666 iCont = sqliteVdbeMakeLabel(v);
667 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
668 iStart = sqliteVdbeCurrentAddr(v);
drh82c3d632000-06-06 21:56:07 +0000669 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000670 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000671 iCont, iBreak);
672 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000673 sqliteVdbeResolveLabel(v, iCont);
674 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +0000675 sqliteVdbeResolveLabel(v, iBreak);
676 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000677 if( p->pOrderBy ){
678 generateSortTail(v, p->pEList->nExpr);
679 }
drh82c3d632000-06-06 21:56:07 +0000680 }
681 break;
682 }
683 case TK_INTERSECT: {
684 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +0000685 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000686
drhd8bc7082000-06-07 23:51:50 +0000687 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000688 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000689 ** by allocating the tables we will need.
690 */
drh82c3d632000-06-06 21:56:07 +0000691 tab1 = pParse->nTab++;
692 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000693 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
694 return 1;
695 }
drhc6b52df2002-01-04 03:09:29 +0000696 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +0000697 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000698
699 /* Code the SELECTs to our left into temporary table "tab1".
700 */
drh82c3d632000-06-06 21:56:07 +0000701 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
702 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000703
704 /* Code the current SELECT into temporary table "tab2"
705 */
drhc6b52df2002-01-04 03:09:29 +0000706 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +0000707 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000708 p->pPrior = 0;
709 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
710 p->pPrior = pPrior;
711 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000712
713 /* Generate code to take the intersection of the two temporary
714 ** tables.
715 */
drh82c3d632000-06-06 21:56:07 +0000716 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000717 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000718 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000719 iCont = sqliteVdbeMakeLabel(v);
720 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
721 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +0000722 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000723 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000724 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000725 iCont, iBreak);
726 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000727 sqliteVdbeResolveLabel(v, iCont);
728 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +0000729 sqliteVdbeResolveLabel(v, iBreak);
730 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
731 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000732 if( p->pOrderBy ){
733 generateSortTail(v, p->pEList->nExpr);
734 }
drh82c3d632000-06-06 21:56:07 +0000735 break;
736 }
737 }
738 assert( p->pEList && pPrior->pEList );
739 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000740 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
741 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000742 pParse->nErr++;
743 return 1;
drh22827922000-06-06 17:27:05 +0000744 }
drh10e5e3c2000-06-08 00:19:02 +0000745 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000746 return 0;
747}
748
749/*
drh1350b032002-02-27 19:00:20 +0000750** This routine attempts to flatten subqueries in order to speed
751** execution. It returns 1 if it makes changes and 0 if no flattening
752** occurs.
753**
754** To understand the concept of flattening, consider the following
755** query:
756**
757** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
758**
759** The default way of implementing this query is to execute the
760** subquery first and store the results in a temporary table, then
761** run the outer query on that temporary table. This requires two
762** passes over the data. Furthermore, because the temporary table
763** has no indices, the WHERE clause on the outer query cannot be
764** optimized using indices.
765**
766** This routine attempts to write queries such as the above into
767** a single flat select, like this:
768**
769** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
770**
771** The code generated for this simpification gives the same result
772** but only has to scan the data once.
773**
774** Generally speaking, flattening is only possible if the subquery
775** query is a simple query without a GROUP BY clause or the DISTINCT
776** keyword and the outer query is not a join.
777**
778** If flattening is not possible, this routine is a no-op and return 0.
779** If flattening is possible, this routine rewrites the query into
780** the simplified form and return 1.
781**
782** All of the expression analysis must occur before this routine runs.
783** This routine depends on the results of the expression analysis.
784*/
785int flattenSubqueries(Select *p){
786 Select *pSub;
787 if( p->pSrc->nId>1 ){
788 return 0; /* Cannot optimize: The outer query is a join. */
789 }
790 pSub = p->pSrc->a[0].pSelect;
791 if( pSub==0 ){
792 return 0; /* Nothing to optimize: There is no subquery. */
793 }
794 if( pSub->isDistinct ){
795 return 0; /* Subquery contains DISTINCT keyword */
796 }
797 if( pSub->pGroupBy ){
798 return 0; /* Subquery contains a GROUP BY clause */
799 }
800 if( pSub->pPrior ){
801 return 0; /* Subquery is the union of two or more queries */
802 }
803
804 return 0;
805}
806
807/*
drh9562b552002-02-19 15:00:07 +0000808** Analyze the SELECT statement passed in as an argument to see if it
809** is a simple min() or max() query. If it is and this query can be
810** satisfied using a single seek to the beginning or end of an index,
811** then generate the code for this SELECT return 1. If this is not a
812** simple min() or max() query, then return 0;
813**
814** A simply min() or max() query looks like this:
815**
816** SELECT min(a) FROM table;
817** SELECT max(a) FROM table;
818**
819** The query may have only a single table in its FROM argument. There
820** can be no GROUP BY or HAVING or WHERE clauses. The result set must
821** be the min() or max() of a single column of the table. The column
822** in the min() or max() function must be indexed.
823**
824** The parameters to this routine are the same as for sqliteSelect().
825** See the header comment on that routine for additional information.
826*/
827static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
828 Expr *pExpr;
829 int iCol;
830 Table *pTab;
831 Index *pIdx;
832 int base;
833 Vdbe *v;
834 int openOp;
835 int seekOp;
836 int cont;
837 ExprList eList;
838 struct ExprList_item eListItem;
839
840 /* Check to see if this query is a simple min() or max() query. Return
841 ** zero if it is not.
842 */
843 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
844 if( p->pSrc->nId!=1 ) return 0;
845 if( p->pEList->nExpr!=1 ) return 0;
846 pExpr = p->pEList->a[0].pExpr;
847 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
848 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh0bce8352002-02-28 00:41:10 +0000849 if( pExpr->token.n!=3 ) return 0;
850 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
851 seekOp = OP_Rewind;
852 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
853 seekOp = OP_Last;
854 }else{
855 return 0;
856 }
drh9562b552002-02-19 15:00:07 +0000857 pExpr = pExpr->pList->a[0].pExpr;
858 if( pExpr->op!=TK_COLUMN ) return 0;
859 iCol = pExpr->iColumn;
860 pTab = p->pSrc->a[0].pTab;
861
862 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +0000863 ** Check to make sure we have an index and make pIdx point to the
864 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
865 ** key column, no index is necessary so set pIdx to NULL. If no
866 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +0000867 */
868 if( iCol<0 ){
869 pIdx = 0;
870 }else{
871 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
872 assert( pIdx->nColumn>=1 );
873 if( pIdx->aiColumn[0]==iCol ) break;
874 }
875 if( pIdx==0 ) return 0;
876 }
877
drh17f71932002-02-21 12:01:27 +0000878 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +0000879 ** step is skipped if the output is going to a table or a memory cell.
880 */
881 v = sqliteGetVdbe(pParse);
882 if( v==0 ) return 0;
883 if( eDest==SRT_Callback ){
884 generateColumnNames(pParse, p->pSrc, p->pEList);
885 }
886
drh17f71932002-02-21 12:01:27 +0000887 /* Generating code to find the min or the max. Basically all we have
888 ** to do is find the first or the last entry in the chosen index. If
889 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
890 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +0000891 */
drh5cf8e8c2002-02-19 22:42:05 +0000892 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
893 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
894 pParse->schemaVerified = 1;
895 }
drh9562b552002-02-19 15:00:07 +0000896 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh5cf8e8c2002-02-19 22:42:05 +0000897 base = pParse->nTab;
drh9562b552002-02-19 15:00:07 +0000898 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +0000899 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +0000900 if( pIdx==0 ){
901 sqliteVdbeAddOp(v, seekOp, base, 0);
902 }else{
903 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +0000904 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +0000905 sqliteVdbeAddOp(v, seekOp, base+1, 0);
906 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
907 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
908 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
909 }
drh5cf8e8c2002-02-19 22:42:05 +0000910 eList.nExpr = 1;
911 memset(&eListItem, 0, sizeof(eListItem));
912 eList.a = &eListItem;
913 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +0000914 cont = sqliteVdbeMakeLabel(v);
915 selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont);
916 sqliteVdbeResolveLabel(v, cont);
917 sqliteVdbeAddOp(v, OP_Close, base, 0);
918 return 1;
919}
920
921/*
drh9bb61fe2000-06-05 16:01:39 +0000922** Generate code for the given SELECT statement.
923**
drhfef52082000-06-06 01:50:43 +0000924** The results are distributed in various ways depending on the
925** value of eDest and iParm.
926**
927** eDest Value Result
928** ------------ -------------------------------------------
929** SRT_Callback Invoke the callback for each row of the result.
930**
931** SRT_Mem Store first result in memory cell iParm
932**
933** SRT_Set Store results as keys of a table with cursor iParm
934**
drh82c3d632000-06-06 21:56:07 +0000935** SRT_Union Store results as a key in a temporary table iParm
936**
drhc4a3c772001-04-04 11:48:57 +0000937** SRT_Except Remove results form the temporary table iParm.
938**
939** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000940**
941** This routine returns the number of errors. If any errors are
942** encountered, then an appropriate error message is left in
943** pParse->zErrMsg.
944**
945** This routine does NOT free the Select structure passed in. The
946** calling function needs to do that.
947*/
948int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000949 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000950 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000951 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000952 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000953){
drhd8bc7082000-06-07 23:51:50 +0000954 int i;
drhcce7d172000-05-31 15:34:51 +0000955 WhereInfo *pWInfo;
956 Vdbe *v;
957 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +0000958 ExprList *pEList; /* List of columns to extract. */
drh9bb61fe2000-06-05 16:01:39 +0000959 IdList *pTabList; /* List of tables to select from */
960 Expr *pWhere; /* The WHERE clause. May be NULL */
961 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000962 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
963 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000964 int isDistinct; /* True if the DISTINCT keyword is present */
965 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000966 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +0000967 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +0000968
drhdaffd0e2001-04-11 14:28:42 +0000969 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
970
drh82c3d632000-06-06 21:56:07 +0000971 /* If there is are a sequence of queries, do the earlier ones first.
972 */
973 if( p->pPrior ){
974 return multiSelect(pParse, p, eDest, iParm);
975 }
976
977 /* Make local copies of the parameters for this query.
978 */
drh9bb61fe2000-06-05 16:01:39 +0000979 pTabList = p->pSrc;
980 pWhere = p->pWhere;
981 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000982 pGroupBy = p->pGroupBy;
983 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000984 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000985
drh10e5e3c2000-06-08 00:19:02 +0000986 /* Save the current value of pParse->nTab. Restore this value before
987 ** we exit.
988 */
989 base = pParse->nTab;
990
drh9bb61fe2000-06-05 16:01:39 +0000991 /*
992 ** Do not even attempt to generate any code if we have already seen
993 ** errors before this routine starts.
994 */
drh1d83f052002-02-17 00:30:36 +0000995 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +0000996
drhd8bc7082000-06-07 23:51:50 +0000997 /* Look up every table in the table list and create an appropriate
998 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000999 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001000 */
drhd8bc7082000-06-07 23:51:50 +00001001 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001002 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001003 }
drhd8bc7082000-06-07 23:51:50 +00001004 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001005 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001006
drh19a775c2000-06-05 18:54:46 +00001007 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +00001008 ** necessary. This must be done early to allocate the cursor before
1009 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +00001010 */
1011 if( isDistinct ){
1012 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +00001013 }else{
1014 distinct = -1;
drh19a775c2000-06-05 18:54:46 +00001015 }
1016
drh22827922000-06-06 17:27:05 +00001017 /* If writing to memory or generating a set
1018 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001019 */
drhfef52082000-06-06 01:50:43 +00001020 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001021 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1022 "a SELECT that is part of an expression", 0);
1023 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001024 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001025 }
1026
drh22827922000-06-06 17:27:05 +00001027 /* ORDER BY is ignored if we are not sending the result to a callback.
1028 */
1029 if( eDest!=SRT_Callback ){
1030 pOrderBy = 0;
1031 }
1032
1033 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +00001034 */
1035 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +00001036 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
1037 }
1038 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
1039 if( pOrderBy ){
1040 for(i=0; i<pOrderBy->nExpr; i++){
1041 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
1042 }
1043 }
drh22827922000-06-06 17:27:05 +00001044 if( pGroupBy ){
1045 for(i=0; i<pGroupBy->nExpr; i++){
1046 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
1047 }
1048 }
1049 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
1050
drh10e5e3c2000-06-08 00:19:02 +00001051 /* At this point, we should have allocated all the cursors that we
1052 ** need to handle subquerys and temporary tables. From here on we
1053 ** are committed to keeping the same value for pParse->nTab.
1054 **
drh967e8b72000-06-21 13:59:10 +00001055 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001056 */
drh4794b982000-06-06 13:54:14 +00001057 for(i=0; i<pEList->nExpr; i++){
drha2e00042002-01-22 03:13:42 +00001058 if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001059 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001060 }
drh22827922000-06-06 17:27:05 +00001061 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001062 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001063 }
1064 }
drhcce7d172000-05-31 15:34:51 +00001065 if( pWhere ){
drha2e00042002-01-22 03:13:42 +00001066 if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001067 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001068 }
1069 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001070 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001071 }
1072 }
1073 if( pOrderBy ){
1074 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001075 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001076 if( sqliteExprIsConstant(pE) ){
1077 sqliteSetString(&pParse->zErrMsg,
1078 "ORDER BY expressions should not be constant", 0);
1079 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001080 goto select_end;
drh92086432002-01-22 14:11:29 +00001081 }
drha2e00042002-01-22 03:13:42 +00001082 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001083 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001084 }
drh22827922000-06-06 17:27:05 +00001085 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001086 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001087 }
1088 }
1089 }
drh22827922000-06-06 17:27:05 +00001090 if( pGroupBy ){
1091 for(i=0; i<pGroupBy->nExpr; i++){
1092 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001093 if( sqliteExprIsConstant(pE) ){
1094 sqliteSetString(&pParse->zErrMsg,
1095 "GROUP BY expressions should not be constant", 0);
1096 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001097 goto select_end;
drh92086432002-01-22 14:11:29 +00001098 }
drha2e00042002-01-22 03:13:42 +00001099 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001100 goto select_end;
drh22827922000-06-06 17:27:05 +00001101 }
1102 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001103 goto select_end;
drh22827922000-06-06 17:27:05 +00001104 }
1105 }
1106 }
1107 if( pHaving ){
1108 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001109 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1110 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001111 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001112 goto select_end;
drh22827922000-06-06 17:27:05 +00001113 }
drha2e00042002-01-22 03:13:42 +00001114 if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001115 goto select_end;
drh22827922000-06-06 17:27:05 +00001116 }
drhda932812000-06-06 18:00:15 +00001117 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001118 goto select_end;
drh22827922000-06-06 17:27:05 +00001119 }
drhcce7d172000-05-31 15:34:51 +00001120 }
1121
drh1350b032002-02-27 19:00:20 +00001122 /* Try to merge subqueries in the FROM clause into the main
1123 ** query.
1124 */
1125 if( flattenSubqueries(p) ){
1126 pEList = p->pEList;
1127 pWhere = p->pWhere;
1128 }
1129
drh9562b552002-02-19 15:00:07 +00001130 /* Check for the special case of a min() or max() function by itself
1131 ** in the result set.
1132 */
1133 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001134 rc = 0;
drh9562b552002-02-19 15:00:07 +00001135 goto select_end;
1136 }
1137
drhd820cb12002-02-18 03:21:45 +00001138 /* Begin generating code.
1139 */
1140 v = sqliteGetVdbe(pParse);
1141 if( v==0 ) goto select_end;
1142
1143 /* Generate code for all sub-queries in the FROM clause
1144 */
1145 for(i=0; i<pTabList->nId; i++){
1146 int oldNTab;
drha76b5df2002-02-23 02:32:10 +00001147 if( pTabList->a[i].pSelect==0 ) continue;
drhd820cb12002-02-18 03:21:45 +00001148 oldNTab = pParse->nTab;
1149 pParse->nTab += i+1;
1150 sqliteVdbeAddOp(v, OP_OpenTemp, oldNTab+i, 0);
1151 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_Table, oldNTab+i);
1152 pParse->nTab = oldNTab;
1153 }
1154
drh22827922000-06-06 17:27:05 +00001155 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001156 */
drhd820cb12002-02-18 03:21:45 +00001157 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001158 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001159 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001160 for(i=0; i<pEList->nExpr; i++){
1161 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001162 goto select_end;
drh22827922000-06-06 17:27:05 +00001163 }
1164 }
1165 if( pGroupBy ){
1166 for(i=0; i<pGroupBy->nExpr; i++){
1167 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001168 goto select_end;
drh22827922000-06-06 17:27:05 +00001169 }
1170 }
1171 }
1172 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001173 goto select_end;
drh22827922000-06-06 17:27:05 +00001174 }
drh191b6902000-06-08 11:13:01 +00001175 if( pOrderBy ){
1176 for(i=0; i<pOrderBy->nExpr; i++){
1177 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001178 goto select_end;
drh191b6902000-06-08 11:13:01 +00001179 }
1180 }
1181 }
drhefb72512000-05-31 20:00:52 +00001182 }
1183
drh9bbca4c2001-11-06 04:00:18 +00001184 /* Set the limiter
1185 */
1186 if( p->nLimit<=0 ){
1187 p->nOffset = 0;
1188 }else{
1189 if( p->nOffset<0 ) p->nOffset = 0;
1190 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
1191 }
1192
1193
drh22827922000-06-06 17:27:05 +00001194 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +00001195 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +00001196 */
drhfef52082000-06-06 01:50:43 +00001197 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +00001198 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +00001199 }
1200
drh22827922000-06-06 17:27:05 +00001201 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001202 */
1203 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001204 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001205 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001206 FuncDef *pFunc;
1207 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001208 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001209 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001210 }
1211 }
drh1bee3d72001-10-15 00:44:35 +00001212 if( pGroupBy==0 ){
1213 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001214 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001215 }
drhcce7d172000-05-31 15:34:51 +00001216 }
1217
drh19a775c2000-06-05 18:54:46 +00001218 /* Initialize the memory cell to NULL
1219 */
drhfef52082000-06-06 01:50:43 +00001220 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001221 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001222 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001223 }
1224
drhcce7d172000-05-31 15:34:51 +00001225 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +00001226 */
drh19a775c2000-06-05 18:54:46 +00001227 if( isDistinct ){
drhc6b52df2002-01-04 03:09:29 +00001228 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drhefb72512000-05-31 20:00:52 +00001229 }
drhcce7d172000-05-31 15:34:51 +00001230 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh1d83f052002-02-17 00:30:36 +00001231 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001232
drh22827922000-06-06 17:27:05 +00001233 /* Use the standard inner loop if we are not dealing with
1234 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001235 */
drhda9d6c42000-05-31 18:20:14 +00001236 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +00001237 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001238 pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001239 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001240 }
drhcce7d172000-05-31 15:34:51 +00001241 }
drhefb72512000-05-31 20:00:52 +00001242
drh22827922000-06-06 17:27:05 +00001243 /* If we are dealing with aggregates, then to the special aggregate
1244 ** processing.
drhefb72512000-05-31 20:00:52 +00001245 */
drh22827922000-06-06 17:27:05 +00001246 else{
drh22827922000-06-06 17:27:05 +00001247 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00001248 int lbl1;
drh22827922000-06-06 17:27:05 +00001249 for(i=0; i<pGroupBy->nExpr; i++){
1250 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
1251 }
drh99fcd712001-10-13 01:06:47 +00001252 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +00001253 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001254 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00001255 for(i=0; i<pParse->nAgg; i++){
1256 if( pParse->aAgg[i].isAgg ) continue;
1257 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00001258 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00001259 }
drh22827922000-06-06 17:27:05 +00001260 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00001261 }
drh22827922000-06-06 17:27:05 +00001262 for(i=0; i<pParse->nAgg; i++){
1263 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00001264 int j;
drh22827922000-06-06 17:27:05 +00001265 if( !pParse->aAgg[i].isAgg ) continue;
1266 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00001267 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00001268 if( pE->pList ){
1269 for(j=0; j<pE->pList->nExpr; j++){
1270 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
1271 }
drhe5095352002-02-24 03:25:14 +00001272 }
drh0bce8352002-02-28 00:41:10 +00001273 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00001274 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00001275 assert( pParse->aAgg[i].pFunc!=0 );
1276 assert( pParse->aAgg[i].pFunc->xStep!=0 );
1277 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00001278 }
drhcce7d172000-05-31 15:34:51 +00001279 }
1280
1281 /* End the database scan loop.
1282 */
1283 sqliteWhereEnd(pWInfo);
1284
drh22827922000-06-06 17:27:05 +00001285 /* If we are processing aggregates, we need to set up a second loop
1286 ** over all of the aggregate values and process them.
1287 */
1288 if( isAgg ){
1289 int endagg = sqliteVdbeMakeLabel(v);
1290 int startagg;
drh99fcd712001-10-13 01:06:47 +00001291 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00001292 pParse->useAgg = 1;
1293 if( pHaving ){
1294 sqliteExprIfFalse(pParse, pHaving, startagg);
1295 }
drh82c3d632000-06-06 21:56:07 +00001296 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001297 startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00001298 goto select_end;
drh22827922000-06-06 17:27:05 +00001299 }
drh99fcd712001-10-13 01:06:47 +00001300 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
1301 sqliteVdbeResolveLabel(v, endagg);
1302 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00001303 pParse->useAgg = 0;
1304 }
1305
drhcce7d172000-05-31 15:34:51 +00001306 /* If there is an ORDER BY clause, then we need to sort the results
1307 ** and send them to the callback one by one.
1308 */
1309 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001310 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001311 }
drh10e5e3c2000-06-08 00:19:02 +00001312 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +00001313
1314
1315 /* Issue a null callback if that is what the user wants.
1316 */
1317 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1318 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1319 }
1320
drh1d83f052002-02-17 00:30:36 +00001321 /* The SELECT was successfully coded. Set the return code to 0
1322 ** to indicate no errors.
1323 */
1324 rc = 0;
1325
1326 /* Control jumps to here if an error is encountered above, or upon
1327 ** successful coding of the SELECT.
1328 */
1329select_end:
1330 sqliteAggregateInfoReset(pParse);
1331 return rc;
drhcce7d172000-05-31 15:34:51 +00001332}