blob: 0aa76ff0116a4d4b23b65381edd451b23b1f89bd [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This file contains C code routines that are called by the parser
25** to handle SELECT statements.
26**
drhaaf88722000-06-08 11:25:00 +000027** $Id: select.c,v 1.21 2000/06/08 11:25:01 drh Exp $
drhcce7d172000-05-31 15:34:51 +000028*/
29#include "sqliteInt.h"
30
drhcce7d172000-05-31 15:34:51 +000031/*
drh9bb61fe2000-06-05 16:01:39 +000032** Allocate a new Select structure and return a pointer to that
33** structure.
drhcce7d172000-05-31 15:34:51 +000034*/
drh9bb61fe2000-06-05 16:01:39 +000035Select *sqliteSelectNew(
36 ExprList *pEList,
37 IdList *pSrc,
38 Expr *pWhere,
39 ExprList *pGroupBy,
40 Expr *pHaving,
41 ExprList *pOrderBy,
42 int isDistinct
43){
44 Select *pNew;
45 pNew = sqliteMalloc( sizeof(*pNew) );
46 if( pNew==0 ) return 0;
47 pNew->pEList = pEList;
48 pNew->pSrc = pSrc;
49 pNew->pWhere = pWhere;
50 pNew->pGroupBy = pGroupBy;
51 pNew->pHaving = pHaving;
52 pNew->pOrderBy = pOrderBy;
53 pNew->isDistinct = isDistinct;
drh82c3d632000-06-06 21:56:07 +000054 pNew->op = TK_SELECT;
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);
drh9bb61fe2000-06-05 16:01:39 +000070 sqliteFree(p);
71}
72
73/*
drh22827922000-06-06 17:27:05 +000074** Delete the aggregate information from the parse structure.
75*/
76void sqliteParseInfoReset(Parse *pParse){
77 sqliteFree(pParse->aAgg);
78 pParse->aAgg = 0;
79 pParse->nAgg = 0;
80 pParse->iAggCount = -1;
81 pParse->useAgg = 0;
82}
83
84/*
85** This routine generates the code for the inside of the inner loop
86** of a SELECT.
drh82c3d632000-06-06 21:56:07 +000087**
88** The pEList is used to determine the values for each column in the
89** result row. Except if pEList==NULL, then we just read nField
90** elements from the srcTab table.
drh22827922000-06-06 17:27:05 +000091*/
92static int selectInnerLoop(
93 Parse *pParse, /* The parser context */
94 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +000095 int srcTab, /* Pull data from this table */
96 int nField, /* Number of fields in the source table */
drh22827922000-06-06 17:27:05 +000097 ExprList *pOrderBy, /* If not NULL, sort results using this key */
98 int distinct, /* If >=0, make sure results are distinct */
99 int eDest, /* How to dispose of the results */
100 int iParm, /* An argument to the disposal method */
101 int iContinue, /* Jump here to continue with next row */
102 int iBreak /* Jump here to break out of the inner loop */
103){
104 Vdbe *v = pParse->pVdbe;
105 int i;
106
107 /* Pull the requested fields.
108 */
drh82c3d632000-06-06 21:56:07 +0000109 if( pEList ){
110 for(i=0; i<pEList->nExpr; i++){
111 sqliteExprCode(pParse, pEList->a[i].pExpr);
112 }
113 nField = pEList->nExpr;
114 }else{
115 for(i=0; i<nField; i++){
116 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
117 }
drh22827922000-06-06 17:27:05 +0000118 }
119
120 /* If the current result is not distinct, skip the rest
121 ** of the processing for the current row.
122 */
123 if( distinct>=0 ){
124 int lbl = sqliteVdbeMakeLabel(v);
125 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0);
126 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0);
127 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0);
128 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0);
129 sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl);
130 sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0);
131 }
drh82c3d632000-06-06 21:56:07 +0000132
drh22827922000-06-06 17:27:05 +0000133 /* If there is an ORDER BY clause, then store the results
134 ** in a sorter.
135 */
136 if( pOrderBy ){
137 char *zSortOrder;
drhd8bc7082000-06-07 23:51:50 +0000138 sqliteVdbeAddOp(v, OP_SortMakeRec, nField, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000139 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
140 if( zSortOrder==0 ) return 1;
141 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000142 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000143 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
144 }
145 zSortOrder[pOrderBy->nExpr] = 0;
146 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0);
147 sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0);
148 }else
149
drh82c3d632000-06-06 21:56:07 +0000150 /* In this mode, write each query result to the key of the temporary
151 ** table iParm.
drh22827922000-06-06 17:27:05 +0000152 */
drh82c3d632000-06-06 21:56:07 +0000153 if( eDest==SRT_Union ){
154 sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0);
155 sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0);
156 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
157 }else
158
drh5974a302000-06-07 14:42:26 +0000159 /* Store the result as data using a unique key.
160 */
161 if( eDest==SRT_Table ){
162 sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0);
163 sqliteVdbeAddOp(v, OP_New, iParm, 0, 0, 0);
164 sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0);
165 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
166 }else
167
drh82c3d632000-06-06 21:56:07 +0000168 /* Construct a record from the query result, but instead of
169 ** saving that record, use it as a key to delete elements from
170 ** the temporary table iParm.
171 */
172 if( eDest==SRT_Except ){
drh1ecec3c2000-06-06 22:13:55 +0000173 sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0);
174 sqliteVdbeAddOp(v, OP_Delete, iParm, 0, 0, 0);
drh22827922000-06-06 17:27:05 +0000175 }else
176
177 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
178 ** then there should be a single item on the stack. Write this
179 ** item into the set table with bogus data.
180 */
181 if( eDest==SRT_Set ){
drhd8bc7082000-06-07 23:51:50 +0000182 assert( nField==1 );
drh22827922000-06-06 17:27:05 +0000183 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
184 sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
185 }else
186
drh82c3d632000-06-06 21:56:07 +0000187
drh22827922000-06-06 17:27:05 +0000188 /* If this is a scalar select that is part of an expression, then
189 ** store the results in the appropriate memory cell and break out
190 ** of the scan loop.
191 */
192 if( eDest==SRT_Mem ){
drhd8bc7082000-06-07 23:51:50 +0000193 assert( nField==1 );
drh22827922000-06-06 17:27:05 +0000194 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
195 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0);
196 }else
197
198 /* If none of the above, send the data to the callback function.
199 */
200 {
drh82c3d632000-06-06 21:56:07 +0000201 sqliteVdbeAddOp(v, OP_Callback, nField, 0, 0, 0);
202 }
203 return 0;
204}
205
206/*
drhd8bc7082000-06-07 23:51:50 +0000207** If the inner loop was generated using a non-null pOrderBy argument,
208** then the results were placed in a sorter. After the loop is terminated
209** we need to run the sorter and output the results. The following
210** routine generates the code needed to do that.
211*/
212static void generateSortTail(Vdbe *v, int nField){
213 int end = sqliteVdbeMakeLabel(v);
214 int addr;
215 sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0);
216 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0);
217 sqliteVdbeAddOp(v, OP_SortCallback, nField, 0, 0, 0);
218 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
219 sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end);
220}
221
222/*
drh82c3d632000-06-06 21:56:07 +0000223** Generate code that will tell the VDBE how many columns there
224** are in the result and the name for each column. This information
225** is used to provide "argc" and "azCol[]" values in the callback.
226*/
drhd8bc7082000-06-07 23:51:50 +0000227static
228void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
229 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000230 int i;
drhd8bc7082000-06-07 23:51:50 +0000231 if( pParse->colNamesSet ) return;
232 pParse->colNamesSet = 1;
drh82c3d632000-06-06 21:56:07 +0000233 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0);
234 for(i=0; i<pEList->nExpr; i++){
235 Expr *p;
236 if( pEList->a[i].zName ){
237 char *zName = pEList->a[i].zName;
drhd8bc7082000-06-07 23:51:50 +0000238 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
drh82c3d632000-06-06 21:56:07 +0000239 continue;
240 }
241 p = pEList->a[i].pExpr;
242 if( p->op!=TK_FIELD || pTabList==0 ){
243 char zName[30];
drh5974a302000-06-07 14:42:26 +0000244 sprintf(zName, "column%d", i+1);
drh82c3d632000-06-06 21:56:07 +0000245 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
246 }else{
247 if( pTabList->nId>1 ){
248 char *zName = 0;
249 Table *pTab = pTabList->a[p->iTable].pTab;
250 char *zTab;
251
252 zTab = pTabList->a[p->iTable].zAlias;
253 if( zTab==0 ) zTab = pTab->zName;
254 sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iField].zName, 0);
255 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
256 sqliteFree(zName);
257 }else{
258 Table *pTab = pTabList->a[0].pTab;
259 char *zName = pTab->aCol[p->iField].zName;
260 sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
261 }
262 }
263 }
264}
265
266/*
drhd8bc7082000-06-07 23:51:50 +0000267** Name of the connection operator, used for error messages.
268*/
269static const char *selectOpName(int id){
270 char *z;
271 switch( id ){
272 case TK_ALL: z = "UNION ALL"; break;
273 case TK_INTERSECT: z = "INTERSECT"; break;
274 case TK_EXCEPT: z = "EXCEPT"; break;
275 default: z = "UNION"; break;
276 }
277 return z;
278}
279
280/*
281** For the given SELECT statement, do two things.
282**
283** (1) Fill in the pTab fields of the IdList that defines the set
284** of tables we are scanning.
285**
286** (2) If the columns to be extracted variable (pEList) is NULL
287** (meaning that a "*" was used in the SQL statement) then
288** create a fake pEList containing the names of all columns
289** of all tables.
290**
291** Return 0 on success. If there are problems, leave an error message
292** in pParse and return non-zero.
293*/
294static int fillInColumnList(Parse *pParse, Select *p){
295 int i, j;
296 IdList *pTabList = p->pSrc;
297 ExprList *pEList = p->pEList;
298
299 /* Look up every table in the table list.
300 */
301 for(i=0; i<pTabList->nId; i++){
302 if( pTabList->a[i].pTab ){
303 /* This routine has run before! No need to continue */
304 return 0;
305 }
306 pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
307 if( pTabList->a[i].pTab==0 ){
308 sqliteSetString(&pParse->zErrMsg, "no such table: ",
309 pTabList->a[i].zName, 0);
310 pParse->nErr++;
311 return 1;
312 }
313 }
314
315 /* If the list of columns to retrieve is "*" then replace it with
316 ** a list of all columns from all tables.
317 */
318 if( pEList==0 ){
319 for(i=0; i<pTabList->nId; i++){
320 Table *pTab = pTabList->a[i].pTab;
321 for(j=0; j<pTab->nCol; j++){
322 Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0);
323 pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0);
324 pExpr->pLeft->token.z = pTab->zName;
325 pExpr->pLeft->token.n = strlen(pTab->zName);
326 pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0);
327 pExpr->pRight->token.z = pTab->aCol[j].zName;
328 pExpr->pRight->token.n = strlen(pTab->aCol[j].zName);
329 pEList = sqliteExprListAppend(pEList, pExpr, 0);
330 }
331 }
332 p->pEList = pEList;
333 }
334 return 0;
335}
336
337/*
338** This routine associates entries in an ORDER BY expression list with
339** columns in a result. For each ORDER BY expression, the opcode of
340** the top-level node is changed to TK_FIELD and the iField value of
341** the top-level node is filled in with column number and the iTable
342** value of the top-level node is filled with iTable parameter.
343**
344** If there are prior SELECT clauses, they are processed first. A match
345** in an earlier SELECT takes precedence over a later SELECT.
346**
347** Any entry that does not match is flagged as an error. The number
348** of errors is returned.
349*/
350static int matchOrderbyToColumn(
351 Parse *pParse, /* A place to leave error messages */
352 Select *pSelect, /* Match to result columns of this SELECT */
353 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
354 int iTable, /* Insert this this value in iTable */
355 int mustComplete /* If TRUE all ORDER BYs must match */
356){
357 int nErr = 0;
358 int i, j;
359 ExprList *pEList;
360
361 assert( pSelect && pOrderBy );
362 if( mustComplete ){
363 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
364 }
365 if( fillInColumnList(pParse, pSelect) ){
366 return 1;
367 }
368 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000369 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
370 return 1;
371 }
drhd8bc7082000-06-07 23:51:50 +0000372 }
373 pEList = pSelect->pEList;
374 for(i=0; i<pOrderBy->nExpr; i++){
375 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000376 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000377 if( pOrderBy->a[i].done ) continue;
378 for(j=0; j<pEList->nExpr; j++){
drhd8bc7082000-06-07 23:51:50 +0000379 if( pEList->a[i].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
380 char *zName = pEList->a[i].zName;
381 char *zLabel = 0;
drh92cd52f2000-06-08 01:55:29 +0000382 sqliteSetNString(&zLabel, pE->token.z, pE->token.n, 0);
drhd8bc7082000-06-07 23:51:50 +0000383 sqliteDequote(zLabel);
384 if( sqliteStrICmp(zName, zLabel)==0 ){
385 match = 1;
386 }
387 }
388 if( match==0 && sqliteExprCompare(pE, pEList->a[i].pExpr) ){
389 match = 1;
390 }
391 if( match ){
392 pE->op = TK_FIELD;
393 pE->iField = j;
394 pE->iTable = iTable;
395 pOrderBy->a[i].done = 1;
396 break;
397 }
398 }
drh92cd52f2000-06-08 01:55:29 +0000399 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000400 char zBuf[30];
401 sprintf(zBuf,"%d",i+1);
402 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
403 " does not match any result column", 0);
404 pParse->nErr++;
405 nErr++;
406 break;
407 }
408 }
409 return nErr;
410}
411
412/*
413** Get a VDBE for the given parser context. Create a new one if necessary.
414** If an error occurs, return NULL and leave a message in pParse.
415*/
416Vdbe *sqliteGetVdbe(Parse *pParse){
417 Vdbe *v = pParse->pVdbe;
418 if( v==0 ){
419 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
420 }
421 if( v==0 ){
422 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
423 pParse->nErr++;
424 }
425 return v;
426}
427
428
429/*
drh82c3d632000-06-06 21:56:07 +0000430** This routine is called to process a query that is really the union
431** or intersection of two or more separate queries.
432*/
433static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000434 int rc; /* Success code from a subroutine */
435 Select *pPrior; /* Another SELECT immediately to our left */
436 Vdbe *v; /* Generate code to this VDBE */
437 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000438
drhd8bc7082000-06-07 23:51:50 +0000439 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
440 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000441 */
drhd8bc7082000-06-07 23:51:50 +0000442 assert( p->pPrior!=0 );
443 pPrior = p->pPrior;
444 if( pPrior->pOrderBy ){
445 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
446 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000447 pParse->nErr++;
448 return 1;
449 }
450
drhd8bc7082000-06-07 23:51:50 +0000451 /* Make sure we have a valid query engine. If not, create a new one.
452 */
453 v = sqliteGetVdbe(pParse);
454 if( v==0 ) return 1;
455
456 /* Process the UNION or INTERSECTION
457 */
drh10e5e3c2000-06-08 00:19:02 +0000458 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000459 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000460 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000461 case TK_EXCEPT:
462 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000463 int unionTab; /* Cursor number of the temporary table holding result */
464 int op; /* One of the SRT_ operations to apply to self */
465 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000466
drhd8bc7082000-06-07 23:51:50 +0000467 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
468 if( eDest==priorOp ){
469 /* We can reuse a temporary table generated by a SELECT to our
470 ** right. This also means we are not the right-most select and so
471 ** we cannot have an ORDER BY clause
472 */
drh82c3d632000-06-06 21:56:07 +0000473 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000474 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000475 }else{
drhd8bc7082000-06-07 23:51:50 +0000476 /* We will need to create our own temporary table to hold the
477 ** intermediate results.
478 */
479 unionTab = pParse->nTab++;
480 if( p->pOrderBy
481 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
482 return 1;
483 }
drh82c3d632000-06-06 21:56:07 +0000484 sqliteVdbeAddOp(v, OP_Open, unionTab, 1, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000485 if( p->op!=TK_ALL ){
486 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0);
487 }
drh82c3d632000-06-06 21:56:07 +0000488 }
drhd8bc7082000-06-07 23:51:50 +0000489
490 /* Code the SELECT statements to our left
491 */
492 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000493 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000494
495 /* Code the current SELECT statement
496 */
497 switch( p->op ){
498 case TK_EXCEPT: op = SRT_Except; break;
499 case TK_UNION: op = SRT_Union; break;
500 case TK_ALL: op = SRT_Table; break;
501 }
drh82c3d632000-06-06 21:56:07 +0000502 p->pPrior = 0;
503 rc = sqliteSelect(pParse, p, op, unionTab);
504 p->pPrior = pPrior;
505 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000506
507 /* Convert the data in the temporary table into whatever form
508 ** it is that we currently need.
509 */
510 if( eDest!=priorOp ){
drh82c3d632000-06-06 21:56:07 +0000511 int iCont, iBreak;
512 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000513 generateColumnNames(pParse, 0, p->pEList);
drh92dba242000-06-08 00:28:51 +0000514 if( p->pOrderBy ){
515 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
516 }
drh82c3d632000-06-06 21:56:07 +0000517 iBreak = sqliteVdbeMakeLabel(v);
518 iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0);
519 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000520 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000521 iCont, iBreak);
522 if( rc ) return 1;
523 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
524 sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak);
drhd8bc7082000-06-07 23:51:50 +0000525 if( p->pOrderBy ){
526 generateSortTail(v, p->pEList->nExpr);
527 }
drh82c3d632000-06-06 21:56:07 +0000528 }
529 break;
530 }
531 case TK_INTERSECT: {
532 int tab1, tab2;
drh82c3d632000-06-06 21:56:07 +0000533 int iCont, iBreak;
534
drhd8bc7082000-06-07 23:51:50 +0000535 /* INTERSECT is different from the others since it requires
536 ** two temporary tables. Hence it has its own case. Begine
537 ** by allocating the tables we will need.
538 */
drh82c3d632000-06-06 21:56:07 +0000539 tab1 = pParse->nTab++;
540 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000541 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
542 return 1;
543 }
drh82c3d632000-06-06 21:56:07 +0000544 sqliteVdbeAddOp(v, OP_Open, tab1, 1, 0, 0);
545 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000546
547 /* Code the SELECTs to our left into temporary table "tab1".
548 */
drh82c3d632000-06-06 21:56:07 +0000549 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
550 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000551
552 /* Code the current SELECT into temporary table "tab2"
553 */
drh82c3d632000-06-06 21:56:07 +0000554 sqliteVdbeAddOp(v, OP_Open, tab2, 1, 0, 0);
555 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0);
556 p->pPrior = 0;
557 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
558 p->pPrior = pPrior;
559 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000560
561 /* Generate code to take the intersection of the two temporary
562 ** tables.
563 */
drh82c3d632000-06-06 21:56:07 +0000564 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000565 generateColumnNames(pParse, 0, p->pEList);
drh92dba242000-06-08 00:28:51 +0000566 if( p->pOrderBy ){
567 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
568 }
drh82c3d632000-06-06 21:56:07 +0000569 iBreak = sqliteVdbeMakeLabel(v);
570 iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0);
571 sqliteVdbeAddOp(v, OP_Key, tab1, 0, 0, 0);
572 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0);
573 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000574 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000575 iCont, iBreak);
576 if( rc ) return 1;
577 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
578 sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak);
579 sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000580 if( p->pOrderBy ){
581 generateSortTail(v, p->pEList->nExpr);
582 }
drh82c3d632000-06-06 21:56:07 +0000583 break;
584 }
585 }
586 assert( p->pEList && pPrior->pEList );
587 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000588 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
589 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000590 pParse->nErr++;
591 return 1;
drh22827922000-06-06 17:27:05 +0000592 }
drh10e5e3c2000-06-08 00:19:02 +0000593 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000594 return 0;
595}
596
597/*
drh9bb61fe2000-06-05 16:01:39 +0000598** Generate code for the given SELECT statement.
599**
drhfef52082000-06-06 01:50:43 +0000600** The results are distributed in various ways depending on the
601** value of eDest and iParm.
602**
603** eDest Value Result
604** ------------ -------------------------------------------
605** SRT_Callback Invoke the callback for each row of the result.
606**
607** SRT_Mem Store first result in memory cell iParm
608**
609** SRT_Set Store results as keys of a table with cursor iParm
610**
drh82c3d632000-06-06 21:56:07 +0000611** SRT_Union Store results as a key in a temporary table iParm
612**
613** SRT_Except Remove results form the temporary talbe iParm.
drh9bb61fe2000-06-05 16:01:39 +0000614**
615** This routine returns the number of errors. If any errors are
616** encountered, then an appropriate error message is left in
617** pParse->zErrMsg.
618**
619** This routine does NOT free the Select structure passed in. The
620** calling function needs to do that.
621*/
622int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000623 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000624 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000625 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000626 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000627){
drhd8bc7082000-06-07 23:51:50 +0000628 int i;
drhcce7d172000-05-31 15:34:51 +0000629 WhereInfo *pWInfo;
630 Vdbe *v;
631 int isAgg = 0; /* True for select lists like "count(*)" */
drh9bb61fe2000-06-05 16:01:39 +0000632 ExprList *pEList; /* List of fields to extract. NULL means "*" */
633 IdList *pTabList; /* List of tables to select from */
634 Expr *pWhere; /* The WHERE clause. May be NULL */
635 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000636 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
637 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000638 int isDistinct; /* True if the DISTINCT keyword is present */
639 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000640 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000641
drh82c3d632000-06-06 21:56:07 +0000642 /* If there is are a sequence of queries, do the earlier ones first.
643 */
644 if( p->pPrior ){
645 return multiSelect(pParse, p, eDest, iParm);
646 }
647
648 /* Make local copies of the parameters for this query.
649 */
drh9bb61fe2000-06-05 16:01:39 +0000650 pTabList = p->pSrc;
651 pWhere = p->pWhere;
652 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000653 pGroupBy = p->pGroupBy;
654 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000655 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000656
drh10e5e3c2000-06-08 00:19:02 +0000657 /* Save the current value of pParse->nTab. Restore this value before
658 ** we exit.
659 */
660 base = pParse->nTab;
661
drh9bb61fe2000-06-05 16:01:39 +0000662 /*
663 ** Do not even attempt to generate any code if we have already seen
664 ** errors before this routine starts.
665 */
drh10e5e3c2000-06-08 00:19:02 +0000666 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000667 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000668
drhd8bc7082000-06-07 23:51:50 +0000669 /* Look up every table in the table list and create an appropriate
670 ** columnlist in pEList if there isn't one already. (The parser leaves
671 ** a NULL in the pEList field if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000672 */
drhd8bc7082000-06-07 23:51:50 +0000673 if( fillInColumnList(pParse, p) ){
674 return 1;
drhcce7d172000-05-31 15:34:51 +0000675 }
drhd8bc7082000-06-07 23:51:50 +0000676 pEList = p->pEList;
drhcce7d172000-05-31 15:34:51 +0000677
drh19a775c2000-06-05 18:54:46 +0000678 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000679 ** necessary. This must be done early to allocate the cursor before
680 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000681 */
682 if( isDistinct ){
683 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000684 }else{
685 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000686 }
687
drh22827922000-06-06 17:27:05 +0000688 /* If writing to memory or generating a set
689 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000690 */
drhfef52082000-06-06 01:50:43 +0000691 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000692 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
693 "a SELECT that is part of an expression", 0);
694 pParse->nErr++;
695 return 1;
696 }
697
drh22827922000-06-06 17:27:05 +0000698 /* ORDER BY is ignored if we are not sending the result to a callback.
699 */
700 if( eDest!=SRT_Callback ){
701 pOrderBy = 0;
702 }
703
704 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000705 */
706 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000707 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
708 }
709 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
710 if( pOrderBy ){
711 for(i=0; i<pOrderBy->nExpr; i++){
712 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
713 }
714 }
drh22827922000-06-06 17:27:05 +0000715 if( pGroupBy ){
716 for(i=0; i<pGroupBy->nExpr; i++){
717 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
718 }
719 }
720 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
721
drh10e5e3c2000-06-08 00:19:02 +0000722 /* At this point, we should have allocated all the cursors that we
723 ** need to handle subquerys and temporary tables. From here on we
724 ** are committed to keeping the same value for pParse->nTab.
725 **
726 ** Resolve the field names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000727 */
drh4794b982000-06-06 13:54:14 +0000728 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000729 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000730 return 1;
drhcce7d172000-05-31 15:34:51 +0000731 }
drh22827922000-06-06 17:27:05 +0000732 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000733 return 1;
drhcce7d172000-05-31 15:34:51 +0000734 }
735 }
drhcce7d172000-05-31 15:34:51 +0000736 if( pWhere ){
737 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000738 return 1;
drhcce7d172000-05-31 15:34:51 +0000739 }
740 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000741 return 1;
drhcce7d172000-05-31 15:34:51 +0000742 }
743 }
744 if( pOrderBy ){
745 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000746 Expr *pE = pOrderBy->a[i].pExpr;
747 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000748 return 1;
drhcce7d172000-05-31 15:34:51 +0000749 }
drh22827922000-06-06 17:27:05 +0000750 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000751 return 1;
drhcce7d172000-05-31 15:34:51 +0000752 }
753 }
754 }
drh22827922000-06-06 17:27:05 +0000755 if( pGroupBy ){
756 for(i=0; i<pGroupBy->nExpr; i++){
757 Expr *pE = pGroupBy->a[i].pExpr;
758 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
759 return 1;
760 }
761 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
762 return 1;
763 }
764 }
765 }
766 if( pHaving ){
767 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000768 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
769 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000770 pParse->nErr++;
771 return 1;
772 }
773 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
774 return 1;
775 }
drhda932812000-06-06 18:00:15 +0000776 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000777 return 1;
778 }
drhcce7d172000-05-31 15:34:51 +0000779 }
780
drh22827922000-06-06 17:27:05 +0000781 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000782 */
drh22827922000-06-06 17:27:05 +0000783 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000784 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000785 for(i=0; i<pEList->nExpr; i++){
786 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
787 return 1;
788 }
789 }
790 if( pGroupBy ){
791 for(i=0; i<pGroupBy->nExpr; i++){
792 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
793 return 1;
794 }
795 }
796 }
797 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
798 return 1;
799 }
drh191b6902000-06-08 11:13:01 +0000800 if( pOrderBy ){
801 for(i=0; i<pOrderBy->nExpr; i++){
802 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
803 return 1;
804 }
805 }
806 }
drhefb72512000-05-31 20:00:52 +0000807 }
808
drhcce7d172000-05-31 15:34:51 +0000809 /* Begin generating code.
810 */
811 v = pParse->pVdbe;
812 if( v==0 ){
813 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
814 }
drh9bb61fe2000-06-05 16:01:39 +0000815 if( v==0 ){
816 sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
817 pParse->nErr++;
818 return 1;
819 }
drhcce7d172000-05-31 15:34:51 +0000820 if( pOrderBy ){
821 sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
822 }
823
drh22827922000-06-06 17:27:05 +0000824 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000825 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000826 */
drhfef52082000-06-06 01:50:43 +0000827 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000828 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000829 }
830
drh22827922000-06-06 17:27:05 +0000831 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000832 */
833 if( isAgg ){
drh22827922000-06-06 17:27:05 +0000834 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000835 }
836
drh19a775c2000-06-05 18:54:46 +0000837 /* Initialize the memory cell to NULL
838 */
drhfef52082000-06-06 01:50:43 +0000839 if( eDest==SRT_Mem ){
drh19a775c2000-06-05 18:54:46 +0000840 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000841 sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
drh19a775c2000-06-05 18:54:46 +0000842 }
843
drhcce7d172000-05-31 15:34:51 +0000844 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000845 */
drh19a775c2000-06-05 18:54:46 +0000846 if( isDistinct ){
drheec553b2000-06-02 01:51:20 +0000847 sqliteVdbeAddOp(v, OP_Open, distinct, 1, 0, 0);
drhefb72512000-05-31 20:00:52 +0000848 }
drhcce7d172000-05-31 15:34:51 +0000849 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000850 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000851
drh22827922000-06-06 17:27:05 +0000852 /* Use the standard inner loop if we are not dealing with
853 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000854 */
drhda9d6c42000-05-31 18:20:14 +0000855 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000856 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000857 pWInfo->iContinue, pWInfo->iBreak) ){
858 return 1;
drhda9d6c42000-05-31 18:20:14 +0000859 }
drhcce7d172000-05-31 15:34:51 +0000860 }
drhefb72512000-05-31 20:00:52 +0000861
drh22827922000-06-06 17:27:05 +0000862 /* If we are dealing with aggregates, then to the special aggregate
863 ** processing.
drhefb72512000-05-31 20:00:52 +0000864 */
drh22827922000-06-06 17:27:05 +0000865 else{
866 int doFocus;
867 if( pGroupBy ){
868 for(i=0; i<pGroupBy->nExpr; i++){
869 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
870 }
871 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0);
872 doFocus = 1;
873 }else{
874 doFocus = 0;
875 for(i=0; i<pParse->nAgg; i++){
876 if( !pParse->aAgg[i].isAgg ){
877 doFocus = 1;
878 break;
879 }
880 }
881 if( doFocus ){
882 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
883 }
drhcce7d172000-05-31 15:34:51 +0000884 }
drh22827922000-06-06 17:27:05 +0000885 if( doFocus ){
886 int lbl1 = sqliteVdbeMakeLabel(v);
887 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0);
888 for(i=0; i<pParse->nAgg; i++){
889 if( pParse->aAgg[i].isAgg ) continue;
890 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
891 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000892 }
drh22827922000-06-06 17:27:05 +0000893 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000894 }
drh22827922000-06-06 17:27:05 +0000895 for(i=0; i<pParse->nAgg; i++){
896 Expr *pE;
897 int op;
898 if( !pParse->aAgg[i].isAgg ) continue;
899 pE = pParse->aAgg[i].pExpr;
900 if( pE==0 ){
901 sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0);
902 continue;
903 }
904 assert( pE->op==TK_AGG_FUNCTION );
905 assert( pE->pList!=0 && pE->pList->nExpr==1 );
906 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
907 sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0);
908 switch( pE->iField ){
909 case FN_Min: op = OP_Min; break;
910 case FN_Max: op = OP_Max; break;
911 case FN_Avg: op = OP_Add; break;
912 case FN_Sum: op = OP_Add; break;
913 }
914 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
915 sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
916 }
drhcce7d172000-05-31 15:34:51 +0000917 }
918
drh22827922000-06-06 17:27:05 +0000919
drhcce7d172000-05-31 15:34:51 +0000920 /* End the database scan loop.
921 */
922 sqliteWhereEnd(pWInfo);
923
drh22827922000-06-06 17:27:05 +0000924 /* If we are processing aggregates, we need to set up a second loop
925 ** over all of the aggregate values and process them.
926 */
927 if( isAgg ){
928 int endagg = sqliteVdbeMakeLabel(v);
929 int startagg;
930 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0);
931 pParse->useAgg = 1;
932 if( pHaving ){
933 sqliteExprIfFalse(pParse, pHaving, startagg);
934 }
drh82c3d632000-06-06 21:56:07 +0000935 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000936 startagg, endagg) ){
937 return 1;
938 }
939 sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0);
940 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg);
941 pParse->useAgg = 0;
942 }
943
drhcce7d172000-05-31 15:34:51 +0000944 /* If there is an ORDER BY clause, then we need to sort the results
945 ** and send them to the callback one by one.
946 */
947 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +0000948 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +0000949 }
drh10e5e3c2000-06-08 00:19:02 +0000950 pParse->nTab = base;
drh9bb61fe2000-06-05 16:01:39 +0000951 return 0;
drhcce7d172000-05-31 15:34:51 +0000952}