blob: e964f48fd77d23fce0d72d62b2e3048c9074dd55 [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**
drh6977fea2002-10-22 23:38:04 +000015** $Id: select.c,v 1.114 2002/10/22 23:38:04 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drh315555c2002-10-20 15:53:03 +000019
drhcce7d172000-05-31 15:34:51 +000020/*
drh9bb61fe2000-06-05 16:01:39 +000021** Allocate a new Select structure and return a pointer to that
22** structure.
drhcce7d172000-05-31 15:34:51 +000023*/
drh9bb61fe2000-06-05 16:01:39 +000024Select *sqliteSelectNew(
drhdaffd0e2001-04-11 14:28:42 +000025 ExprList *pEList, /* which columns to include in the result */
drhad3cab52002-05-24 02:04:32 +000026 SrcList *pSrc, /* the FROM clause -- which tables to scan */
drhdaffd0e2001-04-11 14:28:42 +000027 Expr *pWhere, /* the WHERE clause */
28 ExprList *pGroupBy, /* the GROUP BY clause */
29 Expr *pHaving, /* the HAVING clause */
30 ExprList *pOrderBy, /* the ORDER BY clause */
drh9bbca4c2001-11-06 04:00:18 +000031 int isDistinct, /* true if the DISTINCT keyword is present */
32 int nLimit, /* LIMIT value. -1 means not used */
33 int nOffset /* OFFSET value. -1 means not used */
drh9bb61fe2000-06-05 16:01:39 +000034){
35 Select *pNew;
36 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000037 if( pNew==0 ){
38 sqliteExprListDelete(pEList);
drhad3cab52002-05-24 02:04:32 +000039 sqliteSrcListDelete(pSrc);
drhdaffd0e2001-04-11 14:28:42 +000040 sqliteExprDelete(pWhere);
41 sqliteExprListDelete(pGroupBy);
42 sqliteExprDelete(pHaving);
43 sqliteExprListDelete(pOrderBy);
44 }else{
45 pNew->pEList = pEList;
46 pNew->pSrc = pSrc;
47 pNew->pWhere = pWhere;
48 pNew->pGroupBy = pGroupBy;
49 pNew->pHaving = pHaving;
50 pNew->pOrderBy = pOrderBy;
51 pNew->isDistinct = isDistinct;
52 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000053 pNew->nLimit = nLimit;
54 pNew->nOffset = nOffset;
drhdaffd0e2001-04-11 14:28:42 +000055 }
drh9bb61fe2000-06-05 16:01:39 +000056 return pNew;
57}
58
59/*
drh01f3f252002-05-24 16:14:15 +000060** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
61** type of join. Return an integer constant that expresses that type
62** in terms of the following bit values:
63**
64** JT_INNER
65** JT_OUTER
66** JT_NATURAL
67** JT_LEFT
68** JT_RIGHT
69**
70** A full outer join is the combination of JT_LEFT and JT_RIGHT.
71**
72** If an illegal or unsupported join type is seen, then still return
73** a join type, but put an error in the pParse structure.
74*/
75int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
76 int jointype = 0;
77 Token *apAll[3];
78 Token *p;
79 static struct {
80 const char *zKeyword;
81 int nChar;
82 int code;
83 } keywords[] = {
84 { "natural", 7, JT_NATURAL },
drh195e6962002-05-25 00:18:20 +000085 { "left", 4, JT_LEFT|JT_OUTER },
86 { "right", 5, JT_RIGHT|JT_OUTER },
87 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
drh01f3f252002-05-24 16:14:15 +000088 { "outer", 5, JT_OUTER },
89 { "inner", 5, JT_INNER },
90 { "cross", 5, JT_INNER },
91 };
92 int i, j;
93 apAll[0] = pA;
94 apAll[1] = pB;
95 apAll[2] = pC;
drh195e6962002-05-25 00:18:20 +000096 for(i=0; i<3 && apAll[i]; i++){
drh01f3f252002-05-24 16:14:15 +000097 p = apAll[i];
98 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
99 if( p->n==keywords[j].nChar
100 && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
101 jointype |= keywords[j].code;
102 break;
103 }
104 }
105 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
106 jointype |= JT_ERROR;
107 break;
108 }
109 }
drhad2d8302002-05-24 20:31:36 +0000110 if(
111 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
drh195e6962002-05-25 00:18:20 +0000112 (jointype & JT_ERROR)!=0
drhad2d8302002-05-24 20:31:36 +0000113 ){
drh01f3f252002-05-24 16:14:15 +0000114 static Token dummy = { 0, 0 };
115 char *zSp1 = " ", *zSp2 = " ";
116 if( pB==0 ){ pB = &dummy; zSp1 = 0; }
117 if( pC==0 ){ pC = &dummy; zSp2 = 0; }
118 sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
119 pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
120 pParse->nErr++;
121 jointype = JT_INNER;
drh195e6962002-05-25 00:18:20 +0000122 }else if( jointype & JT_RIGHT ){
123 sqliteSetString(&pParse->zErrMsg,
124 "RIGHT and FULL OUTER JOINs are not currently supported", 0);
125 pParse->nErr++;
126 jointype = JT_INNER;
drh01f3f252002-05-24 16:14:15 +0000127 }
128 return jointype;
129}
130
131/*
drhad2d8302002-05-24 20:31:36 +0000132** Return the index of a column in a table. Return -1 if the column
133** is not contained in the table.
134*/
135static int columnIndex(Table *pTab, const char *zCol){
136 int i;
137 for(i=0; i<pTab->nCol; i++){
138 if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
139 }
140 return -1;
141}
142
143/*
144** Add a term to the WHERE expression in *ppExpr that requires the
145** zCol column to be equal in the two tables pTab1 and pTab2.
146*/
147static void addWhereTerm(
148 const char *zCol, /* Name of the column */
149 const Table *pTab1, /* First table */
150 const Table *pTab2, /* Second table */
151 Expr **ppExpr /* Add the equality term to this expression */
152){
153 Token dummy;
154 Expr *pE1a, *pE1b, *pE1c;
155 Expr *pE2a, *pE2b, *pE2c;
156 Expr *pE;
157
158 dummy.z = zCol;
159 dummy.n = strlen(zCol);
drh4b59ab52002-08-24 18:24:51 +0000160 dummy.dyn = 0;
drhad2d8302002-05-24 20:31:36 +0000161 pE1a = sqliteExpr(TK_ID, 0, 0, &dummy);
162 pE2a = sqliteExpr(TK_ID, 0, 0, &dummy);
163 dummy.z = pTab1->zName;
164 dummy.n = strlen(dummy.z);
165 pE1b = sqliteExpr(TK_ID, 0, 0, &dummy);
166 dummy.z = pTab2->zName;
167 dummy.n = strlen(dummy.z);
168 pE2b = sqliteExpr(TK_ID, 0, 0, &dummy);
169 pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0);
170 pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0);
171 pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0);
drh1cc093c2002-06-24 22:01:57 +0000172 pE->isJoinExpr = 1;
drhad2d8302002-05-24 20:31:36 +0000173 if( *ppExpr ){
174 *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0);
175 }else{
176 *ppExpr = pE;
177 }
178}
179
180/*
drh1cc093c2002-06-24 22:01:57 +0000181** Set the Expr.isJoinExpr flag on all terms of the given expression.
182**
183** The Expr.isJoinExpr flag is used at on terms of an expression to tell
184** the LEFT OUTER JOIN processing logic that this term is part of the
185** join restriction and not a part of the more general WHERE clause.
186*/
187static void setJoinExpr(Expr *p){
188 while( p ){
189 p->isJoinExpr = 1;
190 setJoinExpr(p->pLeft);
191 p = p->pRight;
192 }
193}
194
195/*
drhad2d8302002-05-24 20:31:36 +0000196** This routine processes the join information for a SELECT statement.
197** ON and USING clauses are converted into extra terms of the WHERE clause.
198** NATURAL joins also create extra WHERE clause terms.
199**
200** This routine returns the number of errors encountered.
201*/
202static int sqliteProcessJoin(Parse *pParse, Select *p){
203 SrcList *pSrc;
204 int i, j;
205 pSrc = p->pSrc;
206 for(i=0; i<pSrc->nSrc-1; i++){
207 struct SrcList_item *pTerm = &pSrc->a[i];
208 struct SrcList_item *pOther = &pSrc->a[i+1];
209
210 if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
211
212 /* When the NATURAL keyword is present, add WHERE clause terms for
213 ** every column that the two tables have in common.
214 */
215 if( pTerm->jointype & JT_NATURAL ){
216 Table *pTab;
217 if( pTerm->pOn || pTerm->pUsing ){
218 sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have "
219 "an ON or USING clause", 0);
220 pParse->nErr++;
221 return 1;
222 }
223 pTab = pTerm->pTab;
224 for(j=0; j<pTab->nCol; j++){
225 if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
226 addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
227 }
228 }
229 }
230
231 /* Disallow both ON and USING clauses in the same join
232 */
233 if( pTerm->pOn && pTerm->pUsing ){
234 sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING "
235 "clauses in the same join", 0);
236 pParse->nErr++;
237 return 1;
238 }
239
240 /* Add the ON clause to the end of the WHERE clause, connected by
241 ** and AND operator.
242 */
243 if( pTerm->pOn ){
drh1cc093c2002-06-24 22:01:57 +0000244 setJoinExpr(pTerm->pOn);
drhad2d8302002-05-24 20:31:36 +0000245 if( p->pWhere==0 ){
246 p->pWhere = pTerm->pOn;
247 }else{
248 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
249 }
250 pTerm->pOn = 0;
251 }
252
253 /* Create extra terms on the WHERE clause for each column named
254 ** in the USING clause. Example: If the two tables to be joined are
255 ** A and B and the USING clause names X, Y, and Z, then add this
256 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
257 ** Report an error if any column mentioned in the USING clause is
258 ** not contained in both tables to be joined.
259 */
260 if( pTerm->pUsing ){
261 IdList *pList;
262 int j;
263 assert( i<pSrc->nSrc-1 );
264 pList = pTerm->pUsing;
265 for(j=0; j<pList->nId; j++){
drhbf5cd972002-06-24 12:20:23 +0000266 if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 ||
267 columnIndex(pOther->pTab, pList->a[j].zName)<0 ){
drhad2d8302002-05-24 20:31:36 +0000268 sqliteSetString(&pParse->zErrMsg, "cannot join using column ",
drhbf5cd972002-06-24 12:20:23 +0000269 pList->a[j].zName, " - column not present in both tables", 0);
drhad2d8302002-05-24 20:31:36 +0000270 pParse->nErr++;
271 return 1;
272 }
drhbf5cd972002-06-24 12:20:23 +0000273 addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
drhad2d8302002-05-24 20:31:36 +0000274 }
275 }
276 }
277 return 0;
278}
279
280/*
drh9bb61fe2000-06-05 16:01:39 +0000281** Delete the given Select structure and all of its substructures.
282*/
283void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +0000284 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +0000285 sqliteExprListDelete(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000286 sqliteSrcListDelete(p->pSrc);
drh9bb61fe2000-06-05 16:01:39 +0000287 sqliteExprDelete(p->pWhere);
288 sqliteExprListDelete(p->pGroupBy);
289 sqliteExprDelete(p->pHaving);
290 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +0000291 sqliteSelectDelete(p->pPrior);
drha76b5df2002-02-23 02:32:10 +0000292 sqliteFree(p->zSelect);
drh9bb61fe2000-06-05 16:01:39 +0000293 sqliteFree(p);
294}
295
296/*
drh22827922000-06-06 17:27:05 +0000297** Delete the aggregate information from the parse structure.
298*/
drh1d83f052002-02-17 00:30:36 +0000299static void sqliteAggregateInfoReset(Parse *pParse){
drh22827922000-06-06 17:27:05 +0000300 sqliteFree(pParse->aAgg);
301 pParse->aAgg = 0;
302 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000303 pParse->useAgg = 0;
304}
305
306/*
drhc926afb2002-06-20 03:38:26 +0000307** Insert code into "v" that will push the record on the top of the
308** stack into the sorter.
309*/
310static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
311 char *zSortOrder;
312 int i;
313 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
314 if( zSortOrder==0 ) return;
315 for(i=0; i<pOrderBy->nExpr; i++){
drh38640e12002-07-05 21:42:36 +0000316 int order = pOrderBy->a[i].sortOrder;
317 int type;
318 int c;
319 if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
320 type = SQLITE_SO_TEXT;
321 }else if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_NUM ){
322 type = SQLITE_SO_NUM;
drh491791a2002-07-18 00:34:09 +0000323 }else if( pParse->db->file_format>=4 ){
drh38640e12002-07-05 21:42:36 +0000324 type = sqliteExprType(pOrderBy->a[i].pExpr);
325 }else{
326 type = SQLITE_SO_NUM;
327 }
328 if( (order & SQLITE_SO_DIRMASK)==SQLITE_SO_ASC ){
329 c = type==SQLITE_SO_TEXT ? 'A' : '+';
330 }else{
331 c = type==SQLITE_SO_TEXT ? 'D' : '-';
332 }
333 zSortOrder[i] = c;
drhc926afb2002-06-20 03:38:26 +0000334 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
335 }
336 zSortOrder[pOrderBy->nExpr] = 0;
337 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
338 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
339 sqliteFree(zSortOrder);
340 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
341}
342
343/*
drh38640e12002-07-05 21:42:36 +0000344** This routine adds a P3 argument to the last VDBE opcode that was
345** inserted. The P3 argument added is a string suitable for the
346** OP_MakeKey or OP_MakeIdxKey opcodes. The string consists of
347** characters 't' or 'n' depending on whether or not the various
348** fields of the key to be generated should be treated as numeric
349** or as text. See the OP_MakeKey and OP_MakeIdxKey opcode
350** documentation for additional information about the P3 string.
351** See also the sqliteAddIdxKeyType() routine.
352*/
353void sqliteAddKeyType(Vdbe *v, ExprList *pEList){
354 int nColumn = pEList->nExpr;
355 char *zType = sqliteMalloc( nColumn+1 );
356 int i;
357 if( zType==0 ) return;
358 for(i=0; i<nColumn; i++){
359 zType[i] = sqliteExprType(pEList->a[i].pExpr)==SQLITE_SO_NUM ? 'n' : 't';
360 }
361 zType[i] = 0;
362 sqliteVdbeChangeP3(v, -1, zType, nColumn);
363 sqliteFree(zType);
364}
365
366/*
drh22827922000-06-06 17:27:05 +0000367** This routine generates the code for the inside of the inner loop
368** of a SELECT.
drh82c3d632000-06-06 21:56:07 +0000369**
drh38640e12002-07-05 21:42:36 +0000370** If srcTab and nColumn are both zero, then the pEList expressions
371** are evaluated in order to get the data for this row. If nColumn>0
372** then data is pulled from srcTab and pEList is used only to get the
373** datatypes for each column.
drh22827922000-06-06 17:27:05 +0000374*/
375static int selectInnerLoop(
376 Parse *pParse, /* The parser context */
drhdf199a22002-06-14 22:38:41 +0000377 Select *p, /* The complete select statement being coded */
drh22827922000-06-06 17:27:05 +0000378 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +0000379 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +0000380 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +0000381 ExprList *pOrderBy, /* If not NULL, sort results using this key */
382 int distinct, /* If >=0, make sure results are distinct */
383 int eDest, /* How to dispose of the results */
384 int iParm, /* An argument to the disposal method */
385 int iContinue, /* Jump here to continue with next row */
386 int iBreak /* Jump here to break out of the inner loop */
387){
388 Vdbe *v = pParse->pVdbe;
389 int i;
drh38640e12002-07-05 21:42:36 +0000390
drhdaffd0e2001-04-11 14:28:42 +0000391 if( v==0 ) return 0;
drh38640e12002-07-05 21:42:36 +0000392 assert( pEList!=0 );
drh22827922000-06-06 17:27:05 +0000393
drhdf199a22002-06-14 22:38:41 +0000394 /* If there was a LIMIT clause on the SELECT statement, then do the check
395 ** to see if this row should be output.
396 */
397 if( pOrderBy==0 ){
398 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000399 int addr = sqliteVdbeCurrentAddr(v);
400 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+2);
401 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drhdf199a22002-06-14 22:38:41 +0000402 }
drhd11d3822002-06-21 23:01:49 +0000403 if( p->nLimit>=0 ){
404 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, iBreak);
drhdf199a22002-06-14 22:38:41 +0000405 }
406 }
407
drh967e8b72000-06-21 13:59:10 +0000408 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000409 */
drh38640e12002-07-05 21:42:36 +0000410 if( nColumn>0 ){
drh967e8b72000-06-21 13:59:10 +0000411 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000412 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000413 }
drh38640e12002-07-05 21:42:36 +0000414 }else{
415 nColumn = pEList->nExpr;
416 for(i=0; i<pEList->nExpr; i++){
417 sqliteExprCode(pParse, pEList->a[i].pExpr);
418 }
drh22827922000-06-06 17:27:05 +0000419 }
420
drhdaffd0e2001-04-11 14:28:42 +0000421 /* If the DISTINCT keyword was present on the SELECT statement
422 ** and this row has been seen before, then do not make this row
423 ** part of the result.
drh22827922000-06-06 17:27:05 +0000424 */
drhf5905aa2002-05-26 20:54:33 +0000425 if( distinct>=0 && pEList && pEList->nExpr>0 ){
drh0bd1f4e2002-06-06 18:54:39 +0000426#if NULL_ALWAYS_DISTINCT
427 sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7);
428#endif
drh99fcd712001-10-13 01:06:47 +0000429 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
drh491791a2002-07-18 00:34:09 +0000430 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pEList);
drhf5905aa2002-05-26 20:54:33 +0000431 sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3);
drh99fcd712001-10-13 01:06:47 +0000432 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
433 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drh99fcd712001-10-13 01:06:47 +0000434 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000435 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
drh22827922000-06-06 17:27:05 +0000436 }
drh82c3d632000-06-06 21:56:07 +0000437
drhc926afb2002-06-20 03:38:26 +0000438 switch( eDest ){
439 /* In this mode, write each query result to the key of the temporary
440 ** table iParm.
441 */
442 case SRT_Union: {
443 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
444 sqliteVdbeAddOp(v, OP_String, 0, 0);
445 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
446 break;
drh22827922000-06-06 17:27:05 +0000447 }
drh22827922000-06-06 17:27:05 +0000448
drhc926afb2002-06-20 03:38:26 +0000449 /* Store the result as data using a unique key.
450 */
451 case SRT_Table:
452 case SRT_TempTable: {
453 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
454 if( pOrderBy ){
455 pushOntoSorter(pParse, v, pOrderBy);
456 }else{
457 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
458 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
459 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
460 }
461 break;
462 }
drh82c3d632000-06-06 21:56:07 +0000463
drhc926afb2002-06-20 03:38:26 +0000464 /* Construct a record from the query result, but instead of
465 ** saving that record, use it as a key to delete elements from
466 ** the temporary table iParm.
467 */
468 case SRT_Except: {
469 int addr;
470 addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
471 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
472 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
473 break;
474 }
drh5974a302000-06-07 14:42:26 +0000475
drhc926afb2002-06-20 03:38:26 +0000476 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
477 ** then there should be a single item on the stack. Write this
478 ** item into the set table with bogus data.
479 */
480 case SRT_Set: {
drha9f9d1c2002-06-29 02:20:08 +0000481 int lbl = sqliteVdbeMakeLabel(v);
drhc926afb2002-06-20 03:38:26 +0000482 assert( nColumn==1 );
drha9f9d1c2002-06-29 02:20:08 +0000483 sqliteVdbeAddOp(v, OP_IsNull, -1, lbl);
drhc926afb2002-06-20 03:38:26 +0000484 if( pOrderBy ){
485 pushOntoSorter(pParse, v, pOrderBy);
486 }else{
drha9f9d1c2002-06-29 02:20:08 +0000487 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000488 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
489 }
drha9f9d1c2002-06-29 02:20:08 +0000490 sqliteVdbeResolveLabel(v, lbl);
drhc926afb2002-06-20 03:38:26 +0000491 break;
492 }
drh22827922000-06-06 17:27:05 +0000493
drhc926afb2002-06-20 03:38:26 +0000494 /* If this is a scalar select that is part of an expression, then
495 ** store the results in the appropriate memory cell and break out
496 ** of the scan loop.
497 */
498 case SRT_Mem: {
499 assert( nColumn==1 );
500 if( pOrderBy ){
501 pushOntoSorter(pParse, v, pOrderBy);
502 }else{
503 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
504 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
505 }
506 break;
507 }
drh22827922000-06-06 17:27:05 +0000508
drhf46f9052002-06-22 02:33:38 +0000509 /* Send the data to the callback function.
510 */
511 case SRT_Callback:
512 case SRT_Sorter: {
513 if( pOrderBy ){
514 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
515 pushOntoSorter(pParse, v, pOrderBy);
516 }else{
517 assert( eDest==SRT_Callback );
518 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0);
519 }
520 break;
521 }
522
drh142e30d2002-08-28 03:00:58 +0000523 /* Invoke a subroutine to handle the results. The subroutine itself
524 ** is responsible for popping the results off of the stack.
525 */
526 case SRT_Subroutine: {
drhac82fcf2002-09-08 17:23:41 +0000527 if( pOrderBy ){
528 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
529 pushOntoSorter(pParse, v, pOrderBy);
530 }else{
531 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
532 }
drh142e30d2002-08-28 03:00:58 +0000533 break;
534 }
535
drhc926afb2002-06-20 03:38:26 +0000536 /* Discard the results. This is used for SELECT statements inside
537 ** the body of a TRIGGER. The purpose of such selects is to call
538 ** user-defined functions that have side effects. We do not care
539 ** about the actual results of the select.
540 */
drhc926afb2002-06-20 03:38:26 +0000541 default: {
drhf46f9052002-06-22 02:33:38 +0000542 assert( eDest==SRT_Discard );
543 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000544 break;
545 }
drh82c3d632000-06-06 21:56:07 +0000546 }
547 return 0;
548}
549
550/*
drhd8bc7082000-06-07 23:51:50 +0000551** If the inner loop was generated using a non-null pOrderBy argument,
552** then the results were placed in a sorter. After the loop is terminated
553** we need to run the sorter and output the results. The following
554** routine generates the code needed to do that.
555*/
drhc926afb2002-06-20 03:38:26 +0000556static void generateSortTail(
557 Select *p, /* The SELECT statement */
558 Vdbe *v, /* Generate code into this VDBE */
559 int nColumn, /* Number of columns of data */
560 int eDest, /* Write the sorted results here */
561 int iParm /* Optional parameter associated with eDest */
562){
drhd8bc7082000-06-07 23:51:50 +0000563 int end = sqliteVdbeMakeLabel(v);
564 int addr;
drhf46f9052002-06-22 02:33:38 +0000565 if( eDest==SRT_Sorter ) return;
drh99fcd712001-10-13 01:06:47 +0000566 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
567 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drhdf199a22002-06-14 22:38:41 +0000568 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000569 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+4);
570 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
571 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drhdf199a22002-06-14 22:38:41 +0000572 }
drhd11d3822002-06-21 23:01:49 +0000573 if( p->nLimit>=0 ){
574 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, end);
drhdf199a22002-06-14 22:38:41 +0000575 }
drhc926afb2002-06-20 03:38:26 +0000576 switch( eDest ){
577 case SRT_Callback: {
578 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
579 break;
580 }
581 case SRT_Table:
582 case SRT_TempTable: {
583 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
584 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
585 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
586 break;
587 }
588 case SRT_Set: {
589 assert( nColumn==1 );
590 sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3);
591 sqliteVdbeAddOp(v, OP_String, 0, 0);
592 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
593 break;
594 }
595 case SRT_Mem: {
596 assert( nColumn==1 );
597 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
598 sqliteVdbeAddOp(v, OP_Goto, 0, end);
599 break;
600 }
drhac82fcf2002-09-08 17:23:41 +0000601 case SRT_Subroutine: {
602 int i;
603 for(i=0; i<nColumn; i++){
604 sqliteVdbeAddOp(v, OP_Column, -1-i, i);
605 }
606 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
607 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
608 break;
609 }
drhc926afb2002-06-20 03:38:26 +0000610 default: {
drhf46f9052002-06-22 02:33:38 +0000611 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000612 break;
613 }
614 }
drh99fcd712001-10-13 01:06:47 +0000615 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
616 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000617 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000618}
619
620/*
drh82c3d632000-06-06 21:56:07 +0000621** Generate code that will tell the VDBE how many columns there
622** are in the result and the name for each column. This information
623** is used to provide "argc" and "azCol[]" values in the callback.
624*/
drh832508b2002-03-02 17:04:07 +0000625static void generateColumnNames(
626 Parse *pParse, /* Parser context */
627 int base, /* VDBE cursor corresponding to first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000628 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000629 ExprList *pEList /* Expressions defining the result set */
630){
drhd8bc7082000-06-07 23:51:50 +0000631 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000632 int i;
drhdaffd0e2001-04-11 14:28:42 +0000633 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000634 pParse->colNamesSet = 1;
drh5080aaa2002-07-11 12:18:16 +0000635 if( pParse->db->flags & SQLITE_ReportTypes ){
636 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr*2, 0);
637 }else{
638 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
639 }
drh82c3d632000-06-06 21:56:07 +0000640 for(i=0; i<pEList->nExpr; i++){
641 Expr *p;
drhb1363202002-06-26 02:45:03 +0000642 char *zType = 0;
drh1bee3d72001-10-15 00:44:35 +0000643 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000644 if( pEList->a[i].zName ){
645 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000646 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
647 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000648 continue;
649 }
650 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000651 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000652 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
drhfa173a72002-07-10 21:26:00 +0000653 if( p->op==TK_COLUMN && pTabList ){
drh832508b2002-03-02 17:04:07 +0000654 Table *pTab = pTabList->a[p->iTable - base].pTab;
drh97665872002-02-13 23:22:53 +0000655 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000656 int iCol = p->iColumn;
657 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000658 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000659 if( iCol<0 ){
660 zCol = "_ROWID_";
661 zType = "INTEGER";
662 }else{
663 zCol = pTab->aCol[iCol].zName;
664 zType = pTab->aCol[iCol].zType;
665 }
drh6977fea2002-10-22 23:38:04 +0000666 if( p->span.z && p->span.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000667 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh6977fea2002-10-22 23:38:04 +0000668 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhfa173a72002-07-10 21:26:00 +0000669 sqliteVdbeCompressSpace(v, addr);
670 }else if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000671 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000672 char *zTab;
673
drh832508b2002-03-02 17:04:07 +0000674 zTab = pTabList->a[p->iTable - base].zAlias;
drh01a34662001-10-20 12:30:10 +0000675 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000676 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000677 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
678 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000679 sqliteFree(zName);
680 }else{
drh99fcd712001-10-13 01:06:47 +0000681 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000682 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000683 }
drh6977fea2002-10-22 23:38:04 +0000684 }else if( p->span.z && p->span.z[0] ){
drhfa173a72002-07-10 21:26:00 +0000685 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh6977fea2002-10-22 23:38:04 +0000686 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drh1bee3d72001-10-15 00:44:35 +0000687 sqliteVdbeCompressSpace(v, addr);
688 }else{
689 char zName[30];
690 assert( p->op!=TK_COLUMN || pTabList==0 );
691 sprintf(zName, "column%d", i+1);
692 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
693 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000694 }
drh5080aaa2002-07-11 12:18:16 +0000695 if( pParse->db->flags & SQLITE_ReportTypes ){
696 if( zType==0 ){
697 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
698 zType = "TEXT";
699 }else{
700 zType = "NUMERIC";
701 }
drhb1363202002-06-26 02:45:03 +0000702 }
drh5080aaa2002-07-11 12:18:16 +0000703 sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0);
704 sqliteVdbeChangeP3(v, -1, zType, P3_STATIC);
drhb1363202002-06-26 02:45:03 +0000705 }
drh82c3d632000-06-06 21:56:07 +0000706 }
707}
708
709/*
drhd8bc7082000-06-07 23:51:50 +0000710** Name of the connection operator, used for error messages.
711*/
712static const char *selectOpName(int id){
713 char *z;
714 switch( id ){
715 case TK_ALL: z = "UNION ALL"; break;
716 case TK_INTERSECT: z = "INTERSECT"; break;
717 case TK_EXCEPT: z = "EXCEPT"; break;
718 default: z = "UNION"; break;
719 }
720 return z;
721}
722
723/*
drh315555c2002-10-20 15:53:03 +0000724** Forward declaration
725*/
726static int fillInColumnList(Parse*, Select*);
727
728/*
drh22f70c32002-02-18 01:17:00 +0000729** Given a SELECT statement, generate a Table structure that describes
730** the result set of that SELECT.
731*/
732Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
733 Table *pTab;
734 int i;
735 ExprList *pEList;
drh22f70c32002-02-18 01:17:00 +0000736
737 if( fillInColumnList(pParse, pSelect) ){
738 return 0;
739 }
740 pTab = sqliteMalloc( sizeof(Table) );
741 if( pTab==0 ){
742 return 0;
743 }
744 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
745 pEList = pSelect->pEList;
746 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000747 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000748 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
749 for(i=0; i<pTab->nCol; i++){
750 Expr *p;
751 if( pEList->a[i].zName ){
752 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
drh6977fea2002-10-22 23:38:04 +0000753 }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
754 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
drhd820cb12002-02-18 03:21:45 +0000755 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
756 p->pRight->token.z[0] ){
757 sqliteSetNString(&pTab->aCol[i].zName,
758 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000759 }else{
760 char zBuf[30];
761 sprintf(zBuf, "column%d", i+1);
762 pTab->aCol[i].zName = sqliteStrDup(zBuf);
763 }
764 }
765 pTab->iPKey = -1;
766 return pTab;
767}
768
769/*
drhad2d8302002-05-24 20:31:36 +0000770** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000771**
drhad3cab52002-05-24 02:04:32 +0000772** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh22f70c32002-02-18 01:17:00 +0000773** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000774**
drhad2d8302002-05-24 20:31:36 +0000775** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
776** on joins and the ON and USING clause of joins.
777**
778** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000779** for instances of the "*" operator or the TABLE.* operator.
780** If found, expand each "*" to be every column in every table
781** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000782**
783** Return 0 on success. If there are problems, leave an error message
784** in pParse and return non-zero.
785*/
786static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000787 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000788 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000789 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000790 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000791
792 if( p==0 || p->pSrc==0 ) return 1;
793 pTabList = p->pSrc;
794 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000795
796 /* Look up every table in the table list.
797 */
drhad3cab52002-05-24 02:04:32 +0000798 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000799 if( pTabList->a[i].pTab ){
800 /* This routine has run before! No need to continue */
801 return 0;
802 }
drhdaffd0e2001-04-11 14:28:42 +0000803 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000804 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000805 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000806 if( pTabList->a[i].zAlias==0 ){
807 char zFakeName[60];
808 sprintf(zFakeName, "sqlite_subquery_%p_",
809 (void*)pTabList->a[i].pSelect);
810 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
811 }
drh22f70c32002-02-18 01:17:00 +0000812 pTabList->a[i].pTab = pTab =
813 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
814 pTabList->a[i].pSelect);
815 if( pTab==0 ){
816 return 1;
817 }
818 pTab->isTransient = 1;
819 }else{
drha76b5df2002-02-23 02:32:10 +0000820 /* An ordinary table or view name in the FROM clause */
821 pTabList->a[i].pTab = pTab =
822 sqliteFindTable(pParse->db, pTabList->a[i].zName);
823 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000824 sqliteSetString(&pParse->zErrMsg, "no such table: ",
825 pTabList->a[i].zName, 0);
826 pParse->nErr++;
827 return 1;
828 }
drha76b5df2002-02-23 02:32:10 +0000829 if( pTab->pSelect ){
drh417be792002-03-03 18:59:40 +0000830 if( sqliteViewGetColumnNames(pParse, pTab) ){
831 return 1;
832 }
drhd94a6692002-08-25 18:29:11 +0000833 sqliteSelectDelete(pTabList->a[i].pSelect);
drhff78bd22002-02-27 01:47:11 +0000834 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000835 }
drhd8bc7082000-06-07 23:51:50 +0000836 }
837 }
838
drhad2d8302002-05-24 20:31:36 +0000839 /* Process NATURAL keywords, and ON and USING clauses of joins.
840 */
841 if( sqliteProcessJoin(pParse, p) ) return 1;
842
drh7c917d12001-12-16 20:05:05 +0000843 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000844 ** all columns in all tables. And for every TABLE.* insert the names
845 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000846 ** with the TK_ALL operator for each "*" that it found in the column list.
847 ** The following code just has to locate the TK_ALL expressions and expand
848 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000849 **
850 ** The first loop just checks to see if there are any "*" operators
851 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000852 */
drh7c917d12001-12-16 20:05:05 +0000853 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000854 Expr *pE = pEList->a[k].pExpr;
855 if( pE->op==TK_ALL ) break;
856 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
857 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000858 }
drh54473222002-04-04 02:10:55 +0000859 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000860 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000861 /*
862 ** If we get here it means the result set contains one or more "*"
863 ** operators that need to be expanded. Loop through each expression
864 ** in the result set and expand them one by one.
865 */
drh7c917d12001-12-16 20:05:05 +0000866 struct ExprList_item *a = pEList->a;
867 ExprList *pNew = 0;
868 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000869 Expr *pE = a[k].pExpr;
870 if( pE->op!=TK_ALL &&
871 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
872 /* This particular expression does not need to be expanded.
873 */
drh7c917d12001-12-16 20:05:05 +0000874 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
875 pNew->a[pNew->nExpr-1].zName = a[k].zName;
876 a[k].pExpr = 0;
877 a[k].zName = 0;
878 }else{
drh54473222002-04-04 02:10:55 +0000879 /* This expression is a "*" or a "TABLE.*" and needs to be
880 ** expanded. */
881 int tableSeen = 0; /* Set to 1 when TABLE matches */
882 Token *pName; /* text of name of TABLE */
883 if( pE->op==TK_DOT && pE->pLeft ){
884 pName = &pE->pLeft->token;
885 }else{
886 pName = 0;
887 }
drhad3cab52002-05-24 02:04:32 +0000888 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000889 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000890 char *zTabName = pTabList->a[i].zAlias;
891 if( zTabName==0 || zTabName[0]==0 ){
892 zTabName = pTab->zName;
893 }
drhc754fa52002-05-27 03:25:51 +0000894 if( pName && (zTabName==0 || zTabName[0]==0 ||
895 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
896 zTabName[pName->n]!=0) ){
drh54473222002-04-04 02:10:55 +0000897 continue;
898 }
899 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000900 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000901 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000902 char *zName = pTab->aCol[j].zName;
903
904 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
905 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
906 /* In a NATURAL join, omit the join columns from the
907 ** table on the right */
908 continue;
909 }
910 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
911 /* In a join with a USING clause, omit columns in the
912 ** using clause from the table on the right. */
913 continue;
914 }
drh22f70c32002-02-18 01:17:00 +0000915 pRight = sqliteExpr(TK_ID, 0, 0, 0);
916 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000917 pRight->token.z = zName;
918 pRight->token.n = strlen(zName);
drh4b59ab52002-08-24 18:24:51 +0000919 pRight->token.dyn = 0;
drh4b59ab52002-08-24 18:24:51 +0000920 if( zTabName && pTabList->nSrc>1 ){
drh22f70c32002-02-18 01:17:00 +0000921 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh22f70c32002-02-18 01:17:00 +0000922 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
923 if( pExpr==0 ) break;
drh4b59ab52002-08-24 18:24:51 +0000924 pLeft->token.z = zTabName;
925 pLeft->token.n = strlen(zTabName);
926 pLeft->token.dyn = 0;
drh6977fea2002-10-22 23:38:04 +0000927 sqliteSetString((char**)&pExpr->span.z, zTabName, ".", zName, 0);
928 pExpr->span.n = strlen(pExpr->span.z);
929 pExpr->span.dyn = 1;
930 pExpr->token.z = 0;
931 pExpr->token.n = 0;
932 pExpr->token.dyn = 0;
drh7c917d12001-12-16 20:05:05 +0000933 }else{
drh22f70c32002-02-18 01:17:00 +0000934 pExpr = pRight;
drh6977fea2002-10-22 23:38:04 +0000935 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +0000936 }
drh7c917d12001-12-16 20:05:05 +0000937 pNew = sqliteExprListAppend(pNew, pExpr, 0);
938 }
drh17e24df2001-11-06 14:10:41 +0000939 }
drh54473222002-04-04 02:10:55 +0000940 if( !tableSeen ){
drhf5db2d32002-06-06 23:42:27 +0000941 if( pName ){
942 sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
943 pName->z, pName->n, 0);
944 }else{
945 sqliteSetString(&pParse->zErrMsg, "no tables specified", 0);
946 }
drh54473222002-04-04 02:10:55 +0000947 rc = 1;
948 }
drhd8bc7082000-06-07 23:51:50 +0000949 }
950 }
drh7c917d12001-12-16 20:05:05 +0000951 sqliteExprListDelete(pEList);
952 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000953 }
drh54473222002-04-04 02:10:55 +0000954 return rc;
drhd8bc7082000-06-07 23:51:50 +0000955}
956
957/*
drhff78bd22002-02-27 01:47:11 +0000958** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
959** in a select structure. It just sets the pointers to NULL. This
960** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
961** pointer is not NULL, this routine is called recursively on that pointer.
962**
963** This routine is called on the Select structure that defines a
964** VIEW in order to undo any bindings to tables. This is necessary
965** because those tables might be DROPed by a subsequent SQL command.
966*/
967void sqliteSelectUnbind(Select *p){
968 int i;
drhad3cab52002-05-24 02:04:32 +0000969 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +0000970 Table *pTab;
971 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +0000972 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +0000973 if( (pTab = pSrc->a[i].pTab)!=0 ){
974 if( pTab->isTransient ){
975 sqliteDeleteTable(0, pTab);
drh4b59ab52002-08-24 18:24:51 +0000976#if 0
drhff78bd22002-02-27 01:47:11 +0000977 sqliteSelectDelete(pSrc->a[i].pSelect);
978 pSrc->a[i].pSelect = 0;
drh4b59ab52002-08-24 18:24:51 +0000979#endif
drhff78bd22002-02-27 01:47:11 +0000980 }
981 pSrc->a[i].pTab = 0;
982 if( pSrc->a[i].pSelect ){
983 sqliteSelectUnbind(pSrc->a[i].pSelect);
984 }
985 }
986 }
987}
988
989/*
drhd8bc7082000-06-07 23:51:50 +0000990** This routine associates entries in an ORDER BY expression list with
991** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000992** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000993** the top-level node is filled in with column number and the iTable
994** value of the top-level node is filled with iTable parameter.
995**
996** If there are prior SELECT clauses, they are processed first. A match
997** in an earlier SELECT takes precedence over a later SELECT.
998**
999** Any entry that does not match is flagged as an error. The number
1000** of errors is returned.
1001*/
1002static int matchOrderbyToColumn(
1003 Parse *pParse, /* A place to leave error messages */
1004 Select *pSelect, /* Match to result columns of this SELECT */
1005 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +00001006 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +00001007 int mustComplete /* If TRUE all ORDER BYs must match */
1008){
1009 int nErr = 0;
1010 int i, j;
1011 ExprList *pEList;
1012
drhdaffd0e2001-04-11 14:28:42 +00001013 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001014 if( mustComplete ){
1015 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1016 }
1017 if( fillInColumnList(pParse, pSelect) ){
1018 return 1;
1019 }
1020 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +00001021 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
1022 return 1;
1023 }
drhd8bc7082000-06-07 23:51:50 +00001024 }
1025 pEList = pSelect->pEList;
1026 for(i=0; i<pOrderBy->nExpr; i++){
1027 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +00001028 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +00001029 if( pOrderBy->a[i].done ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001030 if( sqliteExprIsInteger(pE, &iCol) ){
1031 if( iCol<=0 || iCol>pEList->nExpr ){
1032 char zBuf[200];
1033 sprintf(zBuf,"ORDER BY position %d should be between 1 and %d",
1034 iCol, pEList->nExpr);
1035 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1036 pParse->nErr++;
1037 nErr++;
1038 break;
1039 }
1040 iCol--;
1041 }
1042 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001043 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001044 char *zName, *zLabel;
1045 zName = pEList->a[j].zName;
1046 assert( pE->token.z );
1047 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +00001048 sqliteDequote(zLabel);
1049 if( sqliteStrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001050 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001051 }
drh6e142f52000-06-08 13:36:40 +00001052 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001053 }
drhe4de1fe2002-06-02 16:09:01 +00001054 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
1055 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001056 }
1057 }
drhe4de1fe2002-06-02 16:09:01 +00001058 if( iCol>=0 ){
1059 pE->op = TK_COLUMN;
1060 pE->iColumn = iCol;
1061 pE->iTable = iTable;
1062 pOrderBy->a[i].done = 1;
1063 }
1064 if( iCol<0 && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +00001065 char zBuf[30];
1066 sprintf(zBuf,"%d",i+1);
1067 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
1068 " does not match any result column", 0);
1069 pParse->nErr++;
1070 nErr++;
1071 break;
1072 }
1073 }
1074 return nErr;
1075}
1076
1077/*
1078** Get a VDBE for the given parser context. Create a new one if necessary.
1079** If an error occurs, return NULL and leave a message in pParse.
1080*/
1081Vdbe *sqliteGetVdbe(Parse *pParse){
1082 Vdbe *v = pParse->pVdbe;
1083 if( v==0 ){
drh4c504392000-10-16 22:06:40 +00001084 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001085 }
drhd8bc7082000-06-07 23:51:50 +00001086 return v;
1087}
1088
1089
1090/*
drh82c3d632000-06-06 21:56:07 +00001091** This routine is called to process a query that is really the union
1092** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001093**
1094** "p" points to the right-most of the two queries. The results should
1095** be stored in eDest with parameter iParm.
drh82c3d632000-06-06 21:56:07 +00001096*/
1097static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +00001098 int rc; /* Success code from a subroutine */
1099 Select *pPrior; /* Another SELECT immediately to our left */
1100 Vdbe *v; /* Generate code to this VDBE */
drh82c3d632000-06-06 21:56:07 +00001101
drhd8bc7082000-06-07 23:51:50 +00001102 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
1103 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +00001104 */
drhdaffd0e2001-04-11 14:28:42 +00001105 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001106 pPrior = p->pPrior;
1107 if( pPrior->pOrderBy ){
1108 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
1109 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +00001110 pParse->nErr++;
1111 return 1;
1112 }
1113
drhd8bc7082000-06-07 23:51:50 +00001114 /* Make sure we have a valid query engine. If not, create a new one.
1115 */
1116 v = sqliteGetVdbe(pParse);
1117 if( v==0 ) return 1;
1118
drh1cc3d752002-03-23 00:31:29 +00001119 /* Create the destination temporary table if necessary
1120 */
1121 if( eDest==SRT_TempTable ){
1122 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1123 eDest = SRT_Table;
1124 }
1125
drhf46f9052002-06-22 02:33:38 +00001126 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001127 */
drh82c3d632000-06-06 21:56:07 +00001128 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001129 case TK_ALL: {
1130 if( p->pOrderBy==0 ){
1131 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
1132 if( rc ) return rc;
1133 p->pPrior = 0;
1134 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
1135 p->pPrior = pPrior;
1136 if( rc ) return rc;
1137 break;
1138 }
1139 /* For UNION ALL ... ORDER BY fall through to the next case */
1140 }
drh82c3d632000-06-06 21:56:07 +00001141 case TK_EXCEPT:
1142 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001143 int unionTab; /* Cursor number of the temporary table holding result */
1144 int op; /* One of the SRT_ operations to apply to self */
1145 int priorOp; /* The SRT_ operation to apply to prior selects */
drhc926afb2002-06-20 03:38:26 +00001146 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
drh82c3d632000-06-06 21:56:07 +00001147
drhd8bc7082000-06-07 23:51:50 +00001148 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drhc926afb2002-06-20 03:38:26 +00001149 if( eDest==priorOp && p->pOrderBy==0 ){
drhd8bc7082000-06-07 23:51:50 +00001150 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001151 ** right.
drhd8bc7082000-06-07 23:51:50 +00001152 */
drh82c3d632000-06-06 21:56:07 +00001153 unionTab = iParm;
1154 }else{
drhd8bc7082000-06-07 23:51:50 +00001155 /* We will need to create our own temporary table to hold the
1156 ** intermediate results.
1157 */
1158 unionTab = pParse->nTab++;
1159 if( p->pOrderBy
1160 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
1161 return 1;
1162 }
drhd8bc7082000-06-07 23:51:50 +00001163 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +00001164 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +00001165 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +00001166 }else{
drh99fcd712001-10-13 01:06:47 +00001167 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001168 }
drh82c3d632000-06-06 21:56:07 +00001169 }
drhd8bc7082000-06-07 23:51:50 +00001170
1171 /* Code the SELECT statements to our left
1172 */
drh832508b2002-03-02 17:04:07 +00001173 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001174 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001175
1176 /* Code the current SELECT statement
1177 */
1178 switch( p->op ){
1179 case TK_EXCEPT: op = SRT_Except; break;
1180 case TK_UNION: op = SRT_Union; break;
1181 case TK_ALL: op = SRT_Table; break;
1182 }
drh82c3d632000-06-06 21:56:07 +00001183 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001184 pOrderBy = p->pOrderBy;
1185 p->pOrderBy = 0;
drh832508b2002-03-02 17:04:07 +00001186 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001187 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001188 p->pOrderBy = pOrderBy;
drh82c3d632000-06-06 21:56:07 +00001189 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001190
1191 /* Convert the data in the temporary table into whatever form
1192 ** it is that we currently need.
1193 */
drhc926afb2002-06-20 03:38:26 +00001194 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001195 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001196 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001197 if( eDest==SRT_Callback ){
1198 generateColumnNames(pParse, p->base, 0, p->pEList);
1199 }
drh82c3d632000-06-06 21:56:07 +00001200 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001201 iCont = sqliteVdbeMakeLabel(v);
1202 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1203 iStart = sqliteVdbeCurrentAddr(v);
drh38640e12002-07-05 21:42:36 +00001204 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001205 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001206 iCont, iBreak);
1207 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001208 sqliteVdbeResolveLabel(v, iCont);
1209 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +00001210 sqliteVdbeResolveLabel(v, iBreak);
1211 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001212 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001213 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001214 }
drh82c3d632000-06-06 21:56:07 +00001215 }
1216 break;
1217 }
1218 case TK_INTERSECT: {
1219 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001220 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001221
drhd8bc7082000-06-07 23:51:50 +00001222 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001223 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001224 ** by allocating the tables we will need.
1225 */
drh82c3d632000-06-06 21:56:07 +00001226 tab1 = pParse->nTab++;
1227 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001228 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
1229 return 1;
1230 }
drhc6b52df2002-01-04 03:09:29 +00001231 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +00001232 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +00001233
1234 /* Code the SELECTs to our left into temporary table "tab1".
1235 */
drh832508b2002-03-02 17:04:07 +00001236 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001237 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001238
1239 /* Code the current SELECT into temporary table "tab2"
1240 */
drhc6b52df2002-01-04 03:09:29 +00001241 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +00001242 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +00001243 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +00001244 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001245 p->pPrior = pPrior;
1246 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001247
1248 /* Generate code to take the intersection of the two temporary
1249 ** tables.
1250 */
drh82c3d632000-06-06 21:56:07 +00001251 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001252 if( eDest==SRT_Callback ){
1253 generateColumnNames(pParse, p->base, 0, p->pEList);
1254 }
drh82c3d632000-06-06 21:56:07 +00001255 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001256 iCont = sqliteVdbeMakeLabel(v);
1257 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
1258 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001259 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh38640e12002-07-05 21:42:36 +00001260 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001261 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001262 iCont, iBreak);
1263 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001264 sqliteVdbeResolveLabel(v, iCont);
1265 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001266 sqliteVdbeResolveLabel(v, iBreak);
1267 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1268 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001269 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001270 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001271 }
drh82c3d632000-06-06 21:56:07 +00001272 break;
1273 }
1274 }
1275 assert( p->pEList && pPrior->pEList );
1276 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +00001277 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1278 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +00001279 pParse->nErr++;
1280 return 1;
drh22827922000-06-06 17:27:05 +00001281 }
1282 return 0;
1283}
1284
1285/*
drh832508b2002-03-02 17:04:07 +00001286** Recursively scan through an expression tree. For every reference
1287** to a column in table number iFrom, change that reference to the
1288** same column in table number iTo.
1289*/
drh315555c2002-10-20 15:53:03 +00001290static void changeTablesInList(ExprList*, int, int); /* Forward Declaration */
drh832508b2002-03-02 17:04:07 +00001291static void changeTables(Expr *pExpr, int iFrom, int iTo){
1292 if( pExpr==0 ) return;
1293 if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1294 pExpr->iTable = iTo;
1295 }else{
1296 changeTables(pExpr->pLeft, iFrom, iTo);
1297 changeTables(pExpr->pRight, iFrom, iTo);
drh1b2e0322002-03-03 02:49:51 +00001298 changeTablesInList(pExpr->pList, iFrom, iTo);
1299 }
1300}
1301static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
1302 if( pList ){
1303 int i;
1304 for(i=0; i<pList->nExpr; i++){
1305 changeTables(pList->a[i].pExpr, iFrom, iTo);
drh832508b2002-03-02 17:04:07 +00001306 }
1307 }
1308}
1309
1310/*
1311** Scan through the expression pExpr. Replace every reference to
1312** a column in table number iTable with a copy of the corresponding
drh84e59202002-03-14 14:33:31 +00001313** entry in pEList. (But leave references to the ROWID column
1314** unchanged.) When making a copy of an expression in pEList, change
1315** references to columns in table iSub into references to table iTable.
drh832508b2002-03-02 17:04:07 +00001316**
1317** This routine is part of the flattening procedure. A subquery
1318** whose result set is defined by pEList appears as entry in the
1319** FROM clause of a SELECT such that the VDBE cursor assigned to that
1320** FORM clause entry is iTable. This routine make the necessary
1321** changes to pExpr so that it refers directly to the source table
1322** of the subquery rather the result set of the subquery.
1323*/
drh315555c2002-10-20 15:53:03 +00001324static void substExprList(ExprList*,int,ExprList*,int); /* Forward Decl */
drh832508b2002-03-02 17:04:07 +00001325static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1326 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001327 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001328 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001329 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001330 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1331 pNew = pEList->a[pExpr->iColumn].pExpr;
1332 assert( pNew!=0 );
1333 pExpr->op = pNew->op;
drhd94a6692002-08-25 18:29:11 +00001334 assert( pExpr->pLeft==0 );
drh832508b2002-03-02 17:04:07 +00001335 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
drhd94a6692002-08-25 18:29:11 +00001336 assert( pExpr->pRight==0 );
drh832508b2002-03-02 17:04:07 +00001337 pExpr->pRight = sqliteExprDup(pNew->pRight);
drhd94a6692002-08-25 18:29:11 +00001338 assert( pExpr->pList==0 );
drh832508b2002-03-02 17:04:07 +00001339 pExpr->pList = sqliteExprListDup(pNew->pList);
1340 pExpr->iTable = pNew->iTable;
1341 pExpr->iColumn = pNew->iColumn;
1342 pExpr->iAgg = pNew->iAgg;
drh4b59ab52002-08-24 18:24:51 +00001343 sqliteTokenCopy(&pExpr->token, &pNew->token);
drh6977fea2002-10-22 23:38:04 +00001344 sqliteTokenCopy(&pExpr->span, &pNew->span);
drh832508b2002-03-02 17:04:07 +00001345 if( iSub!=iTable ){
1346 changeTables(pExpr, iSub, iTable);
1347 }
1348 }else{
drh832508b2002-03-02 17:04:07 +00001349 substExpr(pExpr->pLeft, iTable, pEList, iSub);
1350 substExpr(pExpr->pRight, iTable, pEList, iSub);
1351 substExprList(pExpr->pList, iTable, pEList, iSub);
1352 }
1353}
1354static void
1355substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1356 int i;
1357 if( pList==0 ) return;
1358 for(i=0; i<pList->nExpr; i++){
1359 substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1360 }
1361}
1362
1363/*
drh1350b032002-02-27 19:00:20 +00001364** This routine attempts to flatten subqueries in order to speed
1365** execution. It returns 1 if it makes changes and 0 if no flattening
1366** occurs.
1367**
1368** To understand the concept of flattening, consider the following
1369** query:
1370**
1371** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1372**
1373** The default way of implementing this query is to execute the
1374** subquery first and store the results in a temporary table, then
1375** run the outer query on that temporary table. This requires two
1376** passes over the data. Furthermore, because the temporary table
1377** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001378** optimized.
drh1350b032002-02-27 19:00:20 +00001379**
drh832508b2002-03-02 17:04:07 +00001380** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001381** a single flat select, like this:
1382**
1383** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1384**
1385** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001386** but only has to scan the data once. And because indices might
1387** exist on the table t1, a complete scan of the data might be
1388** avoided.
drh1350b032002-02-27 19:00:20 +00001389**
drh832508b2002-03-02 17:04:07 +00001390** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001391**
drh832508b2002-03-02 17:04:07 +00001392** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001393**
drh832508b2002-03-02 17:04:07 +00001394** (2) The subquery is not an aggregate or the outer query is not a join.
1395**
1396** (3) The subquery is not a join.
1397**
1398** (4) The subquery is not DISTINCT or the outer query is not a join.
1399**
1400** (5) The subquery is not DISTINCT or the outer query does not use
1401** aggregates.
1402**
1403** (6) The subquery does not use aggregates or the outer query is not
1404** DISTINCT.
1405**
drh08192d52002-04-30 19:20:28 +00001406** (7) The subquery has a FROM clause.
1407**
drhdf199a22002-06-14 22:38:41 +00001408** (8) The subquery does not use LIMIT or the outer query is not a join.
1409**
1410** (9) The subquery does not use LIMIT or the outer query does not use
1411** aggregates.
1412**
1413** (10) The subquery does not use aggregates or the outer query does not
1414** use LIMIT.
1415**
drh832508b2002-03-02 17:04:07 +00001416** In this routine, the "p" parameter is a pointer to the outer query.
1417** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1418** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1419**
1420** If flattening is not attempted, this routine is a no-op and return 0.
1421** If flattening is attempted this routine returns 1.
1422**
1423** All of the expression analysis must occur on both the outer query and
1424** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001425*/
drh8c74a8c2002-08-25 19:20:40 +00001426static int flattenSubquery(
1427 Parse *pParse, /* The parsing context */
1428 Select *p, /* The parent or outer SELECT statement */
1429 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
1430 int isAgg, /* True if outer SELECT uses aggregate functions */
1431 int subqueryIsAgg /* True if the subquery uses aggregate functions */
1432){
drh0bb28102002-05-08 11:54:14 +00001433 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001434 SrcList *pSrc; /* The FROM clause of the outer query */
1435 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001436 ExprList *pList; /* The result set of the outer query */
drh832508b2002-03-02 17:04:07 +00001437 int i;
1438 int iParent, iSub;
1439 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001440
drh832508b2002-03-02 17:04:07 +00001441 /* Check to see if flattening is permitted. Return 0 if not.
1442 */
1443 if( p==0 ) return 0;
1444 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001445 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001446 pSub = pSrc->a[iFrom].pSelect;
1447 assert( pSub!=0 );
1448 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001449 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001450 pSubSrc = pSub->pSrc;
1451 assert( pSubSrc );
drhad3cab52002-05-24 02:04:32 +00001452 if( pSubSrc->nSrc!=1 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001453 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1454 return 0;
1455 }
drhd11d3822002-06-21 23:01:49 +00001456 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh832508b2002-03-02 17:04:07 +00001457
drh0bb28102002-05-08 11:54:14 +00001458 /* If we reach this point, it means flattening is permitted for the
drh832508b2002-03-02 17:04:07 +00001459 ** i-th entry of the FROM clause in the outer query.
1460 */
1461 iParent = p->base + iFrom;
1462 iSub = pSub->base;
1463 substExprList(p->pEList, iParent, pSub->pEList, iSub);
1464 pList = p->pEList;
1465 for(i=0; i<pList->nExpr; i++){
drh6977fea2002-10-22 23:38:04 +00001466 Expr *pExpr;
1467 if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
1468 pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
drh832508b2002-03-02 17:04:07 +00001469 }
1470 }
drh1b2e0322002-03-03 02:49:51 +00001471 if( isAgg ){
1472 substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1473 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
1474 }
drh832508b2002-03-02 17:04:07 +00001475 substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1476 if( pSub->pWhere ){
1477 pWhere = sqliteExprDup(pSub->pWhere);
1478 if( iParent!=iSub ){
1479 changeTables(pWhere, iSub, iParent);
1480 }
1481 }else{
1482 pWhere = 0;
1483 }
1484 if( subqueryIsAgg ){
1485 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001486 p->pHaving = p->pWhere;
1487 p->pWhere = pWhere;
drh832508b2002-03-02 17:04:07 +00001488 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
drh1b2e0322002-03-03 02:49:51 +00001489 if( pSub->pHaving ){
1490 Expr *pHaving = sqliteExprDup(pSub->pHaving);
1491 if( iParent!=iSub ){
1492 changeTables(pHaving, iSub, iParent);
1493 }
1494 if( p->pHaving ){
1495 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1496 }else{
1497 p->pHaving = pHaving;
1498 }
1499 }
1500 assert( p->pGroupBy==0 );
1501 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
1502 if( iParent!=iSub ){
1503 changeTablesInList(p->pGroupBy, iSub, iParent);
1504 }
drh832508b2002-03-02 17:04:07 +00001505 }else if( p->pWhere==0 ){
1506 p->pWhere = pWhere;
1507 }else{
1508 substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1509 if( pWhere ){
1510 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1511 }
1512 }
1513 p->isDistinct = p->isDistinct || pSub->isDistinct;
drh8c74a8c2002-08-25 19:20:40 +00001514
drhdf199a22002-06-14 22:38:41 +00001515 if( pSub->nLimit>=0 ){
1516 if( p->nLimit<0 ){
1517 p->nLimit = pSub->nLimit;
1518 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1519 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1520 }
1521 }
1522 p->nOffset += pSub->nOffset;
drh8c74a8c2002-08-25 19:20:40 +00001523
1524 /* If the subquery contains subqueries of its own, that were not
1525 ** flattened, then code will have already been generated to put
1526 ** the results of those sub-subqueries into VDBE cursors relative
1527 ** to the subquery. We must translate the cursor number into values
1528 ** suitable for use by the outer query.
1529 */
1530 for(i=0; i<pSubSrc->nSrc; i++){
1531 Vdbe *v;
1532 if( pSubSrc->a[i].pSelect==0 ) continue;
1533 v = sqliteGetVdbe(pParse);
1534 sqliteVdbeAddOp(v, OP_RenameCursor, pSub->base+i, p->base+i);
1535 }
1536
drh832508b2002-03-02 17:04:07 +00001537 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1538 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1539 }
1540 pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1541 pSubSrc->a[0].pTab = 0;
drhd94a6692002-08-25 18:29:11 +00001542 assert( pSrc->a[iFrom].pSelect==pSub );
drh832508b2002-03-02 17:04:07 +00001543 pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1544 pSubSrc->a[0].pSelect = 0;
1545 sqliteSelectDelete(pSub);
1546 return 1;
1547}
drh1350b032002-02-27 19:00:20 +00001548
1549/*
drh9562b552002-02-19 15:00:07 +00001550** Analyze the SELECT statement passed in as an argument to see if it
1551** is a simple min() or max() query. If it is and this query can be
1552** satisfied using a single seek to the beginning or end of an index,
1553** then generate the code for this SELECT return 1. If this is not a
1554** simple min() or max() query, then return 0;
1555**
1556** A simply min() or max() query looks like this:
1557**
1558** SELECT min(a) FROM table;
1559** SELECT max(a) FROM table;
1560**
1561** The query may have only a single table in its FROM argument. There
1562** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1563** be the min() or max() of a single column of the table. The column
1564** in the min() or max() function must be indexed.
1565**
1566** The parameters to this routine are the same as for sqliteSelect().
1567** See the header comment on that routine for additional information.
1568*/
1569static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1570 Expr *pExpr;
1571 int iCol;
1572 Table *pTab;
1573 Index *pIdx;
1574 int base;
1575 Vdbe *v;
1576 int openOp;
1577 int seekOp;
1578 int cont;
1579 ExprList eList;
1580 struct ExprList_item eListItem;
1581
1582 /* Check to see if this query is a simple min() or max() query. Return
1583 ** zero if it is not.
1584 */
1585 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001586 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001587 if( p->pEList->nExpr!=1 ) return 0;
1588 pExpr = p->pEList->a[0].pExpr;
1589 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1590 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001591 if( pExpr->token.n!=3 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001592 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1593 seekOp = OP_Rewind;
1594 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1595 seekOp = OP_Last;
1596 }else{
1597 return 0;
1598 }
drh9562b552002-02-19 15:00:07 +00001599 pExpr = pExpr->pList->a[0].pExpr;
1600 if( pExpr->op!=TK_COLUMN ) return 0;
1601 iCol = pExpr->iColumn;
1602 pTab = p->pSrc->a[0].pTab;
1603
1604 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001605 ** Check to make sure we have an index and make pIdx point to the
1606 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1607 ** key column, no index is necessary so set pIdx to NULL. If no
1608 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001609 */
1610 if( iCol<0 ){
1611 pIdx = 0;
1612 }else{
1613 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1614 assert( pIdx->nColumn>=1 );
1615 if( pIdx->aiColumn[0]==iCol ) break;
1616 }
1617 if( pIdx==0 ) return 0;
1618 }
1619
drh17f71932002-02-21 12:01:27 +00001620 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001621 ** step is skipped if the output is going to a table or a memory cell.
1622 */
1623 v = sqliteGetVdbe(pParse);
1624 if( v==0 ) return 0;
1625 if( eDest==SRT_Callback ){
drh832508b2002-03-02 17:04:07 +00001626 generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001627 }
1628
drh17f71932002-02-21 12:01:27 +00001629 /* Generating code to find the min or the max. Basically all we have
1630 ** to do is find the first or the last entry in the chosen index. If
1631 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1632 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001633 */
drh5cf8e8c2002-02-19 22:42:05 +00001634 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
1635 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1636 pParse->schemaVerified = 1;
1637 }
drh9562b552002-02-19 15:00:07 +00001638 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +00001639 base = p->base;
drh9562b552002-02-19 15:00:07 +00001640 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001641 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001642 if( pIdx==0 ){
1643 sqliteVdbeAddOp(v, seekOp, base, 0);
1644 }else{
1645 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001646 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001647 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1648 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1649 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1650 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1651 }
drh5cf8e8c2002-02-19 22:42:05 +00001652 eList.nExpr = 1;
1653 memset(&eListItem, 0, sizeof(eListItem));
1654 eList.a = &eListItem;
1655 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +00001656 cont = sqliteVdbeMakeLabel(v);
drh38640e12002-07-05 21:42:36 +00001657 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
drh9562b552002-02-19 15:00:07 +00001658 sqliteVdbeResolveLabel(v, cont);
1659 sqliteVdbeAddOp(v, OP_Close, base, 0);
1660 return 1;
1661}
1662
1663/*
drh9bb61fe2000-06-05 16:01:39 +00001664** Generate code for the given SELECT statement.
1665**
drhfef52082000-06-06 01:50:43 +00001666** The results are distributed in various ways depending on the
1667** value of eDest and iParm.
1668**
1669** eDest Value Result
1670** ------------ -------------------------------------------
1671** SRT_Callback Invoke the callback for each row of the result.
1672**
1673** SRT_Mem Store first result in memory cell iParm
1674**
1675** SRT_Set Store results as keys of a table with cursor iParm
1676**
drh82c3d632000-06-06 21:56:07 +00001677** SRT_Union Store results as a key in a temporary table iParm
1678**
drhc4a3c772001-04-04 11:48:57 +00001679** SRT_Except Remove results form the temporary table iParm.
1680**
1681** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001682**
1683** This routine returns the number of errors. If any errors are
1684** encountered, then an appropriate error message is left in
1685** pParse->zErrMsg.
1686**
1687** This routine does NOT free the Select structure passed in. The
1688** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001689**
1690** The pParent, parentTab, and *pParentAgg fields are filled in if this
1691** SELECT is a subquery. This routine may try to combine this SELECT
1692** with its parent to form a single flat query. In so doing, it might
1693** change the parent query from a non-aggregate to an aggregate query.
1694** For that reason, the pParentAgg flag is passed as a pointer, so it
1695** can be changed.
drh9bb61fe2000-06-05 16:01:39 +00001696*/
1697int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001698 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001699 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +00001700 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drh832508b2002-03-02 17:04:07 +00001701 int iParm, /* Save result in this memory location, if >=0 */
1702 Select *pParent, /* Another SELECT for which this is a sub-query */
1703 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001704 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001705){
drhd8bc7082000-06-07 23:51:50 +00001706 int i;
drhcce7d172000-05-31 15:34:51 +00001707 WhereInfo *pWInfo;
1708 Vdbe *v;
1709 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001710 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001711 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001712 Expr *pWhere; /* The WHERE clause. May be NULL */
1713 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001714 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1715 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001716 int isDistinct; /* True if the DISTINCT keyword is present */
1717 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +00001718 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +00001719 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001720
drhdaffd0e2001-04-11 14:28:42 +00001721 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1722
drh82c3d632000-06-06 21:56:07 +00001723 /* If there is are a sequence of queries, do the earlier ones first.
1724 */
1725 if( p->pPrior ){
1726 return multiSelect(pParse, p, eDest, iParm);
1727 }
1728
1729 /* Make local copies of the parameters for this query.
1730 */
drh9bb61fe2000-06-05 16:01:39 +00001731 pTabList = p->pSrc;
1732 pWhere = p->pWhere;
1733 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00001734 pGroupBy = p->pGroupBy;
1735 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00001736 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00001737
drh832508b2002-03-02 17:04:07 +00001738 /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1739 ** The WHERE processing requires that the cursors for the tables in the
1740 ** FROM clause be consecutive.
drh10e5e3c2000-06-08 00:19:02 +00001741 */
drh832508b2002-03-02 17:04:07 +00001742 base = p->base = pParse->nTab;
drhad3cab52002-05-24 02:04:32 +00001743 pParse->nTab += pTabList->nSrc;
drh10e5e3c2000-06-08 00:19:02 +00001744
drh9bb61fe2000-06-05 16:01:39 +00001745 /*
1746 ** Do not even attempt to generate any code if we have already seen
1747 ** errors before this routine starts.
1748 */
drh1d83f052002-02-17 00:30:36 +00001749 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001750
drhd8bc7082000-06-07 23:51:50 +00001751 /* Look up every table in the table list and create an appropriate
1752 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +00001753 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001754 */
drhd8bc7082000-06-07 23:51:50 +00001755 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001756 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001757 }
drhad2d8302002-05-24 20:31:36 +00001758 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00001759 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001760 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001761
drh22827922000-06-06 17:27:05 +00001762 /* If writing to memory or generating a set
1763 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001764 */
drhfef52082000-06-06 01:50:43 +00001765 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001766 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1767 "a SELECT that is part of an expression", 0);
1768 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001769 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001770 }
1771
drhc926afb2002-06-20 03:38:26 +00001772 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00001773 */
drhc926afb2002-06-20 03:38:26 +00001774 switch( eDest ){
1775 case SRT_Union:
1776 case SRT_Except:
1777 case SRT_Discard:
1778 pOrderBy = 0;
1779 break;
1780 default:
1781 break;
drh22827922000-06-06 17:27:05 +00001782 }
1783
drh10e5e3c2000-06-08 00:19:02 +00001784 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00001785 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00001786 **
drh967e8b72000-06-21 13:59:10 +00001787 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001788 */
drh4794b982000-06-06 13:54:14 +00001789 for(i=0; i<pEList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001790 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001791 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001792 }
drh22827922000-06-06 17:27:05 +00001793 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001794 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001795 }
1796 }
drhcce7d172000-05-31 15:34:51 +00001797 if( pWhere ){
drh832508b2002-03-02 17:04:07 +00001798 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001799 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001800 }
1801 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001802 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001803 }
1804 }
1805 if( pOrderBy ){
1806 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001807 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001808 if( sqliteExprIsConstant(pE) ){
drhe4de1fe2002-06-02 16:09:01 +00001809 int iCol;
1810 if( sqliteExprIsInteger(pE, &iCol)==0 ){
1811 sqliteSetString(&pParse->zErrMsg,
1812 "ORDER BY terms must not be non-integer constants", 0);
1813 pParse->nErr++;
1814 goto select_end;
1815 }else if( iCol<=0 || iCol>pEList->nExpr ){
1816 char zBuf[2000];
1817 sprintf(zBuf,"ORDER BY column number %d out of range - should be "
1818 "between 1 and %d", iCol, pEList->nExpr);
1819 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1820 pParse->nErr++;
1821 goto select_end;
1822 }
1823 sqliteExprDelete(pE);
1824 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
drh92086432002-01-22 14:11:29 +00001825 }
drh832508b2002-03-02 17:04:07 +00001826 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001827 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001828 }
drh22827922000-06-06 17:27:05 +00001829 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001830 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001831 }
1832 }
1833 }
drh22827922000-06-06 17:27:05 +00001834 if( pGroupBy ){
1835 for(i=0; i<pGroupBy->nExpr; i++){
1836 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001837 if( sqliteExprIsConstant(pE) ){
1838 sqliteSetString(&pParse->zErrMsg,
1839 "GROUP BY expressions should not be constant", 0);
1840 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001841 goto select_end;
drh92086432002-01-22 14:11:29 +00001842 }
drh832508b2002-03-02 17:04:07 +00001843 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001844 goto select_end;
drh22827922000-06-06 17:27:05 +00001845 }
1846 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001847 goto select_end;
drh22827922000-06-06 17:27:05 +00001848 }
1849 }
1850 }
1851 if( pHaving ){
1852 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001853 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1854 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001855 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001856 goto select_end;
drh22827922000-06-06 17:27:05 +00001857 }
drh832508b2002-03-02 17:04:07 +00001858 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001859 goto select_end;
drh22827922000-06-06 17:27:05 +00001860 }
drhda932812000-06-06 18:00:15 +00001861 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001862 goto select_end;
drh22827922000-06-06 17:27:05 +00001863 }
drhcce7d172000-05-31 15:34:51 +00001864 }
1865
drh9562b552002-02-19 15:00:07 +00001866 /* Check for the special case of a min() or max() function by itself
1867 ** in the result set.
1868 */
1869 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001870 rc = 0;
drh9562b552002-02-19 15:00:07 +00001871 goto select_end;
1872 }
1873
drhd820cb12002-02-18 03:21:45 +00001874 /* Begin generating code.
1875 */
1876 v = sqliteGetVdbe(pParse);
1877 if( v==0 ) goto select_end;
1878
drh0bb28102002-05-08 11:54:14 +00001879 /* Identify column names if we will be using in the callback. This
1880 ** step is skipped if the output is going to a table or a memory cell.
1881 */
1882 if( eDest==SRT_Callback ){
1883 generateColumnNames(pParse, p->base, pTabList, pEList);
1884 }
1885
1886 /* Set the limiter
1887 */
1888 if( p->nLimit<=0 ){
drhd11d3822002-06-21 23:01:49 +00001889 p->nLimit = -1;
drh0bb28102002-05-08 11:54:14 +00001890 p->nOffset = 0;
1891 }else{
drhd11d3822002-06-21 23:01:49 +00001892 int iMem = pParse->nMem++;
1893 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
drhbf5cd972002-06-24 12:20:23 +00001894 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001895 p->nLimit = iMem;
1896 if( p->nOffset<=0 ){
1897 p->nOffset = 0;
1898 }else{
1899 iMem = pParse->nMem++;
1900 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
drhbf5cd972002-06-24 12:20:23 +00001901 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001902 p->nOffset = iMem;
1903 }
drh0bb28102002-05-08 11:54:14 +00001904 }
1905
drhd820cb12002-02-18 03:21:45 +00001906 /* Generate code for all sub-queries in the FROM clause
1907 */
drhad3cab52002-05-24 02:04:32 +00001908 for(i=0; i<pTabList->nSrc; i++){
drha76b5df2002-02-23 02:32:10 +00001909 if( pTabList->a[i].pSelect==0 ) continue;
drh2d0794e2002-03-03 03:03:52 +00001910 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
drh1b2e0322002-03-03 02:49:51 +00001911 p, i, &isAgg);
1912 pTabList = p->pSrc;
1913 pWhere = p->pWhere;
drhacd4c692002-03-07 02:02:51 +00001914 if( eDest==SRT_Callback ){
1915 pOrderBy = p->pOrderBy;
1916 }
drh1b2e0322002-03-03 02:49:51 +00001917 pGroupBy = p->pGroupBy;
1918 pHaving = p->pHaving;
1919 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00001920 }
1921
drh832508b2002-03-02 17:04:07 +00001922 /* Check to see if this is a subquery that can be "flattened" into its parent.
1923 ** If flattening is a possiblity, do so and return immediately.
1924 */
drh1b2e0322002-03-03 02:49:51 +00001925 if( pParent && pParentAgg &&
drh8c74a8c2002-08-25 19:20:40 +00001926 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
drh1b2e0322002-03-03 02:49:51 +00001927 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00001928 return rc;
1929 }
drh832508b2002-03-02 17:04:07 +00001930
drh2d0794e2002-03-03 03:03:52 +00001931 /* If the output is destined for a temporary table, open that table.
1932 */
1933 if( eDest==SRT_TempTable ){
1934 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1935 }
1936
drh22827922000-06-06 17:27:05 +00001937 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001938 */
drhd820cb12002-02-18 03:21:45 +00001939 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001940 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001941 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001942 for(i=0; i<pEList->nExpr; i++){
1943 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001944 goto select_end;
drh22827922000-06-06 17:27:05 +00001945 }
1946 }
1947 if( pGroupBy ){
1948 for(i=0; i<pGroupBy->nExpr; i++){
1949 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001950 goto select_end;
drh22827922000-06-06 17:27:05 +00001951 }
1952 }
1953 }
1954 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001955 goto select_end;
drh22827922000-06-06 17:27:05 +00001956 }
drh191b6902000-06-08 11:13:01 +00001957 if( pOrderBy ){
1958 for(i=0; i<pOrderBy->nExpr; i++){
1959 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001960 goto select_end;
drh191b6902000-06-08 11:13:01 +00001961 }
1962 }
1963 }
drhefb72512000-05-31 20:00:52 +00001964 }
1965
drh22827922000-06-06 17:27:05 +00001966 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001967 */
1968 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001969 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001970 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001971 FuncDef *pFunc;
1972 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001973 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001974 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001975 }
1976 }
drh1bee3d72001-10-15 00:44:35 +00001977 if( pGroupBy==0 ){
1978 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001979 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001980 }
drhcce7d172000-05-31 15:34:51 +00001981 }
1982
drh19a775c2000-06-05 18:54:46 +00001983 /* Initialize the memory cell to NULL
1984 */
drhfef52082000-06-06 01:50:43 +00001985 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001986 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001987 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001988 }
1989
drh832508b2002-03-02 17:04:07 +00001990 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00001991 */
drh19a775c2000-06-05 18:54:46 +00001992 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00001993 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00001994 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00001995 }else{
1996 distinct = -1;
drhefb72512000-05-31 20:00:52 +00001997 }
drh832508b2002-03-02 17:04:07 +00001998
1999 /* Begin the database scan
2000 */
drh68d2e592002-08-04 00:52:38 +00002001 pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0,
2002 pGroupBy ? 0 : &pOrderBy);
drh1d83f052002-02-17 00:30:36 +00002003 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002004
drh22827922000-06-06 17:27:05 +00002005 /* Use the standard inner loop if we are not dealing with
2006 ** aggregates
drhcce7d172000-05-31 15:34:51 +00002007 */
drhda9d6c42000-05-31 18:20:14 +00002008 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00002009 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2010 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00002011 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00002012 }
drhcce7d172000-05-31 15:34:51 +00002013 }
drhefb72512000-05-31 20:00:52 +00002014
drhe3184742002-06-19 14:27:05 +00002015 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00002016 ** processing.
drhefb72512000-05-31 20:00:52 +00002017 */
drh22827922000-06-06 17:27:05 +00002018 else{
drh22827922000-06-06 17:27:05 +00002019 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00002020 int lbl1;
drh22827922000-06-06 17:27:05 +00002021 for(i=0; i<pGroupBy->nExpr; i++){
2022 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
2023 }
drh99fcd712001-10-13 01:06:47 +00002024 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh491791a2002-07-18 00:34:09 +00002025 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
drh1bee3d72001-10-15 00:44:35 +00002026 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002027 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00002028 for(i=0; i<pParse->nAgg; i++){
2029 if( pParse->aAgg[i].isAgg ) continue;
2030 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00002031 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00002032 }
drh22827922000-06-06 17:27:05 +00002033 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00002034 }
drh22827922000-06-06 17:27:05 +00002035 for(i=0; i<pParse->nAgg; i++){
2036 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00002037 int j;
drh22827922000-06-06 17:27:05 +00002038 if( !pParse->aAgg[i].isAgg ) continue;
2039 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00002040 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00002041 if( pE->pList ){
2042 for(j=0; j<pE->pList->nExpr; j++){
2043 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
2044 }
drhe5095352002-02-24 03:25:14 +00002045 }
drh0bce8352002-02-28 00:41:10 +00002046 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00002047 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00002048 assert( pParse->aAgg[i].pFunc!=0 );
2049 assert( pParse->aAgg[i].pFunc->xStep!=0 );
2050 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00002051 }
drhcce7d172000-05-31 15:34:51 +00002052 }
2053
2054 /* End the database scan loop.
2055 */
2056 sqliteWhereEnd(pWInfo);
2057
drh22827922000-06-06 17:27:05 +00002058 /* If we are processing aggregates, we need to set up a second loop
2059 ** over all of the aggregate values and process them.
2060 */
2061 if( isAgg ){
2062 int endagg = sqliteVdbeMakeLabel(v);
2063 int startagg;
drh99fcd712001-10-13 01:06:47 +00002064 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002065 pParse->useAgg = 1;
2066 if( pHaving ){
drhf5905aa2002-05-26 20:54:33 +00002067 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002068 }
drhdf199a22002-06-14 22:38:41 +00002069 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2070 iParm, startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00002071 goto select_end;
drh22827922000-06-06 17:27:05 +00002072 }
drh99fcd712001-10-13 01:06:47 +00002073 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
2074 sqliteVdbeResolveLabel(v, endagg);
2075 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002076 pParse->useAgg = 0;
2077 }
2078
drhcce7d172000-05-31 15:34:51 +00002079 /* If there is an ORDER BY clause, then we need to sort the results
2080 ** and send them to the callback one by one.
2081 */
2082 if( pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00002083 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002084 }
drh6a535342001-10-19 16:44:56 +00002085
2086
2087 /* Issue a null callback if that is what the user wants.
2088 */
2089 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
2090 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
2091 }
2092
drh1d83f052002-02-17 00:30:36 +00002093 /* The SELECT was successfully coded. Set the return code to 0
2094 ** to indicate no errors.
2095 */
2096 rc = 0;
2097
2098 /* Control jumps to here if an error is encountered above, or upon
2099 ** successful coding of the SELECT.
2100 */
2101select_end:
drh142e30d2002-08-28 03:00:58 +00002102 /* pParse->nTab = base; */
drh1d83f052002-02-17 00:30:36 +00002103 sqliteAggregateInfoReset(pParse);
2104 return rc;
drhcce7d172000-05-31 15:34:51 +00002105}