blob: 4cbf0344e3efd36e4674756ada0bafbb05f3cadb [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**
drh142e30d2002-08-28 03:00:58 +000015** $Id: select.c,v 1.111 2002/08/28 03:00:59 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);
drh4b59ab52002-08-24 18:24:51 +0000159 dummy.base = 1;
160 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: {
527 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
528 break;
529 }
530
drhc926afb2002-06-20 03:38:26 +0000531 /* Discard the results. This is used for SELECT statements inside
532 ** the body of a TRIGGER. The purpose of such selects is to call
533 ** user-defined functions that have side effects. We do not care
534 ** about the actual results of the select.
535 */
drhc926afb2002-06-20 03:38:26 +0000536 default: {
drhf46f9052002-06-22 02:33:38 +0000537 assert( eDest==SRT_Discard );
538 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000539 break;
540 }
drh82c3d632000-06-06 21:56:07 +0000541 }
542 return 0;
543}
544
545/*
drhd8bc7082000-06-07 23:51:50 +0000546** If the inner loop was generated using a non-null pOrderBy argument,
547** then the results were placed in a sorter. After the loop is terminated
548** we need to run the sorter and output the results. The following
549** routine generates the code needed to do that.
550*/
drhc926afb2002-06-20 03:38:26 +0000551static void generateSortTail(
552 Select *p, /* The SELECT statement */
553 Vdbe *v, /* Generate code into this VDBE */
554 int nColumn, /* Number of columns of data */
555 int eDest, /* Write the sorted results here */
556 int iParm /* Optional parameter associated with eDest */
557){
drhd8bc7082000-06-07 23:51:50 +0000558 int end = sqliteVdbeMakeLabel(v);
559 int addr;
drhf46f9052002-06-22 02:33:38 +0000560 if( eDest==SRT_Sorter ) return;
drh99fcd712001-10-13 01:06:47 +0000561 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
562 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drhdf199a22002-06-14 22:38:41 +0000563 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000564 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+4);
565 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
566 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drhdf199a22002-06-14 22:38:41 +0000567 }
drhd11d3822002-06-21 23:01:49 +0000568 if( p->nLimit>=0 ){
569 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, end);
drhdf199a22002-06-14 22:38:41 +0000570 }
drhc926afb2002-06-20 03:38:26 +0000571 switch( eDest ){
572 case SRT_Callback: {
573 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
574 break;
575 }
576 case SRT_Table:
577 case SRT_TempTable: {
578 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
579 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
580 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
581 break;
582 }
583 case SRT_Set: {
584 assert( nColumn==1 );
585 sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3);
586 sqliteVdbeAddOp(v, OP_String, 0, 0);
587 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
588 break;
589 }
590 case SRT_Mem: {
591 assert( nColumn==1 );
592 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
593 sqliteVdbeAddOp(v, OP_Goto, 0, end);
594 break;
595 }
596 default: {
drhf46f9052002-06-22 02:33:38 +0000597 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000598 break;
599 }
600 }
drh99fcd712001-10-13 01:06:47 +0000601 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
602 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000603 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000604}
605
606/*
drh82c3d632000-06-06 21:56:07 +0000607** Generate code that will tell the VDBE how many columns there
608** are in the result and the name for each column. This information
609** is used to provide "argc" and "azCol[]" values in the callback.
610*/
drh832508b2002-03-02 17:04:07 +0000611static void generateColumnNames(
612 Parse *pParse, /* Parser context */
613 int base, /* VDBE cursor corresponding to first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000614 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000615 ExprList *pEList /* Expressions defining the result set */
616){
drhd8bc7082000-06-07 23:51:50 +0000617 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000618 int i;
drhdaffd0e2001-04-11 14:28:42 +0000619 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000620 pParse->colNamesSet = 1;
drh5080aaa2002-07-11 12:18:16 +0000621 if( pParse->db->flags & SQLITE_ReportTypes ){
622 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr*2, 0);
623 }else{
624 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
625 }
drh82c3d632000-06-06 21:56:07 +0000626 for(i=0; i<pEList->nExpr; i++){
627 Expr *p;
drhb1363202002-06-26 02:45:03 +0000628 char *zType = 0;
drh1bee3d72001-10-15 00:44:35 +0000629 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000630 if( pEList->a[i].zName ){
631 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000632 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
633 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000634 continue;
635 }
636 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000637 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000638 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
drhfa173a72002-07-10 21:26:00 +0000639 if( p->op==TK_COLUMN && pTabList ){
drh832508b2002-03-02 17:04:07 +0000640 Table *pTab = pTabList->a[p->iTable - base].pTab;
drh97665872002-02-13 23:22:53 +0000641 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000642 int iCol = p->iColumn;
643 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000644 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000645 if( iCol<0 ){
646 zCol = "_ROWID_";
647 zType = "INTEGER";
648 }else{
649 zCol = pTab->aCol[iCol].zName;
650 zType = pTab->aCol[iCol].zType;
651 }
drh4b59ab52002-08-24 18:24:51 +0000652 if( p->token.z && p->token.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000653 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000654 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drhfa173a72002-07-10 21:26:00 +0000655 sqliteVdbeCompressSpace(v, addr);
656 }else if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000657 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000658 char *zTab;
659
drh832508b2002-03-02 17:04:07 +0000660 zTab = pTabList->a[p->iTable - base].zAlias;
drh01a34662001-10-20 12:30:10 +0000661 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000662 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000663 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
664 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000665 sqliteFree(zName);
666 }else{
drh99fcd712001-10-13 01:06:47 +0000667 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000668 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000669 }
drh4b59ab52002-08-24 18:24:51 +0000670 }else if( p->token.z && p->token.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000671 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000672 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drhfa173a72002-07-10 21:26:00 +0000673 sqliteVdbeCompressSpace(v, addr);
drh4b59ab52002-08-24 18:24:51 +0000674 }else if( p->token.z && p->token.z[0] ){
drh1bee3d72001-10-15 00:44:35 +0000675 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000676 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drh1bee3d72001-10-15 00:44:35 +0000677 sqliteVdbeCompressSpace(v, addr);
678 }else{
679 char zName[30];
680 assert( p->op!=TK_COLUMN || pTabList==0 );
681 sprintf(zName, "column%d", i+1);
682 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
683 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000684 }
drh5080aaa2002-07-11 12:18:16 +0000685 if( pParse->db->flags & SQLITE_ReportTypes ){
686 if( zType==0 ){
687 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
688 zType = "TEXT";
689 }else{
690 zType = "NUMERIC";
691 }
drhb1363202002-06-26 02:45:03 +0000692 }
drh5080aaa2002-07-11 12:18:16 +0000693 sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0);
694 sqliteVdbeChangeP3(v, -1, zType, P3_STATIC);
drhb1363202002-06-26 02:45:03 +0000695 }
drh82c3d632000-06-06 21:56:07 +0000696 }
697}
698
699/*
drhd8bc7082000-06-07 23:51:50 +0000700** Name of the connection operator, used for error messages.
701*/
702static const char *selectOpName(int id){
703 char *z;
704 switch( id ){
705 case TK_ALL: z = "UNION ALL"; break;
706 case TK_INTERSECT: z = "INTERSECT"; break;
707 case TK_EXCEPT: z = "EXCEPT"; break;
708 default: z = "UNION"; break;
709 }
710 return z;
711}
712
713/*
drh22f70c32002-02-18 01:17:00 +0000714** Given a SELECT statement, generate a Table structure that describes
715** the result set of that SELECT.
716*/
717Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
718 Table *pTab;
719 int i;
720 ExprList *pEList;
721 static int fillInColumnList(Parse*, Select*);
722
723 if( fillInColumnList(pParse, pSelect) ){
724 return 0;
725 }
726 pTab = sqliteMalloc( sizeof(Table) );
727 if( pTab==0 ){
728 return 0;
729 }
730 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
731 pEList = pSelect->pEList;
732 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000733 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000734 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
735 for(i=0; i<pTab->nCol; i++){
736 Expr *p;
737 if( pEList->a[i].zName ){
738 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
drh4b59ab52002-08-24 18:24:51 +0000739 }else if( (p=pEList->a[i].pExpr)->token.z && p->token.z[0] ){
740 sqliteSetNString(&pTab->aCol[i].zName, p->token.z, p->token.n, 0);
drhd820cb12002-02-18 03:21:45 +0000741 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
742 p->pRight->token.z[0] ){
743 sqliteSetNString(&pTab->aCol[i].zName,
744 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000745 }else{
746 char zBuf[30];
747 sprintf(zBuf, "column%d", i+1);
748 pTab->aCol[i].zName = sqliteStrDup(zBuf);
749 }
750 }
751 pTab->iPKey = -1;
752 return pTab;
753}
754
755/*
drhad2d8302002-05-24 20:31:36 +0000756** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000757**
drhad3cab52002-05-24 02:04:32 +0000758** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh22f70c32002-02-18 01:17:00 +0000759** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000760**
drhad2d8302002-05-24 20:31:36 +0000761** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
762** on joins and the ON and USING clause of joins.
763**
764** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000765** for instances of the "*" operator or the TABLE.* operator.
766** If found, expand each "*" to be every column in every table
767** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000768**
769** Return 0 on success. If there are problems, leave an error message
770** in pParse and return non-zero.
771*/
772static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000773 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000774 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000775 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000776 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000777
778 if( p==0 || p->pSrc==0 ) return 1;
779 pTabList = p->pSrc;
780 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000781
782 /* Look up every table in the table list.
783 */
drhad3cab52002-05-24 02:04:32 +0000784 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000785 if( pTabList->a[i].pTab ){
786 /* This routine has run before! No need to continue */
787 return 0;
788 }
drhdaffd0e2001-04-11 14:28:42 +0000789 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000790 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000791 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000792 if( pTabList->a[i].zAlias==0 ){
793 char zFakeName[60];
794 sprintf(zFakeName, "sqlite_subquery_%p_",
795 (void*)pTabList->a[i].pSelect);
796 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
797 }
drh22f70c32002-02-18 01:17:00 +0000798 pTabList->a[i].pTab = pTab =
799 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
800 pTabList->a[i].pSelect);
801 if( pTab==0 ){
802 return 1;
803 }
804 pTab->isTransient = 1;
805 }else{
drha76b5df2002-02-23 02:32:10 +0000806 /* An ordinary table or view name in the FROM clause */
807 pTabList->a[i].pTab = pTab =
808 sqliteFindTable(pParse->db, pTabList->a[i].zName);
809 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000810 sqliteSetString(&pParse->zErrMsg, "no such table: ",
811 pTabList->a[i].zName, 0);
812 pParse->nErr++;
813 return 1;
814 }
drha76b5df2002-02-23 02:32:10 +0000815 if( pTab->pSelect ){
drh417be792002-03-03 18:59:40 +0000816 if( sqliteViewGetColumnNames(pParse, pTab) ){
817 return 1;
818 }
drhd94a6692002-08-25 18:29:11 +0000819 sqliteSelectDelete(pTabList->a[i].pSelect);
drhff78bd22002-02-27 01:47:11 +0000820 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000821 }
drhd8bc7082000-06-07 23:51:50 +0000822 }
823 }
824
drhad2d8302002-05-24 20:31:36 +0000825 /* Process NATURAL keywords, and ON and USING clauses of joins.
826 */
827 if( sqliteProcessJoin(pParse, p) ) return 1;
828
drh7c917d12001-12-16 20:05:05 +0000829 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000830 ** all columns in all tables. And for every TABLE.* insert the names
831 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000832 ** with the TK_ALL operator for each "*" that it found in the column list.
833 ** The following code just has to locate the TK_ALL expressions and expand
834 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000835 **
836 ** The first loop just checks to see if there are any "*" operators
837 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000838 */
drh7c917d12001-12-16 20:05:05 +0000839 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000840 Expr *pE = pEList->a[k].pExpr;
841 if( pE->op==TK_ALL ) break;
842 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
843 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000844 }
drh54473222002-04-04 02:10:55 +0000845 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000846 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000847 /*
848 ** If we get here it means the result set contains one or more "*"
849 ** operators that need to be expanded. Loop through each expression
850 ** in the result set and expand them one by one.
851 */
drh7c917d12001-12-16 20:05:05 +0000852 struct ExprList_item *a = pEList->a;
853 ExprList *pNew = 0;
854 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000855 Expr *pE = a[k].pExpr;
856 if( pE->op!=TK_ALL &&
857 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
858 /* This particular expression does not need to be expanded.
859 */
drh7c917d12001-12-16 20:05:05 +0000860 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
861 pNew->a[pNew->nExpr-1].zName = a[k].zName;
862 a[k].pExpr = 0;
863 a[k].zName = 0;
864 }else{
drh54473222002-04-04 02:10:55 +0000865 /* This expression is a "*" or a "TABLE.*" and needs to be
866 ** expanded. */
867 int tableSeen = 0; /* Set to 1 when TABLE matches */
868 Token *pName; /* text of name of TABLE */
869 if( pE->op==TK_DOT && pE->pLeft ){
870 pName = &pE->pLeft->token;
871 }else{
872 pName = 0;
873 }
drhad3cab52002-05-24 02:04:32 +0000874 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000875 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000876 char *zTabName = pTabList->a[i].zAlias;
877 if( zTabName==0 || zTabName[0]==0 ){
878 zTabName = pTab->zName;
879 }
drhc754fa52002-05-27 03:25:51 +0000880 if( pName && (zTabName==0 || zTabName[0]==0 ||
881 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
882 zTabName[pName->n]!=0) ){
drh54473222002-04-04 02:10:55 +0000883 continue;
884 }
885 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000886 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000887 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000888 char *zName = pTab->aCol[j].zName;
889
890 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
891 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
892 /* In a NATURAL join, omit the join columns from the
893 ** table on the right */
894 continue;
895 }
896 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
897 /* In a join with a USING clause, omit columns in the
898 ** using clause from the table on the right. */
899 continue;
900 }
drh22f70c32002-02-18 01:17:00 +0000901 pRight = sqliteExpr(TK_ID, 0, 0, 0);
902 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000903 pRight->token.z = zName;
904 pRight->token.n = strlen(zName);
drh4b59ab52002-08-24 18:24:51 +0000905 pRight->token.dyn = 0;
906 pRight->token.base = 1;
907 if( zTabName && pTabList->nSrc>1 ){
drh22f70c32002-02-18 01:17:00 +0000908 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh22f70c32002-02-18 01:17:00 +0000909 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
910 if( pExpr==0 ) break;
drh4b59ab52002-08-24 18:24:51 +0000911 pLeft->token.z = zTabName;
912 pLeft->token.n = strlen(zTabName);
913 pLeft->token.dyn = 0;
914 pLeft->token.base = 1;
915 sqliteSetString((char**)&pExpr->token.z, zTabName, ".", zName, 0);
916 pExpr->token.n = strlen(pExpr->token.z);
917 pExpr->token.base = 0;
918 pExpr->token.dyn = 1;
drh7c917d12001-12-16 20:05:05 +0000919 }else{
drh22f70c32002-02-18 01:17:00 +0000920 pExpr = pRight;
drh7c917d12001-12-16 20:05:05 +0000921 }
drh7c917d12001-12-16 20:05:05 +0000922 pNew = sqliteExprListAppend(pNew, pExpr, 0);
923 }
drh17e24df2001-11-06 14:10:41 +0000924 }
drh54473222002-04-04 02:10:55 +0000925 if( !tableSeen ){
drhf5db2d32002-06-06 23:42:27 +0000926 if( pName ){
927 sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
928 pName->z, pName->n, 0);
929 }else{
930 sqliteSetString(&pParse->zErrMsg, "no tables specified", 0);
931 }
drh54473222002-04-04 02:10:55 +0000932 rc = 1;
933 }
drhd8bc7082000-06-07 23:51:50 +0000934 }
935 }
drh7c917d12001-12-16 20:05:05 +0000936 sqliteExprListDelete(pEList);
937 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000938 }
drh54473222002-04-04 02:10:55 +0000939 return rc;
drhd8bc7082000-06-07 23:51:50 +0000940}
941
942/*
drhff78bd22002-02-27 01:47:11 +0000943** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
944** in a select structure. It just sets the pointers to NULL. This
945** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
946** pointer is not NULL, this routine is called recursively on that pointer.
947**
948** This routine is called on the Select structure that defines a
949** VIEW in order to undo any bindings to tables. This is necessary
950** because those tables might be DROPed by a subsequent SQL command.
951*/
952void sqliteSelectUnbind(Select *p){
953 int i;
drhad3cab52002-05-24 02:04:32 +0000954 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +0000955 Table *pTab;
956 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +0000957 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +0000958 if( (pTab = pSrc->a[i].pTab)!=0 ){
959 if( pTab->isTransient ){
960 sqliteDeleteTable(0, pTab);
drh4b59ab52002-08-24 18:24:51 +0000961#if 0
drhff78bd22002-02-27 01:47:11 +0000962 sqliteSelectDelete(pSrc->a[i].pSelect);
963 pSrc->a[i].pSelect = 0;
drh4b59ab52002-08-24 18:24:51 +0000964#endif
drhff78bd22002-02-27 01:47:11 +0000965 }
966 pSrc->a[i].pTab = 0;
967 if( pSrc->a[i].pSelect ){
968 sqliteSelectUnbind(pSrc->a[i].pSelect);
969 }
970 }
971 }
972}
973
974/*
drhd8bc7082000-06-07 23:51:50 +0000975** This routine associates entries in an ORDER BY expression list with
976** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000977** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000978** the top-level node is filled in with column number and the iTable
979** value of the top-level node is filled with iTable parameter.
980**
981** If there are prior SELECT clauses, they are processed first. A match
982** in an earlier SELECT takes precedence over a later SELECT.
983**
984** Any entry that does not match is flagged as an error. The number
985** of errors is returned.
986*/
987static int matchOrderbyToColumn(
988 Parse *pParse, /* A place to leave error messages */
989 Select *pSelect, /* Match to result columns of this SELECT */
990 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +0000991 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +0000992 int mustComplete /* If TRUE all ORDER BYs must match */
993){
994 int nErr = 0;
995 int i, j;
996 ExprList *pEList;
997
drhdaffd0e2001-04-11 14:28:42 +0000998 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000999 if( mustComplete ){
1000 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1001 }
1002 if( fillInColumnList(pParse, pSelect) ){
1003 return 1;
1004 }
1005 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +00001006 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
1007 return 1;
1008 }
drhd8bc7082000-06-07 23:51:50 +00001009 }
1010 pEList = pSelect->pEList;
1011 for(i=0; i<pOrderBy->nExpr; i++){
1012 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +00001013 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +00001014 if( pOrderBy->a[i].done ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001015 if( sqliteExprIsInteger(pE, &iCol) ){
1016 if( iCol<=0 || iCol>pEList->nExpr ){
1017 char zBuf[200];
1018 sprintf(zBuf,"ORDER BY position %d should be between 1 and %d",
1019 iCol, pEList->nExpr);
1020 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1021 pParse->nErr++;
1022 nErr++;
1023 break;
1024 }
1025 iCol--;
1026 }
1027 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001028 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001029 char *zName, *zLabel;
1030 zName = pEList->a[j].zName;
1031 assert( pE->token.z );
1032 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +00001033 sqliteDequote(zLabel);
1034 if( sqliteStrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001035 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001036 }
drh6e142f52000-06-08 13:36:40 +00001037 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001038 }
drhe4de1fe2002-06-02 16:09:01 +00001039 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
1040 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001041 }
1042 }
drhe4de1fe2002-06-02 16:09:01 +00001043 if( iCol>=0 ){
1044 pE->op = TK_COLUMN;
1045 pE->iColumn = iCol;
1046 pE->iTable = iTable;
1047 pOrderBy->a[i].done = 1;
1048 }
1049 if( iCol<0 && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +00001050 char zBuf[30];
1051 sprintf(zBuf,"%d",i+1);
1052 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
1053 " does not match any result column", 0);
1054 pParse->nErr++;
1055 nErr++;
1056 break;
1057 }
1058 }
1059 return nErr;
1060}
1061
1062/*
1063** Get a VDBE for the given parser context. Create a new one if necessary.
1064** If an error occurs, return NULL and leave a message in pParse.
1065*/
1066Vdbe *sqliteGetVdbe(Parse *pParse){
1067 Vdbe *v = pParse->pVdbe;
1068 if( v==0 ){
drh4c504392000-10-16 22:06:40 +00001069 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001070 }
drhd8bc7082000-06-07 23:51:50 +00001071 return v;
1072}
1073
1074
1075/*
drh82c3d632000-06-06 21:56:07 +00001076** This routine is called to process a query that is really the union
1077** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001078**
1079** "p" points to the right-most of the two queries. The results should
1080** be stored in eDest with parameter iParm.
drh82c3d632000-06-06 21:56:07 +00001081*/
1082static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +00001083 int rc; /* Success code from a subroutine */
1084 Select *pPrior; /* Another SELECT immediately to our left */
1085 Vdbe *v; /* Generate code to this VDBE */
drh82c3d632000-06-06 21:56:07 +00001086
drhd8bc7082000-06-07 23:51:50 +00001087 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
1088 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +00001089 */
drhdaffd0e2001-04-11 14:28:42 +00001090 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001091 pPrior = p->pPrior;
1092 if( pPrior->pOrderBy ){
1093 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
1094 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +00001095 pParse->nErr++;
1096 return 1;
1097 }
1098
drhd8bc7082000-06-07 23:51:50 +00001099 /* Make sure we have a valid query engine. If not, create a new one.
1100 */
1101 v = sqliteGetVdbe(pParse);
1102 if( v==0 ) return 1;
1103
drh1cc3d752002-03-23 00:31:29 +00001104 /* Create the destination temporary table if necessary
1105 */
1106 if( eDest==SRT_TempTable ){
1107 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1108 eDest = SRT_Table;
1109 }
1110
drhf46f9052002-06-22 02:33:38 +00001111 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001112 */
drh82c3d632000-06-06 21:56:07 +00001113 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001114 case TK_ALL: {
1115 if( p->pOrderBy==0 ){
1116 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
1117 if( rc ) return rc;
1118 p->pPrior = 0;
1119 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
1120 p->pPrior = pPrior;
1121 if( rc ) return rc;
1122 break;
1123 }
1124 /* For UNION ALL ... ORDER BY fall through to the next case */
1125 }
drh82c3d632000-06-06 21:56:07 +00001126 case TK_EXCEPT:
1127 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001128 int unionTab; /* Cursor number of the temporary table holding result */
1129 int op; /* One of the SRT_ operations to apply to self */
1130 int priorOp; /* The SRT_ operation to apply to prior selects */
drhc926afb2002-06-20 03:38:26 +00001131 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
drh82c3d632000-06-06 21:56:07 +00001132
drhd8bc7082000-06-07 23:51:50 +00001133 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drhc926afb2002-06-20 03:38:26 +00001134 if( eDest==priorOp && p->pOrderBy==0 ){
drhd8bc7082000-06-07 23:51:50 +00001135 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001136 ** right.
drhd8bc7082000-06-07 23:51:50 +00001137 */
drh82c3d632000-06-06 21:56:07 +00001138 unionTab = iParm;
1139 }else{
drhd8bc7082000-06-07 23:51:50 +00001140 /* We will need to create our own temporary table to hold the
1141 ** intermediate results.
1142 */
1143 unionTab = pParse->nTab++;
1144 if( p->pOrderBy
1145 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
1146 return 1;
1147 }
drhd8bc7082000-06-07 23:51:50 +00001148 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +00001149 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +00001150 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +00001151 }else{
drh99fcd712001-10-13 01:06:47 +00001152 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001153 }
drh82c3d632000-06-06 21:56:07 +00001154 }
drhd8bc7082000-06-07 23:51:50 +00001155
1156 /* Code the SELECT statements to our left
1157 */
drh832508b2002-03-02 17:04:07 +00001158 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001159 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001160
1161 /* Code the current SELECT statement
1162 */
1163 switch( p->op ){
1164 case TK_EXCEPT: op = SRT_Except; break;
1165 case TK_UNION: op = SRT_Union; break;
1166 case TK_ALL: op = SRT_Table; break;
1167 }
drh82c3d632000-06-06 21:56:07 +00001168 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001169 pOrderBy = p->pOrderBy;
1170 p->pOrderBy = 0;
drh832508b2002-03-02 17:04:07 +00001171 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001172 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001173 p->pOrderBy = pOrderBy;
drh82c3d632000-06-06 21:56:07 +00001174 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001175
1176 /* Convert the data in the temporary table into whatever form
1177 ** it is that we currently need.
1178 */
drhc926afb2002-06-20 03:38:26 +00001179 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001180 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001181 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001182 if( eDest==SRT_Callback ){
1183 generateColumnNames(pParse, p->base, 0, p->pEList);
1184 }
drh82c3d632000-06-06 21:56:07 +00001185 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001186 iCont = sqliteVdbeMakeLabel(v);
1187 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1188 iStart = sqliteVdbeCurrentAddr(v);
drh38640e12002-07-05 21:42:36 +00001189 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001190 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001191 iCont, iBreak);
1192 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001193 sqliteVdbeResolveLabel(v, iCont);
1194 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +00001195 sqliteVdbeResolveLabel(v, iBreak);
1196 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001197 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001198 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001199 }
drh82c3d632000-06-06 21:56:07 +00001200 }
1201 break;
1202 }
1203 case TK_INTERSECT: {
1204 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001205 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001206
drhd8bc7082000-06-07 23:51:50 +00001207 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001208 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001209 ** by allocating the tables we will need.
1210 */
drh82c3d632000-06-06 21:56:07 +00001211 tab1 = pParse->nTab++;
1212 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001213 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
1214 return 1;
1215 }
drhc6b52df2002-01-04 03:09:29 +00001216 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +00001217 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +00001218
1219 /* Code the SELECTs to our left into temporary table "tab1".
1220 */
drh832508b2002-03-02 17:04:07 +00001221 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001222 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001223
1224 /* Code the current SELECT into temporary table "tab2"
1225 */
drhc6b52df2002-01-04 03:09:29 +00001226 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +00001227 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +00001228 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +00001229 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001230 p->pPrior = pPrior;
1231 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001232
1233 /* Generate code to take the intersection of the two temporary
1234 ** tables.
1235 */
drh82c3d632000-06-06 21:56:07 +00001236 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001237 if( eDest==SRT_Callback ){
1238 generateColumnNames(pParse, p->base, 0, p->pEList);
1239 }
drh82c3d632000-06-06 21:56:07 +00001240 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001241 iCont = sqliteVdbeMakeLabel(v);
1242 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
1243 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001244 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh38640e12002-07-05 21:42:36 +00001245 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001246 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001247 iCont, iBreak);
1248 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001249 sqliteVdbeResolveLabel(v, iCont);
1250 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001251 sqliteVdbeResolveLabel(v, iBreak);
1252 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1253 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001254 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001255 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001256 }
drh82c3d632000-06-06 21:56:07 +00001257 break;
1258 }
1259 }
1260 assert( p->pEList && pPrior->pEList );
1261 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +00001262 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1263 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +00001264 pParse->nErr++;
1265 return 1;
drh22827922000-06-06 17:27:05 +00001266 }
1267 return 0;
1268}
1269
1270/*
drh832508b2002-03-02 17:04:07 +00001271** Recursively scan through an expression tree. For every reference
1272** to a column in table number iFrom, change that reference to the
1273** same column in table number iTo.
1274*/
1275static void changeTables(Expr *pExpr, int iFrom, int iTo){
1276 if( pExpr==0 ) return;
1277 if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1278 pExpr->iTable = iTo;
1279 }else{
drh1b2e0322002-03-03 02:49:51 +00001280 static void changeTablesInList(ExprList*, int, int);
drh832508b2002-03-02 17:04:07 +00001281 changeTables(pExpr->pLeft, iFrom, iTo);
1282 changeTables(pExpr->pRight, iFrom, iTo);
drh1b2e0322002-03-03 02:49:51 +00001283 changeTablesInList(pExpr->pList, iFrom, iTo);
1284 }
1285}
1286static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
1287 if( pList ){
1288 int i;
1289 for(i=0; i<pList->nExpr; i++){
1290 changeTables(pList->a[i].pExpr, iFrom, iTo);
drh832508b2002-03-02 17:04:07 +00001291 }
1292 }
1293}
1294
1295/*
1296** Scan through the expression pExpr. Replace every reference to
1297** a column in table number iTable with a copy of the corresponding
drh84e59202002-03-14 14:33:31 +00001298** entry in pEList. (But leave references to the ROWID column
1299** unchanged.) When making a copy of an expression in pEList, change
1300** references to columns in table iSub into references to table iTable.
drh832508b2002-03-02 17:04:07 +00001301**
1302** This routine is part of the flattening procedure. A subquery
1303** whose result set is defined by pEList appears as entry in the
1304** FROM clause of a SELECT such that the VDBE cursor assigned to that
1305** FORM clause entry is iTable. This routine make the necessary
1306** changes to pExpr so that it refers directly to the source table
1307** of the subquery rather the result set of the subquery.
1308*/
1309static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1310 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001311 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001312 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001313 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001314 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1315 pNew = pEList->a[pExpr->iColumn].pExpr;
1316 assert( pNew!=0 );
1317 pExpr->op = pNew->op;
drhd94a6692002-08-25 18:29:11 +00001318 assert( pExpr->pLeft==0 );
drh832508b2002-03-02 17:04:07 +00001319 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
drhd94a6692002-08-25 18:29:11 +00001320 assert( pExpr->pRight==0 );
drh832508b2002-03-02 17:04:07 +00001321 pExpr->pRight = sqliteExprDup(pNew->pRight);
drhd94a6692002-08-25 18:29:11 +00001322 assert( pExpr->pList==0 );
drh832508b2002-03-02 17:04:07 +00001323 pExpr->pList = sqliteExprListDup(pNew->pList);
1324 pExpr->iTable = pNew->iTable;
1325 pExpr->iColumn = pNew->iColumn;
1326 pExpr->iAgg = pNew->iAgg;
drh4b59ab52002-08-24 18:24:51 +00001327 pExpr->nFuncName = pNew->nFuncName;
1328 sqliteTokenCopy(&pExpr->token, &pNew->token);
drh832508b2002-03-02 17:04:07 +00001329 if( iSub!=iTable ){
1330 changeTables(pExpr, iSub, iTable);
1331 }
1332 }else{
1333 static void substExprList(ExprList*,int,ExprList*,int);
1334 substExpr(pExpr->pLeft, iTable, pEList, iSub);
1335 substExpr(pExpr->pRight, iTable, pEList, iSub);
1336 substExprList(pExpr->pList, iTable, pEList, iSub);
1337 }
1338}
1339static void
1340substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1341 int i;
1342 if( pList==0 ) return;
1343 for(i=0; i<pList->nExpr; i++){
1344 substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1345 }
1346}
1347
1348/*
drh1350b032002-02-27 19:00:20 +00001349** This routine attempts to flatten subqueries in order to speed
1350** execution. It returns 1 if it makes changes and 0 if no flattening
1351** occurs.
1352**
1353** To understand the concept of flattening, consider the following
1354** query:
1355**
1356** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1357**
1358** The default way of implementing this query is to execute the
1359** subquery first and store the results in a temporary table, then
1360** run the outer query on that temporary table. This requires two
1361** passes over the data. Furthermore, because the temporary table
1362** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001363** optimized.
drh1350b032002-02-27 19:00:20 +00001364**
drh832508b2002-03-02 17:04:07 +00001365** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001366** a single flat select, like this:
1367**
1368** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1369**
1370** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001371** but only has to scan the data once. And because indices might
1372** exist on the table t1, a complete scan of the data might be
1373** avoided.
drh1350b032002-02-27 19:00:20 +00001374**
drh832508b2002-03-02 17:04:07 +00001375** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001376**
drh832508b2002-03-02 17:04:07 +00001377** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001378**
drh832508b2002-03-02 17:04:07 +00001379** (2) The subquery is not an aggregate or the outer query is not a join.
1380**
1381** (3) The subquery is not a join.
1382**
1383** (4) The subquery is not DISTINCT or the outer query is not a join.
1384**
1385** (5) The subquery is not DISTINCT or the outer query does not use
1386** aggregates.
1387**
1388** (6) The subquery does not use aggregates or the outer query is not
1389** DISTINCT.
1390**
drh08192d52002-04-30 19:20:28 +00001391** (7) The subquery has a FROM clause.
1392**
drhdf199a22002-06-14 22:38:41 +00001393** (8) The subquery does not use LIMIT or the outer query is not a join.
1394**
1395** (9) The subquery does not use LIMIT or the outer query does not use
1396** aggregates.
1397**
1398** (10) The subquery does not use aggregates or the outer query does not
1399** use LIMIT.
1400**
drh832508b2002-03-02 17:04:07 +00001401** In this routine, the "p" parameter is a pointer to the outer query.
1402** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1403** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1404**
1405** If flattening is not attempted, this routine is a no-op and return 0.
1406** If flattening is attempted this routine returns 1.
1407**
1408** All of the expression analysis must occur on both the outer query and
1409** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001410*/
drh8c74a8c2002-08-25 19:20:40 +00001411static int flattenSubquery(
1412 Parse *pParse, /* The parsing context */
1413 Select *p, /* The parent or outer SELECT statement */
1414 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
1415 int isAgg, /* True if outer SELECT uses aggregate functions */
1416 int subqueryIsAgg /* True if the subquery uses aggregate functions */
1417){
drh0bb28102002-05-08 11:54:14 +00001418 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001419 SrcList *pSrc; /* The FROM clause of the outer query */
1420 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001421 ExprList *pList; /* The result set of the outer query */
drh832508b2002-03-02 17:04:07 +00001422 int i;
1423 int iParent, iSub;
1424 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001425
drh832508b2002-03-02 17:04:07 +00001426 /* Check to see if flattening is permitted. Return 0 if not.
1427 */
1428 if( p==0 ) return 0;
1429 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001430 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001431 pSub = pSrc->a[iFrom].pSelect;
1432 assert( pSub!=0 );
1433 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001434 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001435 pSubSrc = pSub->pSrc;
1436 assert( pSubSrc );
drhad3cab52002-05-24 02:04:32 +00001437 if( pSubSrc->nSrc!=1 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001438 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1439 return 0;
1440 }
drhd11d3822002-06-21 23:01:49 +00001441 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh832508b2002-03-02 17:04:07 +00001442
drh0bb28102002-05-08 11:54:14 +00001443 /* If we reach this point, it means flattening is permitted for the
drh832508b2002-03-02 17:04:07 +00001444 ** i-th entry of the FROM clause in the outer query.
1445 */
1446 iParent = p->base + iFrom;
1447 iSub = pSub->base;
1448 substExprList(p->pEList, iParent, pSub->pEList, iSub);
1449 pList = p->pEList;
1450 for(i=0; i<pList->nExpr; i++){
1451 if( pList->a[i].zName==0 ){
1452 Expr *pExpr = pList->a[i].pExpr;
drh4b59ab52002-08-24 18:24:51 +00001453 assert( pExpr->token.z!=0 );
1454 pList->a[i].zName = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh832508b2002-03-02 17:04:07 +00001455 }
1456 }
drh1b2e0322002-03-03 02:49:51 +00001457 if( isAgg ){
1458 substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1459 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
1460 }
drh832508b2002-03-02 17:04:07 +00001461 substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1462 if( pSub->pWhere ){
1463 pWhere = sqliteExprDup(pSub->pWhere);
1464 if( iParent!=iSub ){
1465 changeTables(pWhere, iSub, iParent);
1466 }
1467 }else{
1468 pWhere = 0;
1469 }
1470 if( subqueryIsAgg ){
1471 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001472 p->pHaving = p->pWhere;
1473 p->pWhere = pWhere;
drh832508b2002-03-02 17:04:07 +00001474 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
drh1b2e0322002-03-03 02:49:51 +00001475 if( pSub->pHaving ){
1476 Expr *pHaving = sqliteExprDup(pSub->pHaving);
1477 if( iParent!=iSub ){
1478 changeTables(pHaving, iSub, iParent);
1479 }
1480 if( p->pHaving ){
1481 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1482 }else{
1483 p->pHaving = pHaving;
1484 }
1485 }
1486 assert( p->pGroupBy==0 );
1487 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
1488 if( iParent!=iSub ){
1489 changeTablesInList(p->pGroupBy, iSub, iParent);
1490 }
drh832508b2002-03-02 17:04:07 +00001491 }else if( p->pWhere==0 ){
1492 p->pWhere = pWhere;
1493 }else{
1494 substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1495 if( pWhere ){
1496 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1497 }
1498 }
1499 p->isDistinct = p->isDistinct || pSub->isDistinct;
drh8c74a8c2002-08-25 19:20:40 +00001500
drhdf199a22002-06-14 22:38:41 +00001501 if( pSub->nLimit>=0 ){
1502 if( p->nLimit<0 ){
1503 p->nLimit = pSub->nLimit;
1504 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1505 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1506 }
1507 }
1508 p->nOffset += pSub->nOffset;
drh8c74a8c2002-08-25 19:20:40 +00001509
1510 /* If the subquery contains subqueries of its own, that were not
1511 ** flattened, then code will have already been generated to put
1512 ** the results of those sub-subqueries into VDBE cursors relative
1513 ** to the subquery. We must translate the cursor number into values
1514 ** suitable for use by the outer query.
1515 */
1516 for(i=0; i<pSubSrc->nSrc; i++){
1517 Vdbe *v;
1518 if( pSubSrc->a[i].pSelect==0 ) continue;
1519 v = sqliteGetVdbe(pParse);
1520 sqliteVdbeAddOp(v, OP_RenameCursor, pSub->base+i, p->base+i);
1521 }
1522
drh832508b2002-03-02 17:04:07 +00001523 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1524 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1525 }
1526 pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1527 pSubSrc->a[0].pTab = 0;
drhd94a6692002-08-25 18:29:11 +00001528 assert( pSrc->a[iFrom].pSelect==pSub );
drh832508b2002-03-02 17:04:07 +00001529 pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1530 pSubSrc->a[0].pSelect = 0;
1531 sqliteSelectDelete(pSub);
1532 return 1;
1533}
drh1350b032002-02-27 19:00:20 +00001534
1535/*
drh9562b552002-02-19 15:00:07 +00001536** Analyze the SELECT statement passed in as an argument to see if it
1537** is a simple min() or max() query. If it is and this query can be
1538** satisfied using a single seek to the beginning or end of an index,
1539** then generate the code for this SELECT return 1. If this is not a
1540** simple min() or max() query, then return 0;
1541**
1542** A simply min() or max() query looks like this:
1543**
1544** SELECT min(a) FROM table;
1545** SELECT max(a) FROM table;
1546**
1547** The query may have only a single table in its FROM argument. There
1548** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1549** be the min() or max() of a single column of the table. The column
1550** in the min() or max() function must be indexed.
1551**
1552** The parameters to this routine are the same as for sqliteSelect().
1553** See the header comment on that routine for additional information.
1554*/
1555static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1556 Expr *pExpr;
1557 int iCol;
1558 Table *pTab;
1559 Index *pIdx;
1560 int base;
1561 Vdbe *v;
1562 int openOp;
1563 int seekOp;
1564 int cont;
1565 ExprList eList;
1566 struct ExprList_item eListItem;
1567
1568 /* Check to see if this query is a simple min() or max() query. Return
1569 ** zero if it is not.
1570 */
1571 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001572 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001573 if( p->pEList->nExpr!=1 ) return 0;
1574 pExpr = p->pEList->a[0].pExpr;
1575 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1576 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh4b59ab52002-08-24 18:24:51 +00001577 if( pExpr->nFuncName!=3 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001578 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1579 seekOp = OP_Rewind;
1580 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1581 seekOp = OP_Last;
1582 }else{
1583 return 0;
1584 }
drh9562b552002-02-19 15:00:07 +00001585 pExpr = pExpr->pList->a[0].pExpr;
1586 if( pExpr->op!=TK_COLUMN ) return 0;
1587 iCol = pExpr->iColumn;
1588 pTab = p->pSrc->a[0].pTab;
1589
1590 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001591 ** Check to make sure we have an index and make pIdx point to the
1592 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1593 ** key column, no index is necessary so set pIdx to NULL. If no
1594 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001595 */
1596 if( iCol<0 ){
1597 pIdx = 0;
1598 }else{
1599 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1600 assert( pIdx->nColumn>=1 );
1601 if( pIdx->aiColumn[0]==iCol ) break;
1602 }
1603 if( pIdx==0 ) return 0;
1604 }
1605
drh17f71932002-02-21 12:01:27 +00001606 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001607 ** step is skipped if the output is going to a table or a memory cell.
1608 */
1609 v = sqliteGetVdbe(pParse);
1610 if( v==0 ) return 0;
1611 if( eDest==SRT_Callback ){
drh832508b2002-03-02 17:04:07 +00001612 generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001613 }
1614
drh17f71932002-02-21 12:01:27 +00001615 /* Generating code to find the min or the max. Basically all we have
1616 ** to do is find the first or the last entry in the chosen index. If
1617 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1618 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001619 */
drh5cf8e8c2002-02-19 22:42:05 +00001620 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
1621 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1622 pParse->schemaVerified = 1;
1623 }
drh9562b552002-02-19 15:00:07 +00001624 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +00001625 base = p->base;
drh9562b552002-02-19 15:00:07 +00001626 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001627 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001628 if( pIdx==0 ){
1629 sqliteVdbeAddOp(v, seekOp, base, 0);
1630 }else{
1631 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001632 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001633 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1634 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1635 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1636 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1637 }
drh5cf8e8c2002-02-19 22:42:05 +00001638 eList.nExpr = 1;
1639 memset(&eListItem, 0, sizeof(eListItem));
1640 eList.a = &eListItem;
1641 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +00001642 cont = sqliteVdbeMakeLabel(v);
drh38640e12002-07-05 21:42:36 +00001643 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
drh9562b552002-02-19 15:00:07 +00001644 sqliteVdbeResolveLabel(v, cont);
1645 sqliteVdbeAddOp(v, OP_Close, base, 0);
1646 return 1;
1647}
1648
1649/*
drh9bb61fe2000-06-05 16:01:39 +00001650** Generate code for the given SELECT statement.
1651**
drhfef52082000-06-06 01:50:43 +00001652** The results are distributed in various ways depending on the
1653** value of eDest and iParm.
1654**
1655** eDest Value Result
1656** ------------ -------------------------------------------
1657** SRT_Callback Invoke the callback for each row of the result.
1658**
1659** SRT_Mem Store first result in memory cell iParm
1660**
1661** SRT_Set Store results as keys of a table with cursor iParm
1662**
drh82c3d632000-06-06 21:56:07 +00001663** SRT_Union Store results as a key in a temporary table iParm
1664**
drhc4a3c772001-04-04 11:48:57 +00001665** SRT_Except Remove results form the temporary table iParm.
1666**
1667** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001668**
1669** This routine returns the number of errors. If any errors are
1670** encountered, then an appropriate error message is left in
1671** pParse->zErrMsg.
1672**
1673** This routine does NOT free the Select structure passed in. The
1674** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001675**
1676** The pParent, parentTab, and *pParentAgg fields are filled in if this
1677** SELECT is a subquery. This routine may try to combine this SELECT
1678** with its parent to form a single flat query. In so doing, it might
1679** change the parent query from a non-aggregate to an aggregate query.
1680** For that reason, the pParentAgg flag is passed as a pointer, so it
1681** can be changed.
drh9bb61fe2000-06-05 16:01:39 +00001682*/
1683int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001684 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001685 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +00001686 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drh832508b2002-03-02 17:04:07 +00001687 int iParm, /* Save result in this memory location, if >=0 */
1688 Select *pParent, /* Another SELECT for which this is a sub-query */
1689 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001690 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001691){
drhd8bc7082000-06-07 23:51:50 +00001692 int i;
drhcce7d172000-05-31 15:34:51 +00001693 WhereInfo *pWInfo;
1694 Vdbe *v;
1695 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001696 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001697 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001698 Expr *pWhere; /* The WHERE clause. May be NULL */
1699 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001700 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1701 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001702 int isDistinct; /* True if the DISTINCT keyword is present */
1703 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +00001704 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +00001705 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001706
drhdaffd0e2001-04-11 14:28:42 +00001707 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1708
drh82c3d632000-06-06 21:56:07 +00001709 /* If there is are a sequence of queries, do the earlier ones first.
1710 */
1711 if( p->pPrior ){
1712 return multiSelect(pParse, p, eDest, iParm);
1713 }
1714
1715 /* Make local copies of the parameters for this query.
1716 */
drh9bb61fe2000-06-05 16:01:39 +00001717 pTabList = p->pSrc;
1718 pWhere = p->pWhere;
1719 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00001720 pGroupBy = p->pGroupBy;
1721 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00001722 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00001723
drh832508b2002-03-02 17:04:07 +00001724 /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1725 ** The WHERE processing requires that the cursors for the tables in the
1726 ** FROM clause be consecutive.
drh10e5e3c2000-06-08 00:19:02 +00001727 */
drh832508b2002-03-02 17:04:07 +00001728 base = p->base = pParse->nTab;
drhad3cab52002-05-24 02:04:32 +00001729 pParse->nTab += pTabList->nSrc;
drh10e5e3c2000-06-08 00:19:02 +00001730
drh9bb61fe2000-06-05 16:01:39 +00001731 /*
1732 ** Do not even attempt to generate any code if we have already seen
1733 ** errors before this routine starts.
1734 */
drh1d83f052002-02-17 00:30:36 +00001735 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001736
drhd8bc7082000-06-07 23:51:50 +00001737 /* Look up every table in the table list and create an appropriate
1738 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +00001739 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001740 */
drhd8bc7082000-06-07 23:51:50 +00001741 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001742 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001743 }
drhad2d8302002-05-24 20:31:36 +00001744 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00001745 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001746 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001747
drh22827922000-06-06 17:27:05 +00001748 /* If writing to memory or generating a set
1749 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001750 */
drhfef52082000-06-06 01:50:43 +00001751 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001752 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1753 "a SELECT that is part of an expression", 0);
1754 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001755 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001756 }
1757
drhc926afb2002-06-20 03:38:26 +00001758 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00001759 */
drhc926afb2002-06-20 03:38:26 +00001760 switch( eDest ){
1761 case SRT_Union:
1762 case SRT_Except:
1763 case SRT_Discard:
1764 pOrderBy = 0;
1765 break;
1766 default:
1767 break;
drh22827922000-06-06 17:27:05 +00001768 }
1769
drh10e5e3c2000-06-08 00:19:02 +00001770 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00001771 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00001772 **
drh967e8b72000-06-21 13:59:10 +00001773 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001774 */
drh4794b982000-06-06 13:54:14 +00001775 for(i=0; i<pEList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001776 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001777 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001778 }
drh22827922000-06-06 17:27:05 +00001779 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001780 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001781 }
1782 }
drhcce7d172000-05-31 15:34:51 +00001783 if( pWhere ){
drh832508b2002-03-02 17:04:07 +00001784 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001785 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001786 }
1787 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001788 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001789 }
1790 }
1791 if( pOrderBy ){
1792 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001793 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001794 if( sqliteExprIsConstant(pE) ){
drhe4de1fe2002-06-02 16:09:01 +00001795 int iCol;
1796 if( sqliteExprIsInteger(pE, &iCol)==0 ){
1797 sqliteSetString(&pParse->zErrMsg,
1798 "ORDER BY terms must not be non-integer constants", 0);
1799 pParse->nErr++;
1800 goto select_end;
1801 }else if( iCol<=0 || iCol>pEList->nExpr ){
1802 char zBuf[2000];
1803 sprintf(zBuf,"ORDER BY column number %d out of range - should be "
1804 "between 1 and %d", iCol, pEList->nExpr);
1805 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1806 pParse->nErr++;
1807 goto select_end;
1808 }
1809 sqliteExprDelete(pE);
1810 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
drh92086432002-01-22 14:11:29 +00001811 }
drh832508b2002-03-02 17:04:07 +00001812 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001813 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001814 }
drh22827922000-06-06 17:27:05 +00001815 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001816 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001817 }
1818 }
1819 }
drh22827922000-06-06 17:27:05 +00001820 if( pGroupBy ){
1821 for(i=0; i<pGroupBy->nExpr; i++){
1822 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001823 if( sqliteExprIsConstant(pE) ){
1824 sqliteSetString(&pParse->zErrMsg,
1825 "GROUP BY expressions should not be constant", 0);
1826 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001827 goto select_end;
drh92086432002-01-22 14:11:29 +00001828 }
drh832508b2002-03-02 17:04:07 +00001829 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001830 goto select_end;
drh22827922000-06-06 17:27:05 +00001831 }
1832 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001833 goto select_end;
drh22827922000-06-06 17:27:05 +00001834 }
1835 }
1836 }
1837 if( pHaving ){
1838 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001839 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1840 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001841 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001842 goto select_end;
drh22827922000-06-06 17:27:05 +00001843 }
drh832508b2002-03-02 17:04:07 +00001844 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001845 goto select_end;
drh22827922000-06-06 17:27:05 +00001846 }
drhda932812000-06-06 18:00:15 +00001847 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001848 goto select_end;
drh22827922000-06-06 17:27:05 +00001849 }
drhcce7d172000-05-31 15:34:51 +00001850 }
1851
drh9562b552002-02-19 15:00:07 +00001852 /* Check for the special case of a min() or max() function by itself
1853 ** in the result set.
1854 */
1855 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001856 rc = 0;
drh9562b552002-02-19 15:00:07 +00001857 goto select_end;
1858 }
1859
drhd820cb12002-02-18 03:21:45 +00001860 /* Begin generating code.
1861 */
1862 v = sqliteGetVdbe(pParse);
1863 if( v==0 ) goto select_end;
1864
drh0bb28102002-05-08 11:54:14 +00001865 /* Identify column names if we will be using in the callback. This
1866 ** step is skipped if the output is going to a table or a memory cell.
1867 */
1868 if( eDest==SRT_Callback ){
1869 generateColumnNames(pParse, p->base, pTabList, pEList);
1870 }
1871
1872 /* Set the limiter
1873 */
1874 if( p->nLimit<=0 ){
drhd11d3822002-06-21 23:01:49 +00001875 p->nLimit = -1;
drh0bb28102002-05-08 11:54:14 +00001876 p->nOffset = 0;
1877 }else{
drhd11d3822002-06-21 23:01:49 +00001878 int iMem = pParse->nMem++;
1879 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
drhbf5cd972002-06-24 12:20:23 +00001880 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001881 p->nLimit = iMem;
1882 if( p->nOffset<=0 ){
1883 p->nOffset = 0;
1884 }else{
1885 iMem = pParse->nMem++;
1886 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
drhbf5cd972002-06-24 12:20:23 +00001887 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001888 p->nOffset = iMem;
1889 }
drh0bb28102002-05-08 11:54:14 +00001890 }
1891
drhd820cb12002-02-18 03:21:45 +00001892 /* Generate code for all sub-queries in the FROM clause
1893 */
drhad3cab52002-05-24 02:04:32 +00001894 for(i=0; i<pTabList->nSrc; i++){
drha76b5df2002-02-23 02:32:10 +00001895 if( pTabList->a[i].pSelect==0 ) continue;
drh2d0794e2002-03-03 03:03:52 +00001896 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
drh1b2e0322002-03-03 02:49:51 +00001897 p, i, &isAgg);
1898 pTabList = p->pSrc;
1899 pWhere = p->pWhere;
drhacd4c692002-03-07 02:02:51 +00001900 if( eDest==SRT_Callback ){
1901 pOrderBy = p->pOrderBy;
1902 }
drh1b2e0322002-03-03 02:49:51 +00001903 pGroupBy = p->pGroupBy;
1904 pHaving = p->pHaving;
1905 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00001906 }
1907
drh832508b2002-03-02 17:04:07 +00001908 /* Check to see if this is a subquery that can be "flattened" into its parent.
1909 ** If flattening is a possiblity, do so and return immediately.
1910 */
drh1b2e0322002-03-03 02:49:51 +00001911 if( pParent && pParentAgg &&
drh8c74a8c2002-08-25 19:20:40 +00001912 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
drh1b2e0322002-03-03 02:49:51 +00001913 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00001914 return rc;
1915 }
drh832508b2002-03-02 17:04:07 +00001916
drh2d0794e2002-03-03 03:03:52 +00001917 /* If the output is destined for a temporary table, open that table.
1918 */
1919 if( eDest==SRT_TempTable ){
1920 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1921 }
1922
drh22827922000-06-06 17:27:05 +00001923 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001924 */
drhd820cb12002-02-18 03:21:45 +00001925 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001926 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001927 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001928 for(i=0; i<pEList->nExpr; i++){
1929 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001930 goto select_end;
drh22827922000-06-06 17:27:05 +00001931 }
1932 }
1933 if( pGroupBy ){
1934 for(i=0; i<pGroupBy->nExpr; i++){
1935 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001936 goto select_end;
drh22827922000-06-06 17:27:05 +00001937 }
1938 }
1939 }
1940 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001941 goto select_end;
drh22827922000-06-06 17:27:05 +00001942 }
drh191b6902000-06-08 11:13:01 +00001943 if( pOrderBy ){
1944 for(i=0; i<pOrderBy->nExpr; i++){
1945 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001946 goto select_end;
drh191b6902000-06-08 11:13:01 +00001947 }
1948 }
1949 }
drhefb72512000-05-31 20:00:52 +00001950 }
1951
drh22827922000-06-06 17:27:05 +00001952 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001953 */
1954 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001955 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001956 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001957 FuncDef *pFunc;
1958 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001959 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001960 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001961 }
1962 }
drh1bee3d72001-10-15 00:44:35 +00001963 if( pGroupBy==0 ){
1964 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001965 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001966 }
drhcce7d172000-05-31 15:34:51 +00001967 }
1968
drh19a775c2000-06-05 18:54:46 +00001969 /* Initialize the memory cell to NULL
1970 */
drhfef52082000-06-06 01:50:43 +00001971 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001972 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001973 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001974 }
1975
drh832508b2002-03-02 17:04:07 +00001976 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00001977 */
drh19a775c2000-06-05 18:54:46 +00001978 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00001979 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00001980 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00001981 }else{
1982 distinct = -1;
drhefb72512000-05-31 20:00:52 +00001983 }
drh832508b2002-03-02 17:04:07 +00001984
1985 /* Begin the database scan
1986 */
drh68d2e592002-08-04 00:52:38 +00001987 pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0,
1988 pGroupBy ? 0 : &pOrderBy);
drh1d83f052002-02-17 00:30:36 +00001989 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001990
drh22827922000-06-06 17:27:05 +00001991 /* Use the standard inner loop if we are not dealing with
1992 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001993 */
drhda9d6c42000-05-31 18:20:14 +00001994 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00001995 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
1996 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001997 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001998 }
drhcce7d172000-05-31 15:34:51 +00001999 }
drhefb72512000-05-31 20:00:52 +00002000
drhe3184742002-06-19 14:27:05 +00002001 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00002002 ** processing.
drhefb72512000-05-31 20:00:52 +00002003 */
drh22827922000-06-06 17:27:05 +00002004 else{
drh22827922000-06-06 17:27:05 +00002005 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00002006 int lbl1;
drh22827922000-06-06 17:27:05 +00002007 for(i=0; i<pGroupBy->nExpr; i++){
2008 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
2009 }
drh99fcd712001-10-13 01:06:47 +00002010 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh491791a2002-07-18 00:34:09 +00002011 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
drh1bee3d72001-10-15 00:44:35 +00002012 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002013 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00002014 for(i=0; i<pParse->nAgg; i++){
2015 if( pParse->aAgg[i].isAgg ) continue;
2016 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00002017 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00002018 }
drh22827922000-06-06 17:27:05 +00002019 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00002020 }
drh22827922000-06-06 17:27:05 +00002021 for(i=0; i<pParse->nAgg; i++){
2022 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00002023 int j;
drh22827922000-06-06 17:27:05 +00002024 if( !pParse->aAgg[i].isAgg ) continue;
2025 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00002026 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00002027 if( pE->pList ){
2028 for(j=0; j<pE->pList->nExpr; j++){
2029 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
2030 }
drhe5095352002-02-24 03:25:14 +00002031 }
drh0bce8352002-02-28 00:41:10 +00002032 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00002033 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00002034 assert( pParse->aAgg[i].pFunc!=0 );
2035 assert( pParse->aAgg[i].pFunc->xStep!=0 );
2036 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00002037 }
drhcce7d172000-05-31 15:34:51 +00002038 }
2039
2040 /* End the database scan loop.
2041 */
2042 sqliteWhereEnd(pWInfo);
2043
drh22827922000-06-06 17:27:05 +00002044 /* If we are processing aggregates, we need to set up a second loop
2045 ** over all of the aggregate values and process them.
2046 */
2047 if( isAgg ){
2048 int endagg = sqliteVdbeMakeLabel(v);
2049 int startagg;
drh99fcd712001-10-13 01:06:47 +00002050 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002051 pParse->useAgg = 1;
2052 if( pHaving ){
drhf5905aa2002-05-26 20:54:33 +00002053 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002054 }
drhdf199a22002-06-14 22:38:41 +00002055 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2056 iParm, startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00002057 goto select_end;
drh22827922000-06-06 17:27:05 +00002058 }
drh99fcd712001-10-13 01:06:47 +00002059 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
2060 sqliteVdbeResolveLabel(v, endagg);
2061 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002062 pParse->useAgg = 0;
2063 }
2064
drhcce7d172000-05-31 15:34:51 +00002065 /* If there is an ORDER BY clause, then we need to sort the results
2066 ** and send them to the callback one by one.
2067 */
2068 if( pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00002069 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002070 }
drh6a535342001-10-19 16:44:56 +00002071
2072
2073 /* Issue a null callback if that is what the user wants.
2074 */
2075 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
2076 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
2077 }
2078
drh1d83f052002-02-17 00:30:36 +00002079 /* The SELECT was successfully coded. Set the return code to 0
2080 ** to indicate no errors.
2081 */
2082 rc = 0;
2083
2084 /* Control jumps to here if an error is encountered above, or upon
2085 ** successful coding of the SELECT.
2086 */
2087select_end:
drh142e30d2002-08-28 03:00:58 +00002088 /* pParse->nTab = base; */
drh1d83f052002-02-17 00:30:36 +00002089 sqliteAggregateInfoReset(pParse);
2090 return rc;
drhcce7d172000-05-31 15:34:51 +00002091}