xref: /sqlite-3.40.0/src/select.c (revision c754fa54)
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*c754fa54Sdrh ** $Id: select.c,v 1.87 2002/05/27 03:25:52 drh Exp $
16cce7d176Sdrh */
17cce7d176Sdrh #include "sqliteInt.h"
18cce7d176Sdrh 
19cce7d176Sdrh /*
209bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that
219bb61fe7Sdrh ** structure.
22cce7d176Sdrh */
239bb61fe7Sdrh Select *sqliteSelectNew(
24daffd0e5Sdrh   ExprList *pEList,     /* which columns to include in the result */
25ad3cab52Sdrh   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
26daffd0e5Sdrh   Expr *pWhere,         /* the WHERE clause */
27daffd0e5Sdrh   ExprList *pGroupBy,   /* the GROUP BY clause */
28daffd0e5Sdrh   Expr *pHaving,        /* the HAVING clause */
29daffd0e5Sdrh   ExprList *pOrderBy,   /* the ORDER BY clause */
309bbca4c1Sdrh   int isDistinct,       /* true if the DISTINCT keyword is present */
319bbca4c1Sdrh   int nLimit,           /* LIMIT value.  -1 means not used */
329bbca4c1Sdrh   int nOffset           /* OFFSET value.  -1 means not used */
339bb61fe7Sdrh ){
349bb61fe7Sdrh   Select *pNew;
359bb61fe7Sdrh   pNew = sqliteMalloc( sizeof(*pNew) );
36daffd0e5Sdrh   if( pNew==0 ){
37daffd0e5Sdrh     sqliteExprListDelete(pEList);
38ad3cab52Sdrh     sqliteSrcListDelete(pSrc);
39daffd0e5Sdrh     sqliteExprDelete(pWhere);
40daffd0e5Sdrh     sqliteExprListDelete(pGroupBy);
41daffd0e5Sdrh     sqliteExprDelete(pHaving);
42daffd0e5Sdrh     sqliteExprListDelete(pOrderBy);
43daffd0e5Sdrh   }else{
449bb61fe7Sdrh     pNew->pEList = pEList;
459bb61fe7Sdrh     pNew->pSrc = pSrc;
469bb61fe7Sdrh     pNew->pWhere = pWhere;
479bb61fe7Sdrh     pNew->pGroupBy = pGroupBy;
489bb61fe7Sdrh     pNew->pHaving = pHaving;
499bb61fe7Sdrh     pNew->pOrderBy = pOrderBy;
509bb61fe7Sdrh     pNew->isDistinct = isDistinct;
5182c3d636Sdrh     pNew->op = TK_SELECT;
529bbca4c1Sdrh     pNew->nLimit = nLimit;
539bbca4c1Sdrh     pNew->nOffset = nOffset;
54daffd0e5Sdrh   }
559bb61fe7Sdrh   return pNew;
569bb61fe7Sdrh }
579bb61fe7Sdrh 
589bb61fe7Sdrh /*
5901f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
6001f3f253Sdrh ** type of join.  Return an integer constant that expresses that type
6101f3f253Sdrh ** in terms of the following bit values:
6201f3f253Sdrh **
6301f3f253Sdrh **     JT_INNER
6401f3f253Sdrh **     JT_OUTER
6501f3f253Sdrh **     JT_NATURAL
6601f3f253Sdrh **     JT_LEFT
6701f3f253Sdrh **     JT_RIGHT
6801f3f253Sdrh **
6901f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
7001f3f253Sdrh **
7101f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return
7201f3f253Sdrh ** a join type, but put an error in the pParse structure.
7301f3f253Sdrh */
7401f3f253Sdrh int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
7501f3f253Sdrh   int jointype = 0;
7601f3f253Sdrh   Token *apAll[3];
7701f3f253Sdrh   Token *p;
7801f3f253Sdrh   static struct {
7901f3f253Sdrh     const char *zKeyword;
8001f3f253Sdrh     int nChar;
8101f3f253Sdrh     int code;
8201f3f253Sdrh   } keywords[] = {
8301f3f253Sdrh     { "natural", 7, JT_NATURAL },
84195e6967Sdrh     { "left",    4, JT_LEFT|JT_OUTER },
85195e6967Sdrh     { "right",   5, JT_RIGHT|JT_OUTER },
86195e6967Sdrh     { "full",    4, JT_LEFT|JT_RIGHT|JT_OUTER },
8701f3f253Sdrh     { "outer",   5, JT_OUTER },
8801f3f253Sdrh     { "inner",   5, JT_INNER },
8901f3f253Sdrh     { "cross",   5, JT_INNER },
9001f3f253Sdrh   };
9101f3f253Sdrh   int i, j;
9201f3f253Sdrh   apAll[0] = pA;
9301f3f253Sdrh   apAll[1] = pB;
9401f3f253Sdrh   apAll[2] = pC;
95195e6967Sdrh   for(i=0; i<3 && apAll[i]; i++){
9601f3f253Sdrh     p = apAll[i];
9701f3f253Sdrh     for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
9801f3f253Sdrh       if( p->n==keywords[j].nChar
9901f3f253Sdrh           && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
10001f3f253Sdrh         jointype |= keywords[j].code;
10101f3f253Sdrh         break;
10201f3f253Sdrh       }
10301f3f253Sdrh     }
10401f3f253Sdrh     if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
10501f3f253Sdrh       jointype |= JT_ERROR;
10601f3f253Sdrh       break;
10701f3f253Sdrh     }
10801f3f253Sdrh   }
109ad2d8307Sdrh   if(
110ad2d8307Sdrh      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
111195e6967Sdrh      (jointype & JT_ERROR)!=0
112ad2d8307Sdrh   ){
11301f3f253Sdrh     static Token dummy = { 0, 0 };
11401f3f253Sdrh     char *zSp1 = " ", *zSp2 = " ";
11501f3f253Sdrh     if( pB==0 ){ pB = &dummy; zSp1 = 0; }
11601f3f253Sdrh     if( pC==0 ){ pC = &dummy; zSp2 = 0; }
11701f3f253Sdrh     sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
11801f3f253Sdrh        pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
11901f3f253Sdrh     pParse->nErr++;
12001f3f253Sdrh     jointype = JT_INNER;
121195e6967Sdrh   }else if( jointype & JT_RIGHT ){
122195e6967Sdrh     sqliteSetString(&pParse->zErrMsg,
123195e6967Sdrh       "RIGHT and FULL OUTER JOINs are not currently supported", 0);
124195e6967Sdrh     pParse->nErr++;
125195e6967Sdrh     jointype = JT_INNER;
12601f3f253Sdrh   }
12701f3f253Sdrh   return jointype;
12801f3f253Sdrh }
12901f3f253Sdrh 
13001f3f253Sdrh /*
131ad2d8307Sdrh ** Return the index of a column in a table.  Return -1 if the column
132ad2d8307Sdrh ** is not contained in the table.
133ad2d8307Sdrh */
134ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){
135ad2d8307Sdrh   int i;
136ad2d8307Sdrh   for(i=0; i<pTab->nCol; i++){
137ad2d8307Sdrh     if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
138ad2d8307Sdrh   }
139ad2d8307Sdrh   return -1;
140ad2d8307Sdrh }
141ad2d8307Sdrh 
142ad2d8307Sdrh /*
143ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the
144ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2.
145ad2d8307Sdrh */
146ad2d8307Sdrh static void addWhereTerm(
147ad2d8307Sdrh   const char *zCol,        /* Name of the column */
148ad2d8307Sdrh   const Table *pTab1,      /* First table */
149ad2d8307Sdrh   const Table *pTab2,      /* Second table */
150ad2d8307Sdrh   Expr **ppExpr            /* Add the equality term to this expression */
151ad2d8307Sdrh ){
152ad2d8307Sdrh   Token dummy;
153ad2d8307Sdrh   Expr *pE1a, *pE1b, *pE1c;
154ad2d8307Sdrh   Expr *pE2a, *pE2b, *pE2c;
155ad2d8307Sdrh   Expr *pE;
156ad2d8307Sdrh 
157ad2d8307Sdrh   dummy.z = zCol;
158ad2d8307Sdrh   dummy.n = strlen(zCol);
159ad2d8307Sdrh   pE1a = sqliteExpr(TK_ID, 0, 0, &dummy);
160ad2d8307Sdrh   pE2a = sqliteExpr(TK_ID, 0, 0, &dummy);
161ad2d8307Sdrh   dummy.z = pTab1->zName;
162ad2d8307Sdrh   dummy.n = strlen(dummy.z);
163ad2d8307Sdrh   pE1b = sqliteExpr(TK_ID, 0, 0, &dummy);
164ad2d8307Sdrh   dummy.z = pTab2->zName;
165ad2d8307Sdrh   dummy.n = strlen(dummy.z);
166ad2d8307Sdrh   pE2b = sqliteExpr(TK_ID, 0, 0, &dummy);
167ad2d8307Sdrh   pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0);
168ad2d8307Sdrh   pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0);
169ad2d8307Sdrh   pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0);
170ad2d8307Sdrh   if( *ppExpr ){
171ad2d8307Sdrh     *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0);
172ad2d8307Sdrh   }else{
173ad2d8307Sdrh     *ppExpr = pE;
174ad2d8307Sdrh   }
175ad2d8307Sdrh }
176ad2d8307Sdrh 
177ad2d8307Sdrh /*
178ad2d8307Sdrh ** This routine processes the join information for a SELECT statement.
179ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause.
180ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms.
181ad2d8307Sdrh **
182ad2d8307Sdrh ** This routine returns the number of errors encountered.
183ad2d8307Sdrh */
184ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){
185ad2d8307Sdrh   SrcList *pSrc;
186ad2d8307Sdrh   int i, j;
187ad2d8307Sdrh   pSrc = p->pSrc;
188ad2d8307Sdrh   for(i=0; i<pSrc->nSrc-1; i++){
189ad2d8307Sdrh     struct SrcList_item *pTerm = &pSrc->a[i];
190ad2d8307Sdrh     struct SrcList_item *pOther = &pSrc->a[i+1];
191ad2d8307Sdrh 
192ad2d8307Sdrh     if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
193ad2d8307Sdrh 
194ad2d8307Sdrh     /* When the NATURAL keyword is present, add WHERE clause terms for
195ad2d8307Sdrh     ** every column that the two tables have in common.
196ad2d8307Sdrh     */
197ad2d8307Sdrh     if( pTerm->jointype & JT_NATURAL ){
198ad2d8307Sdrh       Table *pTab;
199ad2d8307Sdrh       if( pTerm->pOn || pTerm->pUsing ){
200ad2d8307Sdrh         sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have "
201ad2d8307Sdrh            "an ON or USING clause", 0);
202ad2d8307Sdrh         pParse->nErr++;
203ad2d8307Sdrh         return 1;
204ad2d8307Sdrh       }
205ad2d8307Sdrh       pTab = pTerm->pTab;
206ad2d8307Sdrh       for(j=0; j<pTab->nCol; j++){
207ad2d8307Sdrh         if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
208ad2d8307Sdrh           addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
209ad2d8307Sdrh         }
210ad2d8307Sdrh       }
211ad2d8307Sdrh     }
212ad2d8307Sdrh 
213ad2d8307Sdrh     /* Disallow both ON and USING clauses in the same join
214ad2d8307Sdrh     */
215ad2d8307Sdrh     if( pTerm->pOn && pTerm->pUsing ){
216ad2d8307Sdrh       sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING "
217ad2d8307Sdrh         "clauses in the same join", 0);
218ad2d8307Sdrh       pParse->nErr++;
219ad2d8307Sdrh       return 1;
220ad2d8307Sdrh     }
221ad2d8307Sdrh 
222ad2d8307Sdrh     /* Add the ON clause to the end of the WHERE clause, connected by
223ad2d8307Sdrh     ** and AND operator.
224ad2d8307Sdrh     */
225ad2d8307Sdrh     if( pTerm->pOn ){
226ad2d8307Sdrh       if( p->pWhere==0 ){
227ad2d8307Sdrh         p->pWhere = pTerm->pOn;
228ad2d8307Sdrh       }else{
229ad2d8307Sdrh         p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
230ad2d8307Sdrh       }
231ad2d8307Sdrh       pTerm->pOn = 0;
232ad2d8307Sdrh     }
233ad2d8307Sdrh 
234ad2d8307Sdrh     /* Create extra terms on the WHERE clause for each column named
235ad2d8307Sdrh     ** in the USING clause.  Example: If the two tables to be joined are
236ad2d8307Sdrh     ** A and B and the USING clause names X, Y, and Z, then add this
237ad2d8307Sdrh     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
238ad2d8307Sdrh     ** Report an error if any column mentioned in the USING clause is
239ad2d8307Sdrh     ** not contained in both tables to be joined.
240ad2d8307Sdrh     */
241ad2d8307Sdrh     if( pTerm->pUsing ){
242ad2d8307Sdrh       IdList *pList;
243ad2d8307Sdrh       int j;
244ad2d8307Sdrh       assert( i<pSrc->nSrc-1 );
245ad2d8307Sdrh       pList = pTerm->pUsing;
246ad2d8307Sdrh       for(j=0; j<pList->nId; j++){
247ad2d8307Sdrh         if( columnIndex(pTerm->pTab, pList->a[i].zName)<0 ||
248ad2d8307Sdrh             columnIndex(pOther->pTab, pList->a[i].zName)<0 ){
249ad2d8307Sdrh           sqliteSetString(&pParse->zErrMsg, "cannot join using column ",
250ad2d8307Sdrh             pList->a[i].zName, " - column not present in both tables", 0);
251ad2d8307Sdrh           pParse->nErr++;
252ad2d8307Sdrh           return 1;
253ad2d8307Sdrh         }
254ad2d8307Sdrh         addWhereTerm(pList->a[i].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
255ad2d8307Sdrh       }
256ad2d8307Sdrh     }
257ad2d8307Sdrh   }
258ad2d8307Sdrh   return 0;
259ad2d8307Sdrh }
260ad2d8307Sdrh 
261ad2d8307Sdrh /*
2629bb61fe7Sdrh ** Delete the given Select structure and all of its substructures.
2639bb61fe7Sdrh */
2649bb61fe7Sdrh void sqliteSelectDelete(Select *p){
26582c3d636Sdrh   if( p==0 ) return;
2669bb61fe7Sdrh   sqliteExprListDelete(p->pEList);
267ad3cab52Sdrh   sqliteSrcListDelete(p->pSrc);
2689bb61fe7Sdrh   sqliteExprDelete(p->pWhere);
2699bb61fe7Sdrh   sqliteExprListDelete(p->pGroupBy);
2709bb61fe7Sdrh   sqliteExprDelete(p->pHaving);
2719bb61fe7Sdrh   sqliteExprListDelete(p->pOrderBy);
27282c3d636Sdrh   sqliteSelectDelete(p->pPrior);
273a76b5dfcSdrh   sqliteFree(p->zSelect);
2749bb61fe7Sdrh   sqliteFree(p);
2759bb61fe7Sdrh }
2769bb61fe7Sdrh 
2779bb61fe7Sdrh /*
2782282792aSdrh ** Delete the aggregate information from the parse structure.
2792282792aSdrh */
2801d83f052Sdrh static void sqliteAggregateInfoReset(Parse *pParse){
2812282792aSdrh   sqliteFree(pParse->aAgg);
2822282792aSdrh   pParse->aAgg = 0;
2832282792aSdrh   pParse->nAgg = 0;
2842282792aSdrh   pParse->useAgg = 0;
2852282792aSdrh }
2862282792aSdrh 
2872282792aSdrh /*
2882282792aSdrh ** This routine generates the code for the inside of the inner loop
2892282792aSdrh ** of a SELECT.
29082c3d636Sdrh **
29182c3d636Sdrh ** The pEList is used to determine the values for each column in the
292967e8b73Sdrh ** result row.  Except  if pEList==NULL, then we just read nColumn
29382c3d636Sdrh ** elements from the srcTab table.
2942282792aSdrh */
2952282792aSdrh static int selectInnerLoop(
2962282792aSdrh   Parse *pParse,          /* The parser context */
2972282792aSdrh   ExprList *pEList,       /* List of values being extracted */
29882c3d636Sdrh   int srcTab,             /* Pull data from this table */
299967e8b73Sdrh   int nColumn,            /* Number of columns in the source table */
3002282792aSdrh   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
3012282792aSdrh   int distinct,           /* If >=0, make sure results are distinct */
3022282792aSdrh   int eDest,              /* How to dispose of the results */
3032282792aSdrh   int iParm,              /* An argument to the disposal method */
3042282792aSdrh   int iContinue,          /* Jump here to continue with next row */
3052282792aSdrh   int iBreak              /* Jump here to break out of the inner loop */
3062282792aSdrh ){
3072282792aSdrh   Vdbe *v = pParse->pVdbe;
3082282792aSdrh   int i;
309daffd0e5Sdrh   if( v==0 ) return 0;
3102282792aSdrh 
311967e8b73Sdrh   /* Pull the requested columns.
3122282792aSdrh   */
31382c3d636Sdrh   if( pEList ){
3142282792aSdrh     for(i=0; i<pEList->nExpr; i++){
3152282792aSdrh       sqliteExprCode(pParse, pEList->a[i].pExpr);
3162282792aSdrh     }
317967e8b73Sdrh     nColumn = pEList->nExpr;
31882c3d636Sdrh   }else{
319967e8b73Sdrh     for(i=0; i<nColumn; i++){
32099fcd718Sdrh       sqliteVdbeAddOp(v, OP_Column, srcTab, i);
32182c3d636Sdrh     }
32282c3d636Sdrh   }
3232282792aSdrh 
324daffd0e5Sdrh   /* If the DISTINCT keyword was present on the SELECT statement
325daffd0e5Sdrh   ** and this row has been seen before, then do not make this row
326daffd0e5Sdrh   ** part of the result.
3272282792aSdrh   */
328f5905aa7Sdrh   if( distinct>=0 && pEList && pEList->nExpr>0 ){
329f5905aa7Sdrh     sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7);
33099fcd718Sdrh     sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
331f5905aa7Sdrh     sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3);
33299fcd718Sdrh     sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
33399fcd718Sdrh     sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
33499fcd718Sdrh     sqliteVdbeAddOp(v, OP_String, 0, 0);
3356b12545fSdrh     sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
3362282792aSdrh   }
33782c3d636Sdrh 
3382282792aSdrh   /* If there is an ORDER BY clause, then store the results
3392282792aSdrh   ** in a sorter.
3402282792aSdrh   */
3412282792aSdrh   if( pOrderBy ){
3422282792aSdrh     char *zSortOrder;
34399fcd718Sdrh     sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
3442282792aSdrh     zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
3452282792aSdrh     if( zSortOrder==0 ) return 1;
3462282792aSdrh     for(i=0; i<pOrderBy->nExpr; i++){
347d8bc7086Sdrh       zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
3482282792aSdrh       sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
3492282792aSdrh     }
3502282792aSdrh     zSortOrder[pOrderBy->nExpr] = 0;
35199fcd718Sdrh     sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
35299fcd718Sdrh     sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
3536e142f54Sdrh     sqliteFree(zSortOrder);
35499fcd718Sdrh     sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
3552282792aSdrh   }else
3562282792aSdrh 
35782c3d636Sdrh   /* In this mode, write each query result to the key of the temporary
35882c3d636Sdrh   ** table iParm.
3592282792aSdrh   */
36082c3d636Sdrh   if( eDest==SRT_Union ){
361f5905aa7Sdrh     sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 1);
362f5905aa7Sdrh     sqliteVdbeAddOp(v, OP_String, 0, 0);
3636b12545fSdrh     sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
36482c3d636Sdrh   }else
36582c3d636Sdrh 
3665974a30fSdrh   /* Store the result as data using a unique key.
3675974a30fSdrh   */
3682d0794e3Sdrh   if( eDest==SRT_Table || eDest==SRT_TempTable ){
36999fcd718Sdrh     sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
37099fcd718Sdrh     sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
37199fcd718Sdrh     sqliteVdbeAddOp(v, OP_Pull, 1, 0);
3726b12545fSdrh     sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
3735974a30fSdrh   }else
3745974a30fSdrh 
37582c3d636Sdrh   /* Construct a record from the query result, but instead of
37682c3d636Sdrh   ** saving that record, use it as a key to delete elements from
37782c3d636Sdrh   ** the temporary table iParm.
37882c3d636Sdrh   */
37982c3d636Sdrh   if( eDest==SRT_Except ){
380f5905aa7Sdrh     int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 1);
38199fcd718Sdrh     sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
38299fcd718Sdrh     sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
3832282792aSdrh   }else
3842282792aSdrh 
3852282792aSdrh   /* If we are creating a set for an "expr IN (SELECT ...)" construct,
3862282792aSdrh   ** then there should be a single item on the stack.  Write this
3872282792aSdrh   ** item into the set table with bogus data.
3882282792aSdrh   */
3892282792aSdrh   if( eDest==SRT_Set ){
390967e8b73Sdrh     assert( nColumn==1 );
391f5905aa7Sdrh     sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3);
39299fcd718Sdrh     sqliteVdbeAddOp(v, OP_String, 0, 0);
3936b12545fSdrh     sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
3942282792aSdrh   }else
3952282792aSdrh 
39682c3d636Sdrh 
3972282792aSdrh   /* If this is a scalar select that is part of an expression, then
3982282792aSdrh   ** store the results in the appropriate memory cell and break out
3992282792aSdrh   ** of the scan loop.
4002282792aSdrh   */
4012282792aSdrh   if( eDest==SRT_Mem ){
402967e8b73Sdrh     assert( nColumn==1 );
4038721ce4aSdrh     sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
40499fcd718Sdrh     sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
4052282792aSdrh   }else
4062282792aSdrh 
4072282792aSdrh   /* If none of the above, send the data to the callback function.
4082282792aSdrh   */
4092282792aSdrh   {
4109bbca4c1Sdrh     sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak);
41182c3d636Sdrh   }
41282c3d636Sdrh   return 0;
41382c3d636Sdrh }
41482c3d636Sdrh 
41582c3d636Sdrh /*
416d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument,
417d8bc7086Sdrh ** then the results were placed in a sorter.  After the loop is terminated
418d8bc7086Sdrh ** we need to run the sorter and output the results.  The following
419d8bc7086Sdrh ** routine generates the code needed to do that.
420d8bc7086Sdrh */
421967e8b73Sdrh static void generateSortTail(Vdbe *v, int nColumn){
422d8bc7086Sdrh   int end = sqliteVdbeMakeLabel(v);
423d8bc7086Sdrh   int addr;
42499fcd718Sdrh   sqliteVdbeAddOp(v, OP_Sort, 0, 0);
42599fcd718Sdrh   addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
4269bbca4c1Sdrh   sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end);
42799fcd718Sdrh   sqliteVdbeAddOp(v, OP_Goto, 0, addr);
42899fcd718Sdrh   sqliteVdbeResolveLabel(v, end);
429a8b38d28Sdrh   sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
430d8bc7086Sdrh }
431d8bc7086Sdrh 
432d8bc7086Sdrh /*
43382c3d636Sdrh ** Generate code that will tell the VDBE how many columns there
43482c3d636Sdrh ** are in the result and the name for each column.  This information
43582c3d636Sdrh ** is used to provide "argc" and "azCol[]" values in the callback.
43682c3d636Sdrh */
437832508b7Sdrh static void generateColumnNames(
438832508b7Sdrh   Parse *pParse,      /* Parser context */
439832508b7Sdrh   int base,           /* VDBE cursor corresponding to first entry in pTabList */
440ad3cab52Sdrh   SrcList *pTabList,  /* List of tables */
441832508b7Sdrh   ExprList *pEList    /* Expressions defining the result set */
442832508b7Sdrh ){
443d8bc7086Sdrh   Vdbe *v = pParse->pVdbe;
44482c3d636Sdrh   int i;
445daffd0e5Sdrh   if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
446d8bc7086Sdrh   pParse->colNamesSet = 1;
44799fcd718Sdrh   sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
44882c3d636Sdrh   for(i=0; i<pEList->nExpr; i++){
44982c3d636Sdrh     Expr *p;
4501bee3d7bSdrh     int showFullNames;
45182c3d636Sdrh     if( pEList->a[i].zName ){
45282c3d636Sdrh       char *zName = pEList->a[i].zName;
45399fcd718Sdrh       sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
45499fcd718Sdrh       sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
45582c3d636Sdrh       continue;
45682c3d636Sdrh     }
45782c3d636Sdrh     p = pEList->a[i].pExpr;
458daffd0e5Sdrh     if( p==0 ) continue;
4591bee3d7bSdrh     showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
4601bee3d7bSdrh     if( p->span.z && p->span.z[0] && !showFullNames ){
46199fcd718Sdrh       int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
46299fcd718Sdrh       sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
463e1b6a5b8Sdrh       sqliteVdbeCompressSpace(v, addr);
4641bee3d7bSdrh     }else if( p->op==TK_COLUMN && pTabList ){
465832508b7Sdrh       Table *pTab = pTabList->a[p->iTable - base].pTab;
46697665873Sdrh       char *zCol;
4678aff1015Sdrh       int iCol = p->iColumn;
4688aff1015Sdrh       if( iCol<0 ) iCol = pTab->iPKey;
46997665873Sdrh       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
47097665873Sdrh       zCol = iCol<0 ? "_ROWID_" : pTab->aCol[iCol].zName;
471ad3cab52Sdrh       if( pTabList->nSrc>1 || showFullNames ){
47282c3d636Sdrh         char *zName = 0;
47382c3d636Sdrh         char *zTab;
47482c3d636Sdrh 
475832508b7Sdrh         zTab = pTabList->a[p->iTable - base].zAlias;
47601a34661Sdrh         if( showFullNames || zTab==0 ) zTab = pTab->zName;
47797665873Sdrh         sqliteSetString(&zName, zTab, ".", zCol, 0);
47899fcd718Sdrh         sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
47999fcd718Sdrh         sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
48082c3d636Sdrh         sqliteFree(zName);
48182c3d636Sdrh       }else{
48299fcd718Sdrh         sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
48322f70c32Sdrh         sqliteVdbeChangeP3(v, -1, zCol, 0);
48482c3d636Sdrh       }
4851bee3d7bSdrh     }else if( p->span.z && p->span.z[0] ){
4861bee3d7bSdrh       int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
4871bee3d7bSdrh       sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
4881bee3d7bSdrh       sqliteVdbeCompressSpace(v, addr);
4891bee3d7bSdrh     }else{
4901bee3d7bSdrh       char zName[30];
4911bee3d7bSdrh       assert( p->op!=TK_COLUMN || pTabList==0 );
4921bee3d7bSdrh       sprintf(zName, "column%d", i+1);
4931bee3d7bSdrh       sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
4941bee3d7bSdrh       sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
49582c3d636Sdrh     }
49682c3d636Sdrh   }
49782c3d636Sdrh }
49882c3d636Sdrh 
49982c3d636Sdrh /*
500d8bc7086Sdrh ** Name of the connection operator, used for error messages.
501d8bc7086Sdrh */
502d8bc7086Sdrh static const char *selectOpName(int id){
503d8bc7086Sdrh   char *z;
504d8bc7086Sdrh   switch( id ){
505d8bc7086Sdrh     case TK_ALL:       z = "UNION ALL";   break;
506d8bc7086Sdrh     case TK_INTERSECT: z = "INTERSECT";   break;
507d8bc7086Sdrh     case TK_EXCEPT:    z = "EXCEPT";      break;
508d8bc7086Sdrh     default:           z = "UNION";       break;
509d8bc7086Sdrh   }
510d8bc7086Sdrh   return z;
511d8bc7086Sdrh }
512d8bc7086Sdrh 
513d8bc7086Sdrh /*
51422f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes
51522f70c32Sdrh ** the result set of that SELECT.
51622f70c32Sdrh */
51722f70c32Sdrh Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
51822f70c32Sdrh   Table *pTab;
51922f70c32Sdrh   int i;
52022f70c32Sdrh   ExprList *pEList;
52122f70c32Sdrh   static int fillInColumnList(Parse*, Select*);
52222f70c32Sdrh 
52322f70c32Sdrh   if( fillInColumnList(pParse, pSelect) ){
52422f70c32Sdrh     return 0;
52522f70c32Sdrh   }
52622f70c32Sdrh   pTab = sqliteMalloc( sizeof(Table) );
52722f70c32Sdrh   if( pTab==0 ){
52822f70c32Sdrh     return 0;
52922f70c32Sdrh   }
53022f70c32Sdrh   pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
53122f70c32Sdrh   pEList = pSelect->pEList;
53222f70c32Sdrh   pTab->nCol = pEList->nExpr;
533417be79cSdrh   assert( pTab->nCol>0 );
53422f70c32Sdrh   pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
53522f70c32Sdrh   for(i=0; i<pTab->nCol; i++){
53622f70c32Sdrh     Expr *p;
53722f70c32Sdrh     if( pEList->a[i].zName ){
53822f70c32Sdrh       pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
53922f70c32Sdrh     }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
54022f70c32Sdrh       sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
541d820cb1bSdrh     }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
542d820cb1bSdrh            p->pRight->token.z[0] ){
543d820cb1bSdrh       sqliteSetNString(&pTab->aCol[i].zName,
544d820cb1bSdrh            p->pRight->token.z, p->pRight->token.n, 0);
54522f70c32Sdrh     }else{
54622f70c32Sdrh       char zBuf[30];
54722f70c32Sdrh       sprintf(zBuf, "column%d", i+1);
54822f70c32Sdrh       pTab->aCol[i].zName = sqliteStrDup(zBuf);
54922f70c32Sdrh     }
55022f70c32Sdrh   }
55122f70c32Sdrh   pTab->iPKey = -1;
55222f70c32Sdrh   return pTab;
55322f70c32Sdrh }
55422f70c32Sdrh 
55522f70c32Sdrh /*
556ad2d8307Sdrh ** For the given SELECT statement, do three things.
557d8bc7086Sdrh **
558ad3cab52Sdrh **    (1)  Fill in the pTabList->a[].pTab fields in the SrcList that
559967e8b73Sdrh **         defines the set of tables that should be scanned.
560d8bc7086Sdrh **
561ad2d8307Sdrh **    (2)  Add terms to the WHERE clause to accomodate the NATURAL keyword
562ad2d8307Sdrh **         on joins and the ON and USING clause of joins.
563ad2d8307Sdrh **
564ad2d8307Sdrh **    (3)  Scan the list of columns in the result set (pEList) looking
56554473229Sdrh **         for instances of the "*" operator or the TABLE.* operator.
56654473229Sdrh **         If found, expand each "*" to be every column in every table
56754473229Sdrh **         and TABLE.* to be every column in TABLE.
568d8bc7086Sdrh **
569d8bc7086Sdrh ** Return 0 on success.  If there are problems, leave an error message
570d8bc7086Sdrh ** in pParse and return non-zero.
571d8bc7086Sdrh */
572d8bc7086Sdrh static int fillInColumnList(Parse *pParse, Select *p){
57354473229Sdrh   int i, j, k, rc;
574ad3cab52Sdrh   SrcList *pTabList;
575daffd0e5Sdrh   ExprList *pEList;
576a76b5dfcSdrh   Table *pTab;
577daffd0e5Sdrh 
578daffd0e5Sdrh   if( p==0 || p->pSrc==0 ) return 1;
579daffd0e5Sdrh   pTabList = p->pSrc;
580daffd0e5Sdrh   pEList = p->pEList;
581d8bc7086Sdrh 
582d8bc7086Sdrh   /* Look up every table in the table list.
583d8bc7086Sdrh   */
584ad3cab52Sdrh   for(i=0; i<pTabList->nSrc; i++){
585d8bc7086Sdrh     if( pTabList->a[i].pTab ){
586d8bc7086Sdrh       /* This routine has run before!  No need to continue */
587d8bc7086Sdrh       return 0;
588d8bc7086Sdrh     }
589daffd0e5Sdrh     if( pTabList->a[i].zName==0 ){
59022f70c32Sdrh       /* A sub-query in the FROM clause of a SELECT */
59122f70c32Sdrh       assert( pTabList->a[i].pSelect!=0 );
592ad2d8307Sdrh       if( pTabList->a[i].zAlias==0 ){
593ad2d8307Sdrh         char zFakeName[60];
594ad2d8307Sdrh         sprintf(zFakeName, "sqlite_subquery_%p_",
595ad2d8307Sdrh            (void*)pTabList->a[i].pSelect);
596ad2d8307Sdrh         sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
597ad2d8307Sdrh       }
59822f70c32Sdrh       pTabList->a[i].pTab = pTab =
59922f70c32Sdrh         sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
60022f70c32Sdrh                                         pTabList->a[i].pSelect);
60122f70c32Sdrh       if( pTab==0 ){
602daffd0e5Sdrh         return 1;
603daffd0e5Sdrh       }
60422f70c32Sdrh       pTab->isTransient = 1;
60522f70c32Sdrh     }else{
606a76b5dfcSdrh       /* An ordinary table or view name in the FROM clause */
607a76b5dfcSdrh       pTabList->a[i].pTab = pTab =
608a76b5dfcSdrh         sqliteFindTable(pParse->db, pTabList->a[i].zName);
609a76b5dfcSdrh       if( pTab==0 ){
610d8bc7086Sdrh         sqliteSetString(&pParse->zErrMsg, "no such table: ",
611d8bc7086Sdrh            pTabList->a[i].zName, 0);
612d8bc7086Sdrh         pParse->nErr++;
613d8bc7086Sdrh         return 1;
614d8bc7086Sdrh       }
615a76b5dfcSdrh       if( pTab->pSelect ){
616417be79cSdrh         if( sqliteViewGetColumnNames(pParse, pTab) ){
617417be79cSdrh           return 1;
618417be79cSdrh         }
619ff78bd2fSdrh         pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
620a76b5dfcSdrh       }
621d8bc7086Sdrh     }
62222f70c32Sdrh   }
623d8bc7086Sdrh 
624ad2d8307Sdrh   /* Process NATURAL keywords, and ON and USING clauses of joins.
625ad2d8307Sdrh   */
626ad2d8307Sdrh   if( sqliteProcessJoin(pParse, p) ) return 1;
627ad2d8307Sdrh 
6287c917d19Sdrh   /* For every "*" that occurs in the column list, insert the names of
62954473229Sdrh   ** all columns in all tables.  And for every TABLE.* insert the names
63054473229Sdrh   ** of all columns in TABLE.  The parser inserted a special expression
6317c917d19Sdrh   ** with the TK_ALL operator for each "*" that it found in the column list.
6327c917d19Sdrh   ** The following code just has to locate the TK_ALL expressions and expand
6337c917d19Sdrh   ** each one to the list of all columns in all tables.
63454473229Sdrh   **
63554473229Sdrh   ** The first loop just checks to see if there are any "*" operators
63654473229Sdrh   ** that need expanding.
637d8bc7086Sdrh   */
6387c917d19Sdrh   for(k=0; k<pEList->nExpr; k++){
63954473229Sdrh     Expr *pE = pEList->a[k].pExpr;
64054473229Sdrh     if( pE->op==TK_ALL ) break;
64154473229Sdrh     if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
64254473229Sdrh          && pE->pLeft && pE->pLeft->op==TK_ID ) break;
6437c917d19Sdrh   }
64454473229Sdrh   rc = 0;
6457c917d19Sdrh   if( k<pEList->nExpr ){
64654473229Sdrh     /*
64754473229Sdrh     ** If we get here it means the result set contains one or more "*"
64854473229Sdrh     ** operators that need to be expanded.  Loop through each expression
64954473229Sdrh     ** in the result set and expand them one by one.
65054473229Sdrh     */
6517c917d19Sdrh     struct ExprList_item *a = pEList->a;
6527c917d19Sdrh     ExprList *pNew = 0;
6537c917d19Sdrh     for(k=0; k<pEList->nExpr; k++){
65454473229Sdrh       Expr *pE = a[k].pExpr;
65554473229Sdrh       if( pE->op!=TK_ALL &&
65654473229Sdrh            (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
65754473229Sdrh         /* This particular expression does not need to be expanded.
65854473229Sdrh         */
6597c917d19Sdrh         pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
6607c917d19Sdrh         pNew->a[pNew->nExpr-1].zName = a[k].zName;
6617c917d19Sdrh         a[k].pExpr = 0;
6627c917d19Sdrh         a[k].zName = 0;
6637c917d19Sdrh       }else{
66454473229Sdrh         /* This expression is a "*" or a "TABLE.*" and needs to be
66554473229Sdrh         ** expanded. */
66654473229Sdrh         int tableSeen = 0;      /* Set to 1 when TABLE matches */
66754473229Sdrh         Token *pName;           /* text of name of TABLE */
66854473229Sdrh         if( pE->op==TK_DOT && pE->pLeft ){
66954473229Sdrh           pName = &pE->pLeft->token;
67054473229Sdrh         }else{
67154473229Sdrh           pName = 0;
67254473229Sdrh         }
673ad3cab52Sdrh         for(i=0; i<pTabList->nSrc; i++){
674d8bc7086Sdrh           Table *pTab = pTabList->a[i].pTab;
67554473229Sdrh           char *zTabName = pTabList->a[i].zAlias;
67654473229Sdrh           if( zTabName==0 || zTabName[0]==0 ){
67754473229Sdrh             zTabName = pTab->zName;
67854473229Sdrh           }
67954473229Sdrh           if( pName && (zTabName==0 || zTabName[0]==0 ||
680*c754fa54Sdrh                  sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
681*c754fa54Sdrh                  zTabName[pName->n]!=0) ){
68254473229Sdrh             continue;
68354473229Sdrh           }
68454473229Sdrh           tableSeen = 1;
685d8bc7086Sdrh           for(j=0; j<pTab->nCol; j++){
68622f70c32Sdrh             Expr *pExpr, *pLeft, *pRight;
687ad2d8307Sdrh             char *zName = pTab->aCol[j].zName;
688ad2d8307Sdrh 
689ad2d8307Sdrh             if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
690ad2d8307Sdrh                 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
691ad2d8307Sdrh               /* In a NATURAL join, omit the join columns from the
692ad2d8307Sdrh               ** table on the right */
693ad2d8307Sdrh               continue;
694ad2d8307Sdrh             }
695ad2d8307Sdrh             if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
696ad2d8307Sdrh               /* In a join with a USING clause, omit columns in the
697ad2d8307Sdrh               ** using clause from the table on the right. */
698ad2d8307Sdrh               continue;
699ad2d8307Sdrh             }
70022f70c32Sdrh             pRight = sqliteExpr(TK_ID, 0, 0, 0);
70122f70c32Sdrh             if( pRight==0 ) break;
702ad2d8307Sdrh             pRight->token.z = zName;
703ad2d8307Sdrh             pRight->token.n = strlen(zName);
70454473229Sdrh             if( zTabName ){
70522f70c32Sdrh               pLeft = sqliteExpr(TK_ID, 0, 0, 0);
70622f70c32Sdrh               if( pLeft==0 ) break;
70754473229Sdrh               pLeft->token.z = zTabName;
70854473229Sdrh               pLeft->token.n = strlen(zTabName);
70922f70c32Sdrh               pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
71022f70c32Sdrh               if( pExpr==0 ) break;
71122f70c32Sdrh             }else{
71222f70c32Sdrh               pExpr = pRight;
71322f70c32Sdrh               pExpr->span = pExpr->token;
71422f70c32Sdrh             }
7157c917d19Sdrh             pNew = sqliteExprListAppend(pNew, pExpr, 0);
716d8bc7086Sdrh           }
717d8bc7086Sdrh         }
71854473229Sdrh         if( !tableSeen ){
71954473229Sdrh           assert( pName!=0 );
72054473229Sdrh           sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
72154473229Sdrh             pName->z, pName->n, 0);
72254473229Sdrh           rc = 1;
72354473229Sdrh         }
7247c917d19Sdrh       }
7257c917d19Sdrh     }
7267c917d19Sdrh     sqliteExprListDelete(pEList);
7277c917d19Sdrh     p->pEList = pNew;
728d8bc7086Sdrh   }
72954473229Sdrh   return rc;
730d8bc7086Sdrh }
731d8bc7086Sdrh 
732d8bc7086Sdrh /*
733ff78bd2fSdrh ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
734ff78bd2fSdrh ** in a select structure.  It just sets the pointers to NULL.  This
735ff78bd2fSdrh ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
736ff78bd2fSdrh ** pointer is not NULL, this routine is called recursively on that pointer.
737ff78bd2fSdrh **
738ff78bd2fSdrh ** This routine is called on the Select structure that defines a
739ff78bd2fSdrh ** VIEW in order to undo any bindings to tables.  This is necessary
740ff78bd2fSdrh ** because those tables might be DROPed by a subsequent SQL command.
741ff78bd2fSdrh */
742ff78bd2fSdrh void sqliteSelectUnbind(Select *p){
743ff78bd2fSdrh   int i;
744ad3cab52Sdrh   SrcList *pSrc = p->pSrc;
745ff78bd2fSdrh   Table *pTab;
746ff78bd2fSdrh   if( p==0 ) return;
747ad3cab52Sdrh   for(i=0; i<pSrc->nSrc; i++){
748ff78bd2fSdrh     if( (pTab = pSrc->a[i].pTab)!=0 ){
749ff78bd2fSdrh       if( pTab->isTransient ){
750ff78bd2fSdrh         sqliteDeleteTable(0, pTab);
751ff78bd2fSdrh         sqliteSelectDelete(pSrc->a[i].pSelect);
752ff78bd2fSdrh         pSrc->a[i].pSelect = 0;
753ff78bd2fSdrh       }
754ff78bd2fSdrh       pSrc->a[i].pTab = 0;
755ff78bd2fSdrh       if( pSrc->a[i].pSelect ){
756ff78bd2fSdrh         sqliteSelectUnbind(pSrc->a[i].pSelect);
757ff78bd2fSdrh       }
758ff78bd2fSdrh     }
759ff78bd2fSdrh   }
760ff78bd2fSdrh }
761ff78bd2fSdrh 
762ff78bd2fSdrh /*
763d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with
764d8bc7086Sdrh ** columns in a result.  For each ORDER BY expression, the opcode of
765967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of
766d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable
767d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter.
768d8bc7086Sdrh **
769d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first.  A match
770d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT.
771d8bc7086Sdrh **
772d8bc7086Sdrh ** Any entry that does not match is flagged as an error.  The number
773d8bc7086Sdrh ** of errors is returned.
774d8bc7086Sdrh */
775d8bc7086Sdrh static int matchOrderbyToColumn(
776d8bc7086Sdrh   Parse *pParse,          /* A place to leave error messages */
777d8bc7086Sdrh   Select *pSelect,        /* Match to result columns of this SELECT */
778d8bc7086Sdrh   ExprList *pOrderBy,     /* The ORDER BY values to match against columns */
779d8bc7086Sdrh   int iTable,             /* Insert this this value in iTable */
780d8bc7086Sdrh   int mustComplete        /* If TRUE all ORDER BYs must match */
781d8bc7086Sdrh ){
782d8bc7086Sdrh   int nErr = 0;
783d8bc7086Sdrh   int i, j;
784d8bc7086Sdrh   ExprList *pEList;
785d8bc7086Sdrh 
786daffd0e5Sdrh   if( pSelect==0 || pOrderBy==0 ) return 1;
787d8bc7086Sdrh   if( mustComplete ){
788d8bc7086Sdrh     for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
789d8bc7086Sdrh   }
790d8bc7086Sdrh   if( fillInColumnList(pParse, pSelect) ){
791d8bc7086Sdrh     return 1;
792d8bc7086Sdrh   }
793d8bc7086Sdrh   if( pSelect->pPrior ){
79492cd52f5Sdrh     if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
79592cd52f5Sdrh       return 1;
79692cd52f5Sdrh     }
797d8bc7086Sdrh   }
798d8bc7086Sdrh   pEList = pSelect->pEList;
799d8bc7086Sdrh   for(i=0; i<pOrderBy->nExpr; i++){
800d8bc7086Sdrh     Expr *pE = pOrderBy->a[i].pExpr;
80192cd52f5Sdrh     int match = 0;
802d8bc7086Sdrh     if( pOrderBy->a[i].done ) continue;
803d8bc7086Sdrh     for(j=0; j<pEList->nExpr; j++){
8044cfa7934Sdrh       if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
805a76b5dfcSdrh         char *zName, *zLabel;
806a76b5dfcSdrh         zName = pEList->a[j].zName;
807a76b5dfcSdrh         assert( pE->token.z );
808a76b5dfcSdrh         zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
809d8bc7086Sdrh         sqliteDequote(zLabel);
810d8bc7086Sdrh         if( sqliteStrICmp(zName, zLabel)==0 ){
811d8bc7086Sdrh           match = 1;
812d8bc7086Sdrh         }
8136e142f54Sdrh         sqliteFree(zLabel);
814d8bc7086Sdrh       }
8154cfa7934Sdrh       if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
816d8bc7086Sdrh         match = 1;
817d8bc7086Sdrh       }
818d8bc7086Sdrh       if( match ){
819967e8b73Sdrh         pE->op = TK_COLUMN;
820967e8b73Sdrh         pE->iColumn = j;
821d8bc7086Sdrh         pE->iTable = iTable;
822d8bc7086Sdrh         pOrderBy->a[i].done = 1;
823d8bc7086Sdrh         break;
824d8bc7086Sdrh       }
825d8bc7086Sdrh     }
82692cd52f5Sdrh     if( !match && mustComplete ){
827d8bc7086Sdrh       char zBuf[30];
828d8bc7086Sdrh       sprintf(zBuf,"%d",i+1);
829d8bc7086Sdrh       sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
830d8bc7086Sdrh         " does not match any result column", 0);
831d8bc7086Sdrh       pParse->nErr++;
832d8bc7086Sdrh       nErr++;
833d8bc7086Sdrh       break;
834d8bc7086Sdrh     }
835d8bc7086Sdrh   }
836d8bc7086Sdrh   return nErr;
837d8bc7086Sdrh }
838d8bc7086Sdrh 
839d8bc7086Sdrh /*
840d8bc7086Sdrh ** Get a VDBE for the given parser context.  Create a new one if necessary.
841d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse.
842d8bc7086Sdrh */
843d8bc7086Sdrh Vdbe *sqliteGetVdbe(Parse *pParse){
844d8bc7086Sdrh   Vdbe *v = pParse->pVdbe;
845d8bc7086Sdrh   if( v==0 ){
8464c504391Sdrh     v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
847d8bc7086Sdrh   }
848d8bc7086Sdrh   return v;
849d8bc7086Sdrh }
850d8bc7086Sdrh 
851d8bc7086Sdrh 
852d8bc7086Sdrh /*
85382c3d636Sdrh ** This routine is called to process a query that is really the union
85482c3d636Sdrh ** or intersection of two or more separate queries.
85582c3d636Sdrh */
85682c3d636Sdrh static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
85710e5e3cfSdrh   int rc;             /* Success code from a subroutine */
85810e5e3cfSdrh   Select *pPrior;     /* Another SELECT immediately to our left */
85910e5e3cfSdrh   Vdbe *v;            /* Generate code to this VDBE */
86010e5e3cfSdrh   int base;           /* Baseline value for pParse->nTab */
86182c3d636Sdrh 
862d8bc7086Sdrh   /* Make sure there is no ORDER BY clause on prior SELECTs.  Only the
863d8bc7086Sdrh   ** last SELECT in the series may have an ORDER BY.
86482c3d636Sdrh   */
865daffd0e5Sdrh   if( p==0 || p->pPrior==0 ) return 1;
866d8bc7086Sdrh   pPrior = p->pPrior;
867d8bc7086Sdrh   if( pPrior->pOrderBy ){
868d8bc7086Sdrh     sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
869d8bc7086Sdrh       selectOpName(p->op), " not before", 0);
87082c3d636Sdrh     pParse->nErr++;
87182c3d636Sdrh     return 1;
87282c3d636Sdrh   }
87382c3d636Sdrh 
874d8bc7086Sdrh   /* Make sure we have a valid query engine.  If not, create a new one.
875d8bc7086Sdrh   */
876d8bc7086Sdrh   v = sqliteGetVdbe(pParse);
877d8bc7086Sdrh   if( v==0 ) return 1;
878d8bc7086Sdrh 
8791cc3d75fSdrh   /* Create the destination temporary table if necessary
8801cc3d75fSdrh   */
8811cc3d75fSdrh   if( eDest==SRT_TempTable ){
8821cc3d75fSdrh     sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
8831cc3d75fSdrh     eDest = SRT_Table;
8841cc3d75fSdrh   }
8851cc3d75fSdrh 
886d8bc7086Sdrh   /* Process the UNION or INTERSECTION
887d8bc7086Sdrh   */
88810e5e3cfSdrh   base = pParse->nTab;
88982c3d636Sdrh   switch( p->op ){
890d8bc7086Sdrh     case TK_ALL:
89182c3d636Sdrh     case TK_EXCEPT:
89282c3d636Sdrh     case TK_UNION: {
893d8bc7086Sdrh       int unionTab;    /* Cursor number of the temporary table holding result */
894d8bc7086Sdrh       int op;          /* One of the SRT_ operations to apply to self */
895d8bc7086Sdrh       int priorOp;     /* The SRT_ operation to apply to prior selects */
89682c3d636Sdrh 
897d8bc7086Sdrh       priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
898d8bc7086Sdrh       if( eDest==priorOp ){
899d8bc7086Sdrh         /* We can reuse a temporary table generated by a SELECT to our
900d8bc7086Sdrh         ** right.  This also means we are not the right-most select and so
901d8bc7086Sdrh         ** we cannot have an ORDER BY clause
902d8bc7086Sdrh         */
90382c3d636Sdrh         unionTab = iParm;
904d8bc7086Sdrh         assert( p->pOrderBy==0 );
90582c3d636Sdrh       }else{
906d8bc7086Sdrh         /* We will need to create our own temporary table to hold the
907d8bc7086Sdrh         ** intermediate results.
908d8bc7086Sdrh         */
90982c3d636Sdrh         unionTab = pParse->nTab++;
910d8bc7086Sdrh         if( p->pOrderBy
911d8bc7086Sdrh         && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
912d8bc7086Sdrh           return 1;
913d8bc7086Sdrh         }
914d8bc7086Sdrh         if( p->op!=TK_ALL ){
915c6b52df3Sdrh           sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
91699fcd718Sdrh           sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
917345fda3eSdrh         }else{
91899fcd718Sdrh           sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
91982c3d636Sdrh         }
920d8bc7086Sdrh       }
921d8bc7086Sdrh 
922d8bc7086Sdrh       /* Code the SELECT statements to our left
923d8bc7086Sdrh       */
924832508b7Sdrh       rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
92582c3d636Sdrh       if( rc ) return rc;
926d8bc7086Sdrh 
927d8bc7086Sdrh       /* Code the current SELECT statement
928d8bc7086Sdrh       */
929d8bc7086Sdrh       switch( p->op ){
930d8bc7086Sdrh          case TK_EXCEPT:  op = SRT_Except;   break;
931d8bc7086Sdrh          case TK_UNION:   op = SRT_Union;    break;
932d8bc7086Sdrh          case TK_ALL:     op = SRT_Table;    break;
933d8bc7086Sdrh       }
93482c3d636Sdrh       p->pPrior = 0;
935832508b7Sdrh       rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
93682c3d636Sdrh       p->pPrior = pPrior;
93782c3d636Sdrh       if( rc ) return rc;
938d8bc7086Sdrh 
939d8bc7086Sdrh       /* Convert the data in the temporary table into whatever form
940d8bc7086Sdrh       ** it is that we currently need.
941d8bc7086Sdrh       */
942d8bc7086Sdrh       if( eDest!=priorOp ){
9436b56344dSdrh         int iCont, iBreak, iStart;
94482c3d636Sdrh         assert( p->pEList );
94541202ccaSdrh         if( eDest==SRT_Callback ){
946832508b7Sdrh           generateColumnNames(pParse, p->base, 0, p->pEList);
94741202ccaSdrh         }
94882c3d636Sdrh         iBreak = sqliteVdbeMakeLabel(v);
9496b56344dSdrh         iCont = sqliteVdbeMakeLabel(v);
9506b56344dSdrh         sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
9516b56344dSdrh         iStart = sqliteVdbeCurrentAddr(v);
95282c3d636Sdrh         rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
953d8bc7086Sdrh                              p->pOrderBy, -1, eDest, iParm,
95482c3d636Sdrh                              iCont, iBreak);
95582c3d636Sdrh         if( rc ) return 1;
9566b56344dSdrh         sqliteVdbeResolveLabel(v, iCont);
9576b56344dSdrh         sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
95899fcd718Sdrh         sqliteVdbeResolveLabel(v, iBreak);
95999fcd718Sdrh         sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
960d8bc7086Sdrh         if( p->pOrderBy ){
961d8bc7086Sdrh           generateSortTail(v, p->pEList->nExpr);
962d8bc7086Sdrh         }
96382c3d636Sdrh       }
96482c3d636Sdrh       break;
96582c3d636Sdrh     }
96682c3d636Sdrh     case TK_INTERSECT: {
96782c3d636Sdrh       int tab1, tab2;
9686b56344dSdrh       int iCont, iBreak, iStart;
96982c3d636Sdrh 
970d8bc7086Sdrh       /* INTERSECT is different from the others since it requires
9716206d50aSdrh       ** two temporary tables.  Hence it has its own case.  Begin
972d8bc7086Sdrh       ** by allocating the tables we will need.
973d8bc7086Sdrh       */
97482c3d636Sdrh       tab1 = pParse->nTab++;
97582c3d636Sdrh       tab2 = pParse->nTab++;
976d8bc7086Sdrh       if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
977d8bc7086Sdrh         return 1;
978d8bc7086Sdrh       }
979c6b52df3Sdrh       sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
98099fcd718Sdrh       sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
981d8bc7086Sdrh 
982d8bc7086Sdrh       /* Code the SELECTs to our left into temporary table "tab1".
983d8bc7086Sdrh       */
984832508b7Sdrh       rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
98582c3d636Sdrh       if( rc ) return rc;
986d8bc7086Sdrh 
987d8bc7086Sdrh       /* Code the current SELECT into temporary table "tab2"
988d8bc7086Sdrh       */
989c6b52df3Sdrh       sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
99099fcd718Sdrh       sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
99182c3d636Sdrh       p->pPrior = 0;
992832508b7Sdrh       rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
99382c3d636Sdrh       p->pPrior = pPrior;
99482c3d636Sdrh       if( rc ) return rc;
995d8bc7086Sdrh 
996d8bc7086Sdrh       /* Generate code to take the intersection of the two temporary
997d8bc7086Sdrh       ** tables.
998d8bc7086Sdrh       */
99982c3d636Sdrh       assert( p->pEList );
100041202ccaSdrh       if( eDest==SRT_Callback ){
1001832508b7Sdrh         generateColumnNames(pParse, p->base, 0, p->pEList);
100241202ccaSdrh       }
100382c3d636Sdrh       iBreak = sqliteVdbeMakeLabel(v);
10046b56344dSdrh       iCont = sqliteVdbeMakeLabel(v);
10056b56344dSdrh       sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
10066b56344dSdrh       iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
100799fcd718Sdrh       sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
100882c3d636Sdrh       rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
1009d8bc7086Sdrh                              p->pOrderBy, -1, eDest, iParm,
101082c3d636Sdrh                              iCont, iBreak);
101182c3d636Sdrh       if( rc ) return 1;
10126b56344dSdrh       sqliteVdbeResolveLabel(v, iCont);
10136b56344dSdrh       sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
101499fcd718Sdrh       sqliteVdbeResolveLabel(v, iBreak);
101599fcd718Sdrh       sqliteVdbeAddOp(v, OP_Close, tab2, 0);
101699fcd718Sdrh       sqliteVdbeAddOp(v, OP_Close, tab1, 0);
1017d8bc7086Sdrh       if( p->pOrderBy ){
1018d8bc7086Sdrh         generateSortTail(v, p->pEList->nExpr);
1019d8bc7086Sdrh       }
102082c3d636Sdrh       break;
102182c3d636Sdrh     }
102282c3d636Sdrh   }
102382c3d636Sdrh   assert( p->pEList && pPrior->pEList );
102482c3d636Sdrh   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
1025d8bc7086Sdrh     sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1026d8bc7086Sdrh       selectOpName(p->op), " do not have the same number of result columns", 0);
102782c3d636Sdrh     pParse->nErr++;
102882c3d636Sdrh     return 1;
10292282792aSdrh   }
103010e5e3cfSdrh   pParse->nTab = base;
10312282792aSdrh   return 0;
10322282792aSdrh }
10332282792aSdrh 
10342282792aSdrh /*
1035832508b7Sdrh ** Recursively scan through an expression tree.  For every reference
1036832508b7Sdrh ** to a column in table number iFrom, change that reference to the
1037832508b7Sdrh ** same column in table number iTo.
1038832508b7Sdrh */
1039832508b7Sdrh static void changeTables(Expr *pExpr, int iFrom, int iTo){
1040832508b7Sdrh   if( pExpr==0 ) return;
1041832508b7Sdrh   if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1042832508b7Sdrh     pExpr->iTable = iTo;
1043832508b7Sdrh   }else{
10441b2e0329Sdrh     static void changeTablesInList(ExprList*, int, int);
1045832508b7Sdrh     changeTables(pExpr->pLeft, iFrom, iTo);
1046832508b7Sdrh     changeTables(pExpr->pRight, iFrom, iTo);
10471b2e0329Sdrh     changeTablesInList(pExpr->pList, iFrom, iTo);
1048832508b7Sdrh   }
1049832508b7Sdrh }
10501b2e0329Sdrh static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
10511b2e0329Sdrh   if( pList ){
10521b2e0329Sdrh     int i;
10531b2e0329Sdrh     for(i=0; i<pList->nExpr; i++){
10541b2e0329Sdrh       changeTables(pList->a[i].pExpr, iFrom, iTo);
10551b2e0329Sdrh     }
1056832508b7Sdrh   }
1057832508b7Sdrh }
1058832508b7Sdrh 
1059832508b7Sdrh /*
1060832508b7Sdrh ** Scan through the expression pExpr.  Replace every reference to
1061832508b7Sdrh ** a column in table number iTable with a copy of the corresponding
106284e59207Sdrh ** entry in pEList.  (But leave references to the ROWID column
106384e59207Sdrh ** unchanged.)  When making a copy of an expression in pEList, change
106484e59207Sdrh ** references to columns in table iSub into references to table iTable.
1065832508b7Sdrh **
1066832508b7Sdrh ** This routine is part of the flattening procedure.  A subquery
1067832508b7Sdrh ** whose result set is defined by pEList appears as entry in the
1068832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that
1069832508b7Sdrh ** FORM clause entry is iTable.  This routine make the necessary
1070832508b7Sdrh ** changes to pExpr so that it refers directly to the source table
1071832508b7Sdrh ** of the subquery rather the result set of the subquery.
1072832508b7Sdrh */
1073832508b7Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1074832508b7Sdrh   if( pExpr==0 ) return;
107584e59207Sdrh   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
1076832508b7Sdrh     Expr *pNew;
107784e59207Sdrh     assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
1078832508b7Sdrh     assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1079832508b7Sdrh     pNew = pEList->a[pExpr->iColumn].pExpr;
1080832508b7Sdrh     assert( pNew!=0 );
1081832508b7Sdrh     pExpr->op = pNew->op;
1082832508b7Sdrh     pExpr->pLeft = sqliteExprDup(pNew->pLeft);
1083832508b7Sdrh     pExpr->pRight = sqliteExprDup(pNew->pRight);
1084832508b7Sdrh     pExpr->pList = sqliteExprListDup(pNew->pList);
1085832508b7Sdrh     pExpr->iTable = pNew->iTable;
1086832508b7Sdrh     pExpr->iColumn = pNew->iColumn;
1087832508b7Sdrh     pExpr->iAgg = pNew->iAgg;
10881b2e0329Sdrh     pExpr->token = pNew->token;
1089832508b7Sdrh     if( iSub!=iTable ){
1090832508b7Sdrh       changeTables(pExpr, iSub, iTable);
1091832508b7Sdrh     }
1092832508b7Sdrh   }else{
1093832508b7Sdrh     static void substExprList(ExprList*,int,ExprList*,int);
1094832508b7Sdrh     substExpr(pExpr->pLeft, iTable, pEList, iSub);
1095832508b7Sdrh     substExpr(pExpr->pRight, iTable, pEList, iSub);
1096832508b7Sdrh     substExprList(pExpr->pList, iTable, pEList, iSub);
1097832508b7Sdrh   }
1098832508b7Sdrh }
1099832508b7Sdrh static void
1100832508b7Sdrh substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1101832508b7Sdrh   int i;
1102832508b7Sdrh   if( pList==0 ) return;
1103832508b7Sdrh   for(i=0; i<pList->nExpr; i++){
1104832508b7Sdrh     substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1105832508b7Sdrh   }
1106832508b7Sdrh }
1107832508b7Sdrh 
1108832508b7Sdrh /*
11091350b030Sdrh ** This routine attempts to flatten subqueries in order to speed
11101350b030Sdrh ** execution.  It returns 1 if it makes changes and 0 if no flattening
11111350b030Sdrh ** occurs.
11121350b030Sdrh **
11131350b030Sdrh ** To understand the concept of flattening, consider the following
11141350b030Sdrh ** query:
11151350b030Sdrh **
11161350b030Sdrh **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
11171350b030Sdrh **
11181350b030Sdrh ** The default way of implementing this query is to execute the
11191350b030Sdrh ** subquery first and store the results in a temporary table, then
11201350b030Sdrh ** run the outer query on that temporary table.  This requires two
11211350b030Sdrh ** passes over the data.  Furthermore, because the temporary table
11221350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be
1123832508b7Sdrh ** optimized.
11241350b030Sdrh **
1125832508b7Sdrh ** This routine attempts to rewrite queries such as the above into
11261350b030Sdrh ** a single flat select, like this:
11271350b030Sdrh **
11281350b030Sdrh **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
11291350b030Sdrh **
11301350b030Sdrh ** The code generated for this simpification gives the same result
1131832508b7Sdrh ** but only has to scan the data once.  And because indices might
1132832508b7Sdrh ** exist on the table t1, a complete scan of the data might be
1133832508b7Sdrh ** avoided.
11341350b030Sdrh **
1135832508b7Sdrh ** Flattening is only attempted if all of the following are true:
11361350b030Sdrh **
1137832508b7Sdrh **   (1)  The subquery and the outer query do not both use aggregates.
11381350b030Sdrh **
1139832508b7Sdrh **   (2)  The subquery is not an aggregate or the outer query is not a join.
1140832508b7Sdrh **
1141832508b7Sdrh **   (3)  The subquery is not a join.
1142832508b7Sdrh **
1143832508b7Sdrh **   (4)  The subquery is not DISTINCT or the outer query is not a join.
1144832508b7Sdrh **
1145832508b7Sdrh **   (5)  The subquery is not DISTINCT or the outer query does not use
1146832508b7Sdrh **        aggregates.
1147832508b7Sdrh **
1148832508b7Sdrh **   (6)  The subquery does not use aggregates or the outer query is not
1149832508b7Sdrh **        DISTINCT.
1150832508b7Sdrh **
115108192d5fSdrh **   (7)  The subquery has a FROM clause.
115208192d5fSdrh **
1153832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query.
1154832508b7Sdrh ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
1155832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1156832508b7Sdrh **
1157832508b7Sdrh ** If flattening is not attempted, this routine is a no-op and return 0.
1158832508b7Sdrh ** If flattening is attempted this routine returns 1.
1159832508b7Sdrh **
1160832508b7Sdrh ** All of the expression analysis must occur on both the outer query and
1161832508b7Sdrh ** the subquery before this routine runs.
11621350b030Sdrh */
1163832508b7Sdrh int flattenSubquery(Select *p, int iFrom, int isAgg, int subqueryIsAgg){
11640bb28106Sdrh   Select *pSub;       /* The inner query or "subquery" */
1165ad3cab52Sdrh   SrcList *pSrc;      /* The FROM clause of the outer query */
1166ad3cab52Sdrh   SrcList *pSubSrc;   /* The FROM clause of the subquery */
11670bb28106Sdrh   ExprList *pList;    /* The result set of the outer query */
1168832508b7Sdrh   int i;
1169832508b7Sdrh   int iParent, iSub;
1170832508b7Sdrh   Expr *pWhere;
11711350b030Sdrh 
1172832508b7Sdrh   /* Check to see if flattening is permitted.  Return 0 if not.
1173832508b7Sdrh   */
1174832508b7Sdrh   if( p==0 ) return 0;
1175832508b7Sdrh   pSrc = p->pSrc;
1176ad3cab52Sdrh   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
1177832508b7Sdrh   pSub = pSrc->a[iFrom].pSelect;
1178832508b7Sdrh   assert( pSub!=0 );
1179832508b7Sdrh   if( isAgg && subqueryIsAgg ) return 0;
1180ad3cab52Sdrh   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
1181832508b7Sdrh   pSubSrc = pSub->pSrc;
1182832508b7Sdrh   assert( pSubSrc );
1183ad3cab52Sdrh   if( pSubSrc->nSrc!=1 ) return 0;
1184ad3cab52Sdrh   if( pSub->isDistinct && pSrc->nSrc>1 ) return 0;
1185832508b7Sdrh   if( pSub->isDistinct && isAgg ) return 0;
1186832508b7Sdrh   if( p->isDistinct && subqueryIsAgg ) return 0;
1187832508b7Sdrh 
11880bb28106Sdrh   /* If we reach this point, it means flattening is permitted for the
1189832508b7Sdrh   ** i-th entry of the FROM clause in the outer query.
1190832508b7Sdrh   */
1191832508b7Sdrh   iParent = p->base + iFrom;
1192832508b7Sdrh   iSub = pSub->base;
1193832508b7Sdrh   substExprList(p->pEList, iParent, pSub->pEList, iSub);
1194832508b7Sdrh   pList = p->pEList;
1195832508b7Sdrh   for(i=0; i<pList->nExpr; i++){
1196832508b7Sdrh     if( pList->a[i].zName==0 ){
1197832508b7Sdrh       Expr *pExpr = pList->a[i].pExpr;
1198832508b7Sdrh       pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
1199832508b7Sdrh     }
1200832508b7Sdrh   }
12011b2e0329Sdrh   if( isAgg ){
1202832508b7Sdrh     substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1203832508b7Sdrh     substExpr(p->pHaving, iParent, pSub->pEList, iSub);
12041b2e0329Sdrh   }
1205832508b7Sdrh   substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1206832508b7Sdrh   if( pSub->pWhere ){
1207832508b7Sdrh     pWhere = sqliteExprDup(pSub->pWhere);
1208832508b7Sdrh     if( iParent!=iSub ){
1209832508b7Sdrh       changeTables(pWhere, iSub, iParent);
1210832508b7Sdrh     }
1211832508b7Sdrh   }else{
1212832508b7Sdrh     pWhere = 0;
1213832508b7Sdrh   }
1214832508b7Sdrh   if( subqueryIsAgg ){
1215832508b7Sdrh     assert( p->pHaving==0 );
12161b2e0329Sdrh     p->pHaving = p->pWhere;
12171b2e0329Sdrh     p->pWhere = pWhere;
1218832508b7Sdrh     substExpr(p->pHaving, iParent, pSub->pEList, iSub);
12191b2e0329Sdrh     if( pSub->pHaving ){
12201b2e0329Sdrh       Expr *pHaving = sqliteExprDup(pSub->pHaving);
12211b2e0329Sdrh       if( iParent!=iSub ){
12221b2e0329Sdrh         changeTables(pHaving, iSub, iParent);
12231b2e0329Sdrh       }
12241b2e0329Sdrh       if( p->pHaving ){
12251b2e0329Sdrh         p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
12261b2e0329Sdrh       }else{
12271b2e0329Sdrh         p->pHaving = pHaving;
12281b2e0329Sdrh       }
12291b2e0329Sdrh     }
12301b2e0329Sdrh     assert( p->pGroupBy==0 );
12311b2e0329Sdrh     p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
12321b2e0329Sdrh     if( iParent!=iSub ){
12331b2e0329Sdrh       changeTablesInList(p->pGroupBy, iSub, iParent);
12341b2e0329Sdrh     }
1235832508b7Sdrh   }else if( p->pWhere==0 ){
1236832508b7Sdrh     p->pWhere = pWhere;
1237832508b7Sdrh   }else{
1238832508b7Sdrh     substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1239832508b7Sdrh     if( pWhere ){
1240832508b7Sdrh       p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1241832508b7Sdrh     }
1242832508b7Sdrh   }
1243832508b7Sdrh   p->isDistinct = p->isDistinct || pSub->isDistinct;
1244832508b7Sdrh   if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1245832508b7Sdrh     sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1246832508b7Sdrh   }
1247832508b7Sdrh   pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1248832508b7Sdrh   pSubSrc->a[0].pTab = 0;
1249832508b7Sdrh   pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1250832508b7Sdrh   pSubSrc->a[0].pSelect = 0;
1251832508b7Sdrh   sqliteSelectDelete(pSub);
1252832508b7Sdrh   return 1;
12531350b030Sdrh }
12541350b030Sdrh 
12551350b030Sdrh /*
12569562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it
12579562b551Sdrh ** is a simple min() or max() query.  If it is and this query can be
12589562b551Sdrh ** satisfied using a single seek to the beginning or end of an index,
12599562b551Sdrh ** then generate the code for this SELECT return 1.  If this is not a
12609562b551Sdrh ** simple min() or max() query, then return 0;
12619562b551Sdrh **
12629562b551Sdrh ** A simply min() or max() query looks like this:
12639562b551Sdrh **
12649562b551Sdrh **    SELECT min(a) FROM table;
12659562b551Sdrh **    SELECT max(a) FROM table;
12669562b551Sdrh **
12679562b551Sdrh ** The query may have only a single table in its FROM argument.  There
12689562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses.  The result set must
12699562b551Sdrh ** be the min() or max() of a single column of the table.  The column
12709562b551Sdrh ** in the min() or max() function must be indexed.
12719562b551Sdrh **
12729562b551Sdrh ** The parameters to this routine are the same as for sqliteSelect().
12739562b551Sdrh ** See the header comment on that routine for additional information.
12749562b551Sdrh */
12759562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
12769562b551Sdrh   Expr *pExpr;
12779562b551Sdrh   int iCol;
12789562b551Sdrh   Table *pTab;
12799562b551Sdrh   Index *pIdx;
12809562b551Sdrh   int base;
12819562b551Sdrh   Vdbe *v;
12829562b551Sdrh   int openOp;
12839562b551Sdrh   int seekOp;
12849562b551Sdrh   int cont;
12859562b551Sdrh   ExprList eList;
12869562b551Sdrh   struct ExprList_item eListItem;
12879562b551Sdrh 
12889562b551Sdrh   /* Check to see if this query is a simple min() or max() query.  Return
12899562b551Sdrh   ** zero if it is  not.
12909562b551Sdrh   */
12919562b551Sdrh   if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
1292ad3cab52Sdrh   if( p->pSrc->nSrc!=1 ) return 0;
12939562b551Sdrh   if( p->pEList->nExpr!=1 ) return 0;
12949562b551Sdrh   pExpr = p->pEList->a[0].pExpr;
12959562b551Sdrh   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
12969562b551Sdrh   if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
12970bce8354Sdrh   if( pExpr->token.n!=3 ) return 0;
12980bce8354Sdrh   if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
12990bce8354Sdrh     seekOp = OP_Rewind;
13000bce8354Sdrh   }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
13010bce8354Sdrh     seekOp = OP_Last;
13020bce8354Sdrh   }else{
13030bce8354Sdrh     return 0;
13040bce8354Sdrh   }
13059562b551Sdrh   pExpr = pExpr->pList->a[0].pExpr;
13069562b551Sdrh   if( pExpr->op!=TK_COLUMN ) return 0;
13079562b551Sdrh   iCol = pExpr->iColumn;
13089562b551Sdrh   pTab = p->pSrc->a[0].pTab;
13099562b551Sdrh 
13109562b551Sdrh   /* If we get to here, it means the query is of the correct form.
131117f71934Sdrh   ** Check to make sure we have an index and make pIdx point to the
131217f71934Sdrh   ** appropriate index.  If the min() or max() is on an INTEGER PRIMARY
131317f71934Sdrh   ** key column, no index is necessary so set pIdx to NULL.  If no
131417f71934Sdrh   ** usable index is found, return 0.
13159562b551Sdrh   */
13169562b551Sdrh   if( iCol<0 ){
13179562b551Sdrh     pIdx = 0;
13189562b551Sdrh   }else{
13199562b551Sdrh     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
13209562b551Sdrh       assert( pIdx->nColumn>=1 );
13219562b551Sdrh       if( pIdx->aiColumn[0]==iCol ) break;
13229562b551Sdrh     }
13239562b551Sdrh     if( pIdx==0 ) return 0;
13249562b551Sdrh   }
13259562b551Sdrh 
132617f71934Sdrh   /* Identify column names if we will be using the callback.  This
13279562b551Sdrh   ** step is skipped if the output is going to a table or a memory cell.
13289562b551Sdrh   */
13299562b551Sdrh   v = sqliteGetVdbe(pParse);
13309562b551Sdrh   if( v==0 ) return 0;
13319562b551Sdrh   if( eDest==SRT_Callback ){
1332832508b7Sdrh     generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
13339562b551Sdrh   }
13349562b551Sdrh 
133517f71934Sdrh   /* Generating code to find the min or the max.  Basically all we have
133617f71934Sdrh   ** to do is find the first or the last entry in the chosen index.  If
133717f71934Sdrh   ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
133817f71934Sdrh   ** or last entry in the main table.
13399562b551Sdrh   */
13405cf8e8c7Sdrh   if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
13415cf8e8c7Sdrh     sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
13425cf8e8c7Sdrh     pParse->schemaVerified = 1;
13435cf8e8c7Sdrh   }
13449562b551Sdrh   openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
1345832508b7Sdrh   base = p->base;
13469562b551Sdrh   sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
13475cf8e8c7Sdrh   sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
13489562b551Sdrh   if( pIdx==0 ){
13499562b551Sdrh     sqliteVdbeAddOp(v, seekOp, base, 0);
13509562b551Sdrh   }else{
13519562b551Sdrh     sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
13525cf8e8c7Sdrh     sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
13539562b551Sdrh     sqliteVdbeAddOp(v, seekOp, base+1, 0);
13549562b551Sdrh     sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
13559562b551Sdrh     sqliteVdbeAddOp(v, OP_Close, base+1, 0);
13569562b551Sdrh     sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
13579562b551Sdrh   }
13585cf8e8c7Sdrh   eList.nExpr = 1;
13595cf8e8c7Sdrh   memset(&eListItem, 0, sizeof(eListItem));
13605cf8e8c7Sdrh   eList.a = &eListItem;
13615cf8e8c7Sdrh   eList.a[0].pExpr = pExpr;
13629562b551Sdrh   cont = sqliteVdbeMakeLabel(v);
13639562b551Sdrh   selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont);
13649562b551Sdrh   sqliteVdbeResolveLabel(v, cont);
13659562b551Sdrh   sqliteVdbeAddOp(v, OP_Close, base, 0);
13669562b551Sdrh   return 1;
13679562b551Sdrh }
13689562b551Sdrh 
13699562b551Sdrh /*
13709bb61fe7Sdrh ** Generate code for the given SELECT statement.
13719bb61fe7Sdrh **
1372fef5208cSdrh ** The results are distributed in various ways depending on the
1373fef5208cSdrh ** value of eDest and iParm.
1374fef5208cSdrh **
1375fef5208cSdrh **     eDest Value       Result
1376fef5208cSdrh **     ------------    -------------------------------------------
1377fef5208cSdrh **     SRT_Callback    Invoke the callback for each row of the result.
1378fef5208cSdrh **
1379fef5208cSdrh **     SRT_Mem         Store first result in memory cell iParm
1380fef5208cSdrh **
1381fef5208cSdrh **     SRT_Set         Store results as keys of a table with cursor iParm
1382fef5208cSdrh **
138382c3d636Sdrh **     SRT_Union       Store results as a key in a temporary table iParm
138482c3d636Sdrh **
1385c4a3c779Sdrh **     SRT_Except      Remove results form the temporary table iParm.
1386c4a3c779Sdrh **
1387c4a3c779Sdrh **     SRT_Table       Store results in temporary table iParm
13889bb61fe7Sdrh **
13899bb61fe7Sdrh ** This routine returns the number of errors.  If any errors are
13909bb61fe7Sdrh ** encountered, then an appropriate error message is left in
13919bb61fe7Sdrh ** pParse->zErrMsg.
13929bb61fe7Sdrh **
13939bb61fe7Sdrh ** This routine does NOT free the Select structure passed in.  The
13949bb61fe7Sdrh ** calling function needs to do that.
13951b2e0329Sdrh **
13961b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this
13971b2e0329Sdrh ** SELECT is a subquery.  This routine may try to combine this SELECT
13981b2e0329Sdrh ** with its parent to form a single flat query.  In so doing, it might
13991b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query.
14001b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it
14011b2e0329Sdrh ** can be changed.
14029bb61fe7Sdrh */
14039bb61fe7Sdrh int sqliteSelect(
1404cce7d176Sdrh   Parse *pParse,         /* The parser context */
14059bb61fe7Sdrh   Select *p,             /* The SELECT statement being coded. */
140682c3d636Sdrh   int eDest,             /* One of: SRT_Callback Mem Set Union Except */
1407832508b7Sdrh   int iParm,             /* Save result in this memory location, if >=0 */
1408832508b7Sdrh   Select *pParent,       /* Another SELECT for which this is a sub-query */
1409832508b7Sdrh   int parentTab,         /* Index in pParent->pSrc of this query */
14101b2e0329Sdrh   int *pParentAgg        /* True if pParent uses aggregate functions */
1411cce7d176Sdrh ){
1412d8bc7086Sdrh   int i;
1413cce7d176Sdrh   WhereInfo *pWInfo;
1414cce7d176Sdrh   Vdbe *v;
1415cce7d176Sdrh   int isAgg = 0;         /* True for select lists like "count(*)" */
1416a2e00042Sdrh   ExprList *pEList;      /* List of columns to extract. */
1417ad3cab52Sdrh   SrcList *pTabList;     /* List of tables to select from */
14189bb61fe7Sdrh   Expr *pWhere;          /* The WHERE clause.  May be NULL */
14199bb61fe7Sdrh   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
14202282792aSdrh   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
14212282792aSdrh   Expr *pHaving;         /* The HAVING clause.  May be NULL */
142219a775c2Sdrh   int isDistinct;        /* True if the DISTINCT keyword is present */
142319a775c2Sdrh   int distinct;          /* Table to use for the distinct set */
142410e5e3cfSdrh   int base;              /* First cursor available for use */
14251d83f052Sdrh   int rc = 1;            /* Value to return from this function */
14269bb61fe7Sdrh 
1427daffd0e5Sdrh   if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1428daffd0e5Sdrh 
142982c3d636Sdrh   /* If there is are a sequence of queries, do the earlier ones first.
143082c3d636Sdrh   */
143182c3d636Sdrh   if( p->pPrior ){
143282c3d636Sdrh     return multiSelect(pParse, p, eDest, iParm);
143382c3d636Sdrh   }
143482c3d636Sdrh 
143582c3d636Sdrh   /* Make local copies of the parameters for this query.
143682c3d636Sdrh   */
14379bb61fe7Sdrh   pTabList = p->pSrc;
14389bb61fe7Sdrh   pWhere = p->pWhere;
14399bb61fe7Sdrh   pOrderBy = p->pOrderBy;
14402282792aSdrh   pGroupBy = p->pGroupBy;
14412282792aSdrh   pHaving = p->pHaving;
144219a775c2Sdrh   isDistinct = p->isDistinct;
14439bb61fe7Sdrh 
1444832508b7Sdrh   /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1445832508b7Sdrh   ** The WHERE processing requires that the cursors for the tables in the
1446832508b7Sdrh   ** FROM clause be consecutive.
144710e5e3cfSdrh   */
1448832508b7Sdrh   base = p->base = pParse->nTab;
1449ad3cab52Sdrh   pParse->nTab += pTabList->nSrc;
145010e5e3cfSdrh 
14519bb61fe7Sdrh   /*
14529bb61fe7Sdrh   ** Do not even attempt to generate any code if we have already seen
14539bb61fe7Sdrh   ** errors before this routine starts.
14549bb61fe7Sdrh   */
14551d83f052Sdrh   if( pParse->nErr>0 ) goto select_end;
1456cce7d176Sdrh 
1457d8bc7086Sdrh   /* Look up every table in the table list and create an appropriate
1458d8bc7086Sdrh   ** columnlist in pEList if there isn't one already.  (The parser leaves
1459967e8b73Sdrh   ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
1460cce7d176Sdrh   */
1461d8bc7086Sdrh   if( fillInColumnList(pParse, p) ){
14621d83f052Sdrh     goto select_end;
1463cce7d176Sdrh   }
1464ad2d8307Sdrh   pWhere = p->pWhere;
1465d8bc7086Sdrh   pEList = p->pEList;
14661d83f052Sdrh   if( pEList==0 ) goto select_end;
1467cce7d176Sdrh 
14682282792aSdrh   /* If writing to memory or generating a set
14692282792aSdrh   ** only a single column may be output.
147019a775c2Sdrh   */
1471fef5208cSdrh   if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
147219a775c2Sdrh     sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
147319a775c2Sdrh        "a SELECT that is part of an expression", 0);
147419a775c2Sdrh     pParse->nErr++;
14751d83f052Sdrh     goto select_end;
147619a775c2Sdrh   }
147719a775c2Sdrh 
14782282792aSdrh   /* ORDER BY is ignored if we are not sending the result to a callback.
14792282792aSdrh   */
14802282792aSdrh   if( eDest!=SRT_Callback ){
1481acd4c695Sdrh     pOrderBy = 0;
14822282792aSdrh   }
14832282792aSdrh 
148410e5e3cfSdrh   /* At this point, we should have allocated all the cursors that we
1485832508b7Sdrh   ** need to handle subquerys and temporary tables.
148610e5e3cfSdrh   **
1487967e8b73Sdrh   ** Resolve the column names and do a semantics check on all the expressions.
14882282792aSdrh   */
14894794b980Sdrh   for(i=0; i<pEList->nExpr; i++){
1490832508b7Sdrh     if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
14911d83f052Sdrh       goto select_end;
1492cce7d176Sdrh     }
14932282792aSdrh     if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
14941d83f052Sdrh       goto select_end;
1495cce7d176Sdrh     }
1496cce7d176Sdrh   }
1497cce7d176Sdrh   if( pWhere ){
1498832508b7Sdrh     if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
14991d83f052Sdrh       goto select_end;
1500cce7d176Sdrh     }
1501cce7d176Sdrh     if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
15021d83f052Sdrh       goto select_end;
1503cce7d176Sdrh     }
1504cce7d176Sdrh   }
1505cce7d176Sdrh   if( pOrderBy ){
1506cce7d176Sdrh     for(i=0; i<pOrderBy->nExpr; i++){
15072282792aSdrh       Expr *pE = pOrderBy->a[i].pExpr;
15089208643dSdrh       if( sqliteExprIsConstant(pE) ){
15099208643dSdrh         sqliteSetString(&pParse->zErrMsg,
15109208643dSdrh              "ORDER BY expressions should not be constant", 0);
15119208643dSdrh         pParse->nErr++;
15121d83f052Sdrh         goto select_end;
15139208643dSdrh       }
1514832508b7Sdrh       if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
15151d83f052Sdrh         goto select_end;
1516cce7d176Sdrh       }
15172282792aSdrh       if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
15181d83f052Sdrh         goto select_end;
1519cce7d176Sdrh       }
1520cce7d176Sdrh     }
1521cce7d176Sdrh   }
15222282792aSdrh   if( pGroupBy ){
15232282792aSdrh     for(i=0; i<pGroupBy->nExpr; i++){
15242282792aSdrh       Expr *pE = pGroupBy->a[i].pExpr;
15259208643dSdrh       if( sqliteExprIsConstant(pE) ){
15269208643dSdrh         sqliteSetString(&pParse->zErrMsg,
15279208643dSdrh              "GROUP BY expressions should not be constant", 0);
15289208643dSdrh         pParse->nErr++;
15291d83f052Sdrh         goto select_end;
15309208643dSdrh       }
1531832508b7Sdrh       if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
15321d83f052Sdrh         goto select_end;
15332282792aSdrh       }
15342282792aSdrh       if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
15351d83f052Sdrh         goto select_end;
15362282792aSdrh       }
15372282792aSdrh     }
15382282792aSdrh   }
15392282792aSdrh   if( pHaving ){
15402282792aSdrh     if( pGroupBy==0 ){
1541da93281eSdrh       sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1542da93281eSdrh          "before HAVING", 0);
15432282792aSdrh       pParse->nErr++;
15441d83f052Sdrh       goto select_end;
15452282792aSdrh     }
1546832508b7Sdrh     if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
15471d83f052Sdrh       goto select_end;
15482282792aSdrh     }
1549da93281eSdrh     if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
15501d83f052Sdrh       goto select_end;
15512282792aSdrh     }
1552cce7d176Sdrh   }
1553cce7d176Sdrh 
15549562b551Sdrh   /* Check for the special case of a min() or max() function by itself
15559562b551Sdrh   ** in the result set.
15569562b551Sdrh   */
15579562b551Sdrh   if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
15585cf8e8c7Sdrh     rc = 0;
15599562b551Sdrh     goto select_end;
15609562b551Sdrh   }
15619562b551Sdrh 
1562d820cb1bSdrh   /* Begin generating code.
1563d820cb1bSdrh   */
1564d820cb1bSdrh   v = sqliteGetVdbe(pParse);
1565d820cb1bSdrh   if( v==0 ) goto select_end;
1566d820cb1bSdrh 
15670bb28106Sdrh   /* Identify column names if we will be using in the callback.  This
15680bb28106Sdrh   ** step is skipped if the output is going to a table or a memory cell.
15690bb28106Sdrh   */
15700bb28106Sdrh   if( eDest==SRT_Callback ){
15710bb28106Sdrh     generateColumnNames(pParse, p->base, pTabList, pEList);
15720bb28106Sdrh   }
15730bb28106Sdrh 
15740bb28106Sdrh   /* Set the limiter
15750bb28106Sdrh   */
15760bb28106Sdrh   if( p->nLimit<=0 ){
15770bb28106Sdrh     p->nOffset = 0;
15780bb28106Sdrh   }else{
15790bb28106Sdrh     if( p->nOffset<0 ) p->nOffset = 0;
15800bb28106Sdrh     sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
15810bb28106Sdrh   }
15820bb28106Sdrh 
1583d820cb1bSdrh   /* Generate code for all sub-queries in the FROM clause
1584d820cb1bSdrh   */
1585ad3cab52Sdrh   for(i=0; i<pTabList->nSrc; i++){
1586a76b5dfcSdrh     if( pTabList->a[i].pSelect==0 ) continue;
15872d0794e3Sdrh     sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
15881b2e0329Sdrh                  p, i, &isAgg);
1589832508b7Sdrh     pTabList = p->pSrc;
1590832508b7Sdrh     pWhere = p->pWhere;
1591acd4c695Sdrh     if( eDest==SRT_Callback ){
1592832508b7Sdrh       pOrderBy = p->pOrderBy;
1593acd4c695Sdrh     }
1594832508b7Sdrh     pGroupBy = p->pGroupBy;
1595832508b7Sdrh     pHaving = p->pHaving;
1596832508b7Sdrh     isDistinct = p->isDistinct;
15971b2e0329Sdrh   }
15981b2e0329Sdrh 
15991b2e0329Sdrh   /* Check to see if this is a subquery that can be "flattened" into its parent.
16001b2e0329Sdrh   ** If flattening is a possiblity, do so and return immediately.
16011b2e0329Sdrh   */
16021b2e0329Sdrh   if( pParent && pParentAgg &&
16031b2e0329Sdrh       flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){
16041b2e0329Sdrh     if( isAgg ) *pParentAgg = 1;
16051b2e0329Sdrh     return rc;
16061b2e0329Sdrh   }
1607832508b7Sdrh 
16082d0794e3Sdrh   /* If the output is destined for a temporary table, open that table.
16092d0794e3Sdrh   */
16102d0794e3Sdrh   if( eDest==SRT_TempTable ){
16112d0794e3Sdrh     sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
16122d0794e3Sdrh   }
16132d0794e3Sdrh 
16142282792aSdrh   /* Do an analysis of aggregate expressions.
1615efb7251dSdrh   */
1616d820cb1bSdrh   sqliteAggregateInfoReset(pParse);
16172282792aSdrh   if( isAgg ){
16180bce8354Sdrh     assert( pParse->nAgg==0 );
16192282792aSdrh     for(i=0; i<pEList->nExpr; i++){
16202282792aSdrh       if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
16211d83f052Sdrh         goto select_end;
16222282792aSdrh       }
16232282792aSdrh     }
16242282792aSdrh     if( pGroupBy ){
16252282792aSdrh       for(i=0; i<pGroupBy->nExpr; i++){
16262282792aSdrh         if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
16271d83f052Sdrh           goto select_end;
16282282792aSdrh         }
16292282792aSdrh       }
16302282792aSdrh     }
16312282792aSdrh     if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
16321d83f052Sdrh       goto select_end;
16332282792aSdrh     }
1634191b690eSdrh     if( pOrderBy ){
1635191b690eSdrh       for(i=0; i<pOrderBy->nExpr; i++){
1636191b690eSdrh         if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
16371d83f052Sdrh           goto select_end;
1638191b690eSdrh         }
1639191b690eSdrh       }
1640191b690eSdrh     }
1641efb7251dSdrh   }
1642efb7251dSdrh 
16432282792aSdrh   /* Reset the aggregator
1644cce7d176Sdrh   */
1645cce7d176Sdrh   if( isAgg ){
164699fcd718Sdrh     sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
1647e5095355Sdrh     for(i=0; i<pParse->nAgg; i++){
16480bce8354Sdrh       FuncDef *pFunc;
16490bce8354Sdrh       if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
16501350b030Sdrh         sqliteVdbeAddOp(v, OP_AggInit, 0, i);
16510bce8354Sdrh         sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
1652e5095355Sdrh       }
1653e5095355Sdrh     }
16541bee3d7bSdrh     if( pGroupBy==0 ){
16551bee3d7bSdrh       sqliteVdbeAddOp(v, OP_String, 0, 0);
16561bee3d7bSdrh       sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
16571bee3d7bSdrh     }
1658cce7d176Sdrh   }
1659cce7d176Sdrh 
166019a775c2Sdrh   /* Initialize the memory cell to NULL
166119a775c2Sdrh   */
1662fef5208cSdrh   if( eDest==SRT_Mem ){
166399fcd718Sdrh     sqliteVdbeAddOp(v, OP_String, 0, 0);
16648721ce4aSdrh     sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
166519a775c2Sdrh   }
166619a775c2Sdrh 
1667832508b7Sdrh   /* Open a temporary table to use for the distinct set.
1668cce7d176Sdrh   */
166919a775c2Sdrh   if( isDistinct ){
1670832508b7Sdrh     distinct = pParse->nTab++;
1671c6b52df3Sdrh     sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
1672832508b7Sdrh   }else{
1673832508b7Sdrh     distinct = -1;
1674efb7251dSdrh   }
1675832508b7Sdrh 
1676832508b7Sdrh   /* Begin the database scan
1677832508b7Sdrh   */
1678832508b7Sdrh   pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0);
16791d83f052Sdrh   if( pWInfo==0 ) goto select_end;
1680cce7d176Sdrh 
16812282792aSdrh   /* Use the standard inner loop if we are not dealing with
16822282792aSdrh   ** aggregates
1683cce7d176Sdrh   */
1684da9d6c45Sdrh   if( !isAgg ){
168582c3d636Sdrh     if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
16862282792aSdrh                     pWInfo->iContinue, pWInfo->iBreak) ){
16871d83f052Sdrh        goto select_end;
1688cce7d176Sdrh     }
1689da9d6c45Sdrh   }
1690cce7d176Sdrh 
16912282792aSdrh   /* If we are dealing with aggregates, then to the special aggregate
16922282792aSdrh   ** processing.
1693efb7251dSdrh   */
16942282792aSdrh   else{
16952282792aSdrh     if( pGroupBy ){
16961bee3d7bSdrh       int lbl1;
16972282792aSdrh       for(i=0; i<pGroupBy->nExpr; i++){
16982282792aSdrh         sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
1699efb7251dSdrh       }
170099fcd718Sdrh       sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
17011bee3d7bSdrh       lbl1 = sqliteVdbeMakeLabel(v);
170299fcd718Sdrh       sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
17032282792aSdrh       for(i=0; i<pParse->nAgg; i++){
17042282792aSdrh         if( pParse->aAgg[i].isAgg ) continue;
17052282792aSdrh         sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
170699fcd718Sdrh         sqliteVdbeAddOp(v, OP_AggSet, 0, i);
17072282792aSdrh       }
17082282792aSdrh       sqliteVdbeResolveLabel(v, lbl1);
17092282792aSdrh     }
17102282792aSdrh     for(i=0; i<pParse->nAgg; i++){
17112282792aSdrh       Expr *pE;
17120bce8354Sdrh       int j;
17132282792aSdrh       if( !pParse->aAgg[i].isAgg ) continue;
17142282792aSdrh       pE = pParse->aAgg[i].pExpr;
17152282792aSdrh       assert( pE->op==TK_AGG_FUNCTION );
17160bce8354Sdrh       if( pE->pList ){
1717e5095355Sdrh         for(j=0; j<pE->pList->nExpr; j++){
1718e5095355Sdrh           sqliteExprCode(pParse, pE->pList->a[j].pExpr);
1719e5095355Sdrh         }
17202282792aSdrh       }
17211350b030Sdrh       sqliteVdbeAddOp(v, OP_Integer, i, 0);
1722f55f25f0Sdrh       sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
17230bce8354Sdrh       assert( pParse->aAgg[i].pFunc!=0 );
17240bce8354Sdrh       assert( pParse->aAgg[i].pFunc->xStep!=0 );
17250bce8354Sdrh       sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
17262282792aSdrh     }
17272282792aSdrh   }
17282282792aSdrh 
1729cce7d176Sdrh   /* End the database scan loop.
1730cce7d176Sdrh   */
1731cce7d176Sdrh   sqliteWhereEnd(pWInfo);
1732cce7d176Sdrh 
17332282792aSdrh   /* If we are processing aggregates, we need to set up a second loop
17342282792aSdrh   ** over all of the aggregate values and process them.
17352282792aSdrh   */
17362282792aSdrh   if( isAgg ){
17372282792aSdrh     int endagg = sqliteVdbeMakeLabel(v);
17382282792aSdrh     int startagg;
173999fcd718Sdrh     startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
17402282792aSdrh     pParse->useAgg = 1;
17412282792aSdrh     if( pHaving ){
1742f5905aa7Sdrh       sqliteExprIfFalse(pParse, pHaving, startagg, 1);
17432282792aSdrh     }
174482c3d636Sdrh     if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
17452282792aSdrh                     startagg, endagg) ){
17461d83f052Sdrh       goto select_end;
17472282792aSdrh     }
174899fcd718Sdrh     sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
174999fcd718Sdrh     sqliteVdbeResolveLabel(v, endagg);
175099fcd718Sdrh     sqliteVdbeAddOp(v, OP_Noop, 0, 0);
17512282792aSdrh     pParse->useAgg = 0;
17522282792aSdrh   }
17532282792aSdrh 
1754cce7d176Sdrh   /* If there is an ORDER BY clause, then we need to sort the results
1755cce7d176Sdrh   ** and send them to the callback one by one.
1756cce7d176Sdrh   */
1757cce7d176Sdrh   if( pOrderBy ){
1758d8bc7086Sdrh     generateSortTail(v, pEList->nExpr);
1759cce7d176Sdrh   }
17606a535340Sdrh 
17616a535340Sdrh 
17626a535340Sdrh   /* Issue a null callback if that is what the user wants.
17636a535340Sdrh   */
17646a535340Sdrh   if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
17656a535340Sdrh     sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
17666a535340Sdrh   }
17676a535340Sdrh 
17681d83f052Sdrh   /* The SELECT was successfully coded.   Set the return code to 0
17691d83f052Sdrh   ** to indicate no errors.
17701d83f052Sdrh   */
17711d83f052Sdrh   rc = 0;
17721d83f052Sdrh 
17731d83f052Sdrh   /* Control jumps to here if an error is encountered above, or upon
17741d83f052Sdrh   ** successful coding of the SELECT.
17751d83f052Sdrh   */
17761d83f052Sdrh select_end:
1777832508b7Sdrh   pParse->nTab = base;
17781d83f052Sdrh   sqliteAggregateInfoReset(pParse);
17791d83f052Sdrh   return rc;
1780cce7d176Sdrh }
1781