blob: ac2fc5803b0791259b9bbaa084e50458d8c80690 [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**
drha76b5df2002-02-23 02:32:10 +000015** $Id: select.c,v 1.65 2002/02/23 02:32:10 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 ){
400 pTabList->a[i].pSelect = pTab->pSelect;
401 }
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/*
460** This routine associates entries in an ORDER BY expression list with
461** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000462** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000463** the top-level node is filled in with column number and the iTable
464** value of the top-level node is filled with iTable parameter.
465**
466** If there are prior SELECT clauses, they are processed first. A match
467** in an earlier SELECT takes precedence over a later SELECT.
468**
469** Any entry that does not match is flagged as an error. The number
470** of errors is returned.
471*/
472static int matchOrderbyToColumn(
473 Parse *pParse, /* A place to leave error messages */
474 Select *pSelect, /* Match to result columns of this SELECT */
475 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
476 int iTable, /* Insert this this value in iTable */
477 int mustComplete /* If TRUE all ORDER BYs must match */
478){
479 int nErr = 0;
480 int i, j;
481 ExprList *pEList;
482
drhdaffd0e2001-04-11 14:28:42 +0000483 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000484 if( mustComplete ){
485 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
486 }
487 if( fillInColumnList(pParse, pSelect) ){
488 return 1;
489 }
490 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000491 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
492 return 1;
493 }
drhd8bc7082000-06-07 23:51:50 +0000494 }
495 pEList = pSelect->pEList;
496 for(i=0; i<pOrderBy->nExpr; i++){
497 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000498 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000499 if( pOrderBy->a[i].done ) continue;
500 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000501 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +0000502 char *zName, *zLabel;
503 zName = pEList->a[j].zName;
504 assert( pE->token.z );
505 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000506 sqliteDequote(zLabel);
507 if( sqliteStrICmp(zName, zLabel)==0 ){
508 match = 1;
509 }
drh6e142f52000-06-08 13:36:40 +0000510 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000511 }
drh4cfa7932000-06-08 15:10:46 +0000512 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000513 match = 1;
514 }
515 if( match ){
drh967e8b72000-06-21 13:59:10 +0000516 pE->op = TK_COLUMN;
517 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000518 pE->iTable = iTable;
519 pOrderBy->a[i].done = 1;
520 break;
521 }
522 }
drh92cd52f2000-06-08 01:55:29 +0000523 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000524 char zBuf[30];
525 sprintf(zBuf,"%d",i+1);
526 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
527 " does not match any result column", 0);
528 pParse->nErr++;
529 nErr++;
530 break;
531 }
532 }
533 return nErr;
534}
535
536/*
537** Get a VDBE for the given parser context. Create a new one if necessary.
538** If an error occurs, return NULL and leave a message in pParse.
539*/
540Vdbe *sqliteGetVdbe(Parse *pParse){
541 Vdbe *v = pParse->pVdbe;
542 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000543 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000544 }
drhd8bc7082000-06-07 23:51:50 +0000545 return v;
546}
547
548
549/*
drh82c3d632000-06-06 21:56:07 +0000550** This routine is called to process a query that is really the union
551** or intersection of two or more separate queries.
552*/
553static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000554 int rc; /* Success code from a subroutine */
555 Select *pPrior; /* Another SELECT immediately to our left */
556 Vdbe *v; /* Generate code to this VDBE */
557 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000558
drhd8bc7082000-06-07 23:51:50 +0000559 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
560 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000561 */
drhdaffd0e2001-04-11 14:28:42 +0000562 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000563 pPrior = p->pPrior;
564 if( pPrior->pOrderBy ){
565 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
566 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000567 pParse->nErr++;
568 return 1;
569 }
570
drhd8bc7082000-06-07 23:51:50 +0000571 /* Make sure we have a valid query engine. If not, create a new one.
572 */
573 v = sqliteGetVdbe(pParse);
574 if( v==0 ) return 1;
575
576 /* Process the UNION or INTERSECTION
577 */
drh10e5e3c2000-06-08 00:19:02 +0000578 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000579 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000580 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000581 case TK_EXCEPT:
582 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000583 int unionTab; /* Cursor number of the temporary table holding result */
584 int op; /* One of the SRT_ operations to apply to self */
585 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000586
drhd8bc7082000-06-07 23:51:50 +0000587 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
588 if( eDest==priorOp ){
589 /* We can reuse a temporary table generated by a SELECT to our
590 ** right. This also means we are not the right-most select and so
591 ** we cannot have an ORDER BY clause
592 */
drh82c3d632000-06-06 21:56:07 +0000593 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000594 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000595 }else{
drhd8bc7082000-06-07 23:51:50 +0000596 /* We will need to create our own temporary table to hold the
597 ** intermediate results.
598 */
599 unionTab = pParse->nTab++;
600 if( p->pOrderBy
601 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
602 return 1;
603 }
drhd8bc7082000-06-07 23:51:50 +0000604 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +0000605 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +0000606 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000607 }else{
drh99fcd712001-10-13 01:06:47 +0000608 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000609 }
drh82c3d632000-06-06 21:56:07 +0000610 }
drhd8bc7082000-06-07 23:51:50 +0000611
612 /* Code the SELECT statements to our left
613 */
614 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000615 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000616
617 /* Code the current SELECT statement
618 */
619 switch( p->op ){
620 case TK_EXCEPT: op = SRT_Except; break;
621 case TK_UNION: op = SRT_Union; break;
622 case TK_ALL: op = SRT_Table; break;
623 }
drh82c3d632000-06-06 21:56:07 +0000624 p->pPrior = 0;
625 rc = sqliteSelect(pParse, p, op, unionTab);
626 p->pPrior = pPrior;
627 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000628
629 /* Convert the data in the temporary table into whatever form
630 ** it is that we currently need.
631 */
632 if( eDest!=priorOp ){
drh6b563442001-11-07 16:48:26 +0000633 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000634 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000635 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000636 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000637 iCont = sqliteVdbeMakeLabel(v);
638 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
639 iStart = sqliteVdbeCurrentAddr(v);
drh82c3d632000-06-06 21:56:07 +0000640 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000641 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000642 iCont, iBreak);
643 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000644 sqliteVdbeResolveLabel(v, iCont);
645 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +0000646 sqliteVdbeResolveLabel(v, iBreak);
647 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000648 if( p->pOrderBy ){
649 generateSortTail(v, p->pEList->nExpr);
650 }
drh82c3d632000-06-06 21:56:07 +0000651 }
652 break;
653 }
654 case TK_INTERSECT: {
655 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +0000656 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000657
drhd8bc7082000-06-07 23:51:50 +0000658 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000659 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000660 ** by allocating the tables we will need.
661 */
drh82c3d632000-06-06 21:56:07 +0000662 tab1 = pParse->nTab++;
663 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000664 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
665 return 1;
666 }
drhc6b52df2002-01-04 03:09:29 +0000667 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +0000668 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000669
670 /* Code the SELECTs to our left into temporary table "tab1".
671 */
drh82c3d632000-06-06 21:56:07 +0000672 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
673 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000674
675 /* Code the current SELECT into temporary table "tab2"
676 */
drhc6b52df2002-01-04 03:09:29 +0000677 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +0000678 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000679 p->pPrior = 0;
680 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
681 p->pPrior = pPrior;
682 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000683
684 /* Generate code to take the intersection of the two temporary
685 ** tables.
686 */
drh82c3d632000-06-06 21:56:07 +0000687 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000688 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000689 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000690 iCont = sqliteVdbeMakeLabel(v);
691 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
692 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +0000693 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000694 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000695 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000696 iCont, iBreak);
697 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000698 sqliteVdbeResolveLabel(v, iCont);
699 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +0000700 sqliteVdbeResolveLabel(v, iBreak);
701 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
702 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000703 if( p->pOrderBy ){
704 generateSortTail(v, p->pEList->nExpr);
705 }
drh82c3d632000-06-06 21:56:07 +0000706 break;
707 }
708 }
709 assert( p->pEList && pPrior->pEList );
710 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000711 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
712 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000713 pParse->nErr++;
714 return 1;
drh22827922000-06-06 17:27:05 +0000715 }
drh10e5e3c2000-06-08 00:19:02 +0000716 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000717 return 0;
718}
719
720/*
drh9562b552002-02-19 15:00:07 +0000721** Analyze the SELECT statement passed in as an argument to see if it
722** is a simple min() or max() query. If it is and this query can be
723** satisfied using a single seek to the beginning or end of an index,
724** then generate the code for this SELECT return 1. If this is not a
725** simple min() or max() query, then return 0;
726**
727** A simply min() or max() query looks like this:
728**
729** SELECT min(a) FROM table;
730** SELECT max(a) FROM table;
731**
732** The query may have only a single table in its FROM argument. There
733** can be no GROUP BY or HAVING or WHERE clauses. The result set must
734** be the min() or max() of a single column of the table. The column
735** in the min() or max() function must be indexed.
736**
737** The parameters to this routine are the same as for sqliteSelect().
738** See the header comment on that routine for additional information.
739*/
740static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
741 Expr *pExpr;
742 int iCol;
743 Table *pTab;
744 Index *pIdx;
745 int base;
746 Vdbe *v;
747 int openOp;
748 int seekOp;
749 int cont;
750 ExprList eList;
751 struct ExprList_item eListItem;
752
753 /* Check to see if this query is a simple min() or max() query. Return
754 ** zero if it is not.
755 */
756 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
757 if( p->pSrc->nId!=1 ) return 0;
758 if( p->pEList->nExpr!=1 ) return 0;
759 pExpr = p->pEList->a[0].pExpr;
760 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
761 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
762 if( pExpr->iColumn!=FN_Min && pExpr->iColumn!=FN_Max ) return 0;
763 seekOp = pExpr->iColumn==FN_Min ? OP_Rewind : OP_Last;
764 pExpr = pExpr->pList->a[0].pExpr;
765 if( pExpr->op!=TK_COLUMN ) return 0;
766 iCol = pExpr->iColumn;
767 pTab = p->pSrc->a[0].pTab;
768
769 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +0000770 ** Check to make sure we have an index and make pIdx point to the
771 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
772 ** key column, no index is necessary so set pIdx to NULL. If no
773 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +0000774 */
775 if( iCol<0 ){
776 pIdx = 0;
777 }else{
778 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
779 assert( pIdx->nColumn>=1 );
780 if( pIdx->aiColumn[0]==iCol ) break;
781 }
782 if( pIdx==0 ) return 0;
783 }
784
drh17f71932002-02-21 12:01:27 +0000785 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +0000786 ** step is skipped if the output is going to a table or a memory cell.
787 */
788 v = sqliteGetVdbe(pParse);
789 if( v==0 ) return 0;
790 if( eDest==SRT_Callback ){
791 generateColumnNames(pParse, p->pSrc, p->pEList);
792 }
793
drh17f71932002-02-21 12:01:27 +0000794 /* Generating code to find the min or the max. Basically all we have
795 ** to do is find the first or the last entry in the chosen index. If
796 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
797 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +0000798 */
drh5cf8e8c2002-02-19 22:42:05 +0000799 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
800 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
801 pParse->schemaVerified = 1;
802 }
drh9562b552002-02-19 15:00:07 +0000803 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh5cf8e8c2002-02-19 22:42:05 +0000804 base = pParse->nTab;
drh9562b552002-02-19 15:00:07 +0000805 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +0000806 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +0000807 if( pIdx==0 ){
808 sqliteVdbeAddOp(v, seekOp, base, 0);
809 }else{
810 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +0000811 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +0000812 sqliteVdbeAddOp(v, seekOp, base+1, 0);
813 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
814 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
815 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
816 }
drh5cf8e8c2002-02-19 22:42:05 +0000817 eList.nExpr = 1;
818 memset(&eListItem, 0, sizeof(eListItem));
819 eList.a = &eListItem;
820 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +0000821 cont = sqliteVdbeMakeLabel(v);
822 selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont);
823 sqliteVdbeResolveLabel(v, cont);
824 sqliteVdbeAddOp(v, OP_Close, base, 0);
825 return 1;
826}
827
828/*
drh9bb61fe2000-06-05 16:01:39 +0000829** Generate code for the given SELECT statement.
830**
drhfef52082000-06-06 01:50:43 +0000831** The results are distributed in various ways depending on the
832** value of eDest and iParm.
833**
834** eDest Value Result
835** ------------ -------------------------------------------
836** SRT_Callback Invoke the callback for each row of the result.
837**
838** SRT_Mem Store first result in memory cell iParm
839**
840** SRT_Set Store results as keys of a table with cursor iParm
841**
drh82c3d632000-06-06 21:56:07 +0000842** SRT_Union Store results as a key in a temporary table iParm
843**
drhc4a3c772001-04-04 11:48:57 +0000844** SRT_Except Remove results form the temporary table iParm.
845**
846** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000847**
848** This routine returns the number of errors. If any errors are
849** encountered, then an appropriate error message is left in
850** pParse->zErrMsg.
851**
852** This routine does NOT free the Select structure passed in. The
853** calling function needs to do that.
854*/
855int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000856 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000857 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000858 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000859 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000860){
drhd8bc7082000-06-07 23:51:50 +0000861 int i;
drhcce7d172000-05-31 15:34:51 +0000862 WhereInfo *pWInfo;
863 Vdbe *v;
864 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +0000865 ExprList *pEList; /* List of columns to extract. */
drh9bb61fe2000-06-05 16:01:39 +0000866 IdList *pTabList; /* List of tables to select from */
867 Expr *pWhere; /* The WHERE clause. May be NULL */
868 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000869 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
870 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000871 int isDistinct; /* True if the DISTINCT keyword is present */
872 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000873 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +0000874 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +0000875
drhdaffd0e2001-04-11 14:28:42 +0000876 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
877
drh82c3d632000-06-06 21:56:07 +0000878 /* If there is are a sequence of queries, do the earlier ones first.
879 */
880 if( p->pPrior ){
881 return multiSelect(pParse, p, eDest, iParm);
882 }
883
884 /* Make local copies of the parameters for this query.
885 */
drh9bb61fe2000-06-05 16:01:39 +0000886 pTabList = p->pSrc;
887 pWhere = p->pWhere;
888 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000889 pGroupBy = p->pGroupBy;
890 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000891 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000892
drh10e5e3c2000-06-08 00:19:02 +0000893 /* Save the current value of pParse->nTab. Restore this value before
894 ** we exit.
895 */
896 base = pParse->nTab;
897
drh9bb61fe2000-06-05 16:01:39 +0000898 /*
899 ** Do not even attempt to generate any code if we have already seen
900 ** errors before this routine starts.
901 */
drh1d83f052002-02-17 00:30:36 +0000902 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +0000903
drhd8bc7082000-06-07 23:51:50 +0000904 /* Look up every table in the table list and create an appropriate
905 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000906 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000907 */
drhd8bc7082000-06-07 23:51:50 +0000908 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +0000909 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000910 }
drhd8bc7082000-06-07 23:51:50 +0000911 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +0000912 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +0000913
drh19a775c2000-06-05 18:54:46 +0000914 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000915 ** necessary. This must be done early to allocate the cursor before
916 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000917 */
918 if( isDistinct ){
919 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000920 }else{
921 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000922 }
923
drh22827922000-06-06 17:27:05 +0000924 /* If writing to memory or generating a set
925 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000926 */
drhfef52082000-06-06 01:50:43 +0000927 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000928 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
929 "a SELECT that is part of an expression", 0);
930 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +0000931 goto select_end;
drh19a775c2000-06-05 18:54:46 +0000932 }
933
drh22827922000-06-06 17:27:05 +0000934 /* ORDER BY is ignored if we are not sending the result to a callback.
935 */
936 if( eDest!=SRT_Callback ){
937 pOrderBy = 0;
938 }
939
940 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000941 */
942 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000943 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
944 }
945 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
946 if( pOrderBy ){
947 for(i=0; i<pOrderBy->nExpr; i++){
948 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
949 }
950 }
drh22827922000-06-06 17:27:05 +0000951 if( pGroupBy ){
952 for(i=0; i<pGroupBy->nExpr; i++){
953 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
954 }
955 }
956 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
957
drh10e5e3c2000-06-08 00:19:02 +0000958 /* At this point, we should have allocated all the cursors that we
959 ** need to handle subquerys and temporary tables. From here on we
960 ** are committed to keeping the same value for pParse->nTab.
961 **
drh967e8b72000-06-21 13:59:10 +0000962 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000963 */
drh4794b982000-06-06 13:54:14 +0000964 for(i=0; i<pEList->nExpr; i++){
drha2e00042002-01-22 03:13:42 +0000965 if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +0000966 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000967 }
drh22827922000-06-06 17:27:05 +0000968 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +0000969 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000970 }
971 }
drhcce7d172000-05-31 15:34:51 +0000972 if( pWhere ){
drha2e00042002-01-22 03:13:42 +0000973 if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +0000974 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000975 }
976 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +0000977 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000978 }
979 }
980 if( pOrderBy ){
981 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000982 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000983 if( sqliteExprIsConstant(pE) ){
984 sqliteSetString(&pParse->zErrMsg,
985 "ORDER BY expressions should not be constant", 0);
986 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +0000987 goto select_end;
drh92086432002-01-22 14:11:29 +0000988 }
drha2e00042002-01-22 03:13:42 +0000989 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +0000990 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000991 }
drh22827922000-06-06 17:27:05 +0000992 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +0000993 goto select_end;
drhcce7d172000-05-31 15:34:51 +0000994 }
995 }
996 }
drh22827922000-06-06 17:27:05 +0000997 if( pGroupBy ){
998 for(i=0; i<pGroupBy->nExpr; i++){
999 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001000 if( sqliteExprIsConstant(pE) ){
1001 sqliteSetString(&pParse->zErrMsg,
1002 "GROUP BY expressions should not be constant", 0);
1003 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001004 goto select_end;
drh92086432002-01-22 14:11:29 +00001005 }
drha2e00042002-01-22 03:13:42 +00001006 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001007 goto select_end;
drh22827922000-06-06 17:27:05 +00001008 }
1009 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001010 goto select_end;
drh22827922000-06-06 17:27:05 +00001011 }
1012 }
1013 }
1014 if( pHaving ){
1015 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001016 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1017 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001018 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001019 goto select_end;
drh22827922000-06-06 17:27:05 +00001020 }
drha2e00042002-01-22 03:13:42 +00001021 if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001022 goto select_end;
drh22827922000-06-06 17:27:05 +00001023 }
drhda932812000-06-06 18:00:15 +00001024 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001025 goto select_end;
drh22827922000-06-06 17:27:05 +00001026 }
drhcce7d172000-05-31 15:34:51 +00001027 }
1028
drh9562b552002-02-19 15:00:07 +00001029 /* Check for the special case of a min() or max() function by itself
1030 ** in the result set.
1031 */
1032 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001033 rc = 0;
drh9562b552002-02-19 15:00:07 +00001034 goto select_end;
1035 }
1036
drhd820cb12002-02-18 03:21:45 +00001037 /* Begin generating code.
1038 */
1039 v = sqliteGetVdbe(pParse);
1040 if( v==0 ) goto select_end;
1041
1042 /* Generate code for all sub-queries in the FROM clause
1043 */
1044 for(i=0; i<pTabList->nId; i++){
1045 int oldNTab;
drha76b5df2002-02-23 02:32:10 +00001046 if( pTabList->a[i].pSelect==0 ) continue;
drhd820cb12002-02-18 03:21:45 +00001047 oldNTab = pParse->nTab;
1048 pParse->nTab += i+1;
1049 sqliteVdbeAddOp(v, OP_OpenTemp, oldNTab+i, 0);
1050 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_Table, oldNTab+i);
1051 pParse->nTab = oldNTab;
1052 }
1053
drh22827922000-06-06 17:27:05 +00001054 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001055 */
drhd820cb12002-02-18 03:21:45 +00001056 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001057 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +00001058 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +00001059 for(i=0; i<pEList->nExpr; i++){
1060 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001061 goto select_end;
drh22827922000-06-06 17:27:05 +00001062 }
1063 }
1064 if( pGroupBy ){
1065 for(i=0; i<pGroupBy->nExpr; i++){
1066 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001067 goto select_end;
drh22827922000-06-06 17:27:05 +00001068 }
1069 }
1070 }
1071 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001072 goto select_end;
drh22827922000-06-06 17:27:05 +00001073 }
drh191b6902000-06-08 11:13:01 +00001074 if( pOrderBy ){
1075 for(i=0; i<pOrderBy->nExpr; i++){
1076 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001077 goto select_end;
drh191b6902000-06-08 11:13:01 +00001078 }
1079 }
1080 }
drhefb72512000-05-31 20:00:52 +00001081 }
1082
drh9bbca4c2001-11-06 04:00:18 +00001083 /* Set the limiter
1084 */
1085 if( p->nLimit<=0 ){
1086 p->nOffset = 0;
1087 }else{
1088 if( p->nOffset<0 ) p->nOffset = 0;
1089 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
1090 }
1091
1092
drh22827922000-06-06 17:27:05 +00001093 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +00001094 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +00001095 */
drhfef52082000-06-06 01:50:43 +00001096 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +00001097 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +00001098 }
1099
drh22827922000-06-06 17:27:05 +00001100 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001101 */
1102 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001103 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drh1bee3d72001-10-15 00:44:35 +00001104 if( pGroupBy==0 ){
1105 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001106 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
1107 for(i=0; i<pParse->nAgg; i++){
1108 Expr *pE;
1109 if( !pParse->aAgg[i].isAgg ) continue;
1110 pE = pParse->aAgg[i].pExpr;
1111 assert( pE==0 || pE->op==TK_AGG_FUNCTION );
1112 assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) );
1113 if( pE==0 || pE->iColumn==FN_Sum ){
1114 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1115 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
1116 continue;
1117 }
1118 }
1119 }
drhcce7d172000-05-31 15:34:51 +00001120 }
1121
drh19a775c2000-06-05 18:54:46 +00001122 /* Initialize the memory cell to NULL
1123 */
drhfef52082000-06-06 01:50:43 +00001124 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001125 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001126 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001127 }
1128
drhcce7d172000-05-31 15:34:51 +00001129 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +00001130 */
drh19a775c2000-06-05 18:54:46 +00001131 if( isDistinct ){
drhc6b52df2002-01-04 03:09:29 +00001132 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drhefb72512000-05-31 20:00:52 +00001133 }
drhcce7d172000-05-31 15:34:51 +00001134 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh1d83f052002-02-17 00:30:36 +00001135 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001136
drh22827922000-06-06 17:27:05 +00001137 /* Use the standard inner loop if we are not dealing with
1138 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001139 */
drhda9d6c42000-05-31 18:20:14 +00001140 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +00001141 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001142 pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001143 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001144 }
drhcce7d172000-05-31 15:34:51 +00001145 }
drhefb72512000-05-31 20:00:52 +00001146
drh22827922000-06-06 17:27:05 +00001147 /* If we are dealing with aggregates, then to the special aggregate
1148 ** processing.
drhefb72512000-05-31 20:00:52 +00001149 */
drh22827922000-06-06 17:27:05 +00001150 else{
drh22827922000-06-06 17:27:05 +00001151 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00001152 int lbl1;
drh22827922000-06-06 17:27:05 +00001153 for(i=0; i<pGroupBy->nExpr; i++){
1154 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
1155 }
drh99fcd712001-10-13 01:06:47 +00001156 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +00001157 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001158 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00001159 for(i=0; i<pParse->nAgg; i++){
1160 if( pParse->aAgg[i].isAgg ) continue;
1161 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00001162 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00001163 }
drh22827922000-06-06 17:27:05 +00001164 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00001165 }
drh22827922000-06-06 17:27:05 +00001166 for(i=0; i<pParse->nAgg; i++){
1167 Expr *pE;
1168 int op;
1169 if( !pParse->aAgg[i].isAgg ) continue;
1170 pE = pParse->aAgg[i].pExpr;
1171 if( pE==0 ){
drh99fcd712001-10-13 01:06:47 +00001172 sqliteVdbeAddOp(v, OP_AggIncr, 1, i);
drh22827922000-06-06 17:27:05 +00001173 continue;
1174 }
1175 assert( pE->op==TK_AGG_FUNCTION );
1176 assert( pE->pList!=0 && pE->pList->nExpr==1 );
1177 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +00001178 sqliteVdbeAddOp(v, OP_AggGet, 0, i);
drh967e8b72000-06-21 13:59:10 +00001179 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +00001180 case FN_Min: op = OP_Min; break;
1181 case FN_Max: op = OP_Max; break;
1182 case FN_Avg: op = OP_Add; break;
1183 case FN_Sum: op = OP_Add; break;
1184 }
drh99fcd712001-10-13 01:06:47 +00001185 sqliteVdbeAddOp(v, op, 0, 0);
1186 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drh22827922000-06-06 17:27:05 +00001187 }
drhcce7d172000-05-31 15:34:51 +00001188 }
1189
drh22827922000-06-06 17:27:05 +00001190
drhcce7d172000-05-31 15:34:51 +00001191 /* End the database scan loop.
1192 */
1193 sqliteWhereEnd(pWInfo);
1194
drh22827922000-06-06 17:27:05 +00001195 /* If we are processing aggregates, we need to set up a second loop
1196 ** over all of the aggregate values and process them.
1197 */
1198 if( isAgg ){
1199 int endagg = sqliteVdbeMakeLabel(v);
1200 int startagg;
drh99fcd712001-10-13 01:06:47 +00001201 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00001202 pParse->useAgg = 1;
1203 if( pHaving ){
1204 sqliteExprIfFalse(pParse, pHaving, startagg);
1205 }
drh82c3d632000-06-06 21:56:07 +00001206 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001207 startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00001208 goto select_end;
drh22827922000-06-06 17:27:05 +00001209 }
drh99fcd712001-10-13 01:06:47 +00001210 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
1211 sqliteVdbeResolveLabel(v, endagg);
1212 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00001213 pParse->useAgg = 0;
1214 }
1215
drhcce7d172000-05-31 15:34:51 +00001216 /* If there is an ORDER BY clause, then we need to sort the results
1217 ** and send them to the callback one by one.
1218 */
1219 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001220 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001221 }
drh10e5e3c2000-06-08 00:19:02 +00001222 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +00001223
1224
1225 /* Issue a null callback if that is what the user wants.
1226 */
1227 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1228 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1229 }
1230
drh1d83f052002-02-17 00:30:36 +00001231 /* The SELECT was successfully coded. Set the return code to 0
1232 ** to indicate no errors.
1233 */
1234 rc = 0;
1235
1236 /* Control jumps to here if an error is encountered above, or upon
1237 ** successful coding of the SELECT.
1238 */
1239select_end:
1240 sqliteAggregateInfoReset(pParse);
1241 return rc;
drhcce7d172000-05-31 15:34:51 +00001242}