blob: 89511db34d905d5b0d55124d86d8d941ae380ab9 [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**
drh491791a2002-07-18 00:34:09 +000015** $Id: select.c,v 1.106 2002/07/18 00:34:12 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drhcce7d172000-05-31 15:34:51 +000019/*
drh9bb61fe2000-06-05 16:01:39 +000020** Allocate a new Select structure and return a pointer to that
21** structure.
drhcce7d172000-05-31 15:34:51 +000022*/
drh9bb61fe2000-06-05 16:01:39 +000023Select *sqliteSelectNew(
drhdaffd0e2001-04-11 14:28:42 +000024 ExprList *pEList, /* which columns to include in the result */
drhad3cab52002-05-24 02:04:32 +000025 SrcList *pSrc, /* the FROM clause -- which tables to scan */
drhdaffd0e2001-04-11 14:28:42 +000026 Expr *pWhere, /* the WHERE clause */
27 ExprList *pGroupBy, /* the GROUP BY clause */
28 Expr *pHaving, /* the HAVING clause */
29 ExprList *pOrderBy, /* the ORDER BY clause */
drh9bbca4c2001-11-06 04:00:18 +000030 int isDistinct, /* true if the DISTINCT keyword is present */
31 int nLimit, /* LIMIT value. -1 means not used */
32 int nOffset /* OFFSET value. -1 means not used */
drh9bb61fe2000-06-05 16:01:39 +000033){
34 Select *pNew;
35 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000036 if( pNew==0 ){
37 sqliteExprListDelete(pEList);
drhad3cab52002-05-24 02:04:32 +000038 sqliteSrcListDelete(pSrc);
drhdaffd0e2001-04-11 14:28:42 +000039 sqliteExprDelete(pWhere);
40 sqliteExprListDelete(pGroupBy);
41 sqliteExprDelete(pHaving);
42 sqliteExprListDelete(pOrderBy);
43 }else{
44 pNew->pEList = pEList;
45 pNew->pSrc = pSrc;
46 pNew->pWhere = pWhere;
47 pNew->pGroupBy = pGroupBy;
48 pNew->pHaving = pHaving;
49 pNew->pOrderBy = pOrderBy;
50 pNew->isDistinct = isDistinct;
51 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000052 pNew->nLimit = nLimit;
53 pNew->nOffset = nOffset;
drhdaffd0e2001-04-11 14:28:42 +000054 }
drh9bb61fe2000-06-05 16:01:39 +000055 return pNew;
56}
57
58/*
drh01f3f252002-05-24 16:14:15 +000059** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
60** type of join. Return an integer constant that expresses that type
61** in terms of the following bit values:
62**
63** JT_INNER
64** JT_OUTER
65** JT_NATURAL
66** JT_LEFT
67** JT_RIGHT
68**
69** A full outer join is the combination of JT_LEFT and JT_RIGHT.
70**
71** If an illegal or unsupported join type is seen, then still return
72** a join type, but put an error in the pParse structure.
73*/
74int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
75 int jointype = 0;
76 Token *apAll[3];
77 Token *p;
78 static struct {
79 const char *zKeyword;
80 int nChar;
81 int code;
82 } keywords[] = {
83 { "natural", 7, JT_NATURAL },
drh195e6962002-05-25 00:18:20 +000084 { "left", 4, JT_LEFT|JT_OUTER },
85 { "right", 5, JT_RIGHT|JT_OUTER },
86 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
drh01f3f252002-05-24 16:14:15 +000087 { "outer", 5, JT_OUTER },
88 { "inner", 5, JT_INNER },
89 { "cross", 5, JT_INNER },
90 };
91 int i, j;
92 apAll[0] = pA;
93 apAll[1] = pB;
94 apAll[2] = pC;
drh195e6962002-05-25 00:18:20 +000095 for(i=0; i<3 && apAll[i]; i++){
drh01f3f252002-05-24 16:14:15 +000096 p = apAll[i];
97 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
98 if( p->n==keywords[j].nChar
99 && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
100 jointype |= keywords[j].code;
101 break;
102 }
103 }
104 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
105 jointype |= JT_ERROR;
106 break;
107 }
108 }
drhad2d8302002-05-24 20:31:36 +0000109 if(
110 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
drh195e6962002-05-25 00:18:20 +0000111 (jointype & JT_ERROR)!=0
drhad2d8302002-05-24 20:31:36 +0000112 ){
drh01f3f252002-05-24 16:14:15 +0000113 static Token dummy = { 0, 0 };
114 char *zSp1 = " ", *zSp2 = " ";
115 if( pB==0 ){ pB = &dummy; zSp1 = 0; }
116 if( pC==0 ){ pC = &dummy; zSp2 = 0; }
117 sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
118 pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
119 pParse->nErr++;
120 jointype = JT_INNER;
drh195e6962002-05-25 00:18:20 +0000121 }else if( jointype & JT_RIGHT ){
122 sqliteSetString(&pParse->zErrMsg,
123 "RIGHT and FULL OUTER JOINs are not currently supported", 0);
124 pParse->nErr++;
125 jointype = JT_INNER;
drh01f3f252002-05-24 16:14:15 +0000126 }
127 return jointype;
128}
129
130/*
drhad2d8302002-05-24 20:31:36 +0000131** Return the index of a column in a table. Return -1 if the column
132** is not contained in the table.
133*/
134static int columnIndex(Table *pTab, const char *zCol){
135 int i;
136 for(i=0; i<pTab->nCol; i++){
137 if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
138 }
139 return -1;
140}
141
142/*
143** Add a term to the WHERE expression in *ppExpr that requires the
144** zCol column to be equal in the two tables pTab1 and pTab2.
145*/
146static void addWhereTerm(
147 const char *zCol, /* Name of the column */
148 const Table *pTab1, /* First table */
149 const Table *pTab2, /* Second table */
150 Expr **ppExpr /* Add the equality term to this expression */
151){
152 Token dummy;
153 Expr *pE1a, *pE1b, *pE1c;
154 Expr *pE2a, *pE2b, *pE2c;
155 Expr *pE;
156
157 dummy.z = zCol;
158 dummy.n = strlen(zCol);
159 pE1a = sqliteExpr(TK_ID, 0, 0, &dummy);
drh3b167c72002-06-28 12:18:47 +0000160 pE1a->staticToken = 1;
drhad2d8302002-05-24 20:31:36 +0000161 pE2a = sqliteExpr(TK_ID, 0, 0, &dummy);
drh3b167c72002-06-28 12:18:47 +0000162 pE2a->staticToken = 1;
drhad2d8302002-05-24 20:31:36 +0000163 dummy.z = pTab1->zName;
164 dummy.n = strlen(dummy.z);
165 pE1b = sqliteExpr(TK_ID, 0, 0, &dummy);
drh3b167c72002-06-28 12:18:47 +0000166 pE1b->staticToken = 1;
drhad2d8302002-05-24 20:31:36 +0000167 dummy.z = pTab2->zName;
168 dummy.n = strlen(dummy.z);
169 pE2b = sqliteExpr(TK_ID, 0, 0, &dummy);
drh3b167c72002-06-28 12:18:47 +0000170 pE2b->staticToken = 1;
drhad2d8302002-05-24 20:31:36 +0000171 pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0);
172 pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0);
173 pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0);
drh1cc093c2002-06-24 22:01:57 +0000174 pE->isJoinExpr = 1;
drhad2d8302002-05-24 20:31:36 +0000175 if( *ppExpr ){
176 *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0);
177 }else{
178 *ppExpr = pE;
179 }
180}
181
182/*
drh1cc093c2002-06-24 22:01:57 +0000183** Set the Expr.isJoinExpr flag on all terms of the given expression.
184**
185** The Expr.isJoinExpr flag is used at on terms of an expression to tell
186** the LEFT OUTER JOIN processing logic that this term is part of the
187** join restriction and not a part of the more general WHERE clause.
188*/
189static void setJoinExpr(Expr *p){
190 while( p ){
191 p->isJoinExpr = 1;
192 setJoinExpr(p->pLeft);
193 p = p->pRight;
194 }
195}
196
197/*
drhad2d8302002-05-24 20:31:36 +0000198** This routine processes the join information for a SELECT statement.
199** ON and USING clauses are converted into extra terms of the WHERE clause.
200** NATURAL joins also create extra WHERE clause terms.
201**
202** This routine returns the number of errors encountered.
203*/
204static int sqliteProcessJoin(Parse *pParse, Select *p){
205 SrcList *pSrc;
206 int i, j;
207 pSrc = p->pSrc;
208 for(i=0; i<pSrc->nSrc-1; i++){
209 struct SrcList_item *pTerm = &pSrc->a[i];
210 struct SrcList_item *pOther = &pSrc->a[i+1];
211
212 if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
213
214 /* When the NATURAL keyword is present, add WHERE clause terms for
215 ** every column that the two tables have in common.
216 */
217 if( pTerm->jointype & JT_NATURAL ){
218 Table *pTab;
219 if( pTerm->pOn || pTerm->pUsing ){
220 sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have "
221 "an ON or USING clause", 0);
222 pParse->nErr++;
223 return 1;
224 }
225 pTab = pTerm->pTab;
226 for(j=0; j<pTab->nCol; j++){
227 if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
228 addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
229 }
230 }
231 }
232
233 /* Disallow both ON and USING clauses in the same join
234 */
235 if( pTerm->pOn && pTerm->pUsing ){
236 sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING "
237 "clauses in the same join", 0);
238 pParse->nErr++;
239 return 1;
240 }
241
242 /* Add the ON clause to the end of the WHERE clause, connected by
243 ** and AND operator.
244 */
245 if( pTerm->pOn ){
drh1cc093c2002-06-24 22:01:57 +0000246 setJoinExpr(pTerm->pOn);
drhad2d8302002-05-24 20:31:36 +0000247 if( p->pWhere==0 ){
248 p->pWhere = pTerm->pOn;
249 }else{
250 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
251 }
252 pTerm->pOn = 0;
253 }
254
255 /* Create extra terms on the WHERE clause for each column named
256 ** in the USING clause. Example: If the two tables to be joined are
257 ** A and B and the USING clause names X, Y, and Z, then add this
258 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
259 ** Report an error if any column mentioned in the USING clause is
260 ** not contained in both tables to be joined.
261 */
262 if( pTerm->pUsing ){
263 IdList *pList;
264 int j;
265 assert( i<pSrc->nSrc-1 );
266 pList = pTerm->pUsing;
267 for(j=0; j<pList->nId; j++){
drhbf5cd972002-06-24 12:20:23 +0000268 if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 ||
269 columnIndex(pOther->pTab, pList->a[j].zName)<0 ){
drhad2d8302002-05-24 20:31:36 +0000270 sqliteSetString(&pParse->zErrMsg, "cannot join using column ",
drhbf5cd972002-06-24 12:20:23 +0000271 pList->a[j].zName, " - column not present in both tables", 0);
drhad2d8302002-05-24 20:31:36 +0000272 pParse->nErr++;
273 return 1;
274 }
drhbf5cd972002-06-24 12:20:23 +0000275 addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
drhad2d8302002-05-24 20:31:36 +0000276 }
277 }
278 }
279 return 0;
280}
281
282/*
drh9bb61fe2000-06-05 16:01:39 +0000283** Delete the given Select structure and all of its substructures.
284*/
285void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +0000286 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +0000287 sqliteExprListDelete(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000288 sqliteSrcListDelete(p->pSrc);
drh9bb61fe2000-06-05 16:01:39 +0000289 sqliteExprDelete(p->pWhere);
290 sqliteExprListDelete(p->pGroupBy);
291 sqliteExprDelete(p->pHaving);
292 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +0000293 sqliteSelectDelete(p->pPrior);
drha76b5df2002-02-23 02:32:10 +0000294 sqliteFree(p->zSelect);
drh9bb61fe2000-06-05 16:01:39 +0000295 sqliteFree(p);
296}
297
298/*
drh22827922000-06-06 17:27:05 +0000299** Delete the aggregate information from the parse structure.
300*/
drh1d83f052002-02-17 00:30:36 +0000301static void sqliteAggregateInfoReset(Parse *pParse){
drh22827922000-06-06 17:27:05 +0000302 sqliteFree(pParse->aAgg);
303 pParse->aAgg = 0;
304 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000305 pParse->useAgg = 0;
306}
307
308/*
drhc926afb2002-06-20 03:38:26 +0000309** Insert code into "v" that will push the record on the top of the
310** stack into the sorter.
311*/
312static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
313 char *zSortOrder;
314 int i;
315 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
316 if( zSortOrder==0 ) return;
317 for(i=0; i<pOrderBy->nExpr; i++){
drh38640e12002-07-05 21:42:36 +0000318 int order = pOrderBy->a[i].sortOrder;
319 int type;
320 int c;
321 if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
322 type = SQLITE_SO_TEXT;
323 }else if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_NUM ){
324 type = SQLITE_SO_NUM;
drh491791a2002-07-18 00:34:09 +0000325 }else if( pParse->db->file_format>=4 ){
drh38640e12002-07-05 21:42:36 +0000326 type = sqliteExprType(pOrderBy->a[i].pExpr);
327 }else{
328 type = SQLITE_SO_NUM;
329 }
330 if( (order & SQLITE_SO_DIRMASK)==SQLITE_SO_ASC ){
331 c = type==SQLITE_SO_TEXT ? 'A' : '+';
332 }else{
333 c = type==SQLITE_SO_TEXT ? 'D' : '-';
334 }
335 zSortOrder[i] = c;
drhc926afb2002-06-20 03:38:26 +0000336 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
337 }
338 zSortOrder[pOrderBy->nExpr] = 0;
339 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
340 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
341 sqliteFree(zSortOrder);
342 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
343}
344
345/*
drh38640e12002-07-05 21:42:36 +0000346** This routine adds a P3 argument to the last VDBE opcode that was
347** inserted. The P3 argument added is a string suitable for the
348** OP_MakeKey or OP_MakeIdxKey opcodes. The string consists of
349** characters 't' or 'n' depending on whether or not the various
350** fields of the key to be generated should be treated as numeric
351** or as text. See the OP_MakeKey and OP_MakeIdxKey opcode
352** documentation for additional information about the P3 string.
353** See also the sqliteAddIdxKeyType() routine.
354*/
355void sqliteAddKeyType(Vdbe *v, ExprList *pEList){
356 int nColumn = pEList->nExpr;
357 char *zType = sqliteMalloc( nColumn+1 );
358 int i;
359 if( zType==0 ) return;
360 for(i=0; i<nColumn; i++){
361 zType[i] = sqliteExprType(pEList->a[i].pExpr)==SQLITE_SO_NUM ? 'n' : 't';
362 }
363 zType[i] = 0;
364 sqliteVdbeChangeP3(v, -1, zType, nColumn);
365 sqliteFree(zType);
366}
367
368/*
drh22827922000-06-06 17:27:05 +0000369** This routine generates the code for the inside of the inner loop
370** of a SELECT.
drh82c3d632000-06-06 21:56:07 +0000371**
drh38640e12002-07-05 21:42:36 +0000372** If srcTab and nColumn are both zero, then the pEList expressions
373** are evaluated in order to get the data for this row. If nColumn>0
374** then data is pulled from srcTab and pEList is used only to get the
375** datatypes for each column.
drh22827922000-06-06 17:27:05 +0000376*/
377static int selectInnerLoop(
378 Parse *pParse, /* The parser context */
drhdf199a22002-06-14 22:38:41 +0000379 Select *p, /* The complete select statement being coded */
drh22827922000-06-06 17:27:05 +0000380 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +0000381 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +0000382 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +0000383 ExprList *pOrderBy, /* If not NULL, sort results using this key */
384 int distinct, /* If >=0, make sure results are distinct */
385 int eDest, /* How to dispose of the results */
386 int iParm, /* An argument to the disposal method */
387 int iContinue, /* Jump here to continue with next row */
388 int iBreak /* Jump here to break out of the inner loop */
389){
390 Vdbe *v = pParse->pVdbe;
391 int i;
drh38640e12002-07-05 21:42:36 +0000392
drhdaffd0e2001-04-11 14:28:42 +0000393 if( v==0 ) return 0;
drh38640e12002-07-05 21:42:36 +0000394 assert( pEList!=0 );
drh22827922000-06-06 17:27:05 +0000395
drhdf199a22002-06-14 22:38:41 +0000396 /* If there was a LIMIT clause on the SELECT statement, then do the check
397 ** to see if this row should be output.
398 */
399 if( pOrderBy==0 ){
400 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000401 int addr = sqliteVdbeCurrentAddr(v);
402 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+2);
403 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drhdf199a22002-06-14 22:38:41 +0000404 }
drhd11d3822002-06-21 23:01:49 +0000405 if( p->nLimit>=0 ){
406 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, iBreak);
drhdf199a22002-06-14 22:38:41 +0000407 }
408 }
409
drh967e8b72000-06-21 13:59:10 +0000410 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000411 */
drh38640e12002-07-05 21:42:36 +0000412 if( nColumn>0 ){
drh967e8b72000-06-21 13:59:10 +0000413 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000414 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000415 }
drh38640e12002-07-05 21:42:36 +0000416 }else{
417 nColumn = pEList->nExpr;
418 for(i=0; i<pEList->nExpr; i++){
419 sqliteExprCode(pParse, pEList->a[i].pExpr);
420 }
drh22827922000-06-06 17:27:05 +0000421 }
422
drhdaffd0e2001-04-11 14:28:42 +0000423 /* If the DISTINCT keyword was present on the SELECT statement
424 ** and this row has been seen before, then do not make this row
425 ** part of the result.
drh22827922000-06-06 17:27:05 +0000426 */
drhf5905aa2002-05-26 20:54:33 +0000427 if( distinct>=0 && pEList && pEList->nExpr>0 ){
drh0bd1f4e2002-06-06 18:54:39 +0000428#if NULL_ALWAYS_DISTINCT
429 sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7);
430#endif
drh99fcd712001-10-13 01:06:47 +0000431 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
drh491791a2002-07-18 00:34:09 +0000432 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pEList);
drhf5905aa2002-05-26 20:54:33 +0000433 sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3);
drh99fcd712001-10-13 01:06:47 +0000434 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
435 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drh99fcd712001-10-13 01:06:47 +0000436 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000437 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
drh22827922000-06-06 17:27:05 +0000438 }
drh82c3d632000-06-06 21:56:07 +0000439
drhc926afb2002-06-20 03:38:26 +0000440 switch( eDest ){
441 /* In this mode, write each query result to the key of the temporary
442 ** table iParm.
443 */
444 case SRT_Union: {
445 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
446 sqliteVdbeAddOp(v, OP_String, 0, 0);
447 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
448 break;
drh22827922000-06-06 17:27:05 +0000449 }
drh22827922000-06-06 17:27:05 +0000450
drhc926afb2002-06-20 03:38:26 +0000451 /* Store the result as data using a unique key.
452 */
453 case SRT_Table:
454 case SRT_TempTable: {
455 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
456 if( pOrderBy ){
457 pushOntoSorter(pParse, v, pOrderBy);
458 }else{
459 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
460 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
461 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
462 }
463 break;
464 }
drh82c3d632000-06-06 21:56:07 +0000465
drhc926afb2002-06-20 03:38:26 +0000466 /* Construct a record from the query result, but instead of
467 ** saving that record, use it as a key to delete elements from
468 ** the temporary table iParm.
469 */
470 case SRT_Except: {
471 int addr;
472 addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
473 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
474 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
475 break;
476 }
drh5974a302000-06-07 14:42:26 +0000477
drhc926afb2002-06-20 03:38:26 +0000478 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
479 ** then there should be a single item on the stack. Write this
480 ** item into the set table with bogus data.
481 */
482 case SRT_Set: {
drha9f9d1c2002-06-29 02:20:08 +0000483 int lbl = sqliteVdbeMakeLabel(v);
drhc926afb2002-06-20 03:38:26 +0000484 assert( nColumn==1 );
drha9f9d1c2002-06-29 02:20:08 +0000485 sqliteVdbeAddOp(v, OP_IsNull, -1, lbl);
drhc926afb2002-06-20 03:38:26 +0000486 if( pOrderBy ){
487 pushOntoSorter(pParse, v, pOrderBy);
488 }else{
drha9f9d1c2002-06-29 02:20:08 +0000489 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000490 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
491 }
drha9f9d1c2002-06-29 02:20:08 +0000492 sqliteVdbeResolveLabel(v, lbl);
drhc926afb2002-06-20 03:38:26 +0000493 break;
494 }
drh22827922000-06-06 17:27:05 +0000495
drhc926afb2002-06-20 03:38:26 +0000496 /* If this is a scalar select that is part of an expression, then
497 ** store the results in the appropriate memory cell and break out
498 ** of the scan loop.
499 */
500 case SRT_Mem: {
501 assert( nColumn==1 );
502 if( pOrderBy ){
503 pushOntoSorter(pParse, v, pOrderBy);
504 }else{
505 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
506 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
507 }
508 break;
509 }
drh22827922000-06-06 17:27:05 +0000510
drhf46f9052002-06-22 02:33:38 +0000511 /* Send the data to the callback function.
512 */
513 case SRT_Callback:
514 case SRT_Sorter: {
515 if( pOrderBy ){
516 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
517 pushOntoSorter(pParse, v, pOrderBy);
518 }else{
519 assert( eDest==SRT_Callback );
520 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0);
521 }
522 break;
523 }
524
drhc926afb2002-06-20 03:38:26 +0000525 /* Discard the results. This is used for SELECT statements inside
526 ** the body of a TRIGGER. The purpose of such selects is to call
527 ** user-defined functions that have side effects. We do not care
528 ** about the actual results of the select.
529 */
drhc926afb2002-06-20 03:38:26 +0000530 default: {
drhf46f9052002-06-22 02:33:38 +0000531 assert( eDest==SRT_Discard );
532 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000533 break;
534 }
drh82c3d632000-06-06 21:56:07 +0000535 }
536 return 0;
537}
538
539/*
drhd8bc7082000-06-07 23:51:50 +0000540** If the inner loop was generated using a non-null pOrderBy argument,
541** then the results were placed in a sorter. After the loop is terminated
542** we need to run the sorter and output the results. The following
543** routine generates the code needed to do that.
544*/
drhc926afb2002-06-20 03:38:26 +0000545static void generateSortTail(
546 Select *p, /* The SELECT statement */
547 Vdbe *v, /* Generate code into this VDBE */
548 int nColumn, /* Number of columns of data */
549 int eDest, /* Write the sorted results here */
550 int iParm /* Optional parameter associated with eDest */
551){
drhd8bc7082000-06-07 23:51:50 +0000552 int end = sqliteVdbeMakeLabel(v);
553 int addr;
drhf46f9052002-06-22 02:33:38 +0000554 if( eDest==SRT_Sorter ) return;
drh99fcd712001-10-13 01:06:47 +0000555 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
556 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drhdf199a22002-06-14 22:38:41 +0000557 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000558 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+4);
559 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
560 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drhdf199a22002-06-14 22:38:41 +0000561 }
drhd11d3822002-06-21 23:01:49 +0000562 if( p->nLimit>=0 ){
563 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, end);
drhdf199a22002-06-14 22:38:41 +0000564 }
drhc926afb2002-06-20 03:38:26 +0000565 switch( eDest ){
566 case SRT_Callback: {
567 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
568 break;
569 }
570 case SRT_Table:
571 case SRT_TempTable: {
572 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
573 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
574 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
575 break;
576 }
577 case SRT_Set: {
578 assert( nColumn==1 );
579 sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3);
580 sqliteVdbeAddOp(v, OP_String, 0, 0);
581 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
582 break;
583 }
584 case SRT_Mem: {
585 assert( nColumn==1 );
586 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
587 sqliteVdbeAddOp(v, OP_Goto, 0, end);
588 break;
589 }
590 default: {
drhf46f9052002-06-22 02:33:38 +0000591 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000592 break;
593 }
594 }
drh99fcd712001-10-13 01:06:47 +0000595 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
596 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000597 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000598}
599
600/*
drh82c3d632000-06-06 21:56:07 +0000601** Generate code that will tell the VDBE how many columns there
602** are in the result and the name for each column. This information
603** is used to provide "argc" and "azCol[]" values in the callback.
604*/
drh832508b2002-03-02 17:04:07 +0000605static void generateColumnNames(
606 Parse *pParse, /* Parser context */
607 int base, /* VDBE cursor corresponding to first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000608 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000609 ExprList *pEList /* Expressions defining the result set */
610){
drhd8bc7082000-06-07 23:51:50 +0000611 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000612 int i;
drhdaffd0e2001-04-11 14:28:42 +0000613 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000614 pParse->colNamesSet = 1;
drh5080aaa2002-07-11 12:18:16 +0000615 if( pParse->db->flags & SQLITE_ReportTypes ){
616 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr*2, 0);
617 }else{
618 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
619 }
drh82c3d632000-06-06 21:56:07 +0000620 for(i=0; i<pEList->nExpr; i++){
621 Expr *p;
drhb1363202002-06-26 02:45:03 +0000622 char *zType = 0;
drh1bee3d72001-10-15 00:44:35 +0000623 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000624 if( pEList->a[i].zName ){
625 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000626 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
627 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000628 continue;
629 }
630 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000631 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000632 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
drhfa173a72002-07-10 21:26:00 +0000633 if( p->op==TK_COLUMN && pTabList ){
drh832508b2002-03-02 17:04:07 +0000634 Table *pTab = pTabList->a[p->iTable - base].pTab;
drh97665872002-02-13 23:22:53 +0000635 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000636 int iCol = p->iColumn;
637 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000638 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000639 if( iCol<0 ){
640 zCol = "_ROWID_";
641 zType = "INTEGER";
642 }else{
643 zCol = pTab->aCol[iCol].zName;
644 zType = pTab->aCol[iCol].zType;
645 }
drhfa173a72002-07-10 21:26:00 +0000646 if( p->span.z && p->span.z[0] && !showFullNames ){
647 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
648 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
649 sqliteVdbeCompressSpace(v, addr);
650 }else if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000651 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000652 char *zTab;
653
drh832508b2002-03-02 17:04:07 +0000654 zTab = pTabList->a[p->iTable - base].zAlias;
drh01a34662001-10-20 12:30:10 +0000655 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000656 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000657 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
658 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000659 sqliteFree(zName);
660 }else{
drh99fcd712001-10-13 01:06:47 +0000661 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000662 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000663 }
drhfa173a72002-07-10 21:26:00 +0000664 }else if( p->span.z && p->span.z[0] && !showFullNames ){
665 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
666 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
667 sqliteVdbeCompressSpace(v, addr);
drh1bee3d72001-10-15 00:44:35 +0000668 }else if( p->span.z && p->span.z[0] ){
669 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
670 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
671 sqliteVdbeCompressSpace(v, addr);
672 }else{
673 char zName[30];
674 assert( p->op!=TK_COLUMN || pTabList==0 );
675 sprintf(zName, "column%d", i+1);
676 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
677 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000678 }
drh5080aaa2002-07-11 12:18:16 +0000679 if( pParse->db->flags & SQLITE_ReportTypes ){
680 if( zType==0 ){
681 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
682 zType = "TEXT";
683 }else{
684 zType = "NUMERIC";
685 }
drhb1363202002-06-26 02:45:03 +0000686 }
drh5080aaa2002-07-11 12:18:16 +0000687 sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0);
688 sqliteVdbeChangeP3(v, -1, zType, P3_STATIC);
drhb1363202002-06-26 02:45:03 +0000689 }
drh82c3d632000-06-06 21:56:07 +0000690 }
691}
692
693/*
drhd8bc7082000-06-07 23:51:50 +0000694** Name of the connection operator, used for error messages.
695*/
696static const char *selectOpName(int id){
697 char *z;
698 switch( id ){
699 case TK_ALL: z = "UNION ALL"; break;
700 case TK_INTERSECT: z = "INTERSECT"; break;
701 case TK_EXCEPT: z = "EXCEPT"; break;
702 default: z = "UNION"; break;
703 }
704 return z;
705}
706
707/*
drh22f70c32002-02-18 01:17:00 +0000708** Given a SELECT statement, generate a Table structure that describes
709** the result set of that SELECT.
710*/
711Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
712 Table *pTab;
713 int i;
714 ExprList *pEList;
715 static int fillInColumnList(Parse*, Select*);
716
717 if( fillInColumnList(pParse, pSelect) ){
718 return 0;
719 }
720 pTab = sqliteMalloc( sizeof(Table) );
721 if( pTab==0 ){
722 return 0;
723 }
724 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
725 pEList = pSelect->pEList;
726 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000727 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000728 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
729 for(i=0; i<pTab->nCol; i++){
730 Expr *p;
731 if( pEList->a[i].zName ){
732 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
733 }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
734 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
drhd820cb12002-02-18 03:21:45 +0000735 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
736 p->pRight->token.z[0] ){
737 sqliteSetNString(&pTab->aCol[i].zName,
738 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000739 }else{
740 char zBuf[30];
741 sprintf(zBuf, "column%d", i+1);
742 pTab->aCol[i].zName = sqliteStrDup(zBuf);
743 }
744 }
745 pTab->iPKey = -1;
746 return pTab;
747}
748
749/*
drhad2d8302002-05-24 20:31:36 +0000750** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000751**
drhad3cab52002-05-24 02:04:32 +0000752** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh22f70c32002-02-18 01:17:00 +0000753** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000754**
drhad2d8302002-05-24 20:31:36 +0000755** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
756** on joins and the ON and USING clause of joins.
757**
758** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000759** for instances of the "*" operator or the TABLE.* operator.
760** If found, expand each "*" to be every column in every table
761** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000762**
763** Return 0 on success. If there are problems, leave an error message
764** in pParse and return non-zero.
765*/
766static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000767 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000768 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000769 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000770 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000771
772 if( p==0 || p->pSrc==0 ) return 1;
773 pTabList = p->pSrc;
774 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000775
776 /* Look up every table in the table list.
777 */
drhad3cab52002-05-24 02:04:32 +0000778 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000779 if( pTabList->a[i].pTab ){
780 /* This routine has run before! No need to continue */
781 return 0;
782 }
drhdaffd0e2001-04-11 14:28:42 +0000783 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000784 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000785 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000786 if( pTabList->a[i].zAlias==0 ){
787 char zFakeName[60];
788 sprintf(zFakeName, "sqlite_subquery_%p_",
789 (void*)pTabList->a[i].pSelect);
790 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
791 }
drh22f70c32002-02-18 01:17:00 +0000792 pTabList->a[i].pTab = pTab =
793 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
794 pTabList->a[i].pSelect);
795 if( pTab==0 ){
796 return 1;
797 }
798 pTab->isTransient = 1;
799 }else{
drha76b5df2002-02-23 02:32:10 +0000800 /* An ordinary table or view name in the FROM clause */
801 pTabList->a[i].pTab = pTab =
802 sqliteFindTable(pParse->db, pTabList->a[i].zName);
803 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000804 sqliteSetString(&pParse->zErrMsg, "no such table: ",
805 pTabList->a[i].zName, 0);
806 pParse->nErr++;
807 return 1;
808 }
drha76b5df2002-02-23 02:32:10 +0000809 if( pTab->pSelect ){
drh417be792002-03-03 18:59:40 +0000810 if( sqliteViewGetColumnNames(pParse, pTab) ){
811 return 1;
812 }
drhff78bd22002-02-27 01:47:11 +0000813 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000814 }
drhd8bc7082000-06-07 23:51:50 +0000815 }
816 }
817
drhad2d8302002-05-24 20:31:36 +0000818 /* Process NATURAL keywords, and ON and USING clauses of joins.
819 */
820 if( sqliteProcessJoin(pParse, p) ) return 1;
821
drh7c917d12001-12-16 20:05:05 +0000822 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000823 ** all columns in all tables. And for every TABLE.* insert the names
824 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000825 ** with the TK_ALL operator for each "*" that it found in the column list.
826 ** The following code just has to locate the TK_ALL expressions and expand
827 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000828 **
829 ** The first loop just checks to see if there are any "*" operators
830 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000831 */
drh7c917d12001-12-16 20:05:05 +0000832 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000833 Expr *pE = pEList->a[k].pExpr;
834 if( pE->op==TK_ALL ) break;
835 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
836 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000837 }
drh54473222002-04-04 02:10:55 +0000838 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000839 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000840 /*
841 ** If we get here it means the result set contains one or more "*"
842 ** operators that need to be expanded. Loop through each expression
843 ** in the result set and expand them one by one.
844 */
drh7c917d12001-12-16 20:05:05 +0000845 struct ExprList_item *a = pEList->a;
846 ExprList *pNew = 0;
847 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000848 Expr *pE = a[k].pExpr;
849 if( pE->op!=TK_ALL &&
850 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
851 /* This particular expression does not need to be expanded.
852 */
drh7c917d12001-12-16 20:05:05 +0000853 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
854 pNew->a[pNew->nExpr-1].zName = a[k].zName;
855 a[k].pExpr = 0;
856 a[k].zName = 0;
857 }else{
drh54473222002-04-04 02:10:55 +0000858 /* This expression is a "*" or a "TABLE.*" and needs to be
859 ** expanded. */
860 int tableSeen = 0; /* Set to 1 when TABLE matches */
861 Token *pName; /* text of name of TABLE */
862 if( pE->op==TK_DOT && pE->pLeft ){
863 pName = &pE->pLeft->token;
864 }else{
865 pName = 0;
866 }
drhad3cab52002-05-24 02:04:32 +0000867 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000868 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000869 char *zTabName = pTabList->a[i].zAlias;
870 if( zTabName==0 || zTabName[0]==0 ){
871 zTabName = pTab->zName;
872 }
drhc754fa52002-05-27 03:25:51 +0000873 if( pName && (zTabName==0 || zTabName[0]==0 ||
874 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
875 zTabName[pName->n]!=0) ){
drh54473222002-04-04 02:10:55 +0000876 continue;
877 }
878 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000879 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000880 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000881 char *zName = pTab->aCol[j].zName;
882
883 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
884 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
885 /* In a NATURAL join, omit the join columns from the
886 ** table on the right */
887 continue;
888 }
889 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
890 /* In a join with a USING clause, omit columns in the
891 ** using clause from the table on the right. */
892 continue;
893 }
drh22f70c32002-02-18 01:17:00 +0000894 pRight = sqliteExpr(TK_ID, 0, 0, 0);
895 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000896 pRight->token.z = zName;
897 pRight->token.n = strlen(zName);
drh54473222002-04-04 02:10:55 +0000898 if( zTabName ){
drh22f70c32002-02-18 01:17:00 +0000899 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
900 if( pLeft==0 ) break;
drh54473222002-04-04 02:10:55 +0000901 pLeft->token.z = zTabName;
902 pLeft->token.n = strlen(zTabName);
drh22f70c32002-02-18 01:17:00 +0000903 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
904 if( pExpr==0 ) break;
drh7c917d12001-12-16 20:05:05 +0000905 }else{
drh22f70c32002-02-18 01:17:00 +0000906 pExpr = pRight;
907 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +0000908 }
drh7c917d12001-12-16 20:05:05 +0000909 pNew = sqliteExprListAppend(pNew, pExpr, 0);
910 }
drh17e24df2001-11-06 14:10:41 +0000911 }
drh54473222002-04-04 02:10:55 +0000912 if( !tableSeen ){
drhf5db2d32002-06-06 23:42:27 +0000913 if( pName ){
914 sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
915 pName->z, pName->n, 0);
916 }else{
917 sqliteSetString(&pParse->zErrMsg, "no tables specified", 0);
918 }
drh54473222002-04-04 02:10:55 +0000919 rc = 1;
920 }
drhd8bc7082000-06-07 23:51:50 +0000921 }
922 }
drh7c917d12001-12-16 20:05:05 +0000923 sqliteExprListDelete(pEList);
924 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000925 }
drh54473222002-04-04 02:10:55 +0000926 return rc;
drhd8bc7082000-06-07 23:51:50 +0000927}
928
929/*
drhff78bd22002-02-27 01:47:11 +0000930** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
931** in a select structure. It just sets the pointers to NULL. This
932** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
933** pointer is not NULL, this routine is called recursively on that pointer.
934**
935** This routine is called on the Select structure that defines a
936** VIEW in order to undo any bindings to tables. This is necessary
937** because those tables might be DROPed by a subsequent SQL command.
938*/
939void sqliteSelectUnbind(Select *p){
940 int i;
drhad3cab52002-05-24 02:04:32 +0000941 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +0000942 Table *pTab;
943 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +0000944 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +0000945 if( (pTab = pSrc->a[i].pTab)!=0 ){
946 if( pTab->isTransient ){
947 sqliteDeleteTable(0, pTab);
948 sqliteSelectDelete(pSrc->a[i].pSelect);
949 pSrc->a[i].pSelect = 0;
950 }
951 pSrc->a[i].pTab = 0;
952 if( pSrc->a[i].pSelect ){
953 sqliteSelectUnbind(pSrc->a[i].pSelect);
954 }
955 }
956 }
957}
958
959/*
drhd8bc7082000-06-07 23:51:50 +0000960** This routine associates entries in an ORDER BY expression list with
961** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000962** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000963** the top-level node is filled in with column number and the iTable
964** value of the top-level node is filled with iTable parameter.
965**
966** If there are prior SELECT clauses, they are processed first. A match
967** in an earlier SELECT takes precedence over a later SELECT.
968**
969** Any entry that does not match is flagged as an error. The number
970** of errors is returned.
971*/
972static int matchOrderbyToColumn(
973 Parse *pParse, /* A place to leave error messages */
974 Select *pSelect, /* Match to result columns of this SELECT */
975 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +0000976 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +0000977 int mustComplete /* If TRUE all ORDER BYs must match */
978){
979 int nErr = 0;
980 int i, j;
981 ExprList *pEList;
982
drhdaffd0e2001-04-11 14:28:42 +0000983 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000984 if( mustComplete ){
985 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
986 }
987 if( fillInColumnList(pParse, pSelect) ){
988 return 1;
989 }
990 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000991 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
992 return 1;
993 }
drhd8bc7082000-06-07 23:51:50 +0000994 }
995 pEList = pSelect->pEList;
996 for(i=0; i<pOrderBy->nExpr; i++){
997 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +0000998 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +0000999 if( pOrderBy->a[i].done ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001000 if( sqliteExprIsInteger(pE, &iCol) ){
1001 if( iCol<=0 || iCol>pEList->nExpr ){
1002 char zBuf[200];
1003 sprintf(zBuf,"ORDER BY position %d should be between 1 and %d",
1004 iCol, pEList->nExpr);
1005 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1006 pParse->nErr++;
1007 nErr++;
1008 break;
1009 }
1010 iCol--;
1011 }
1012 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001013 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001014 char *zName, *zLabel;
1015 zName = pEList->a[j].zName;
1016 assert( pE->token.z );
1017 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +00001018 sqliteDequote(zLabel);
1019 if( sqliteStrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001020 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001021 }
drh6e142f52000-06-08 13:36:40 +00001022 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001023 }
drhe4de1fe2002-06-02 16:09:01 +00001024 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
1025 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001026 }
1027 }
drhe4de1fe2002-06-02 16:09:01 +00001028 if( iCol>=0 ){
1029 pE->op = TK_COLUMN;
1030 pE->iColumn = iCol;
1031 pE->iTable = iTable;
1032 pOrderBy->a[i].done = 1;
1033 }
1034 if( iCol<0 && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +00001035 char zBuf[30];
1036 sprintf(zBuf,"%d",i+1);
1037 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
1038 " does not match any result column", 0);
1039 pParse->nErr++;
1040 nErr++;
1041 break;
1042 }
1043 }
1044 return nErr;
1045}
1046
1047/*
1048** Get a VDBE for the given parser context. Create a new one if necessary.
1049** If an error occurs, return NULL and leave a message in pParse.
1050*/
1051Vdbe *sqliteGetVdbe(Parse *pParse){
1052 Vdbe *v = pParse->pVdbe;
1053 if( v==0 ){
drh4c504392000-10-16 22:06:40 +00001054 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001055 }
drhd8bc7082000-06-07 23:51:50 +00001056 return v;
1057}
1058
1059
1060/*
drh82c3d632000-06-06 21:56:07 +00001061** This routine is called to process a query that is really the union
1062** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001063**
1064** "p" points to the right-most of the two queries. The results should
1065** be stored in eDest with parameter iParm.
drh82c3d632000-06-06 21:56:07 +00001066*/
1067static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +00001068 int rc; /* Success code from a subroutine */
1069 Select *pPrior; /* Another SELECT immediately to our left */
1070 Vdbe *v; /* Generate code to this VDBE */
1071 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +00001072
drhd8bc7082000-06-07 23:51:50 +00001073 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
1074 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +00001075 */
drhdaffd0e2001-04-11 14:28:42 +00001076 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001077 pPrior = p->pPrior;
1078 if( pPrior->pOrderBy ){
1079 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
1080 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +00001081 pParse->nErr++;
1082 return 1;
1083 }
1084
drhd8bc7082000-06-07 23:51:50 +00001085 /* Make sure we have a valid query engine. If not, create a new one.
1086 */
1087 v = sqliteGetVdbe(pParse);
1088 if( v==0 ) return 1;
1089
drh1cc3d752002-03-23 00:31:29 +00001090 /* Create the destination temporary table if necessary
1091 */
1092 if( eDest==SRT_TempTable ){
1093 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1094 eDest = SRT_Table;
1095 }
1096
drhf46f9052002-06-22 02:33:38 +00001097 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001098 */
drh10e5e3c2000-06-08 00:19:02 +00001099 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +00001100 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001101 case TK_ALL: {
1102 if( p->pOrderBy==0 ){
1103 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
1104 if( rc ) return rc;
1105 p->pPrior = 0;
1106 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
1107 p->pPrior = pPrior;
1108 if( rc ) return rc;
1109 break;
1110 }
1111 /* For UNION ALL ... ORDER BY fall through to the next case */
1112 }
drh82c3d632000-06-06 21:56:07 +00001113 case TK_EXCEPT:
1114 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001115 int unionTab; /* Cursor number of the temporary table holding result */
1116 int op; /* One of the SRT_ operations to apply to self */
1117 int priorOp; /* The SRT_ operation to apply to prior selects */
drhc926afb2002-06-20 03:38:26 +00001118 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
drh82c3d632000-06-06 21:56:07 +00001119
drhd8bc7082000-06-07 23:51:50 +00001120 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drhc926afb2002-06-20 03:38:26 +00001121 if( eDest==priorOp && p->pOrderBy==0 ){
drhd8bc7082000-06-07 23:51:50 +00001122 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001123 ** right.
drhd8bc7082000-06-07 23:51:50 +00001124 */
drh82c3d632000-06-06 21:56:07 +00001125 unionTab = iParm;
1126 }else{
drhd8bc7082000-06-07 23:51:50 +00001127 /* We will need to create our own temporary table to hold the
1128 ** intermediate results.
1129 */
1130 unionTab = pParse->nTab++;
1131 if( p->pOrderBy
1132 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
1133 return 1;
1134 }
drhd8bc7082000-06-07 23:51:50 +00001135 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +00001136 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +00001137 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +00001138 }else{
drh99fcd712001-10-13 01:06:47 +00001139 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001140 }
drh82c3d632000-06-06 21:56:07 +00001141 }
drhd8bc7082000-06-07 23:51:50 +00001142
1143 /* Code the SELECT statements to our left
1144 */
drh832508b2002-03-02 17:04:07 +00001145 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001146 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001147
1148 /* Code the current SELECT statement
1149 */
1150 switch( p->op ){
1151 case TK_EXCEPT: op = SRT_Except; break;
1152 case TK_UNION: op = SRT_Union; break;
1153 case TK_ALL: op = SRT_Table; break;
1154 }
drh82c3d632000-06-06 21:56:07 +00001155 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001156 pOrderBy = p->pOrderBy;
1157 p->pOrderBy = 0;
drh832508b2002-03-02 17:04:07 +00001158 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001159 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001160 p->pOrderBy = pOrderBy;
drh82c3d632000-06-06 21:56:07 +00001161 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001162
1163 /* Convert the data in the temporary table into whatever form
1164 ** it is that we currently need.
1165 */
drhc926afb2002-06-20 03:38:26 +00001166 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001167 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001168 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001169 if( eDest==SRT_Callback ){
1170 generateColumnNames(pParse, p->base, 0, p->pEList);
1171 }
drh82c3d632000-06-06 21:56:07 +00001172 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001173 iCont = sqliteVdbeMakeLabel(v);
1174 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1175 iStart = sqliteVdbeCurrentAddr(v);
drh38640e12002-07-05 21:42:36 +00001176 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001177 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001178 iCont, iBreak);
1179 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001180 sqliteVdbeResolveLabel(v, iCont);
1181 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +00001182 sqliteVdbeResolveLabel(v, iBreak);
1183 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001184 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001185 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001186 }
drh82c3d632000-06-06 21:56:07 +00001187 }
1188 break;
1189 }
1190 case TK_INTERSECT: {
1191 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001192 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001193
drhd8bc7082000-06-07 23:51:50 +00001194 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001195 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001196 ** by allocating the tables we will need.
1197 */
drh82c3d632000-06-06 21:56:07 +00001198 tab1 = pParse->nTab++;
1199 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001200 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
1201 return 1;
1202 }
drhc6b52df2002-01-04 03:09:29 +00001203 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +00001204 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +00001205
1206 /* Code the SELECTs to our left into temporary table "tab1".
1207 */
drh832508b2002-03-02 17:04:07 +00001208 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001209 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001210
1211 /* Code the current SELECT into temporary table "tab2"
1212 */
drhc6b52df2002-01-04 03:09:29 +00001213 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +00001214 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +00001215 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +00001216 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001217 p->pPrior = pPrior;
1218 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001219
1220 /* Generate code to take the intersection of the two temporary
1221 ** tables.
1222 */
drh82c3d632000-06-06 21:56:07 +00001223 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001224 if( eDest==SRT_Callback ){
1225 generateColumnNames(pParse, p->base, 0, p->pEList);
1226 }
drh82c3d632000-06-06 21:56:07 +00001227 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001228 iCont = sqliteVdbeMakeLabel(v);
1229 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
1230 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001231 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh38640e12002-07-05 21:42:36 +00001232 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001233 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001234 iCont, iBreak);
1235 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001236 sqliteVdbeResolveLabel(v, iCont);
1237 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001238 sqliteVdbeResolveLabel(v, iBreak);
1239 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1240 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001241 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001242 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001243 }
drh82c3d632000-06-06 21:56:07 +00001244 break;
1245 }
1246 }
1247 assert( p->pEList && pPrior->pEList );
1248 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +00001249 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1250 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +00001251 pParse->nErr++;
1252 return 1;
drh22827922000-06-06 17:27:05 +00001253 }
drh10e5e3c2000-06-08 00:19:02 +00001254 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +00001255 return 0;
1256}
1257
1258/*
drh832508b2002-03-02 17:04:07 +00001259** Recursively scan through an expression tree. For every reference
1260** to a column in table number iFrom, change that reference to the
1261** same column in table number iTo.
1262*/
1263static void changeTables(Expr *pExpr, int iFrom, int iTo){
1264 if( pExpr==0 ) return;
1265 if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1266 pExpr->iTable = iTo;
1267 }else{
drh1b2e0322002-03-03 02:49:51 +00001268 static void changeTablesInList(ExprList*, int, int);
drh832508b2002-03-02 17:04:07 +00001269 changeTables(pExpr->pLeft, iFrom, iTo);
1270 changeTables(pExpr->pRight, iFrom, iTo);
drh1b2e0322002-03-03 02:49:51 +00001271 changeTablesInList(pExpr->pList, iFrom, iTo);
1272 }
1273}
1274static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
1275 if( pList ){
1276 int i;
1277 for(i=0; i<pList->nExpr; i++){
1278 changeTables(pList->a[i].pExpr, iFrom, iTo);
drh832508b2002-03-02 17:04:07 +00001279 }
1280 }
1281}
1282
1283/*
1284** Scan through the expression pExpr. Replace every reference to
1285** a column in table number iTable with a copy of the corresponding
drh84e59202002-03-14 14:33:31 +00001286** entry in pEList. (But leave references to the ROWID column
1287** unchanged.) When making a copy of an expression in pEList, change
1288** references to columns in table iSub into references to table iTable.
drh832508b2002-03-02 17:04:07 +00001289**
1290** This routine is part of the flattening procedure. A subquery
1291** whose result set is defined by pEList appears as entry in the
1292** FROM clause of a SELECT such that the VDBE cursor assigned to that
1293** FORM clause entry is iTable. This routine make the necessary
1294** changes to pExpr so that it refers directly to the source table
1295** of the subquery rather the result set of the subquery.
1296*/
1297static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1298 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001299 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001300 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001301 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001302 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1303 pNew = pEList->a[pExpr->iColumn].pExpr;
1304 assert( pNew!=0 );
1305 pExpr->op = pNew->op;
1306 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
1307 pExpr->pRight = sqliteExprDup(pNew->pRight);
1308 pExpr->pList = sqliteExprListDup(pNew->pList);
1309 pExpr->iTable = pNew->iTable;
1310 pExpr->iColumn = pNew->iColumn;
1311 pExpr->iAgg = pNew->iAgg;
drh1b2e0322002-03-03 02:49:51 +00001312 pExpr->token = pNew->token;
drh832508b2002-03-02 17:04:07 +00001313 if( iSub!=iTable ){
1314 changeTables(pExpr, iSub, iTable);
1315 }
1316 }else{
1317 static void substExprList(ExprList*,int,ExprList*,int);
1318 substExpr(pExpr->pLeft, iTable, pEList, iSub);
1319 substExpr(pExpr->pRight, iTable, pEList, iSub);
1320 substExprList(pExpr->pList, iTable, pEList, iSub);
1321 }
1322}
1323static void
1324substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1325 int i;
1326 if( pList==0 ) return;
1327 for(i=0; i<pList->nExpr; i++){
1328 substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1329 }
1330}
1331
1332/*
drh1350b032002-02-27 19:00:20 +00001333** This routine attempts to flatten subqueries in order to speed
1334** execution. It returns 1 if it makes changes and 0 if no flattening
1335** occurs.
1336**
1337** To understand the concept of flattening, consider the following
1338** query:
1339**
1340** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1341**
1342** The default way of implementing this query is to execute the
1343** subquery first and store the results in a temporary table, then
1344** run the outer query on that temporary table. This requires two
1345** passes over the data. Furthermore, because the temporary table
1346** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001347** optimized.
drh1350b032002-02-27 19:00:20 +00001348**
drh832508b2002-03-02 17:04:07 +00001349** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001350** a single flat select, like this:
1351**
1352** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1353**
1354** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001355** but only has to scan the data once. And because indices might
1356** exist on the table t1, a complete scan of the data might be
1357** avoided.
drh1350b032002-02-27 19:00:20 +00001358**
drh832508b2002-03-02 17:04:07 +00001359** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001360**
drh832508b2002-03-02 17:04:07 +00001361** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001362**
drh832508b2002-03-02 17:04:07 +00001363** (2) The subquery is not an aggregate or the outer query is not a join.
1364**
1365** (3) The subquery is not a join.
1366**
1367** (4) The subquery is not DISTINCT or the outer query is not a join.
1368**
1369** (5) The subquery is not DISTINCT or the outer query does not use
1370** aggregates.
1371**
1372** (6) The subquery does not use aggregates or the outer query is not
1373** DISTINCT.
1374**
drh08192d52002-04-30 19:20:28 +00001375** (7) The subquery has a FROM clause.
1376**
drhdf199a22002-06-14 22:38:41 +00001377** (8) The subquery does not use LIMIT or the outer query is not a join.
1378**
1379** (9) The subquery does not use LIMIT or the outer query does not use
1380** aggregates.
1381**
1382** (10) The subquery does not use aggregates or the outer query does not
1383** use LIMIT.
1384**
drh832508b2002-03-02 17:04:07 +00001385** In this routine, the "p" parameter is a pointer to the outer query.
1386** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1387** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1388**
1389** If flattening is not attempted, this routine is a no-op and return 0.
1390** If flattening is attempted this routine returns 1.
1391**
1392** All of the expression analysis must occur on both the outer query and
1393** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001394*/
drh832508b2002-03-02 17:04:07 +00001395int flattenSubquery(Select *p, int iFrom, int isAgg, int subqueryIsAgg){
drh0bb28102002-05-08 11:54:14 +00001396 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001397 SrcList *pSrc; /* The FROM clause of the outer query */
1398 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001399 ExprList *pList; /* The result set of the outer query */
drh832508b2002-03-02 17:04:07 +00001400 int i;
1401 int iParent, iSub;
1402 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001403
drh832508b2002-03-02 17:04:07 +00001404 /* Check to see if flattening is permitted. Return 0 if not.
1405 */
1406 if( p==0 ) return 0;
1407 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001408 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001409 pSub = pSrc->a[iFrom].pSelect;
1410 assert( pSub!=0 );
1411 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001412 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001413 pSubSrc = pSub->pSrc;
1414 assert( pSubSrc );
drhad3cab52002-05-24 02:04:32 +00001415 if( pSubSrc->nSrc!=1 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001416 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1417 return 0;
1418 }
drhd11d3822002-06-21 23:01:49 +00001419 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh832508b2002-03-02 17:04:07 +00001420
drh0bb28102002-05-08 11:54:14 +00001421 /* If we reach this point, it means flattening is permitted for the
drh832508b2002-03-02 17:04:07 +00001422 ** i-th entry of the FROM clause in the outer query.
1423 */
1424 iParent = p->base + iFrom;
1425 iSub = pSub->base;
1426 substExprList(p->pEList, iParent, pSub->pEList, iSub);
1427 pList = p->pEList;
1428 for(i=0; i<pList->nExpr; i++){
1429 if( pList->a[i].zName==0 ){
1430 Expr *pExpr = pList->a[i].pExpr;
1431 pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
1432 }
1433 }
drh1b2e0322002-03-03 02:49:51 +00001434 if( isAgg ){
1435 substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1436 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
1437 }
drh832508b2002-03-02 17:04:07 +00001438 substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1439 if( pSub->pWhere ){
1440 pWhere = sqliteExprDup(pSub->pWhere);
1441 if( iParent!=iSub ){
1442 changeTables(pWhere, iSub, iParent);
1443 }
1444 }else{
1445 pWhere = 0;
1446 }
1447 if( subqueryIsAgg ){
1448 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001449 p->pHaving = p->pWhere;
1450 p->pWhere = pWhere;
drh832508b2002-03-02 17:04:07 +00001451 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
drh1b2e0322002-03-03 02:49:51 +00001452 if( pSub->pHaving ){
1453 Expr *pHaving = sqliteExprDup(pSub->pHaving);
1454 if( iParent!=iSub ){
1455 changeTables(pHaving, iSub, iParent);
1456 }
1457 if( p->pHaving ){
1458 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1459 }else{
1460 p->pHaving = pHaving;
1461 }
1462 }
1463 assert( p->pGroupBy==0 );
1464 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
1465 if( iParent!=iSub ){
1466 changeTablesInList(p->pGroupBy, iSub, iParent);
1467 }
drh832508b2002-03-02 17:04:07 +00001468 }else if( p->pWhere==0 ){
1469 p->pWhere = pWhere;
1470 }else{
1471 substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1472 if( pWhere ){
1473 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1474 }
1475 }
1476 p->isDistinct = p->isDistinct || pSub->isDistinct;
drhdf199a22002-06-14 22:38:41 +00001477 if( pSub->nLimit>=0 ){
1478 if( p->nLimit<0 ){
1479 p->nLimit = pSub->nLimit;
1480 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1481 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1482 }
1483 }
1484 p->nOffset += pSub->nOffset;
drh832508b2002-03-02 17:04:07 +00001485 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1486 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1487 }
1488 pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1489 pSubSrc->a[0].pTab = 0;
1490 pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1491 pSubSrc->a[0].pSelect = 0;
1492 sqliteSelectDelete(pSub);
1493 return 1;
1494}
drh1350b032002-02-27 19:00:20 +00001495
1496/*
drh9562b552002-02-19 15:00:07 +00001497** Analyze the SELECT statement passed in as an argument to see if it
1498** is a simple min() or max() query. If it is and this query can be
1499** satisfied using a single seek to the beginning or end of an index,
1500** then generate the code for this SELECT return 1. If this is not a
1501** simple min() or max() query, then return 0;
1502**
1503** A simply min() or max() query looks like this:
1504**
1505** SELECT min(a) FROM table;
1506** SELECT max(a) FROM table;
1507**
1508** The query may have only a single table in its FROM argument. There
1509** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1510** be the min() or max() of a single column of the table. The column
1511** in the min() or max() function must be indexed.
1512**
1513** The parameters to this routine are the same as for sqliteSelect().
1514** See the header comment on that routine for additional information.
1515*/
1516static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1517 Expr *pExpr;
1518 int iCol;
1519 Table *pTab;
1520 Index *pIdx;
1521 int base;
1522 Vdbe *v;
1523 int openOp;
1524 int seekOp;
1525 int cont;
1526 ExprList eList;
1527 struct ExprList_item eListItem;
1528
1529 /* Check to see if this query is a simple min() or max() query. Return
1530 ** zero if it is not.
1531 */
1532 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001533 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001534 if( p->pEList->nExpr!=1 ) return 0;
1535 pExpr = p->pEList->a[0].pExpr;
1536 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1537 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001538 if( pExpr->token.n!=3 ) return 0;
1539 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1540 seekOp = OP_Rewind;
1541 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1542 seekOp = OP_Last;
1543 }else{
1544 return 0;
1545 }
drh9562b552002-02-19 15:00:07 +00001546 pExpr = pExpr->pList->a[0].pExpr;
1547 if( pExpr->op!=TK_COLUMN ) return 0;
1548 iCol = pExpr->iColumn;
1549 pTab = p->pSrc->a[0].pTab;
1550
1551 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001552 ** Check to make sure we have an index and make pIdx point to the
1553 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1554 ** key column, no index is necessary so set pIdx to NULL. If no
1555 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001556 */
1557 if( iCol<0 ){
1558 pIdx = 0;
1559 }else{
1560 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1561 assert( pIdx->nColumn>=1 );
1562 if( pIdx->aiColumn[0]==iCol ) break;
1563 }
1564 if( pIdx==0 ) return 0;
1565 }
1566
drh17f71932002-02-21 12:01:27 +00001567 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001568 ** step is skipped if the output is going to a table or a memory cell.
1569 */
1570 v = sqliteGetVdbe(pParse);
1571 if( v==0 ) return 0;
1572 if( eDest==SRT_Callback ){
drh832508b2002-03-02 17:04:07 +00001573 generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001574 }
1575
drh17f71932002-02-21 12:01:27 +00001576 /* Generating code to find the min or the max. Basically all we have
1577 ** to do is find the first or the last entry in the chosen index. If
1578 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1579 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001580 */
drh5cf8e8c2002-02-19 22:42:05 +00001581 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
1582 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1583 pParse->schemaVerified = 1;
1584 }
drh9562b552002-02-19 15:00:07 +00001585 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +00001586 base = p->base;
drh9562b552002-02-19 15:00:07 +00001587 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001588 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001589 if( pIdx==0 ){
1590 sqliteVdbeAddOp(v, seekOp, base, 0);
1591 }else{
1592 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001593 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001594 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1595 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1596 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1597 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1598 }
drh5cf8e8c2002-02-19 22:42:05 +00001599 eList.nExpr = 1;
1600 memset(&eListItem, 0, sizeof(eListItem));
1601 eList.a = &eListItem;
1602 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +00001603 cont = sqliteVdbeMakeLabel(v);
drh38640e12002-07-05 21:42:36 +00001604 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
drh9562b552002-02-19 15:00:07 +00001605 sqliteVdbeResolveLabel(v, cont);
1606 sqliteVdbeAddOp(v, OP_Close, base, 0);
1607 return 1;
1608}
1609
1610/*
drh9bb61fe2000-06-05 16:01:39 +00001611** Generate code for the given SELECT statement.
1612**
drhfef52082000-06-06 01:50:43 +00001613** The results are distributed in various ways depending on the
1614** value of eDest and iParm.
1615**
1616** eDest Value Result
1617** ------------ -------------------------------------------
1618** SRT_Callback Invoke the callback for each row of the result.
1619**
1620** SRT_Mem Store first result in memory cell iParm
1621**
1622** SRT_Set Store results as keys of a table with cursor iParm
1623**
drh82c3d632000-06-06 21:56:07 +00001624** SRT_Union Store results as a key in a temporary table iParm
1625**
drhc4a3c772001-04-04 11:48:57 +00001626** SRT_Except Remove results form the temporary table iParm.
1627**
1628** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001629**
1630** This routine returns the number of errors. If any errors are
1631** encountered, then an appropriate error message is left in
1632** pParse->zErrMsg.
1633**
1634** This routine does NOT free the Select structure passed in. The
1635** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001636**
1637** The pParent, parentTab, and *pParentAgg fields are filled in if this
1638** SELECT is a subquery. This routine may try to combine this SELECT
1639** with its parent to form a single flat query. In so doing, it might
1640** change the parent query from a non-aggregate to an aggregate query.
1641** For that reason, the pParentAgg flag is passed as a pointer, so it
1642** can be changed.
drh9bb61fe2000-06-05 16:01:39 +00001643*/
1644int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001645 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001646 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +00001647 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drh832508b2002-03-02 17:04:07 +00001648 int iParm, /* Save result in this memory location, if >=0 */
1649 Select *pParent, /* Another SELECT for which this is a sub-query */
1650 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001651 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001652){
drhd8bc7082000-06-07 23:51:50 +00001653 int i;
drhcce7d172000-05-31 15:34:51 +00001654 WhereInfo *pWInfo;
1655 Vdbe *v;
1656 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001657 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001658 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001659 Expr *pWhere; /* The WHERE clause. May be NULL */
1660 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001661 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1662 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001663 int isDistinct; /* True if the DISTINCT keyword is present */
1664 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +00001665 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +00001666 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001667
drhdaffd0e2001-04-11 14:28:42 +00001668 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1669
drh82c3d632000-06-06 21:56:07 +00001670 /* If there is are a sequence of queries, do the earlier ones first.
1671 */
1672 if( p->pPrior ){
1673 return multiSelect(pParse, p, eDest, iParm);
1674 }
1675
1676 /* Make local copies of the parameters for this query.
1677 */
drh9bb61fe2000-06-05 16:01:39 +00001678 pTabList = p->pSrc;
1679 pWhere = p->pWhere;
1680 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00001681 pGroupBy = p->pGroupBy;
1682 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00001683 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00001684
drh832508b2002-03-02 17:04:07 +00001685 /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1686 ** The WHERE processing requires that the cursors for the tables in the
1687 ** FROM clause be consecutive.
drh10e5e3c2000-06-08 00:19:02 +00001688 */
drh832508b2002-03-02 17:04:07 +00001689 base = p->base = pParse->nTab;
drhad3cab52002-05-24 02:04:32 +00001690 pParse->nTab += pTabList->nSrc;
drh10e5e3c2000-06-08 00:19:02 +00001691
drh9bb61fe2000-06-05 16:01:39 +00001692 /*
1693 ** Do not even attempt to generate any code if we have already seen
1694 ** errors before this routine starts.
1695 */
drh1d83f052002-02-17 00:30:36 +00001696 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001697
drhd8bc7082000-06-07 23:51:50 +00001698 /* Look up every table in the table list and create an appropriate
1699 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +00001700 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001701 */
drhd8bc7082000-06-07 23:51:50 +00001702 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001703 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001704 }
drhad2d8302002-05-24 20:31:36 +00001705 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00001706 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001707 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001708
drh22827922000-06-06 17:27:05 +00001709 /* If writing to memory or generating a set
1710 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001711 */
drhfef52082000-06-06 01:50:43 +00001712 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001713 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1714 "a SELECT that is part of an expression", 0);
1715 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001716 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001717 }
1718
drhc926afb2002-06-20 03:38:26 +00001719 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00001720 */
drhc926afb2002-06-20 03:38:26 +00001721 switch( eDest ){
1722 case SRT_Union:
1723 case SRT_Except:
1724 case SRT_Discard:
1725 pOrderBy = 0;
1726 break;
1727 default:
1728 break;
drh22827922000-06-06 17:27:05 +00001729 }
1730
drh10e5e3c2000-06-08 00:19:02 +00001731 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00001732 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00001733 **
drh967e8b72000-06-21 13:59:10 +00001734 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001735 */
drh4794b982000-06-06 13:54:14 +00001736 for(i=0; i<pEList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001737 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001738 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001739 }
drh22827922000-06-06 17:27:05 +00001740 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001741 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001742 }
1743 }
drhcce7d172000-05-31 15:34:51 +00001744 if( pWhere ){
drh832508b2002-03-02 17:04:07 +00001745 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001746 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001747 }
1748 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001749 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001750 }
1751 }
1752 if( pOrderBy ){
1753 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001754 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001755 if( sqliteExprIsConstant(pE) ){
drhe4de1fe2002-06-02 16:09:01 +00001756 int iCol;
1757 if( sqliteExprIsInteger(pE, &iCol)==0 ){
1758 sqliteSetString(&pParse->zErrMsg,
1759 "ORDER BY terms must not be non-integer constants", 0);
1760 pParse->nErr++;
1761 goto select_end;
1762 }else if( iCol<=0 || iCol>pEList->nExpr ){
1763 char zBuf[2000];
1764 sprintf(zBuf,"ORDER BY column number %d out of range - should be "
1765 "between 1 and %d", iCol, pEList->nExpr);
1766 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1767 pParse->nErr++;
1768 goto select_end;
1769 }
1770 sqliteExprDelete(pE);
1771 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
drh92086432002-01-22 14:11:29 +00001772 }
drh832508b2002-03-02 17:04:07 +00001773 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001774 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001775 }
drh22827922000-06-06 17:27:05 +00001776 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001777 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001778 }
1779 }
1780 }
drh22827922000-06-06 17:27:05 +00001781 if( pGroupBy ){
1782 for(i=0; i<pGroupBy->nExpr; i++){
1783 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001784 if( sqliteExprIsConstant(pE) ){
1785 sqliteSetString(&pParse->zErrMsg,
1786 "GROUP BY expressions should not be constant", 0);
1787 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001788 goto select_end;
drh92086432002-01-22 14:11:29 +00001789 }
drh832508b2002-03-02 17:04:07 +00001790 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001791 goto select_end;
drh22827922000-06-06 17:27:05 +00001792 }
1793 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001794 goto select_end;
drh22827922000-06-06 17:27:05 +00001795 }
1796 }
1797 }
1798 if( pHaving ){
1799 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001800 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1801 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001802 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001803 goto select_end;
drh22827922000-06-06 17:27:05 +00001804 }
drh832508b2002-03-02 17:04:07 +00001805 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001806 goto select_end;
drh22827922000-06-06 17:27:05 +00001807 }
drhda932812000-06-06 18:00:15 +00001808 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001809 goto select_end;
drh22827922000-06-06 17:27:05 +00001810 }
drhcce7d172000-05-31 15:34:51 +00001811 }
1812
drh9562b552002-02-19 15:00:07 +00001813 /* Check for the special case of a min() or max() function by itself
1814 ** in the result set.
1815 */
1816 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001817 rc = 0;
drh9562b552002-02-19 15:00:07 +00001818 goto select_end;
1819 }
1820
drhd820cb12002-02-18 03:21:45 +00001821 /* Begin generating code.
1822 */
1823 v = sqliteGetVdbe(pParse);
1824 if( v==0 ) goto select_end;
1825
drh0bb28102002-05-08 11:54:14 +00001826 /* Identify column names if we will be using in the callback. This
1827 ** step is skipped if the output is going to a table or a memory cell.
1828 */
1829 if( eDest==SRT_Callback ){
1830 generateColumnNames(pParse, p->base, pTabList, pEList);
1831 }
1832
1833 /* Set the limiter
1834 */
1835 if( p->nLimit<=0 ){
drhd11d3822002-06-21 23:01:49 +00001836 p->nLimit = -1;
drh0bb28102002-05-08 11:54:14 +00001837 p->nOffset = 0;
1838 }else{
drhd11d3822002-06-21 23:01:49 +00001839 int iMem = pParse->nMem++;
1840 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
drhbf5cd972002-06-24 12:20:23 +00001841 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001842 p->nLimit = iMem;
1843 if( p->nOffset<=0 ){
1844 p->nOffset = 0;
1845 }else{
1846 iMem = pParse->nMem++;
1847 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
drhbf5cd972002-06-24 12:20:23 +00001848 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001849 p->nOffset = iMem;
1850 }
drh0bb28102002-05-08 11:54:14 +00001851 }
1852
drhd820cb12002-02-18 03:21:45 +00001853 /* Generate code for all sub-queries in the FROM clause
1854 */
drhad3cab52002-05-24 02:04:32 +00001855 for(i=0; i<pTabList->nSrc; i++){
drha76b5df2002-02-23 02:32:10 +00001856 if( pTabList->a[i].pSelect==0 ) continue;
drh2d0794e2002-03-03 03:03:52 +00001857 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
drh1b2e0322002-03-03 02:49:51 +00001858 p, i, &isAgg);
1859 pTabList = p->pSrc;
1860 pWhere = p->pWhere;
drhacd4c692002-03-07 02:02:51 +00001861 if( eDest==SRT_Callback ){
1862 pOrderBy = p->pOrderBy;
1863 }
drh1b2e0322002-03-03 02:49:51 +00001864 pGroupBy = p->pGroupBy;
1865 pHaving = p->pHaving;
1866 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00001867 }
1868
drh832508b2002-03-02 17:04:07 +00001869 /* Check to see if this is a subquery that can be "flattened" into its parent.
1870 ** If flattening is a possiblity, do so and return immediately.
1871 */
drh1b2e0322002-03-03 02:49:51 +00001872 if( pParent && pParentAgg &&
1873 flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){
1874 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00001875 return rc;
1876 }
drh832508b2002-03-02 17:04:07 +00001877
drh2d0794e2002-03-03 03:03:52 +00001878 /* If the output is destined for a temporary table, open that table.
1879 */
1880 if( eDest==SRT_TempTable ){
1881 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1882 }
1883
drh22827922000-06-06 17:27:05 +00001884 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001885 */
drhd820cb12002-02-18 03:21:45 +00001886 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001887 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001888 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001889 for(i=0; i<pEList->nExpr; i++){
1890 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001891 goto select_end;
drh22827922000-06-06 17:27:05 +00001892 }
1893 }
1894 if( pGroupBy ){
1895 for(i=0; i<pGroupBy->nExpr; i++){
1896 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001897 goto select_end;
drh22827922000-06-06 17:27:05 +00001898 }
1899 }
1900 }
1901 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001902 goto select_end;
drh22827922000-06-06 17:27:05 +00001903 }
drh191b6902000-06-08 11:13:01 +00001904 if( pOrderBy ){
1905 for(i=0; i<pOrderBy->nExpr; i++){
1906 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001907 goto select_end;
drh191b6902000-06-08 11:13:01 +00001908 }
1909 }
1910 }
drhefb72512000-05-31 20:00:52 +00001911 }
1912
drh22827922000-06-06 17:27:05 +00001913 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001914 */
1915 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001916 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001917 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001918 FuncDef *pFunc;
1919 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001920 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001921 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001922 }
1923 }
drh1bee3d72001-10-15 00:44:35 +00001924 if( pGroupBy==0 ){
1925 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001926 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001927 }
drhcce7d172000-05-31 15:34:51 +00001928 }
1929
drh19a775c2000-06-05 18:54:46 +00001930 /* Initialize the memory cell to NULL
1931 */
drhfef52082000-06-06 01:50:43 +00001932 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001933 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001934 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001935 }
1936
drh832508b2002-03-02 17:04:07 +00001937 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00001938 */
drh19a775c2000-06-05 18:54:46 +00001939 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00001940 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00001941 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00001942 }else{
1943 distinct = -1;
drhefb72512000-05-31 20:00:52 +00001944 }
drh832508b2002-03-02 17:04:07 +00001945
1946 /* Begin the database scan
1947 */
drhe3184742002-06-19 14:27:05 +00001948 pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0, &pOrderBy);
drh1d83f052002-02-17 00:30:36 +00001949 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001950
drh22827922000-06-06 17:27:05 +00001951 /* Use the standard inner loop if we are not dealing with
1952 ** aggregates
drhcce7d172000-05-31 15:34:51 +00001953 */
drhda9d6c42000-05-31 18:20:14 +00001954 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00001955 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
1956 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00001957 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00001958 }
drhcce7d172000-05-31 15:34:51 +00001959 }
drhefb72512000-05-31 20:00:52 +00001960
drhe3184742002-06-19 14:27:05 +00001961 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00001962 ** processing.
drhefb72512000-05-31 20:00:52 +00001963 */
drh22827922000-06-06 17:27:05 +00001964 else{
drh22827922000-06-06 17:27:05 +00001965 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00001966 int lbl1;
drh22827922000-06-06 17:27:05 +00001967 for(i=0; i<pGroupBy->nExpr; i++){
1968 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
1969 }
drh99fcd712001-10-13 01:06:47 +00001970 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh491791a2002-07-18 00:34:09 +00001971 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
drh1bee3d72001-10-15 00:44:35 +00001972 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001973 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00001974 for(i=0; i<pParse->nAgg; i++){
1975 if( pParse->aAgg[i].isAgg ) continue;
1976 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00001977 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00001978 }
drh22827922000-06-06 17:27:05 +00001979 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00001980 }
drh22827922000-06-06 17:27:05 +00001981 for(i=0; i<pParse->nAgg; i++){
1982 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00001983 int j;
drh22827922000-06-06 17:27:05 +00001984 if( !pParse->aAgg[i].isAgg ) continue;
1985 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00001986 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00001987 if( pE->pList ){
1988 for(j=0; j<pE->pList->nExpr; j++){
1989 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
1990 }
drhe5095352002-02-24 03:25:14 +00001991 }
drh0bce8352002-02-28 00:41:10 +00001992 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00001993 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00001994 assert( pParse->aAgg[i].pFunc!=0 );
1995 assert( pParse->aAgg[i].pFunc->xStep!=0 );
1996 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00001997 }
drhcce7d172000-05-31 15:34:51 +00001998 }
1999
2000 /* End the database scan loop.
2001 */
2002 sqliteWhereEnd(pWInfo);
2003
drh22827922000-06-06 17:27:05 +00002004 /* If we are processing aggregates, we need to set up a second loop
2005 ** over all of the aggregate values and process them.
2006 */
2007 if( isAgg ){
2008 int endagg = sqliteVdbeMakeLabel(v);
2009 int startagg;
drh99fcd712001-10-13 01:06:47 +00002010 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002011 pParse->useAgg = 1;
2012 if( pHaving ){
drhf5905aa2002-05-26 20:54:33 +00002013 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002014 }
drhdf199a22002-06-14 22:38:41 +00002015 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2016 iParm, startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00002017 goto select_end;
drh22827922000-06-06 17:27:05 +00002018 }
drh99fcd712001-10-13 01:06:47 +00002019 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
2020 sqliteVdbeResolveLabel(v, endagg);
2021 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002022 pParse->useAgg = 0;
2023 }
2024
drhcce7d172000-05-31 15:34:51 +00002025 /* If there is an ORDER BY clause, then we need to sort the results
2026 ** and send them to the callback one by one.
2027 */
2028 if( pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00002029 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002030 }
drh6a535342001-10-19 16:44:56 +00002031
2032
2033 /* Issue a null callback if that is what the user wants.
2034 */
2035 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
2036 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
2037 }
2038
drh1d83f052002-02-17 00:30:36 +00002039 /* The SELECT was successfully coded. Set the return code to 0
2040 ** to indicate no errors.
2041 */
2042 rc = 0;
2043
2044 /* Control jumps to here if an error is encountered above, or upon
2045 ** successful coding of the SELECT.
2046 */
2047select_end:
drh832508b2002-03-02 17:04:07 +00002048 pParse->nTab = base;
drh1d83f052002-02-17 00:30:36 +00002049 sqliteAggregateInfoReset(pParse);
2050 return rc;
drhcce7d172000-05-31 15:34:51 +00002051}