xref: /sqlite-3.40.0/src/select.c (revision cf55b7ae)
1cce7d176Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
3cce7d176Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
6cce7d176Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
10cce7d176Sdrh **
11cce7d176Sdrh *************************************************************************
12cce7d176Sdrh ** This file contains C code routines that are called by the parser
13b19a2bc6Sdrh ** to handle SELECT statements in SQLite.
14cce7d176Sdrh **
15*cf55b7aeSdrh ** $Id: select.c,v 1.201 2004/07/20 01:45:20 drh Exp $
16cce7d176Sdrh */
17cce7d176Sdrh #include "sqliteInt.h"
18cce7d176Sdrh 
19315555caSdrh 
20cce7d176Sdrh /*
219bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that
229bb61fe7Sdrh ** structure.
23cce7d176Sdrh */
244adee20fSdanielk1977 Select *sqlite3SelectNew(
25daffd0e5Sdrh   ExprList *pEList,     /* which columns to include in the result */
26ad3cab52Sdrh   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
27daffd0e5Sdrh   Expr *pWhere,         /* the WHERE clause */
28daffd0e5Sdrh   ExprList *pGroupBy,   /* the GROUP BY clause */
29daffd0e5Sdrh   Expr *pHaving,        /* the HAVING clause */
30daffd0e5Sdrh   ExprList *pOrderBy,   /* the ORDER BY clause */
319bbca4c1Sdrh   int isDistinct,       /* true if the DISTINCT keyword is present */
329bbca4c1Sdrh   int nLimit,           /* LIMIT value.  -1 means not used */
33ef0cae50Sdrh   int nOffset           /* OFFSET value.  0 means no offset */
349bb61fe7Sdrh ){
359bb61fe7Sdrh   Select *pNew;
369bb61fe7Sdrh   pNew = sqliteMalloc( sizeof(*pNew) );
37daffd0e5Sdrh   if( pNew==0 ){
384adee20fSdanielk1977     sqlite3ExprListDelete(pEList);
394adee20fSdanielk1977     sqlite3SrcListDelete(pSrc);
404adee20fSdanielk1977     sqlite3ExprDelete(pWhere);
414adee20fSdanielk1977     sqlite3ExprListDelete(pGroupBy);
424adee20fSdanielk1977     sqlite3ExprDelete(pHaving);
434adee20fSdanielk1977     sqlite3ExprListDelete(pOrderBy);
44daffd0e5Sdrh   }else{
45b733d037Sdrh     if( pEList==0 ){
464adee20fSdanielk1977       pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0);
47b733d037Sdrh     }
489bb61fe7Sdrh     pNew->pEList = pEList;
499bb61fe7Sdrh     pNew->pSrc = pSrc;
509bb61fe7Sdrh     pNew->pWhere = pWhere;
519bb61fe7Sdrh     pNew->pGroupBy = pGroupBy;
529bb61fe7Sdrh     pNew->pHaving = pHaving;
539bb61fe7Sdrh     pNew->pOrderBy = pOrderBy;
549bb61fe7Sdrh     pNew->isDistinct = isDistinct;
5582c3d636Sdrh     pNew->op = TK_SELECT;
569bbca4c1Sdrh     pNew->nLimit = nLimit;
579bbca4c1Sdrh     pNew->nOffset = nOffset;
587b58daeaSdrh     pNew->iLimit = -1;
597b58daeaSdrh     pNew->iOffset = -1;
60daffd0e5Sdrh   }
619bb61fe7Sdrh   return pNew;
629bb61fe7Sdrh }
639bb61fe7Sdrh 
649bb61fe7Sdrh /*
6501f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
6601f3f253Sdrh ** type of join.  Return an integer constant that expresses that type
6701f3f253Sdrh ** in terms of the following bit values:
6801f3f253Sdrh **
6901f3f253Sdrh **     JT_INNER
7001f3f253Sdrh **     JT_OUTER
7101f3f253Sdrh **     JT_NATURAL
7201f3f253Sdrh **     JT_LEFT
7301f3f253Sdrh **     JT_RIGHT
7401f3f253Sdrh **
7501f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
7601f3f253Sdrh **
7701f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return
7801f3f253Sdrh ** a join type, but put an error in the pParse structure.
7901f3f253Sdrh */
804adee20fSdanielk1977 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
8101f3f253Sdrh   int jointype = 0;
8201f3f253Sdrh   Token *apAll[3];
8301f3f253Sdrh   Token *p;
8401f3f253Sdrh   static struct {
8501f3f253Sdrh     const char *zKeyword;
8601f3f253Sdrh     int nChar;
8701f3f253Sdrh     int code;
8801f3f253Sdrh   } keywords[] = {
8901f3f253Sdrh     { "natural", 7, JT_NATURAL },
90195e6967Sdrh     { "left",    4, JT_LEFT|JT_OUTER },
91195e6967Sdrh     { "right",   5, JT_RIGHT|JT_OUTER },
92195e6967Sdrh     { "full",    4, JT_LEFT|JT_RIGHT|JT_OUTER },
9301f3f253Sdrh     { "outer",   5, JT_OUTER },
9401f3f253Sdrh     { "inner",   5, JT_INNER },
9501f3f253Sdrh     { "cross",   5, JT_INNER },
9601f3f253Sdrh   };
9701f3f253Sdrh   int i, j;
9801f3f253Sdrh   apAll[0] = pA;
9901f3f253Sdrh   apAll[1] = pB;
10001f3f253Sdrh   apAll[2] = pC;
101195e6967Sdrh   for(i=0; i<3 && apAll[i]; i++){
10201f3f253Sdrh     p = apAll[i];
10301f3f253Sdrh     for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
10401f3f253Sdrh       if( p->n==keywords[j].nChar
1054adee20fSdanielk1977           && sqlite3StrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
10601f3f253Sdrh         jointype |= keywords[j].code;
10701f3f253Sdrh         break;
10801f3f253Sdrh       }
10901f3f253Sdrh     }
11001f3f253Sdrh     if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
11101f3f253Sdrh       jointype |= JT_ERROR;
11201f3f253Sdrh       break;
11301f3f253Sdrh     }
11401f3f253Sdrh   }
115ad2d8307Sdrh   if(
116ad2d8307Sdrh      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
117195e6967Sdrh      (jointype & JT_ERROR)!=0
118ad2d8307Sdrh   ){
11901f3f253Sdrh     static Token dummy = { 0, 0 };
12001f3f253Sdrh     char *zSp1 = " ", *zSp2 = " ";
12101f3f253Sdrh     if( pB==0 ){ pB = &dummy; zSp1 = 0; }
12201f3f253Sdrh     if( pC==0 ){ pC = &dummy; zSp2 = 0; }
1234adee20fSdanielk1977     sqlite3SetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
12401f3f253Sdrh        pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
12501f3f253Sdrh     pParse->nErr++;
12601f3f253Sdrh     jointype = JT_INNER;
127195e6967Sdrh   }else if( jointype & JT_RIGHT ){
1284adee20fSdanielk1977     sqlite3ErrorMsg(pParse,
129da93d238Sdrh       "RIGHT and FULL OUTER JOINs are not currently supported");
130195e6967Sdrh     jointype = JT_INNER;
13101f3f253Sdrh   }
13201f3f253Sdrh   return jointype;
13301f3f253Sdrh }
13401f3f253Sdrh 
13501f3f253Sdrh /*
136ad2d8307Sdrh ** Return the index of a column in a table.  Return -1 if the column
137ad2d8307Sdrh ** is not contained in the table.
138ad2d8307Sdrh */
139ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){
140ad2d8307Sdrh   int i;
141ad2d8307Sdrh   for(i=0; i<pTab->nCol; i++){
1424adee20fSdanielk1977     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
143ad2d8307Sdrh   }
144ad2d8307Sdrh   return -1;
145ad2d8307Sdrh }
146ad2d8307Sdrh 
147ad2d8307Sdrh /*
148ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the
149ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2.
150ad2d8307Sdrh */
151ad2d8307Sdrh static void addWhereTerm(
152ad2d8307Sdrh   const char *zCol,        /* Name of the column */
153ad2d8307Sdrh   const Table *pTab1,      /* First table */
154ad2d8307Sdrh   const Table *pTab2,      /* Second table */
155ad2d8307Sdrh   Expr **ppExpr            /* Add the equality term to this expression */
156ad2d8307Sdrh ){
157ad2d8307Sdrh   Token dummy;
158ad2d8307Sdrh   Expr *pE1a, *pE1b, *pE1c;
159ad2d8307Sdrh   Expr *pE2a, *pE2b, *pE2c;
160ad2d8307Sdrh   Expr *pE;
161ad2d8307Sdrh 
162ad2d8307Sdrh   dummy.z = zCol;
163ad2d8307Sdrh   dummy.n = strlen(zCol);
1644b59ab5eSdrh   dummy.dyn = 0;
1654adee20fSdanielk1977   pE1a = sqlite3Expr(TK_ID, 0, 0, &dummy);
1664adee20fSdanielk1977   pE2a = sqlite3Expr(TK_ID, 0, 0, &dummy);
167ad2d8307Sdrh   dummy.z = pTab1->zName;
168ad2d8307Sdrh   dummy.n = strlen(dummy.z);
1694adee20fSdanielk1977   pE1b = sqlite3Expr(TK_ID, 0, 0, &dummy);
170ad2d8307Sdrh   dummy.z = pTab2->zName;
171ad2d8307Sdrh   dummy.n = strlen(dummy.z);
1724adee20fSdanielk1977   pE2b = sqlite3Expr(TK_ID, 0, 0, &dummy);
1734adee20fSdanielk1977   pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0);
1744adee20fSdanielk1977   pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0);
1754adee20fSdanielk1977   pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0);
1761f16230bSdrh   ExprSetProperty(pE, EP_FromJoin);
177ad2d8307Sdrh   if( *ppExpr ){
1784adee20fSdanielk1977     *ppExpr = sqlite3Expr(TK_AND, *ppExpr, pE, 0);
179ad2d8307Sdrh   }else{
180ad2d8307Sdrh     *ppExpr = pE;
181ad2d8307Sdrh   }
182ad2d8307Sdrh }
183ad2d8307Sdrh 
184ad2d8307Sdrh /*
1851f16230bSdrh ** Set the EP_FromJoin property on all terms of the given expression.
1861cc093c2Sdrh **
187e78e8284Sdrh ** The EP_FromJoin property is used on terms of an expression to tell
1881cc093c2Sdrh ** the LEFT OUTER JOIN processing logic that this term is part of the
1891f16230bSdrh ** join restriction specified in the ON or USING clause and not a part
1901f16230bSdrh ** of the more general WHERE clause.  These terms are moved over to the
1911f16230bSdrh ** WHERE clause during join processing but we need to remember that they
1921f16230bSdrh ** originated in the ON or USING clause.
1931cc093c2Sdrh */
1941cc093c2Sdrh static void setJoinExpr(Expr *p){
1951cc093c2Sdrh   while( p ){
1961f16230bSdrh     ExprSetProperty(p, EP_FromJoin);
1971cc093c2Sdrh     setJoinExpr(p->pLeft);
1981cc093c2Sdrh     p = p->pRight;
1991cc093c2Sdrh   }
2001cc093c2Sdrh }
2011cc093c2Sdrh 
2021cc093c2Sdrh /*
203ad2d8307Sdrh ** This routine processes the join information for a SELECT statement.
204ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause.
205ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms.
206ad2d8307Sdrh **
207ad2d8307Sdrh ** This routine returns the number of errors encountered.
208ad2d8307Sdrh */
209ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){
210ad2d8307Sdrh   SrcList *pSrc;
211ad2d8307Sdrh   int i, j;
212ad2d8307Sdrh   pSrc = p->pSrc;
213ad2d8307Sdrh   for(i=0; i<pSrc->nSrc-1; i++){
214ad2d8307Sdrh     struct SrcList_item *pTerm = &pSrc->a[i];
215ad2d8307Sdrh     struct SrcList_item *pOther = &pSrc->a[i+1];
216ad2d8307Sdrh 
217ad2d8307Sdrh     if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
218ad2d8307Sdrh 
219ad2d8307Sdrh     /* When the NATURAL keyword is present, add WHERE clause terms for
220ad2d8307Sdrh     ** every column that the two tables have in common.
221ad2d8307Sdrh     */
222ad2d8307Sdrh     if( pTerm->jointype & JT_NATURAL ){
223ad2d8307Sdrh       Table *pTab;
224ad2d8307Sdrh       if( pTerm->pOn || pTerm->pUsing ){
2254adee20fSdanielk1977         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
226ad2d8307Sdrh            "an ON or USING clause", 0);
227ad2d8307Sdrh         return 1;
228ad2d8307Sdrh       }
229ad2d8307Sdrh       pTab = pTerm->pTab;
230ad2d8307Sdrh       for(j=0; j<pTab->nCol; j++){
231ad2d8307Sdrh         if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
232ad2d8307Sdrh           addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
233ad2d8307Sdrh         }
234ad2d8307Sdrh       }
235ad2d8307Sdrh     }
236ad2d8307Sdrh 
237ad2d8307Sdrh     /* Disallow both ON and USING clauses in the same join
238ad2d8307Sdrh     */
239ad2d8307Sdrh     if( pTerm->pOn && pTerm->pUsing ){
2404adee20fSdanielk1977       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
241da93d238Sdrh         "clauses in the same join");
242ad2d8307Sdrh       return 1;
243ad2d8307Sdrh     }
244ad2d8307Sdrh 
245ad2d8307Sdrh     /* Add the ON clause to the end of the WHERE clause, connected by
246ad2d8307Sdrh     ** and AND operator.
247ad2d8307Sdrh     */
248ad2d8307Sdrh     if( pTerm->pOn ){
2491cc093c2Sdrh       setJoinExpr(pTerm->pOn);
250ad2d8307Sdrh       if( p->pWhere==0 ){
251ad2d8307Sdrh         p->pWhere = pTerm->pOn;
252ad2d8307Sdrh       }else{
2534adee20fSdanielk1977         p->pWhere = sqlite3Expr(TK_AND, p->pWhere, pTerm->pOn, 0);
254ad2d8307Sdrh       }
255ad2d8307Sdrh       pTerm->pOn = 0;
256ad2d8307Sdrh     }
257ad2d8307Sdrh 
258ad2d8307Sdrh     /* Create extra terms on the WHERE clause for each column named
259ad2d8307Sdrh     ** in the USING clause.  Example: If the two tables to be joined are
260ad2d8307Sdrh     ** A and B and the USING clause names X, Y, and Z, then add this
261ad2d8307Sdrh     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
262ad2d8307Sdrh     ** Report an error if any column mentioned in the USING clause is
263ad2d8307Sdrh     ** not contained in both tables to be joined.
264ad2d8307Sdrh     */
265ad2d8307Sdrh     if( pTerm->pUsing ){
266ad2d8307Sdrh       IdList *pList;
267ad2d8307Sdrh       int j;
268ad2d8307Sdrh       assert( i<pSrc->nSrc-1 );
269ad2d8307Sdrh       pList = pTerm->pUsing;
270ad2d8307Sdrh       for(j=0; j<pList->nId; j++){
271bf5cd97eSdrh         if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 ||
272bf5cd97eSdrh             columnIndex(pOther->pTab, pList->a[j].zName)<0 ){
2734adee20fSdanielk1977           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
274da93d238Sdrh             "not present in both tables", pList->a[j].zName);
275ad2d8307Sdrh           return 1;
276ad2d8307Sdrh         }
277bf5cd97eSdrh         addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
278ad2d8307Sdrh       }
279ad2d8307Sdrh     }
280ad2d8307Sdrh   }
281ad2d8307Sdrh   return 0;
282ad2d8307Sdrh }
283ad2d8307Sdrh 
284ad2d8307Sdrh /*
2859bb61fe7Sdrh ** Delete the given Select structure and all of its substructures.
2869bb61fe7Sdrh */
2874adee20fSdanielk1977 void sqlite3SelectDelete(Select *p){
28882c3d636Sdrh   if( p==0 ) return;
2894adee20fSdanielk1977   sqlite3ExprListDelete(p->pEList);
2904adee20fSdanielk1977   sqlite3SrcListDelete(p->pSrc);
2914adee20fSdanielk1977   sqlite3ExprDelete(p->pWhere);
2924adee20fSdanielk1977   sqlite3ExprListDelete(p->pGroupBy);
2934adee20fSdanielk1977   sqlite3ExprDelete(p->pHaving);
2944adee20fSdanielk1977   sqlite3ExprListDelete(p->pOrderBy);
2954adee20fSdanielk1977   sqlite3SelectDelete(p->pPrior);
296a76b5dfcSdrh   sqliteFree(p->zSelect);
2979bb61fe7Sdrh   sqliteFree(p);
2989bb61fe7Sdrh }
2999bb61fe7Sdrh 
3009bb61fe7Sdrh /*
3012282792aSdrh ** Delete the aggregate information from the parse structure.
3022282792aSdrh */
3031d83f052Sdrh static void sqliteAggregateInfoReset(Parse *pParse){
3042282792aSdrh   sqliteFree(pParse->aAgg);
3052282792aSdrh   pParse->aAgg = 0;
3062282792aSdrh   pParse->nAgg = 0;
3072282792aSdrh   pParse->useAgg = 0;
3082282792aSdrh }
3092282792aSdrh 
3102282792aSdrh /*
311c926afbcSdrh ** Insert code into "v" that will push the record on the top of the
312c926afbcSdrh ** stack into the sorter.
313c926afbcSdrh */
314c926afbcSdrh static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
315c926afbcSdrh   int i;
316c926afbcSdrh   for(i=0; i<pOrderBy->nExpr; i++){
3174adee20fSdanielk1977     sqlite3ExprCode(pParse, pOrderBy->a[i].pExpr);
318c926afbcSdrh   }
319ededfd5eSdanielk1977   sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr, 0);
3204adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_SortPut, 0, 0);
321c926afbcSdrh }
322c926afbcSdrh 
323c926afbcSdrh /*
324ea48eb2eSdrh ** Add code to implement the OFFSET and LIMIT
325ea48eb2eSdrh */
326ea48eb2eSdrh static void codeLimiter(
327bab39e13Sdrh   Vdbe *v,          /* Generate code into this VM */
328ea48eb2eSdrh   Select *p,        /* The SELECT statement being coded */
329ea48eb2eSdrh   int iContinue,    /* Jump here to skip the current record */
330ea48eb2eSdrh   int iBreak,       /* Jump here to end the loop */
331ea48eb2eSdrh   int nPop          /* Number of times to pop stack when jumping */
332ea48eb2eSdrh ){
333ea48eb2eSdrh   if( p->iOffset>=0 ){
334ea48eb2eSdrh     int addr = sqlite3VdbeCurrentAddr(v) + 2;
335ea48eb2eSdrh     if( nPop>0 ) addr++;
336ea48eb2eSdrh     sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr);
337ea48eb2eSdrh     if( nPop>0 ){
338ea48eb2eSdrh       sqlite3VdbeAddOp(v, OP_Pop, nPop, 0);
339ea48eb2eSdrh     }
340ea48eb2eSdrh     sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
341ea48eb2eSdrh   }
342ea48eb2eSdrh   if( p->iLimit>=0 ){
343ea48eb2eSdrh     sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak);
344ea48eb2eSdrh   }
345ea48eb2eSdrh }
346ea48eb2eSdrh 
347ea48eb2eSdrh /*
3482282792aSdrh ** This routine generates the code for the inside of the inner loop
3492282792aSdrh ** of a SELECT.
35082c3d636Sdrh **
35138640e15Sdrh ** If srcTab and nColumn are both zero, then the pEList expressions
35238640e15Sdrh ** are evaluated in order to get the data for this row.  If nColumn>0
35338640e15Sdrh ** then data is pulled from srcTab and pEList is used only to get the
35438640e15Sdrh ** datatypes for each column.
3552282792aSdrh */
3562282792aSdrh static int selectInnerLoop(
3572282792aSdrh   Parse *pParse,          /* The parser context */
358df199a25Sdrh   Select *p,              /* The complete select statement being coded */
3592282792aSdrh   ExprList *pEList,       /* List of values being extracted */
36082c3d636Sdrh   int srcTab,             /* Pull data from this table */
361967e8b73Sdrh   int nColumn,            /* Number of columns in the source table */
3622282792aSdrh   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
3632282792aSdrh   int distinct,           /* If >=0, make sure results are distinct */
3642282792aSdrh   int eDest,              /* How to dispose of the results */
3652282792aSdrh   int iParm,              /* An argument to the disposal method */
3662282792aSdrh   int iContinue,          /* Jump here to continue with next row */
36784ac9d02Sdanielk1977   int iBreak,             /* Jump here to break out of the inner loop */
36884ac9d02Sdanielk1977   char *aff               /* affinity string if eDest is SRT_Union */
3692282792aSdrh ){
3702282792aSdrh   Vdbe *v = pParse->pVdbe;
3712282792aSdrh   int i;
372ea48eb2eSdrh   int hasDistinct;        /* True if the DISTINCT keyword is present */
37338640e15Sdrh 
374daffd0e5Sdrh   if( v==0 ) return 0;
37538640e15Sdrh   assert( pEList!=0 );
3762282792aSdrh 
377df199a25Sdrh   /* If there was a LIMIT clause on the SELECT statement, then do the check
378df199a25Sdrh   ** to see if this row should be output.
379df199a25Sdrh   */
380ea48eb2eSdrh   hasDistinct = distinct>=0 && pEList && pEList->nExpr>0;
381ea48eb2eSdrh   if( pOrderBy==0 && !hasDistinct ){
382bab39e13Sdrh     codeLimiter(v, p, iContinue, iBreak, 0);
383df199a25Sdrh   }
384df199a25Sdrh 
385967e8b73Sdrh   /* Pull the requested columns.
3862282792aSdrh   */
38738640e15Sdrh   if( nColumn>0 ){
388967e8b73Sdrh     for(i=0; i<nColumn; i++){
3894adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Column, srcTab, i);
39082c3d636Sdrh     }
39138640e15Sdrh   }else{
39238640e15Sdrh     nColumn = pEList->nExpr;
39338640e15Sdrh     for(i=0; i<pEList->nExpr; i++){
3944adee20fSdanielk1977       sqlite3ExprCode(pParse, pEList->a[i].pExpr);
39538640e15Sdrh     }
39682c3d636Sdrh   }
3972282792aSdrh 
398daffd0e5Sdrh   /* If the DISTINCT keyword was present on the SELECT statement
399daffd0e5Sdrh   ** and this row has been seen before, then do not make this row
400daffd0e5Sdrh   ** part of the result.
4012282792aSdrh   */
402ea48eb2eSdrh   if( hasDistinct ){
4030bd1f4eaSdrh #if NULL_ALWAYS_DISTINCT
4044adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7);
4050bd1f4eaSdrh #endif
406ededfd5eSdanielk1977     /* Deliberately leave the affinity string off of the following
407ededfd5eSdanielk1977     ** OP_MakeRecord */
408ededfd5eSdanielk1977     sqlite3VdbeAddOp(v, OP_MakeRecord, pEList->nExpr * -1, 0);
4094adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3);
4104adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
4114adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
4120f69c1e3Sdanielk1977     sqlite3VdbeAddOp(v, OP_String8, 0, 0);
4134adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_PutStrKey, distinct, 0);
414ea48eb2eSdrh     if( pOrderBy==0 ){
415bab39e13Sdrh       codeLimiter(v, p, iContinue, iBreak, nColumn);
416ea48eb2eSdrh     }
4172282792aSdrh   }
41882c3d636Sdrh 
419c926afbcSdrh   switch( eDest ){
42082c3d636Sdrh     /* In this mode, write each query result to the key of the temporary
42182c3d636Sdrh     ** table iParm.
4222282792aSdrh     */
423c926afbcSdrh     case SRT_Union: {
4244adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
42584ac9d02Sdanielk1977       sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
4260f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
4274adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_PutStrKey, iParm, 0);
428c926afbcSdrh       break;
429c926afbcSdrh     }
43082c3d636Sdrh 
4315974a30fSdrh     /* Store the result as data using a unique key.
4325974a30fSdrh     */
433c926afbcSdrh     case SRT_Table:
434c926afbcSdrh     case SRT_TempTable: {
4354adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
436c926afbcSdrh       if( pOrderBy ){
437c926afbcSdrh         pushOntoSorter(pParse, v, pOrderBy);
438c926afbcSdrh       }else{
4394adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0);
4404adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
4414adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0);
442c926afbcSdrh       }
443c926afbcSdrh       break;
444c926afbcSdrh     }
4455974a30fSdrh 
44682c3d636Sdrh     /* Construct a record from the query result, but instead of
44782c3d636Sdrh     ** saving that record, use it as a key to delete elements from
44882c3d636Sdrh     ** the temporary table iParm.
44982c3d636Sdrh     */
450c926afbcSdrh     case SRT_Except: {
4510bd1f4eaSdrh       int addr;
4524adee20fSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
45384ac9d02Sdanielk1977       sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
4544adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3);
4554adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Delete, iParm, 0);
456c926afbcSdrh       break;
457c926afbcSdrh     }
4582282792aSdrh 
4592282792aSdrh     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
4602282792aSdrh     ** then there should be a single item on the stack.  Write this
4612282792aSdrh     ** item into the set table with bogus data.
4622282792aSdrh     */
463c926afbcSdrh     case SRT_Set: {
4644adee20fSdanielk1977       int addr1 = sqlite3VdbeCurrentAddr(v);
46552b36cabSdrh       int addr2;
466e014a838Sdanielk1977 
467967e8b73Sdrh       assert( nColumn==1 );
4684adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3);
4694adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
4704adee20fSdanielk1977       addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
471c926afbcSdrh       if( pOrderBy ){
472c926afbcSdrh         pushOntoSorter(pParse, v, pOrderBy);
473c926afbcSdrh       }else{
474e014a838Sdanielk1977         char const *affStr;
475e014a838Sdanielk1977         char aff = (iParm>>16)&0xFF;
476e014a838Sdanielk1977         aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff);
477e014a838Sdanielk1977         affStr = sqlite3AffinityString(aff);
478ededfd5eSdanielk1977         sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
4790f69c1e3Sdanielk1977         sqlite3VdbeAddOp(v, OP_String8, 0, 0);
480e014a838Sdanielk1977         sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0);
481c926afbcSdrh       }
4824adee20fSdanielk1977       sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v));
483c926afbcSdrh       break;
484c926afbcSdrh     }
48582c3d636Sdrh 
4862282792aSdrh     /* If this is a scalar select that is part of an expression, then
4872282792aSdrh     ** store the results in the appropriate memory cell and break out
4882282792aSdrh     ** of the scan loop.
4892282792aSdrh     */
490c926afbcSdrh     case SRT_Mem: {
491967e8b73Sdrh       assert( nColumn==1 );
492c926afbcSdrh       if( pOrderBy ){
493c926afbcSdrh         pushOntoSorter(pParse, v, pOrderBy);
494c926afbcSdrh       }else{
4954adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
4964adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Goto, 0, iBreak);
497c926afbcSdrh       }
498c926afbcSdrh       break;
499c926afbcSdrh     }
5002282792aSdrh 
501f46f905aSdrh     /* Send the data to the callback function.
502f46f905aSdrh     */
503f46f905aSdrh     case SRT_Callback:
504f46f905aSdrh     case SRT_Sorter: {
505f46f905aSdrh       if( pOrderBy ){
506ce665cf6Sdrh         sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
507f46f905aSdrh         pushOntoSorter(pParse, v, pOrderBy);
508f46f905aSdrh       }else{
509f46f905aSdrh         assert( eDest==SRT_Callback );
5104adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
511f46f905aSdrh       }
512f46f905aSdrh       break;
513f46f905aSdrh     }
514f46f905aSdrh 
515142e30dfSdrh     /* Invoke a subroutine to handle the results.  The subroutine itself
516142e30dfSdrh     ** is responsible for popping the results off of the stack.
517142e30dfSdrh     */
518142e30dfSdrh     case SRT_Subroutine: {
519ac82fcf5Sdrh       if( pOrderBy ){
5204adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
521ac82fcf5Sdrh         pushOntoSorter(pParse, v, pOrderBy);
522ac82fcf5Sdrh       }else{
5234adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
524ac82fcf5Sdrh       }
525142e30dfSdrh       break;
526142e30dfSdrh     }
527142e30dfSdrh 
528d7489c39Sdrh     /* Discard the results.  This is used for SELECT statements inside
529d7489c39Sdrh     ** the body of a TRIGGER.  The purpose of such selects is to call
530d7489c39Sdrh     ** user-defined functions that have side effects.  We do not care
531d7489c39Sdrh     ** about the actual results of the select.
532d7489c39Sdrh     */
533c926afbcSdrh     default: {
534f46f905aSdrh       assert( eDest==SRT_Discard );
5354adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
536c926afbcSdrh       break;
537c926afbcSdrh     }
538c926afbcSdrh   }
53982c3d636Sdrh   return 0;
54082c3d636Sdrh }
54182c3d636Sdrh 
54282c3d636Sdrh /*
543d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument,
544d8bc7086Sdrh ** then the results were placed in a sorter.  After the loop is terminated
545d8bc7086Sdrh ** we need to run the sorter and output the results.  The following
546d8bc7086Sdrh ** routine generates the code needed to do that.
547d8bc7086Sdrh */
548c926afbcSdrh static void generateSortTail(
549ffbc3088Sdrh   Parse *pParse,   /* The parsing context */
550c926afbcSdrh   Select *p,       /* The SELECT statement */
551c926afbcSdrh   Vdbe *v,         /* Generate code into this VDBE */
552c926afbcSdrh   int nColumn,     /* Number of columns of data */
553c926afbcSdrh   int eDest,       /* Write the sorted results here */
554c926afbcSdrh   int iParm        /* Optional parameter associated with eDest */
555c926afbcSdrh ){
5564adee20fSdanielk1977   int end1 = sqlite3VdbeMakeLabel(v);
5574adee20fSdanielk1977   int end2 = sqlite3VdbeMakeLabel(v);
558d8bc7086Sdrh   int addr;
559ffbc3088Sdrh   KeyInfo *pInfo;
560ffbc3088Sdrh   ExprList *pOrderBy;
561ffbc3088Sdrh   int nCol, i;
562ffbc3088Sdrh   sqlite *db = pParse->db;
563ffbc3088Sdrh 
564f46f905aSdrh   if( eDest==SRT_Sorter ) return;
565ffbc3088Sdrh   pOrderBy = p->pOrderBy;
566ffbc3088Sdrh   nCol = pOrderBy->nExpr;
567ffbc3088Sdrh   pInfo = sqliteMalloc( sizeof(*pInfo) + nCol*(sizeof(CollSeq*)+1) );
568ffbc3088Sdrh   if( pInfo==0 ) return;
569ffbc3088Sdrh   pInfo->aSortOrder = (char*)&pInfo->aColl[nCol];
570ffbc3088Sdrh   pInfo->nField = nCol;
571ffbc3088Sdrh   for(i=0; i<nCol; i++){
5720202b29eSdanielk1977     /* If a collation sequence was specified explicity, then it
5730202b29eSdanielk1977     ** is stored in pOrderBy->a[i].zName. Otherwise, use the default
5740202b29eSdanielk1977     ** collation type for the expression.
5750202b29eSdanielk1977     */
5767cedc8d4Sdanielk1977     pInfo->aColl[i] = sqlite3ExprCollSeq(pParse, pOrderBy->a[i].pExpr);
5770202b29eSdanielk1977     if( !pInfo->aColl[i] ){
578ffbc3088Sdrh       pInfo->aColl[i] = db->pDfltColl;
5790202b29eSdanielk1977     }
580ffbc3088Sdrh     pInfo->aSortOrder[i] = pOrderBy->a[i].sortOrder;
581ffbc3088Sdrh   }
582ffbc3088Sdrh   sqlite3VdbeOp3(v, OP_Sort, 0, 0, (char*)pInfo, P3_KEYINFO_HANDOFF);
5834adee20fSdanielk1977   addr = sqlite3VdbeAddOp(v, OP_SortNext, 0, end1);
584bab39e13Sdrh   codeLimiter(v, p, addr, end2, 1);
585c926afbcSdrh   switch( eDest ){
586c926afbcSdrh     case SRT_Table:
587c926afbcSdrh     case SRT_TempTable: {
5884adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0);
5894adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
5904adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0);
591c926afbcSdrh       break;
592c926afbcSdrh     }
593c926afbcSdrh     case SRT_Set: {
594c926afbcSdrh       assert( nColumn==1 );
5954adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
5964adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
5974adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
598ededfd5eSdanielk1977       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, "n", P3_STATIC);
5990f69c1e3Sdanielk1977       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
600e014a838Sdanielk1977       sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0);
601c926afbcSdrh       break;
602c926afbcSdrh     }
603c926afbcSdrh     case SRT_Mem: {
604c926afbcSdrh       assert( nColumn==1 );
6054adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
6064adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Goto, 0, end1);
607c926afbcSdrh       break;
608c926afbcSdrh     }
609ce665cf6Sdrh     case SRT_Callback:
610ac82fcf5Sdrh     case SRT_Subroutine: {
611ac82fcf5Sdrh       int i;
61284ac9d02Sdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0);
61384ac9d02Sdanielk1977       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
614ac82fcf5Sdrh       for(i=0; i<nColumn; i++){
6154adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Column, -1-i, i);
616ac82fcf5Sdrh       }
617ce665cf6Sdrh       if( eDest==SRT_Callback ){
618ce665cf6Sdrh         sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
619ce665cf6Sdrh       }else{
6204adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
621ce665cf6Sdrh       }
62284ac9d02Sdanielk1977       sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
623ac82fcf5Sdrh       break;
624ac82fcf5Sdrh     }
625c926afbcSdrh     default: {
626f46f905aSdrh       /* Do nothing */
627c926afbcSdrh       break;
628c926afbcSdrh     }
629c926afbcSdrh   }
6304adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
6314adee20fSdanielk1977   sqlite3VdbeResolveLabel(v, end2);
6324adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
6334adee20fSdanielk1977   sqlite3VdbeResolveLabel(v, end1);
6344adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_SortReset, 0, 0);
635d8bc7086Sdrh }
636d8bc7086Sdrh 
637d8bc7086Sdrh /*
638517eb646Sdanielk1977 ** Return a pointer to a string containing the 'declaration type' of the
639517eb646Sdanielk1977 ** expression pExpr. The string may be treated as static by the caller.
640e78e8284Sdrh **
641517eb646Sdanielk1977 ** If the declaration type is the exact datatype definition extracted from
642517eb646Sdanielk1977 ** the original CREATE TABLE statement if the expression is a column.
643e78e8284Sdrh **
644517eb646Sdanielk1977 ** The declaration type for an expression is either TEXT, NUMERIC or ANY.
645517eb646Sdanielk1977 ** The declaration type for a ROWID field is INTEGER.
646fcb78a49Sdrh */
647517eb646Sdanielk1977 static const char *columnType(Parse *pParse, SrcList *pTabList, Expr *pExpr){
64800e279d9Sdanielk1977   char const *zType;
649517eb646Sdanielk1977   int j;
65000e279d9Sdanielk1977   if( pExpr==0 || pTabList==0 ) return 0;
65100e279d9Sdanielk1977 
65200e279d9Sdanielk1977   switch( pExpr->op ){
65300e279d9Sdanielk1977     case TK_COLUMN: {
6546a3ea0e6Sdrh       Table *pTab;
655517eb646Sdanielk1977       int iCol = pExpr->iColumn;
656517eb646Sdanielk1977       for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable; j++){}
6576a3ea0e6Sdrh       assert( j<pTabList->nSrc );
6586a3ea0e6Sdrh       pTab = pTabList->a[j].pTab;
659fcb78a49Sdrh       if( iCol<0 ) iCol = pTab->iPKey;
660fcb78a49Sdrh       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
661fcb78a49Sdrh       if( iCol<0 ){
662fcb78a49Sdrh         zType = "INTEGER";
663fcb78a49Sdrh       }else{
664fcb78a49Sdrh         zType = pTab->aCol[iCol].zType;
665fcb78a49Sdrh       }
66600e279d9Sdanielk1977       break;
667736c22b8Sdrh     }
66800e279d9Sdanielk1977     case TK_AS:
66900e279d9Sdanielk1977       zType = columnType(pParse, pTabList, pExpr->pLeft);
67000e279d9Sdanielk1977       break;
67100e279d9Sdanielk1977     case TK_SELECT: {
67200e279d9Sdanielk1977       Select *pS = pExpr->pSelect;
67300e279d9Sdanielk1977       zType = columnType(pParse, pS->pSrc, pS->pEList->a[0].pExpr);
67400e279d9Sdanielk1977       break;
675fcb78a49Sdrh     }
67600e279d9Sdanielk1977     default:
67700e279d9Sdanielk1977       zType = 0;
67800e279d9Sdanielk1977   }
67900e279d9Sdanielk1977 
680517eb646Sdanielk1977   return zType;
681517eb646Sdanielk1977 }
682517eb646Sdanielk1977 
683517eb646Sdanielk1977 /*
684517eb646Sdanielk1977 ** Generate code that will tell the VDBE the declaration types of columns
685517eb646Sdanielk1977 ** in the result set.
686517eb646Sdanielk1977 */
687517eb646Sdanielk1977 static void generateColumnTypes(
688517eb646Sdanielk1977   Parse *pParse,      /* Parser context */
689517eb646Sdanielk1977   SrcList *pTabList,  /* List of tables */
690517eb646Sdanielk1977   ExprList *pEList    /* Expressions defining the result set */
691517eb646Sdanielk1977 ){
692517eb646Sdanielk1977   Vdbe *v = pParse->pVdbe;
693517eb646Sdanielk1977   int i;
694517eb646Sdanielk1977   for(i=0; i<pEList->nExpr; i++){
695517eb646Sdanielk1977     Expr *p = pEList->a[i].pExpr;
696517eb646Sdanielk1977     const char *zType = columnType(pParse, pTabList, p);
69700e279d9Sdanielk1977     if( zType==0 ) continue;
698fbcd585fSdanielk1977     /* The vdbe must make it's own copy of the column-type, in case the
699fbcd585fSdanielk1977     ** schema is reset before this virtual machine is deleted.
700fbcd585fSdanielk1977     */
701fbcd585fSdanielk1977     sqlite3VdbeSetColName(v, i+pEList->nExpr, zType, strlen(zType));
702fcb78a49Sdrh   }
703fcb78a49Sdrh }
704fcb78a49Sdrh 
705fcb78a49Sdrh /*
706fcb78a49Sdrh ** Generate code that will tell the VDBE the names of columns
707fcb78a49Sdrh ** in the result set.  This information is used to provide the
708fcabd464Sdrh ** azCol[] values in the callback.
70982c3d636Sdrh */
710832508b7Sdrh static void generateColumnNames(
711832508b7Sdrh   Parse *pParse,      /* Parser context */
712ad3cab52Sdrh   SrcList *pTabList,  /* List of tables */
713832508b7Sdrh   ExprList *pEList    /* Expressions defining the result set */
714832508b7Sdrh ){
715d8bc7086Sdrh   Vdbe *v = pParse->pVdbe;
7166a3ea0e6Sdrh   int i, j;
717fcabd464Sdrh   sqlite *db = pParse->db;
718fcabd464Sdrh   int fullNames, shortNames;
719fcabd464Sdrh 
7203cf86063Sdanielk1977   /* If this is an EXPLAIN, skip this step */
7213cf86063Sdanielk1977   if( pParse->explain ){
72261de0d1bSdanielk1977     return;
7233cf86063Sdanielk1977   }
7243cf86063Sdanielk1977 
725d6502758Sdrh   assert( v!=0 );
7266f8a503dSdanielk1977   if( pParse->colNamesSet || v==0 || sqlite3_malloc_failed ) return;
727d8bc7086Sdrh   pParse->colNamesSet = 1;
728fcabd464Sdrh   fullNames = (db->flags & SQLITE_FullColNames)!=0;
729fcabd464Sdrh   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
73022322fd4Sdanielk1977   sqlite3VdbeSetNumCols(v, pEList->nExpr);
73182c3d636Sdrh   for(i=0; i<pEList->nExpr; i++){
73282c3d636Sdrh     Expr *p;
7335a38705eSdrh     p = pEList->a[i].pExpr;
7345a38705eSdrh     if( p==0 ) continue;
73582c3d636Sdrh     if( pEList->a[i].zName ){
73682c3d636Sdrh       char *zName = pEList->a[i].zName;
737d8123366Sdanielk1977       sqlite3VdbeSetColName(v, i, zName, strlen(zName));
73882c3d636Sdrh       continue;
73982c3d636Sdrh     }
740fa173a76Sdrh     if( p->op==TK_COLUMN && pTabList ){
7416a3ea0e6Sdrh       Table *pTab;
74297665873Sdrh       char *zCol;
7438aff1015Sdrh       int iCol = p->iColumn;
7446a3ea0e6Sdrh       for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
7456a3ea0e6Sdrh       assert( j<pTabList->nSrc );
7466a3ea0e6Sdrh       pTab = pTabList->a[j].pTab;
7478aff1015Sdrh       if( iCol<0 ) iCol = pTab->iPKey;
74897665873Sdrh       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
749b1363206Sdrh       if( iCol<0 ){
750b1363206Sdrh         zCol = "_ROWID_";
751b1363206Sdrh       }else{
752b1363206Sdrh         zCol = pTab->aCol[iCol].zName;
753b1363206Sdrh       }
754fcabd464Sdrh       if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){
7553cf86063Sdanielk1977         sqlite3VdbeSetColName(v, i, p->span.z, p->span.n);
756fcabd464Sdrh       }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){
75782c3d636Sdrh         char *zName = 0;
75882c3d636Sdrh         char *zTab;
75982c3d636Sdrh 
7606a3ea0e6Sdrh         zTab = pTabList->a[j].zAlias;
761fcabd464Sdrh         if( fullNames || zTab==0 ) zTab = pTab->zName;
7624adee20fSdanielk1977         sqlite3SetString(&zName, zTab, ".", zCol, 0);
7633cf86063Sdanielk1977         sqlite3VdbeSetColName(v, i, zName, P3_DYNAMIC);
76482c3d636Sdrh       }else{
7653cf86063Sdanielk1977         sqlite3VdbeSetColName(v, i, zCol, 0);
76682c3d636Sdrh       }
7676977fea8Sdrh     }else if( p->span.z && p->span.z[0] ){
7683cf86063Sdanielk1977       sqlite3VdbeSetColName(v, i, p->span.z, p->span.n);
7693cf86063Sdanielk1977       /* sqlite3VdbeCompressSpace(v, addr); */
7701bee3d7bSdrh     }else{
7711bee3d7bSdrh       char zName[30];
7721bee3d7bSdrh       assert( p->op!=TK_COLUMN || pTabList==0 );
7731bee3d7bSdrh       sprintf(zName, "column%d", i+1);
7743cf86063Sdanielk1977       sqlite3VdbeSetColName(v, i, zName, 0);
77582c3d636Sdrh     }
77682c3d636Sdrh   }
77776d505baSdanielk1977   generateColumnTypes(pParse, pTabList, pEList);
7785080aaa7Sdrh }
77982c3d636Sdrh 
78082c3d636Sdrh /*
781d8bc7086Sdrh ** Name of the connection operator, used for error messages.
782d8bc7086Sdrh */
783d8bc7086Sdrh static const char *selectOpName(int id){
784d8bc7086Sdrh   char *z;
785d8bc7086Sdrh   switch( id ){
786d8bc7086Sdrh     case TK_ALL:       z = "UNION ALL";   break;
787d8bc7086Sdrh     case TK_INTERSECT: z = "INTERSECT";   break;
788d8bc7086Sdrh     case TK_EXCEPT:    z = "EXCEPT";      break;
789d8bc7086Sdrh     default:           z = "UNION";       break;
790d8bc7086Sdrh   }
791d8bc7086Sdrh   return z;
792d8bc7086Sdrh }
793d8bc7086Sdrh 
794d8bc7086Sdrh /*
795315555caSdrh ** Forward declaration
796315555caSdrh */
797315555caSdrh static int fillInColumnList(Parse*, Select*);
798315555caSdrh 
799315555caSdrh /*
80022f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes
80122f70c32Sdrh ** the result set of that SELECT.
80222f70c32Sdrh */
8034adee20fSdanielk1977 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
80422f70c32Sdrh   Table *pTab;
805b733d037Sdrh   int i, j;
80622f70c32Sdrh   ExprList *pEList;
807b733d037Sdrh   Column *aCol;
80822f70c32Sdrh 
80922f70c32Sdrh   if( fillInColumnList(pParse, pSelect) ){
81022f70c32Sdrh     return 0;
81122f70c32Sdrh   }
81222f70c32Sdrh   pTab = sqliteMalloc( sizeof(Table) );
81322f70c32Sdrh   if( pTab==0 ){
81422f70c32Sdrh     return 0;
81522f70c32Sdrh   }
81622f70c32Sdrh   pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
81722f70c32Sdrh   pEList = pSelect->pEList;
81822f70c32Sdrh   pTab->nCol = pEList->nExpr;
819417be79cSdrh   assert( pTab->nCol>0 );
820b733d037Sdrh   pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
82122f70c32Sdrh   for(i=0; i<pTab->nCol; i++){
822517eb646Sdanielk1977     Expr *pR;
823517eb646Sdanielk1977     char *zType;
824517eb646Sdanielk1977     Expr *p = pEList->a[i].pExpr;
82522f70c32Sdrh     if( pEList->a[i].zName ){
826b733d037Sdrh       aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
827517eb646Sdanielk1977     }else if( p->op==TK_DOT
828b733d037Sdrh                && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
829b733d037Sdrh       int cnt;
8304adee20fSdanielk1977       sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0);
831b733d037Sdrh       for(j=cnt=0; j<i; j++){
8324adee20fSdanielk1977         if( sqlite3StrICmp(aCol[j].zName, aCol[i].zName)==0 ){
833b733d037Sdrh           int n;
834b733d037Sdrh           char zBuf[30];
835b733d037Sdrh           sprintf(zBuf,"_%d",++cnt);
836b733d037Sdrh           n = strlen(zBuf);
8374adee20fSdanielk1977           sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf,n,0);
838b733d037Sdrh           j = -1;
839b733d037Sdrh         }
840b733d037Sdrh       }
841b733d037Sdrh     }else if( p->span.z && p->span.z[0] ){
8424adee20fSdanielk1977       sqlite3SetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
84322f70c32Sdrh     }else{
84422f70c32Sdrh       char zBuf[30];
84522f70c32Sdrh       sprintf(zBuf, "column%d", i+1);
84622f70c32Sdrh       pTab->aCol[i].zName = sqliteStrDup(zBuf);
84722f70c32Sdrh     }
8482c61c070Sdrh     sqlite3Dequote(aCol[i].zName);
849e014a838Sdanielk1977 
850517eb646Sdanielk1977     zType = sqliteStrDup(columnType(pParse, pSelect->pSrc ,p));
851517eb646Sdanielk1977     pTab->aCol[i].zType = zType;
852517eb646Sdanielk1977     pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
853517eb646Sdanielk1977     if( zType ){
854517eb646Sdanielk1977       pTab->aCol[i].affinity = sqlite3AffinityType(zType, strlen(zType));
855517eb646Sdanielk1977     }
8567cedc8d4Sdanielk1977     pTab->aCol[i].pColl = sqlite3ExprCollSeq(pParse, p);
8570202b29eSdanielk1977     if( !pTab->aCol[i].pColl ){
8580202b29eSdanielk1977       pTab->aCol[i].pColl = pParse->db->pDfltColl;
8590202b29eSdanielk1977     }
86022f70c32Sdrh   }
86122f70c32Sdrh   pTab->iPKey = -1;
86222f70c32Sdrh   return pTab;
86322f70c32Sdrh }
86422f70c32Sdrh 
86522f70c32Sdrh /*
866ad2d8307Sdrh ** For the given SELECT statement, do three things.
867d8bc7086Sdrh **
868ad3cab52Sdrh **    (1)  Fill in the pTabList->a[].pTab fields in the SrcList that
86963eb5f29Sdrh **         defines the set of tables that should be scanned.  For views,
87063eb5f29Sdrh **         fill pTabList->a[].pSelect with a copy of the SELECT statement
87163eb5f29Sdrh **         that implements the view.  A copy is made of the view's SELECT
87263eb5f29Sdrh **         statement so that we can freely modify or delete that statement
87363eb5f29Sdrh **         without worrying about messing up the presistent representation
87463eb5f29Sdrh **         of the view.
875d8bc7086Sdrh **
876ad2d8307Sdrh **    (2)  Add terms to the WHERE clause to accomodate the NATURAL keyword
877ad2d8307Sdrh **         on joins and the ON and USING clause of joins.
878ad2d8307Sdrh **
879ad2d8307Sdrh **    (3)  Scan the list of columns in the result set (pEList) looking
88054473229Sdrh **         for instances of the "*" operator or the TABLE.* operator.
88154473229Sdrh **         If found, expand each "*" to be every column in every table
88254473229Sdrh **         and TABLE.* to be every column in TABLE.
883d8bc7086Sdrh **
884d8bc7086Sdrh ** Return 0 on success.  If there are problems, leave an error message
885d8bc7086Sdrh ** in pParse and return non-zero.
886d8bc7086Sdrh */
887d8bc7086Sdrh static int fillInColumnList(Parse *pParse, Select *p){
88854473229Sdrh   int i, j, k, rc;
889ad3cab52Sdrh   SrcList *pTabList;
890daffd0e5Sdrh   ExprList *pEList;
891a76b5dfcSdrh   Table *pTab;
892daffd0e5Sdrh 
893daffd0e5Sdrh   if( p==0 || p->pSrc==0 ) return 1;
894daffd0e5Sdrh   pTabList = p->pSrc;
895daffd0e5Sdrh   pEList = p->pEList;
896d8bc7086Sdrh 
897d8bc7086Sdrh   /* Look up every table in the table list.
898d8bc7086Sdrh   */
899ad3cab52Sdrh   for(i=0; i<pTabList->nSrc; i++){
900d8bc7086Sdrh     if( pTabList->a[i].pTab ){
901d8bc7086Sdrh       /* This routine has run before!  No need to continue */
902d8bc7086Sdrh       return 0;
903d8bc7086Sdrh     }
904daffd0e5Sdrh     if( pTabList->a[i].zName==0 ){
90522f70c32Sdrh       /* A sub-query in the FROM clause of a SELECT */
90622f70c32Sdrh       assert( pTabList->a[i].pSelect!=0 );
907ad2d8307Sdrh       if( pTabList->a[i].zAlias==0 ){
908ad2d8307Sdrh         char zFakeName[60];
909ad2d8307Sdrh         sprintf(zFakeName, "sqlite_subquery_%p_",
910ad2d8307Sdrh            (void*)pTabList->a[i].pSelect);
9114adee20fSdanielk1977         sqlite3SetString(&pTabList->a[i].zAlias, zFakeName, 0);
912ad2d8307Sdrh       }
91322f70c32Sdrh       pTabList->a[i].pTab = pTab =
9144adee20fSdanielk1977         sqlite3ResultSetOfSelect(pParse, pTabList->a[i].zAlias,
91522f70c32Sdrh                                         pTabList->a[i].pSelect);
91622f70c32Sdrh       if( pTab==0 ){
917daffd0e5Sdrh         return 1;
918daffd0e5Sdrh       }
9195cf590c1Sdrh       /* The isTransient flag indicates that the Table structure has been
9205cf590c1Sdrh       ** dynamically allocated and may be freed at any time.  In other words,
9215cf590c1Sdrh       ** pTab is not pointing to a persistent table structure that defines
9225cf590c1Sdrh       ** part of the schema. */
92322f70c32Sdrh       pTab->isTransient = 1;
92422f70c32Sdrh     }else{
925a76b5dfcSdrh       /* An ordinary table or view name in the FROM clause */
926a76b5dfcSdrh       pTabList->a[i].pTab = pTab =
9274adee20fSdanielk1977         sqlite3LocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase);
928a76b5dfcSdrh       if( pTab==0 ){
929d8bc7086Sdrh         return 1;
930d8bc7086Sdrh       }
931a76b5dfcSdrh       if( pTab->pSelect ){
93263eb5f29Sdrh         /* We reach here if the named table is a really a view */
9334adee20fSdanielk1977         if( sqlite3ViewGetColumnNames(pParse, pTab) ){
934417be79cSdrh           return 1;
935417be79cSdrh         }
93663eb5f29Sdrh         /* If pTabList->a[i].pSelect!=0 it means we are dealing with a
93763eb5f29Sdrh         ** view within a view.  The SELECT structure has already been
93863eb5f29Sdrh         ** copied by the outer view so we can skip the copy step here
93963eb5f29Sdrh         ** in the inner view.
94063eb5f29Sdrh         */
94163eb5f29Sdrh         if( pTabList->a[i].pSelect==0 ){
9424adee20fSdanielk1977           pTabList->a[i].pSelect = sqlite3SelectDup(pTab->pSelect);
943a76b5dfcSdrh         }
944d8bc7086Sdrh       }
94522f70c32Sdrh     }
94663eb5f29Sdrh   }
947d8bc7086Sdrh 
948ad2d8307Sdrh   /* Process NATURAL keywords, and ON and USING clauses of joins.
949ad2d8307Sdrh   */
950ad2d8307Sdrh   if( sqliteProcessJoin(pParse, p) ) return 1;
951ad2d8307Sdrh 
9527c917d19Sdrh   /* For every "*" that occurs in the column list, insert the names of
95354473229Sdrh   ** all columns in all tables.  And for every TABLE.* insert the names
95454473229Sdrh   ** of all columns in TABLE.  The parser inserted a special expression
9557c917d19Sdrh   ** with the TK_ALL operator for each "*" that it found in the column list.
9567c917d19Sdrh   ** The following code just has to locate the TK_ALL expressions and expand
9577c917d19Sdrh   ** each one to the list of all columns in all tables.
95854473229Sdrh   **
95954473229Sdrh   ** The first loop just checks to see if there are any "*" operators
96054473229Sdrh   ** that need expanding.
961d8bc7086Sdrh   */
9627c917d19Sdrh   for(k=0; k<pEList->nExpr; k++){
96354473229Sdrh     Expr *pE = pEList->a[k].pExpr;
96454473229Sdrh     if( pE->op==TK_ALL ) break;
96554473229Sdrh     if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
96654473229Sdrh          && pE->pLeft && pE->pLeft->op==TK_ID ) break;
9677c917d19Sdrh   }
96854473229Sdrh   rc = 0;
9697c917d19Sdrh   if( k<pEList->nExpr ){
97054473229Sdrh     /*
97154473229Sdrh     ** If we get here it means the result set contains one or more "*"
97254473229Sdrh     ** operators that need to be expanded.  Loop through each expression
97354473229Sdrh     ** in the result set and expand them one by one.
97454473229Sdrh     */
9757c917d19Sdrh     struct ExprList_item *a = pEList->a;
9767c917d19Sdrh     ExprList *pNew = 0;
9777c917d19Sdrh     for(k=0; k<pEList->nExpr; k++){
97854473229Sdrh       Expr *pE = a[k].pExpr;
97954473229Sdrh       if( pE->op!=TK_ALL &&
98054473229Sdrh            (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
98154473229Sdrh         /* This particular expression does not need to be expanded.
98254473229Sdrh         */
9834adee20fSdanielk1977         pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0);
9847c917d19Sdrh         pNew->a[pNew->nExpr-1].zName = a[k].zName;
9857c917d19Sdrh         a[k].pExpr = 0;
9867c917d19Sdrh         a[k].zName = 0;
9877c917d19Sdrh       }else{
98854473229Sdrh         /* This expression is a "*" or a "TABLE.*" and needs to be
98954473229Sdrh         ** expanded. */
99054473229Sdrh         int tableSeen = 0;      /* Set to 1 when TABLE matches */
991*cf55b7aeSdrh         char *zTName;            /* text of name of TABLE */
99254473229Sdrh         if( pE->op==TK_DOT && pE->pLeft ){
993*cf55b7aeSdrh           zTName = sqlite3NameFromToken(&pE->pLeft->token);
99454473229Sdrh         }else{
995*cf55b7aeSdrh           zTName = 0;
99654473229Sdrh         }
997ad3cab52Sdrh         for(i=0; i<pTabList->nSrc; i++){
998d8bc7086Sdrh           Table *pTab = pTabList->a[i].pTab;
99954473229Sdrh           char *zTabName = pTabList->a[i].zAlias;
100054473229Sdrh           if( zTabName==0 || zTabName[0]==0 ){
100154473229Sdrh             zTabName = pTab->zName;
100254473229Sdrh           }
1003*cf55b7aeSdrh           if( zTName && (zTabName==0 || zTabName[0]==0 ||
1004*cf55b7aeSdrh                  sqlite3StrICmp(zTName, zTabName)!=0) ){
100554473229Sdrh             continue;
100654473229Sdrh           }
100754473229Sdrh           tableSeen = 1;
1008d8bc7086Sdrh           for(j=0; j<pTab->nCol; j++){
100922f70c32Sdrh             Expr *pExpr, *pLeft, *pRight;
1010ad2d8307Sdrh             char *zName = pTab->aCol[j].zName;
1011ad2d8307Sdrh 
1012ad2d8307Sdrh             if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
1013ad2d8307Sdrh                 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
1014ad2d8307Sdrh               /* In a NATURAL join, omit the join columns from the
1015ad2d8307Sdrh               ** table on the right */
1016ad2d8307Sdrh               continue;
1017ad2d8307Sdrh             }
10184adee20fSdanielk1977             if( i>0 && sqlite3IdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
1019ad2d8307Sdrh               /* In a join with a USING clause, omit columns in the
1020ad2d8307Sdrh               ** using clause from the table on the right. */
1021ad2d8307Sdrh               continue;
1022ad2d8307Sdrh             }
10234adee20fSdanielk1977             pRight = sqlite3Expr(TK_ID, 0, 0, 0);
102422f70c32Sdrh             if( pRight==0 ) break;
1025ad2d8307Sdrh             pRight->token.z = zName;
1026ad2d8307Sdrh             pRight->token.n = strlen(zName);
10274b59ab5eSdrh             pRight->token.dyn = 0;
10284b59ab5eSdrh             if( zTabName && pTabList->nSrc>1 ){
10294adee20fSdanielk1977               pLeft = sqlite3Expr(TK_ID, 0, 0, 0);
10304adee20fSdanielk1977               pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0);
103122f70c32Sdrh               if( pExpr==0 ) break;
10324b59ab5eSdrh               pLeft->token.z = zTabName;
10334b59ab5eSdrh               pLeft->token.n = strlen(zTabName);
10344b59ab5eSdrh               pLeft->token.dyn = 0;
10354adee20fSdanielk1977               sqlite3SetString((char**)&pExpr->span.z, zTabName, ".", zName, 0);
10366977fea8Sdrh               pExpr->span.n = strlen(pExpr->span.z);
10376977fea8Sdrh               pExpr->span.dyn = 1;
10386977fea8Sdrh               pExpr->token.z = 0;
10396977fea8Sdrh               pExpr->token.n = 0;
10406977fea8Sdrh               pExpr->token.dyn = 0;
104122f70c32Sdrh             }else{
104222f70c32Sdrh               pExpr = pRight;
10436977fea8Sdrh               pExpr->span = pExpr->token;
104422f70c32Sdrh             }
10454adee20fSdanielk1977             pNew = sqlite3ExprListAppend(pNew, pExpr, 0);
1046d8bc7086Sdrh           }
1047d8bc7086Sdrh         }
104854473229Sdrh         if( !tableSeen ){
1049*cf55b7aeSdrh           if( zTName ){
1050*cf55b7aeSdrh             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
1051f5db2d3eSdrh           }else{
10524adee20fSdanielk1977             sqlite3ErrorMsg(pParse, "no tables specified");
1053f5db2d3eSdrh           }
105454473229Sdrh           rc = 1;
105554473229Sdrh         }
1056*cf55b7aeSdrh         sqliteFree(zTName);
10577c917d19Sdrh       }
10587c917d19Sdrh     }
10594adee20fSdanielk1977     sqlite3ExprListDelete(pEList);
10607c917d19Sdrh     p->pEList = pNew;
1061d8bc7086Sdrh   }
106254473229Sdrh   return rc;
1063d8bc7086Sdrh }
1064d8bc7086Sdrh 
1065d8bc7086Sdrh /*
1066ff78bd2fSdrh ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
1067ff78bd2fSdrh ** in a select structure.  It just sets the pointers to NULL.  This
1068ff78bd2fSdrh ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
1069ff78bd2fSdrh ** pointer is not NULL, this routine is called recursively on that pointer.
1070ff78bd2fSdrh **
1071ff78bd2fSdrh ** This routine is called on the Select structure that defines a
1072ff78bd2fSdrh ** VIEW in order to undo any bindings to tables.  This is necessary
1073ff78bd2fSdrh ** because those tables might be DROPed by a subsequent SQL command.
10745cf590c1Sdrh ** If the bindings are not removed, then the Select.pSrc->a[].pTab field
10755cf590c1Sdrh ** will be left pointing to a deallocated Table structure after the
10765cf590c1Sdrh ** DROP and a coredump will occur the next time the VIEW is used.
1077ff78bd2fSdrh */
10784adee20fSdanielk1977 void sqlite3SelectUnbind(Select *p){
1079ff78bd2fSdrh   int i;
1080ad3cab52Sdrh   SrcList *pSrc = p->pSrc;
1081ff78bd2fSdrh   Table *pTab;
1082ff78bd2fSdrh   if( p==0 ) return;
1083ad3cab52Sdrh   for(i=0; i<pSrc->nSrc; i++){
1084ff78bd2fSdrh     if( (pTab = pSrc->a[i].pTab)!=0 ){
1085ff78bd2fSdrh       if( pTab->isTransient ){
10864adee20fSdanielk1977         sqlite3DeleteTable(0, pTab);
1087ff78bd2fSdrh       }
1088ff78bd2fSdrh       pSrc->a[i].pTab = 0;
1089ff78bd2fSdrh       if( pSrc->a[i].pSelect ){
10904adee20fSdanielk1977         sqlite3SelectUnbind(pSrc->a[i].pSelect);
1091ff78bd2fSdrh       }
1092ff78bd2fSdrh     }
1093ff78bd2fSdrh   }
1094ff78bd2fSdrh }
1095ff78bd2fSdrh 
1096ff78bd2fSdrh /*
1097d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with
1098d8bc7086Sdrh ** columns in a result.  For each ORDER BY expression, the opcode of
1099967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of
1100d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable
1101d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter.
1102d8bc7086Sdrh **
1103d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first.  A match
1104d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT.
1105d8bc7086Sdrh **
1106d8bc7086Sdrh ** Any entry that does not match is flagged as an error.  The number
1107d8bc7086Sdrh ** of errors is returned.
1108d8bc7086Sdrh */
1109d8bc7086Sdrh static int matchOrderbyToColumn(
1110d8bc7086Sdrh   Parse *pParse,          /* A place to leave error messages */
1111d8bc7086Sdrh   Select *pSelect,        /* Match to result columns of this SELECT */
1112d8bc7086Sdrh   ExprList *pOrderBy,     /* The ORDER BY values to match against columns */
1113e4de1febSdrh   int iTable,             /* Insert this value in iTable */
1114d8bc7086Sdrh   int mustComplete        /* If TRUE all ORDER BYs must match */
1115d8bc7086Sdrh ){
1116d8bc7086Sdrh   int nErr = 0;
1117d8bc7086Sdrh   int i, j;
1118d8bc7086Sdrh   ExprList *pEList;
1119d8bc7086Sdrh 
1120daffd0e5Sdrh   if( pSelect==0 || pOrderBy==0 ) return 1;
1121d8bc7086Sdrh   if( mustComplete ){
1122d8bc7086Sdrh     for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1123d8bc7086Sdrh   }
1124d8bc7086Sdrh   if( fillInColumnList(pParse, pSelect) ){
1125d8bc7086Sdrh     return 1;
1126d8bc7086Sdrh   }
1127d8bc7086Sdrh   if( pSelect->pPrior ){
112892cd52f5Sdrh     if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
112992cd52f5Sdrh       return 1;
113092cd52f5Sdrh     }
1131d8bc7086Sdrh   }
1132d8bc7086Sdrh   pEList = pSelect->pEList;
1133d8bc7086Sdrh   for(i=0; i<pOrderBy->nExpr; i++){
1134d8bc7086Sdrh     Expr *pE = pOrderBy->a[i].pExpr;
1135e4de1febSdrh     int iCol = -1;
1136d8bc7086Sdrh     if( pOrderBy->a[i].done ) continue;
11374adee20fSdanielk1977     if( sqlite3ExprIsInteger(pE, &iCol) ){
1138e4de1febSdrh       if( iCol<=0 || iCol>pEList->nExpr ){
11394adee20fSdanielk1977         sqlite3ErrorMsg(pParse,
1140da93d238Sdrh           "ORDER BY position %d should be between 1 and %d",
1141e4de1febSdrh           iCol, pEList->nExpr);
1142e4de1febSdrh         nErr++;
1143e4de1febSdrh         break;
1144e4de1febSdrh       }
1145fcb78a49Sdrh       if( !mustComplete ) continue;
1146e4de1febSdrh       iCol--;
1147e4de1febSdrh     }
1148e4de1febSdrh     for(j=0; iCol<0 && j<pEList->nExpr; j++){
11494cfa7934Sdrh       if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
1150a76b5dfcSdrh         char *zName, *zLabel;
1151a76b5dfcSdrh         zName = pEList->a[j].zName;
1152a99db3b6Sdrh         zLabel = sqlite3NameFromToken(&pE->token);
1153a99db3b6Sdrh         assert( zLabel!=0 );
11544adee20fSdanielk1977         if( sqlite3StrICmp(zName, zLabel)==0 ){
1155e4de1febSdrh           iCol = j;
1156d8bc7086Sdrh         }
11576e142f54Sdrh         sqliteFree(zLabel);
1158d8bc7086Sdrh       }
11594adee20fSdanielk1977       if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){
1160e4de1febSdrh         iCol = j;
1161d8bc7086Sdrh       }
1162e4de1febSdrh     }
1163e4de1febSdrh     if( iCol>=0 ){
1164967e8b73Sdrh       pE->op = TK_COLUMN;
1165e4de1febSdrh       pE->iColumn = iCol;
1166d8bc7086Sdrh       pE->iTable = iTable;
1167d8bc7086Sdrh       pOrderBy->a[i].done = 1;
1168d8bc7086Sdrh     }
1169e4de1febSdrh     if( iCol<0 && mustComplete ){
11704adee20fSdanielk1977       sqlite3ErrorMsg(pParse,
1171da93d238Sdrh         "ORDER BY term number %d does not match any result column", i+1);
1172d8bc7086Sdrh       nErr++;
1173d8bc7086Sdrh       break;
1174d8bc7086Sdrh     }
1175d8bc7086Sdrh   }
1176d8bc7086Sdrh   return nErr;
1177d8bc7086Sdrh }
1178d8bc7086Sdrh 
1179d8bc7086Sdrh /*
1180d8bc7086Sdrh ** Get a VDBE for the given parser context.  Create a new one if necessary.
1181d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse.
1182d8bc7086Sdrh */
11834adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){
1184d8bc7086Sdrh   Vdbe *v = pParse->pVdbe;
1185d8bc7086Sdrh   if( v==0 ){
11864adee20fSdanielk1977     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
1187d8bc7086Sdrh   }
1188d8bc7086Sdrh   return v;
1189d8bc7086Sdrh }
1190d8bc7086Sdrh 
1191d3d39e93Sdrh #if 0  /***** This routine needs deleting *****/
119284ac9d02Sdanielk1977 static void multiSelectAffinity(Select *p, char *zAff){
119384ac9d02Sdanielk1977   int i;
119484ac9d02Sdanielk1977 
119584ac9d02Sdanielk1977   if( !p ) return;
119684ac9d02Sdanielk1977   multiSelectAffinity(p->pPrior, zAff);
119784ac9d02Sdanielk1977 
119884ac9d02Sdanielk1977   for(i=0; i<p->pEList->nExpr; i++){
119984ac9d02Sdanielk1977     if( zAff[i]=='\0' ){
120084ac9d02Sdanielk1977       zAff[i] = sqlite3ExprAffinity(p->pEList->a[i].pExpr);
120184ac9d02Sdanielk1977     }
120284ac9d02Sdanielk1977   }
120384ac9d02Sdanielk1977 }
1204d3d39e93Sdrh #endif
120584ac9d02Sdanielk1977 
1206d8bc7086Sdrh /*
12077b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the
12087b58daeaSdrh ** nLimit and nOffset fields.  nLimit and nOffset hold the integers
12097b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET
12107b58daeaSdrh ** keywords.  Or that hold -1 and 0 if those keywords are omitted.
12117b58daeaSdrh ** iLimit and iOffset are the integer memory register numbers for
12127b58daeaSdrh ** counters used to compute the limit and offset.  If there is no
12137b58daeaSdrh ** limit and/or offset, then iLimit and iOffset are negative.
12147b58daeaSdrh **
12157b58daeaSdrh ** This routine changes the values if iLimit and iOffset only if
12167b58daeaSdrh ** a limit or offset is defined by nLimit and nOffset.  iLimit and
12177b58daeaSdrh ** iOffset should have been preset to appropriate default values
12187b58daeaSdrh ** (usually but not always -1) prior to calling this routine.
12197b58daeaSdrh ** Only if nLimit>=0 or nOffset>0 do the limit registers get
12207b58daeaSdrh ** redefined.  The UNION ALL operator uses this property to force
12217b58daeaSdrh ** the reuse of the same limit and offset registers across multiple
12227b58daeaSdrh ** SELECT statements.
12237b58daeaSdrh */
12247b58daeaSdrh static void computeLimitRegisters(Parse *pParse, Select *p){
12257b58daeaSdrh   /*
12267b58daeaSdrh   ** If the comparison is p->nLimit>0 then "LIMIT 0" shows
12277b58daeaSdrh   ** all rows.  It is the same as no limit. If the comparision is
12287b58daeaSdrh   ** p->nLimit>=0 then "LIMIT 0" show no rows at all.
12297b58daeaSdrh   ** "LIMIT -1" always shows all rows.  There is some
12307b58daeaSdrh   ** contraversy about what the correct behavior should be.
12317b58daeaSdrh   ** The current implementation interprets "LIMIT 0" to mean
12327b58daeaSdrh   ** no rows.
12337b58daeaSdrh   */
12347b58daeaSdrh   if( p->nLimit>=0 ){
12357b58daeaSdrh     int iMem = pParse->nMem++;
12364adee20fSdanielk1977     Vdbe *v = sqlite3GetVdbe(pParse);
12377b58daeaSdrh     if( v==0 ) return;
12384adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Integer, -p->nLimit, 0);
12394adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1);
12407b58daeaSdrh     p->iLimit = iMem;
12417b58daeaSdrh   }
12427b58daeaSdrh   if( p->nOffset>0 ){
12437b58daeaSdrh     int iMem = pParse->nMem++;
12444adee20fSdanielk1977     Vdbe *v = sqlite3GetVdbe(pParse);
12457b58daeaSdrh     if( v==0 ) return;
12464adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Integer, -p->nOffset, 0);
12474adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1);
12487b58daeaSdrh     p->iOffset = iMem;
12497b58daeaSdrh   }
12507b58daeaSdrh }
12517b58daeaSdrh 
12527b58daeaSdrh /*
1253d3d39e93Sdrh ** Generate VDBE instructions that will open a transient table that
1254d3d39e93Sdrh ** will be used for an index or to store keyed results for a compound
1255d3d39e93Sdrh ** select.  In other words, open a transient table that needs a
1256d3d39e93Sdrh ** KeyInfo structure.  The number of columns in the KeyInfo is determined
1257d3d39e93Sdrh ** by the result set of the SELECT statement in the second argument.
1258d3d39e93Sdrh **
1259dc1bdc4fSdanielk1977 ** Specifically, this routine is called to open an index table for
1260dc1bdc4fSdanielk1977 ** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not
1261dc1bdc4fSdanielk1977 ** UNION ALL).
1262dc1bdc4fSdanielk1977 **
1263d3d39e93Sdrh ** Make the new table a KeyAsData table if keyAsData is true.
1264dc1bdc4fSdanielk1977 **
1265dc1bdc4fSdanielk1977 ** The value returned is the address of the OP_OpenTemp instruction.
1266d3d39e93Sdrh */
1267dc1bdc4fSdanielk1977 static int openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){
1268d3d39e93Sdrh   KeyInfo *pKeyInfo;
1269736c22b8Sdrh   int nColumn;
1270d3d39e93Sdrh   sqlite *db = pParse->db;
1271d3d39e93Sdrh   int i;
1272d3d39e93Sdrh   Vdbe *v = pParse->pVdbe;
1273dc1bdc4fSdanielk1977   int addr;
1274d3d39e93Sdrh 
1275736c22b8Sdrh   if( fillInColumnList(pParse, p) ){
1276dc1bdc4fSdanielk1977     return 0;
1277736c22b8Sdrh   }
1278736c22b8Sdrh   nColumn = p->pEList->nExpr;
1279d3d39e93Sdrh   pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) );
1280dc1bdc4fSdanielk1977   if( pKeyInfo==0 ) return 0;
1281dc1bdc4fSdanielk1977   pKeyInfo->enc = pParse->db->enc;
1282d3d39e93Sdrh   pKeyInfo->nField = nColumn;
1283d3d39e93Sdrh   for(i=0; i<nColumn; i++){
1284dc1bdc4fSdanielk1977     pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
1285dc1bdc4fSdanielk1977     if( !pKeyInfo->aColl[i] ){
1286d3d39e93Sdrh       pKeyInfo->aColl[i] = db->pDfltColl;
1287d3d39e93Sdrh     }
1288dc1bdc4fSdanielk1977   }
1289dc1bdc4fSdanielk1977   addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0,
1290dc1bdc4fSdanielk1977       (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
1291d3d39e93Sdrh   if( keyAsData ){
1292d3d39e93Sdrh     sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1);
1293d3d39e93Sdrh   }
1294dc1bdc4fSdanielk1977   return addr;
1295dc1bdc4fSdanielk1977 }
1296dc1bdc4fSdanielk1977 
1297dc1bdc4fSdanielk1977 static int multiSelectOpenTempAddr(Select *p, int addr, IdList **ppOpenTemp){
1298dc1bdc4fSdanielk1977   if( !p->ppOpenTemp ){
1299dc1bdc4fSdanielk1977     *ppOpenTemp = sqlite3IdListAppend(0, 0);
1300dc1bdc4fSdanielk1977     p->ppOpenTemp = ppOpenTemp;
1301dc1bdc4fSdanielk1977   }else{
1302dc1bdc4fSdanielk1977     *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0);
1303dc1bdc4fSdanielk1977   }
1304dc1bdc4fSdanielk1977   if( !(*p->ppOpenTemp) ){
1305dc1bdc4fSdanielk1977     return SQLITE_NOMEM;
1306dc1bdc4fSdanielk1977   }
1307dc1bdc4fSdanielk1977   (*p->ppOpenTemp)->a[(*p->ppOpenTemp)->nId-1].idx = addr;
1308dc1bdc4fSdanielk1977   return SQLITE_OK;
1309dc1bdc4fSdanielk1977 }
1310dc1bdc4fSdanielk1977 
1311dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
1312dc1bdc4fSdanielk1977   CollSeq *pRet = 0;
1313dc1bdc4fSdanielk1977   if( p->pPrior ){
1314dc1bdc4fSdanielk1977     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
1315dc1bdc4fSdanielk1977   }
1316dc1bdc4fSdanielk1977   if( !pRet ){
1317dc1bdc4fSdanielk1977     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
1318dc1bdc4fSdanielk1977   }
1319dc1bdc4fSdanielk1977   return pRet;
1320d3d39e93Sdrh }
1321d3d39e93Sdrh 
1322d3d39e93Sdrh /*
132382c3d636Sdrh ** This routine is called to process a query that is really the union
132482c3d636Sdrh ** or intersection of two or more separate queries.
1325c926afbcSdrh **
1326e78e8284Sdrh ** "p" points to the right-most of the two queries.  the query on the
1327e78e8284Sdrh ** left is p->pPrior.  The left query could also be a compound query
1328e78e8284Sdrh ** in which case this routine will be called recursively.
1329e78e8284Sdrh **
1330e78e8284Sdrh ** The results of the total query are to be written into a destination
1331e78e8284Sdrh ** of type eDest with parameter iParm.
1332e78e8284Sdrh **
1333e78e8284Sdrh ** Example 1:  Consider a three-way compound SQL statement.
1334e78e8284Sdrh **
1335e78e8284Sdrh **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1336e78e8284Sdrh **
1337e78e8284Sdrh ** This statement is parsed up as follows:
1338e78e8284Sdrh **
1339e78e8284Sdrh **     SELECT c FROM t3
1340e78e8284Sdrh **      |
1341e78e8284Sdrh **      `----->  SELECT b FROM t2
1342e78e8284Sdrh **                |
13434b11c6d3Sjplyon **                `------>  SELECT a FROM t1
1344e78e8284Sdrh **
1345e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer.
1346e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then
1347e78e8284Sdrh ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
1348e78e8284Sdrh **
1349e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the
1350e78e8284Sdrh ** individual selects always group from left to right.
135182c3d636Sdrh */
135284ac9d02Sdanielk1977 static int multiSelect(
135384ac9d02Sdanielk1977   Parse *pParse,
135484ac9d02Sdanielk1977   Select *p,
135584ac9d02Sdanielk1977   int eDest,
135684ac9d02Sdanielk1977   int iParm,
135784ac9d02Sdanielk1977   char *aff           /* If eDest is SRT_Union, the affinity string */
135884ac9d02Sdanielk1977 ){
135984ac9d02Sdanielk1977   int rc = SQLITE_OK;  /* Success code from a subroutine */
136010e5e3cfSdrh   Select *pPrior;     /* Another SELECT immediately to our left */
136110e5e3cfSdrh   Vdbe *v;            /* Generate code to this VDBE */
1362dc1bdc4fSdanielk1977   IdList *pOpenTemp = 0;
136382c3d636Sdrh 
13647b58daeaSdrh   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
13657b58daeaSdrh   ** the last SELECT in the series may have an ORDER BY or LIMIT.
136682c3d636Sdrh   */
136784ac9d02Sdanielk1977   if( p==0 || p->pPrior==0 ){
136884ac9d02Sdanielk1977     rc = 1;
136984ac9d02Sdanielk1977     goto multi_select_end;
137084ac9d02Sdanielk1977   }
1371d8bc7086Sdrh   pPrior = p->pPrior;
1372d8bc7086Sdrh   if( pPrior->pOrderBy ){
13734adee20fSdanielk1977     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
1374da93d238Sdrh       selectOpName(p->op));
137584ac9d02Sdanielk1977     rc = 1;
137684ac9d02Sdanielk1977     goto multi_select_end;
137782c3d636Sdrh   }
13787b58daeaSdrh   if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){
13794adee20fSdanielk1977     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
13807b58daeaSdrh       selectOpName(p->op));
138184ac9d02Sdanielk1977     rc = 1;
138284ac9d02Sdanielk1977     goto multi_select_end;
13837b58daeaSdrh   }
138482c3d636Sdrh 
1385d8bc7086Sdrh   /* Make sure we have a valid query engine.  If not, create a new one.
1386d8bc7086Sdrh   */
13874adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
138884ac9d02Sdanielk1977   if( v==0 ){
138984ac9d02Sdanielk1977     rc = 1;
139084ac9d02Sdanielk1977     goto multi_select_end;
139184ac9d02Sdanielk1977   }
1392d8bc7086Sdrh 
13931cc3d75fSdrh   /* Create the destination temporary table if necessary
13941cc3d75fSdrh   */
13951cc3d75fSdrh   if( eDest==SRT_TempTable ){
1396b4964b72Sdanielk1977     assert( p->pEList );
13974adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);
1398b4964b72Sdanielk1977     sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, p->pEList->nExpr);
13991cc3d75fSdrh     eDest = SRT_Table;
14001cc3d75fSdrh   }
14011cc3d75fSdrh 
1402f46f905aSdrh   /* Generate code for the left and right SELECT statements.
1403d8bc7086Sdrh   */
140482c3d636Sdrh   switch( p->op ){
1405f46f905aSdrh     case TK_ALL: {
1406f46f905aSdrh       if( p->pOrderBy==0 ){
14077b58daeaSdrh         pPrior->nLimit = p->nLimit;
14087b58daeaSdrh         pPrior->nOffset = p->nOffset;
1409dc1bdc4fSdanielk1977         pPrior->ppOpenTemp = p->ppOpenTemp;
141084ac9d02Sdanielk1977         rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff);
141184ac9d02Sdanielk1977         if( rc ){
141284ac9d02Sdanielk1977           goto multi_select_end;
141384ac9d02Sdanielk1977         }
1414f46f905aSdrh         p->pPrior = 0;
14157b58daeaSdrh         p->iLimit = pPrior->iLimit;
14167b58daeaSdrh         p->iOffset = pPrior->iOffset;
14177b58daeaSdrh         p->nLimit = -1;
14187b58daeaSdrh         p->nOffset = 0;
141984ac9d02Sdanielk1977         rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff);
1420f46f905aSdrh         p->pPrior = pPrior;
142184ac9d02Sdanielk1977         if( rc ){
142284ac9d02Sdanielk1977           goto multi_select_end;
142384ac9d02Sdanielk1977         }
1424f46f905aSdrh         break;
1425f46f905aSdrh       }
1426f46f905aSdrh       /* For UNION ALL ... ORDER BY fall through to the next case */
1427f46f905aSdrh     }
142882c3d636Sdrh     case TK_EXCEPT:
142982c3d636Sdrh     case TK_UNION: {
1430d8bc7086Sdrh       int unionTab;    /* Cursor number of the temporary table holding result */
1431742f947bSdanielk1977       int op = 0;      /* One of the SRT_ operations to apply to self */
1432d8bc7086Sdrh       int priorOp;     /* The SRT_ operation to apply to prior selects */
14337b58daeaSdrh       int nLimit, nOffset; /* Saved values of p->nLimit and p->nOffset */
1434c926afbcSdrh       ExprList *pOrderBy;  /* The ORDER BY clause for the right SELECT */
1435dc1bdc4fSdanielk1977       int addr;
143682c3d636Sdrh 
1437d8bc7086Sdrh       priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
14387b58daeaSdrh       if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){
1439d8bc7086Sdrh         /* We can reuse a temporary table generated by a SELECT to our
1440c926afbcSdrh         ** right.
1441d8bc7086Sdrh         */
144282c3d636Sdrh         unionTab = iParm;
144382c3d636Sdrh       }else{
1444d8bc7086Sdrh         /* We will need to create our own temporary table to hold the
1445d8bc7086Sdrh         ** intermediate results.
1446d8bc7086Sdrh         */
144782c3d636Sdrh         unionTab = pParse->nTab++;
1448d8bc7086Sdrh         if( p->pOrderBy
1449d8bc7086Sdrh         && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
145084ac9d02Sdanielk1977           rc = 1;
145184ac9d02Sdanielk1977           goto multi_select_end;
1452d8bc7086Sdrh         }
1453dc1bdc4fSdanielk1977         addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0);
1454d8bc7086Sdrh         if( p->op!=TK_ALL ){
1455dc1bdc4fSdanielk1977           rc = multiSelectOpenTempAddr(p, addr, &pOpenTemp);
1456dc1bdc4fSdanielk1977           if( rc!=SQLITE_OK ){
1457dc1bdc4fSdanielk1977             goto multi_select_end;
1458dc1bdc4fSdanielk1977           }
1459dc1bdc4fSdanielk1977           sqlite3VdbeAddOp(v, OP_KeyAsData, unionTab, 1);
146082c3d636Sdrh         }
146184ac9d02Sdanielk1977         assert( p->pEList );
1462d8bc7086Sdrh       }
1463d8bc7086Sdrh 
1464d8bc7086Sdrh       /* Code the SELECT statements to our left
1465d8bc7086Sdrh       */
1466dc1bdc4fSdanielk1977       pPrior->ppOpenTemp = p->ppOpenTemp;
146784ac9d02Sdanielk1977       rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff);
146884ac9d02Sdanielk1977       if( rc ){
146984ac9d02Sdanielk1977         goto multi_select_end;
147084ac9d02Sdanielk1977       }
147184ac9d02Sdanielk1977       if( p->op==TK_ALL ){
147284ac9d02Sdanielk1977         sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, pPrior->pEList->nExpr);
147384ac9d02Sdanielk1977       }
1474d8bc7086Sdrh 
1475d8bc7086Sdrh       /* Code the current SELECT statement
1476d8bc7086Sdrh       */
1477d8bc7086Sdrh       switch( p->op ){
1478d8bc7086Sdrh          case TK_EXCEPT:  op = SRT_Except;   break;
1479d8bc7086Sdrh          case TK_UNION:   op = SRT_Union;    break;
1480d8bc7086Sdrh          case TK_ALL:     op = SRT_Table;    break;
1481d8bc7086Sdrh       }
148282c3d636Sdrh       p->pPrior = 0;
1483c926afbcSdrh       pOrderBy = p->pOrderBy;
1484c926afbcSdrh       p->pOrderBy = 0;
14857b58daeaSdrh       nLimit = p->nLimit;
14867b58daeaSdrh       p->nLimit = -1;
14877b58daeaSdrh       nOffset = p->nOffset;
14887b58daeaSdrh       p->nOffset = 0;
148984ac9d02Sdanielk1977       rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff);
149082c3d636Sdrh       p->pPrior = pPrior;
1491c926afbcSdrh       p->pOrderBy = pOrderBy;
14927b58daeaSdrh       p->nLimit = nLimit;
14937b58daeaSdrh       p->nOffset = nOffset;
149484ac9d02Sdanielk1977       if( rc ){
149584ac9d02Sdanielk1977         goto multi_select_end;
149684ac9d02Sdanielk1977       }
149784ac9d02Sdanielk1977 
1498d8bc7086Sdrh 
1499d8bc7086Sdrh       /* Convert the data in the temporary table into whatever form
1500d8bc7086Sdrh       ** it is that we currently need.
1501d8bc7086Sdrh       */
1502c926afbcSdrh       if( eDest!=priorOp || unionTab!=iParm ){
15036b56344dSdrh         int iCont, iBreak, iStart;
150482c3d636Sdrh         assert( p->pEList );
150541202ccaSdrh         if( eDest==SRT_Callback ){
15066a3ea0e6Sdrh           generateColumnNames(pParse, 0, p->pEList);
150741202ccaSdrh         }
15084adee20fSdanielk1977         iBreak = sqlite3VdbeMakeLabel(v);
15094adee20fSdanielk1977         iCont = sqlite3VdbeMakeLabel(v);
15104adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak);
15117b58daeaSdrh         computeLimitRegisters(pParse, p);
15124adee20fSdanielk1977         iStart = sqlite3VdbeCurrentAddr(v);
151338640e15Sdrh         rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
1514d8bc7086Sdrh                              p->pOrderBy, -1, eDest, iParm,
151584ac9d02Sdanielk1977                              iCont, iBreak, 0);
151684ac9d02Sdanielk1977         if( rc ){
151784ac9d02Sdanielk1977           rc = 1;
151884ac9d02Sdanielk1977           goto multi_select_end;
151984ac9d02Sdanielk1977         }
15204adee20fSdanielk1977         sqlite3VdbeResolveLabel(v, iCont);
15214adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart);
15224adee20fSdanielk1977         sqlite3VdbeResolveLabel(v, iBreak);
15234adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_Close, unionTab, 0);
152482c3d636Sdrh       }
152582c3d636Sdrh       break;
152682c3d636Sdrh     }
152782c3d636Sdrh     case TK_INTERSECT: {
152882c3d636Sdrh       int tab1, tab2;
15296b56344dSdrh       int iCont, iBreak, iStart;
15307b58daeaSdrh       int nLimit, nOffset;
1531dc1bdc4fSdanielk1977       int addr;
153282c3d636Sdrh 
1533d8bc7086Sdrh       /* INTERSECT is different from the others since it requires
15346206d50aSdrh       ** two temporary tables.  Hence it has its own case.  Begin
1535d8bc7086Sdrh       ** by allocating the tables we will need.
1536d8bc7086Sdrh       */
153782c3d636Sdrh       tab1 = pParse->nTab++;
153882c3d636Sdrh       tab2 = pParse->nTab++;
1539d8bc7086Sdrh       if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
154084ac9d02Sdanielk1977         rc = 1;
154184ac9d02Sdanielk1977         goto multi_select_end;
1542d8bc7086Sdrh       }
1543dc1bdc4fSdanielk1977 
1544dc1bdc4fSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab1, 0);
1545dc1bdc4fSdanielk1977       rc = multiSelectOpenTempAddr(p, addr, &pOpenTemp);
1546dc1bdc4fSdanielk1977       if( rc!=SQLITE_OK ){
1547dc1bdc4fSdanielk1977         goto multi_select_end;
1548dc1bdc4fSdanielk1977       }
1549dc1bdc4fSdanielk1977       sqlite3VdbeAddOp(v, OP_KeyAsData, tab1, 1);
155084ac9d02Sdanielk1977       assert( p->pEList );
1551d8bc7086Sdrh 
1552d8bc7086Sdrh       /* Code the SELECTs to our left into temporary table "tab1".
1553d8bc7086Sdrh       */
1554dc1bdc4fSdanielk1977       pPrior->ppOpenTemp = p->ppOpenTemp;
155584ac9d02Sdanielk1977       rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff);
155684ac9d02Sdanielk1977       if( rc ){
155784ac9d02Sdanielk1977         goto multi_select_end;
155884ac9d02Sdanielk1977       }
1559d8bc7086Sdrh 
1560d8bc7086Sdrh       /* Code the current SELECT into temporary table "tab2"
1561d8bc7086Sdrh       */
1562dc1bdc4fSdanielk1977       addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab2, 0);
1563dc1bdc4fSdanielk1977       rc = multiSelectOpenTempAddr(p, addr, &pOpenTemp);
1564dc1bdc4fSdanielk1977       if( rc!=SQLITE_OK ){
1565dc1bdc4fSdanielk1977         goto multi_select_end;
1566dc1bdc4fSdanielk1977       }
1567dc1bdc4fSdanielk1977       sqlite3VdbeAddOp(v, OP_KeyAsData, tab2, 1);
156882c3d636Sdrh       p->pPrior = 0;
15697b58daeaSdrh       nLimit = p->nLimit;
15707b58daeaSdrh       p->nLimit = -1;
15717b58daeaSdrh       nOffset = p->nOffset;
15727b58daeaSdrh       p->nOffset = 0;
157384ac9d02Sdanielk1977       rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff);
157482c3d636Sdrh       p->pPrior = pPrior;
15757b58daeaSdrh       p->nLimit = nLimit;
15767b58daeaSdrh       p->nOffset = nOffset;
157784ac9d02Sdanielk1977       if( rc ){
157884ac9d02Sdanielk1977         goto multi_select_end;
157984ac9d02Sdanielk1977       }
1580d8bc7086Sdrh 
1581d8bc7086Sdrh       /* Generate code to take the intersection of the two temporary
1582d8bc7086Sdrh       ** tables.
1583d8bc7086Sdrh       */
158482c3d636Sdrh       assert( p->pEList );
158541202ccaSdrh       if( eDest==SRT_Callback ){
15866a3ea0e6Sdrh         generateColumnNames(pParse, 0, p->pEList);
158741202ccaSdrh       }
15884adee20fSdanielk1977       iBreak = sqlite3VdbeMakeLabel(v);
15894adee20fSdanielk1977       iCont = sqlite3VdbeMakeLabel(v);
15904adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak);
15917b58daeaSdrh       computeLimitRegisters(pParse, p);
15924adee20fSdanielk1977       iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0);
15934adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont);
159438640e15Sdrh       rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
1595d8bc7086Sdrh                              p->pOrderBy, -1, eDest, iParm,
159684ac9d02Sdanielk1977                              iCont, iBreak, 0);
159784ac9d02Sdanielk1977       if( rc ){
159884ac9d02Sdanielk1977         rc = 1;
159984ac9d02Sdanielk1977         goto multi_select_end;
160084ac9d02Sdanielk1977       }
16014adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, iCont);
16024adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Next, tab1, iStart);
16034adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, iBreak);
16044adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Close, tab2, 0);
16054adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Close, tab1, 0);
160682c3d636Sdrh       break;
160782c3d636Sdrh     }
160882c3d636Sdrh   }
160982c3d636Sdrh   assert( p->pEList && pPrior->pEList );
161082c3d636Sdrh   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
16114adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
1612da93d238Sdrh       " do not have the same number of result columns", selectOpName(p->op));
161384ac9d02Sdanielk1977     rc = 1;
161484ac9d02Sdanielk1977     goto multi_select_end;
16152282792aSdrh   }
161684ac9d02Sdanielk1977 
1617dc1bdc4fSdanielk1977   if( p->pOrderBy || (pOpenTemp && pOpenTemp->nId>0) ){
1618dc1bdc4fSdanielk1977     int nCol = p->pEList->nExpr;
1619dc1bdc4fSdanielk1977     int i;
1620dc1bdc4fSdanielk1977     KeyInfo *pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*sizeof(CollSeq*));
1621dc1bdc4fSdanielk1977     if( !pKeyInfo ){
1622dc1bdc4fSdanielk1977       rc = SQLITE_NOMEM;
1623dc1bdc4fSdanielk1977       goto multi_select_end;
1624dc1bdc4fSdanielk1977     }
1625dc1bdc4fSdanielk1977 
1626dc1bdc4fSdanielk1977     pKeyInfo->enc = pParse->db->enc;
1627dc1bdc4fSdanielk1977     pKeyInfo->nField = nCol;
1628dc1bdc4fSdanielk1977 
1629dc1bdc4fSdanielk1977     for(i=0; i<nCol; i++){
1630dc1bdc4fSdanielk1977       pKeyInfo->aColl[i] = multiSelectCollSeq(pParse, p, i);
1631dc1bdc4fSdanielk1977       if( !pKeyInfo->aColl[i] ){
1632dc1bdc4fSdanielk1977         pKeyInfo->aColl[i] = pParse->db->pDfltColl;
1633dc1bdc4fSdanielk1977       }
1634dc1bdc4fSdanielk1977     }
1635dc1bdc4fSdanielk1977 
1636dc1bdc4fSdanielk1977     for(i=0; pOpenTemp && i<pOpenTemp->nId; i++){
1637dc1bdc4fSdanielk1977       int p3type = (i==0?P3_KEYINFO_HANDOFF:P3_KEYINFO);
1638dc1bdc4fSdanielk1977       int addr = pOpenTemp->a[i].idx;
1639dc1bdc4fSdanielk1977       sqlite3VdbeChangeP3(v, addr, (char *)pKeyInfo, p3type);
1640dc1bdc4fSdanielk1977     }
1641dc1bdc4fSdanielk1977 
1642dc1bdc4fSdanielk1977     if( p->pOrderBy ){
1643dc1bdc4fSdanielk1977       for(i=0; i<p->pOrderBy->nExpr; i++){
1644dc1bdc4fSdanielk1977         Expr *pExpr = p->pOrderBy->a[i].pExpr;
1645dc1bdc4fSdanielk1977         char *zName = p->pOrderBy->a[i].zName;
1646dc1bdc4fSdanielk1977         assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol );
1647dc1bdc4fSdanielk1977         assert( !pExpr->pColl );
1648dc1bdc4fSdanielk1977         if( zName ){
1649dc1bdc4fSdanielk1977           pExpr->pColl = sqlite3LocateCollSeq(pParse, zName, -1);
165084ac9d02Sdanielk1977         }else{
1651dc1bdc4fSdanielk1977           pExpr->pColl = pKeyInfo->aColl[pExpr->iColumn];
165284ac9d02Sdanielk1977         }
165384ac9d02Sdanielk1977       }
1654dc1bdc4fSdanielk1977       generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
1655dc1bdc4fSdanielk1977     }
1656dc1bdc4fSdanielk1977 
1657dc1bdc4fSdanielk1977     if( !pOpenTemp ){
1658dc1bdc4fSdanielk1977       /* This happens for UNION ALL ... ORDER BY */
1659dc1bdc4fSdanielk1977       sqliteFree(pKeyInfo);
1660dc1bdc4fSdanielk1977     }
1661dc1bdc4fSdanielk1977   }
1662dc1bdc4fSdanielk1977 
1663dc1bdc4fSdanielk1977 multi_select_end:
1664dc1bdc4fSdanielk1977   if( pOpenTemp ){
1665dc1bdc4fSdanielk1977     sqlite3IdListDelete(pOpenTemp);
1666dc1bdc4fSdanielk1977   }
1667dc1bdc4fSdanielk1977   p->ppOpenTemp = 0;
166884ac9d02Sdanielk1977   return rc;
16692282792aSdrh }
16702282792aSdrh 
16712282792aSdrh /*
1672832508b7Sdrh ** Scan through the expression pExpr.  Replace every reference to
16736a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th
167484e59207Sdrh ** entry in pEList.  (But leave references to the ROWID column
16756a3ea0e6Sdrh ** unchanged.)
1676832508b7Sdrh **
1677832508b7Sdrh ** This routine is part of the flattening procedure.  A subquery
1678832508b7Sdrh ** whose result set is defined by pEList appears as entry in the
1679832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that
1680832508b7Sdrh ** FORM clause entry is iTable.  This routine make the necessary
1681832508b7Sdrh ** changes to pExpr so that it refers directly to the source table
1682832508b7Sdrh ** of the subquery rather the result set of the subquery.
1683832508b7Sdrh */
16846a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*);  /* Forward Decl */
16856a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){
1686832508b7Sdrh   if( pExpr==0 ) return;
168750350a15Sdrh   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
168850350a15Sdrh     if( pExpr->iColumn<0 ){
168950350a15Sdrh       pExpr->op = TK_NULL;
169050350a15Sdrh     }else{
1691832508b7Sdrh       Expr *pNew;
169284e59207Sdrh       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
1693832508b7Sdrh       assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1694832508b7Sdrh       pNew = pEList->a[pExpr->iColumn].pExpr;
1695832508b7Sdrh       assert( pNew!=0 );
1696832508b7Sdrh       pExpr->op = pNew->op;
1697d94a6698Sdrh       assert( pExpr->pLeft==0 );
16984adee20fSdanielk1977       pExpr->pLeft = sqlite3ExprDup(pNew->pLeft);
1699d94a6698Sdrh       assert( pExpr->pRight==0 );
17004adee20fSdanielk1977       pExpr->pRight = sqlite3ExprDup(pNew->pRight);
1701d94a6698Sdrh       assert( pExpr->pList==0 );
17024adee20fSdanielk1977       pExpr->pList = sqlite3ExprListDup(pNew->pList);
1703832508b7Sdrh       pExpr->iTable = pNew->iTable;
1704832508b7Sdrh       pExpr->iColumn = pNew->iColumn;
1705832508b7Sdrh       pExpr->iAgg = pNew->iAgg;
17064adee20fSdanielk1977       sqlite3TokenCopy(&pExpr->token, &pNew->token);
17074adee20fSdanielk1977       sqlite3TokenCopy(&pExpr->span, &pNew->span);
170850350a15Sdrh     }
1709832508b7Sdrh   }else{
17106a3ea0e6Sdrh     substExpr(pExpr->pLeft, iTable, pEList);
17116a3ea0e6Sdrh     substExpr(pExpr->pRight, iTable, pEList);
17126a3ea0e6Sdrh     substExprList(pExpr->pList, iTable, pEList);
1713832508b7Sdrh   }
1714832508b7Sdrh }
1715832508b7Sdrh static void
17166a3ea0e6Sdrh substExprList(ExprList *pList, int iTable, ExprList *pEList){
1717832508b7Sdrh   int i;
1718832508b7Sdrh   if( pList==0 ) return;
1719832508b7Sdrh   for(i=0; i<pList->nExpr; i++){
17206a3ea0e6Sdrh     substExpr(pList->a[i].pExpr, iTable, pEList);
1721832508b7Sdrh   }
1722832508b7Sdrh }
1723832508b7Sdrh 
1724832508b7Sdrh /*
17251350b030Sdrh ** This routine attempts to flatten subqueries in order to speed
17261350b030Sdrh ** execution.  It returns 1 if it makes changes and 0 if no flattening
17271350b030Sdrh ** occurs.
17281350b030Sdrh **
17291350b030Sdrh ** To understand the concept of flattening, consider the following
17301350b030Sdrh ** query:
17311350b030Sdrh **
17321350b030Sdrh **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
17331350b030Sdrh **
17341350b030Sdrh ** The default way of implementing this query is to execute the
17351350b030Sdrh ** subquery first and store the results in a temporary table, then
17361350b030Sdrh ** run the outer query on that temporary table.  This requires two
17371350b030Sdrh ** passes over the data.  Furthermore, because the temporary table
17381350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be
1739832508b7Sdrh ** optimized.
17401350b030Sdrh **
1741832508b7Sdrh ** This routine attempts to rewrite queries such as the above into
17421350b030Sdrh ** a single flat select, like this:
17431350b030Sdrh **
17441350b030Sdrh **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
17451350b030Sdrh **
17461350b030Sdrh ** The code generated for this simpification gives the same result
1747832508b7Sdrh ** but only has to scan the data once.  And because indices might
1748832508b7Sdrh ** exist on the table t1, a complete scan of the data might be
1749832508b7Sdrh ** avoided.
17501350b030Sdrh **
1751832508b7Sdrh ** Flattening is only attempted if all of the following are true:
17521350b030Sdrh **
1753832508b7Sdrh **   (1)  The subquery and the outer query do not both use aggregates.
17541350b030Sdrh **
1755832508b7Sdrh **   (2)  The subquery is not an aggregate or the outer query is not a join.
1756832508b7Sdrh **
17578af4d3acSdrh **   (3)  The subquery is not the right operand of a left outer join, or
17588af4d3acSdrh **        the subquery is not itself a join.  (Ticket #306)
1759832508b7Sdrh **
1760832508b7Sdrh **   (4)  The subquery is not DISTINCT or the outer query is not a join.
1761832508b7Sdrh **
1762832508b7Sdrh **   (5)  The subquery is not DISTINCT or the outer query does not use
1763832508b7Sdrh **        aggregates.
1764832508b7Sdrh **
1765832508b7Sdrh **   (6)  The subquery does not use aggregates or the outer query is not
1766832508b7Sdrh **        DISTINCT.
1767832508b7Sdrh **
176808192d5fSdrh **   (7)  The subquery has a FROM clause.
176908192d5fSdrh **
1770df199a25Sdrh **   (8)  The subquery does not use LIMIT or the outer query is not a join.
1771df199a25Sdrh **
1772df199a25Sdrh **   (9)  The subquery does not use LIMIT or the outer query does not use
1773df199a25Sdrh **        aggregates.
1774df199a25Sdrh **
1775df199a25Sdrh **  (10)  The subquery does not use aggregates or the outer query does not
1776df199a25Sdrh **        use LIMIT.
1777df199a25Sdrh **
1778174b6195Sdrh **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
1779174b6195Sdrh **
17803fc673e6Sdrh **  (12)  The subquery is not the right term of a LEFT OUTER JOIN or the
17813fc673e6Sdrh **        subquery has no WHERE clause.  (added by ticket #350)
17823fc673e6Sdrh **
1783832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query.
1784832508b7Sdrh ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
1785832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1786832508b7Sdrh **
1787665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0.
1788832508b7Sdrh ** If flattening is attempted this routine returns 1.
1789832508b7Sdrh **
1790832508b7Sdrh ** All of the expression analysis must occur on both the outer query and
1791832508b7Sdrh ** the subquery before this routine runs.
17921350b030Sdrh */
17938c74a8caSdrh static int flattenSubquery(
17948c74a8caSdrh   Parse *pParse,       /* The parsing context */
17958c74a8caSdrh   Select *p,           /* The parent or outer SELECT statement */
17968c74a8caSdrh   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
17978c74a8caSdrh   int isAgg,           /* True if outer SELECT uses aggregate functions */
17988c74a8caSdrh   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
17998c74a8caSdrh ){
18000bb28106Sdrh   Select *pSub;       /* The inner query or "subquery" */
1801ad3cab52Sdrh   SrcList *pSrc;      /* The FROM clause of the outer query */
1802ad3cab52Sdrh   SrcList *pSubSrc;   /* The FROM clause of the subquery */
18030bb28106Sdrh   ExprList *pList;    /* The result set of the outer query */
18046a3ea0e6Sdrh   int iParent;        /* VDBE cursor number of the pSub result set temp table */
1805832508b7Sdrh   int i;
1806832508b7Sdrh   Expr *pWhere;
18071350b030Sdrh 
1808832508b7Sdrh   /* Check to see if flattening is permitted.  Return 0 if not.
1809832508b7Sdrh   */
1810832508b7Sdrh   if( p==0 ) return 0;
1811832508b7Sdrh   pSrc = p->pSrc;
1812ad3cab52Sdrh   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
1813832508b7Sdrh   pSub = pSrc->a[iFrom].pSelect;
1814832508b7Sdrh   assert( pSub!=0 );
1815832508b7Sdrh   if( isAgg && subqueryIsAgg ) return 0;
1816ad3cab52Sdrh   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
1817832508b7Sdrh   pSubSrc = pSub->pSrc;
1818832508b7Sdrh   assert( pSubSrc );
1819c31c2eb8Sdrh   if( pSubSrc->nSrc==0 ) return 0;
1820df199a25Sdrh   if( (pSub->isDistinct || pSub->nLimit>=0) &&  (pSrc->nSrc>1 || isAgg) ){
1821df199a25Sdrh      return 0;
1822df199a25Sdrh   }
1823d11d382cSdrh   if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
1824174b6195Sdrh   if( p->pOrderBy && pSub->pOrderBy ) return 0;
1825832508b7Sdrh 
18268af4d3acSdrh   /* Restriction 3:  If the subquery is a join, make sure the subquery is
18278af4d3acSdrh   ** not used as the right operand of an outer join.  Examples of why this
18288af4d3acSdrh   ** is not allowed:
18298af4d3acSdrh   **
18308af4d3acSdrh   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
18318af4d3acSdrh   **
18328af4d3acSdrh   ** If we flatten the above, we would get
18338af4d3acSdrh   **
18348af4d3acSdrh   **         (t1 LEFT OUTER JOIN t2) JOIN t3
18358af4d3acSdrh   **
18368af4d3acSdrh   ** which is not at all the same thing.
18378af4d3acSdrh   */
18388af4d3acSdrh   if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){
18398af4d3acSdrh     return 0;
18408af4d3acSdrh   }
18418af4d3acSdrh 
18423fc673e6Sdrh   /* Restriction 12:  If the subquery is the right operand of a left outer
18433fc673e6Sdrh   ** join, make sure the subquery has no WHERE clause.
18443fc673e6Sdrh   ** An examples of why this is not allowed:
18453fc673e6Sdrh   **
18463fc673e6Sdrh   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
18473fc673e6Sdrh   **
18483fc673e6Sdrh   ** If we flatten the above, we would get
18493fc673e6Sdrh   **
18503fc673e6Sdrh   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
18513fc673e6Sdrh   **
18523fc673e6Sdrh   ** But the t2.x>0 test will always fail on a NULL row of t2, which
18533fc673e6Sdrh   ** effectively converts the OUTER JOIN into an INNER JOIN.
18543fc673e6Sdrh   */
18553fc673e6Sdrh   if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0
18563fc673e6Sdrh       && pSub->pWhere!=0 ){
18573fc673e6Sdrh     return 0;
18583fc673e6Sdrh   }
18593fc673e6Sdrh 
18600bb28106Sdrh   /* If we reach this point, it means flattening is permitted for the
186163eb5f29Sdrh   ** iFrom-th entry of the FROM clause in the outer query.
1862832508b7Sdrh   */
1863c31c2eb8Sdrh 
1864c31c2eb8Sdrh   /* Move all of the FROM elements of the subquery into the
1865c31c2eb8Sdrh   ** the FROM clause of the outer query.  Before doing this, remember
1866c31c2eb8Sdrh   ** the cursor number for the original outer query FROM element in
1867c31c2eb8Sdrh   ** iParent.  The iParent cursor will never be used.  Subsequent code
1868c31c2eb8Sdrh   ** will scan expressions looking for iParent references and replace
1869c31c2eb8Sdrh   ** those references with expressions that resolve to the subquery FROM
1870c31c2eb8Sdrh   ** elements we are now copying in.
1871c31c2eb8Sdrh   */
18726a3ea0e6Sdrh   iParent = pSrc->a[iFrom].iCursor;
1873c31c2eb8Sdrh   {
1874c31c2eb8Sdrh     int nSubSrc = pSubSrc->nSrc;
18758af4d3acSdrh     int jointype = pSrc->a[iFrom].jointype;
1876c31c2eb8Sdrh 
1877c31c2eb8Sdrh     if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
18784adee20fSdanielk1977       sqlite3DeleteTable(0, pSrc->a[iFrom].pTab);
1879c31c2eb8Sdrh     }
1880f26e09c8Sdrh     sqliteFree(pSrc->a[iFrom].zDatabase);
1881c31c2eb8Sdrh     sqliteFree(pSrc->a[iFrom].zName);
1882c31c2eb8Sdrh     sqliteFree(pSrc->a[iFrom].zAlias);
1883c31c2eb8Sdrh     if( nSubSrc>1 ){
1884c31c2eb8Sdrh       int extra = nSubSrc - 1;
1885c31c2eb8Sdrh       for(i=1; i<nSubSrc; i++){
18864adee20fSdanielk1977         pSrc = sqlite3SrcListAppend(pSrc, 0, 0);
1887c31c2eb8Sdrh       }
1888c31c2eb8Sdrh       p->pSrc = pSrc;
1889c31c2eb8Sdrh       for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
1890c31c2eb8Sdrh         pSrc->a[i] = pSrc->a[i-extra];
1891c31c2eb8Sdrh       }
1892c31c2eb8Sdrh     }
1893c31c2eb8Sdrh     for(i=0; i<nSubSrc; i++){
1894c31c2eb8Sdrh       pSrc->a[i+iFrom] = pSubSrc->a[i];
1895c31c2eb8Sdrh       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
1896c31c2eb8Sdrh     }
18978af4d3acSdrh     pSrc->a[iFrom+nSubSrc-1].jointype = jointype;
1898c31c2eb8Sdrh   }
1899c31c2eb8Sdrh 
1900c31c2eb8Sdrh   /* Now begin substituting subquery result set expressions for
1901c31c2eb8Sdrh   ** references to the iParent in the outer query.
1902c31c2eb8Sdrh   **
1903c31c2eb8Sdrh   ** Example:
1904c31c2eb8Sdrh   **
1905c31c2eb8Sdrh   **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
1906c31c2eb8Sdrh   **   \                     \_____________ subquery __________/          /
1907c31c2eb8Sdrh   **    \_____________________ outer query ______________________________/
1908c31c2eb8Sdrh   **
1909c31c2eb8Sdrh   ** We look at every expression in the outer query and every place we see
1910c31c2eb8Sdrh   ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
1911c31c2eb8Sdrh   */
19126a3ea0e6Sdrh   substExprList(p->pEList, iParent, pSub->pEList);
1913832508b7Sdrh   pList = p->pEList;
1914832508b7Sdrh   for(i=0; i<pList->nExpr; i++){
19156977fea8Sdrh     Expr *pExpr;
19166977fea8Sdrh     if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
19176977fea8Sdrh       pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
1918832508b7Sdrh     }
1919832508b7Sdrh   }
19201b2e0329Sdrh   if( isAgg ){
19216a3ea0e6Sdrh     substExprList(p->pGroupBy, iParent, pSub->pEList);
19226a3ea0e6Sdrh     substExpr(p->pHaving, iParent, pSub->pEList);
19231b2e0329Sdrh   }
1924174b6195Sdrh   if( pSub->pOrderBy ){
1925174b6195Sdrh     assert( p->pOrderBy==0 );
1926174b6195Sdrh     p->pOrderBy = pSub->pOrderBy;
1927174b6195Sdrh     pSub->pOrderBy = 0;
1928174b6195Sdrh   }else if( p->pOrderBy ){
19296a3ea0e6Sdrh     substExprList(p->pOrderBy, iParent, pSub->pEList);
1930174b6195Sdrh   }
1931832508b7Sdrh   if( pSub->pWhere ){
19324adee20fSdanielk1977     pWhere = sqlite3ExprDup(pSub->pWhere);
1933832508b7Sdrh   }else{
1934832508b7Sdrh     pWhere = 0;
1935832508b7Sdrh   }
1936832508b7Sdrh   if( subqueryIsAgg ){
1937832508b7Sdrh     assert( p->pHaving==0 );
19381b2e0329Sdrh     p->pHaving = p->pWhere;
19391b2e0329Sdrh     p->pWhere = pWhere;
19406a3ea0e6Sdrh     substExpr(p->pHaving, iParent, pSub->pEList);
19411b2e0329Sdrh     if( pSub->pHaving ){
19424adee20fSdanielk1977       Expr *pHaving = sqlite3ExprDup(pSub->pHaving);
19431b2e0329Sdrh       if( p->pHaving ){
19444adee20fSdanielk1977         p->pHaving = sqlite3Expr(TK_AND, p->pHaving, pHaving, 0);
19451b2e0329Sdrh       }else{
19461b2e0329Sdrh         p->pHaving = pHaving;
19471b2e0329Sdrh       }
19481b2e0329Sdrh     }
19491b2e0329Sdrh     assert( p->pGroupBy==0 );
19504adee20fSdanielk1977     p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy);
1951832508b7Sdrh   }else if( p->pWhere==0 ){
1952832508b7Sdrh     p->pWhere = pWhere;
1953832508b7Sdrh   }else{
19546a3ea0e6Sdrh     substExpr(p->pWhere, iParent, pSub->pEList);
1955832508b7Sdrh     if( pWhere ){
19564adee20fSdanielk1977       p->pWhere = sqlite3Expr(TK_AND, p->pWhere, pWhere, 0);
1957832508b7Sdrh     }
1958832508b7Sdrh   }
1959c31c2eb8Sdrh 
1960c31c2eb8Sdrh   /* The flattened query is distinct if either the inner or the
1961c31c2eb8Sdrh   ** outer query is distinct.
1962c31c2eb8Sdrh   */
1963832508b7Sdrh   p->isDistinct = p->isDistinct || pSub->isDistinct;
19648c74a8caSdrh 
1965c31c2eb8Sdrh   /* Transfer the limit expression from the subquery to the outer
1966c31c2eb8Sdrh   ** query.
1967c31c2eb8Sdrh   */
1968df199a25Sdrh   if( pSub->nLimit>=0 ){
1969df199a25Sdrh     if( p->nLimit<0 ){
1970df199a25Sdrh       p->nLimit = pSub->nLimit;
1971df199a25Sdrh     }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1972df199a25Sdrh       p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1973df199a25Sdrh     }
1974df199a25Sdrh   }
1975df199a25Sdrh   p->nOffset += pSub->nOffset;
19768c74a8caSdrh 
1977c31c2eb8Sdrh   /* Finially, delete what is left of the subquery and return
1978c31c2eb8Sdrh   ** success.
1979c31c2eb8Sdrh   */
19804adee20fSdanielk1977   sqlite3SelectDelete(pSub);
1981832508b7Sdrh   return 1;
19821350b030Sdrh }
19831350b030Sdrh 
19841350b030Sdrh /*
19859562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it
19869562b551Sdrh ** is a simple min() or max() query.  If it is and this query can be
19879562b551Sdrh ** satisfied using a single seek to the beginning or end of an index,
1988e78e8284Sdrh ** then generate the code for this SELECT and return 1.  If this is not a
19899562b551Sdrh ** simple min() or max() query, then return 0;
19909562b551Sdrh **
19919562b551Sdrh ** A simply min() or max() query looks like this:
19929562b551Sdrh **
19939562b551Sdrh **    SELECT min(a) FROM table;
19949562b551Sdrh **    SELECT max(a) FROM table;
19959562b551Sdrh **
19969562b551Sdrh ** The query may have only a single table in its FROM argument.  There
19979562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses.  The result set must
19989562b551Sdrh ** be the min() or max() of a single column of the table.  The column
19999562b551Sdrh ** in the min() or max() function must be indexed.
20009562b551Sdrh **
20014adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select().
20029562b551Sdrh ** See the header comment on that routine for additional information.
20039562b551Sdrh */
20049562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
20059562b551Sdrh   Expr *pExpr;
20069562b551Sdrh   int iCol;
20079562b551Sdrh   Table *pTab;
20089562b551Sdrh   Index *pIdx;
20099562b551Sdrh   int base;
20109562b551Sdrh   Vdbe *v;
20119562b551Sdrh   int seekOp;
20129562b551Sdrh   int cont;
20136e17529eSdrh   ExprList *pEList, *pList, eList;
20149562b551Sdrh   struct ExprList_item eListItem;
20156e17529eSdrh   SrcList *pSrc;
20166e17529eSdrh 
20179562b551Sdrh 
20189562b551Sdrh   /* Check to see if this query is a simple min() or max() query.  Return
20199562b551Sdrh   ** zero if it is  not.
20209562b551Sdrh   */
20219562b551Sdrh   if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
20226e17529eSdrh   pSrc = p->pSrc;
20236e17529eSdrh   if( pSrc->nSrc!=1 ) return 0;
20246e17529eSdrh   pEList = p->pEList;
20256e17529eSdrh   if( pEList->nExpr!=1 ) return 0;
20266e17529eSdrh   pExpr = pEList->a[0].pExpr;
20279562b551Sdrh   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
20286e17529eSdrh   pList = pExpr->pList;
20296e17529eSdrh   if( pList==0 || pList->nExpr!=1 ) return 0;
20306977fea8Sdrh   if( pExpr->token.n!=3 ) return 0;
20314adee20fSdanielk1977   if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){
20320bce8354Sdrh     seekOp = OP_Rewind;
20334adee20fSdanielk1977   }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){
20340bce8354Sdrh     seekOp = OP_Last;
20350bce8354Sdrh   }else{
20360bce8354Sdrh     return 0;
20370bce8354Sdrh   }
20386e17529eSdrh   pExpr = pList->a[0].pExpr;
20399562b551Sdrh   if( pExpr->op!=TK_COLUMN ) return 0;
20409562b551Sdrh   iCol = pExpr->iColumn;
20416e17529eSdrh   pTab = pSrc->a[0].pTab;
20429562b551Sdrh 
20439562b551Sdrh   /* If we get to here, it means the query is of the correct form.
204417f71934Sdrh   ** Check to make sure we have an index and make pIdx point to the
204517f71934Sdrh   ** appropriate index.  If the min() or max() is on an INTEGER PRIMARY
204617f71934Sdrh   ** key column, no index is necessary so set pIdx to NULL.  If no
204717f71934Sdrh   ** usable index is found, return 0.
20489562b551Sdrh   */
20499562b551Sdrh   if( iCol<0 ){
20509562b551Sdrh     pIdx = 0;
20519562b551Sdrh   }else{
2052dc1bdc4fSdanielk1977     CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
20539562b551Sdrh     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
20549562b551Sdrh       assert( pIdx->nColumn>=1 );
2055dc1bdc4fSdanielk1977       if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break;
20569562b551Sdrh     }
20579562b551Sdrh     if( pIdx==0 ) return 0;
20589562b551Sdrh   }
20599562b551Sdrh 
2060e5f50722Sdrh   /* Identify column types if we will be using the callback.  This
20619562b551Sdrh   ** step is skipped if the output is going to a table or a memory cell.
2062e5f50722Sdrh   ** The column names have already been generated in the calling function.
20639562b551Sdrh   */
20644adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
20659562b551Sdrh   if( v==0 ) return 0;
20669562b551Sdrh 
20670c37e630Sdrh   /* If the output is destined for a temporary table, open that table.
20680c37e630Sdrh   */
20690c37e630Sdrh   if( eDest==SRT_TempTable ){
20704adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);
2071b4964b72Sdanielk1977     sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1);
20720c37e630Sdrh   }
20730c37e630Sdrh 
207417f71934Sdrh   /* Generating code to find the min or the max.  Basically all we have
207517f71934Sdrh   ** to do is find the first or the last entry in the chosen index.  If
207617f71934Sdrh   ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
207717f71934Sdrh   ** or last entry in the main table.
20789562b551Sdrh   */
20794adee20fSdanielk1977   sqlite3CodeVerifySchema(pParse, pTab->iDb);
20806e17529eSdrh   base = pSrc->a[0].iCursor;
20817b58daeaSdrh   computeLimitRegisters(pParse, p);
20826e17529eSdrh   if( pSrc->a[0].pSelect==0 ){
20834adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2084d3d39e93Sdrh     sqlite3VdbeAddOp(v, OP_OpenRead, base, pTab->tnum);
2085b4964b72Sdanielk1977     sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol);
20866e17529eSdrh   }
20874adee20fSdanielk1977   cont = sqlite3VdbeMakeLabel(v);
20889562b551Sdrh   if( pIdx==0 ){
20894adee20fSdanielk1977     sqlite3VdbeAddOp(v, seekOp, base, 0);
20909562b551Sdrh   }else{
20914adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
2092d3d39e93Sdrh     sqlite3VdbeOp3(v, OP_OpenRead, base+1, pIdx->tnum,
2093d3d39e93Sdrh                    (char*)&pIdx->keyInfo, P3_KEYINFO);
20949eb516c0Sdrh     if( seekOp==OP_Rewind ){
20951af3fdb4Sdrh       sqlite3VdbeAddOp(v, OP_String, 0, 0);
20961af3fdb4Sdrh       sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
20971af3fdb4Sdrh       seekOp = OP_MoveGt;
20989eb516c0Sdrh     }
20991af3fdb4Sdrh     sqlite3VdbeAddOp(v, seekOp, base+1, 0);
21004adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_IdxRecno, base+1, 0);
21014adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Close, base+1, 0);
21027cf6e4deSdrh     sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
21039562b551Sdrh   }
21045cf8e8c7Sdrh   eList.nExpr = 1;
21055cf8e8c7Sdrh   memset(&eListItem, 0, sizeof(eListItem));
21065cf8e8c7Sdrh   eList.a = &eListItem;
21075cf8e8c7Sdrh   eList.a[0].pExpr = pExpr;
210884ac9d02Sdanielk1977   selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0);
21094adee20fSdanielk1977   sqlite3VdbeResolveLabel(v, cont);
21104adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_Close, base, 0);
21116e17529eSdrh 
21129562b551Sdrh   return 1;
21139562b551Sdrh }
21149562b551Sdrh 
21159562b551Sdrh /*
21169bb61fe7Sdrh ** Generate code for the given SELECT statement.
21179bb61fe7Sdrh **
2118fef5208cSdrh ** The results are distributed in various ways depending on the
2119fef5208cSdrh ** value of eDest and iParm.
2120fef5208cSdrh **
2121fef5208cSdrh **     eDest Value       Result
2122fef5208cSdrh **     ------------    -------------------------------------------
2123fef5208cSdrh **     SRT_Callback    Invoke the callback for each row of the result.
2124fef5208cSdrh **
2125fef5208cSdrh **     SRT_Mem         Store first result in memory cell iParm
2126fef5208cSdrh **
2127e014a838Sdanielk1977 **     SRT_Set         Store results as keys of table iParm.
2128fef5208cSdrh **
212982c3d636Sdrh **     SRT_Union       Store results as a key in a temporary table iParm
213082c3d636Sdrh **
21314b11c6d3Sjplyon **     SRT_Except      Remove results from the temporary table iParm.
2132c4a3c779Sdrh **
2133c4a3c779Sdrh **     SRT_Table       Store results in temporary table iParm
21349bb61fe7Sdrh **
2135e78e8284Sdrh ** The table above is incomplete.  Additional eDist value have be added
2136e78e8284Sdrh ** since this comment was written.  See the selectInnerLoop() function for
2137e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings.
2138e78e8284Sdrh **
21399bb61fe7Sdrh ** This routine returns the number of errors.  If any errors are
21409bb61fe7Sdrh ** encountered, then an appropriate error message is left in
21419bb61fe7Sdrh ** pParse->zErrMsg.
21429bb61fe7Sdrh **
21439bb61fe7Sdrh ** This routine does NOT free the Select structure passed in.  The
21449bb61fe7Sdrh ** calling function needs to do that.
21451b2e0329Sdrh **
21461b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this
21471b2e0329Sdrh ** SELECT is a subquery.  This routine may try to combine this SELECT
21481b2e0329Sdrh ** with its parent to form a single flat query.  In so doing, it might
21491b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query.
21501b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it
21511b2e0329Sdrh ** can be changed.
2152e78e8284Sdrh **
2153e78e8284Sdrh ** Example 1:   The meaning of the pParent parameter.
2154e78e8284Sdrh **
2155e78e8284Sdrh **    SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
2156e78e8284Sdrh **    \                      \_______ subquery _______/        /
2157e78e8284Sdrh **     \                                                      /
2158e78e8284Sdrh **      \____________________ outer query ___________________/
2159e78e8284Sdrh **
2160e78e8284Sdrh ** This routine is called for the outer query first.   For that call,
2161e78e8284Sdrh ** pParent will be NULL.  During the processing of the outer query, this
2162e78e8284Sdrh ** routine is called recursively to handle the subquery.  For the recursive
2163e78e8284Sdrh ** call, pParent will point to the outer query.  Because the subquery is
2164e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will
2165e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.)
21669bb61fe7Sdrh */
21674adee20fSdanielk1977 int sqlite3Select(
2168cce7d176Sdrh   Parse *pParse,         /* The parser context */
21699bb61fe7Sdrh   Select *p,             /* The SELECT statement being coded. */
2170e78e8284Sdrh   int eDest,             /* How to dispose of the results */
2171e78e8284Sdrh   int iParm,             /* A parameter used by the eDest disposal method */
2172832508b7Sdrh   Select *pParent,       /* Another SELECT for which this is a sub-query */
2173832508b7Sdrh   int parentTab,         /* Index in pParent->pSrc of this query */
217484ac9d02Sdanielk1977   int *pParentAgg,       /* True if pParent uses aggregate functions */
217584ac9d02Sdanielk1977   char *aff              /* If eDest is SRT_Union, the affinity string */
2176cce7d176Sdrh ){
2177d8bc7086Sdrh   int i;
2178cce7d176Sdrh   WhereInfo *pWInfo;
2179cce7d176Sdrh   Vdbe *v;
2180cce7d176Sdrh   int isAgg = 0;         /* True for select lists like "count(*)" */
2181a2e00042Sdrh   ExprList *pEList;      /* List of columns to extract. */
2182ad3cab52Sdrh   SrcList *pTabList;     /* List of tables to select from */
21839bb61fe7Sdrh   Expr *pWhere;          /* The WHERE clause.  May be NULL */
21849bb61fe7Sdrh   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
21852282792aSdrh   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
21862282792aSdrh   Expr *pHaving;         /* The HAVING clause.  May be NULL */
218719a775c2Sdrh   int isDistinct;        /* True if the DISTINCT keyword is present */
218819a775c2Sdrh   int distinct;          /* Table to use for the distinct set */
21891d83f052Sdrh   int rc = 1;            /* Value to return from this function */
21909bb61fe7Sdrh 
21916f8a503dSdanielk1977   if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1;
21924adee20fSdanielk1977   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
2193daffd0e5Sdrh 
219482c3d636Sdrh   /* If there is are a sequence of queries, do the earlier ones first.
219582c3d636Sdrh   */
219682c3d636Sdrh   if( p->pPrior ){
219784ac9d02Sdanielk1977     return multiSelect(pParse, p, eDest, iParm, aff);
219882c3d636Sdrh   }
219982c3d636Sdrh 
220082c3d636Sdrh   /* Make local copies of the parameters for this query.
220182c3d636Sdrh   */
22029bb61fe7Sdrh   pTabList = p->pSrc;
22039bb61fe7Sdrh   pWhere = p->pWhere;
22049bb61fe7Sdrh   pOrderBy = p->pOrderBy;
22052282792aSdrh   pGroupBy = p->pGroupBy;
22062282792aSdrh   pHaving = p->pHaving;
220719a775c2Sdrh   isDistinct = p->isDistinct;
22089bb61fe7Sdrh 
22096a3ea0e6Sdrh   /* Allocate VDBE cursors for each table in the FROM clause
221010e5e3cfSdrh   */
22114adee20fSdanielk1977   sqlite3SrcListAssignCursors(pParse, pTabList);
221210e5e3cfSdrh 
22139bb61fe7Sdrh   /*
22149bb61fe7Sdrh   ** Do not even attempt to generate any code if we have already seen
22159bb61fe7Sdrh   ** errors before this routine starts.
22169bb61fe7Sdrh   */
22171d83f052Sdrh   if( pParse->nErr>0 ) goto select_end;
2218cce7d176Sdrh 
2219e78e8284Sdrh   /* Expand any "*" terms in the result set.  (For example the "*" in
2220e78e8284Sdrh   ** "SELECT * FROM t1")  The fillInColumnlist() routine also does some
2221e78e8284Sdrh   ** other housekeeping - see the header comment for details.
2222cce7d176Sdrh   */
2223d8bc7086Sdrh   if( fillInColumnList(pParse, p) ){
22241d83f052Sdrh     goto select_end;
2225cce7d176Sdrh   }
2226ad2d8307Sdrh   pWhere = p->pWhere;
2227d8bc7086Sdrh   pEList = p->pEList;
22281d83f052Sdrh   if( pEList==0 ) goto select_end;
2229cce7d176Sdrh 
22302282792aSdrh   /* If writing to memory or generating a set
22312282792aSdrh   ** only a single column may be output.
223219a775c2Sdrh   */
2233fef5208cSdrh   if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
22344adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "only a single result allowed for "
2235da93d238Sdrh        "a SELECT that is part of an expression");
22361d83f052Sdrh     goto select_end;
223719a775c2Sdrh   }
223819a775c2Sdrh 
2239c926afbcSdrh   /* ORDER BY is ignored for some destinations.
22402282792aSdrh   */
2241c926afbcSdrh   switch( eDest ){
2242c926afbcSdrh     case SRT_Union:
2243c926afbcSdrh     case SRT_Except:
2244c926afbcSdrh     case SRT_Discard:
2245f93bbbeaSdanielk1977     case SRT_Set:
2246acd4c695Sdrh       pOrderBy = 0;
2247c926afbcSdrh       break;
2248c926afbcSdrh     default:
2249c926afbcSdrh       break;
22502282792aSdrh   }
22512282792aSdrh 
225210e5e3cfSdrh   /* At this point, we should have allocated all the cursors that we
2253832508b7Sdrh   ** need to handle subquerys and temporary tables.
225410e5e3cfSdrh   **
2255967e8b73Sdrh   ** Resolve the column names and do a semantics check on all the expressions.
22562282792aSdrh   */
22574794b980Sdrh   for(i=0; i<pEList->nExpr; i++){
22584adee20fSdanielk1977     if( sqlite3ExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){
22591d83f052Sdrh       goto select_end;
2260cce7d176Sdrh     }
22614adee20fSdanielk1977     if( sqlite3ExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
22621d83f052Sdrh       goto select_end;
2263cce7d176Sdrh     }
2264cce7d176Sdrh   }
2265cce7d176Sdrh   if( pWhere ){
22664adee20fSdanielk1977     if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pWhere) ){
22671d83f052Sdrh       goto select_end;
2268cce7d176Sdrh     }
22694adee20fSdanielk1977     if( sqlite3ExprCheck(pParse, pWhere, 0, 0) ){
22701d83f052Sdrh       goto select_end;
2271cce7d176Sdrh     }
2272cce7d176Sdrh   }
2273c66c5a26Sdrh   if( pHaving ){
2274c66c5a26Sdrh     if( pGroupBy==0 ){
22754adee20fSdanielk1977       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
2276c66c5a26Sdrh       goto select_end;
2277c66c5a26Sdrh     }
22784adee20fSdanielk1977     if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pHaving) ){
2279c66c5a26Sdrh       goto select_end;
2280c66c5a26Sdrh     }
22814adee20fSdanielk1977     if( sqlite3ExprCheck(pParse, pHaving, 1, &isAgg) ){
2282c66c5a26Sdrh       goto select_end;
2283c66c5a26Sdrh     }
2284c66c5a26Sdrh   }
2285cce7d176Sdrh   if( pOrderBy ){
2286cce7d176Sdrh     for(i=0; i<pOrderBy->nExpr; i++){
2287e4de1febSdrh       int iCol;
228888eee38aSdrh       Expr *pE = pOrderBy->a[i].pExpr;
22894adee20fSdanielk1977       if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
22904adee20fSdanielk1977         sqlite3ExprDelete(pE);
22914adee20fSdanielk1977         pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr);
229288eee38aSdrh       }
22934adee20fSdanielk1977       if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pE) ){
229488eee38aSdrh         goto select_end;
229588eee38aSdrh       }
22964adee20fSdanielk1977       if( sqlite3ExprCheck(pParse, pE, isAgg, 0) ){
229788eee38aSdrh         goto select_end;
229888eee38aSdrh       }
22994adee20fSdanielk1977       if( sqlite3ExprIsConstant(pE) ){
23004adee20fSdanielk1977         if( sqlite3ExprIsInteger(pE, &iCol)==0 ){
23014adee20fSdanielk1977           sqlite3ErrorMsg(pParse,
2302da93d238Sdrh              "ORDER BY terms must not be non-integer constants");
23031d83f052Sdrh           goto select_end;
2304e4de1febSdrh         }else if( iCol<=0 || iCol>pEList->nExpr ){
23054adee20fSdanielk1977           sqlite3ErrorMsg(pParse,
2306da93d238Sdrh              "ORDER BY column number %d out of range - should be "
2307e4de1febSdrh              "between 1 and %d", iCol, pEList->nExpr);
2308e4de1febSdrh           goto select_end;
2309e4de1febSdrh         }
2310cce7d176Sdrh       }
2311cce7d176Sdrh     }
2312cce7d176Sdrh   }
23132282792aSdrh   if( pGroupBy ){
23142282792aSdrh     for(i=0; i<pGroupBy->nExpr; i++){
231588eee38aSdrh       int iCol;
23162282792aSdrh       Expr *pE = pGroupBy->a[i].pExpr;
23174adee20fSdanielk1977       if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
23184adee20fSdanielk1977         sqlite3ExprDelete(pE);
23194adee20fSdanielk1977         pE = pGroupBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr);
23209208643dSdrh       }
23214adee20fSdanielk1977       if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pE) ){
23221d83f052Sdrh         goto select_end;
23232282792aSdrh       }
23244adee20fSdanielk1977       if( sqlite3ExprCheck(pParse, pE, isAgg, 0) ){
23251d83f052Sdrh         goto select_end;
23262282792aSdrh       }
23274adee20fSdanielk1977       if( sqlite3ExprIsConstant(pE) ){
23284adee20fSdanielk1977         if( sqlite3ExprIsInteger(pE, &iCol)==0 ){
23294adee20fSdanielk1977           sqlite3ErrorMsg(pParse,
2330da93d238Sdrh             "GROUP BY terms must not be non-integer constants");
233188eee38aSdrh           goto select_end;
233288eee38aSdrh         }else if( iCol<=0 || iCol>pEList->nExpr ){
23334adee20fSdanielk1977           sqlite3ErrorMsg(pParse,
2334da93d238Sdrh              "GROUP BY column number %d out of range - should be "
233588eee38aSdrh              "between 1 and %d", iCol, pEList->nExpr);
233688eee38aSdrh           goto select_end;
233788eee38aSdrh         }
233888eee38aSdrh       }
23392282792aSdrh     }
23402282792aSdrh   }
2341cce7d176Sdrh 
2342d820cb1bSdrh   /* Begin generating code.
2343d820cb1bSdrh   */
23444adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
2345d820cb1bSdrh   if( v==0 ) goto select_end;
2346d820cb1bSdrh 
2347e78e8284Sdrh   /* Identify column names if we will be using them in a callback.  This
2348e78e8284Sdrh   ** step is skipped if the output is going to some other destination.
23490bb28106Sdrh   */
23500bb28106Sdrh   if( eDest==SRT_Callback ){
23516a3ea0e6Sdrh     generateColumnNames(pParse, pTabList, pEList);
23520bb28106Sdrh   }
23530bb28106Sdrh 
2354d3d39e93Sdrh #if 1  /* I do not think we need the following code any more.... */
235584ac9d02Sdanielk1977   /* If the destination is SRT_Union, then set the number of columns in
235684ac9d02Sdanielk1977   ** the records that will be inserted into the temporary table. The caller
235784ac9d02Sdanielk1977   ** couldn't do this, in case the select statement is of the form
235884ac9d02Sdanielk1977   ** "SELECT * FROM ....".
235984ac9d02Sdanielk1977   **
236084ac9d02Sdanielk1977   ** We need to do this before we start inserting records into the
236184ac9d02Sdanielk1977   ** temporary table (which has had OP_KeyAsData executed on it), because
236284ac9d02Sdanielk1977   ** it is required by the key comparison function. So do it now, even
236384ac9d02Sdanielk1977   ** though this means that OP_SetNumColumns may be executed on the same
236484ac9d02Sdanielk1977   ** cursor more than once.
236584ac9d02Sdanielk1977   */
236684ac9d02Sdanielk1977   if( eDest==SRT_Union ){
236784ac9d02Sdanielk1977     sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr);
236884ac9d02Sdanielk1977   }
2369d3d39e93Sdrh #endif
237084ac9d02Sdanielk1977 
2371d820cb1bSdrh   /* Generate code for all sub-queries in the FROM clause
2372d820cb1bSdrh   */
2373ad3cab52Sdrh   for(i=0; i<pTabList->nSrc; i++){
2374742f947bSdanielk1977     const char *zSavedAuthContext = 0;
2375c31c2eb8Sdrh     int needRestoreContext;
2376c31c2eb8Sdrh 
2377a76b5dfcSdrh     if( pTabList->a[i].pSelect==0 ) continue;
23785cf590c1Sdrh     if( pTabList->a[i].zName!=0 ){
23795cf590c1Sdrh       zSavedAuthContext = pParse->zAuthContext;
23805cf590c1Sdrh       pParse->zAuthContext = pTabList->a[i].zName;
2381c31c2eb8Sdrh       needRestoreContext = 1;
2382c31c2eb8Sdrh     }else{
2383c31c2eb8Sdrh       needRestoreContext = 0;
23845cf590c1Sdrh     }
23854adee20fSdanielk1977     sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable,
238684ac9d02Sdanielk1977                  pTabList->a[i].iCursor, p, i, &isAgg, 0);
2387c31c2eb8Sdrh     if( needRestoreContext ){
23885cf590c1Sdrh       pParse->zAuthContext = zSavedAuthContext;
23895cf590c1Sdrh     }
2390832508b7Sdrh     pTabList = p->pSrc;
2391832508b7Sdrh     pWhere = p->pWhere;
2392c31c2eb8Sdrh     if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){
2393832508b7Sdrh       pOrderBy = p->pOrderBy;
2394acd4c695Sdrh     }
2395832508b7Sdrh     pGroupBy = p->pGroupBy;
2396832508b7Sdrh     pHaving = p->pHaving;
2397832508b7Sdrh     isDistinct = p->isDistinct;
23981b2e0329Sdrh   }
23991b2e0329Sdrh 
24006e17529eSdrh   /* Check for the special case of a min() or max() function by itself
24016e17529eSdrh   ** in the result set.
24026e17529eSdrh   */
24036e17529eSdrh   if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
24046e17529eSdrh     rc = 0;
24056e17529eSdrh     goto select_end;
24066e17529eSdrh   }
24076e17529eSdrh 
24081b2e0329Sdrh   /* Check to see if this is a subquery that can be "flattened" into its parent.
24091b2e0329Sdrh   ** If flattening is a possiblity, do so and return immediately.
24101b2e0329Sdrh   */
24111b2e0329Sdrh   if( pParent && pParentAgg &&
24128c74a8caSdrh       flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
24131b2e0329Sdrh     if( isAgg ) *pParentAgg = 1;
24141b2e0329Sdrh     return rc;
24151b2e0329Sdrh   }
2416832508b7Sdrh 
24177cedc8d4Sdanielk1977   /* If there is an ORDER BY clause, resolve any collation sequences
24187cedc8d4Sdanielk1977   ** names that have been explicitly specified.
24197cedc8d4Sdanielk1977   */
24207cedc8d4Sdanielk1977   if( pOrderBy ){
24217cedc8d4Sdanielk1977     for(i=0; i<pOrderBy->nExpr; i++){
24227cedc8d4Sdanielk1977       if( pOrderBy->a[i].zName ){
24237cedc8d4Sdanielk1977         pOrderBy->a[i].pExpr->pColl =
24247cedc8d4Sdanielk1977             sqlite3LocateCollSeq(pParse, pOrderBy->a[i].zName, -1);
24257cedc8d4Sdanielk1977       }
24267cedc8d4Sdanielk1977     }
24277cedc8d4Sdanielk1977     if( pParse->nErr ){
24287cedc8d4Sdanielk1977       goto select_end;
24297cedc8d4Sdanielk1977     }
24307cedc8d4Sdanielk1977   }
24317cedc8d4Sdanielk1977 
24327b58daeaSdrh   /* Set the limiter.
24337b58daeaSdrh   */
24347b58daeaSdrh   computeLimitRegisters(pParse, p);
24357b58daeaSdrh 
24362d0794e3Sdrh   /* If the output is destined for a temporary table, open that table.
24372d0794e3Sdrh   */
24382d0794e3Sdrh   if( eDest==SRT_TempTable ){
24394adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);
2440b4964b72Sdanielk1977     sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr);
24412d0794e3Sdrh   }
24422d0794e3Sdrh 
24432282792aSdrh   /* Do an analysis of aggregate expressions.
2444efb7251dSdrh   */
2445d820cb1bSdrh   sqliteAggregateInfoReset(pParse);
2446bb999ef6Sdrh   if( isAgg || pGroupBy ){
24470bce8354Sdrh     assert( pParse->nAgg==0 );
2448bb999ef6Sdrh     isAgg = 1;
24492282792aSdrh     for(i=0; i<pEList->nExpr; i++){
24504adee20fSdanielk1977       if( sqlite3ExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
24511d83f052Sdrh         goto select_end;
24522282792aSdrh       }
24532282792aSdrh     }
24542282792aSdrh     if( pGroupBy ){
24552282792aSdrh       for(i=0; i<pGroupBy->nExpr; i++){
24564adee20fSdanielk1977         if( sqlite3ExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
24571d83f052Sdrh           goto select_end;
24582282792aSdrh         }
24592282792aSdrh       }
24602282792aSdrh     }
24614adee20fSdanielk1977     if( pHaving && sqlite3ExprAnalyzeAggregates(pParse, pHaving) ){
24621d83f052Sdrh       goto select_end;
24632282792aSdrh     }
2464191b690eSdrh     if( pOrderBy ){
2465191b690eSdrh       for(i=0; i<pOrderBy->nExpr; i++){
24664adee20fSdanielk1977         if( sqlite3ExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
24671d83f052Sdrh           goto select_end;
2468191b690eSdrh         }
2469191b690eSdrh       }
2470191b690eSdrh     }
2471efb7251dSdrh   }
2472efb7251dSdrh 
24732282792aSdrh   /* Reset the aggregator
2474cce7d176Sdrh   */
2475cce7d176Sdrh   if( isAgg ){
2476e159fdf2Sdanielk1977     int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg);
2477e5095355Sdrh     for(i=0; i<pParse->nAgg; i++){
24780bce8354Sdrh       FuncDef *pFunc;
24790bce8354Sdrh       if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
2480f9b596ebSdrh         sqlite3VdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_FUNCDEF);
2481e5095355Sdrh       }
2482e5095355Sdrh     }
2483e159fdf2Sdanielk1977     if( pGroupBy ){
2484ce2663ccSdanielk1977       int sz = sizeof(KeyInfo) + pGroupBy->nExpr*sizeof(CollSeq*);
2485ce2663ccSdanielk1977       KeyInfo *pKey = (KeyInfo *)sqliteMalloc(sz);
2486ce2663ccSdanielk1977       if( 0==pKey ){
2487ce2663ccSdanielk1977         goto select_end;
2488ce2663ccSdanielk1977       }
2489ce2663ccSdanielk1977       pKey->enc = pParse->db->enc;
2490ce2663ccSdanielk1977       pKey->nField = pGroupBy->nExpr;
2491ce2663ccSdanielk1977       for(i=0; i<pGroupBy->nExpr; i++){
2492ce2663ccSdanielk1977         pKey->aColl[i] = sqlite3ExprCollSeq(pParse, pGroupBy->a[i].pExpr);
2493ce2663ccSdanielk1977         if( !pKey->aColl[i] ){
2494ce2663ccSdanielk1977           pKey->aColl[i] = pParse->db->pDfltColl;
2495ce2663ccSdanielk1977         }
2496ce2663ccSdanielk1977       }
2497ce2663ccSdanielk1977       sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF);
24981bee3d7bSdrh     }
2499cce7d176Sdrh   }
2500cce7d176Sdrh 
250119a775c2Sdrh   /* Initialize the memory cell to NULL
250219a775c2Sdrh   */
2503fef5208cSdrh   if( eDest==SRT_Mem ){
25040f69c1e3Sdanielk1977     sqlite3VdbeAddOp(v, OP_String8, 0, 0);
25054adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
250619a775c2Sdrh   }
250719a775c2Sdrh 
2508832508b7Sdrh   /* Open a temporary table to use for the distinct set.
2509cce7d176Sdrh   */
251019a775c2Sdrh   if( isDistinct ){
2511832508b7Sdrh     distinct = pParse->nTab++;
2512d3d39e93Sdrh     openTempIndex(pParse, p, distinct, 0);
2513832508b7Sdrh   }else{
2514832508b7Sdrh     distinct = -1;
2515efb7251dSdrh   }
2516832508b7Sdrh 
2517832508b7Sdrh   /* Begin the database scan
2518832508b7Sdrh   */
25194adee20fSdanielk1977   pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0,
252068d2e591Sdrh                             pGroupBy ? 0 : &pOrderBy);
25211d83f052Sdrh   if( pWInfo==0 ) goto select_end;
2522cce7d176Sdrh 
25232282792aSdrh   /* Use the standard inner loop if we are not dealing with
25242282792aSdrh   ** aggregates
2525cce7d176Sdrh   */
2526da9d6c45Sdrh   if( !isAgg ){
2527df199a25Sdrh     if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
252884ac9d02Sdanielk1977                     iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){
25291d83f052Sdrh        goto select_end;
2530cce7d176Sdrh     }
2531da9d6c45Sdrh   }
2532cce7d176Sdrh 
2533e3184744Sdrh   /* If we are dealing with aggregates, then do the special aggregate
25342282792aSdrh   ** processing.
2535efb7251dSdrh   */
25362282792aSdrh   else{
2537268380caSdrh     AggExpr *pAgg;
25382282792aSdrh     if( pGroupBy ){
25391bee3d7bSdrh       int lbl1;
25402282792aSdrh       for(i=0; i<pGroupBy->nExpr; i++){
25414adee20fSdanielk1977         sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr);
2542efb7251dSdrh       }
2543ededfd5eSdanielk1977       /* No affinity string is attached to the following OP_MakeRecord
2544d3d39e93Sdrh       ** because we do not need to do any coercion of datatypes. */
2545ededfd5eSdanielk1977       sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0);
25464adee20fSdanielk1977       lbl1 = sqlite3VdbeMakeLabel(v);
25474adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1);
2548268380caSdrh       for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){
2549268380caSdrh         if( pAgg->isAgg ) continue;
25504adee20fSdanielk1977         sqlite3ExprCode(pParse, pAgg->pExpr);
25514adee20fSdanielk1977         sqlite3VdbeAddOp(v, OP_AggSet, 0, i);
25522282792aSdrh       }
25534adee20fSdanielk1977       sqlite3VdbeResolveLabel(v, lbl1);
25542282792aSdrh     }
2555268380caSdrh     for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){
25562282792aSdrh       Expr *pE;
2557268380caSdrh       int nExpr;
2558268380caSdrh       FuncDef *pDef;
2559268380caSdrh       if( !pAgg->isAgg ) continue;
2560268380caSdrh       assert( pAgg->pFunc!=0 );
2561268380caSdrh       assert( pAgg->pFunc->xStep!=0 );
2562268380caSdrh       pDef = pAgg->pFunc;
2563268380caSdrh       pE = pAgg->pExpr;
2564268380caSdrh       assert( pE!=0 );
25652282792aSdrh       assert( pE->op==TK_AGG_FUNCTION );
2566f9b596ebSdrh       nExpr = sqlite3ExprCodeExprList(pParse, pE->pList);
25674adee20fSdanielk1977       sqlite3VdbeAddOp(v, OP_Integer, i, 0);
2568dc1bdc4fSdanielk1977       if( pDef->needCollSeq ){
2569dc1bdc4fSdanielk1977         CollSeq *pColl = 0;
2570dc1bdc4fSdanielk1977         int j;
2571dc1bdc4fSdanielk1977         for(j=0; !pColl && j<nExpr; j++){
2572dc1bdc4fSdanielk1977           pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr);
2573dc1bdc4fSdanielk1977         }
2574dc1bdc4fSdanielk1977         if( !pColl ) pColl = pParse->db->pDfltColl;
2575dc1bdc4fSdanielk1977         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
2576dc1bdc4fSdanielk1977       }
25774adee20fSdanielk1977       sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER);
25782282792aSdrh     }
25792282792aSdrh   }
25802282792aSdrh 
2581cce7d176Sdrh   /* End the database scan loop.
2582cce7d176Sdrh   */
25834adee20fSdanielk1977   sqlite3WhereEnd(pWInfo);
2584cce7d176Sdrh 
25852282792aSdrh   /* If we are processing aggregates, we need to set up a second loop
25862282792aSdrh   ** over all of the aggregate values and process them.
25872282792aSdrh   */
25882282792aSdrh   if( isAgg ){
25894adee20fSdanielk1977     int endagg = sqlite3VdbeMakeLabel(v);
25902282792aSdrh     int startagg;
25914adee20fSdanielk1977     startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg);
25922282792aSdrh     pParse->useAgg = 1;
25932282792aSdrh     if( pHaving ){
25944adee20fSdanielk1977       sqlite3ExprIfFalse(pParse, pHaving, startagg, 1);
25952282792aSdrh     }
2596df199a25Sdrh     if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
259784ac9d02Sdanielk1977                     iParm, startagg, endagg, aff) ){
25981d83f052Sdrh       goto select_end;
25992282792aSdrh     }
26004adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Goto, 0, startagg);
26014adee20fSdanielk1977     sqlite3VdbeResolveLabel(v, endagg);
26024adee20fSdanielk1977     sqlite3VdbeAddOp(v, OP_Noop, 0, 0);
26032282792aSdrh     pParse->useAgg = 0;
26042282792aSdrh   }
26052282792aSdrh 
2606cce7d176Sdrh   /* If there is an ORDER BY clause, then we need to sort the results
2607cce7d176Sdrh   ** and send them to the callback one by one.
2608cce7d176Sdrh   */
2609cce7d176Sdrh   if( pOrderBy ){
2610ffbc3088Sdrh     generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm);
2611cce7d176Sdrh   }
26126a535340Sdrh 
2613f620b4e2Sdrh   /* If this was a subquery, we have now converted the subquery into a
2614f620b4e2Sdrh   ** temporary table.  So delete the subquery structure from the parent
2615f620b4e2Sdrh   ** to prevent this subquery from being evaluated again and to force the
2616f620b4e2Sdrh   ** the use of the temporary table.
2617f620b4e2Sdrh   */
2618f620b4e2Sdrh   if( pParent ){
2619f620b4e2Sdrh     assert( pParent->pSrc->nSrc>parentTab );
2620f620b4e2Sdrh     assert( pParent->pSrc->a[parentTab].pSelect==p );
26214adee20fSdanielk1977     sqlite3SelectDelete(p);
2622f620b4e2Sdrh     pParent->pSrc->a[parentTab].pSelect = 0;
2623f620b4e2Sdrh   }
2624f620b4e2Sdrh 
26251d83f052Sdrh   /* The SELECT was successfully coded.   Set the return code to 0
26261d83f052Sdrh   ** to indicate no errors.
26271d83f052Sdrh   */
26281d83f052Sdrh   rc = 0;
26291d83f052Sdrh 
26301d83f052Sdrh   /* Control jumps to here if an error is encountered above, or upon
26311d83f052Sdrh   ** successful coding of the SELECT.
26321d83f052Sdrh   */
26331d83f052Sdrh select_end:
26341d83f052Sdrh   sqliteAggregateInfoReset(pParse);
26351d83f052Sdrh   return rc;
2636cce7d176Sdrh }
2637