blob: e4117f9d67954e2806d7ce31c1667fc270c069ee [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**
drh195e6962002-05-25 00:18:20 +000015** $Id: select.c,v 1.85 2002/05/25 00:18:21 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drhcce7d172000-05-31 15:34:51 +000019/*
drh9bb61fe2000-06-05 16:01:39 +000020** Allocate a new Select structure and return a pointer to that
21** structure.
drhcce7d172000-05-31 15:34:51 +000022*/
drh9bb61fe2000-06-05 16:01:39 +000023Select *sqliteSelectNew(
drhdaffd0e2001-04-11 14:28:42 +000024 ExprList *pEList, /* which columns to include in the result */
drhad3cab52002-05-24 02:04:32 +000025 SrcList *pSrc, /* the FROM clause -- which tables to scan */
drhdaffd0e2001-04-11 14:28:42 +000026 Expr *pWhere, /* the WHERE clause */
27 ExprList *pGroupBy, /* the GROUP BY clause */
28 Expr *pHaving, /* the HAVING clause */
29 ExprList *pOrderBy, /* the ORDER BY clause */
drh9bbca4c2001-11-06 04:00:18 +000030 int isDistinct, /* true if the DISTINCT keyword is present */
31 int nLimit, /* LIMIT value. -1 means not used */
32 int nOffset /* OFFSET value. -1 means not used */
drh9bb61fe2000-06-05 16:01:39 +000033){
34 Select *pNew;
35 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000036 if( pNew==0 ){
37 sqliteExprListDelete(pEList);
drhad3cab52002-05-24 02:04:32 +000038 sqliteSrcListDelete(pSrc);
drhdaffd0e2001-04-11 14:28:42 +000039 sqliteExprDelete(pWhere);
40 sqliteExprListDelete(pGroupBy);
41 sqliteExprDelete(pHaving);
42 sqliteExprListDelete(pOrderBy);
43 }else{
44 pNew->pEList = pEList;
45 pNew->pSrc = pSrc;
46 pNew->pWhere = pWhere;
47 pNew->pGroupBy = pGroupBy;
48 pNew->pHaving = pHaving;
49 pNew->pOrderBy = pOrderBy;
50 pNew->isDistinct = isDistinct;
51 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000052 pNew->nLimit = nLimit;
53 pNew->nOffset = nOffset;
drhdaffd0e2001-04-11 14:28:42 +000054 }
drh9bb61fe2000-06-05 16:01:39 +000055 return pNew;
56}
57
58/*
drh01f3f252002-05-24 16:14:15 +000059** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
60** type of join. Return an integer constant that expresses that type
61** in terms of the following bit values:
62**
63** JT_INNER
64** JT_OUTER
65** JT_NATURAL
66** JT_LEFT
67** JT_RIGHT
68**
69** A full outer join is the combination of JT_LEFT and JT_RIGHT.
70**
71** If an illegal or unsupported join type is seen, then still return
72** a join type, but put an error in the pParse structure.
73*/
74int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
75 int jointype = 0;
76 Token *apAll[3];
77 Token *p;
78 static struct {
79 const char *zKeyword;
80 int nChar;
81 int code;
82 } keywords[] = {
83 { "natural", 7, JT_NATURAL },
drh195e6962002-05-25 00:18:20 +000084 { "left", 4, JT_LEFT|JT_OUTER },
85 { "right", 5, JT_RIGHT|JT_OUTER },
86 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
drh01f3f252002-05-24 16:14:15 +000087 { "outer", 5, JT_OUTER },
88 { "inner", 5, JT_INNER },
89 { "cross", 5, JT_INNER },
90 };
91 int i, j;
92 apAll[0] = pA;
93 apAll[1] = pB;
94 apAll[2] = pC;
drh195e6962002-05-25 00:18:20 +000095 for(i=0; i<3 && apAll[i]; i++){
drh01f3f252002-05-24 16:14:15 +000096 p = apAll[i];
97 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
98 if( p->n==keywords[j].nChar
99 && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
100 jointype |= keywords[j].code;
101 break;
102 }
103 }
104 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
105 jointype |= JT_ERROR;
106 break;
107 }
108 }
drhad2d8302002-05-24 20:31:36 +0000109 if(
110 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
drh195e6962002-05-25 00:18:20 +0000111 (jointype & JT_ERROR)!=0
drhad2d8302002-05-24 20:31:36 +0000112 ){
drh01f3f252002-05-24 16:14:15 +0000113 static Token dummy = { 0, 0 };
114 char *zSp1 = " ", *zSp2 = " ";
115 if( pB==0 ){ pB = &dummy; zSp1 = 0; }
116 if( pC==0 ){ pC = &dummy; zSp2 = 0; }
117 sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
118 pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
119 pParse->nErr++;
120 jointype = JT_INNER;
drh195e6962002-05-25 00:18:20 +0000121 }else if( jointype & JT_RIGHT ){
122 sqliteSetString(&pParse->zErrMsg,
123 "RIGHT and FULL OUTER JOINs are not currently supported", 0);
124 pParse->nErr++;
125 jointype = JT_INNER;
drh01f3f252002-05-24 16:14:15 +0000126 }
127 return jointype;
128}
129
130/*
drhad2d8302002-05-24 20:31:36 +0000131** Return the index of a column in a table. Return -1 if the column
132** is not contained in the table.
133*/
134static int columnIndex(Table *pTab, const char *zCol){
135 int i;
136 for(i=0; i<pTab->nCol; i++){
137 if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
138 }
139 return -1;
140}
141
142/*
143** Add a term to the WHERE expression in *ppExpr that requires the
144** zCol column to be equal in the two tables pTab1 and pTab2.
145*/
146static void addWhereTerm(
147 const char *zCol, /* Name of the column */
148 const Table *pTab1, /* First table */
149 const Table *pTab2, /* Second table */
150 Expr **ppExpr /* Add the equality term to this expression */
151){
152 Token dummy;
153 Expr *pE1a, *pE1b, *pE1c;
154 Expr *pE2a, *pE2b, *pE2c;
155 Expr *pE;
156
157 dummy.z = zCol;
158 dummy.n = strlen(zCol);
159 pE1a = sqliteExpr(TK_ID, 0, 0, &dummy);
160 pE2a = sqliteExpr(TK_ID, 0, 0, &dummy);
161 dummy.z = pTab1->zName;
162 dummy.n = strlen(dummy.z);
163 pE1b = sqliteExpr(TK_ID, 0, 0, &dummy);
164 dummy.z = pTab2->zName;
165 dummy.n = strlen(dummy.z);
166 pE2b = sqliteExpr(TK_ID, 0, 0, &dummy);
167 pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0);
168 pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0);
169 pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0);
170 if( *ppExpr ){
171 *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0);
172 }else{
173 *ppExpr = pE;
174 }
175}
176
177/*
178** This routine processes the join information for a SELECT statement.
179** ON and USING clauses are converted into extra terms of the WHERE clause.
180** NATURAL joins also create extra WHERE clause terms.
181**
182** This routine returns the number of errors encountered.
183*/
184static int sqliteProcessJoin(Parse *pParse, Select *p){
185 SrcList *pSrc;
186 int i, j;
187 pSrc = p->pSrc;
188 for(i=0; i<pSrc->nSrc-1; i++){
189 struct SrcList_item *pTerm = &pSrc->a[i];
190 struct SrcList_item *pOther = &pSrc->a[i+1];
191
192 if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
193
194 /* When the NATURAL keyword is present, add WHERE clause terms for
195 ** every column that the two tables have in common.
196 */
197 if( pTerm->jointype & JT_NATURAL ){
198 Table *pTab;
199 if( pTerm->pOn || pTerm->pUsing ){
200 sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have "
201 "an ON or USING clause", 0);
202 pParse->nErr++;
203 return 1;
204 }
205 pTab = pTerm->pTab;
206 for(j=0; j<pTab->nCol; j++){
207 if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
208 addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
209 }
210 }
211 }
212
213 /* Disallow both ON and USING clauses in the same join
214 */
215 if( pTerm->pOn && pTerm->pUsing ){
216 sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING "
217 "clauses in the same join", 0);
218 pParse->nErr++;
219 return 1;
220 }
221
222 /* Add the ON clause to the end of the WHERE clause, connected by
223 ** and AND operator.
224 */
225 if( pTerm->pOn ){
226 if( p->pWhere==0 ){
227 p->pWhere = pTerm->pOn;
228 }else{
229 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
230 }
231 pTerm->pOn = 0;
232 }
233
234 /* Create extra terms on the WHERE clause for each column named
235 ** in the USING clause. Example: If the two tables to be joined are
236 ** A and B and the USING clause names X, Y, and Z, then add this
237 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
238 ** Report an error if any column mentioned in the USING clause is
239 ** not contained in both tables to be joined.
240 */
241 if( pTerm->pUsing ){
242 IdList *pList;
243 int j;
244 assert( i<pSrc->nSrc-1 );
245 pList = pTerm->pUsing;
246 for(j=0; j<pList->nId; j++){
247 if( columnIndex(pTerm->pTab, pList->a[i].zName)<0 ||
248 columnIndex(pOther->pTab, pList->a[i].zName)<0 ){
249 sqliteSetString(&pParse->zErrMsg, "cannot join using column ",
250 pList->a[i].zName, " - column not present in both tables", 0);
251 pParse->nErr++;
252 return 1;
253 }
254 addWhereTerm(pList->a[i].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
255 }
256 }
257 }
258 return 0;
259}
260
261/*
drh9bb61fe2000-06-05 16:01:39 +0000262** Delete the given Select structure and all of its substructures.
263*/
264void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +0000265 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +0000266 sqliteExprListDelete(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000267 sqliteSrcListDelete(p->pSrc);
drh9bb61fe2000-06-05 16:01:39 +0000268 sqliteExprDelete(p->pWhere);
269 sqliteExprListDelete(p->pGroupBy);
270 sqliteExprDelete(p->pHaving);
271 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +0000272 sqliteSelectDelete(p->pPrior);
drha76b5df2002-02-23 02:32:10 +0000273 sqliteFree(p->zSelect);
drh9bb61fe2000-06-05 16:01:39 +0000274 sqliteFree(p);
275}
276
277/*
drh22827922000-06-06 17:27:05 +0000278** Delete the aggregate information from the parse structure.
279*/
drh1d83f052002-02-17 00:30:36 +0000280static void sqliteAggregateInfoReset(Parse *pParse){
drh22827922000-06-06 17:27:05 +0000281 sqliteFree(pParse->aAgg);
282 pParse->aAgg = 0;
283 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000284 pParse->useAgg = 0;
285}
286
287/*
288** This routine generates the code for the inside of the inner loop
289** of a SELECT.
drh82c3d632000-06-06 21:56:07 +0000290**
291** The pEList is used to determine the values for each column in the
drh967e8b72000-06-21 13:59:10 +0000292** result row. Except if pEList==NULL, then we just read nColumn
drh82c3d632000-06-06 21:56:07 +0000293** elements from the srcTab table.
drh22827922000-06-06 17:27:05 +0000294*/
295static int selectInnerLoop(
296 Parse *pParse, /* The parser context */
297 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +0000298 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +0000299 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +0000300 ExprList *pOrderBy, /* If not NULL, sort results using this key */
301 int distinct, /* If >=0, make sure results are distinct */
302 int eDest, /* How to dispose of the results */
303 int iParm, /* An argument to the disposal method */
304 int iContinue, /* Jump here to continue with next row */
305 int iBreak /* Jump here to break out of the inner loop */
306){
307 Vdbe *v = pParse->pVdbe;
308 int i;
drhdaffd0e2001-04-11 14:28:42 +0000309 if( v==0 ) return 0;
drh22827922000-06-06 17:27:05 +0000310
drh967e8b72000-06-21 13:59:10 +0000311 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000312 */
drh82c3d632000-06-06 21:56:07 +0000313 if( pEList ){
314 for(i=0; i<pEList->nExpr; i++){
315 sqliteExprCode(pParse, pEList->a[i].pExpr);
316 }
drh967e8b72000-06-21 13:59:10 +0000317 nColumn = pEList->nExpr;
drh82c3d632000-06-06 21:56:07 +0000318 }else{
drh967e8b72000-06-21 13:59:10 +0000319 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000320 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000321 }
drh22827922000-06-06 17:27:05 +0000322 }
323
drhdaffd0e2001-04-11 14:28:42 +0000324 /* If the DISTINCT keyword was present on the SELECT statement
325 ** and this row has been seen before, then do not make this row
326 ** part of the result.
drh22827922000-06-06 17:27:05 +0000327 */
328 if( distinct>=0 ){
329 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000330 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
331 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl);
332 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
333 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
334 sqliteVdbeResolveLabel(v, lbl);
335 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000336 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
drh22827922000-06-06 17:27:05 +0000337 }
drh82c3d632000-06-06 21:56:07 +0000338
drh22827922000-06-06 17:27:05 +0000339 /* If there is an ORDER BY clause, then store the results
340 ** in a sorter.
341 */
342 if( pOrderBy ){
343 char *zSortOrder;
drh99fcd712001-10-13 01:06:47 +0000344 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
drh22827922000-06-06 17:27:05 +0000345 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
346 if( zSortOrder==0 ) return 1;
347 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000348 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000349 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
350 }
351 zSortOrder[pOrderBy->nExpr] = 0;
drh99fcd712001-10-13 01:06:47 +0000352 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
353 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
drh6e142f52000-06-08 13:36:40 +0000354 sqliteFree(zSortOrder);
drh99fcd712001-10-13 01:06:47 +0000355 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
drh22827922000-06-06 17:27:05 +0000356 }else
357
drh82c3d632000-06-06 21:56:07 +0000358 /* In this mode, write each query result to the key of the temporary
359 ** table iParm.
drh22827922000-06-06 17:27:05 +0000360 */
drh82c3d632000-06-06 21:56:07 +0000361 if( eDest==SRT_Union ){
drh99fcd712001-10-13 01:06:47 +0000362 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
363 sqliteVdbeAddOp(v, OP_String, iParm, 0);
drh6b125452002-01-28 15:53:03 +0000364 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
drh82c3d632000-06-06 21:56:07 +0000365 }else
366
drh5974a302000-06-07 14:42:26 +0000367 /* Store the result as data using a unique key.
368 */
drh2d0794e2002-03-03 03:03:52 +0000369 if( eDest==SRT_Table || eDest==SRT_TempTable ){
drh99fcd712001-10-13 01:06:47 +0000370 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
371 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
372 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh6b125452002-01-28 15:53:03 +0000373 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
drh5974a302000-06-07 14:42:26 +0000374 }else
375
drh82c3d632000-06-06 21:56:07 +0000376 /* Construct a record from the query result, but instead of
377 ** saving that record, use it as a key to delete elements from
378 ** the temporary table iParm.
379 */
380 if( eDest==SRT_Except ){
drh99fcd712001-10-13 01:06:47 +0000381 int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
382 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
383 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
drh22827922000-06-06 17:27:05 +0000384 }else
385
386 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
387 ** then there should be a single item on the stack. Write this
388 ** item into the set table with bogus data.
389 */
390 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000391 assert( nColumn==1 );
drh99fcd712001-10-13 01:06:47 +0000392 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000393 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
drh22827922000-06-06 17:27:05 +0000394 }else
395
drh82c3d632000-06-06 21:56:07 +0000396
drh22827922000-06-06 17:27:05 +0000397 /* If this is a scalar select that is part of an expression, then
398 ** store the results in the appropriate memory cell and break out
399 ** of the scan loop.
400 */
401 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000402 assert( nColumn==1 );
drh8721ce42001-11-07 14:22:00 +0000403 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh99fcd712001-10-13 01:06:47 +0000404 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
drh22827922000-06-06 17:27:05 +0000405 }else
406
407 /* If none of the above, send the data to the callback function.
408 */
409 {
drh9bbca4c2001-11-06 04:00:18 +0000410 sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak);
drh82c3d632000-06-06 21:56:07 +0000411 }
412 return 0;
413}
414
415/*
drhd8bc7082000-06-07 23:51:50 +0000416** If the inner loop was generated using a non-null pOrderBy argument,
417** then the results were placed in a sorter. After the loop is terminated
418** we need to run the sorter and output the results. The following
419** routine generates the code needed to do that.
420*/
drh967e8b72000-06-21 13:59:10 +0000421static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000422 int end = sqliteVdbeMakeLabel(v);
423 int addr;
drh99fcd712001-10-13 01:06:47 +0000424 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
425 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drh9bbca4c2001-11-06 04:00:18 +0000426 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end);
drh99fcd712001-10-13 01:06:47 +0000427 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
428 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000429 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000430}
431
432/*
drh82c3d632000-06-06 21:56:07 +0000433** Generate code that will tell the VDBE how many columns there
434** are in the result and the name for each column. This information
435** is used to provide "argc" and "azCol[]" values in the callback.
436*/
drh832508b2002-03-02 17:04:07 +0000437static void generateColumnNames(
438 Parse *pParse, /* Parser context */
439 int base, /* VDBE cursor corresponding to first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000440 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000441 ExprList *pEList /* Expressions defining the result set */
442){
drhd8bc7082000-06-07 23:51:50 +0000443 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000444 int i;
drhdaffd0e2001-04-11 14:28:42 +0000445 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000446 pParse->colNamesSet = 1;
drh99fcd712001-10-13 01:06:47 +0000447 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
drh82c3d632000-06-06 21:56:07 +0000448 for(i=0; i<pEList->nExpr; i++){
449 Expr *p;
drh1bee3d72001-10-15 00:44:35 +0000450 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000451 if( pEList->a[i].zName ){
452 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000453 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
454 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000455 continue;
456 }
457 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000458 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000459 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
460 if( p->span.z && p->span.z[0] && !showFullNames ){
drh99fcd712001-10-13 01:06:47 +0000461 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
462 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhe1b6a5b2000-07-29 13:06:59 +0000463 sqliteVdbeCompressSpace(v, addr);
drh1bee3d72001-10-15 00:44:35 +0000464 }else if( p->op==TK_COLUMN && pTabList ){
drh832508b2002-03-02 17:04:07 +0000465 Table *pTab = pTabList->a[p->iTable - base].pTab;
drh97665872002-02-13 23:22:53 +0000466 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000467 int iCol = p->iColumn;
468 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000469 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
470 zCol = iCol<0 ? "_ROWID_" : pTab->aCol[iCol].zName;
drhad3cab52002-05-24 02:04:32 +0000471 if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000472 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000473 char *zTab;
474
drh832508b2002-03-02 17:04:07 +0000475 zTab = pTabList->a[p->iTable - base].zAlias;
drh01a34662001-10-20 12:30:10 +0000476 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000477 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000478 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
479 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000480 sqliteFree(zName);
481 }else{
drh99fcd712001-10-13 01:06:47 +0000482 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000483 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000484 }
drh1bee3d72001-10-15 00:44:35 +0000485 }else if( p->span.z && p->span.z[0] ){
486 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
487 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
488 sqliteVdbeCompressSpace(v, addr);
489 }else{
490 char zName[30];
491 assert( p->op!=TK_COLUMN || pTabList==0 );
492 sprintf(zName, "column%d", i+1);
493 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
494 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000495 }
496 }
497}
498
499/*
drhd8bc7082000-06-07 23:51:50 +0000500** Name of the connection operator, used for error messages.
501*/
502static const char *selectOpName(int id){
503 char *z;
504 switch( id ){
505 case TK_ALL: z = "UNION ALL"; break;
506 case TK_INTERSECT: z = "INTERSECT"; break;
507 case TK_EXCEPT: z = "EXCEPT"; break;
508 default: z = "UNION"; break;
509 }
510 return z;
511}
512
513/*
drh22f70c32002-02-18 01:17:00 +0000514** Given a SELECT statement, generate a Table structure that describes
515** the result set of that SELECT.
516*/
517Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
518 Table *pTab;
519 int i;
520 ExprList *pEList;
521 static int fillInColumnList(Parse*, Select*);
522
523 if( fillInColumnList(pParse, pSelect) ){
524 return 0;
525 }
526 pTab = sqliteMalloc( sizeof(Table) );
527 if( pTab==0 ){
528 return 0;
529 }
530 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
531 pEList = pSelect->pEList;
532 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000533 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000534 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
535 for(i=0; i<pTab->nCol; i++){
536 Expr *p;
537 if( pEList->a[i].zName ){
538 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
539 }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
540 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
drhd820cb12002-02-18 03:21:45 +0000541 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
542 p->pRight->token.z[0] ){
543 sqliteSetNString(&pTab->aCol[i].zName,
544 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000545 }else{
546 char zBuf[30];
547 sprintf(zBuf, "column%d", i+1);
548 pTab->aCol[i].zName = sqliteStrDup(zBuf);
549 }
550 }
551 pTab->iPKey = -1;
552 return pTab;
553}
554
555/*
drhad2d8302002-05-24 20:31:36 +0000556** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000557**
drhad3cab52002-05-24 02:04:32 +0000558** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh22f70c32002-02-18 01:17:00 +0000559** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000560**
drhad2d8302002-05-24 20:31:36 +0000561** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
562** on joins and the ON and USING clause of joins.
563**
564** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000565** for instances of the "*" operator or the TABLE.* operator.
566** If found, expand each "*" to be every column in every table
567** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000568**
569** Return 0 on success. If there are problems, leave an error message
570** in pParse and return non-zero.
571*/
572static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000573 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000574 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000575 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000576 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000577
578 if( p==0 || p->pSrc==0 ) return 1;
579 pTabList = p->pSrc;
580 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000581
582 /* Look up every table in the table list.
583 */
drhad3cab52002-05-24 02:04:32 +0000584 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000585 if( pTabList->a[i].pTab ){
586 /* This routine has run before! No need to continue */
587 return 0;
588 }
drhdaffd0e2001-04-11 14:28:42 +0000589 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000590 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000591 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000592 if( pTabList->a[i].zAlias==0 ){
593 char zFakeName[60];
594 sprintf(zFakeName, "sqlite_subquery_%p_",
595 (void*)pTabList->a[i].pSelect);
596 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
597 }
drh22f70c32002-02-18 01:17:00 +0000598 pTabList->a[i].pTab = pTab =
599 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
600 pTabList->a[i].pSelect);
601 if( pTab==0 ){
602 return 1;
603 }
604 pTab->isTransient = 1;
605 }else{
drha76b5df2002-02-23 02:32:10 +0000606 /* An ordinary table or view name in the FROM clause */
607 pTabList->a[i].pTab = pTab =
608 sqliteFindTable(pParse->db, pTabList->a[i].zName);
609 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000610 sqliteSetString(&pParse->zErrMsg, "no such table: ",
611 pTabList->a[i].zName, 0);
612 pParse->nErr++;
613 return 1;
614 }
drha76b5df2002-02-23 02:32:10 +0000615 if( pTab->pSelect ){
drh417be792002-03-03 18:59:40 +0000616 if( sqliteViewGetColumnNames(pParse, pTab) ){
617 return 1;
618 }
drhff78bd22002-02-27 01:47:11 +0000619 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000620 }
drhd8bc7082000-06-07 23:51:50 +0000621 }
622 }
623
drhad2d8302002-05-24 20:31:36 +0000624 /* Process NATURAL keywords, and ON and USING clauses of joins.
625 */
626 if( sqliteProcessJoin(pParse, p) ) return 1;
627
drh7c917d12001-12-16 20:05:05 +0000628 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000629 ** all columns in all tables. And for every TABLE.* insert the names
630 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000631 ** with the TK_ALL operator for each "*" that it found in the column list.
632 ** The following code just has to locate the TK_ALL expressions and expand
633 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000634 **
635 ** The first loop just checks to see if there are any "*" operators
636 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000637 */
drh7c917d12001-12-16 20:05:05 +0000638 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000639 Expr *pE = pEList->a[k].pExpr;
640 if( pE->op==TK_ALL ) break;
641 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
642 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000643 }
drh54473222002-04-04 02:10:55 +0000644 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000645 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000646 /*
647 ** If we get here it means the result set contains one or more "*"
648 ** operators that need to be expanded. Loop through each expression
649 ** in the result set and expand them one by one.
650 */
drh7c917d12001-12-16 20:05:05 +0000651 struct ExprList_item *a = pEList->a;
652 ExprList *pNew = 0;
653 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000654 Expr *pE = a[k].pExpr;
655 if( pE->op!=TK_ALL &&
656 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
657 /* This particular expression does not need to be expanded.
658 */
drh7c917d12001-12-16 20:05:05 +0000659 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
660 pNew->a[pNew->nExpr-1].zName = a[k].zName;
661 a[k].pExpr = 0;
662 a[k].zName = 0;
663 }else{
drh54473222002-04-04 02:10:55 +0000664 /* This expression is a "*" or a "TABLE.*" and needs to be
665 ** expanded. */
666 int tableSeen = 0; /* Set to 1 when TABLE matches */
667 Token *pName; /* text of name of TABLE */
668 if( pE->op==TK_DOT && pE->pLeft ){
669 pName = &pE->pLeft->token;
670 }else{
671 pName = 0;
672 }
drhad3cab52002-05-24 02:04:32 +0000673 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000674 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000675 char *zTabName = pTabList->a[i].zAlias;
676 if( zTabName==0 || zTabName[0]==0 ){
677 zTabName = pTab->zName;
678 }
679 if( pName && (zTabName==0 || zTabName[0]==0 ||
680 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0) ){
681 continue;
682 }
683 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000684 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000685 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000686 char *zName = pTab->aCol[j].zName;
687
688 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
689 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
690 /* In a NATURAL join, omit the join columns from the
691 ** table on the right */
692 continue;
693 }
694 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
695 /* In a join with a USING clause, omit columns in the
696 ** using clause from the table on the right. */
697 continue;
698 }
drh22f70c32002-02-18 01:17:00 +0000699 pRight = sqliteExpr(TK_ID, 0, 0, 0);
700 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000701 pRight->token.z = zName;
702 pRight->token.n = strlen(zName);
drh54473222002-04-04 02:10:55 +0000703 if( zTabName ){
drh22f70c32002-02-18 01:17:00 +0000704 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
705 if( pLeft==0 ) break;
drh54473222002-04-04 02:10:55 +0000706 pLeft->token.z = zTabName;
707 pLeft->token.n = strlen(zTabName);
drh22f70c32002-02-18 01:17:00 +0000708 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
709 if( pExpr==0 ) break;
drh7c917d12001-12-16 20:05:05 +0000710 }else{
drh22f70c32002-02-18 01:17:00 +0000711 pExpr = pRight;
712 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +0000713 }
drh7c917d12001-12-16 20:05:05 +0000714 pNew = sqliteExprListAppend(pNew, pExpr, 0);
715 }
drh17e24df2001-11-06 14:10:41 +0000716 }
drh54473222002-04-04 02:10:55 +0000717 if( !tableSeen ){
718 assert( pName!=0 );
719 sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
720 pName->z, pName->n, 0);
721 rc = 1;
722 }
drhd8bc7082000-06-07 23:51:50 +0000723 }
724 }
drh7c917d12001-12-16 20:05:05 +0000725 sqliteExprListDelete(pEList);
726 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000727 }
drh54473222002-04-04 02:10:55 +0000728 return rc;
drhd8bc7082000-06-07 23:51:50 +0000729}
730
731/*
drhff78bd22002-02-27 01:47:11 +0000732** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
733** in a select structure. It just sets the pointers to NULL. This
734** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
735** pointer is not NULL, this routine is called recursively on that pointer.
736**
737** This routine is called on the Select structure that defines a
738** VIEW in order to undo any bindings to tables. This is necessary
739** because those tables might be DROPed by a subsequent SQL command.
740*/
741void sqliteSelectUnbind(Select *p){
742 int i;
drhad3cab52002-05-24 02:04:32 +0000743 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +0000744 Table *pTab;
745 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +0000746 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +0000747 if( (pTab = pSrc->a[i].pTab)!=0 ){
748 if( pTab->isTransient ){
749 sqliteDeleteTable(0, pTab);
750 sqliteSelectDelete(pSrc->a[i].pSelect);
751 pSrc->a[i].pSelect = 0;
752 }
753 pSrc->a[i].pTab = 0;
754 if( pSrc->a[i].pSelect ){
755 sqliteSelectUnbind(pSrc->a[i].pSelect);
756 }
757 }
758 }
759}
760
761/*
drhd8bc7082000-06-07 23:51:50 +0000762** This routine associates entries in an ORDER BY expression list with
763** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000764** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000765** the top-level node is filled in with column number and the iTable
766** value of the top-level node is filled with iTable parameter.
767**
768** If there are prior SELECT clauses, they are processed first. A match
769** in an earlier SELECT takes precedence over a later SELECT.
770**
771** Any entry that does not match is flagged as an error. The number
772** of errors is returned.
773*/
774static int matchOrderbyToColumn(
775 Parse *pParse, /* A place to leave error messages */
776 Select *pSelect, /* Match to result columns of this SELECT */
777 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
778 int iTable, /* Insert this this value in iTable */
779 int mustComplete /* If TRUE all ORDER BYs must match */
780){
781 int nErr = 0;
782 int i, j;
783 ExprList *pEList;
784
drhdaffd0e2001-04-11 14:28:42 +0000785 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000786 if( mustComplete ){
787 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
788 }
789 if( fillInColumnList(pParse, pSelect) ){
790 return 1;
791 }
792 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000793 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
794 return 1;
795 }
drhd8bc7082000-06-07 23:51:50 +0000796 }
797 pEList = pSelect->pEList;
798 for(i=0; i<pOrderBy->nExpr; i++){
799 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000800 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000801 if( pOrderBy->a[i].done ) continue;
802 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000803 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +0000804 char *zName, *zLabel;
805 zName = pEList->a[j].zName;
806 assert( pE->token.z );
807 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000808 sqliteDequote(zLabel);
809 if( sqliteStrICmp(zName, zLabel)==0 ){
810 match = 1;
811 }
drh6e142f52000-06-08 13:36:40 +0000812 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000813 }
drh4cfa7932000-06-08 15:10:46 +0000814 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000815 match = 1;
816 }
817 if( match ){
drh967e8b72000-06-21 13:59:10 +0000818 pE->op = TK_COLUMN;
819 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000820 pE->iTable = iTable;
821 pOrderBy->a[i].done = 1;
822 break;
823 }
824 }
drh92cd52f2000-06-08 01:55:29 +0000825 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000826 char zBuf[30];
827 sprintf(zBuf,"%d",i+1);
828 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
829 " does not match any result column", 0);
830 pParse->nErr++;
831 nErr++;
832 break;
833 }
834 }
835 return nErr;
836}
837
838/*
839** Get a VDBE for the given parser context. Create a new one if necessary.
840** If an error occurs, return NULL and leave a message in pParse.
841*/
842Vdbe *sqliteGetVdbe(Parse *pParse){
843 Vdbe *v = pParse->pVdbe;
844 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000845 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000846 }
drhd8bc7082000-06-07 23:51:50 +0000847 return v;
848}
849
850
851/*
drh82c3d632000-06-06 21:56:07 +0000852** This routine is called to process a query that is really the union
853** or intersection of two or more separate queries.
854*/
855static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000856 int rc; /* Success code from a subroutine */
857 Select *pPrior; /* Another SELECT immediately to our left */
858 Vdbe *v; /* Generate code to this VDBE */
859 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000860
drhd8bc7082000-06-07 23:51:50 +0000861 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
862 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000863 */
drhdaffd0e2001-04-11 14:28:42 +0000864 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000865 pPrior = p->pPrior;
866 if( pPrior->pOrderBy ){
867 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
868 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000869 pParse->nErr++;
870 return 1;
871 }
872
drhd8bc7082000-06-07 23:51:50 +0000873 /* Make sure we have a valid query engine. If not, create a new one.
874 */
875 v = sqliteGetVdbe(pParse);
876 if( v==0 ) return 1;
877
drh1cc3d752002-03-23 00:31:29 +0000878 /* Create the destination temporary table if necessary
879 */
880 if( eDest==SRT_TempTable ){
881 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
882 eDest = SRT_Table;
883 }
884
drhd8bc7082000-06-07 23:51:50 +0000885 /* Process the UNION or INTERSECTION
886 */
drh10e5e3c2000-06-08 00:19:02 +0000887 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000888 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000889 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000890 case TK_EXCEPT:
891 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000892 int unionTab; /* Cursor number of the temporary table holding result */
893 int op; /* One of the SRT_ operations to apply to self */
894 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000895
drhd8bc7082000-06-07 23:51:50 +0000896 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
897 if( eDest==priorOp ){
898 /* We can reuse a temporary table generated by a SELECT to our
899 ** right. This also means we are not the right-most select and so
900 ** we cannot have an ORDER BY clause
901 */
drh82c3d632000-06-06 21:56:07 +0000902 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000903 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000904 }else{
drhd8bc7082000-06-07 23:51:50 +0000905 /* We will need to create our own temporary table to hold the
906 ** intermediate results.
907 */
908 unionTab = pParse->nTab++;
909 if( p->pOrderBy
910 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
911 return 1;
912 }
drhd8bc7082000-06-07 23:51:50 +0000913 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +0000914 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +0000915 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000916 }else{
drh99fcd712001-10-13 01:06:47 +0000917 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000918 }
drh82c3d632000-06-06 21:56:07 +0000919 }
drhd8bc7082000-06-07 23:51:50 +0000920
921 /* Code the SELECT statements to our left
922 */
drh832508b2002-03-02 17:04:07 +0000923 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000924 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000925
926 /* Code the current SELECT statement
927 */
928 switch( p->op ){
929 case TK_EXCEPT: op = SRT_Except; break;
930 case TK_UNION: op = SRT_Union; break;
931 case TK_ALL: op = SRT_Table; break;
932 }
drh82c3d632000-06-06 21:56:07 +0000933 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +0000934 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000935 p->pPrior = pPrior;
936 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000937
938 /* Convert the data in the temporary table into whatever form
939 ** it is that we currently need.
940 */
941 if( eDest!=priorOp ){
drh6b563442001-11-07 16:48:26 +0000942 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000943 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +0000944 if( eDest==SRT_Callback ){
945 generateColumnNames(pParse, p->base, 0, p->pEList);
946 }
drh82c3d632000-06-06 21:56:07 +0000947 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000948 iCont = sqliteVdbeMakeLabel(v);
949 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
950 iStart = sqliteVdbeCurrentAddr(v);
drh82c3d632000-06-06 21:56:07 +0000951 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000952 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000953 iCont, iBreak);
954 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000955 sqliteVdbeResolveLabel(v, iCont);
956 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +0000957 sqliteVdbeResolveLabel(v, iBreak);
958 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000959 if( p->pOrderBy ){
960 generateSortTail(v, p->pEList->nExpr);
961 }
drh82c3d632000-06-06 21:56:07 +0000962 }
963 break;
964 }
965 case TK_INTERSECT: {
966 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +0000967 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000968
drhd8bc7082000-06-07 23:51:50 +0000969 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000970 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000971 ** by allocating the tables we will need.
972 */
drh82c3d632000-06-06 21:56:07 +0000973 tab1 = pParse->nTab++;
974 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000975 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
976 return 1;
977 }
drhc6b52df2002-01-04 03:09:29 +0000978 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +0000979 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000980
981 /* Code the SELECTs to our left into temporary table "tab1".
982 */
drh832508b2002-03-02 17:04:07 +0000983 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000984 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000985
986 /* Code the current SELECT into temporary table "tab2"
987 */
drhc6b52df2002-01-04 03:09:29 +0000988 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +0000989 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000990 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +0000991 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +0000992 p->pPrior = pPrior;
993 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000994
995 /* Generate code to take the intersection of the two temporary
996 ** tables.
997 */
drh82c3d632000-06-06 21:56:07 +0000998 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +0000999 if( eDest==SRT_Callback ){
1000 generateColumnNames(pParse, p->base, 0, p->pEList);
1001 }
drh82c3d632000-06-06 21:56:07 +00001002 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001003 iCont = sqliteVdbeMakeLabel(v);
1004 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
1005 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001006 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +00001007 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001008 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001009 iCont, iBreak);
1010 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001011 sqliteVdbeResolveLabel(v, iCont);
1012 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001013 sqliteVdbeResolveLabel(v, iBreak);
1014 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1015 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001016 if( p->pOrderBy ){
1017 generateSortTail(v, p->pEList->nExpr);
1018 }
drh82c3d632000-06-06 21:56:07 +00001019 break;
1020 }
1021 }
1022 assert( p->pEList && pPrior->pEList );
1023 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +00001024 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1025 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +00001026 pParse->nErr++;
1027 return 1;
drh22827922000-06-06 17:27:05 +00001028 }
drh10e5e3c2000-06-08 00:19:02 +00001029 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +00001030 return 0;
1031}
1032
1033/*
drh832508b2002-03-02 17:04:07 +00001034** Recursively scan through an expression tree. For every reference
1035** to a column in table number iFrom, change that reference to the
1036** same column in table number iTo.
1037*/
1038static void changeTables(Expr *pExpr, int iFrom, int iTo){
1039 if( pExpr==0 ) return;
1040 if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1041 pExpr->iTable = iTo;
1042 }else{
drh1b2e0322002-03-03 02:49:51 +00001043 static void changeTablesInList(ExprList*, int, int);
drh832508b2002-03-02 17:04:07 +00001044 changeTables(pExpr->pLeft, iFrom, iTo);
1045 changeTables(pExpr->pRight, iFrom, iTo);
drh1b2e0322002-03-03 02:49:51 +00001046 changeTablesInList(pExpr->pList, iFrom, iTo);
1047 }
1048}
1049static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
1050 if( pList ){
1051 int i;
1052 for(i=0; i<pList->nExpr; i++){
1053 changeTables(pList->a[i].pExpr, iFrom, iTo);
drh832508b2002-03-02 17:04:07 +00001054 }
1055 }
1056}
1057
1058/*
1059** Scan through the expression pExpr. Replace every reference to
1060** a column in table number iTable with a copy of the corresponding
drh84e59202002-03-14 14:33:31 +00001061** entry in pEList. (But leave references to the ROWID column
1062** unchanged.) When making a copy of an expression in pEList, change
1063** references to columns in table iSub into references to table iTable.
drh832508b2002-03-02 17:04:07 +00001064**
1065** This routine is part of the flattening procedure. A subquery
1066** whose result set is defined by pEList appears as entry in the
1067** FROM clause of a SELECT such that the VDBE cursor assigned to that
1068** FORM clause entry is iTable. This routine make the necessary
1069** changes to pExpr so that it refers directly to the source table
1070** of the subquery rather the result set of the subquery.
1071*/
1072static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1073 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001074 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001075 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001076 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001077 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1078 pNew = pEList->a[pExpr->iColumn].pExpr;
1079 assert( pNew!=0 );
1080 pExpr->op = pNew->op;
1081 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
1082 pExpr->pRight = sqliteExprDup(pNew->pRight);
1083 pExpr->pList = sqliteExprListDup(pNew->pList);
1084 pExpr->iTable = pNew->iTable;
1085 pExpr->iColumn = pNew->iColumn;
1086 pExpr->iAgg = pNew->iAgg;
drh1b2e0322002-03-03 02:49:51 +00001087 pExpr->token = pNew->token;
drh832508b2002-03-02 17:04:07 +00001088 if( iSub!=iTable ){
1089 changeTables(pExpr, iSub, iTable);
1090 }
1091 }else{
1092 static void substExprList(ExprList*,int,ExprList*,int);
1093 substExpr(pExpr->pLeft, iTable, pEList, iSub);
1094 substExpr(pExpr->pRight, iTable, pEList, iSub);
1095 substExprList(pExpr->pList, iTable, pEList, iSub);
1096 }
1097}
1098static void
1099substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1100 int i;
1101 if( pList==0 ) return;
1102 for(i=0; i<pList->nExpr; i++){
1103 substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1104 }
1105}
1106
1107/*
drh1350b032002-02-27 19:00:20 +00001108** This routine attempts to flatten subqueries in order to speed
1109** execution. It returns 1 if it makes changes and 0 if no flattening
1110** occurs.
1111**
1112** To understand the concept of flattening, consider the following
1113** query:
1114**
1115** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1116**
1117** The default way of implementing this query is to execute the
1118** subquery first and store the results in a temporary table, then
1119** run the outer query on that temporary table. This requires two
1120** passes over the data. Furthermore, because the temporary table
1121** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001122** optimized.
drh1350b032002-02-27 19:00:20 +00001123**
drh832508b2002-03-02 17:04:07 +00001124** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001125** a single flat select, like this:
1126**
1127** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1128**
1129** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001130** but only has to scan the data once. And because indices might
1131** exist on the table t1, a complete scan of the data might be
1132** avoided.
drh1350b032002-02-27 19:00:20 +00001133**
drh832508b2002-03-02 17:04:07 +00001134** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001135**
drh832508b2002-03-02 17:04:07 +00001136** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001137**
drh832508b2002-03-02 17:04:07 +00001138** (2) The subquery is not an aggregate or the outer query is not a join.
1139**
1140** (3) The subquery is not a join.
1141**
1142** (4) The subquery is not DISTINCT or the outer query is not a join.
1143**
1144** (5) The subquery is not DISTINCT or the outer query does not use
1145** aggregates.
1146**
1147** (6) The subquery does not use aggregates or the outer query is not
1148** DISTINCT.
1149**
drh08192d52002-04-30 19:20:28 +00001150** (7) The subquery has a FROM clause.
1151**
drh832508b2002-03-02 17:04:07 +00001152** In this routine, the "p" parameter is a pointer to the outer query.
1153** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1154** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1155**
1156** If flattening is not attempted, this routine is a no-op and return 0.
1157** If flattening is attempted this routine returns 1.
1158**
1159** All of the expression analysis must occur on both the outer query and
1160** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001161*/
drh832508b2002-03-02 17:04:07 +00001162int flattenSubquery(Select *p, int iFrom, int isAgg, int subqueryIsAgg){
drh0bb28102002-05-08 11:54:14 +00001163 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001164 SrcList *pSrc; /* The FROM clause of the outer query */
1165 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001166 ExprList *pList; /* The result set of the outer query */
drh832508b2002-03-02 17:04:07 +00001167 int i;
1168 int iParent, iSub;
1169 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001170
drh832508b2002-03-02 17:04:07 +00001171 /* Check to see if flattening is permitted. Return 0 if not.
1172 */
1173 if( p==0 ) return 0;
1174 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001175 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001176 pSub = pSrc->a[iFrom].pSelect;
1177 assert( pSub!=0 );
1178 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001179 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001180 pSubSrc = pSub->pSrc;
1181 assert( pSubSrc );
drhad3cab52002-05-24 02:04:32 +00001182 if( pSubSrc->nSrc!=1 ) return 0;
1183 if( pSub->isDistinct && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001184 if( pSub->isDistinct && isAgg ) return 0;
1185 if( p->isDistinct && subqueryIsAgg ) return 0;
1186
drh0bb28102002-05-08 11:54:14 +00001187 /* If we reach this point, it means flattening is permitted for the
drh832508b2002-03-02 17:04:07 +00001188 ** i-th entry of the FROM clause in the outer query.
1189 */
1190 iParent = p->base + iFrom;
1191 iSub = pSub->base;
1192 substExprList(p->pEList, iParent, pSub->pEList, iSub);
1193 pList = p->pEList;
1194 for(i=0; i<pList->nExpr; i++){
1195 if( pList->a[i].zName==0 ){
1196 Expr *pExpr = pList->a[i].pExpr;
1197 pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
1198 }
1199 }
drh1b2e0322002-03-03 02:49:51 +00001200 if( isAgg ){
1201 substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1202 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
1203 }
drh832508b2002-03-02 17:04:07 +00001204 substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1205 if( pSub->pWhere ){
1206 pWhere = sqliteExprDup(pSub->pWhere);
1207 if( iParent!=iSub ){
1208 changeTables(pWhere, iSub, iParent);
1209 }
1210 }else{
1211 pWhere = 0;
1212 }
1213 if( subqueryIsAgg ){
1214 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001215 p->pHaving = p->pWhere;
1216 p->pWhere = pWhere;
drh832508b2002-03-02 17:04:07 +00001217 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
drh1b2e0322002-03-03 02:49:51 +00001218 if( pSub->pHaving ){
1219 Expr *pHaving = sqliteExprDup(pSub->pHaving);
1220 if( iParent!=iSub ){
1221 changeTables(pHaving, iSub, iParent);
1222 }
1223 if( p->pHaving ){
1224 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1225 }else{
1226 p->pHaving = pHaving;
1227 }
1228 }
1229 assert( p->pGroupBy==0 );
1230 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
1231 if( iParent!=iSub ){
1232 changeTablesInList(p->pGroupBy, iSub, iParent);
1233 }
drh832508b2002-03-02 17:04:07 +00001234 }else if( p->pWhere==0 ){
1235 p->pWhere = pWhere;
1236 }else{
1237 substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1238 if( pWhere ){
1239 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1240 }
1241 }
1242 p->isDistinct = p->isDistinct || pSub->isDistinct;
1243 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1244 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1245 }
1246 pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1247 pSubSrc->a[0].pTab = 0;
1248 pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1249 pSubSrc->a[0].pSelect = 0;
1250 sqliteSelectDelete(pSub);
1251 return 1;
1252}
drh1350b032002-02-27 19:00:20 +00001253
1254/*
drh9562b552002-02-19 15:00:07 +00001255** Analyze the SELECT statement passed in as an argument to see if it
1256** is a simple min() or max() query. If it is and this query can be
1257** satisfied using a single seek to the beginning or end of an index,
1258** then generate the code for this SELECT return 1. If this is not a
1259** simple min() or max() query, then return 0;
1260**
1261** A simply min() or max() query looks like this:
1262**
1263** SELECT min(a) FROM table;
1264** SELECT max(a) FROM table;
1265**
1266** The query may have only a single table in its FROM argument. There
1267** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1268** be the min() or max() of a single column of the table. The column
1269** in the min() or max() function must be indexed.
1270**
1271** The parameters to this routine are the same as for sqliteSelect().
1272** See the header comment on that routine for additional information.
1273*/
1274static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1275 Expr *pExpr;
1276 int iCol;
1277 Table *pTab;
1278 Index *pIdx;
1279 int base;
1280 Vdbe *v;
1281 int openOp;
1282 int seekOp;
1283 int cont;
1284 ExprList eList;
1285 struct ExprList_item eListItem;
1286
1287 /* Check to see if this query is a simple min() or max() query. Return
1288 ** zero if it is not.
1289 */
1290 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001291 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001292 if( p->pEList->nExpr!=1 ) return 0;
1293 pExpr = p->pEList->a[0].pExpr;
1294 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1295 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001296 if( pExpr->token.n!=3 ) return 0;
1297 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1298 seekOp = OP_Rewind;
1299 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1300 seekOp = OP_Last;
1301 }else{
1302 return 0;
1303 }
drh9562b552002-02-19 15:00:07 +00001304 pExpr = pExpr->pList->a[0].pExpr;
1305 if( pExpr->op!=TK_COLUMN ) return 0;
1306 iCol = pExpr->iColumn;
1307 pTab = p->pSrc->a[0].pTab;
1308
1309 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001310 ** Check to make sure we have an index and make pIdx point to the
1311 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1312 ** key column, no index is necessary so set pIdx to NULL. If no
1313 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001314 */
1315 if( iCol<0 ){
1316 pIdx = 0;
1317 }else{
1318 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1319 assert( pIdx->nColumn>=1 );
1320 if( pIdx->aiColumn[0]==iCol ) break;
1321 }
1322 if( pIdx==0 ) return 0;
1323 }
1324
drh17f71932002-02-21 12:01:27 +00001325 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001326 ** step is skipped if the output is going to a table or a memory cell.
1327 */
1328 v = sqliteGetVdbe(pParse);
1329 if( v==0 ) return 0;
1330 if( eDest==SRT_Callback ){
drh832508b2002-03-02 17:04:07 +00001331 generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001332 }
1333
drh17f71932002-02-21 12:01:27 +00001334 /* Generating code to find the min or the max. Basically all we have
1335 ** to do is find the first or the last entry in the chosen index. If
1336 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1337 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001338 */
drh5cf8e8c2002-02-19 22:42:05 +00001339 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
1340 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1341 pParse->schemaVerified = 1;
1342 }
drh9562b552002-02-19 15:00:07 +00001343 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +00001344 base = p->base;
drh9562b552002-02-19 15:00:07 +00001345 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001346 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001347 if( pIdx==0 ){
1348 sqliteVdbeAddOp(v, seekOp, base, 0);
1349 }else{
1350 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001351 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001352 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1353 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1354 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1355 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1356 }
drh5cf8e8c2002-02-19 22:42:05 +00001357 eList.nExpr = 1;
1358 memset(&eListItem, 0, sizeof(eListItem));
1359 eList.a = &eListItem;
1360 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +00001361 cont = sqliteVdbeMakeLabel(v);
1362 selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont);
1363 sqliteVdbeResolveLabel(v, cont);
1364 sqliteVdbeAddOp(v, OP_Close, base, 0);
1365 return 1;
1366}
1367
1368/*
drh9bb61fe2000-06-05 16:01:39 +00001369** Generate code for the given SELECT statement.
1370**
drhfef52082000-06-06 01:50:43 +00001371** The results are distributed in various ways depending on the
1372** value of eDest and iParm.
1373**
1374** eDest Value Result
1375** ------------ -------------------------------------------
1376** SRT_Callback Invoke the callback for each row of the result.
1377**
1378** SRT_Mem Store first result in memory cell iParm
1379**
1380** SRT_Set Store results as keys of a table with cursor iParm
1381**
drh82c3d632000-06-06 21:56:07 +00001382** SRT_Union Store results as a key in a temporary table iParm
1383**
drhc4a3c772001-04-04 11:48:57 +00001384** SRT_Except Remove results form the temporary table iParm.
1385**
1386** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001387**
1388** This routine returns the number of errors. If any errors are
1389** encountered, then an appropriate error message is left in
1390** pParse->zErrMsg.
1391**
1392** This routine does NOT free the Select structure passed in. The
1393** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001394**
1395** The pParent, parentTab, and *pParentAgg fields are filled in if this
1396** SELECT is a subquery. This routine may try to combine this SELECT
1397** with its parent to form a single flat query. In so doing, it might
1398** change the parent query from a non-aggregate to an aggregate query.
1399** For that reason, the pParentAgg flag is passed as a pointer, so it
1400** can be changed.
drh9bb61fe2000-06-05 16:01:39 +00001401*/
1402int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001403 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001404 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +00001405 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drh832508b2002-03-02 17:04:07 +00001406 int iParm, /* Save result in this memory location, if >=0 */
1407 Select *pParent, /* Another SELECT for which this is a sub-query */
1408 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001409 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001410){
drhd8bc7082000-06-07 23:51:50 +00001411 int i;
drhcce7d172000-05-31 15:34:51 +00001412 WhereInfo *pWInfo;
1413 Vdbe *v;
1414 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001415 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001416 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001417 Expr *pWhere; /* The WHERE clause. May be NULL */
1418 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001419 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1420 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001421 int isDistinct; /* True if the DISTINCT keyword is present */
1422 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +00001423 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +00001424 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001425
drhdaffd0e2001-04-11 14:28:42 +00001426 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1427
drh82c3d632000-06-06 21:56:07 +00001428 /* If there is are a sequence of queries, do the earlier ones first.
1429 */
1430 if( p->pPrior ){
1431 return multiSelect(pParse, p, eDest, iParm);
1432 }
1433
1434 /* Make local copies of the parameters for this query.
1435 */
drh9bb61fe2000-06-05 16:01:39 +00001436 pTabList = p->pSrc;
1437 pWhere = p->pWhere;
1438 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00001439 pGroupBy = p->pGroupBy;
1440 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00001441 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00001442
drh832508b2002-03-02 17:04:07 +00001443 /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1444 ** The WHERE processing requires that the cursors for the tables in the
1445 ** FROM clause be consecutive.
drh10e5e3c2000-06-08 00:19:02 +00001446 */
drh832508b2002-03-02 17:04:07 +00001447 base = p->base = pParse->nTab;
drhad3cab52002-05-24 02:04:32 +00001448 pParse->nTab += pTabList->nSrc;
drh10e5e3c2000-06-08 00:19:02 +00001449
drh9bb61fe2000-06-05 16:01:39 +00001450 /*
1451 ** Do not even attempt to generate any code if we have already seen
1452 ** errors before this routine starts.
1453 */
drh1d83f052002-02-17 00:30:36 +00001454 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001455
drhd8bc7082000-06-07 23:51:50 +00001456 /* Look up every table in the table list and create an appropriate
1457 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +00001458 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001459 */
drhd8bc7082000-06-07 23:51:50 +00001460 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001461 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001462 }
drhad2d8302002-05-24 20:31:36 +00001463 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00001464 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001465 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001466
drh22827922000-06-06 17:27:05 +00001467 /* If writing to memory or generating a set
1468 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001469 */
drhfef52082000-06-06 01:50:43 +00001470 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001471 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1472 "a SELECT that is part of an expression", 0);
1473 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001474 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001475 }
1476
drh22827922000-06-06 17:27:05 +00001477 /* ORDER BY is ignored if we are not sending the result to a callback.
1478 */
1479 if( eDest!=SRT_Callback ){
drhacd4c692002-03-07 02:02:51 +00001480 pOrderBy = 0;
drh22827922000-06-06 17:27:05 +00001481 }
1482
drh10e5e3c2000-06-08 00:19:02 +00001483 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00001484 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00001485 **
drh967e8b72000-06-21 13:59:10 +00001486 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001487 */
drh4794b982000-06-06 13:54:14 +00001488 for(i=0; i<pEList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001489 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001490 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001491 }
drh22827922000-06-06 17:27:05 +00001492 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001493 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001494 }
1495 }
drhcce7d172000-05-31 15:34:51 +00001496 if( pWhere ){
drh832508b2002-03-02 17:04:07 +00001497 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001498 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001499 }
1500 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001501 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001502 }
1503 }
1504 if( pOrderBy ){
1505 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001506 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001507 if( sqliteExprIsConstant(pE) ){
1508 sqliteSetString(&pParse->zErrMsg,
1509 "ORDER BY expressions should not be constant", 0);
1510 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001511 goto select_end;
drh92086432002-01-22 14:11:29 +00001512 }
drh832508b2002-03-02 17:04:07 +00001513 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001514 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001515 }
drh22827922000-06-06 17:27:05 +00001516 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001517 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001518 }
1519 }
1520 }
drh22827922000-06-06 17:27:05 +00001521 if( pGroupBy ){
1522 for(i=0; i<pGroupBy->nExpr; i++){
1523 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001524 if( sqliteExprIsConstant(pE) ){
1525 sqliteSetString(&pParse->zErrMsg,
1526 "GROUP BY expressions should not be constant", 0);
1527 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001528 goto select_end;
drh92086432002-01-22 14:11:29 +00001529 }
drh832508b2002-03-02 17:04:07 +00001530 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001531 goto select_end;
drh22827922000-06-06 17:27:05 +00001532 }
1533 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001534 goto select_end;
drh22827922000-06-06 17:27:05 +00001535 }
1536 }
1537 }
1538 if( pHaving ){
1539 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001540 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1541 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001542 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001543 goto select_end;
drh22827922000-06-06 17:27:05 +00001544 }
drh832508b2002-03-02 17:04:07 +00001545 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001546 goto select_end;
drh22827922000-06-06 17:27:05 +00001547 }
drhda932812000-06-06 18:00:15 +00001548 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001549 goto select_end;
drh22827922000-06-06 17:27:05 +00001550 }
drhcce7d172000-05-31 15:34:51 +00001551 }
1552
drh9562b552002-02-19 15:00:07 +00001553 /* Check for the special case of a min() or max() function by itself
1554 ** in the result set.
1555 */
1556 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001557 rc = 0;
drh9562b552002-02-19 15:00:07 +00001558 goto select_end;
1559 }
1560
drhd820cb12002-02-18 03:21:45 +00001561 /* Begin generating code.
1562 */
1563 v = sqliteGetVdbe(pParse);
1564 if( v==0 ) goto select_end;
1565
drh0bb28102002-05-08 11:54:14 +00001566 /* Identify column names if we will be using in the callback. This
1567 ** step is skipped if the output is going to a table or a memory cell.
1568 */
1569 if( eDest==SRT_Callback ){
1570 generateColumnNames(pParse, p->base, pTabList, pEList);
1571 }
1572
1573 /* Set the limiter
1574 */
1575 if( p->nLimit<=0 ){
1576 p->nOffset = 0;
1577 }else{
1578 if( p->nOffset<0 ) p->nOffset = 0;
1579 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
1580 }
1581
drhd820cb12002-02-18 03:21:45 +00001582 /* Generate code for all sub-queries in the FROM clause
1583 */
drhad3cab52002-05-24 02:04:32 +00001584 for(i=0; i<pTabList->nSrc; i++){
drha76b5df2002-02-23 02:32:10 +00001585 if( pTabList->a[i].pSelect==0 ) continue;
drh2d0794e2002-03-03 03:03:52 +00001586 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
drh1b2e0322002-03-03 02:49:51 +00001587 p, i, &isAgg);
1588 pTabList = p->pSrc;
1589 pWhere = p->pWhere;
drhacd4c692002-03-07 02:02:51 +00001590 if( eDest==SRT_Callback ){
1591 pOrderBy = p->pOrderBy;
1592 }
drh1b2e0322002-03-03 02:49:51 +00001593 pGroupBy = p->pGroupBy;
1594 pHaving = p->pHaving;
1595 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00001596 }
1597
drh832508b2002-03-02 17:04:07 +00001598 /* Check to see if this is a subquery that can be "flattened" into its parent.
1599 ** If flattening is a possiblity, do so and return immediately.
1600 */
drh1b2e0322002-03-03 02:49:51 +00001601 if( pParent && pParentAgg &&
1602 flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){
1603 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00001604 return rc;
1605 }
drh832508b2002-03-02 17:04:07 +00001606
drh2d0794e2002-03-03 03:03:52 +00001607 /* If the output is destined for a temporary table, open that table.
1608 */
1609 if( eDest==SRT_TempTable ){
1610 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1611 }
1612
drh22827922000-06-06 17:27:05 +00001613 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001614 */
drhd820cb12002-02-18 03:21:45 +00001615 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001616 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001617 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001618 for(i=0; i<pEList->nExpr; i++){
1619 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001620 goto select_end;
drh22827922000-06-06 17:27:05 +00001621 }
1622 }
1623 if( pGroupBy ){
1624 for(i=0; i<pGroupBy->nExpr; i++){
1625 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001626 goto select_end;
drh22827922000-06-06 17:27:05 +00001627 }
1628 }
1629 }
1630 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001631 goto select_end;
drh22827922000-06-06 17:27:05 +00001632 }
drh191b6902000-06-08 11:13:01 +00001633 if( pOrderBy ){
1634 for(i=0; i<pOrderBy->nExpr; i++){
1635 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001636 goto select_end;
drh191b6902000-06-08 11:13:01 +00001637 }
1638 }
1639 }
drhefb72512000-05-31 20:00:52 +00001640 }
1641
drh22827922000-06-06 17:27:05 +00001642 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001643 */
1644 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001645 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001646 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001647 FuncDef *pFunc;
1648 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001649 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001650 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001651 }
1652 }
drh1bee3d72001-10-15 00:44:35 +00001653 if( pGroupBy==0 ){
1654 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001655 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001656 }
drhcce7d172000-05-31 15:34:51 +00001657 }
1658
drh19a775c2000-06-05 18:54:46 +00001659 /* Initialize the memory cell to NULL
1660 */
drhfef52082000-06-06 01:50:43 +00001661 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001662 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001663 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001664 }
1665
drh832508b2002-03-02 17:04:07 +00001666 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00001667 */
drh19a775c2000-06-05 18:54:46 +00001668 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00001669 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00001670 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00001671 }else{
1672 distinct = -1;
drhefb72512000-05-31 20:00:52 +00001673 }
drh832508b2002-03-02 17:04:07 +00001674
1675 /* Begin the database scan
1676 */
1677 pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0);
drh1d83f052002-02-17 00:30:36 +00001678 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001679
drh22827922000-06-06 17:27:05 +00001680 /* Use the standard inner loop if we are not dealing with
1681 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001682 */
drhda9d6c42000-05-31 18:20:14 +00001683 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +00001684 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001685 pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001686 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001687 }
drhcce7d172000-05-31 15:34:51 +00001688 }
drhefb72512000-05-31 20:00:52 +00001689
drh22827922000-06-06 17:27:05 +00001690 /* If we are dealing with aggregates, then to the special aggregate
1691 ** processing.
drhefb72512000-05-31 20:00:52 +00001692 */
drh22827922000-06-06 17:27:05 +00001693 else{
drh22827922000-06-06 17:27:05 +00001694 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00001695 int lbl1;
drh22827922000-06-06 17:27:05 +00001696 for(i=0; i<pGroupBy->nExpr; i++){
1697 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
1698 }
drh99fcd712001-10-13 01:06:47 +00001699 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +00001700 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001701 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00001702 for(i=0; i<pParse->nAgg; i++){
1703 if( pParse->aAgg[i].isAgg ) continue;
1704 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00001705 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00001706 }
drh22827922000-06-06 17:27:05 +00001707 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00001708 }
drh22827922000-06-06 17:27:05 +00001709 for(i=0; i<pParse->nAgg; i++){
1710 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00001711 int j;
drh22827922000-06-06 17:27:05 +00001712 if( !pParse->aAgg[i].isAgg ) continue;
1713 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00001714 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00001715 if( pE->pList ){
1716 for(j=0; j<pE->pList->nExpr; j++){
1717 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
1718 }
drhe5095352002-02-24 03:25:14 +00001719 }
drh0bce8352002-02-28 00:41:10 +00001720 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00001721 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00001722 assert( pParse->aAgg[i].pFunc!=0 );
1723 assert( pParse->aAgg[i].pFunc->xStep!=0 );
1724 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00001725 }
drhcce7d172000-05-31 15:34:51 +00001726 }
1727
1728 /* End the database scan loop.
1729 */
1730 sqliteWhereEnd(pWInfo);
1731
drh22827922000-06-06 17:27:05 +00001732 /* If we are processing aggregates, we need to set up a second loop
1733 ** over all of the aggregate values and process them.
1734 */
1735 if( isAgg ){
1736 int endagg = sqliteVdbeMakeLabel(v);
1737 int startagg;
drh99fcd712001-10-13 01:06:47 +00001738 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00001739 pParse->useAgg = 1;
1740 if( pHaving ){
1741 sqliteExprIfFalse(pParse, pHaving, startagg);
1742 }
drh82c3d632000-06-06 21:56:07 +00001743 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001744 startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00001745 goto select_end;
drh22827922000-06-06 17:27:05 +00001746 }
drh99fcd712001-10-13 01:06:47 +00001747 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
1748 sqliteVdbeResolveLabel(v, endagg);
1749 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00001750 pParse->useAgg = 0;
1751 }
1752
drhcce7d172000-05-31 15:34:51 +00001753 /* If there is an ORDER BY clause, then we need to sort the results
1754 ** and send them to the callback one by one.
1755 */
1756 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001757 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001758 }
drh6a535342001-10-19 16:44:56 +00001759
1760
1761 /* Issue a null callback if that is what the user wants.
1762 */
1763 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1764 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1765 }
1766
drh1d83f052002-02-17 00:30:36 +00001767 /* The SELECT was successfully coded. Set the return code to 0
1768 ** to indicate no errors.
1769 */
1770 rc = 0;
1771
1772 /* Control jumps to here if an error is encountered above, or upon
1773 ** successful coding of the SELECT.
1774 */
1775select_end:
drh832508b2002-03-02 17:04:07 +00001776 pParse->nTab = base;
drh1d83f052002-02-17 00:30:36 +00001777 sqliteAggregateInfoReset(pParse);
1778 return rc;
drhcce7d172000-05-31 15:34:51 +00001779}