blob: cc9a7b5548405557b9a04e167bc6f9d40276cbc6 [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**
drh8c74a8c2002-08-25 19:20:40 +000015** $Id: select.c,v 1.110 2002/08/25 19:20:40 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
drhc926afb2002-06-20 03:38:26 +0000523 /* Discard the results. This is used for SELECT statements inside
524 ** the body of a TRIGGER. The purpose of such selects is to call
525 ** user-defined functions that have side effects. We do not care
526 ** about the actual results of the select.
527 */
drhc926afb2002-06-20 03:38:26 +0000528 default: {
drhf46f9052002-06-22 02:33:38 +0000529 assert( eDest==SRT_Discard );
530 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000531 break;
532 }
drh82c3d632000-06-06 21:56:07 +0000533 }
534 return 0;
535}
536
537/*
drhd8bc7082000-06-07 23:51:50 +0000538** If the inner loop was generated using a non-null pOrderBy argument,
539** then the results were placed in a sorter. After the loop is terminated
540** we need to run the sorter and output the results. The following
541** routine generates the code needed to do that.
542*/
drhc926afb2002-06-20 03:38:26 +0000543static void generateSortTail(
544 Select *p, /* The SELECT statement */
545 Vdbe *v, /* Generate code into this VDBE */
546 int nColumn, /* Number of columns of data */
547 int eDest, /* Write the sorted results here */
548 int iParm /* Optional parameter associated with eDest */
549){
drhd8bc7082000-06-07 23:51:50 +0000550 int end = sqliteVdbeMakeLabel(v);
551 int addr;
drhf46f9052002-06-22 02:33:38 +0000552 if( eDest==SRT_Sorter ) return;
drh99fcd712001-10-13 01:06:47 +0000553 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
554 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drhdf199a22002-06-14 22:38:41 +0000555 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000556 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+4);
557 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
558 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drhdf199a22002-06-14 22:38:41 +0000559 }
drhd11d3822002-06-21 23:01:49 +0000560 if( p->nLimit>=0 ){
561 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, end);
drhdf199a22002-06-14 22:38:41 +0000562 }
drhc926afb2002-06-20 03:38:26 +0000563 switch( eDest ){
564 case SRT_Callback: {
565 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
566 break;
567 }
568 case SRT_Table:
569 case SRT_TempTable: {
570 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
571 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
572 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
573 break;
574 }
575 case SRT_Set: {
576 assert( nColumn==1 );
577 sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3);
578 sqliteVdbeAddOp(v, OP_String, 0, 0);
579 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
580 break;
581 }
582 case SRT_Mem: {
583 assert( nColumn==1 );
584 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
585 sqliteVdbeAddOp(v, OP_Goto, 0, end);
586 break;
587 }
588 default: {
drhf46f9052002-06-22 02:33:38 +0000589 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000590 break;
591 }
592 }
drh99fcd712001-10-13 01:06:47 +0000593 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
594 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000595 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000596}
597
598/*
drh82c3d632000-06-06 21:56:07 +0000599** Generate code that will tell the VDBE how many columns there
600** are in the result and the name for each column. This information
601** is used to provide "argc" and "azCol[]" values in the callback.
602*/
drh832508b2002-03-02 17:04:07 +0000603static void generateColumnNames(
604 Parse *pParse, /* Parser context */
605 int base, /* VDBE cursor corresponding to first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000606 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000607 ExprList *pEList /* Expressions defining the result set */
608){
drhd8bc7082000-06-07 23:51:50 +0000609 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000610 int i;
drhdaffd0e2001-04-11 14:28:42 +0000611 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000612 pParse->colNamesSet = 1;
drh5080aaa2002-07-11 12:18:16 +0000613 if( pParse->db->flags & SQLITE_ReportTypes ){
614 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr*2, 0);
615 }else{
616 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
617 }
drh82c3d632000-06-06 21:56:07 +0000618 for(i=0; i<pEList->nExpr; i++){
619 Expr *p;
drhb1363202002-06-26 02:45:03 +0000620 char *zType = 0;
drh1bee3d72001-10-15 00:44:35 +0000621 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000622 if( pEList->a[i].zName ){
623 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000624 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
625 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000626 continue;
627 }
628 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000629 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000630 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
drhfa173a72002-07-10 21:26:00 +0000631 if( p->op==TK_COLUMN && pTabList ){
drh832508b2002-03-02 17:04:07 +0000632 Table *pTab = pTabList->a[p->iTable - base].pTab;
drh97665872002-02-13 23:22:53 +0000633 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000634 int iCol = p->iColumn;
635 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000636 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000637 if( iCol<0 ){
638 zCol = "_ROWID_";
639 zType = "INTEGER";
640 }else{
641 zCol = pTab->aCol[iCol].zName;
642 zType = pTab->aCol[iCol].zType;
643 }
drh4b59ab52002-08-24 18:24:51 +0000644 if( p->token.z && p->token.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000645 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000646 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drhfa173a72002-07-10 21:26:00 +0000647 sqliteVdbeCompressSpace(v, addr);
648 }else if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000649 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000650 char *zTab;
651
drh832508b2002-03-02 17:04:07 +0000652 zTab = pTabList->a[p->iTable - base].zAlias;
drh01a34662001-10-20 12:30:10 +0000653 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000654 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000655 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
656 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000657 sqliteFree(zName);
658 }else{
drh99fcd712001-10-13 01:06:47 +0000659 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000660 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000661 }
drh4b59ab52002-08-24 18:24:51 +0000662 }else if( p->token.z && p->token.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000663 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000664 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drhfa173a72002-07-10 21:26:00 +0000665 sqliteVdbeCompressSpace(v, addr);
drh4b59ab52002-08-24 18:24:51 +0000666 }else if( p->token.z && p->token.z[0] ){
drh1bee3d72001-10-15 00:44:35 +0000667 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000668 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drh1bee3d72001-10-15 00:44:35 +0000669 sqliteVdbeCompressSpace(v, addr);
670 }else{
671 char zName[30];
672 assert( p->op!=TK_COLUMN || pTabList==0 );
673 sprintf(zName, "column%d", i+1);
674 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
675 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000676 }
drh5080aaa2002-07-11 12:18:16 +0000677 if( pParse->db->flags & SQLITE_ReportTypes ){
678 if( zType==0 ){
679 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
680 zType = "TEXT";
681 }else{
682 zType = "NUMERIC";
683 }
drhb1363202002-06-26 02:45:03 +0000684 }
drh5080aaa2002-07-11 12:18:16 +0000685 sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0);
686 sqliteVdbeChangeP3(v, -1, zType, P3_STATIC);
drhb1363202002-06-26 02:45:03 +0000687 }
drh82c3d632000-06-06 21:56:07 +0000688 }
689}
690
691/*
drhd8bc7082000-06-07 23:51:50 +0000692** Name of the connection operator, used for error messages.
693*/
694static const char *selectOpName(int id){
695 char *z;
696 switch( id ){
697 case TK_ALL: z = "UNION ALL"; break;
698 case TK_INTERSECT: z = "INTERSECT"; break;
699 case TK_EXCEPT: z = "EXCEPT"; break;
700 default: z = "UNION"; break;
701 }
702 return z;
703}
704
705/*
drh22f70c32002-02-18 01:17:00 +0000706** Given a SELECT statement, generate a Table structure that describes
707** the result set of that SELECT.
708*/
709Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
710 Table *pTab;
711 int i;
712 ExprList *pEList;
713 static int fillInColumnList(Parse*, Select*);
714
715 if( fillInColumnList(pParse, pSelect) ){
716 return 0;
717 }
718 pTab = sqliteMalloc( sizeof(Table) );
719 if( pTab==0 ){
720 return 0;
721 }
722 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
723 pEList = pSelect->pEList;
724 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000725 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000726 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
727 for(i=0; i<pTab->nCol; i++){
728 Expr *p;
729 if( pEList->a[i].zName ){
730 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
drh4b59ab52002-08-24 18:24:51 +0000731 }else if( (p=pEList->a[i].pExpr)->token.z && p->token.z[0] ){
732 sqliteSetNString(&pTab->aCol[i].zName, p->token.z, p->token.n, 0);
drhd820cb12002-02-18 03:21:45 +0000733 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
734 p->pRight->token.z[0] ){
735 sqliteSetNString(&pTab->aCol[i].zName,
736 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000737 }else{
738 char zBuf[30];
739 sprintf(zBuf, "column%d", i+1);
740 pTab->aCol[i].zName = sqliteStrDup(zBuf);
741 }
742 }
743 pTab->iPKey = -1;
744 return pTab;
745}
746
747/*
drhad2d8302002-05-24 20:31:36 +0000748** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000749**
drhad3cab52002-05-24 02:04:32 +0000750** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh22f70c32002-02-18 01:17:00 +0000751** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000752**
drhad2d8302002-05-24 20:31:36 +0000753** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
754** on joins and the ON and USING clause of joins.
755**
756** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000757** for instances of the "*" operator or the TABLE.* operator.
758** If found, expand each "*" to be every column in every table
759** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000760**
761** Return 0 on success. If there are problems, leave an error message
762** in pParse and return non-zero.
763*/
764static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000765 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000766 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000767 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000768 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000769
770 if( p==0 || p->pSrc==0 ) return 1;
771 pTabList = p->pSrc;
772 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000773
774 /* Look up every table in the table list.
775 */
drhad3cab52002-05-24 02:04:32 +0000776 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000777 if( pTabList->a[i].pTab ){
778 /* This routine has run before! No need to continue */
779 return 0;
780 }
drhdaffd0e2001-04-11 14:28:42 +0000781 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000782 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000783 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000784 if( pTabList->a[i].zAlias==0 ){
785 char zFakeName[60];
786 sprintf(zFakeName, "sqlite_subquery_%p_",
787 (void*)pTabList->a[i].pSelect);
788 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
789 }
drh22f70c32002-02-18 01:17:00 +0000790 pTabList->a[i].pTab = pTab =
791 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
792 pTabList->a[i].pSelect);
793 if( pTab==0 ){
794 return 1;
795 }
796 pTab->isTransient = 1;
797 }else{
drha76b5df2002-02-23 02:32:10 +0000798 /* An ordinary table or view name in the FROM clause */
799 pTabList->a[i].pTab = pTab =
800 sqliteFindTable(pParse->db, pTabList->a[i].zName);
801 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000802 sqliteSetString(&pParse->zErrMsg, "no such table: ",
803 pTabList->a[i].zName, 0);
804 pParse->nErr++;
805 return 1;
806 }
drha76b5df2002-02-23 02:32:10 +0000807 if( pTab->pSelect ){
drh417be792002-03-03 18:59:40 +0000808 if( sqliteViewGetColumnNames(pParse, pTab) ){
809 return 1;
810 }
drhd94a6692002-08-25 18:29:11 +0000811 sqliteSelectDelete(pTabList->a[i].pSelect);
drhff78bd22002-02-27 01:47:11 +0000812 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000813 }
drhd8bc7082000-06-07 23:51:50 +0000814 }
815 }
816
drhad2d8302002-05-24 20:31:36 +0000817 /* Process NATURAL keywords, and ON and USING clauses of joins.
818 */
819 if( sqliteProcessJoin(pParse, p) ) return 1;
820
drh7c917d12001-12-16 20:05:05 +0000821 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000822 ** all columns in all tables. And for every TABLE.* insert the names
823 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000824 ** with the TK_ALL operator for each "*" that it found in the column list.
825 ** The following code just has to locate the TK_ALL expressions and expand
826 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000827 **
828 ** The first loop just checks to see if there are any "*" operators
829 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000830 */
drh7c917d12001-12-16 20:05:05 +0000831 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000832 Expr *pE = pEList->a[k].pExpr;
833 if( pE->op==TK_ALL ) break;
834 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
835 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000836 }
drh54473222002-04-04 02:10:55 +0000837 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000838 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000839 /*
840 ** If we get here it means the result set contains one or more "*"
841 ** operators that need to be expanded. Loop through each expression
842 ** in the result set and expand them one by one.
843 */
drh7c917d12001-12-16 20:05:05 +0000844 struct ExprList_item *a = pEList->a;
845 ExprList *pNew = 0;
846 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000847 Expr *pE = a[k].pExpr;
848 if( pE->op!=TK_ALL &&
849 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
850 /* This particular expression does not need to be expanded.
851 */
drh7c917d12001-12-16 20:05:05 +0000852 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
853 pNew->a[pNew->nExpr-1].zName = a[k].zName;
854 a[k].pExpr = 0;
855 a[k].zName = 0;
856 }else{
drh54473222002-04-04 02:10:55 +0000857 /* This expression is a "*" or a "TABLE.*" and needs to be
858 ** expanded. */
859 int tableSeen = 0; /* Set to 1 when TABLE matches */
860 Token *pName; /* text of name of TABLE */
861 if( pE->op==TK_DOT && pE->pLeft ){
862 pName = &pE->pLeft->token;
863 }else{
864 pName = 0;
865 }
drhad3cab52002-05-24 02:04:32 +0000866 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000867 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000868 char *zTabName = pTabList->a[i].zAlias;
869 if( zTabName==0 || zTabName[0]==0 ){
870 zTabName = pTab->zName;
871 }
drhc754fa52002-05-27 03:25:51 +0000872 if( pName && (zTabName==0 || zTabName[0]==0 ||
873 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
874 zTabName[pName->n]!=0) ){
drh54473222002-04-04 02:10:55 +0000875 continue;
876 }
877 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000878 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000879 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000880 char *zName = pTab->aCol[j].zName;
881
882 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
883 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
884 /* In a NATURAL join, omit the join columns from the
885 ** table on the right */
886 continue;
887 }
888 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
889 /* In a join with a USING clause, omit columns in the
890 ** using clause from the table on the right. */
891 continue;
892 }
drh22f70c32002-02-18 01:17:00 +0000893 pRight = sqliteExpr(TK_ID, 0, 0, 0);
894 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000895 pRight->token.z = zName;
896 pRight->token.n = strlen(zName);
drh4b59ab52002-08-24 18:24:51 +0000897 pRight->token.dyn = 0;
898 pRight->token.base = 1;
899 if( zTabName && pTabList->nSrc>1 ){
drh22f70c32002-02-18 01:17:00 +0000900 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh22f70c32002-02-18 01:17:00 +0000901 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
902 if( pExpr==0 ) break;
drh4b59ab52002-08-24 18:24:51 +0000903 pLeft->token.z = zTabName;
904 pLeft->token.n = strlen(zTabName);
905 pLeft->token.dyn = 0;
906 pLeft->token.base = 1;
907 sqliteSetString((char**)&pExpr->token.z, zTabName, ".", zName, 0);
908 pExpr->token.n = strlen(pExpr->token.z);
909 pExpr->token.base = 0;
910 pExpr->token.dyn = 1;
drh7c917d12001-12-16 20:05:05 +0000911 }else{
drh22f70c32002-02-18 01:17:00 +0000912 pExpr = pRight;
drh7c917d12001-12-16 20:05:05 +0000913 }
drh7c917d12001-12-16 20:05:05 +0000914 pNew = sqliteExprListAppend(pNew, pExpr, 0);
915 }
drh17e24df2001-11-06 14:10:41 +0000916 }
drh54473222002-04-04 02:10:55 +0000917 if( !tableSeen ){
drhf5db2d32002-06-06 23:42:27 +0000918 if( pName ){
919 sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
920 pName->z, pName->n, 0);
921 }else{
922 sqliteSetString(&pParse->zErrMsg, "no tables specified", 0);
923 }
drh54473222002-04-04 02:10:55 +0000924 rc = 1;
925 }
drhd8bc7082000-06-07 23:51:50 +0000926 }
927 }
drh7c917d12001-12-16 20:05:05 +0000928 sqliteExprListDelete(pEList);
929 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000930 }
drh54473222002-04-04 02:10:55 +0000931 return rc;
drhd8bc7082000-06-07 23:51:50 +0000932}
933
934/*
drhff78bd22002-02-27 01:47:11 +0000935** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
936** in a select structure. It just sets the pointers to NULL. This
937** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
938** pointer is not NULL, this routine is called recursively on that pointer.
939**
940** This routine is called on the Select structure that defines a
941** VIEW in order to undo any bindings to tables. This is necessary
942** because those tables might be DROPed by a subsequent SQL command.
943*/
944void sqliteSelectUnbind(Select *p){
945 int i;
drhad3cab52002-05-24 02:04:32 +0000946 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +0000947 Table *pTab;
948 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +0000949 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +0000950 if( (pTab = pSrc->a[i].pTab)!=0 ){
951 if( pTab->isTransient ){
952 sqliteDeleteTable(0, pTab);
drh4b59ab52002-08-24 18:24:51 +0000953#if 0
drhff78bd22002-02-27 01:47:11 +0000954 sqliteSelectDelete(pSrc->a[i].pSelect);
955 pSrc->a[i].pSelect = 0;
drh4b59ab52002-08-24 18:24:51 +0000956#endif
drhff78bd22002-02-27 01:47:11 +0000957 }
958 pSrc->a[i].pTab = 0;
959 if( pSrc->a[i].pSelect ){
960 sqliteSelectUnbind(pSrc->a[i].pSelect);
961 }
962 }
963 }
964}
965
966/*
drhd8bc7082000-06-07 23:51:50 +0000967** This routine associates entries in an ORDER BY expression list with
968** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000969** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000970** the top-level node is filled in with column number and the iTable
971** value of the top-level node is filled with iTable parameter.
972**
973** If there are prior SELECT clauses, they are processed first. A match
974** in an earlier SELECT takes precedence over a later SELECT.
975**
976** Any entry that does not match is flagged as an error. The number
977** of errors is returned.
978*/
979static int matchOrderbyToColumn(
980 Parse *pParse, /* A place to leave error messages */
981 Select *pSelect, /* Match to result columns of this SELECT */
982 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +0000983 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +0000984 int mustComplete /* If TRUE all ORDER BYs must match */
985){
986 int nErr = 0;
987 int i, j;
988 ExprList *pEList;
989
drhdaffd0e2001-04-11 14:28:42 +0000990 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000991 if( mustComplete ){
992 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
993 }
994 if( fillInColumnList(pParse, pSelect) ){
995 return 1;
996 }
997 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000998 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
999 return 1;
1000 }
drhd8bc7082000-06-07 23:51:50 +00001001 }
1002 pEList = pSelect->pEList;
1003 for(i=0; i<pOrderBy->nExpr; i++){
1004 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +00001005 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +00001006 if( pOrderBy->a[i].done ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001007 if( sqliteExprIsInteger(pE, &iCol) ){
1008 if( iCol<=0 || iCol>pEList->nExpr ){
1009 char zBuf[200];
1010 sprintf(zBuf,"ORDER BY position %d should be between 1 and %d",
1011 iCol, pEList->nExpr);
1012 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1013 pParse->nErr++;
1014 nErr++;
1015 break;
1016 }
1017 iCol--;
1018 }
1019 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001020 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001021 char *zName, *zLabel;
1022 zName = pEList->a[j].zName;
1023 assert( pE->token.z );
1024 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +00001025 sqliteDequote(zLabel);
1026 if( sqliteStrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001027 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001028 }
drh6e142f52000-06-08 13:36:40 +00001029 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001030 }
drhe4de1fe2002-06-02 16:09:01 +00001031 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
1032 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001033 }
1034 }
drhe4de1fe2002-06-02 16:09:01 +00001035 if( iCol>=0 ){
1036 pE->op = TK_COLUMN;
1037 pE->iColumn = iCol;
1038 pE->iTable = iTable;
1039 pOrderBy->a[i].done = 1;
1040 }
1041 if( iCol<0 && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +00001042 char zBuf[30];
1043 sprintf(zBuf,"%d",i+1);
1044 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
1045 " does not match any result column", 0);
1046 pParse->nErr++;
1047 nErr++;
1048 break;
1049 }
1050 }
1051 return nErr;
1052}
1053
1054/*
1055** Get a VDBE for the given parser context. Create a new one if necessary.
1056** If an error occurs, return NULL and leave a message in pParse.
1057*/
1058Vdbe *sqliteGetVdbe(Parse *pParse){
1059 Vdbe *v = pParse->pVdbe;
1060 if( v==0 ){
drh4c504392000-10-16 22:06:40 +00001061 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001062 }
drhd8bc7082000-06-07 23:51:50 +00001063 return v;
1064}
1065
1066
1067/*
drh82c3d632000-06-06 21:56:07 +00001068** This routine is called to process a query that is really the union
1069** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001070**
1071** "p" points to the right-most of the two queries. The results should
1072** be stored in eDest with parameter iParm.
drh82c3d632000-06-06 21:56:07 +00001073*/
1074static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +00001075 int rc; /* Success code from a subroutine */
1076 Select *pPrior; /* Another SELECT immediately to our left */
1077 Vdbe *v; /* Generate code to this VDBE */
1078 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +00001079
drhd8bc7082000-06-07 23:51:50 +00001080 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
1081 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +00001082 */
drhdaffd0e2001-04-11 14:28:42 +00001083 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001084 pPrior = p->pPrior;
1085 if( pPrior->pOrderBy ){
1086 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
1087 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +00001088 pParse->nErr++;
1089 return 1;
1090 }
1091
drhd8bc7082000-06-07 23:51:50 +00001092 /* Make sure we have a valid query engine. If not, create a new one.
1093 */
1094 v = sqliteGetVdbe(pParse);
1095 if( v==0 ) return 1;
1096
drh1cc3d752002-03-23 00:31:29 +00001097 /* Create the destination temporary table if necessary
1098 */
1099 if( eDest==SRT_TempTable ){
1100 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1101 eDest = SRT_Table;
1102 }
1103
drhf46f9052002-06-22 02:33:38 +00001104 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001105 */
drh10e5e3c2000-06-08 00:19:02 +00001106 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +00001107 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001108 case TK_ALL: {
1109 if( p->pOrderBy==0 ){
1110 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
1111 if( rc ) return rc;
1112 p->pPrior = 0;
1113 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
1114 p->pPrior = pPrior;
1115 if( rc ) return rc;
1116 break;
1117 }
1118 /* For UNION ALL ... ORDER BY fall through to the next case */
1119 }
drh82c3d632000-06-06 21:56:07 +00001120 case TK_EXCEPT:
1121 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001122 int unionTab; /* Cursor number of the temporary table holding result */
1123 int op; /* One of the SRT_ operations to apply to self */
1124 int priorOp; /* The SRT_ operation to apply to prior selects */
drhc926afb2002-06-20 03:38:26 +00001125 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
drh82c3d632000-06-06 21:56:07 +00001126
drhd8bc7082000-06-07 23:51:50 +00001127 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drhc926afb2002-06-20 03:38:26 +00001128 if( eDest==priorOp && p->pOrderBy==0 ){
drhd8bc7082000-06-07 23:51:50 +00001129 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001130 ** right.
drhd8bc7082000-06-07 23:51:50 +00001131 */
drh82c3d632000-06-06 21:56:07 +00001132 unionTab = iParm;
1133 }else{
drhd8bc7082000-06-07 23:51:50 +00001134 /* We will need to create our own temporary table to hold the
1135 ** intermediate results.
1136 */
1137 unionTab = pParse->nTab++;
1138 if( p->pOrderBy
1139 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
1140 return 1;
1141 }
drhd8bc7082000-06-07 23:51:50 +00001142 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +00001143 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +00001144 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +00001145 }else{
drh99fcd712001-10-13 01:06:47 +00001146 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001147 }
drh82c3d632000-06-06 21:56:07 +00001148 }
drhd8bc7082000-06-07 23:51:50 +00001149
1150 /* Code the SELECT statements to our left
1151 */
drh832508b2002-03-02 17:04:07 +00001152 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001153 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001154
1155 /* Code the current SELECT statement
1156 */
1157 switch( p->op ){
1158 case TK_EXCEPT: op = SRT_Except; break;
1159 case TK_UNION: op = SRT_Union; break;
1160 case TK_ALL: op = SRT_Table; break;
1161 }
drh82c3d632000-06-06 21:56:07 +00001162 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001163 pOrderBy = p->pOrderBy;
1164 p->pOrderBy = 0;
drh832508b2002-03-02 17:04:07 +00001165 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001166 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001167 p->pOrderBy = pOrderBy;
drh82c3d632000-06-06 21:56:07 +00001168 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001169
1170 /* Convert the data in the temporary table into whatever form
1171 ** it is that we currently need.
1172 */
drhc926afb2002-06-20 03:38:26 +00001173 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001174 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001175 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001176 if( eDest==SRT_Callback ){
1177 generateColumnNames(pParse, p->base, 0, p->pEList);
1178 }
drh82c3d632000-06-06 21:56:07 +00001179 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001180 iCont = sqliteVdbeMakeLabel(v);
1181 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1182 iStart = sqliteVdbeCurrentAddr(v);
drh38640e12002-07-05 21:42:36 +00001183 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001184 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001185 iCont, iBreak);
1186 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001187 sqliteVdbeResolveLabel(v, iCont);
1188 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +00001189 sqliteVdbeResolveLabel(v, iBreak);
1190 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001191 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001192 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001193 }
drh82c3d632000-06-06 21:56:07 +00001194 }
1195 break;
1196 }
1197 case TK_INTERSECT: {
1198 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001199 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001200
drhd8bc7082000-06-07 23:51:50 +00001201 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001202 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001203 ** by allocating the tables we will need.
1204 */
drh82c3d632000-06-06 21:56:07 +00001205 tab1 = pParse->nTab++;
1206 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001207 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
1208 return 1;
1209 }
drhc6b52df2002-01-04 03:09:29 +00001210 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +00001211 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +00001212
1213 /* Code the SELECTs to our left into temporary table "tab1".
1214 */
drh832508b2002-03-02 17:04:07 +00001215 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001216 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001217
1218 /* Code the current SELECT into temporary table "tab2"
1219 */
drhc6b52df2002-01-04 03:09:29 +00001220 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +00001221 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +00001222 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +00001223 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001224 p->pPrior = pPrior;
1225 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001226
1227 /* Generate code to take the intersection of the two temporary
1228 ** tables.
1229 */
drh82c3d632000-06-06 21:56:07 +00001230 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001231 if( eDest==SRT_Callback ){
1232 generateColumnNames(pParse, p->base, 0, p->pEList);
1233 }
drh82c3d632000-06-06 21:56:07 +00001234 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001235 iCont = sqliteVdbeMakeLabel(v);
1236 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
1237 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001238 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh38640e12002-07-05 21:42:36 +00001239 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001240 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001241 iCont, iBreak);
1242 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001243 sqliteVdbeResolveLabel(v, iCont);
1244 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001245 sqliteVdbeResolveLabel(v, iBreak);
1246 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1247 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001248 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001249 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001250 }
drh82c3d632000-06-06 21:56:07 +00001251 break;
1252 }
1253 }
1254 assert( p->pEList && pPrior->pEList );
1255 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +00001256 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1257 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +00001258 pParse->nErr++;
1259 return 1;
drh22827922000-06-06 17:27:05 +00001260 }
drh10e5e3c2000-06-08 00:19:02 +00001261 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +00001262 return 0;
1263}
1264
1265/*
drh832508b2002-03-02 17:04:07 +00001266** Recursively scan through an expression tree. For every reference
1267** to a column in table number iFrom, change that reference to the
1268** same column in table number iTo.
1269*/
1270static void changeTables(Expr *pExpr, int iFrom, int iTo){
1271 if( pExpr==0 ) return;
1272 if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1273 pExpr->iTable = iTo;
1274 }else{
drh1b2e0322002-03-03 02:49:51 +00001275 static void changeTablesInList(ExprList*, int, int);
drh832508b2002-03-02 17:04:07 +00001276 changeTables(pExpr->pLeft, iFrom, iTo);
1277 changeTables(pExpr->pRight, iFrom, iTo);
drh1b2e0322002-03-03 02:49:51 +00001278 changeTablesInList(pExpr->pList, iFrom, iTo);
1279 }
1280}
1281static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
1282 if( pList ){
1283 int i;
1284 for(i=0; i<pList->nExpr; i++){
1285 changeTables(pList->a[i].pExpr, iFrom, iTo);
drh832508b2002-03-02 17:04:07 +00001286 }
1287 }
1288}
1289
1290/*
1291** Scan through the expression pExpr. Replace every reference to
1292** a column in table number iTable with a copy of the corresponding
drh84e59202002-03-14 14:33:31 +00001293** entry in pEList. (But leave references to the ROWID column
1294** unchanged.) When making a copy of an expression in pEList, change
1295** references to columns in table iSub into references to table iTable.
drh832508b2002-03-02 17:04:07 +00001296**
1297** This routine is part of the flattening procedure. A subquery
1298** whose result set is defined by pEList appears as entry in the
1299** FROM clause of a SELECT such that the VDBE cursor assigned to that
1300** FORM clause entry is iTable. This routine make the necessary
1301** changes to pExpr so that it refers directly to the source table
1302** of the subquery rather the result set of the subquery.
1303*/
1304static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1305 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001306 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001307 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001308 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001309 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1310 pNew = pEList->a[pExpr->iColumn].pExpr;
1311 assert( pNew!=0 );
1312 pExpr->op = pNew->op;
drhd94a6692002-08-25 18:29:11 +00001313 assert( pExpr->pLeft==0 );
drh832508b2002-03-02 17:04:07 +00001314 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
drhd94a6692002-08-25 18:29:11 +00001315 assert( pExpr->pRight==0 );
drh832508b2002-03-02 17:04:07 +00001316 pExpr->pRight = sqliteExprDup(pNew->pRight);
drhd94a6692002-08-25 18:29:11 +00001317 assert( pExpr->pList==0 );
drh832508b2002-03-02 17:04:07 +00001318 pExpr->pList = sqliteExprListDup(pNew->pList);
1319 pExpr->iTable = pNew->iTable;
1320 pExpr->iColumn = pNew->iColumn;
1321 pExpr->iAgg = pNew->iAgg;
drh4b59ab52002-08-24 18:24:51 +00001322 pExpr->nFuncName = pNew->nFuncName;
1323 sqliteTokenCopy(&pExpr->token, &pNew->token);
drh832508b2002-03-02 17:04:07 +00001324 if( iSub!=iTable ){
1325 changeTables(pExpr, iSub, iTable);
1326 }
1327 }else{
1328 static void substExprList(ExprList*,int,ExprList*,int);
1329 substExpr(pExpr->pLeft, iTable, pEList, iSub);
1330 substExpr(pExpr->pRight, iTable, pEList, iSub);
1331 substExprList(pExpr->pList, iTable, pEList, iSub);
1332 }
1333}
1334static void
1335substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1336 int i;
1337 if( pList==0 ) return;
1338 for(i=0; i<pList->nExpr; i++){
1339 substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1340 }
1341}
1342
1343/*
drh1350b032002-02-27 19:00:20 +00001344** This routine attempts to flatten subqueries in order to speed
1345** execution. It returns 1 if it makes changes and 0 if no flattening
1346** occurs.
1347**
1348** To understand the concept of flattening, consider the following
1349** query:
1350**
1351** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1352**
1353** The default way of implementing this query is to execute the
1354** subquery first and store the results in a temporary table, then
1355** run the outer query on that temporary table. This requires two
1356** passes over the data. Furthermore, because the temporary table
1357** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001358** optimized.
drh1350b032002-02-27 19:00:20 +00001359**
drh832508b2002-03-02 17:04:07 +00001360** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001361** a single flat select, like this:
1362**
1363** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1364**
1365** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001366** but only has to scan the data once. And because indices might
1367** exist on the table t1, a complete scan of the data might be
1368** avoided.
drh1350b032002-02-27 19:00:20 +00001369**
drh832508b2002-03-02 17:04:07 +00001370** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001371**
drh832508b2002-03-02 17:04:07 +00001372** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001373**
drh832508b2002-03-02 17:04:07 +00001374** (2) The subquery is not an aggregate or the outer query is not a join.
1375**
1376** (3) The subquery is not a join.
1377**
1378** (4) The subquery is not DISTINCT or the outer query is not a join.
1379**
1380** (5) The subquery is not DISTINCT or the outer query does not use
1381** aggregates.
1382**
1383** (6) The subquery does not use aggregates or the outer query is not
1384** DISTINCT.
1385**
drh08192d52002-04-30 19:20:28 +00001386** (7) The subquery has a FROM clause.
1387**
drhdf199a22002-06-14 22:38:41 +00001388** (8) The subquery does not use LIMIT or the outer query is not a join.
1389**
1390** (9) The subquery does not use LIMIT or the outer query does not use
1391** aggregates.
1392**
1393** (10) The subquery does not use aggregates or the outer query does not
1394** use LIMIT.
1395**
drh832508b2002-03-02 17:04:07 +00001396** In this routine, the "p" parameter is a pointer to the outer query.
1397** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1398** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1399**
1400** If flattening is not attempted, this routine is a no-op and return 0.
1401** If flattening is attempted this routine returns 1.
1402**
1403** All of the expression analysis must occur on both the outer query and
1404** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001405*/
drh8c74a8c2002-08-25 19:20:40 +00001406static int flattenSubquery(
1407 Parse *pParse, /* The parsing context */
1408 Select *p, /* The parent or outer SELECT statement */
1409 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
1410 int isAgg, /* True if outer SELECT uses aggregate functions */
1411 int subqueryIsAgg /* True if the subquery uses aggregate functions */
1412){
drh0bb28102002-05-08 11:54:14 +00001413 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001414 SrcList *pSrc; /* The FROM clause of the outer query */
1415 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001416 ExprList *pList; /* The result set of the outer query */
drh832508b2002-03-02 17:04:07 +00001417 int i;
1418 int iParent, iSub;
1419 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001420
drh832508b2002-03-02 17:04:07 +00001421 /* Check to see if flattening is permitted. Return 0 if not.
1422 */
1423 if( p==0 ) return 0;
1424 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001425 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001426 pSub = pSrc->a[iFrom].pSelect;
1427 assert( pSub!=0 );
1428 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001429 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001430 pSubSrc = pSub->pSrc;
1431 assert( pSubSrc );
drhad3cab52002-05-24 02:04:32 +00001432 if( pSubSrc->nSrc!=1 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001433 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1434 return 0;
1435 }
drhd11d3822002-06-21 23:01:49 +00001436 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh832508b2002-03-02 17:04:07 +00001437
drh0bb28102002-05-08 11:54:14 +00001438 /* If we reach this point, it means flattening is permitted for the
drh832508b2002-03-02 17:04:07 +00001439 ** i-th entry of the FROM clause in the outer query.
1440 */
1441 iParent = p->base + iFrom;
1442 iSub = pSub->base;
1443 substExprList(p->pEList, iParent, pSub->pEList, iSub);
1444 pList = p->pEList;
1445 for(i=0; i<pList->nExpr; i++){
1446 if( pList->a[i].zName==0 ){
1447 Expr *pExpr = pList->a[i].pExpr;
drh4b59ab52002-08-24 18:24:51 +00001448 assert( pExpr->token.z!=0 );
1449 pList->a[i].zName = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh832508b2002-03-02 17:04:07 +00001450 }
1451 }
drh1b2e0322002-03-03 02:49:51 +00001452 if( isAgg ){
1453 substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1454 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
1455 }
drh832508b2002-03-02 17:04:07 +00001456 substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1457 if( pSub->pWhere ){
1458 pWhere = sqliteExprDup(pSub->pWhere);
1459 if( iParent!=iSub ){
1460 changeTables(pWhere, iSub, iParent);
1461 }
1462 }else{
1463 pWhere = 0;
1464 }
1465 if( subqueryIsAgg ){
1466 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001467 p->pHaving = p->pWhere;
1468 p->pWhere = pWhere;
drh832508b2002-03-02 17:04:07 +00001469 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
drh1b2e0322002-03-03 02:49:51 +00001470 if( pSub->pHaving ){
1471 Expr *pHaving = sqliteExprDup(pSub->pHaving);
1472 if( iParent!=iSub ){
1473 changeTables(pHaving, iSub, iParent);
1474 }
1475 if( p->pHaving ){
1476 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1477 }else{
1478 p->pHaving = pHaving;
1479 }
1480 }
1481 assert( p->pGroupBy==0 );
1482 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
1483 if( iParent!=iSub ){
1484 changeTablesInList(p->pGroupBy, iSub, iParent);
1485 }
drh832508b2002-03-02 17:04:07 +00001486 }else if( p->pWhere==0 ){
1487 p->pWhere = pWhere;
1488 }else{
1489 substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1490 if( pWhere ){
1491 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1492 }
1493 }
1494 p->isDistinct = p->isDistinct || pSub->isDistinct;
drh8c74a8c2002-08-25 19:20:40 +00001495
drhdf199a22002-06-14 22:38:41 +00001496 if( pSub->nLimit>=0 ){
1497 if( p->nLimit<0 ){
1498 p->nLimit = pSub->nLimit;
1499 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1500 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1501 }
1502 }
1503 p->nOffset += pSub->nOffset;
drh8c74a8c2002-08-25 19:20:40 +00001504
1505 /* If the subquery contains subqueries of its own, that were not
1506 ** flattened, then code will have already been generated to put
1507 ** the results of those sub-subqueries into VDBE cursors relative
1508 ** to the subquery. We must translate the cursor number into values
1509 ** suitable for use by the outer query.
1510 */
1511 for(i=0; i<pSubSrc->nSrc; i++){
1512 Vdbe *v;
1513 if( pSubSrc->a[i].pSelect==0 ) continue;
1514 v = sqliteGetVdbe(pParse);
1515 sqliteVdbeAddOp(v, OP_RenameCursor, pSub->base+i, p->base+i);
1516 }
1517
drh832508b2002-03-02 17:04:07 +00001518 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1519 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1520 }
1521 pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1522 pSubSrc->a[0].pTab = 0;
drhd94a6692002-08-25 18:29:11 +00001523 assert( pSrc->a[iFrom].pSelect==pSub );
drh832508b2002-03-02 17:04:07 +00001524 pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1525 pSubSrc->a[0].pSelect = 0;
1526 sqliteSelectDelete(pSub);
1527 return 1;
1528}
drh1350b032002-02-27 19:00:20 +00001529
1530/*
drh9562b552002-02-19 15:00:07 +00001531** Analyze the SELECT statement passed in as an argument to see if it
1532** is a simple min() or max() query. If it is and this query can be
1533** satisfied using a single seek to the beginning or end of an index,
1534** then generate the code for this SELECT return 1. If this is not a
1535** simple min() or max() query, then return 0;
1536**
1537** A simply min() or max() query looks like this:
1538**
1539** SELECT min(a) FROM table;
1540** SELECT max(a) FROM table;
1541**
1542** The query may have only a single table in its FROM argument. There
1543** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1544** be the min() or max() of a single column of the table. The column
1545** in the min() or max() function must be indexed.
1546**
1547** The parameters to this routine are the same as for sqliteSelect().
1548** See the header comment on that routine for additional information.
1549*/
1550static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1551 Expr *pExpr;
1552 int iCol;
1553 Table *pTab;
1554 Index *pIdx;
1555 int base;
1556 Vdbe *v;
1557 int openOp;
1558 int seekOp;
1559 int cont;
1560 ExprList eList;
1561 struct ExprList_item eListItem;
1562
1563 /* Check to see if this query is a simple min() or max() query. Return
1564 ** zero if it is not.
1565 */
1566 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001567 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001568 if( p->pEList->nExpr!=1 ) return 0;
1569 pExpr = p->pEList->a[0].pExpr;
1570 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1571 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh4b59ab52002-08-24 18:24:51 +00001572 if( pExpr->nFuncName!=3 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001573 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1574 seekOp = OP_Rewind;
1575 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1576 seekOp = OP_Last;
1577 }else{
1578 return 0;
1579 }
drh9562b552002-02-19 15:00:07 +00001580 pExpr = pExpr->pList->a[0].pExpr;
1581 if( pExpr->op!=TK_COLUMN ) return 0;
1582 iCol = pExpr->iColumn;
1583 pTab = p->pSrc->a[0].pTab;
1584
1585 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001586 ** Check to make sure we have an index and make pIdx point to the
1587 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1588 ** key column, no index is necessary so set pIdx to NULL. If no
1589 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001590 */
1591 if( iCol<0 ){
1592 pIdx = 0;
1593 }else{
1594 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1595 assert( pIdx->nColumn>=1 );
1596 if( pIdx->aiColumn[0]==iCol ) break;
1597 }
1598 if( pIdx==0 ) return 0;
1599 }
1600
drh17f71932002-02-21 12:01:27 +00001601 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001602 ** step is skipped if the output is going to a table or a memory cell.
1603 */
1604 v = sqliteGetVdbe(pParse);
1605 if( v==0 ) return 0;
1606 if( eDest==SRT_Callback ){
drh832508b2002-03-02 17:04:07 +00001607 generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001608 }
1609
drh17f71932002-02-21 12:01:27 +00001610 /* Generating code to find the min or the max. Basically all we have
1611 ** to do is find the first or the last entry in the chosen index. If
1612 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1613 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001614 */
drh5cf8e8c2002-02-19 22:42:05 +00001615 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
1616 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1617 pParse->schemaVerified = 1;
1618 }
drh9562b552002-02-19 15:00:07 +00001619 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +00001620 base = p->base;
drh9562b552002-02-19 15:00:07 +00001621 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001622 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001623 if( pIdx==0 ){
1624 sqliteVdbeAddOp(v, seekOp, base, 0);
1625 }else{
1626 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001627 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001628 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1629 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1630 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1631 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1632 }
drh5cf8e8c2002-02-19 22:42:05 +00001633 eList.nExpr = 1;
1634 memset(&eListItem, 0, sizeof(eListItem));
1635 eList.a = &eListItem;
1636 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +00001637 cont = sqliteVdbeMakeLabel(v);
drh38640e12002-07-05 21:42:36 +00001638 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
drh9562b552002-02-19 15:00:07 +00001639 sqliteVdbeResolveLabel(v, cont);
1640 sqliteVdbeAddOp(v, OP_Close, base, 0);
1641 return 1;
1642}
1643
1644/*
drh9bb61fe2000-06-05 16:01:39 +00001645** Generate code for the given SELECT statement.
1646**
drhfef52082000-06-06 01:50:43 +00001647** The results are distributed in various ways depending on the
1648** value of eDest and iParm.
1649**
1650** eDest Value Result
1651** ------------ -------------------------------------------
1652** SRT_Callback Invoke the callback for each row of the result.
1653**
1654** SRT_Mem Store first result in memory cell iParm
1655**
1656** SRT_Set Store results as keys of a table with cursor iParm
1657**
drh82c3d632000-06-06 21:56:07 +00001658** SRT_Union Store results as a key in a temporary table iParm
1659**
drhc4a3c772001-04-04 11:48:57 +00001660** SRT_Except Remove results form the temporary table iParm.
1661**
1662** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001663**
1664** This routine returns the number of errors. If any errors are
1665** encountered, then an appropriate error message is left in
1666** pParse->zErrMsg.
1667**
1668** This routine does NOT free the Select structure passed in. The
1669** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001670**
1671** The pParent, parentTab, and *pParentAgg fields are filled in if this
1672** SELECT is a subquery. This routine may try to combine this SELECT
1673** with its parent to form a single flat query. In so doing, it might
1674** change the parent query from a non-aggregate to an aggregate query.
1675** For that reason, the pParentAgg flag is passed as a pointer, so it
1676** can be changed.
drh9bb61fe2000-06-05 16:01:39 +00001677*/
1678int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001679 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001680 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +00001681 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drh832508b2002-03-02 17:04:07 +00001682 int iParm, /* Save result in this memory location, if >=0 */
1683 Select *pParent, /* Another SELECT for which this is a sub-query */
1684 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001685 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001686){
drhd8bc7082000-06-07 23:51:50 +00001687 int i;
drhcce7d172000-05-31 15:34:51 +00001688 WhereInfo *pWInfo;
1689 Vdbe *v;
1690 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001691 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001692 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001693 Expr *pWhere; /* The WHERE clause. May be NULL */
1694 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001695 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1696 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001697 int isDistinct; /* True if the DISTINCT keyword is present */
1698 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +00001699 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +00001700 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001701
drhdaffd0e2001-04-11 14:28:42 +00001702 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1703
drh82c3d632000-06-06 21:56:07 +00001704 /* If there is are a sequence of queries, do the earlier ones first.
1705 */
1706 if( p->pPrior ){
1707 return multiSelect(pParse, p, eDest, iParm);
1708 }
1709
1710 /* Make local copies of the parameters for this query.
1711 */
drh9bb61fe2000-06-05 16:01:39 +00001712 pTabList = p->pSrc;
1713 pWhere = p->pWhere;
1714 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00001715 pGroupBy = p->pGroupBy;
1716 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00001717 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00001718
drh832508b2002-03-02 17:04:07 +00001719 /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1720 ** The WHERE processing requires that the cursors for the tables in the
1721 ** FROM clause be consecutive.
drh10e5e3c2000-06-08 00:19:02 +00001722 */
drh832508b2002-03-02 17:04:07 +00001723 base = p->base = pParse->nTab;
drhad3cab52002-05-24 02:04:32 +00001724 pParse->nTab += pTabList->nSrc;
drh10e5e3c2000-06-08 00:19:02 +00001725
drh9bb61fe2000-06-05 16:01:39 +00001726 /*
1727 ** Do not even attempt to generate any code if we have already seen
1728 ** errors before this routine starts.
1729 */
drh1d83f052002-02-17 00:30:36 +00001730 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001731
drhd8bc7082000-06-07 23:51:50 +00001732 /* Look up every table in the table list and create an appropriate
1733 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +00001734 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001735 */
drhd8bc7082000-06-07 23:51:50 +00001736 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001737 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001738 }
drhad2d8302002-05-24 20:31:36 +00001739 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00001740 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001741 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001742
drh22827922000-06-06 17:27:05 +00001743 /* If writing to memory or generating a set
1744 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001745 */
drhfef52082000-06-06 01:50:43 +00001746 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001747 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1748 "a SELECT that is part of an expression", 0);
1749 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001750 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001751 }
1752
drhc926afb2002-06-20 03:38:26 +00001753 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00001754 */
drhc926afb2002-06-20 03:38:26 +00001755 switch( eDest ){
1756 case SRT_Union:
1757 case SRT_Except:
1758 case SRT_Discard:
1759 pOrderBy = 0;
1760 break;
1761 default:
1762 break;
drh22827922000-06-06 17:27:05 +00001763 }
1764
drh10e5e3c2000-06-08 00:19:02 +00001765 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00001766 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00001767 **
drh967e8b72000-06-21 13:59:10 +00001768 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001769 */
drh4794b982000-06-06 13:54:14 +00001770 for(i=0; i<pEList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001771 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001772 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001773 }
drh22827922000-06-06 17:27:05 +00001774 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001775 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001776 }
1777 }
drhcce7d172000-05-31 15:34:51 +00001778 if( pWhere ){
drh832508b2002-03-02 17:04:07 +00001779 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001780 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001781 }
1782 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001783 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001784 }
1785 }
1786 if( pOrderBy ){
1787 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001788 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001789 if( sqliteExprIsConstant(pE) ){
drhe4de1fe2002-06-02 16:09:01 +00001790 int iCol;
1791 if( sqliteExprIsInteger(pE, &iCol)==0 ){
1792 sqliteSetString(&pParse->zErrMsg,
1793 "ORDER BY terms must not be non-integer constants", 0);
1794 pParse->nErr++;
1795 goto select_end;
1796 }else if( iCol<=0 || iCol>pEList->nExpr ){
1797 char zBuf[2000];
1798 sprintf(zBuf,"ORDER BY column number %d out of range - should be "
1799 "between 1 and %d", iCol, pEList->nExpr);
1800 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1801 pParse->nErr++;
1802 goto select_end;
1803 }
1804 sqliteExprDelete(pE);
1805 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
drh92086432002-01-22 14:11:29 +00001806 }
drh832508b2002-03-02 17:04:07 +00001807 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001808 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001809 }
drh22827922000-06-06 17:27:05 +00001810 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001811 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001812 }
1813 }
1814 }
drh22827922000-06-06 17:27:05 +00001815 if( pGroupBy ){
1816 for(i=0; i<pGroupBy->nExpr; i++){
1817 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001818 if( sqliteExprIsConstant(pE) ){
1819 sqliteSetString(&pParse->zErrMsg,
1820 "GROUP BY expressions should not be constant", 0);
1821 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001822 goto select_end;
drh92086432002-01-22 14:11:29 +00001823 }
drh832508b2002-03-02 17:04:07 +00001824 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001825 goto select_end;
drh22827922000-06-06 17:27:05 +00001826 }
1827 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001828 goto select_end;
drh22827922000-06-06 17:27:05 +00001829 }
1830 }
1831 }
1832 if( pHaving ){
1833 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001834 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1835 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001836 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001837 goto select_end;
drh22827922000-06-06 17:27:05 +00001838 }
drh832508b2002-03-02 17:04:07 +00001839 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001840 goto select_end;
drh22827922000-06-06 17:27:05 +00001841 }
drhda932812000-06-06 18:00:15 +00001842 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001843 goto select_end;
drh22827922000-06-06 17:27:05 +00001844 }
drhcce7d172000-05-31 15:34:51 +00001845 }
1846
drh9562b552002-02-19 15:00:07 +00001847 /* Check for the special case of a min() or max() function by itself
1848 ** in the result set.
1849 */
1850 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001851 rc = 0;
drh9562b552002-02-19 15:00:07 +00001852 goto select_end;
1853 }
1854
drhd820cb12002-02-18 03:21:45 +00001855 /* Begin generating code.
1856 */
1857 v = sqliteGetVdbe(pParse);
1858 if( v==0 ) goto select_end;
1859
drh0bb28102002-05-08 11:54:14 +00001860 /* Identify column names if we will be using in the callback. This
1861 ** step is skipped if the output is going to a table or a memory cell.
1862 */
1863 if( eDest==SRT_Callback ){
1864 generateColumnNames(pParse, p->base, pTabList, pEList);
1865 }
1866
1867 /* Set the limiter
1868 */
1869 if( p->nLimit<=0 ){
drhd11d3822002-06-21 23:01:49 +00001870 p->nLimit = -1;
drh0bb28102002-05-08 11:54:14 +00001871 p->nOffset = 0;
1872 }else{
drhd11d3822002-06-21 23:01:49 +00001873 int iMem = pParse->nMem++;
1874 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
drhbf5cd972002-06-24 12:20:23 +00001875 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001876 p->nLimit = iMem;
1877 if( p->nOffset<=0 ){
1878 p->nOffset = 0;
1879 }else{
1880 iMem = pParse->nMem++;
1881 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
drhbf5cd972002-06-24 12:20:23 +00001882 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001883 p->nOffset = iMem;
1884 }
drh0bb28102002-05-08 11:54:14 +00001885 }
1886
drhd820cb12002-02-18 03:21:45 +00001887 /* Generate code for all sub-queries in the FROM clause
1888 */
drhad3cab52002-05-24 02:04:32 +00001889 for(i=0; i<pTabList->nSrc; i++){
drha76b5df2002-02-23 02:32:10 +00001890 if( pTabList->a[i].pSelect==0 ) continue;
drh2d0794e2002-03-03 03:03:52 +00001891 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
drh1b2e0322002-03-03 02:49:51 +00001892 p, i, &isAgg);
1893 pTabList = p->pSrc;
1894 pWhere = p->pWhere;
drhacd4c692002-03-07 02:02:51 +00001895 if( eDest==SRT_Callback ){
1896 pOrderBy = p->pOrderBy;
1897 }
drh1b2e0322002-03-03 02:49:51 +00001898 pGroupBy = p->pGroupBy;
1899 pHaving = p->pHaving;
1900 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00001901 }
1902
drh832508b2002-03-02 17:04:07 +00001903 /* Check to see if this is a subquery that can be "flattened" into its parent.
1904 ** If flattening is a possiblity, do so and return immediately.
1905 */
drh1b2e0322002-03-03 02:49:51 +00001906 if( pParent && pParentAgg &&
drh8c74a8c2002-08-25 19:20:40 +00001907 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
drh1b2e0322002-03-03 02:49:51 +00001908 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00001909 return rc;
1910 }
drh832508b2002-03-02 17:04:07 +00001911
drh2d0794e2002-03-03 03:03:52 +00001912 /* If the output is destined for a temporary table, open that table.
1913 */
1914 if( eDest==SRT_TempTable ){
1915 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1916 }
1917
drh22827922000-06-06 17:27:05 +00001918 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001919 */
drhd820cb12002-02-18 03:21:45 +00001920 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001921 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001922 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001923 for(i=0; i<pEList->nExpr; i++){
1924 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001925 goto select_end;
drh22827922000-06-06 17:27:05 +00001926 }
1927 }
1928 if( pGroupBy ){
1929 for(i=0; i<pGroupBy->nExpr; i++){
1930 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001931 goto select_end;
drh22827922000-06-06 17:27:05 +00001932 }
1933 }
1934 }
1935 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001936 goto select_end;
drh22827922000-06-06 17:27:05 +00001937 }
drh191b6902000-06-08 11:13:01 +00001938 if( pOrderBy ){
1939 for(i=0; i<pOrderBy->nExpr; i++){
1940 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001941 goto select_end;
drh191b6902000-06-08 11:13:01 +00001942 }
1943 }
1944 }
drhefb72512000-05-31 20:00:52 +00001945 }
1946
drh22827922000-06-06 17:27:05 +00001947 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001948 */
1949 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001950 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001951 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001952 FuncDef *pFunc;
1953 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001954 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001955 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001956 }
1957 }
drh1bee3d72001-10-15 00:44:35 +00001958 if( pGroupBy==0 ){
1959 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001960 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001961 }
drhcce7d172000-05-31 15:34:51 +00001962 }
1963
drh19a775c2000-06-05 18:54:46 +00001964 /* Initialize the memory cell to NULL
1965 */
drhfef52082000-06-06 01:50:43 +00001966 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001967 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001968 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001969 }
1970
drh832508b2002-03-02 17:04:07 +00001971 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00001972 */
drh19a775c2000-06-05 18:54:46 +00001973 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00001974 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00001975 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00001976 }else{
1977 distinct = -1;
drhefb72512000-05-31 20:00:52 +00001978 }
drh832508b2002-03-02 17:04:07 +00001979
1980 /* Begin the database scan
1981 */
drh68d2e592002-08-04 00:52:38 +00001982 pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0,
1983 pGroupBy ? 0 : &pOrderBy);
drh1d83f052002-02-17 00:30:36 +00001984 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001985
drh22827922000-06-06 17:27:05 +00001986 /* Use the standard inner loop if we are not dealing with
1987 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001988 */
drhda9d6c42000-05-31 18:20:14 +00001989 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00001990 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
1991 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001992 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001993 }
drhcce7d172000-05-31 15:34:51 +00001994 }
drhefb72512000-05-31 20:00:52 +00001995
drhe3184742002-06-19 14:27:05 +00001996 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00001997 ** processing.
drhefb72512000-05-31 20:00:52 +00001998 */
drh22827922000-06-06 17:27:05 +00001999 else{
drh22827922000-06-06 17:27:05 +00002000 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00002001 int lbl1;
drh22827922000-06-06 17:27:05 +00002002 for(i=0; i<pGroupBy->nExpr; i++){
2003 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
2004 }
drh99fcd712001-10-13 01:06:47 +00002005 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh491791a2002-07-18 00:34:09 +00002006 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
drh1bee3d72001-10-15 00:44:35 +00002007 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002008 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00002009 for(i=0; i<pParse->nAgg; i++){
2010 if( pParse->aAgg[i].isAgg ) continue;
2011 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00002012 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00002013 }
drh22827922000-06-06 17:27:05 +00002014 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00002015 }
drh22827922000-06-06 17:27:05 +00002016 for(i=0; i<pParse->nAgg; i++){
2017 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00002018 int j;
drh22827922000-06-06 17:27:05 +00002019 if( !pParse->aAgg[i].isAgg ) continue;
2020 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00002021 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00002022 if( pE->pList ){
2023 for(j=0; j<pE->pList->nExpr; j++){
2024 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
2025 }
drhe5095352002-02-24 03:25:14 +00002026 }
drh0bce8352002-02-28 00:41:10 +00002027 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00002028 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00002029 assert( pParse->aAgg[i].pFunc!=0 );
2030 assert( pParse->aAgg[i].pFunc->xStep!=0 );
2031 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00002032 }
drhcce7d172000-05-31 15:34:51 +00002033 }
2034
2035 /* End the database scan loop.
2036 */
2037 sqliteWhereEnd(pWInfo);
2038
drh22827922000-06-06 17:27:05 +00002039 /* If we are processing aggregates, we need to set up a second loop
2040 ** over all of the aggregate values and process them.
2041 */
2042 if( isAgg ){
2043 int endagg = sqliteVdbeMakeLabel(v);
2044 int startagg;
drh99fcd712001-10-13 01:06:47 +00002045 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002046 pParse->useAgg = 1;
2047 if( pHaving ){
drhf5905aa2002-05-26 20:54:33 +00002048 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002049 }
drhdf199a22002-06-14 22:38:41 +00002050 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2051 iParm, startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00002052 goto select_end;
drh22827922000-06-06 17:27:05 +00002053 }
drh99fcd712001-10-13 01:06:47 +00002054 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
2055 sqliteVdbeResolveLabel(v, endagg);
2056 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002057 pParse->useAgg = 0;
2058 }
2059
drhcce7d172000-05-31 15:34:51 +00002060 /* If there is an ORDER BY clause, then we need to sort the results
2061 ** and send them to the callback one by one.
2062 */
2063 if( pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00002064 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002065 }
drh6a535342001-10-19 16:44:56 +00002066
2067
2068 /* Issue a null callback if that is what the user wants.
2069 */
2070 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
2071 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
2072 }
2073
drh1d83f052002-02-17 00:30:36 +00002074 /* The SELECT was successfully coded. Set the return code to 0
2075 ** to indicate no errors.
2076 */
2077 rc = 0;
2078
2079 /* Control jumps to here if an error is encountered above, or upon
2080 ** successful coding of the SELECT.
2081 */
2082select_end:
drh832508b2002-03-02 17:04:07 +00002083 pParse->nTab = base;
drh1d83f052002-02-17 00:30:36 +00002084 sqliteAggregateInfoReset(pParse);
2085 return rc;
drhcce7d172000-05-31 15:34:51 +00002086}