blob: b5b88136897add9fb97fd0fcdb198ae65d7817f2 [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**
drhff78bd22002-02-27 01:47:11 +000015** $Id: select.c,v 1.67 2002/02/27 01:47:12 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;
81 pParse->iAggCount = -1;
82 pParse->useAgg = 0;
83}
84
85/*
86** This routine generates the code for the inside of the inner loop
87** of a SELECT.
drh82c3d632000-06-06 21:56:07 +000088**
89** The pEList is used to determine the values for each column in the
drh967e8b72000-06-21 13:59:10 +000090** result row. Except if pEList==NULL, then we just read nColumn
drh82c3d632000-06-06 21:56:07 +000091** elements from the srcTab table.
drh22827922000-06-06 17:27:05 +000092*/
93static int selectInnerLoop(
94 Parse *pParse, /* The parser context */
95 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +000096 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +000097 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +000098 ExprList *pOrderBy, /* If not NULL, sort results using this key */
99 int distinct, /* If >=0, make sure results are distinct */
100 int eDest, /* How to dispose of the results */
101 int iParm, /* An argument to the disposal method */
102 int iContinue, /* Jump here to continue with next row */
103 int iBreak /* Jump here to break out of the inner loop */
104){
105 Vdbe *v = pParse->pVdbe;
106 int i;
drhdaffd0e2001-04-11 14:28:42 +0000107 if( v==0 ) return 0;
drh22827922000-06-06 17:27:05 +0000108
drh967e8b72000-06-21 13:59:10 +0000109 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000110 */
drh82c3d632000-06-06 21:56:07 +0000111 if( pEList ){
112 for(i=0; i<pEList->nExpr; i++){
113 sqliteExprCode(pParse, pEList->a[i].pExpr);
114 }
drh967e8b72000-06-21 13:59:10 +0000115 nColumn = pEList->nExpr;
drh82c3d632000-06-06 21:56:07 +0000116 }else{
drh967e8b72000-06-21 13:59:10 +0000117 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000118 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000119 }
drh22827922000-06-06 17:27:05 +0000120 }
121
drhdaffd0e2001-04-11 14:28:42 +0000122 /* If the DISTINCT keyword was present on the SELECT statement
123 ** and this row has been seen before, then do not make this row
124 ** part of the result.
drh22827922000-06-06 17:27:05 +0000125 */
126 if( distinct>=0 ){
127 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000128 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
129 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl);
130 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
131 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
132 sqliteVdbeResolveLabel(v, lbl);
133 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000134 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
drh22827922000-06-06 17:27:05 +0000135 }
drh82c3d632000-06-06 21:56:07 +0000136
drh22827922000-06-06 17:27:05 +0000137 /* If there is an ORDER BY clause, then store the results
138 ** in a sorter.
139 */
140 if( pOrderBy ){
141 char *zSortOrder;
drh99fcd712001-10-13 01:06:47 +0000142 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
drh22827922000-06-06 17:27:05 +0000143 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
144 if( zSortOrder==0 ) return 1;
145 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000146 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000147 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
148 }
149 zSortOrder[pOrderBy->nExpr] = 0;
drh99fcd712001-10-13 01:06:47 +0000150 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
151 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
drh6e142f52000-06-08 13:36:40 +0000152 sqliteFree(zSortOrder);
drh99fcd712001-10-13 01:06:47 +0000153 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
drh22827922000-06-06 17:27:05 +0000154 }else
155
drh82c3d632000-06-06 21:56:07 +0000156 /* In this mode, write each query result to the key of the temporary
157 ** table iParm.
drh22827922000-06-06 17:27:05 +0000158 */
drh82c3d632000-06-06 21:56:07 +0000159 if( eDest==SRT_Union ){
drh99fcd712001-10-13 01:06:47 +0000160 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
161 sqliteVdbeAddOp(v, OP_String, iParm, 0);
drh6b125452002-01-28 15:53:03 +0000162 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
drh82c3d632000-06-06 21:56:07 +0000163 }else
164
drh5974a302000-06-07 14:42:26 +0000165 /* Store the result as data using a unique key.
166 */
167 if( eDest==SRT_Table ){
drh99fcd712001-10-13 01:06:47 +0000168 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
169 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
170 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh6b125452002-01-28 15:53:03 +0000171 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
drh5974a302000-06-07 14:42:26 +0000172 }else
173
drh82c3d632000-06-06 21:56:07 +0000174 /* Construct a record from the query result, but instead of
175 ** saving that record, use it as a key to delete elements from
176 ** the temporary table iParm.
177 */
178 if( eDest==SRT_Except ){
drh99fcd712001-10-13 01:06:47 +0000179 int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
180 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
181 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
drh22827922000-06-06 17:27:05 +0000182 }else
183
184 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
185 ** then there should be a single item on the stack. Write this
186 ** item into the set table with bogus data.
187 */
188 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000189 assert( nColumn==1 );
drh99fcd712001-10-13 01:06:47 +0000190 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000191 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
drh22827922000-06-06 17:27:05 +0000192 }else
193
drh82c3d632000-06-06 21:56:07 +0000194
drh22827922000-06-06 17:27:05 +0000195 /* If this is a scalar select that is part of an expression, then
196 ** store the results in the appropriate memory cell and break out
197 ** of the scan loop.
198 */
199 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000200 assert( nColumn==1 );
drh8721ce42001-11-07 14:22:00 +0000201 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh99fcd712001-10-13 01:06:47 +0000202 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
drh22827922000-06-06 17:27:05 +0000203 }else
204
205 /* If none of the above, send the data to the callback function.
206 */
207 {
drh9bbca4c2001-11-06 04:00:18 +0000208 sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak);
drh82c3d632000-06-06 21:56:07 +0000209 }
210 return 0;
211}
212
213/*
drhd8bc7082000-06-07 23:51:50 +0000214** If the inner loop was generated using a non-null pOrderBy argument,
215** then the results were placed in a sorter. After the loop is terminated
216** we need to run the sorter and output the results. The following
217** routine generates the code needed to do that.
218*/
drh967e8b72000-06-21 13:59:10 +0000219static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000220 int end = sqliteVdbeMakeLabel(v);
221 int addr;
drh99fcd712001-10-13 01:06:47 +0000222 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
223 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drh9bbca4c2001-11-06 04:00:18 +0000224 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end);
drh99fcd712001-10-13 01:06:47 +0000225 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
226 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000227 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000228}
229
230/*
drh82c3d632000-06-06 21:56:07 +0000231** Generate code that will tell the VDBE how many columns there
232** are in the result and the name for each column. This information
233** is used to provide "argc" and "azCol[]" values in the callback.
234*/
drhd8bc7082000-06-07 23:51:50 +0000235static
236void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
237 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000238 int i;
drhdaffd0e2001-04-11 14:28:42 +0000239 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000240 pParse->colNamesSet = 1;
drh99fcd712001-10-13 01:06:47 +0000241 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
drh82c3d632000-06-06 21:56:07 +0000242 for(i=0; i<pEList->nExpr; i++){
243 Expr *p;
drh1bee3d72001-10-15 00:44:35 +0000244 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000245 if( pEList->a[i].zName ){
246 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000247 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
248 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000249 continue;
250 }
251 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000252 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000253 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
254 if( p->span.z && p->span.z[0] && !showFullNames ){
drh99fcd712001-10-13 01:06:47 +0000255 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
256 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhe1b6a5b2000-07-29 13:06:59 +0000257 sqliteVdbeCompressSpace(v, addr);
drh1bee3d72001-10-15 00:44:35 +0000258 }else if( p->op==TK_COLUMN && pTabList ){
drh8aff1012001-12-22 14:49:24 +0000259 Table *pTab = pTabList->a[p->iTable - pParse->nTab].pTab;
drh97665872002-02-13 23:22:53 +0000260 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000261 int iCol = p->iColumn;
262 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000263 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
264 zCol = iCol<0 ? "_ROWID_" : pTab->aCol[iCol].zName;
drh1bee3d72001-10-15 00:44:35 +0000265 if( pTabList->nId>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000266 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000267 char *zTab;
268
drh98808ba2001-10-18 12:34:46 +0000269 zTab = pTabList->a[p->iTable - pParse->nTab].zAlias;
drh01a34662001-10-20 12:30:10 +0000270 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000271 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000272 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
273 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000274 sqliteFree(zName);
275 }else{
drh99fcd712001-10-13 01:06:47 +0000276 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000277 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000278 }
drh1bee3d72001-10-15 00:44:35 +0000279 }else if( p->span.z && p->span.z[0] ){
280 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
281 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
282 sqliteVdbeCompressSpace(v, addr);
283 }else{
284 char zName[30];
285 assert( p->op!=TK_COLUMN || pTabList==0 );
286 sprintf(zName, "column%d", i+1);
287 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
288 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000289 }
290 }
291}
292
293/*
drhd8bc7082000-06-07 23:51:50 +0000294** Name of the connection operator, used for error messages.
295*/
296static const char *selectOpName(int id){
297 char *z;
298 switch( id ){
299 case TK_ALL: z = "UNION ALL"; break;
300 case TK_INTERSECT: z = "INTERSECT"; break;
301 case TK_EXCEPT: z = "EXCEPT"; break;
302 default: z = "UNION"; break;
303 }
304 return z;
305}
306
307/*
drh22f70c32002-02-18 01:17:00 +0000308** Given a SELECT statement, generate a Table structure that describes
309** the result set of that SELECT.
310*/
311Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
312 Table *pTab;
313 int i;
314 ExprList *pEList;
315 static int fillInColumnList(Parse*, Select*);
316
317 if( fillInColumnList(pParse, pSelect) ){
318 return 0;
319 }
320 pTab = sqliteMalloc( sizeof(Table) );
321 if( pTab==0 ){
322 return 0;
323 }
324 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
325 pEList = pSelect->pEList;
326 pTab->nCol = pEList->nExpr;
327 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
328 for(i=0; i<pTab->nCol; i++){
329 Expr *p;
330 if( pEList->a[i].zName ){
331 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
332 }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
333 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
drhd820cb12002-02-18 03:21:45 +0000334 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
335 p->pRight->token.z[0] ){
336 sqliteSetNString(&pTab->aCol[i].zName,
337 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000338 }else{
339 char zBuf[30];
340 sprintf(zBuf, "column%d", i+1);
341 pTab->aCol[i].zName = sqliteStrDup(zBuf);
342 }
343 }
344 pTab->iPKey = -1;
345 return pTab;
346}
347
348/*
drhd8bc7082000-06-07 23:51:50 +0000349** For the given SELECT statement, do two things.
350**
drh967e8b72000-06-21 13:59:10 +0000351** (1) Fill in the pTabList->a[].pTab fields in the IdList that
drh22f70c32002-02-18 01:17:00 +0000352** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000353**
354** (2) If the columns to be extracted variable (pEList) is NULL
355** (meaning that a "*" was used in the SQL statement) then
356** create a fake pEList containing the names of all columns
357** of all tables.
358**
359** Return 0 on success. If there are problems, leave an error message
360** in pParse and return non-zero.
361*/
362static int fillInColumnList(Parse *pParse, Select *p){
drh7c917d12001-12-16 20:05:05 +0000363 int i, j, k;
drhdaffd0e2001-04-11 14:28:42 +0000364 IdList *pTabList;
365 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000366 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000367
368 if( p==0 || p->pSrc==0 ) return 1;
369 pTabList = p->pSrc;
370 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000371
372 /* Look up every table in the table list.
373 */
374 for(i=0; i<pTabList->nId; i++){
375 if( pTabList->a[i].pTab ){
376 /* This routine has run before! No need to continue */
377 return 0;
378 }
drhdaffd0e2001-04-11 14:28:42 +0000379 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000380 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000381 assert( pTabList->a[i].pSelect!=0 );
382 pTabList->a[i].pTab = pTab =
383 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
384 pTabList->a[i].pSelect);
385 if( pTab==0 ){
386 return 1;
387 }
388 pTab->isTransient = 1;
389 }else{
drha76b5df2002-02-23 02:32:10 +0000390 /* An ordinary table or view name in the FROM clause */
391 pTabList->a[i].pTab = pTab =
392 sqliteFindTable(pParse->db, pTabList->a[i].zName);
393 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000394 sqliteSetString(&pParse->zErrMsg, "no such table: ",
395 pTabList->a[i].zName, 0);
396 pParse->nErr++;
397 return 1;
398 }
drha76b5df2002-02-23 02:32:10 +0000399 if( pTab->pSelect ){
drhff78bd22002-02-27 01:47:11 +0000400 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000401 }
drhd8bc7082000-06-07 23:51:50 +0000402 }
403 }
404
drh7c917d12001-12-16 20:05:05 +0000405 /* For every "*" that occurs in the column list, insert the names of
406 ** all columns in all tables. The parser inserted a special expression
407 ** with the TK_ALL operator for each "*" that it found in the column list.
408 ** The following code just has to locate the TK_ALL expressions and expand
409 ** each one to the list of all columns in all tables.
drhd8bc7082000-06-07 23:51:50 +0000410 */
drh7c917d12001-12-16 20:05:05 +0000411 for(k=0; k<pEList->nExpr; k++){
412 if( pEList->a[k].pExpr->op==TK_ALL ) break;
413 }
414 if( k<pEList->nExpr ){
415 struct ExprList_item *a = pEList->a;
416 ExprList *pNew = 0;
417 for(k=0; k<pEList->nExpr; k++){
418 if( a[k].pExpr->op!=TK_ALL ){
419 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
420 pNew->a[pNew->nExpr-1].zName = a[k].zName;
421 a[k].pExpr = 0;
422 a[k].zName = 0;
423 }else{
424 for(i=0; i<pTabList->nId; i++){
425 Table *pTab = pTabList->a[i].pTab;
426 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000427 Expr *pExpr, *pLeft, *pRight;
428 pRight = sqliteExpr(TK_ID, 0, 0, 0);
429 if( pRight==0 ) break;
430 pRight->token.z = pTab->aCol[j].zName;
431 pRight->token.n = strlen(pTab->aCol[j].zName);
432 if( pTab->zName ){
433 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
434 if( pLeft==0 ) break;
435 if( pTabList->a[i].zAlias && pTabList->a[i].zAlias[0] ){
436 pLeft->token.z = pTabList->a[i].zAlias;
437 pLeft->token.n = strlen(pTabList->a[i].zAlias);
438 }else{
439 pLeft->token.z = pTab->zName;
440 pLeft->token.n = strlen(pTab->zName);
441 }
442 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
443 if( pExpr==0 ) break;
drh7c917d12001-12-16 20:05:05 +0000444 }else{
drh22f70c32002-02-18 01:17:00 +0000445 pExpr = pRight;
446 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +0000447 }
drh7c917d12001-12-16 20:05:05 +0000448 pNew = sqliteExprListAppend(pNew, pExpr, 0);
449 }
drh17e24df2001-11-06 14:10:41 +0000450 }
drhd8bc7082000-06-07 23:51:50 +0000451 }
452 }
drh7c917d12001-12-16 20:05:05 +0000453 sqliteExprListDelete(pEList);
454 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000455 }
456 return 0;
457}
458
459/*
drhff78bd22002-02-27 01:47:11 +0000460** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
461** in a select structure. It just sets the pointers to NULL. This
462** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
463** pointer is not NULL, this routine is called recursively on that pointer.
464**
465** This routine is called on the Select structure that defines a
466** VIEW in order to undo any bindings to tables. This is necessary
467** because those tables might be DROPed by a subsequent SQL command.
468*/
469void sqliteSelectUnbind(Select *p){
470 int i;
471 IdList *pSrc = p->pSrc;
472 Table *pTab;
473 if( p==0 ) return;
474 for(i=0; i<pSrc->nId; i++){
475 if( (pTab = pSrc->a[i].pTab)!=0 ){
476 if( pTab->isTransient ){
477 sqliteDeleteTable(0, pTab);
478 sqliteSelectDelete(pSrc->a[i].pSelect);
479 pSrc->a[i].pSelect = 0;
480 }
481 pSrc->a[i].pTab = 0;
482 if( pSrc->a[i].pSelect ){
483 sqliteSelectUnbind(pSrc->a[i].pSelect);
484 }
485 }
486 }
487}
488
489/*
drhd8bc7082000-06-07 23:51:50 +0000490** This routine associates entries in an ORDER BY expression list with
491** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000492** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000493** the top-level node is filled in with column number and the iTable
494** value of the top-level node is filled with iTable parameter.
495**
496** If there are prior SELECT clauses, they are processed first. A match
497** in an earlier SELECT takes precedence over a later SELECT.
498**
499** Any entry that does not match is flagged as an error. The number
500** of errors is returned.
501*/
502static int matchOrderbyToColumn(
503 Parse *pParse, /* A place to leave error messages */
504 Select *pSelect, /* Match to result columns of this SELECT */
505 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
506 int iTable, /* Insert this this value in iTable */
507 int mustComplete /* If TRUE all ORDER BYs must match */
508){
509 int nErr = 0;
510 int i, j;
511 ExprList *pEList;
512
drhdaffd0e2001-04-11 14:28:42 +0000513 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000514 if( mustComplete ){
515 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
516 }
517 if( fillInColumnList(pParse, pSelect) ){
518 return 1;
519 }
520 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000521 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
522 return 1;
523 }
drhd8bc7082000-06-07 23:51:50 +0000524 }
525 pEList = pSelect->pEList;
526 for(i=0; i<pOrderBy->nExpr; i++){
527 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000528 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000529 if( pOrderBy->a[i].done ) continue;
530 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000531 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +0000532 char *zName, *zLabel;
533 zName = pEList->a[j].zName;
534 assert( pE->token.z );
535 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000536 sqliteDequote(zLabel);
537 if( sqliteStrICmp(zName, zLabel)==0 ){
538 match = 1;
539 }
drh6e142f52000-06-08 13:36:40 +0000540 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000541 }
drh4cfa7932000-06-08 15:10:46 +0000542 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000543 match = 1;
544 }
545 if( match ){
drh967e8b72000-06-21 13:59:10 +0000546 pE->op = TK_COLUMN;
547 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000548 pE->iTable = iTable;
549 pOrderBy->a[i].done = 1;
550 break;
551 }
552 }
drh92cd52f2000-06-08 01:55:29 +0000553 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000554 char zBuf[30];
555 sprintf(zBuf,"%d",i+1);
556 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
557 " does not match any result column", 0);
558 pParse->nErr++;
559 nErr++;
560 break;
561 }
562 }
563 return nErr;
564}
565
566/*
567** Get a VDBE for the given parser context. Create a new one if necessary.
568** If an error occurs, return NULL and leave a message in pParse.
569*/
570Vdbe *sqliteGetVdbe(Parse *pParse){
571 Vdbe *v = pParse->pVdbe;
572 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000573 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000574 }
drhd8bc7082000-06-07 23:51:50 +0000575 return v;
576}
577
578
579/*
drh82c3d632000-06-06 21:56:07 +0000580** This routine is called to process a query that is really the union
581** or intersection of two or more separate queries.
582*/
583static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000584 int rc; /* Success code from a subroutine */
585 Select *pPrior; /* Another SELECT immediately to our left */
586 Vdbe *v; /* Generate code to this VDBE */
587 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000588
drhd8bc7082000-06-07 23:51:50 +0000589 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
590 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000591 */
drhdaffd0e2001-04-11 14:28:42 +0000592 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000593 pPrior = p->pPrior;
594 if( pPrior->pOrderBy ){
595 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
596 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000597 pParse->nErr++;
598 return 1;
599 }
600
drhd8bc7082000-06-07 23:51:50 +0000601 /* Make sure we have a valid query engine. If not, create a new one.
602 */
603 v = sqliteGetVdbe(pParse);
604 if( v==0 ) return 1;
605
606 /* Process the UNION or INTERSECTION
607 */
drh10e5e3c2000-06-08 00:19:02 +0000608 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000609 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000610 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000611 case TK_EXCEPT:
612 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000613 int unionTab; /* Cursor number of the temporary table holding result */
614 int op; /* One of the SRT_ operations to apply to self */
615 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000616
drhd8bc7082000-06-07 23:51:50 +0000617 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
618 if( eDest==priorOp ){
619 /* We can reuse a temporary table generated by a SELECT to our
620 ** right. This also means we are not the right-most select and so
621 ** we cannot have an ORDER BY clause
622 */
drh82c3d632000-06-06 21:56:07 +0000623 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000624 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000625 }else{
drhd8bc7082000-06-07 23:51:50 +0000626 /* We will need to create our own temporary table to hold the
627 ** intermediate results.
628 */
629 unionTab = pParse->nTab++;
630 if( p->pOrderBy
631 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
632 return 1;
633 }
drhd8bc7082000-06-07 23:51:50 +0000634 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +0000635 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +0000636 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000637 }else{
drh99fcd712001-10-13 01:06:47 +0000638 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000639 }
drh82c3d632000-06-06 21:56:07 +0000640 }
drhd8bc7082000-06-07 23:51:50 +0000641
642 /* Code the SELECT statements to our left
643 */
644 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000645 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000646
647 /* Code the current SELECT statement
648 */
649 switch( p->op ){
650 case TK_EXCEPT: op = SRT_Except; break;
651 case TK_UNION: op = SRT_Union; break;
652 case TK_ALL: op = SRT_Table; break;
653 }
drh82c3d632000-06-06 21:56:07 +0000654 p->pPrior = 0;
655 rc = sqliteSelect(pParse, p, op, unionTab);
656 p->pPrior = pPrior;
657 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000658
659 /* Convert the data in the temporary table into whatever form
660 ** it is that we currently need.
661 */
662 if( eDest!=priorOp ){
drh6b563442001-11-07 16:48:26 +0000663 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000664 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000665 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000666 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000667 iCont = sqliteVdbeMakeLabel(v);
668 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
669 iStart = sqliteVdbeCurrentAddr(v);
drh82c3d632000-06-06 21:56:07 +0000670 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000671 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000672 iCont, iBreak);
673 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000674 sqliteVdbeResolveLabel(v, iCont);
675 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +0000676 sqliteVdbeResolveLabel(v, iBreak);
677 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000678 if( p->pOrderBy ){
679 generateSortTail(v, p->pEList->nExpr);
680 }
drh82c3d632000-06-06 21:56:07 +0000681 }
682 break;
683 }
684 case TK_INTERSECT: {
685 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +0000686 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000687
drhd8bc7082000-06-07 23:51:50 +0000688 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000689 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000690 ** by allocating the tables we will need.
691 */
drh82c3d632000-06-06 21:56:07 +0000692 tab1 = pParse->nTab++;
693 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000694 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
695 return 1;
696 }
drhc6b52df2002-01-04 03:09:29 +0000697 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +0000698 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000699
700 /* Code the SELECTs to our left into temporary table "tab1".
701 */
drh82c3d632000-06-06 21:56:07 +0000702 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
703 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000704
705 /* Code the current SELECT into temporary table "tab2"
706 */
drhc6b52df2002-01-04 03:09:29 +0000707 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +0000708 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000709 p->pPrior = 0;
710 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
711 p->pPrior = pPrior;
712 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000713
714 /* Generate code to take the intersection of the two temporary
715 ** tables.
716 */
drh82c3d632000-06-06 21:56:07 +0000717 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000718 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000719 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000720 iCont = sqliteVdbeMakeLabel(v);
721 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
722 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +0000723 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000724 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000725 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000726 iCont, iBreak);
727 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000728 sqliteVdbeResolveLabel(v, iCont);
729 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +0000730 sqliteVdbeResolveLabel(v, iBreak);
731 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
732 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000733 if( p->pOrderBy ){
734 generateSortTail(v, p->pEList->nExpr);
735 }
drh82c3d632000-06-06 21:56:07 +0000736 break;
737 }
738 }
739 assert( p->pEList && pPrior->pEList );
740 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000741 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
742 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000743 pParse->nErr++;
744 return 1;
drh22827922000-06-06 17:27:05 +0000745 }
drh10e5e3c2000-06-08 00:19:02 +0000746 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000747 return 0;
748}
749
750/*
drh9562b552002-02-19 15:00:07 +0000751** Analyze the SELECT statement passed in as an argument to see if it
752** is a simple min() or max() query. If it is and this query can be
753** satisfied using a single seek to the beginning or end of an index,
754** then generate the code for this SELECT return 1. If this is not a
755** simple min() or max() query, then return 0;
756**
757** A simply min() or max() query looks like this:
758**
759** SELECT min(a) FROM table;
760** SELECT max(a) FROM table;
761**
762** The query may have only a single table in its FROM argument. There
763** can be no GROUP BY or HAVING or WHERE clauses. The result set must
764** be the min() or max() of a single column of the table. The column
765** in the min() or max() function must be indexed.
766**
767** The parameters to this routine are the same as for sqliteSelect().
768** See the header comment on that routine for additional information.
769*/
770static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
771 Expr *pExpr;
772 int iCol;
773 Table *pTab;
774 Index *pIdx;
775 int base;
776 Vdbe *v;
777 int openOp;
778 int seekOp;
779 int cont;
780 ExprList eList;
781 struct ExprList_item eListItem;
782
783 /* Check to see if this query is a simple min() or max() query. Return
784 ** zero if it is not.
785 */
786 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
787 if( p->pSrc->nId!=1 ) return 0;
788 if( p->pEList->nExpr!=1 ) return 0;
789 pExpr = p->pEList->a[0].pExpr;
790 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
791 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
792 if( pExpr->iColumn!=FN_Min && pExpr->iColumn!=FN_Max ) return 0;
793 seekOp = pExpr->iColumn==FN_Min ? OP_Rewind : OP_Last;
794 pExpr = pExpr->pList->a[0].pExpr;
795 if( pExpr->op!=TK_COLUMN ) return 0;
796 iCol = pExpr->iColumn;
797 pTab = p->pSrc->a[0].pTab;
798
799 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +0000800 ** Check to make sure we have an index and make pIdx point to the
801 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
802 ** key column, no index is necessary so set pIdx to NULL. If no
803 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +0000804 */
805 if( iCol<0 ){
806 pIdx = 0;
807 }else{
808 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
809 assert( pIdx->nColumn>=1 );
810 if( pIdx->aiColumn[0]==iCol ) break;
811 }
812 if( pIdx==0 ) return 0;
813 }
814
drh17f71932002-02-21 12:01:27 +0000815 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +0000816 ** step is skipped if the output is going to a table or a memory cell.
817 */
818 v = sqliteGetVdbe(pParse);
819 if( v==0 ) return 0;
820 if( eDest==SRT_Callback ){
821 generateColumnNames(pParse, p->pSrc, p->pEList);
822 }
823
drh17f71932002-02-21 12:01:27 +0000824 /* Generating code to find the min or the max. Basically all we have
825 ** to do is find the first or the last entry in the chosen index. If
826 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
827 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +0000828 */
drh5cf8e8c2002-02-19 22:42:05 +0000829 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
830 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
831 pParse->schemaVerified = 1;
832 }
drh9562b552002-02-19 15:00:07 +0000833 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh5cf8e8c2002-02-19 22:42:05 +0000834 base = pParse->nTab;
drh9562b552002-02-19 15:00:07 +0000835 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +0000836 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +0000837 if( pIdx==0 ){
838 sqliteVdbeAddOp(v, seekOp, base, 0);
839 }else{
840 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +0000841 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +0000842 sqliteVdbeAddOp(v, seekOp, base+1, 0);
843 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
844 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
845 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
846 }
drh5cf8e8c2002-02-19 22:42:05 +0000847 eList.nExpr = 1;
848 memset(&eListItem, 0, sizeof(eListItem));
849 eList.a = &eListItem;
850 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +0000851 cont = sqliteVdbeMakeLabel(v);
852 selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont);
853 sqliteVdbeResolveLabel(v, cont);
854 sqliteVdbeAddOp(v, OP_Close, base, 0);
855 return 1;
856}
857
858/*
drh9bb61fe2000-06-05 16:01:39 +0000859** Generate code for the given SELECT statement.
860**
drhfef52082000-06-06 01:50:43 +0000861** The results are distributed in various ways depending on the
862** value of eDest and iParm.
863**
864** eDest Value Result
865** ------------ -------------------------------------------
866** SRT_Callback Invoke the callback for each row of the result.
867**
868** SRT_Mem Store first result in memory cell iParm
869**
870** SRT_Set Store results as keys of a table with cursor iParm
871**
drh82c3d632000-06-06 21:56:07 +0000872** SRT_Union Store results as a key in a temporary table iParm
873**
drhc4a3c772001-04-04 11:48:57 +0000874** SRT_Except Remove results form the temporary table iParm.
875**
876** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000877**
878** This routine returns the number of errors. If any errors are
879** encountered, then an appropriate error message is left in
880** pParse->zErrMsg.
881**
882** This routine does NOT free the Select structure passed in. The
883** calling function needs to do that.
884*/
885int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000886 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000887 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000888 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000889 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000890){
drhd8bc7082000-06-07 23:51:50 +0000891 int i;
drhcce7d172000-05-31 15:34:51 +0000892 WhereInfo *pWInfo;
893 Vdbe *v;
894 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +0000895 ExprList *pEList; /* List of columns to extract. */
drh9bb61fe2000-06-05 16:01:39 +0000896 IdList *pTabList; /* List of tables to select from */
897 Expr *pWhere; /* The WHERE clause. May be NULL */
898 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000899 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
900 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000901 int isDistinct; /* True if the DISTINCT keyword is present */
902 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000903 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +0000904 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +0000905
drhdaffd0e2001-04-11 14:28:42 +0000906 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
907
drh82c3d632000-06-06 21:56:07 +0000908 /* If there is are a sequence of queries, do the earlier ones first.
909 */
910 if( p->pPrior ){
911 return multiSelect(pParse, p, eDest, iParm);
912 }
913
914 /* Make local copies of the parameters for this query.
915 */
drh9bb61fe2000-06-05 16:01:39 +0000916 pTabList = p->pSrc;
917 pWhere = p->pWhere;
918 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000919 pGroupBy = p->pGroupBy;
920 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000921 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000922
drh10e5e3c2000-06-08 00:19:02 +0000923 /* Save the current value of pParse->nTab. Restore this value before
924 ** we exit.
925 */
926 base = pParse->nTab;
927
drh9bb61fe2000-06-05 16:01:39 +0000928 /*
929 ** Do not even attempt to generate any code if we have already seen
930 ** errors before this routine starts.
931 */
drh1d83f052002-02-17 00:30:36 +0000932 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +0000933
drhd8bc7082000-06-07 23:51:50 +0000934 /* Look up every table in the table list and create an appropriate
935 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000936 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000937 */
drhd8bc7082000-06-07 23:51:50 +0000938 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +0000939 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000940 }
drhd8bc7082000-06-07 23:51:50 +0000941 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +0000942 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +0000943
drh19a775c2000-06-05 18:54:46 +0000944 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000945 ** necessary. This must be done early to allocate the cursor before
946 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000947 */
948 if( isDistinct ){
949 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000950 }else{
951 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000952 }
953
drh22827922000-06-06 17:27:05 +0000954 /* If writing to memory or generating a set
955 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000956 */
drhfef52082000-06-06 01:50:43 +0000957 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000958 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
959 "a SELECT that is part of an expression", 0);
960 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +0000961 goto select_end;
drh19a775c2000-06-05 18:54:46 +0000962 }
963
drh22827922000-06-06 17:27:05 +0000964 /* ORDER BY is ignored if we are not sending the result to a callback.
965 */
966 if( eDest!=SRT_Callback ){
967 pOrderBy = 0;
968 }
969
970 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000971 */
972 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000973 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
974 }
975 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
976 if( pOrderBy ){
977 for(i=0; i<pOrderBy->nExpr; i++){
978 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
979 }
980 }
drh22827922000-06-06 17:27:05 +0000981 if( pGroupBy ){
982 for(i=0; i<pGroupBy->nExpr; i++){
983 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
984 }
985 }
986 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
987
drh10e5e3c2000-06-08 00:19:02 +0000988 /* At this point, we should have allocated all the cursors that we
989 ** need to handle subquerys and temporary tables. From here on we
990 ** are committed to keeping the same value for pParse->nTab.
991 **
drh967e8b72000-06-21 13:59:10 +0000992 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000993 */
drh4794b982000-06-06 13:54:14 +0000994 for(i=0; i<pEList->nExpr; i++){
drha2e00042002-01-22 03:13:42 +0000995 if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +0000996 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000997 }
drh22827922000-06-06 17:27:05 +0000998 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +0000999 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001000 }
1001 }
drhcce7d172000-05-31 15:34:51 +00001002 if( pWhere ){
drha2e00042002-01-22 03:13:42 +00001003 if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001004 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001005 }
1006 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001007 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001008 }
1009 }
1010 if( pOrderBy ){
1011 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001012 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001013 if( sqliteExprIsConstant(pE) ){
1014 sqliteSetString(&pParse->zErrMsg,
1015 "ORDER BY expressions should not be constant", 0);
1016 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001017 goto select_end;
drh92086432002-01-22 14:11:29 +00001018 }
drha2e00042002-01-22 03:13:42 +00001019 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001020 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001021 }
drh22827922000-06-06 17:27:05 +00001022 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001023 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001024 }
1025 }
1026 }
drh22827922000-06-06 17:27:05 +00001027 if( pGroupBy ){
1028 for(i=0; i<pGroupBy->nExpr; i++){
1029 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001030 if( sqliteExprIsConstant(pE) ){
1031 sqliteSetString(&pParse->zErrMsg,
1032 "GROUP BY expressions should not be constant", 0);
1033 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001034 goto select_end;
drh92086432002-01-22 14:11:29 +00001035 }
drha2e00042002-01-22 03:13:42 +00001036 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001037 goto select_end;
drh22827922000-06-06 17:27:05 +00001038 }
1039 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001040 goto select_end;
drh22827922000-06-06 17:27:05 +00001041 }
1042 }
1043 }
1044 if( pHaving ){
1045 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001046 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1047 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001048 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001049 goto select_end;
drh22827922000-06-06 17:27:05 +00001050 }
drha2e00042002-01-22 03:13:42 +00001051 if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001052 goto select_end;
drh22827922000-06-06 17:27:05 +00001053 }
drhda932812000-06-06 18:00:15 +00001054 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001055 goto select_end;
drh22827922000-06-06 17:27:05 +00001056 }
drhcce7d172000-05-31 15:34:51 +00001057 }
1058
drh9562b552002-02-19 15:00:07 +00001059 /* Check for the special case of a min() or max() function by itself
1060 ** in the result set.
1061 */
1062 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001063 rc = 0;
drh9562b552002-02-19 15:00:07 +00001064 goto select_end;
1065 }
1066
drhd820cb12002-02-18 03:21:45 +00001067 /* Begin generating code.
1068 */
1069 v = sqliteGetVdbe(pParse);
1070 if( v==0 ) goto select_end;
1071
1072 /* Generate code for all sub-queries in the FROM clause
1073 */
1074 for(i=0; i<pTabList->nId; i++){
1075 int oldNTab;
drha76b5df2002-02-23 02:32:10 +00001076 if( pTabList->a[i].pSelect==0 ) continue;
drhd820cb12002-02-18 03:21:45 +00001077 oldNTab = pParse->nTab;
1078 pParse->nTab += i+1;
1079 sqliteVdbeAddOp(v, OP_OpenTemp, oldNTab+i, 0);
1080 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_Table, oldNTab+i);
1081 pParse->nTab = oldNTab;
1082 }
1083
drh22827922000-06-06 17:27:05 +00001084 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001085 */
drhd820cb12002-02-18 03:21:45 +00001086 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001087 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +00001088 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +00001089 for(i=0; i<pEList->nExpr; i++){
1090 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001091 goto select_end;
drh22827922000-06-06 17:27:05 +00001092 }
1093 }
1094 if( pGroupBy ){
1095 for(i=0; i<pGroupBy->nExpr; i++){
1096 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001097 goto select_end;
drh22827922000-06-06 17:27:05 +00001098 }
1099 }
1100 }
1101 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001102 goto select_end;
drh22827922000-06-06 17:27:05 +00001103 }
drh191b6902000-06-08 11:13:01 +00001104 if( pOrderBy ){
1105 for(i=0; i<pOrderBy->nExpr; i++){
1106 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001107 goto select_end;
drh191b6902000-06-08 11:13:01 +00001108 }
1109 }
1110 }
drhefb72512000-05-31 20:00:52 +00001111 }
1112
drh9bbca4c2001-11-06 04:00:18 +00001113 /* Set the limiter
1114 */
1115 if( p->nLimit<=0 ){
1116 p->nOffset = 0;
1117 }else{
1118 if( p->nOffset<0 ) p->nOffset = 0;
1119 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
1120 }
1121
1122
drh22827922000-06-06 17:27:05 +00001123 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +00001124 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +00001125 */
drhfef52082000-06-06 01:50:43 +00001126 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +00001127 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +00001128 }
1129
drh22827922000-06-06 17:27:05 +00001130 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001131 */
1132 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001133 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001134 for(i=0; i<pParse->nAgg; i++){
1135 UserFunc *pUser;
1136 if( (pUser = pParse->aAgg[i].pUser)!=0 && pUser->xFinalize!=0 ){
1137 sqliteVdbeAddOp(v, OP_AggFinalizer, 0, i);
1138 sqliteVdbeChangeP3(v, -1, (char*)pUser->xFinalize, P3_POINTER);
1139 }
1140 }
drh1bee3d72001-10-15 00:44:35 +00001141 if( pGroupBy==0 ){
1142 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001143 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
1144 for(i=0; i<pParse->nAgg; i++){
1145 Expr *pE;
1146 if( !pParse->aAgg[i].isAgg ) continue;
1147 pE = pParse->aAgg[i].pExpr;
1148 assert( pE==0 || pE->op==TK_AGG_FUNCTION );
1149 assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) );
1150 if( pE==0 || pE->iColumn==FN_Sum ){
1151 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1152 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
1153 continue;
1154 }
1155 }
1156 }
drhcce7d172000-05-31 15:34:51 +00001157 }
1158
drh19a775c2000-06-05 18:54:46 +00001159 /* Initialize the memory cell to NULL
1160 */
drhfef52082000-06-06 01:50:43 +00001161 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001162 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001163 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001164 }
1165
drhcce7d172000-05-31 15:34:51 +00001166 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +00001167 */
drh19a775c2000-06-05 18:54:46 +00001168 if( isDistinct ){
drhc6b52df2002-01-04 03:09:29 +00001169 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drhefb72512000-05-31 20:00:52 +00001170 }
drhcce7d172000-05-31 15:34:51 +00001171 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh1d83f052002-02-17 00:30:36 +00001172 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001173
drh22827922000-06-06 17:27:05 +00001174 /* Use the standard inner loop if we are not dealing with
1175 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001176 */
drhda9d6c42000-05-31 18:20:14 +00001177 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +00001178 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001179 pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001180 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001181 }
drhcce7d172000-05-31 15:34:51 +00001182 }
drhefb72512000-05-31 20:00:52 +00001183
drh22827922000-06-06 17:27:05 +00001184 /* If we are dealing with aggregates, then to the special aggregate
1185 ** processing.
drhefb72512000-05-31 20:00:52 +00001186 */
drh22827922000-06-06 17:27:05 +00001187 else{
drh22827922000-06-06 17:27:05 +00001188 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00001189 int lbl1;
drh22827922000-06-06 17:27:05 +00001190 for(i=0; i<pGroupBy->nExpr; i++){
1191 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
1192 }
drh99fcd712001-10-13 01:06:47 +00001193 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +00001194 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001195 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00001196 for(i=0; i<pParse->nAgg; i++){
1197 if( pParse->aAgg[i].isAgg ) continue;
1198 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00001199 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00001200 }
drh22827922000-06-06 17:27:05 +00001201 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00001202 }
drh22827922000-06-06 17:27:05 +00001203 for(i=0; i<pParse->nAgg; i++){
1204 Expr *pE;
drhe5095352002-02-24 03:25:14 +00001205 int op, j;
drh22827922000-06-06 17:27:05 +00001206 if( !pParse->aAgg[i].isAgg ) continue;
1207 pE = pParse->aAgg[i].pExpr;
1208 if( pE==0 ){
drh99fcd712001-10-13 01:06:47 +00001209 sqliteVdbeAddOp(v, OP_AggIncr, 1, i);
drh22827922000-06-06 17:27:05 +00001210 continue;
1211 }
1212 assert( pE->op==TK_AGG_FUNCTION );
drhe5095352002-02-24 03:25:14 +00001213 assert( pE->pList!=0 );
1214 for(j=0; j<pE->pList->nExpr; j++){
1215 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
1216 }
drh99fcd712001-10-13 01:06:47 +00001217 sqliteVdbeAddOp(v, OP_AggGet, 0, i);
drh967e8b72000-06-21 13:59:10 +00001218 switch( pE->iColumn ){
drhe5095352002-02-24 03:25:14 +00001219 case FN_Min: op = OP_Min; break;
1220 case FN_Max: op = OP_Max; break;
1221 case FN_Avg: op = OP_Add; break;
1222 case FN_Sum: op = OP_Add; break;
1223 case FN_Unknown: op = OP_AggFunc; break;
drh22827922000-06-06 17:27:05 +00001224 }
drhe5095352002-02-24 03:25:14 +00001225 if( op!=OP_AggFunc ){
1226 sqliteVdbeAddOp(v, op, 0, 0);
1227 }else{
1228 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList->nExpr);
1229 assert( pParse->aAgg[i].pUser!=0 );
1230 assert( pParse->aAgg[i].pUser->xStep!=0 );
1231 sqliteVdbeChangeP3(v,-1,(char*)pParse->aAgg[i].pUser->xStep,P3_POINTER);
1232 }
drh99fcd712001-10-13 01:06:47 +00001233 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drh22827922000-06-06 17:27:05 +00001234 }
drhcce7d172000-05-31 15:34:51 +00001235 }
1236
1237 /* End the database scan loop.
1238 */
1239 sqliteWhereEnd(pWInfo);
1240
drh22827922000-06-06 17:27:05 +00001241 /* If we are processing aggregates, we need to set up a second loop
1242 ** over all of the aggregate values and process them.
1243 */
1244 if( isAgg ){
1245 int endagg = sqliteVdbeMakeLabel(v);
1246 int startagg;
drh99fcd712001-10-13 01:06:47 +00001247 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00001248 pParse->useAgg = 1;
1249 if( pHaving ){
1250 sqliteExprIfFalse(pParse, pHaving, startagg);
1251 }
drh82c3d632000-06-06 21:56:07 +00001252 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001253 startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00001254 goto select_end;
drh22827922000-06-06 17:27:05 +00001255 }
drh99fcd712001-10-13 01:06:47 +00001256 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
1257 sqliteVdbeResolveLabel(v, endagg);
1258 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00001259 pParse->useAgg = 0;
1260 }
1261
drhcce7d172000-05-31 15:34:51 +00001262 /* If there is an ORDER BY clause, then we need to sort the results
1263 ** and send them to the callback one by one.
1264 */
1265 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001266 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001267 }
drh10e5e3c2000-06-08 00:19:02 +00001268 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +00001269
1270
1271 /* Issue a null callback if that is what the user wants.
1272 */
1273 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1274 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1275 }
1276
drh1d83f052002-02-17 00:30:36 +00001277 /* The SELECT was successfully coded. Set the return code to 0
1278 ** to indicate no errors.
1279 */
1280 rc = 0;
1281
1282 /* Control jumps to here if an error is encountered above, or upon
1283 ** successful coding of the SELECT.
1284 */
1285select_end:
1286 sqliteAggregateInfoReset(pParse);
1287 return rc;
drhcce7d172000-05-31 15:34:51 +00001288}