xref: /sqlite-3.40.0/src/select.c (revision 7a420e22)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains C code routines that are called by the parser
13 ** to handle SELECT statements in SQLite.
14 */
15 #include "sqliteInt.h"
16 
17 
18 /*
19 ** Delete all the content of a Select structure but do not deallocate
20 ** the select structure itself.
21 */
22 static void clearSelect(sqlite3 *db, Select *p){
23   sqlite3ExprListDelete(db, p->pEList);
24   sqlite3SrcListDelete(db, p->pSrc);
25   sqlite3ExprDelete(db, p->pWhere);
26   sqlite3ExprListDelete(db, p->pGroupBy);
27   sqlite3ExprDelete(db, p->pHaving);
28   sqlite3ExprListDelete(db, p->pOrderBy);
29   sqlite3SelectDelete(db, p->pPrior);
30   sqlite3ExprDelete(db, p->pLimit);
31   sqlite3ExprDelete(db, p->pOffset);
32 }
33 
34 /*
35 ** Initialize a SelectDest structure.
36 */
37 void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
38   pDest->eDest = (u8)eDest;
39   pDest->iParm = iParm;
40   pDest->affinity = 0;
41   pDest->iMem = 0;
42   pDest->nMem = 0;
43 }
44 
45 
46 /*
47 ** Allocate a new Select structure and return a pointer to that
48 ** structure.
49 */
50 Select *sqlite3SelectNew(
51   Parse *pParse,        /* Parsing context */
52   ExprList *pEList,     /* which columns to include in the result */
53   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
54   Expr *pWhere,         /* the WHERE clause */
55   ExprList *pGroupBy,   /* the GROUP BY clause */
56   Expr *pHaving,        /* the HAVING clause */
57   ExprList *pOrderBy,   /* the ORDER BY clause */
58   int isDistinct,       /* true if the DISTINCT keyword is present */
59   Expr *pLimit,         /* LIMIT value.  NULL means not used */
60   Expr *pOffset         /* OFFSET value.  NULL means no offset */
61 ){
62   Select *pNew;
63   Select standin;
64   sqlite3 *db = pParse->db;
65   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
66   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
67   if( pNew==0 ){
68     pNew = &standin;
69     memset(pNew, 0, sizeof(*pNew));
70   }
71   if( pEList==0 ){
72     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
73   }
74   pNew->pEList = pEList;
75   pNew->pSrc = pSrc;
76   pNew->pWhere = pWhere;
77   pNew->pGroupBy = pGroupBy;
78   pNew->pHaving = pHaving;
79   pNew->pOrderBy = pOrderBy;
80   pNew->selFlags = isDistinct ? SF_Distinct : 0;
81   pNew->op = TK_SELECT;
82   pNew->pLimit = pLimit;
83   pNew->pOffset = pOffset;
84   assert( pOffset==0 || pLimit!=0 );
85   pNew->addrOpenEphm[0] = -1;
86   pNew->addrOpenEphm[1] = -1;
87   pNew->addrOpenEphm[2] = -1;
88   if( db->mallocFailed ) {
89     clearSelect(db, pNew);
90     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
91     pNew = 0;
92   }
93   return pNew;
94 }
95 
96 /*
97 ** Delete the given Select structure and all of its substructures.
98 */
99 void sqlite3SelectDelete(sqlite3 *db, Select *p){
100   if( p ){
101     clearSelect(db, p);
102     sqlite3DbFree(db, p);
103   }
104 }
105 
106 /*
107 ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
108 ** type of join.  Return an integer constant that expresses that type
109 ** in terms of the following bit values:
110 **
111 **     JT_INNER
112 **     JT_CROSS
113 **     JT_OUTER
114 **     JT_NATURAL
115 **     JT_LEFT
116 **     JT_RIGHT
117 **
118 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
119 **
120 ** If an illegal or unsupported join type is seen, then still return
121 ** a join type, but put an error in the pParse structure.
122 */
123 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
124   int jointype = 0;
125   Token *apAll[3];
126   Token *p;
127                              /*   0123456789 123456789 123456789 123 */
128   static const char zKeyText[] = "naturaleftouterightfullinnercross";
129   static const struct {
130     u8 i;        /* Beginning of keyword text in zKeyText[] */
131     u8 nChar;    /* Length of the keyword in characters */
132     u8 code;     /* Join type mask */
133   } aKeyword[] = {
134     /* natural */ { 0,  7, JT_NATURAL                },
135     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
136     /* outer   */ { 10, 5, JT_OUTER                  },
137     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
138     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
139     /* inner   */ { 23, 5, JT_INNER                  },
140     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
141   };
142   int i, j;
143   apAll[0] = pA;
144   apAll[1] = pB;
145   apAll[2] = pC;
146   for(i=0; i<3 && apAll[i]; i++){
147     p = apAll[i];
148     for(j=0; j<ArraySize(aKeyword); j++){
149       if( p->n==aKeyword[j].nChar
150           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
151         jointype |= aKeyword[j].code;
152         break;
153       }
154     }
155     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
156     if( j>=ArraySize(aKeyword) ){
157       jointype |= JT_ERROR;
158       break;
159     }
160   }
161   if(
162      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
163      (jointype & JT_ERROR)!=0
164   ){
165     const char *zSp = " ";
166     assert( pB!=0 );
167     if( pC==0 ){ zSp++; }
168     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
169        "%T %T%s%T", pA, pB, zSp, pC);
170     jointype = JT_INNER;
171   }else if( (jointype & JT_OUTER)!=0
172          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
173     sqlite3ErrorMsg(pParse,
174       "RIGHT and FULL OUTER JOINs are not currently supported");
175     jointype = JT_INNER;
176   }
177   return jointype;
178 }
179 
180 /*
181 ** Return the index of a column in a table.  Return -1 if the column
182 ** is not contained in the table.
183 */
184 static int columnIndex(Table *pTab, const char *zCol){
185   int i;
186   for(i=0; i<pTab->nCol; i++){
187     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
188   }
189   return -1;
190 }
191 
192 /*
193 ** Search the first N tables in pSrc, from left to right, looking for a
194 ** table that has a column named zCol.
195 **
196 ** When found, set *piTab and *piCol to the table index and column index
197 ** of the matching column and return TRUE.
198 **
199 ** If not found, return FALSE.
200 */
201 static int tableAndColumnIndex(
202   SrcList *pSrc,       /* Array of tables to search */
203   int N,               /* Number of tables in pSrc->a[] to search */
204   const char *zCol,    /* Name of the column we are looking for */
205   int *piTab,          /* Write index of pSrc->a[] here */
206   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
207 ){
208   int i;               /* For looping over tables in pSrc */
209   int iCol;            /* Index of column matching zCol */
210 
211   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
212   for(i=0; i<N; i++){
213     iCol = columnIndex(pSrc->a[i].pTab, zCol);
214     if( iCol>=0 ){
215       if( piTab ){
216         *piTab = i;
217         *piCol = iCol;
218       }
219       return 1;
220     }
221   }
222   return 0;
223 }
224 
225 /*
226 ** This function is used to add terms implied by JOIN syntax to the
227 ** WHERE clause expression of a SELECT statement. The new term, which
228 ** is ANDed with the existing WHERE clause, is of the form:
229 **
230 **    (tab1.col1 = tab2.col2)
231 **
232 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
233 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
234 ** column iColRight of tab2.
235 */
236 static void addWhereTerm(
237   Parse *pParse,                  /* Parsing context */
238   SrcList *pSrc,                  /* List of tables in FROM clause */
239   int iLeft,                      /* Index of first table to join in pSrc */
240   int iColLeft,                   /* Index of column in first table */
241   int iRight,                     /* Index of second table in pSrc */
242   int iColRight,                  /* Index of column in second table */
243   int isOuterJoin,                /* True if this is an OUTER join */
244   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
245 ){
246   sqlite3 *db = pParse->db;
247   Expr *pE1;
248   Expr *pE2;
249   Expr *pEq;
250 
251   assert( iLeft<iRight );
252   assert( pSrc->nSrc>iRight );
253   assert( pSrc->a[iLeft].pTab );
254   assert( pSrc->a[iRight].pTab );
255 
256   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
257   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
258 
259   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
260   if( pEq && isOuterJoin ){
261     ExprSetProperty(pEq, EP_FromJoin);
262     assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
263     ExprSetIrreducible(pEq);
264     pEq->iRightJoinTable = (i16)pE2->iTable;
265   }
266   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
267 }
268 
269 /*
270 ** Set the EP_FromJoin property on all terms of the given expression.
271 ** And set the Expr.iRightJoinTable to iTable for every term in the
272 ** expression.
273 **
274 ** The EP_FromJoin property is used on terms of an expression to tell
275 ** the LEFT OUTER JOIN processing logic that this term is part of the
276 ** join restriction specified in the ON or USING clause and not a part
277 ** of the more general WHERE clause.  These terms are moved over to the
278 ** WHERE clause during join processing but we need to remember that they
279 ** originated in the ON or USING clause.
280 **
281 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
282 ** expression depends on table iRightJoinTable even if that table is not
283 ** explicitly mentioned in the expression.  That information is needed
284 ** for cases like this:
285 **
286 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
287 **
288 ** The where clause needs to defer the handling of the t1.x=5
289 ** term until after the t2 loop of the join.  In that way, a
290 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
291 ** defer the handling of t1.x=5, it will be processed immediately
292 ** after the t1 loop and rows with t1.x!=5 will never appear in
293 ** the output, which is incorrect.
294 */
295 static void setJoinExpr(Expr *p, int iTable){
296   while( p ){
297     ExprSetProperty(p, EP_FromJoin);
298     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
299     ExprSetIrreducible(p);
300     p->iRightJoinTable = (i16)iTable;
301     setJoinExpr(p->pLeft, iTable);
302     p = p->pRight;
303   }
304 }
305 
306 /*
307 ** This routine processes the join information for a SELECT statement.
308 ** ON and USING clauses are converted into extra terms of the WHERE clause.
309 ** NATURAL joins also create extra WHERE clause terms.
310 **
311 ** The terms of a FROM clause are contained in the Select.pSrc structure.
312 ** The left most table is the first entry in Select.pSrc.  The right-most
313 ** table is the last entry.  The join operator is held in the entry to
314 ** the left.  Thus entry 0 contains the join operator for the join between
315 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
316 ** also attached to the left entry.
317 **
318 ** This routine returns the number of errors encountered.
319 */
320 static int sqliteProcessJoin(Parse *pParse, Select *p){
321   SrcList *pSrc;                  /* All tables in the FROM clause */
322   int i, j;                       /* Loop counters */
323   struct SrcList_item *pLeft;     /* Left table being joined */
324   struct SrcList_item *pRight;    /* Right table being joined */
325 
326   pSrc = p->pSrc;
327   pLeft = &pSrc->a[0];
328   pRight = &pLeft[1];
329   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
330     Table *pLeftTab = pLeft->pTab;
331     Table *pRightTab = pRight->pTab;
332     int isOuter;
333 
334     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
335     isOuter = (pRight->jointype & JT_OUTER)!=0;
336 
337     /* When the NATURAL keyword is present, add WHERE clause terms for
338     ** every column that the two tables have in common.
339     */
340     if( pRight->jointype & JT_NATURAL ){
341       if( pRight->pOn || pRight->pUsing ){
342         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
343            "an ON or USING clause", 0);
344         return 1;
345       }
346       for(j=0; j<pRightTab->nCol; j++){
347         char *zName;   /* Name of column in the right table */
348         int iLeft;     /* Matching left table */
349         int iLeftCol;  /* Matching column in the left table */
350 
351         zName = pRightTab->aCol[j].zName;
352         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
353           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
354                        isOuter, &p->pWhere);
355         }
356       }
357     }
358 
359     /* Disallow both ON and USING clauses in the same join
360     */
361     if( pRight->pOn && pRight->pUsing ){
362       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
363         "clauses in the same join");
364       return 1;
365     }
366 
367     /* Add the ON clause to the end of the WHERE clause, connected by
368     ** an AND operator.
369     */
370     if( pRight->pOn ){
371       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
372       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
373       pRight->pOn = 0;
374     }
375 
376     /* Create extra terms on the WHERE clause for each column named
377     ** in the USING clause.  Example: If the two tables to be joined are
378     ** A and B and the USING clause names X, Y, and Z, then add this
379     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
380     ** Report an error if any column mentioned in the USING clause is
381     ** not contained in both tables to be joined.
382     */
383     if( pRight->pUsing ){
384       IdList *pList = pRight->pUsing;
385       for(j=0; j<pList->nId; j++){
386         char *zName;     /* Name of the term in the USING clause */
387         int iLeft;       /* Table on the left with matching column name */
388         int iLeftCol;    /* Column number of matching column on the left */
389         int iRightCol;   /* Column number of matching column on the right */
390 
391         zName = pList->a[j].zName;
392         iRightCol = columnIndex(pRightTab, zName);
393         if( iRightCol<0
394          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
395         ){
396           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
397             "not present in both tables", zName);
398           return 1;
399         }
400         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
401                      isOuter, &p->pWhere);
402       }
403     }
404   }
405   return 0;
406 }
407 
408 /*
409 ** Insert code into "v" that will push the record on the top of the
410 ** stack into the sorter.
411 */
412 static void pushOntoSorter(
413   Parse *pParse,         /* Parser context */
414   ExprList *pOrderBy,    /* The ORDER BY clause */
415   Select *pSelect,       /* The whole SELECT statement */
416   int regData            /* Register holding data to be sorted */
417 ){
418   Vdbe *v = pParse->pVdbe;
419   int nExpr = pOrderBy->nExpr;
420   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
421   int regRecord = sqlite3GetTempReg(pParse);
422   sqlite3ExprCacheClear(pParse);
423   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
424   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
425   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
426   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
427   sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
428   sqlite3ReleaseTempReg(pParse, regRecord);
429   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
430   if( pSelect->iLimit ){
431     int addr1, addr2;
432     int iLimit;
433     if( pSelect->iOffset ){
434       iLimit = pSelect->iOffset+1;
435     }else{
436       iLimit = pSelect->iLimit;
437     }
438     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
439     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
440     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
441     sqlite3VdbeJumpHere(v, addr1);
442     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
443     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
444     sqlite3VdbeJumpHere(v, addr2);
445   }
446 }
447 
448 /*
449 ** Add code to implement the OFFSET
450 */
451 static void codeOffset(
452   Vdbe *v,          /* Generate code into this VM */
453   Select *p,        /* The SELECT statement being coded */
454   int iContinue     /* Jump here to skip the current record */
455 ){
456   if( p->iOffset && iContinue!=0 ){
457     int addr;
458     sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
459     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
460     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
461     VdbeComment((v, "skip OFFSET records"));
462     sqlite3VdbeJumpHere(v, addr);
463   }
464 }
465 
466 /*
467 ** Add code that will check to make sure the N registers starting at iMem
468 ** form a distinct entry.  iTab is a sorting index that holds previously
469 ** seen combinations of the N values.  A new entry is made in iTab
470 ** if the current N values are new.
471 **
472 ** A jump to addrRepeat is made and the N+1 values are popped from the
473 ** stack if the top N elements are not distinct.
474 */
475 static void codeDistinct(
476   Parse *pParse,     /* Parsing and code generating context */
477   int iTab,          /* A sorting index used to test for distinctness */
478   int addrRepeat,    /* Jump to here if not distinct */
479   int N,             /* Number of elements */
480   int iMem           /* First element */
481 ){
482   Vdbe *v;
483   int r1;
484 
485   v = pParse->pVdbe;
486   r1 = sqlite3GetTempReg(pParse);
487   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
488   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
489   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
490   sqlite3ReleaseTempReg(pParse, r1);
491 }
492 
493 #ifndef SQLITE_OMIT_SUBQUERY
494 /*
495 ** Generate an error message when a SELECT is used within a subexpression
496 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
497 ** column.  We do this in a subroutine because the error used to occur
498 ** in multiple places.  (The error only occurs in one place now, but we
499 ** retain the subroutine to minimize code disruption.)
500 */
501 static int checkForMultiColumnSelectError(
502   Parse *pParse,       /* Parse context. */
503   SelectDest *pDest,   /* Destination of SELECT results */
504   int nExpr            /* Number of result columns returned by SELECT */
505 ){
506   int eDest = pDest->eDest;
507   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
508     sqlite3ErrorMsg(pParse, "only a single result allowed for "
509        "a SELECT that is part of an expression");
510     return 1;
511   }else{
512     return 0;
513   }
514 }
515 #endif
516 
517 /*
518 ** This routine generates the code for the inside of the inner loop
519 ** of a SELECT.
520 **
521 ** If srcTab and nColumn are both zero, then the pEList expressions
522 ** are evaluated in order to get the data for this row.  If nColumn>0
523 ** then data is pulled from srcTab and pEList is used only to get the
524 ** datatypes for each column.
525 */
526 static void selectInnerLoop(
527   Parse *pParse,          /* The parser context */
528   Select *p,              /* The complete select statement being coded */
529   ExprList *pEList,       /* List of values being extracted */
530   int srcTab,             /* Pull data from this table */
531   int nColumn,            /* Number of columns in the source table */
532   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
533   int distinct,           /* If >=0, make sure results are distinct */
534   SelectDest *pDest,      /* How to dispose of the results */
535   int iContinue,          /* Jump here to continue with next row */
536   int iBreak              /* Jump here to break out of the inner loop */
537 ){
538   Vdbe *v = pParse->pVdbe;
539   int i;
540   int hasDistinct;        /* True if the DISTINCT keyword is present */
541   int regResult;              /* Start of memory holding result set */
542   int eDest = pDest->eDest;   /* How to dispose of results */
543   int iParm = pDest->iParm;   /* First argument to disposal method */
544   int nResultCol;             /* Number of result columns */
545 
546   assert( v );
547   if( NEVER(v==0) ) return;
548   assert( pEList!=0 );
549   hasDistinct = distinct>=0;
550   if( pOrderBy==0 && !hasDistinct ){
551     codeOffset(v, p, iContinue);
552   }
553 
554   /* Pull the requested columns.
555   */
556   if( nColumn>0 ){
557     nResultCol = nColumn;
558   }else{
559     nResultCol = pEList->nExpr;
560   }
561   if( pDest->iMem==0 ){
562     pDest->iMem = pParse->nMem+1;
563     pDest->nMem = nResultCol;
564     pParse->nMem += nResultCol;
565   }else{
566     assert( pDest->nMem==nResultCol );
567   }
568   regResult = pDest->iMem;
569   if( nColumn>0 ){
570     for(i=0; i<nColumn; i++){
571       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
572     }
573   }else if( eDest!=SRT_Exists ){
574     /* If the destination is an EXISTS(...) expression, the actual
575     ** values returned by the SELECT are not required.
576     */
577     sqlite3ExprCacheClear(pParse);
578     sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
579   }
580   nColumn = nResultCol;
581 
582   /* If the DISTINCT keyword was present on the SELECT statement
583   ** and this row has been seen before, then do not make this row
584   ** part of the result.
585   */
586   if( hasDistinct ){
587     assert( pEList!=0 );
588     assert( pEList->nExpr==nColumn );
589     codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
590     if( pOrderBy==0 ){
591       codeOffset(v, p, iContinue);
592     }
593   }
594 
595   switch( eDest ){
596     /* In this mode, write each query result to the key of the temporary
597     ** table iParm.
598     */
599 #ifndef SQLITE_OMIT_COMPOUND_SELECT
600     case SRT_Union: {
601       int r1;
602       r1 = sqlite3GetTempReg(pParse);
603       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
604       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
605       sqlite3ReleaseTempReg(pParse, r1);
606       break;
607     }
608 
609     /* Construct a record from the query result, but instead of
610     ** saving that record, use it as a key to delete elements from
611     ** the temporary table iParm.
612     */
613     case SRT_Except: {
614       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
615       break;
616     }
617 #endif
618 
619     /* Store the result as data using a unique key.
620     */
621     case SRT_Table:
622     case SRT_EphemTab: {
623       int r1 = sqlite3GetTempReg(pParse);
624       testcase( eDest==SRT_Table );
625       testcase( eDest==SRT_EphemTab );
626       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
627       if( pOrderBy ){
628         pushOntoSorter(pParse, pOrderBy, p, r1);
629       }else{
630         int r2 = sqlite3GetTempReg(pParse);
631         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
632         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
633         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
634         sqlite3ReleaseTempReg(pParse, r2);
635       }
636       sqlite3ReleaseTempReg(pParse, r1);
637       break;
638     }
639 
640 #ifndef SQLITE_OMIT_SUBQUERY
641     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
642     ** then there should be a single item on the stack.  Write this
643     ** item into the set table with bogus data.
644     */
645     case SRT_Set: {
646       assert( nColumn==1 );
647       p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
648       if( pOrderBy ){
649         /* At first glance you would think we could optimize out the
650         ** ORDER BY in this case since the order of entries in the set
651         ** does not matter.  But there might be a LIMIT clause, in which
652         ** case the order does matter */
653         pushOntoSorter(pParse, pOrderBy, p, regResult);
654       }else{
655         int r1 = sqlite3GetTempReg(pParse);
656         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
657         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
658         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
659         sqlite3ReleaseTempReg(pParse, r1);
660       }
661       break;
662     }
663 
664     /* If any row exist in the result set, record that fact and abort.
665     */
666     case SRT_Exists: {
667       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
668       /* The LIMIT clause will terminate the loop for us */
669       break;
670     }
671 
672     /* If this is a scalar select that is part of an expression, then
673     ** store the results in the appropriate memory cell and break out
674     ** of the scan loop.
675     */
676     case SRT_Mem: {
677       assert( nColumn==1 );
678       if( pOrderBy ){
679         pushOntoSorter(pParse, pOrderBy, p, regResult);
680       }else{
681         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
682         /* The LIMIT clause will jump out of the loop for us */
683       }
684       break;
685     }
686 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
687 
688     /* Send the data to the callback function or to a subroutine.  In the
689     ** case of a subroutine, the subroutine itself is responsible for
690     ** popping the data from the stack.
691     */
692     case SRT_Coroutine:
693     case SRT_Output: {
694       testcase( eDest==SRT_Coroutine );
695       testcase( eDest==SRT_Output );
696       if( pOrderBy ){
697         int r1 = sqlite3GetTempReg(pParse);
698         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
699         pushOntoSorter(pParse, pOrderBy, p, r1);
700         sqlite3ReleaseTempReg(pParse, r1);
701       }else if( eDest==SRT_Coroutine ){
702         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
703       }else{
704         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
705         sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
706       }
707       break;
708     }
709 
710 #if !defined(SQLITE_OMIT_TRIGGER)
711     /* Discard the results.  This is used for SELECT statements inside
712     ** the body of a TRIGGER.  The purpose of such selects is to call
713     ** user-defined functions that have side effects.  We do not care
714     ** about the actual results of the select.
715     */
716     default: {
717       assert( eDest==SRT_Discard );
718       break;
719     }
720 #endif
721   }
722 
723   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
724   ** there is a sorter, in which case the sorter has already limited
725   ** the output for us.
726   */
727   if( pOrderBy==0 && p->iLimit ){
728     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
729   }
730 }
731 
732 /*
733 ** Given an expression list, generate a KeyInfo structure that records
734 ** the collating sequence for each expression in that expression list.
735 **
736 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
737 ** KeyInfo structure is appropriate for initializing a virtual index to
738 ** implement that clause.  If the ExprList is the result set of a SELECT
739 ** then the KeyInfo structure is appropriate for initializing a virtual
740 ** index to implement a DISTINCT test.
741 **
742 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
743 ** function is responsible for seeing that this structure is eventually
744 ** freed.  Add the KeyInfo structure to the P4 field of an opcode using
745 ** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
746 */
747 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
748   sqlite3 *db = pParse->db;
749   int nExpr;
750   KeyInfo *pInfo;
751   struct ExprList_item *pItem;
752   int i;
753 
754   nExpr = pList->nExpr;
755   pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
756   if( pInfo ){
757     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
758     pInfo->nField = (u16)nExpr;
759     pInfo->enc = ENC(db);
760     pInfo->db = db;
761     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
762       CollSeq *pColl;
763       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
764       if( !pColl ){
765         pColl = db->pDfltColl;
766       }
767       pInfo->aColl[i] = pColl;
768       pInfo->aSortOrder[i] = pItem->sortOrder;
769     }
770   }
771   return pInfo;
772 }
773 
774 
775 /*
776 ** If the inner loop was generated using a non-null pOrderBy argument,
777 ** then the results were placed in a sorter.  After the loop is terminated
778 ** we need to run the sorter and output the results.  The following
779 ** routine generates the code needed to do that.
780 */
781 static void generateSortTail(
782   Parse *pParse,    /* Parsing context */
783   Select *p,        /* The SELECT statement */
784   Vdbe *v,          /* Generate code into this VDBE */
785   int nColumn,      /* Number of columns of data */
786   SelectDest *pDest /* Write the sorted results here */
787 ){
788   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
789   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
790   int addr;
791   int iTab;
792   int pseudoTab = 0;
793   ExprList *pOrderBy = p->pOrderBy;
794 
795   int eDest = pDest->eDest;
796   int iParm = pDest->iParm;
797 
798   int regRow;
799   int regRowid;
800 
801   iTab = pOrderBy->iECursor;
802   regRow = sqlite3GetTempReg(pParse);
803   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
804     pseudoTab = pParse->nTab++;
805     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
806     regRowid = 0;
807   }else{
808     regRowid = sqlite3GetTempReg(pParse);
809   }
810   addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
811   codeOffset(v, p, addrContinue);
812   sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
813   switch( eDest ){
814     case SRT_Table:
815     case SRT_EphemTab: {
816       testcase( eDest==SRT_Table );
817       testcase( eDest==SRT_EphemTab );
818       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
819       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
820       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
821       break;
822     }
823 #ifndef SQLITE_OMIT_SUBQUERY
824     case SRT_Set: {
825       assert( nColumn==1 );
826       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
827       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
828       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
829       break;
830     }
831     case SRT_Mem: {
832       assert( nColumn==1 );
833       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
834       /* The LIMIT clause will terminate the loop for us */
835       break;
836     }
837 #endif
838     default: {
839       int i;
840       assert( eDest==SRT_Output || eDest==SRT_Coroutine );
841       testcase( eDest==SRT_Output );
842       testcase( eDest==SRT_Coroutine );
843       for(i=0; i<nColumn; i++){
844         assert( regRow!=pDest->iMem+i );
845         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
846         if( i==0 ){
847           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
848         }
849       }
850       if( eDest==SRT_Output ){
851         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
852         sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
853       }else{
854         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
855       }
856       break;
857     }
858   }
859   sqlite3ReleaseTempReg(pParse, regRow);
860   sqlite3ReleaseTempReg(pParse, regRowid);
861 
862   /* The bottom of the loop
863   */
864   sqlite3VdbeResolveLabel(v, addrContinue);
865   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
866   sqlite3VdbeResolveLabel(v, addrBreak);
867   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
868     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
869   }
870 }
871 
872 /*
873 ** Return a pointer to a string containing the 'declaration type' of the
874 ** expression pExpr. The string may be treated as static by the caller.
875 **
876 ** The declaration type is the exact datatype definition extracted from the
877 ** original CREATE TABLE statement if the expression is a column. The
878 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
879 ** is considered a column can be complex in the presence of subqueries. The
880 ** result-set expression in all of the following SELECT statements is
881 ** considered a column by this function.
882 **
883 **   SELECT col FROM tbl;
884 **   SELECT (SELECT col FROM tbl;
885 **   SELECT (SELECT col FROM tbl);
886 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
887 **
888 ** The declaration type for any expression other than a column is NULL.
889 */
890 static const char *columnType(
891   NameContext *pNC,
892   Expr *pExpr,
893   const char **pzOriginDb,
894   const char **pzOriginTab,
895   const char **pzOriginCol
896 ){
897   char const *zType = 0;
898   char const *zOriginDb = 0;
899   char const *zOriginTab = 0;
900   char const *zOriginCol = 0;
901   int j;
902   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
903 
904   switch( pExpr->op ){
905     case TK_AGG_COLUMN:
906     case TK_COLUMN: {
907       /* The expression is a column. Locate the table the column is being
908       ** extracted from in NameContext.pSrcList. This table may be real
909       ** database table or a subquery.
910       */
911       Table *pTab = 0;            /* Table structure column is extracted from */
912       Select *pS = 0;             /* Select the column is extracted from */
913       int iCol = pExpr->iColumn;  /* Index of column in pTab */
914       testcase( pExpr->op==TK_AGG_COLUMN );
915       testcase( pExpr->op==TK_COLUMN );
916       while( pNC && !pTab ){
917         SrcList *pTabList = pNC->pSrcList;
918         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
919         if( j<pTabList->nSrc ){
920           pTab = pTabList->a[j].pTab;
921           pS = pTabList->a[j].pSelect;
922         }else{
923           pNC = pNC->pNext;
924         }
925       }
926 
927       if( pTab==0 ){
928         /* At one time, code such as "SELECT new.x" within a trigger would
929         ** cause this condition to run.  Since then, we have restructured how
930         ** trigger code is generated and so this condition is no longer
931         ** possible. However, it can still be true for statements like
932         ** the following:
933         **
934         **   CREATE TABLE t1(col INTEGER);
935         **   SELECT (SELECT t1.col) FROM FROM t1;
936         **
937         ** when columnType() is called on the expression "t1.col" in the
938         ** sub-select. In this case, set the column type to NULL, even
939         ** though it should really be "INTEGER".
940         **
941         ** This is not a problem, as the column type of "t1.col" is never
942         ** used. When columnType() is called on the expression
943         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
944         ** branch below.  */
945         break;
946       }
947 
948       assert( pTab && pExpr->pTab==pTab );
949       if( pS ){
950         /* The "table" is actually a sub-select or a view in the FROM clause
951         ** of the SELECT statement. Return the declaration type and origin
952         ** data for the result-set column of the sub-select.
953         */
954         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
955           /* If iCol is less than zero, then the expression requests the
956           ** rowid of the sub-select or view. This expression is legal (see
957           ** test case misc2.2.2) - it always evaluates to NULL.
958           */
959           NameContext sNC;
960           Expr *p = pS->pEList->a[iCol].pExpr;
961           sNC.pSrcList = pS->pSrc;
962           sNC.pNext = pNC;
963           sNC.pParse = pNC->pParse;
964           zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
965         }
966       }else if( ALWAYS(pTab->pSchema) ){
967         /* A real table */
968         assert( !pS );
969         if( iCol<0 ) iCol = pTab->iPKey;
970         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
971         if( iCol<0 ){
972           zType = "INTEGER";
973           zOriginCol = "rowid";
974         }else{
975           zType = pTab->aCol[iCol].zType;
976           zOriginCol = pTab->aCol[iCol].zName;
977         }
978         zOriginTab = pTab->zName;
979         if( pNC->pParse ){
980           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
981           zOriginDb = pNC->pParse->db->aDb[iDb].zName;
982         }
983       }
984       break;
985     }
986 #ifndef SQLITE_OMIT_SUBQUERY
987     case TK_SELECT: {
988       /* The expression is a sub-select. Return the declaration type and
989       ** origin info for the single column in the result set of the SELECT
990       ** statement.
991       */
992       NameContext sNC;
993       Select *pS = pExpr->x.pSelect;
994       Expr *p = pS->pEList->a[0].pExpr;
995       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
996       sNC.pSrcList = pS->pSrc;
997       sNC.pNext = pNC;
998       sNC.pParse = pNC->pParse;
999       zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
1000       break;
1001     }
1002 #endif
1003   }
1004 
1005   if( pzOriginDb ){
1006     assert( pzOriginTab && pzOriginCol );
1007     *pzOriginDb = zOriginDb;
1008     *pzOriginTab = zOriginTab;
1009     *pzOriginCol = zOriginCol;
1010   }
1011   return zType;
1012 }
1013 
1014 /*
1015 ** Generate code that will tell the VDBE the declaration types of columns
1016 ** in the result set.
1017 */
1018 static void generateColumnTypes(
1019   Parse *pParse,      /* Parser context */
1020   SrcList *pTabList,  /* List of tables */
1021   ExprList *pEList    /* Expressions defining the result set */
1022 ){
1023 #ifndef SQLITE_OMIT_DECLTYPE
1024   Vdbe *v = pParse->pVdbe;
1025   int i;
1026   NameContext sNC;
1027   sNC.pSrcList = pTabList;
1028   sNC.pParse = pParse;
1029   for(i=0; i<pEList->nExpr; i++){
1030     Expr *p = pEList->a[i].pExpr;
1031     const char *zType;
1032 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1033     const char *zOrigDb = 0;
1034     const char *zOrigTab = 0;
1035     const char *zOrigCol = 0;
1036     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
1037 
1038     /* The vdbe must make its own copy of the column-type and other
1039     ** column specific strings, in case the schema is reset before this
1040     ** virtual machine is deleted.
1041     */
1042     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
1043     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
1044     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
1045 #else
1046     zType = columnType(&sNC, p, 0, 0, 0);
1047 #endif
1048     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
1049   }
1050 #endif /* SQLITE_OMIT_DECLTYPE */
1051 }
1052 
1053 /*
1054 ** Generate code that will tell the VDBE the names of columns
1055 ** in the result set.  This information is used to provide the
1056 ** azCol[] values in the callback.
1057 */
1058 static void generateColumnNames(
1059   Parse *pParse,      /* Parser context */
1060   SrcList *pTabList,  /* List of tables */
1061   ExprList *pEList    /* Expressions defining the result set */
1062 ){
1063   Vdbe *v = pParse->pVdbe;
1064   int i, j;
1065   sqlite3 *db = pParse->db;
1066   int fullNames, shortNames;
1067 
1068 #ifndef SQLITE_OMIT_EXPLAIN
1069   /* If this is an EXPLAIN, skip this step */
1070   if( pParse->explain ){
1071     return;
1072   }
1073 #endif
1074 
1075   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
1076   pParse->colNamesSet = 1;
1077   fullNames = (db->flags & SQLITE_FullColNames)!=0;
1078   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
1079   sqlite3VdbeSetNumCols(v, pEList->nExpr);
1080   for(i=0; i<pEList->nExpr; i++){
1081     Expr *p;
1082     p = pEList->a[i].pExpr;
1083     if( NEVER(p==0) ) continue;
1084     if( pEList->a[i].zName ){
1085       char *zName = pEList->a[i].zName;
1086       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
1087     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
1088       Table *pTab;
1089       char *zCol;
1090       int iCol = p->iColumn;
1091       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
1092         if( pTabList->a[j].iCursor==p->iTable ) break;
1093       }
1094       assert( j<pTabList->nSrc );
1095       pTab = pTabList->a[j].pTab;
1096       if( iCol<0 ) iCol = pTab->iPKey;
1097       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
1098       if( iCol<0 ){
1099         zCol = "rowid";
1100       }else{
1101         zCol = pTab->aCol[iCol].zName;
1102       }
1103       if( !shortNames && !fullNames ){
1104         sqlite3VdbeSetColName(v, i, COLNAME_NAME,
1105             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
1106       }else if( fullNames ){
1107         char *zName = 0;
1108         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
1109         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
1110       }else{
1111         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
1112       }
1113     }else{
1114       sqlite3VdbeSetColName(v, i, COLNAME_NAME,
1115           sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
1116     }
1117   }
1118   generateColumnTypes(pParse, pTabList, pEList);
1119 }
1120 
1121 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1122 /*
1123 ** Name of the connection operator, used for error messages.
1124 */
1125 static const char *selectOpName(int id){
1126   char *z;
1127   switch( id ){
1128     case TK_ALL:       z = "UNION ALL";   break;
1129     case TK_INTERSECT: z = "INTERSECT";   break;
1130     case TK_EXCEPT:    z = "EXCEPT";      break;
1131     default:           z = "UNION";       break;
1132   }
1133   return z;
1134 }
1135 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1136 
1137 /*
1138 ** Given a an expression list (which is really the list of expressions
1139 ** that form the result set of a SELECT statement) compute appropriate
1140 ** column names for a table that would hold the expression list.
1141 **
1142 ** All column names will be unique.
1143 **
1144 ** Only the column names are computed.  Column.zType, Column.zColl,
1145 ** and other fields of Column are zeroed.
1146 **
1147 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
1148 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
1149 */
1150 static int selectColumnsFromExprList(
1151   Parse *pParse,          /* Parsing context */
1152   ExprList *pEList,       /* Expr list from which to derive column names */
1153   int *pnCol,             /* Write the number of columns here */
1154   Column **paCol          /* Write the new column list here */
1155 ){
1156   sqlite3 *db = pParse->db;   /* Database connection */
1157   int i, j;                   /* Loop counters */
1158   int cnt;                    /* Index added to make the name unique */
1159   Column *aCol, *pCol;        /* For looping over result columns */
1160   int nCol;                   /* Number of columns in the result set */
1161   Expr *p;                    /* Expression for a single result column */
1162   char *zName;                /* Column name */
1163   int nName;                  /* Size of name in zName[] */
1164 
1165   *pnCol = nCol = pEList->nExpr;
1166   aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
1167   if( aCol==0 ) return SQLITE_NOMEM;
1168   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
1169     /* Get an appropriate name for the column
1170     */
1171     p = pEList->a[i].pExpr;
1172     assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
1173                || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
1174     if( (zName = pEList->a[i].zName)!=0 ){
1175       /* If the column contains an "AS <name>" phrase, use <name> as the name */
1176       zName = sqlite3DbStrDup(db, zName);
1177     }else{
1178       Expr *pColExpr = p;  /* The expression that is the result column name */
1179       Table *pTab;         /* Table associated with this expression */
1180       while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
1181       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
1182         /* For columns use the column name name */
1183         int iCol = pColExpr->iColumn;
1184         pTab = pColExpr->pTab;
1185         if( iCol<0 ) iCol = pTab->iPKey;
1186         zName = sqlite3MPrintf(db, "%s",
1187                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
1188       }else if( pColExpr->op==TK_ID ){
1189         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
1190         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
1191       }else{
1192         /* Use the original text of the column expression as its name */
1193         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
1194       }
1195     }
1196     if( db->mallocFailed ){
1197       sqlite3DbFree(db, zName);
1198       break;
1199     }
1200 
1201     /* Make sure the column name is unique.  If the name is not unique,
1202     ** append a integer to the name so that it becomes unique.
1203     */
1204     nName = sqlite3Strlen30(zName);
1205     for(j=cnt=0; j<i; j++){
1206       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
1207         char *zNewName;
1208         zName[nName] = 0;
1209         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
1210         sqlite3DbFree(db, zName);
1211         zName = zNewName;
1212         j = -1;
1213         if( zName==0 ) break;
1214       }
1215     }
1216     pCol->zName = zName;
1217   }
1218   if( db->mallocFailed ){
1219     for(j=0; j<i; j++){
1220       sqlite3DbFree(db, aCol[j].zName);
1221     }
1222     sqlite3DbFree(db, aCol);
1223     *paCol = 0;
1224     *pnCol = 0;
1225     return SQLITE_NOMEM;
1226   }
1227   return SQLITE_OK;
1228 }
1229 
1230 /*
1231 ** Add type and collation information to a column list based on
1232 ** a SELECT statement.
1233 **
1234 ** The column list presumably came from selectColumnNamesFromExprList().
1235 ** The column list has only names, not types or collations.  This
1236 ** routine goes through and adds the types and collations.
1237 **
1238 ** This routine requires that all identifiers in the SELECT
1239 ** statement be resolved.
1240 */
1241 static void selectAddColumnTypeAndCollation(
1242   Parse *pParse,        /* Parsing contexts */
1243   int nCol,             /* Number of columns */
1244   Column *aCol,         /* List of columns */
1245   Select *pSelect       /* SELECT used to determine types and collations */
1246 ){
1247   sqlite3 *db = pParse->db;
1248   NameContext sNC;
1249   Column *pCol;
1250   CollSeq *pColl;
1251   int i;
1252   Expr *p;
1253   struct ExprList_item *a;
1254 
1255   assert( pSelect!=0 );
1256   assert( (pSelect->selFlags & SF_Resolved)!=0 );
1257   assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
1258   if( db->mallocFailed ) return;
1259   memset(&sNC, 0, sizeof(sNC));
1260   sNC.pSrcList = pSelect->pSrc;
1261   a = pSelect->pEList->a;
1262   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
1263     p = a[i].pExpr;
1264     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
1265     pCol->affinity = sqlite3ExprAffinity(p);
1266     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
1267     pColl = sqlite3ExprCollSeq(pParse, p);
1268     if( pColl ){
1269       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
1270     }
1271   }
1272 }
1273 
1274 /*
1275 ** Given a SELECT statement, generate a Table structure that describes
1276 ** the result set of that SELECT.
1277 */
1278 Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
1279   Table *pTab;
1280   sqlite3 *db = pParse->db;
1281   int savedFlags;
1282 
1283   savedFlags = db->flags;
1284   db->flags &= ~SQLITE_FullColNames;
1285   db->flags |= SQLITE_ShortColNames;
1286   sqlite3SelectPrep(pParse, pSelect, 0);
1287   if( pParse->nErr ) return 0;
1288   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
1289   db->flags = savedFlags;
1290   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
1291   if( pTab==0 ){
1292     return 0;
1293   }
1294   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
1295   ** is disabled */
1296   assert( db->lookaside.bEnabled==0 );
1297   pTab->nRef = 1;
1298   pTab->zName = 0;
1299   pTab->nRowEst = 1000000;
1300   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
1301   selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
1302   pTab->iPKey = -1;
1303   if( db->mallocFailed ){
1304     sqlite3DeleteTable(db, pTab);
1305     return 0;
1306   }
1307   return pTab;
1308 }
1309 
1310 /*
1311 ** Get a VDBE for the given parser context.  Create a new one if necessary.
1312 ** If an error occurs, return NULL and leave a message in pParse.
1313 */
1314 Vdbe *sqlite3GetVdbe(Parse *pParse){
1315   Vdbe *v = pParse->pVdbe;
1316   if( v==0 ){
1317     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
1318 #ifndef SQLITE_OMIT_TRACE
1319     if( v ){
1320       sqlite3VdbeAddOp0(v, OP_Trace);
1321     }
1322 #endif
1323   }
1324   return v;
1325 }
1326 
1327 
1328 /*
1329 ** Compute the iLimit and iOffset fields of the SELECT based on the
1330 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
1331 ** that appear in the original SQL statement after the LIMIT and OFFSET
1332 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
1333 ** are the integer memory register numbers for counters used to compute
1334 ** the limit and offset.  If there is no limit and/or offset, then
1335 ** iLimit and iOffset are negative.
1336 **
1337 ** This routine changes the values of iLimit and iOffset only if
1338 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
1339 ** iOffset should have been preset to appropriate default values
1340 ** (usually but not always -1) prior to calling this routine.
1341 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
1342 ** redefined.  The UNION ALL operator uses this property to force
1343 ** the reuse of the same limit and offset registers across multiple
1344 ** SELECT statements.
1345 */
1346 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
1347   Vdbe *v = 0;
1348   int iLimit = 0;
1349   int iOffset;
1350   int addr1, n;
1351   if( p->iLimit ) return;
1352 
1353   /*
1354   ** "LIMIT -1" always shows all rows.  There is some
1355   ** contraversy about what the correct behavior should be.
1356   ** The current implementation interprets "LIMIT 0" to mean
1357   ** no rows.
1358   */
1359   sqlite3ExprCacheClear(pParse);
1360   assert( p->pOffset==0 || p->pLimit!=0 );
1361   if( p->pLimit ){
1362     p->iLimit = iLimit = ++pParse->nMem;
1363     v = sqlite3GetVdbe(pParse);
1364     if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
1365     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
1366       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
1367       VdbeComment((v, "LIMIT counter"));
1368       if( n==0 ){
1369         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
1370       }
1371     }else{
1372       sqlite3ExprCode(pParse, p->pLimit, iLimit);
1373       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
1374       VdbeComment((v, "LIMIT counter"));
1375       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
1376     }
1377     if( p->pOffset ){
1378       p->iOffset = iOffset = ++pParse->nMem;
1379       pParse->nMem++;   /* Allocate an extra register for limit+offset */
1380       sqlite3ExprCode(pParse, p->pOffset, iOffset);
1381       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
1382       VdbeComment((v, "OFFSET counter"));
1383       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
1384       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
1385       sqlite3VdbeJumpHere(v, addr1);
1386       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
1387       VdbeComment((v, "LIMIT+OFFSET"));
1388       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
1389       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
1390       sqlite3VdbeJumpHere(v, addr1);
1391     }
1392   }
1393 }
1394 
1395 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1396 /*
1397 ** Return the appropriate collating sequence for the iCol-th column of
1398 ** the result set for the compound-select statement "p".  Return NULL if
1399 ** the column has no default collating sequence.
1400 **
1401 ** The collating sequence for the compound select is taken from the
1402 ** left-most term of the select that has a collating sequence.
1403 */
1404 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
1405   CollSeq *pRet;
1406   if( p->pPrior ){
1407     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
1408   }else{
1409     pRet = 0;
1410   }
1411   assert( iCol>=0 );
1412   if( pRet==0 && iCol<p->pEList->nExpr ){
1413     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
1414   }
1415   return pRet;
1416 }
1417 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1418 
1419 /* Forward reference */
1420 static int multiSelectOrderBy(
1421   Parse *pParse,        /* Parsing context */
1422   Select *p,            /* The right-most of SELECTs to be coded */
1423   SelectDest *pDest     /* What to do with query results */
1424 );
1425 
1426 
1427 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1428 /*
1429 ** This routine is called to process a compound query form from
1430 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
1431 ** INTERSECT
1432 **
1433 ** "p" points to the right-most of the two queries.  the query on the
1434 ** left is p->pPrior.  The left query could also be a compound query
1435 ** in which case this routine will be called recursively.
1436 **
1437 ** The results of the total query are to be written into a destination
1438 ** of type eDest with parameter iParm.
1439 **
1440 ** Example 1:  Consider a three-way compound SQL statement.
1441 **
1442 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1443 **
1444 ** This statement is parsed up as follows:
1445 **
1446 **     SELECT c FROM t3
1447 **      |
1448 **      `----->  SELECT b FROM t2
1449 **                |
1450 **                `------>  SELECT a FROM t1
1451 **
1452 ** The arrows in the diagram above represent the Select.pPrior pointer.
1453 ** So if this routine is called with p equal to the t3 query, then
1454 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
1455 **
1456 ** Notice that because of the way SQLite parses compound SELECTs, the
1457 ** individual selects always group from left to right.
1458 */
1459 static int multiSelect(
1460   Parse *pParse,        /* Parsing context */
1461   Select *p,            /* The right-most of SELECTs to be coded */
1462   SelectDest *pDest     /* What to do with query results */
1463 ){
1464   int rc = SQLITE_OK;   /* Success code from a subroutine */
1465   Select *pPrior;       /* Another SELECT immediately to our left */
1466   Vdbe *v;              /* Generate code to this VDBE */
1467   SelectDest dest;      /* Alternative data destination */
1468   Select *pDelete = 0;  /* Chain of simple selects to delete */
1469   sqlite3 *db;          /* Database connection */
1470 
1471   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
1472   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
1473   */
1474   assert( p && p->pPrior );  /* Calling function guarantees this much */
1475   db = pParse->db;
1476   pPrior = p->pPrior;
1477   assert( pPrior->pRightmost!=pPrior );
1478   assert( pPrior->pRightmost==p->pRightmost );
1479   dest = *pDest;
1480   if( pPrior->pOrderBy ){
1481     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
1482       selectOpName(p->op));
1483     rc = 1;
1484     goto multi_select_end;
1485   }
1486   if( pPrior->pLimit ){
1487     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
1488       selectOpName(p->op));
1489     rc = 1;
1490     goto multi_select_end;
1491   }
1492 
1493   v = sqlite3GetVdbe(pParse);
1494   assert( v!=0 );  /* The VDBE already created by calling function */
1495 
1496   /* Create the destination temporary table if necessary
1497   */
1498   if( dest.eDest==SRT_EphemTab ){
1499     assert( p->pEList );
1500     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
1501     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
1502     dest.eDest = SRT_Table;
1503   }
1504 
1505   /* Make sure all SELECTs in the statement have the same number of elements
1506   ** in their result sets.
1507   */
1508   assert( p->pEList && pPrior->pEList );
1509   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
1510     sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
1511       " do not have the same number of result columns", selectOpName(p->op));
1512     rc = 1;
1513     goto multi_select_end;
1514   }
1515 
1516   /* Compound SELECTs that have an ORDER BY clause are handled separately.
1517   */
1518   if( p->pOrderBy ){
1519     return multiSelectOrderBy(pParse, p, pDest);
1520   }
1521 
1522   /* Generate code for the left and right SELECT statements.
1523   */
1524   switch( p->op ){
1525     case TK_ALL: {
1526       int addr = 0;
1527       assert( !pPrior->pLimit );
1528       pPrior->pLimit = p->pLimit;
1529       pPrior->pOffset = p->pOffset;
1530       rc = sqlite3Select(pParse, pPrior, &dest);
1531       p->pLimit = 0;
1532       p->pOffset = 0;
1533       if( rc ){
1534         goto multi_select_end;
1535       }
1536       p->pPrior = 0;
1537       p->iLimit = pPrior->iLimit;
1538       p->iOffset = pPrior->iOffset;
1539       if( p->iLimit ){
1540         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
1541         VdbeComment((v, "Jump ahead if LIMIT reached"));
1542       }
1543       rc = sqlite3Select(pParse, p, &dest);
1544       testcase( rc!=SQLITE_OK );
1545       pDelete = p->pPrior;
1546       p->pPrior = pPrior;
1547       if( addr ){
1548         sqlite3VdbeJumpHere(v, addr);
1549       }
1550       break;
1551     }
1552     case TK_EXCEPT:
1553     case TK_UNION: {
1554       int unionTab;    /* Cursor number of the temporary table holding result */
1555       u8 op = 0;       /* One of the SRT_ operations to apply to self */
1556       int priorOp;     /* The SRT_ operation to apply to prior selects */
1557       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
1558       int addr;
1559       SelectDest uniondest;
1560 
1561       testcase( p->op==TK_EXCEPT );
1562       testcase( p->op==TK_UNION );
1563       priorOp = SRT_Union;
1564       if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
1565         /* We can reuse a temporary table generated by a SELECT to our
1566         ** right.
1567         */
1568         assert( p->pRightmost!=p );  /* Can only happen for leftward elements
1569                                      ** of a 3-way or more compound */
1570         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
1571         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
1572         unionTab = dest.iParm;
1573       }else{
1574         /* We will need to create our own temporary table to hold the
1575         ** intermediate results.
1576         */
1577         unionTab = pParse->nTab++;
1578         assert( p->pOrderBy==0 );
1579         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
1580         assert( p->addrOpenEphm[0] == -1 );
1581         p->addrOpenEphm[0] = addr;
1582         p->pRightmost->selFlags |= SF_UsesEphemeral;
1583         assert( p->pEList );
1584       }
1585 
1586       /* Code the SELECT statements to our left
1587       */
1588       assert( !pPrior->pOrderBy );
1589       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
1590       rc = sqlite3Select(pParse, pPrior, &uniondest);
1591       if( rc ){
1592         goto multi_select_end;
1593       }
1594 
1595       /* Code the current SELECT statement
1596       */
1597       if( p->op==TK_EXCEPT ){
1598         op = SRT_Except;
1599       }else{
1600         assert( p->op==TK_UNION );
1601         op = SRT_Union;
1602       }
1603       p->pPrior = 0;
1604       pLimit = p->pLimit;
1605       p->pLimit = 0;
1606       pOffset = p->pOffset;
1607       p->pOffset = 0;
1608       uniondest.eDest = op;
1609       rc = sqlite3Select(pParse, p, &uniondest);
1610       testcase( rc!=SQLITE_OK );
1611       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
1612       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
1613       sqlite3ExprListDelete(db, p->pOrderBy);
1614       pDelete = p->pPrior;
1615       p->pPrior = pPrior;
1616       p->pOrderBy = 0;
1617       sqlite3ExprDelete(db, p->pLimit);
1618       p->pLimit = pLimit;
1619       p->pOffset = pOffset;
1620       p->iLimit = 0;
1621       p->iOffset = 0;
1622 
1623       /* Convert the data in the temporary table into whatever form
1624       ** it is that we currently need.
1625       */
1626       assert( unionTab==dest.iParm || dest.eDest!=priorOp );
1627       if( dest.eDest!=priorOp ){
1628         int iCont, iBreak, iStart;
1629         assert( p->pEList );
1630         if( dest.eDest==SRT_Output ){
1631           Select *pFirst = p;
1632           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
1633           generateColumnNames(pParse, 0, pFirst->pEList);
1634         }
1635         iBreak = sqlite3VdbeMakeLabel(v);
1636         iCont = sqlite3VdbeMakeLabel(v);
1637         computeLimitRegisters(pParse, p, iBreak);
1638         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
1639         iStart = sqlite3VdbeCurrentAddr(v);
1640         selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
1641                         0, -1, &dest, iCont, iBreak);
1642         sqlite3VdbeResolveLabel(v, iCont);
1643         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
1644         sqlite3VdbeResolveLabel(v, iBreak);
1645         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
1646       }
1647       break;
1648     }
1649     default: assert( p->op==TK_INTERSECT ); {
1650       int tab1, tab2;
1651       int iCont, iBreak, iStart;
1652       Expr *pLimit, *pOffset;
1653       int addr;
1654       SelectDest intersectdest;
1655       int r1;
1656 
1657       /* INTERSECT is different from the others since it requires
1658       ** two temporary tables.  Hence it has its own case.  Begin
1659       ** by allocating the tables we will need.
1660       */
1661       tab1 = pParse->nTab++;
1662       tab2 = pParse->nTab++;
1663       assert( p->pOrderBy==0 );
1664 
1665       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
1666       assert( p->addrOpenEphm[0] == -1 );
1667       p->addrOpenEphm[0] = addr;
1668       p->pRightmost->selFlags |= SF_UsesEphemeral;
1669       assert( p->pEList );
1670 
1671       /* Code the SELECTs to our left into temporary table "tab1".
1672       */
1673       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
1674       rc = sqlite3Select(pParse, pPrior, &intersectdest);
1675       if( rc ){
1676         goto multi_select_end;
1677       }
1678 
1679       /* Code the current SELECT into temporary table "tab2"
1680       */
1681       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
1682       assert( p->addrOpenEphm[1] == -1 );
1683       p->addrOpenEphm[1] = addr;
1684       p->pPrior = 0;
1685       pLimit = p->pLimit;
1686       p->pLimit = 0;
1687       pOffset = p->pOffset;
1688       p->pOffset = 0;
1689       intersectdest.iParm = tab2;
1690       rc = sqlite3Select(pParse, p, &intersectdest);
1691       testcase( rc!=SQLITE_OK );
1692       pDelete = p->pPrior;
1693       p->pPrior = pPrior;
1694       sqlite3ExprDelete(db, p->pLimit);
1695       p->pLimit = pLimit;
1696       p->pOffset = pOffset;
1697 
1698       /* Generate code to take the intersection of the two temporary
1699       ** tables.
1700       */
1701       assert( p->pEList );
1702       if( dest.eDest==SRT_Output ){
1703         Select *pFirst = p;
1704         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
1705         generateColumnNames(pParse, 0, pFirst->pEList);
1706       }
1707       iBreak = sqlite3VdbeMakeLabel(v);
1708       iCont = sqlite3VdbeMakeLabel(v);
1709       computeLimitRegisters(pParse, p, iBreak);
1710       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
1711       r1 = sqlite3GetTempReg(pParse);
1712       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
1713       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
1714       sqlite3ReleaseTempReg(pParse, r1);
1715       selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
1716                       0, -1, &dest, iCont, iBreak);
1717       sqlite3VdbeResolveLabel(v, iCont);
1718       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
1719       sqlite3VdbeResolveLabel(v, iBreak);
1720       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
1721       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
1722       break;
1723     }
1724   }
1725 
1726   /* Compute collating sequences used by
1727   ** temporary tables needed to implement the compound select.
1728   ** Attach the KeyInfo structure to all temporary tables.
1729   **
1730   ** This section is run by the right-most SELECT statement only.
1731   ** SELECT statements to the left always skip this part.  The right-most
1732   ** SELECT might also skip this part if it has no ORDER BY clause and
1733   ** no temp tables are required.
1734   */
1735   if( p->selFlags & SF_UsesEphemeral ){
1736     int i;                        /* Loop counter */
1737     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
1738     Select *pLoop;                /* For looping through SELECT statements */
1739     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
1740     int nCol;                     /* Number of columns in result set */
1741 
1742     assert( p->pRightmost==p );
1743     nCol = p->pEList->nExpr;
1744     pKeyInfo = sqlite3DbMallocZero(db,
1745                        sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
1746     if( !pKeyInfo ){
1747       rc = SQLITE_NOMEM;
1748       goto multi_select_end;
1749     }
1750 
1751     pKeyInfo->enc = ENC(db);
1752     pKeyInfo->nField = (u16)nCol;
1753 
1754     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
1755       *apColl = multiSelectCollSeq(pParse, p, i);
1756       if( 0==*apColl ){
1757         *apColl = db->pDfltColl;
1758       }
1759     }
1760 
1761     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
1762       for(i=0; i<2; i++){
1763         int addr = pLoop->addrOpenEphm[i];
1764         if( addr<0 ){
1765           /* If [0] is unused then [1] is also unused.  So we can
1766           ** always safely abort as soon as the first unused slot is found */
1767           assert( pLoop->addrOpenEphm[1]<0 );
1768           break;
1769         }
1770         sqlite3VdbeChangeP2(v, addr, nCol);
1771         sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
1772         pLoop->addrOpenEphm[i] = -1;
1773       }
1774     }
1775     sqlite3DbFree(db, pKeyInfo);
1776   }
1777 
1778 multi_select_end:
1779   pDest->iMem = dest.iMem;
1780   pDest->nMem = dest.nMem;
1781   sqlite3SelectDelete(db, pDelete);
1782   return rc;
1783 }
1784 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1785 
1786 /*
1787 ** Code an output subroutine for a coroutine implementation of a
1788 ** SELECT statment.
1789 **
1790 ** The data to be output is contained in pIn->iMem.  There are
1791 ** pIn->nMem columns to be output.  pDest is where the output should
1792 ** be sent.
1793 **
1794 ** regReturn is the number of the register holding the subroutine
1795 ** return address.
1796 **
1797 ** If regPrev>0 then it is the first register in a vector that
1798 ** records the previous output.  mem[regPrev] is a flag that is false
1799 ** if there has been no previous output.  If regPrev>0 then code is
1800 ** generated to suppress duplicates.  pKeyInfo is used for comparing
1801 ** keys.
1802 **
1803 ** If the LIMIT found in p->iLimit is reached, jump immediately to
1804 ** iBreak.
1805 */
1806 static int generateOutputSubroutine(
1807   Parse *pParse,          /* Parsing context */
1808   Select *p,              /* The SELECT statement */
1809   SelectDest *pIn,        /* Coroutine supplying data */
1810   SelectDest *pDest,      /* Where to send the data */
1811   int regReturn,          /* The return address register */
1812   int regPrev,            /* Previous result register.  No uniqueness if 0 */
1813   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
1814   int p4type,             /* The p4 type for pKeyInfo */
1815   int iBreak              /* Jump here if we hit the LIMIT */
1816 ){
1817   Vdbe *v = pParse->pVdbe;
1818   int iContinue;
1819   int addr;
1820 
1821   addr = sqlite3VdbeCurrentAddr(v);
1822   iContinue = sqlite3VdbeMakeLabel(v);
1823 
1824   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
1825   */
1826   if( regPrev ){
1827     int j1, j2;
1828     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
1829     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
1830                               (char*)pKeyInfo, p4type);
1831     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
1832     sqlite3VdbeJumpHere(v, j1);
1833     sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
1834     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
1835   }
1836   if( pParse->db->mallocFailed ) return 0;
1837 
1838   /* Suppress the the first OFFSET entries if there is an OFFSET clause
1839   */
1840   codeOffset(v, p, iContinue);
1841 
1842   switch( pDest->eDest ){
1843     /* Store the result as data using a unique key.
1844     */
1845     case SRT_Table:
1846     case SRT_EphemTab: {
1847       int r1 = sqlite3GetTempReg(pParse);
1848       int r2 = sqlite3GetTempReg(pParse);
1849       testcase( pDest->eDest==SRT_Table );
1850       testcase( pDest->eDest==SRT_EphemTab );
1851       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
1852       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
1853       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
1854       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
1855       sqlite3ReleaseTempReg(pParse, r2);
1856       sqlite3ReleaseTempReg(pParse, r1);
1857       break;
1858     }
1859 
1860 #ifndef SQLITE_OMIT_SUBQUERY
1861     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
1862     ** then there should be a single item on the stack.  Write this
1863     ** item into the set table with bogus data.
1864     */
1865     case SRT_Set: {
1866       int r1;
1867       assert( pIn->nMem==1 );
1868       p->affinity =
1869          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
1870       r1 = sqlite3GetTempReg(pParse);
1871       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
1872       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
1873       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
1874       sqlite3ReleaseTempReg(pParse, r1);
1875       break;
1876     }
1877 
1878 #if 0  /* Never occurs on an ORDER BY query */
1879     /* If any row exist in the result set, record that fact and abort.
1880     */
1881     case SRT_Exists: {
1882       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
1883       /* The LIMIT clause will terminate the loop for us */
1884       break;
1885     }
1886 #endif
1887 
1888     /* If this is a scalar select that is part of an expression, then
1889     ** store the results in the appropriate memory cell and break out
1890     ** of the scan loop.
1891     */
1892     case SRT_Mem: {
1893       assert( pIn->nMem==1 );
1894       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
1895       /* The LIMIT clause will jump out of the loop for us */
1896       break;
1897     }
1898 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
1899 
1900     /* The results are stored in a sequence of registers
1901     ** starting at pDest->iMem.  Then the co-routine yields.
1902     */
1903     case SRT_Coroutine: {
1904       if( pDest->iMem==0 ){
1905         pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
1906         pDest->nMem = pIn->nMem;
1907       }
1908       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
1909       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
1910       break;
1911     }
1912 
1913     /* If none of the above, then the result destination must be
1914     ** SRT_Output.  This routine is never called with any other
1915     ** destination other than the ones handled above or SRT_Output.
1916     **
1917     ** For SRT_Output, results are stored in a sequence of registers.
1918     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
1919     ** return the next row of result.
1920     */
1921     default: {
1922       assert( pDest->eDest==SRT_Output );
1923       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
1924       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
1925       break;
1926     }
1927   }
1928 
1929   /* Jump to the end of the loop if the LIMIT is reached.
1930   */
1931   if( p->iLimit ){
1932     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
1933   }
1934 
1935   /* Generate the subroutine return
1936   */
1937   sqlite3VdbeResolveLabel(v, iContinue);
1938   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
1939 
1940   return addr;
1941 }
1942 
1943 /*
1944 ** Alternative compound select code generator for cases when there
1945 ** is an ORDER BY clause.
1946 **
1947 ** We assume a query of the following form:
1948 **
1949 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
1950 **
1951 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
1952 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
1953 ** co-routines.  Then run the co-routines in parallel and merge the results
1954 ** into the output.  In addition to the two coroutines (called selectA and
1955 ** selectB) there are 7 subroutines:
1956 **
1957 **    outA:    Move the output of the selectA coroutine into the output
1958 **             of the compound query.
1959 **
1960 **    outB:    Move the output of the selectB coroutine into the output
1961 **             of the compound query.  (Only generated for UNION and
1962 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
1963 **             appears only in B.)
1964 **
1965 **    AltB:    Called when there is data from both coroutines and A<B.
1966 **
1967 **    AeqB:    Called when there is data from both coroutines and A==B.
1968 **
1969 **    AgtB:    Called when there is data from both coroutines and A>B.
1970 **
1971 **    EofA:    Called when data is exhausted from selectA.
1972 **
1973 **    EofB:    Called when data is exhausted from selectB.
1974 **
1975 ** The implementation of the latter five subroutines depend on which
1976 ** <operator> is used:
1977 **
1978 **
1979 **             UNION ALL         UNION            EXCEPT          INTERSECT
1980 **          -------------  -----------------  --------------  -----------------
1981 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
1982 **
1983 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
1984 **
1985 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
1986 **
1987 **   EofA:   outB, nextB      outB, nextB          halt             halt
1988 **
1989 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
1990 **
1991 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
1992 ** causes an immediate jump to EofA and an EOF on B following nextB causes
1993 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
1994 ** following nextX causes a jump to the end of the select processing.
1995 **
1996 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
1997 ** within the output subroutine.  The regPrev register set holds the previously
1998 ** output value.  A comparison is made against this value and the output
1999 ** is skipped if the next results would be the same as the previous.
2000 **
2001 ** The implementation plan is to implement the two coroutines and seven
2002 ** subroutines first, then put the control logic at the bottom.  Like this:
2003 **
2004 **          goto Init
2005 **     coA: coroutine for left query (A)
2006 **     coB: coroutine for right query (B)
2007 **    outA: output one row of A
2008 **    outB: output one row of B (UNION and UNION ALL only)
2009 **    EofA: ...
2010 **    EofB: ...
2011 **    AltB: ...
2012 **    AeqB: ...
2013 **    AgtB: ...
2014 **    Init: initialize coroutine registers
2015 **          yield coA
2016 **          if eof(A) goto EofA
2017 **          yield coB
2018 **          if eof(B) goto EofB
2019 **    Cmpr: Compare A, B
2020 **          Jump AltB, AeqB, AgtB
2021 **     End: ...
2022 **
2023 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
2024 ** actually called using Gosub and they do not Return.  EofA and EofB loop
2025 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
2026 ** and AgtB jump to either L2 or to one of EofA or EofB.
2027 */
2028 #ifndef SQLITE_OMIT_COMPOUND_SELECT
2029 static int multiSelectOrderBy(
2030   Parse *pParse,        /* Parsing context */
2031   Select *p,            /* The right-most of SELECTs to be coded */
2032   SelectDest *pDest     /* What to do with query results */
2033 ){
2034   int i, j;             /* Loop counters */
2035   Select *pPrior;       /* Another SELECT immediately to our left */
2036   Vdbe *v;              /* Generate code to this VDBE */
2037   SelectDest destA;     /* Destination for coroutine A */
2038   SelectDest destB;     /* Destination for coroutine B */
2039   int regAddrA;         /* Address register for select-A coroutine */
2040   int regEofA;          /* Flag to indicate when select-A is complete */
2041   int regAddrB;         /* Address register for select-B coroutine */
2042   int regEofB;          /* Flag to indicate when select-B is complete */
2043   int addrSelectA;      /* Address of the select-A coroutine */
2044   int addrSelectB;      /* Address of the select-B coroutine */
2045   int regOutA;          /* Address register for the output-A subroutine */
2046   int regOutB;          /* Address register for the output-B subroutine */
2047   int addrOutA;         /* Address of the output-A subroutine */
2048   int addrOutB = 0;     /* Address of the output-B subroutine */
2049   int addrEofA;         /* Address of the select-A-exhausted subroutine */
2050   int addrEofB;         /* Address of the select-B-exhausted subroutine */
2051   int addrAltB;         /* Address of the A<B subroutine */
2052   int addrAeqB;         /* Address of the A==B subroutine */
2053   int addrAgtB;         /* Address of the A>B subroutine */
2054   int regLimitA;        /* Limit register for select-A */
2055   int regLimitB;        /* Limit register for select-A */
2056   int regPrev;          /* A range of registers to hold previous output */
2057   int savedLimit;       /* Saved value of p->iLimit */
2058   int savedOffset;      /* Saved value of p->iOffset */
2059   int labelCmpr;        /* Label for the start of the merge algorithm */
2060   int labelEnd;         /* Label for the end of the overall SELECT stmt */
2061   int j1;               /* Jump instructions that get retargetted */
2062   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
2063   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
2064   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
2065   sqlite3 *db;          /* Database connection */
2066   ExprList *pOrderBy;   /* The ORDER BY clause */
2067   int nOrderBy;         /* Number of terms in the ORDER BY clause */
2068   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
2069 
2070   assert( p->pOrderBy!=0 );
2071   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
2072   db = pParse->db;
2073   v = pParse->pVdbe;
2074   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
2075   labelEnd = sqlite3VdbeMakeLabel(v);
2076   labelCmpr = sqlite3VdbeMakeLabel(v);
2077 
2078 
2079   /* Patch up the ORDER BY clause
2080   */
2081   op = p->op;
2082   pPrior = p->pPrior;
2083   assert( pPrior->pOrderBy==0 );
2084   pOrderBy = p->pOrderBy;
2085   assert( pOrderBy );
2086   nOrderBy = pOrderBy->nExpr;
2087 
2088   /* For operators other than UNION ALL we have to make sure that
2089   ** the ORDER BY clause covers every term of the result set.  Add
2090   ** terms to the ORDER BY clause as necessary.
2091   */
2092   if( op!=TK_ALL ){
2093     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
2094       struct ExprList_item *pItem;
2095       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
2096         assert( pItem->iCol>0 );
2097         if( pItem->iCol==i ) break;
2098       }
2099       if( j==nOrderBy ){
2100         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
2101         if( pNew==0 ) return SQLITE_NOMEM;
2102         pNew->flags |= EP_IntValue;
2103         pNew->u.iValue = i;
2104         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
2105         pOrderBy->a[nOrderBy++].iCol = (u16)i;
2106       }
2107     }
2108   }
2109 
2110   /* Compute the comparison permutation and keyinfo that is used with
2111   ** the permutation used to determine if the next
2112   ** row of results comes from selectA or selectB.  Also add explicit
2113   ** collations to the ORDER BY clause terms so that when the subqueries
2114   ** to the right and the left are evaluated, they use the correct
2115   ** collation.
2116   */
2117   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
2118   if( aPermute ){
2119     struct ExprList_item *pItem;
2120     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
2121       assert( pItem->iCol>0  && pItem->iCol<=p->pEList->nExpr );
2122       aPermute[i] = pItem->iCol - 1;
2123     }
2124     pKeyMerge =
2125       sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
2126     if( pKeyMerge ){
2127       pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
2128       pKeyMerge->nField = (u16)nOrderBy;
2129       pKeyMerge->enc = ENC(db);
2130       for(i=0; i<nOrderBy; i++){
2131         CollSeq *pColl;
2132         Expr *pTerm = pOrderBy->a[i].pExpr;
2133         if( pTerm->flags & EP_ExpCollate ){
2134           pColl = pTerm->pColl;
2135         }else{
2136           pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
2137           pTerm->flags |= EP_ExpCollate;
2138           pTerm->pColl = pColl;
2139         }
2140         pKeyMerge->aColl[i] = pColl;
2141         pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
2142       }
2143     }
2144   }else{
2145     pKeyMerge = 0;
2146   }
2147 
2148   /* Reattach the ORDER BY clause to the query.
2149   */
2150   p->pOrderBy = pOrderBy;
2151   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
2152 
2153   /* Allocate a range of temporary registers and the KeyInfo needed
2154   ** for the logic that removes duplicate result rows when the
2155   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
2156   */
2157   if( op==TK_ALL ){
2158     regPrev = 0;
2159   }else{
2160     int nExpr = p->pEList->nExpr;
2161     assert( nOrderBy>=nExpr || db->mallocFailed );
2162     regPrev = sqlite3GetTempRange(pParse, nExpr+1);
2163     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
2164     pKeyDup = sqlite3DbMallocZero(db,
2165                   sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
2166     if( pKeyDup ){
2167       pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
2168       pKeyDup->nField = (u16)nExpr;
2169       pKeyDup->enc = ENC(db);
2170       for(i=0; i<nExpr; i++){
2171         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
2172         pKeyDup->aSortOrder[i] = 0;
2173       }
2174     }
2175   }
2176 
2177   /* Separate the left and the right query from one another
2178   */
2179   p->pPrior = 0;
2180   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
2181   if( pPrior->pPrior==0 ){
2182     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
2183   }
2184 
2185   /* Compute the limit registers */
2186   computeLimitRegisters(pParse, p, labelEnd);
2187   if( p->iLimit && op==TK_ALL ){
2188     regLimitA = ++pParse->nMem;
2189     regLimitB = ++pParse->nMem;
2190     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
2191                                   regLimitA);
2192     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
2193   }else{
2194     regLimitA = regLimitB = 0;
2195   }
2196   sqlite3ExprDelete(db, p->pLimit);
2197   p->pLimit = 0;
2198   sqlite3ExprDelete(db, p->pOffset);
2199   p->pOffset = 0;
2200 
2201   regAddrA = ++pParse->nMem;
2202   regEofA = ++pParse->nMem;
2203   regAddrB = ++pParse->nMem;
2204   regEofB = ++pParse->nMem;
2205   regOutA = ++pParse->nMem;
2206   regOutB = ++pParse->nMem;
2207   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
2208   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
2209 
2210   /* Jump past the various subroutines and coroutines to the main
2211   ** merge loop
2212   */
2213   j1 = sqlite3VdbeAddOp0(v, OP_Goto);
2214   addrSelectA = sqlite3VdbeCurrentAddr(v);
2215 
2216 
2217   /* Generate a coroutine to evaluate the SELECT statement to the
2218   ** left of the compound operator - the "A" select.
2219   */
2220   VdbeNoopComment((v, "Begin coroutine for left SELECT"));
2221   pPrior->iLimit = regLimitA;
2222   sqlite3Select(pParse, pPrior, &destA);
2223   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
2224   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
2225   VdbeNoopComment((v, "End coroutine for left SELECT"));
2226 
2227   /* Generate a coroutine to evaluate the SELECT statement on
2228   ** the right - the "B" select
2229   */
2230   addrSelectB = sqlite3VdbeCurrentAddr(v);
2231   VdbeNoopComment((v, "Begin coroutine for right SELECT"));
2232   savedLimit = p->iLimit;
2233   savedOffset = p->iOffset;
2234   p->iLimit = regLimitB;
2235   p->iOffset = 0;
2236   sqlite3Select(pParse, p, &destB);
2237   p->iLimit = savedLimit;
2238   p->iOffset = savedOffset;
2239   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
2240   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
2241   VdbeNoopComment((v, "End coroutine for right SELECT"));
2242 
2243   /* Generate a subroutine that outputs the current row of the A
2244   ** select as the next output row of the compound select.
2245   */
2246   VdbeNoopComment((v, "Output routine for A"));
2247   addrOutA = generateOutputSubroutine(pParse,
2248                  p, &destA, pDest, regOutA,
2249                  regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
2250 
2251   /* Generate a subroutine that outputs the current row of the B
2252   ** select as the next output row of the compound select.
2253   */
2254   if( op==TK_ALL || op==TK_UNION ){
2255     VdbeNoopComment((v, "Output routine for B"));
2256     addrOutB = generateOutputSubroutine(pParse,
2257                  p, &destB, pDest, regOutB,
2258                  regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
2259   }
2260 
2261   /* Generate a subroutine to run when the results from select A
2262   ** are exhausted and only data in select B remains.
2263   */
2264   VdbeNoopComment((v, "eof-A subroutine"));
2265   if( op==TK_EXCEPT || op==TK_INTERSECT ){
2266     addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
2267   }else{
2268     addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
2269     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
2270     sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
2271     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
2272   }
2273 
2274   /* Generate a subroutine to run when the results from select B
2275   ** are exhausted and only data in select A remains.
2276   */
2277   if( op==TK_INTERSECT ){
2278     addrEofB = addrEofA;
2279   }else{
2280     VdbeNoopComment((v, "eof-B subroutine"));
2281     addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
2282     sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
2283     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
2284     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
2285   }
2286 
2287   /* Generate code to handle the case of A<B
2288   */
2289   VdbeNoopComment((v, "A-lt-B subroutine"));
2290   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
2291   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
2292   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
2293   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
2294 
2295   /* Generate code to handle the case of A==B
2296   */
2297   if( op==TK_ALL ){
2298     addrAeqB = addrAltB;
2299   }else if( op==TK_INTERSECT ){
2300     addrAeqB = addrAltB;
2301     addrAltB++;
2302   }else{
2303     VdbeNoopComment((v, "A-eq-B subroutine"));
2304     addrAeqB =
2305     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
2306     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
2307     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
2308   }
2309 
2310   /* Generate code to handle the case of A>B
2311   */
2312   VdbeNoopComment((v, "A-gt-B subroutine"));
2313   addrAgtB = sqlite3VdbeCurrentAddr(v);
2314   if( op==TK_ALL || op==TK_UNION ){
2315     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
2316   }
2317   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
2318   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
2319   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
2320 
2321   /* This code runs once to initialize everything.
2322   */
2323   sqlite3VdbeJumpHere(v, j1);
2324   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
2325   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
2326   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
2327   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
2328   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
2329   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
2330 
2331   /* Implement the main merge loop
2332   */
2333   sqlite3VdbeResolveLabel(v, labelCmpr);
2334   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
2335   sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
2336                          (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
2337   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
2338 
2339   /* Release temporary registers
2340   */
2341   if( regPrev ){
2342     sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
2343   }
2344 
2345   /* Jump to the this point in order to terminate the query.
2346   */
2347   sqlite3VdbeResolveLabel(v, labelEnd);
2348 
2349   /* Set the number of output columns
2350   */
2351   if( pDest->eDest==SRT_Output ){
2352     Select *pFirst = pPrior;
2353     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
2354     generateColumnNames(pParse, 0, pFirst->pEList);
2355   }
2356 
2357   /* Reassembly the compound query so that it will be freed correctly
2358   ** by the calling function */
2359   if( p->pPrior ){
2360     sqlite3SelectDelete(db, p->pPrior);
2361   }
2362   p->pPrior = pPrior;
2363 
2364   /*** TBD:  Insert subroutine calls to close cursors on incomplete
2365   **** subqueries ****/
2366   return SQLITE_OK;
2367 }
2368 #endif
2369 
2370 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
2371 /* Forward Declarations */
2372 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
2373 static void substSelect(sqlite3*, Select *, int, ExprList *);
2374 
2375 /*
2376 ** Scan through the expression pExpr.  Replace every reference to
2377 ** a column in table number iTable with a copy of the iColumn-th
2378 ** entry in pEList.  (But leave references to the ROWID column
2379 ** unchanged.)
2380 **
2381 ** This routine is part of the flattening procedure.  A subquery
2382 ** whose result set is defined by pEList appears as entry in the
2383 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
2384 ** FORM clause entry is iTable.  This routine make the necessary
2385 ** changes to pExpr so that it refers directly to the source table
2386 ** of the subquery rather the result set of the subquery.
2387 */
2388 static Expr *substExpr(
2389   sqlite3 *db,        /* Report malloc errors to this connection */
2390   Expr *pExpr,        /* Expr in which substitution occurs */
2391   int iTable,         /* Table to be substituted */
2392   ExprList *pEList    /* Substitute expressions */
2393 ){
2394   if( pExpr==0 ) return 0;
2395   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
2396     if( pExpr->iColumn<0 ){
2397       pExpr->op = TK_NULL;
2398     }else{
2399       Expr *pNew;
2400       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
2401       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
2402       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
2403       if( pNew && pExpr->pColl ){
2404         pNew->pColl = pExpr->pColl;
2405       }
2406       sqlite3ExprDelete(db, pExpr);
2407       pExpr = pNew;
2408     }
2409   }else{
2410     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
2411     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
2412     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
2413       substSelect(db, pExpr->x.pSelect, iTable, pEList);
2414     }else{
2415       substExprList(db, pExpr->x.pList, iTable, pEList);
2416     }
2417   }
2418   return pExpr;
2419 }
2420 static void substExprList(
2421   sqlite3 *db,         /* Report malloc errors here */
2422   ExprList *pList,     /* List to scan and in which to make substitutes */
2423   int iTable,          /* Table to be substituted */
2424   ExprList *pEList     /* Substitute values */
2425 ){
2426   int i;
2427   if( pList==0 ) return;
2428   for(i=0; i<pList->nExpr; i++){
2429     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
2430   }
2431 }
2432 static void substSelect(
2433   sqlite3 *db,         /* Report malloc errors here */
2434   Select *p,           /* SELECT statement in which to make substitutions */
2435   int iTable,          /* Table to be replaced */
2436   ExprList *pEList     /* Substitute values */
2437 ){
2438   SrcList *pSrc;
2439   struct SrcList_item *pItem;
2440   int i;
2441   if( !p ) return;
2442   substExprList(db, p->pEList, iTable, pEList);
2443   substExprList(db, p->pGroupBy, iTable, pEList);
2444   substExprList(db, p->pOrderBy, iTable, pEList);
2445   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
2446   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
2447   substSelect(db, p->pPrior, iTable, pEList);
2448   pSrc = p->pSrc;
2449   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
2450   if( ALWAYS(pSrc) ){
2451     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
2452       substSelect(db, pItem->pSelect, iTable, pEList);
2453     }
2454   }
2455 }
2456 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
2457 
2458 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
2459 /*
2460 ** This routine attempts to flatten subqueries in order to speed
2461 ** execution.  It returns 1 if it makes changes and 0 if no flattening
2462 ** occurs.
2463 **
2464 ** To understand the concept of flattening, consider the following
2465 ** query:
2466 **
2467 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
2468 **
2469 ** The default way of implementing this query is to execute the
2470 ** subquery first and store the results in a temporary table, then
2471 ** run the outer query on that temporary table.  This requires two
2472 ** passes over the data.  Furthermore, because the temporary table
2473 ** has no indices, the WHERE clause on the outer query cannot be
2474 ** optimized.
2475 **
2476 ** This routine attempts to rewrite queries such as the above into
2477 ** a single flat select, like this:
2478 **
2479 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
2480 **
2481 ** The code generated for this simpification gives the same result
2482 ** but only has to scan the data once.  And because indices might
2483 ** exist on the table t1, a complete scan of the data might be
2484 ** avoided.
2485 **
2486 ** Flattening is only attempted if all of the following are true:
2487 **
2488 **   (1)  The subquery and the outer query do not both use aggregates.
2489 **
2490 **   (2)  The subquery is not an aggregate or the outer query is not a join.
2491 **
2492 **   (3)  The subquery is not the right operand of a left outer join
2493 **        (Originally ticket #306.  Strengthened by ticket #3300)
2494 **
2495 **   (4)  The subquery is not DISTINCT.
2496 **
2497 **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
2498 **        sub-queries that were excluded from this optimization. Restriction
2499 **        (4) has since been expanded to exclude all DISTINCT subqueries.
2500 **
2501 **   (6)  The subquery does not use aggregates or the outer query is not
2502 **        DISTINCT.
2503 **
2504 **   (7)  The subquery has a FROM clause.
2505 **
2506 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
2507 **
2508 **   (9)  The subquery does not use LIMIT or the outer query does not use
2509 **        aggregates.
2510 **
2511 **  (10)  The subquery does not use aggregates or the outer query does not
2512 **        use LIMIT.
2513 **
2514 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
2515 **
2516 **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
2517 **        a separate restriction deriving from ticket #350.
2518 **
2519 **  (13)  The subquery and outer query do not both use LIMIT.
2520 **
2521 **  (14)  The subquery does not use OFFSET.
2522 **
2523 **  (15)  The outer query is not part of a compound select or the
2524 **        subquery does not have a LIMIT clause.
2525 **        (See ticket #2339 and ticket [02a8e81d44]).
2526 **
2527 **  (16)  The outer query is not an aggregate or the subquery does
2528 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
2529 **        until we introduced the group_concat() function.
2530 **
2531 **  (17)  The sub-query is not a compound select, or it is a UNION ALL
2532 **        compound clause made up entirely of non-aggregate queries, and
2533 **        the parent query:
2534 **
2535 **          * is not itself part of a compound select,
2536 **          * is not an aggregate or DISTINCT query, and
2537 **          * has no other tables or sub-selects in the FROM clause.
2538 **
2539 **        The parent and sub-query may contain WHERE clauses. Subject to
2540 **        rules (11), (13) and (14), they may also contain ORDER BY,
2541 **        LIMIT and OFFSET clauses.
2542 **
2543 **  (18)  If the sub-query is a compound select, then all terms of the
2544 **        ORDER by clause of the parent must be simple references to
2545 **        columns of the sub-query.
2546 **
2547 **  (19)  The subquery does not use LIMIT or the outer query does not
2548 **        have a WHERE clause.
2549 **
2550 **  (20)  If the sub-query is a compound select, then it must not use
2551 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
2552 **        somewhat by saying that the terms of the ORDER BY clause must
2553 **        appear as unmodified result columns in the outer query.  But
2554 **        have other optimizations in mind to deal with that case.
2555 **
2556 ** In this routine, the "p" parameter is a pointer to the outer query.
2557 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
2558 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
2559 **
2560 ** If flattening is not attempted, this routine is a no-op and returns 0.
2561 ** If flattening is attempted this routine returns 1.
2562 **
2563 ** All of the expression analysis must occur on both the outer query and
2564 ** the subquery before this routine runs.
2565 */
2566 static int flattenSubquery(
2567   Parse *pParse,       /* Parsing context */
2568   Select *p,           /* The parent or outer SELECT statement */
2569   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
2570   int isAgg,           /* True if outer SELECT uses aggregate functions */
2571   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
2572 ){
2573   const char *zSavedAuthContext = pParse->zAuthContext;
2574   Select *pParent;
2575   Select *pSub;       /* The inner query or "subquery" */
2576   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
2577   SrcList *pSrc;      /* The FROM clause of the outer query */
2578   SrcList *pSubSrc;   /* The FROM clause of the subquery */
2579   ExprList *pList;    /* The result set of the outer query */
2580   int iParent;        /* VDBE cursor number of the pSub result set temp table */
2581   int i;              /* Loop counter */
2582   Expr *pWhere;                    /* The WHERE clause */
2583   struct SrcList_item *pSubitem;   /* The subquery */
2584   sqlite3 *db = pParse->db;
2585 
2586   /* Check to see if flattening is permitted.  Return 0 if not.
2587   */
2588   assert( p!=0 );
2589   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
2590   if( db->flags & SQLITE_QueryFlattener ) return 0;
2591   pSrc = p->pSrc;
2592   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
2593   pSubitem = &pSrc->a[iFrom];
2594   iParent = pSubitem->iCursor;
2595   pSub = pSubitem->pSelect;
2596   assert( pSub!=0 );
2597   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
2598   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
2599   pSubSrc = pSub->pSrc;
2600   assert( pSubSrc );
2601   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
2602   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
2603   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
2604   ** became arbitrary expressions, we were forced to add restrictions (13)
2605   ** and (14). */
2606   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
2607   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
2608   if( p->pRightmost && pSub->pLimit ){
2609     return 0;                                            /* Restriction (15) */
2610   }
2611   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
2612   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
2613   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
2614      return 0;         /* Restrictions (8)(9) */
2615   }
2616   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
2617      return 0;         /* Restriction (6)  */
2618   }
2619   if( p->pOrderBy && pSub->pOrderBy ){
2620      return 0;                                           /* Restriction (11) */
2621   }
2622   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
2623   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
2624 
2625   /* OBSOLETE COMMENT 1:
2626   ** Restriction 3:  If the subquery is a join, make sure the subquery is
2627   ** not used as the right operand of an outer join.  Examples of why this
2628   ** is not allowed:
2629   **
2630   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
2631   **
2632   ** If we flatten the above, we would get
2633   **
2634   **         (t1 LEFT OUTER JOIN t2) JOIN t3
2635   **
2636   ** which is not at all the same thing.
2637   **
2638   ** OBSOLETE COMMENT 2:
2639   ** Restriction 12:  If the subquery is the right operand of a left outer
2640   ** join, make sure the subquery has no WHERE clause.
2641   ** An examples of why this is not allowed:
2642   **
2643   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
2644   **
2645   ** If we flatten the above, we would get
2646   **
2647   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
2648   **
2649   ** But the t2.x>0 test will always fail on a NULL row of t2, which
2650   ** effectively converts the OUTER JOIN into an INNER JOIN.
2651   **
2652   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
2653   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
2654   ** is fraught with danger.  Best to avoid the whole thing.  If the
2655   ** subquery is the right term of a LEFT JOIN, then do not flatten.
2656   */
2657   if( (pSubitem->jointype & JT_OUTER)!=0 ){
2658     return 0;
2659   }
2660 
2661   /* Restriction 17: If the sub-query is a compound SELECT, then it must
2662   ** use only the UNION ALL operator. And none of the simple select queries
2663   ** that make up the compound SELECT are allowed to be aggregate or distinct
2664   ** queries.
2665   */
2666   if( pSub->pPrior ){
2667     if( pSub->pOrderBy ){
2668       return 0;  /* Restriction 20 */
2669     }
2670     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
2671       return 0;
2672     }
2673     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
2674       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
2675       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
2676       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
2677        || (pSub1->pPrior && pSub1->op!=TK_ALL)
2678        || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
2679       ){
2680         return 0;
2681       }
2682     }
2683 
2684     /* Restriction 18. */
2685     if( p->pOrderBy ){
2686       int ii;
2687       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
2688         if( p->pOrderBy->a[ii].iCol==0 ) return 0;
2689       }
2690     }
2691   }
2692 
2693   /***** If we reach this point, flattening is permitted. *****/
2694 
2695   /* Authorize the subquery */
2696   pParse->zAuthContext = pSubitem->zName;
2697   sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
2698   pParse->zAuthContext = zSavedAuthContext;
2699 
2700   /* If the sub-query is a compound SELECT statement, then (by restrictions
2701   ** 17 and 18 above) it must be a UNION ALL and the parent query must
2702   ** be of the form:
2703   **
2704   **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
2705   **
2706   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
2707   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
2708   ** OFFSET clauses and joins them to the left-hand-side of the original
2709   ** using UNION ALL operators. In this case N is the number of simple
2710   ** select statements in the compound sub-query.
2711   **
2712   ** Example:
2713   **
2714   **     SELECT a+1 FROM (
2715   **        SELECT x FROM tab
2716   **        UNION ALL
2717   **        SELECT y FROM tab
2718   **        UNION ALL
2719   **        SELECT abs(z*2) FROM tab2
2720   **     ) WHERE a!=5 ORDER BY 1
2721   **
2722   ** Transformed into:
2723   **
2724   **     SELECT x+1 FROM tab WHERE x+1!=5
2725   **     UNION ALL
2726   **     SELECT y+1 FROM tab WHERE y+1!=5
2727   **     UNION ALL
2728   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
2729   **     ORDER BY 1
2730   **
2731   ** We call this the "compound-subquery flattening".
2732   */
2733   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
2734     Select *pNew;
2735     ExprList *pOrderBy = p->pOrderBy;
2736     Expr *pLimit = p->pLimit;
2737     Select *pPrior = p->pPrior;
2738     p->pOrderBy = 0;
2739     p->pSrc = 0;
2740     p->pPrior = 0;
2741     p->pLimit = 0;
2742     pNew = sqlite3SelectDup(db, p, 0);
2743     p->pLimit = pLimit;
2744     p->pOrderBy = pOrderBy;
2745     p->pSrc = pSrc;
2746     p->op = TK_ALL;
2747     p->pRightmost = 0;
2748     if( pNew==0 ){
2749       pNew = pPrior;
2750     }else{
2751       pNew->pPrior = pPrior;
2752       pNew->pRightmost = 0;
2753     }
2754     p->pPrior = pNew;
2755     if( db->mallocFailed ) return 1;
2756   }
2757 
2758   /* Begin flattening the iFrom-th entry of the FROM clause
2759   ** in the outer query.
2760   */
2761   pSub = pSub1 = pSubitem->pSelect;
2762 
2763   /* Delete the transient table structure associated with the
2764   ** subquery
2765   */
2766   sqlite3DbFree(db, pSubitem->zDatabase);
2767   sqlite3DbFree(db, pSubitem->zName);
2768   sqlite3DbFree(db, pSubitem->zAlias);
2769   pSubitem->zDatabase = 0;
2770   pSubitem->zName = 0;
2771   pSubitem->zAlias = 0;
2772   pSubitem->pSelect = 0;
2773 
2774   /* Defer deleting the Table object associated with the
2775   ** subquery until code generation is
2776   ** complete, since there may still exist Expr.pTab entries that
2777   ** refer to the subquery even after flattening.  Ticket #3346.
2778   **
2779   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
2780   */
2781   if( ALWAYS(pSubitem->pTab!=0) ){
2782     Table *pTabToDel = pSubitem->pTab;
2783     if( pTabToDel->nRef==1 ){
2784       Parse *pToplevel = sqlite3ParseToplevel(pParse);
2785       pTabToDel->pNextZombie = pToplevel->pZombieTab;
2786       pToplevel->pZombieTab = pTabToDel;
2787     }else{
2788       pTabToDel->nRef--;
2789     }
2790     pSubitem->pTab = 0;
2791   }
2792 
2793   /* The following loop runs once for each term in a compound-subquery
2794   ** flattening (as described above).  If we are doing a different kind
2795   ** of flattening - a flattening other than a compound-subquery flattening -
2796   ** then this loop only runs once.
2797   **
2798   ** This loop moves all of the FROM elements of the subquery into the
2799   ** the FROM clause of the outer query.  Before doing this, remember
2800   ** the cursor number for the original outer query FROM element in
2801   ** iParent.  The iParent cursor will never be used.  Subsequent code
2802   ** will scan expressions looking for iParent references and replace
2803   ** those references with expressions that resolve to the subquery FROM
2804   ** elements we are now copying in.
2805   */
2806   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
2807     int nSubSrc;
2808     u8 jointype = 0;
2809     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
2810     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
2811     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
2812 
2813     if( pSrc ){
2814       assert( pParent==p );  /* First time through the loop */
2815       jointype = pSubitem->jointype;
2816     }else{
2817       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
2818       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
2819       if( pSrc==0 ){
2820         assert( db->mallocFailed );
2821         break;
2822       }
2823     }
2824 
2825     /* The subquery uses a single slot of the FROM clause of the outer
2826     ** query.  If the subquery has more than one element in its FROM clause,
2827     ** then expand the outer query to make space for it to hold all elements
2828     ** of the subquery.
2829     **
2830     ** Example:
2831     **
2832     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
2833     **
2834     ** The outer query has 3 slots in its FROM clause.  One slot of the
2835     ** outer query (the middle slot) is used by the subquery.  The next
2836     ** block of code will expand the out query to 4 slots.  The middle
2837     ** slot is expanded to two slots in order to make space for the
2838     ** two elements in the FROM clause of the subquery.
2839     */
2840     if( nSubSrc>1 ){
2841       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
2842       if( db->mallocFailed ){
2843         break;
2844       }
2845     }
2846 
2847     /* Transfer the FROM clause terms from the subquery into the
2848     ** outer query.
2849     */
2850     for(i=0; i<nSubSrc; i++){
2851       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
2852       pSrc->a[i+iFrom] = pSubSrc->a[i];
2853       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
2854     }
2855     pSrc->a[iFrom].jointype = jointype;
2856 
2857     /* Now begin substituting subquery result set expressions for
2858     ** references to the iParent in the outer query.
2859     **
2860     ** Example:
2861     **
2862     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
2863     **   \                     \_____________ subquery __________/          /
2864     **    \_____________________ outer query ______________________________/
2865     **
2866     ** We look at every expression in the outer query and every place we see
2867     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
2868     */
2869     pList = pParent->pEList;
2870     for(i=0; i<pList->nExpr; i++){
2871       if( pList->a[i].zName==0 ){
2872         const char *zSpan = pList->a[i].zSpan;
2873         if( ALWAYS(zSpan) ){
2874           pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
2875         }
2876       }
2877     }
2878     substExprList(db, pParent->pEList, iParent, pSub->pEList);
2879     if( isAgg ){
2880       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
2881       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
2882     }
2883     if( pSub->pOrderBy ){
2884       assert( pParent->pOrderBy==0 );
2885       pParent->pOrderBy = pSub->pOrderBy;
2886       pSub->pOrderBy = 0;
2887     }else if( pParent->pOrderBy ){
2888       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
2889     }
2890     if( pSub->pWhere ){
2891       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
2892     }else{
2893       pWhere = 0;
2894     }
2895     if( subqueryIsAgg ){
2896       assert( pParent->pHaving==0 );
2897       pParent->pHaving = pParent->pWhere;
2898       pParent->pWhere = pWhere;
2899       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
2900       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
2901                                   sqlite3ExprDup(db, pSub->pHaving, 0));
2902       assert( pParent->pGroupBy==0 );
2903       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
2904     }else{
2905       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
2906       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
2907     }
2908 
2909     /* The flattened query is distinct if either the inner or the
2910     ** outer query is distinct.
2911     */
2912     pParent->selFlags |= pSub->selFlags & SF_Distinct;
2913 
2914     /*
2915     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
2916     **
2917     ** One is tempted to try to add a and b to combine the limits.  But this
2918     ** does not work if either limit is negative.
2919     */
2920     if( pSub->pLimit ){
2921       pParent->pLimit = pSub->pLimit;
2922       pSub->pLimit = 0;
2923     }
2924   }
2925 
2926   /* Finially, delete what is left of the subquery and return
2927   ** success.
2928   */
2929   sqlite3SelectDelete(db, pSub1);
2930 
2931   return 1;
2932 }
2933 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
2934 
2935 /*
2936 ** Analyze the SELECT statement passed as an argument to see if it
2937 ** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if
2938 ** it is, or 0 otherwise. At present, a query is considered to be
2939 ** a min()/max() query if:
2940 **
2941 **   1. There is a single object in the FROM clause.
2942 **
2943 **   2. There is a single expression in the result set, and it is
2944 **      either min(x) or max(x), where x is a column reference.
2945 */
2946 static u8 minMaxQuery(Select *p){
2947   Expr *pExpr;
2948   ExprList *pEList = p->pEList;
2949 
2950   if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
2951   pExpr = pEList->a[0].pExpr;
2952   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
2953   if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
2954   pEList = pExpr->x.pList;
2955   if( pEList==0 || pEList->nExpr!=1 ) return 0;
2956   if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
2957   assert( !ExprHasProperty(pExpr, EP_IntValue) );
2958   if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
2959     return WHERE_ORDERBY_MIN;
2960   }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
2961     return WHERE_ORDERBY_MAX;
2962   }
2963   return WHERE_ORDERBY_NORMAL;
2964 }
2965 
2966 /*
2967 ** The select statement passed as the first argument is an aggregate query.
2968 ** The second argment is the associated aggregate-info object. This
2969 ** function tests if the SELECT is of the form:
2970 **
2971 **   SELECT count(*) FROM <tbl>
2972 **
2973 ** where table is a database table, not a sub-select or view. If the query
2974 ** does match this pattern, then a pointer to the Table object representing
2975 ** <tbl> is returned. Otherwise, 0 is returned.
2976 */
2977 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
2978   Table *pTab;
2979   Expr *pExpr;
2980 
2981   assert( !p->pGroupBy );
2982 
2983   if( p->pWhere || p->pEList->nExpr!=1
2984    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
2985   ){
2986     return 0;
2987   }
2988   pTab = p->pSrc->a[0].pTab;
2989   pExpr = p->pEList->a[0].pExpr;
2990   assert( pTab && !pTab->pSelect && pExpr );
2991 
2992   if( IsVirtual(pTab) ) return 0;
2993   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
2994   if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
2995   if( pExpr->flags&EP_Distinct ) return 0;
2996 
2997   return pTab;
2998 }
2999 
3000 /*
3001 ** If the source-list item passed as an argument was augmented with an
3002 ** INDEXED BY clause, then try to locate the specified index. If there
3003 ** was such a clause and the named index cannot be found, return
3004 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
3005 ** pFrom->pIndex and return SQLITE_OK.
3006 */
3007 int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
3008   if( pFrom->pTab && pFrom->zIndex ){
3009     Table *pTab = pFrom->pTab;
3010     char *zIndex = pFrom->zIndex;
3011     Index *pIdx;
3012     for(pIdx=pTab->pIndex;
3013         pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
3014         pIdx=pIdx->pNext
3015     );
3016     if( !pIdx ){
3017       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
3018       pParse->checkSchema = 1;
3019       return SQLITE_ERROR;
3020     }
3021     pFrom->pIndex = pIdx;
3022   }
3023   return SQLITE_OK;
3024 }
3025 
3026 /*
3027 ** This routine is a Walker callback for "expanding" a SELECT statement.
3028 ** "Expanding" means to do the following:
3029 **
3030 **    (1)  Make sure VDBE cursor numbers have been assigned to every
3031 **         element of the FROM clause.
3032 **
3033 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
3034 **         defines FROM clause.  When views appear in the FROM clause,
3035 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
3036 **         that implements the view.  A copy is made of the view's SELECT
3037 **         statement so that we can freely modify or delete that statement
3038 **         without worrying about messing up the presistent representation
3039 **         of the view.
3040 **
3041 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
3042 **         on joins and the ON and USING clause of joins.
3043 **
3044 **    (4)  Scan the list of columns in the result set (pEList) looking
3045 **         for instances of the "*" operator or the TABLE.* operator.
3046 **         If found, expand each "*" to be every column in every table
3047 **         and TABLE.* to be every column in TABLE.
3048 **
3049 */
3050 static int selectExpander(Walker *pWalker, Select *p){
3051   Parse *pParse = pWalker->pParse;
3052   int i, j, k;
3053   SrcList *pTabList;
3054   ExprList *pEList;
3055   struct SrcList_item *pFrom;
3056   sqlite3 *db = pParse->db;
3057 
3058   if( db->mallocFailed  ){
3059     return WRC_Abort;
3060   }
3061   if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
3062     return WRC_Prune;
3063   }
3064   p->selFlags |= SF_Expanded;
3065   pTabList = p->pSrc;
3066   pEList = p->pEList;
3067 
3068   /* Make sure cursor numbers have been assigned to all entries in
3069   ** the FROM clause of the SELECT statement.
3070   */
3071   sqlite3SrcListAssignCursors(pParse, pTabList);
3072 
3073   /* Look up every table named in the FROM clause of the select.  If
3074   ** an entry of the FROM clause is a subquery instead of a table or view,
3075   ** then create a transient table structure to describe the subquery.
3076   */
3077   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
3078     Table *pTab;
3079     if( pFrom->pTab!=0 ){
3080       /* This statement has already been prepared.  There is no need
3081       ** to go further. */
3082       assert( i==0 );
3083       return WRC_Prune;
3084     }
3085     if( pFrom->zName==0 ){
3086 #ifndef SQLITE_OMIT_SUBQUERY
3087       Select *pSel = pFrom->pSelect;
3088       /* A sub-query in the FROM clause of a SELECT */
3089       assert( pSel!=0 );
3090       assert( pFrom->pTab==0 );
3091       sqlite3WalkSelect(pWalker, pSel);
3092       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
3093       if( pTab==0 ) return WRC_Abort;
3094       pTab->nRef = 1;
3095       pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
3096       while( pSel->pPrior ){ pSel = pSel->pPrior; }
3097       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
3098       pTab->iPKey = -1;
3099       pTab->nRowEst = 1000000;
3100       pTab->tabFlags |= TF_Ephemeral;
3101 #endif
3102     }else{
3103       /* An ordinary table or view name in the FROM clause */
3104       assert( pFrom->pTab==0 );
3105       pFrom->pTab = pTab =
3106         sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
3107       if( pTab==0 ) return WRC_Abort;
3108       pTab->nRef++;
3109 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
3110       if( pTab->pSelect || IsVirtual(pTab) ){
3111         /* We reach here if the named table is a really a view */
3112         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
3113         assert( pFrom->pSelect==0 );
3114         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
3115         sqlite3WalkSelect(pWalker, pFrom->pSelect);
3116       }
3117 #endif
3118     }
3119 
3120     /* Locate the index named by the INDEXED BY clause, if any. */
3121     if( sqlite3IndexedByLookup(pParse, pFrom) ){
3122       return WRC_Abort;
3123     }
3124   }
3125 
3126   /* Process NATURAL keywords, and ON and USING clauses of joins.
3127   */
3128   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
3129     return WRC_Abort;
3130   }
3131 
3132   /* For every "*" that occurs in the column list, insert the names of
3133   ** all columns in all tables.  And for every TABLE.* insert the names
3134   ** of all columns in TABLE.  The parser inserted a special expression
3135   ** with the TK_ALL operator for each "*" that it found in the column list.
3136   ** The following code just has to locate the TK_ALL expressions and expand
3137   ** each one to the list of all columns in all tables.
3138   **
3139   ** The first loop just checks to see if there are any "*" operators
3140   ** that need expanding.
3141   */
3142   for(k=0; k<pEList->nExpr; k++){
3143     Expr *pE = pEList->a[k].pExpr;
3144     if( pE->op==TK_ALL ) break;
3145     assert( pE->op!=TK_DOT || pE->pRight!=0 );
3146     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
3147     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
3148   }
3149   if( k<pEList->nExpr ){
3150     /*
3151     ** If we get here it means the result set contains one or more "*"
3152     ** operators that need to be expanded.  Loop through each expression
3153     ** in the result set and expand them one by one.
3154     */
3155     struct ExprList_item *a = pEList->a;
3156     ExprList *pNew = 0;
3157     int flags = pParse->db->flags;
3158     int longNames = (flags & SQLITE_FullColNames)!=0
3159                       && (flags & SQLITE_ShortColNames)==0;
3160 
3161     for(k=0; k<pEList->nExpr; k++){
3162       Expr *pE = a[k].pExpr;
3163       assert( pE->op!=TK_DOT || pE->pRight!=0 );
3164       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
3165         /* This particular expression does not need to be expanded.
3166         */
3167         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
3168         if( pNew ){
3169           pNew->a[pNew->nExpr-1].zName = a[k].zName;
3170           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
3171           a[k].zName = 0;
3172           a[k].zSpan = 0;
3173         }
3174         a[k].pExpr = 0;
3175       }else{
3176         /* This expression is a "*" or a "TABLE.*" and needs to be
3177         ** expanded. */
3178         int tableSeen = 0;      /* Set to 1 when TABLE matches */
3179         char *zTName;            /* text of name of TABLE */
3180         if( pE->op==TK_DOT ){
3181           assert( pE->pLeft!=0 );
3182           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
3183           zTName = pE->pLeft->u.zToken;
3184         }else{
3185           zTName = 0;
3186         }
3187         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
3188           Table *pTab = pFrom->pTab;
3189           char *zTabName = pFrom->zAlias;
3190           if( zTabName==0 ){
3191             zTabName = pTab->zName;
3192           }
3193           if( db->mallocFailed ) break;
3194           if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
3195             continue;
3196           }
3197           tableSeen = 1;
3198           for(j=0; j<pTab->nCol; j++){
3199             Expr *pExpr, *pRight;
3200             char *zName = pTab->aCol[j].zName;
3201             char *zColname;  /* The computed column name */
3202             char *zToFree;   /* Malloced string that needs to be freed */
3203             Token sColname;  /* Computed column name as a token */
3204 
3205             /* If a column is marked as 'hidden' (currently only possible
3206             ** for virtual tables), do not include it in the expanded
3207             ** result-set list.
3208             */
3209             if( IsHiddenColumn(&pTab->aCol[j]) ){
3210               assert(IsVirtual(pTab));
3211               continue;
3212             }
3213 
3214             if( i>0 && zTName==0 ){
3215               if( (pFrom->jointype & JT_NATURAL)!=0
3216                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
3217               ){
3218                 /* In a NATURAL join, omit the join columns from the
3219                 ** table to the right of the join */
3220                 continue;
3221               }
3222               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
3223                 /* In a join with a USING clause, omit columns in the
3224                 ** using clause from the table on the right. */
3225                 continue;
3226               }
3227             }
3228             pRight = sqlite3Expr(db, TK_ID, zName);
3229             zColname = zName;
3230             zToFree = 0;
3231             if( longNames || pTabList->nSrc>1 ){
3232               Expr *pLeft;
3233               pLeft = sqlite3Expr(db, TK_ID, zTabName);
3234               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
3235               if( longNames ){
3236                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
3237                 zToFree = zColname;
3238               }
3239             }else{
3240               pExpr = pRight;
3241             }
3242             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
3243             sColname.z = zColname;
3244             sColname.n = sqlite3Strlen30(zColname);
3245             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
3246             sqlite3DbFree(db, zToFree);
3247           }
3248         }
3249         if( !tableSeen ){
3250           if( zTName ){
3251             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
3252           }else{
3253             sqlite3ErrorMsg(pParse, "no tables specified");
3254           }
3255         }
3256       }
3257     }
3258     sqlite3ExprListDelete(db, pEList);
3259     p->pEList = pNew;
3260   }
3261 #if SQLITE_MAX_COLUMN
3262   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
3263     sqlite3ErrorMsg(pParse, "too many columns in result set");
3264   }
3265 #endif
3266   return WRC_Continue;
3267 }
3268 
3269 /*
3270 ** No-op routine for the parse-tree walker.
3271 **
3272 ** When this routine is the Walker.xExprCallback then expression trees
3273 ** are walked without any actions being taken at each node.  Presumably,
3274 ** when this routine is used for Walker.xExprCallback then
3275 ** Walker.xSelectCallback is set to do something useful for every
3276 ** subquery in the parser tree.
3277 */
3278 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
3279   UNUSED_PARAMETER2(NotUsed, NotUsed2);
3280   return WRC_Continue;
3281 }
3282 
3283 /*
3284 ** This routine "expands" a SELECT statement and all of its subqueries.
3285 ** For additional information on what it means to "expand" a SELECT
3286 ** statement, see the comment on the selectExpand worker callback above.
3287 **
3288 ** Expanding a SELECT statement is the first step in processing a
3289 ** SELECT statement.  The SELECT statement must be expanded before
3290 ** name resolution is performed.
3291 **
3292 ** If anything goes wrong, an error message is written into pParse.
3293 ** The calling function can detect the problem by looking at pParse->nErr
3294 ** and/or pParse->db->mallocFailed.
3295 */
3296 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
3297   Walker w;
3298   w.xSelectCallback = selectExpander;
3299   w.xExprCallback = exprWalkNoop;
3300   w.pParse = pParse;
3301   sqlite3WalkSelect(&w, pSelect);
3302 }
3303 
3304 
3305 #ifndef SQLITE_OMIT_SUBQUERY
3306 /*
3307 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
3308 ** interface.
3309 **
3310 ** For each FROM-clause subquery, add Column.zType and Column.zColl
3311 ** information to the Table structure that represents the result set
3312 ** of that subquery.
3313 **
3314 ** The Table structure that represents the result set was constructed
3315 ** by selectExpander() but the type and collation information was omitted
3316 ** at that point because identifiers had not yet been resolved.  This
3317 ** routine is called after identifier resolution.
3318 */
3319 static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
3320   Parse *pParse;
3321   int i;
3322   SrcList *pTabList;
3323   struct SrcList_item *pFrom;
3324 
3325   assert( p->selFlags & SF_Resolved );
3326   if( (p->selFlags & SF_HasTypeInfo)==0 ){
3327     p->selFlags |= SF_HasTypeInfo;
3328     pParse = pWalker->pParse;
3329     pTabList = p->pSrc;
3330     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
3331       Table *pTab = pFrom->pTab;
3332       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
3333         /* A sub-query in the FROM clause of a SELECT */
3334         Select *pSel = pFrom->pSelect;
3335         assert( pSel );
3336         while( pSel->pPrior ) pSel = pSel->pPrior;
3337         selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
3338       }
3339     }
3340   }
3341   return WRC_Continue;
3342 }
3343 #endif
3344 
3345 
3346 /*
3347 ** This routine adds datatype and collating sequence information to
3348 ** the Table structures of all FROM-clause subqueries in a
3349 ** SELECT statement.
3350 **
3351 ** Use this routine after name resolution.
3352 */
3353 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
3354 #ifndef SQLITE_OMIT_SUBQUERY
3355   Walker w;
3356   w.xSelectCallback = selectAddSubqueryTypeInfo;
3357   w.xExprCallback = exprWalkNoop;
3358   w.pParse = pParse;
3359   sqlite3WalkSelect(&w, pSelect);
3360 #endif
3361 }
3362 
3363 
3364 /*
3365 ** This routine sets of a SELECT statement for processing.  The
3366 ** following is accomplished:
3367 **
3368 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
3369 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
3370 **     *  ON and USING clauses are shifted into WHERE statements
3371 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
3372 **     *  Identifiers in expression are matched to tables.
3373 **
3374 ** This routine acts recursively on all subqueries within the SELECT.
3375 */
3376 void sqlite3SelectPrep(
3377   Parse *pParse,         /* The parser context */
3378   Select *p,             /* The SELECT statement being coded. */
3379   NameContext *pOuterNC  /* Name context for container */
3380 ){
3381   sqlite3 *db;
3382   if( NEVER(p==0) ) return;
3383   db = pParse->db;
3384   if( p->selFlags & SF_HasTypeInfo ) return;
3385   sqlite3SelectExpand(pParse, p);
3386   if( pParse->nErr || db->mallocFailed ) return;
3387   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
3388   if( pParse->nErr || db->mallocFailed ) return;
3389   sqlite3SelectAddTypeInfo(pParse, p);
3390 }
3391 
3392 /*
3393 ** Reset the aggregate accumulator.
3394 **
3395 ** The aggregate accumulator is a set of memory cells that hold
3396 ** intermediate results while calculating an aggregate.  This
3397 ** routine simply stores NULLs in all of those memory cells.
3398 */
3399 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
3400   Vdbe *v = pParse->pVdbe;
3401   int i;
3402   struct AggInfo_func *pFunc;
3403   if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
3404     return;
3405   }
3406   for(i=0; i<pAggInfo->nColumn; i++){
3407     sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
3408   }
3409   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
3410     sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
3411     if( pFunc->iDistinct>=0 ){
3412       Expr *pE = pFunc->pExpr;
3413       assert( !ExprHasProperty(pE, EP_xIsSelect) );
3414       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
3415         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
3416            "argument");
3417         pFunc->iDistinct = -1;
3418       }else{
3419         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
3420         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
3421                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
3422       }
3423     }
3424   }
3425 }
3426 
3427 /*
3428 ** Invoke the OP_AggFinalize opcode for every aggregate function
3429 ** in the AggInfo structure.
3430 */
3431 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
3432   Vdbe *v = pParse->pVdbe;
3433   int i;
3434   struct AggInfo_func *pF;
3435   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
3436     ExprList *pList = pF->pExpr->x.pList;
3437     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
3438     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
3439                       (void*)pF->pFunc, P4_FUNCDEF);
3440   }
3441 }
3442 
3443 /*
3444 ** Update the accumulator memory cells for an aggregate based on
3445 ** the current cursor position.
3446 */
3447 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
3448   Vdbe *v = pParse->pVdbe;
3449   int i;
3450   struct AggInfo_func *pF;
3451   struct AggInfo_col *pC;
3452 
3453   pAggInfo->directMode = 1;
3454   sqlite3ExprCacheClear(pParse);
3455   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
3456     int nArg;
3457     int addrNext = 0;
3458     int regAgg;
3459     ExprList *pList = pF->pExpr->x.pList;
3460     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
3461     if( pList ){
3462       nArg = pList->nExpr;
3463       regAgg = sqlite3GetTempRange(pParse, nArg);
3464       sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
3465     }else{
3466       nArg = 0;
3467       regAgg = 0;
3468     }
3469     if( pF->iDistinct>=0 ){
3470       addrNext = sqlite3VdbeMakeLabel(v);
3471       assert( nArg==1 );
3472       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
3473     }
3474     if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
3475       CollSeq *pColl = 0;
3476       struct ExprList_item *pItem;
3477       int j;
3478       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
3479       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
3480         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
3481       }
3482       if( !pColl ){
3483         pColl = pParse->db->pDfltColl;
3484       }
3485       sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
3486     }
3487     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
3488                       (void*)pF->pFunc, P4_FUNCDEF);
3489     sqlite3VdbeChangeP5(v, (u8)nArg);
3490     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
3491     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
3492     if( addrNext ){
3493       sqlite3VdbeResolveLabel(v, addrNext);
3494       sqlite3ExprCacheClear(pParse);
3495     }
3496   }
3497 
3498   /* Before populating the accumulator registers, clear the column cache.
3499   ** Otherwise, if any of the required column values are already present
3500   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
3501   ** to pC->iMem. But by the time the value is used, the original register
3502   ** may have been used, invalidating the underlying buffer holding the
3503   ** text or blob value. See ticket [883034dcb5].
3504   **
3505   ** Another solution would be to change the OP_SCopy used to copy cached
3506   ** values to an OP_Copy.
3507   */
3508   sqlite3ExprCacheClear(pParse);
3509   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
3510     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
3511   }
3512   pAggInfo->directMode = 0;
3513   sqlite3ExprCacheClear(pParse);
3514 }
3515 
3516 /*
3517 ** Generate code for the SELECT statement given in the p argument.
3518 **
3519 ** The results are distributed in various ways depending on the
3520 ** contents of the SelectDest structure pointed to by argument pDest
3521 ** as follows:
3522 **
3523 **     pDest->eDest    Result
3524 **     ------------    -------------------------------------------
3525 **     SRT_Output      Generate a row of output (using the OP_ResultRow
3526 **                     opcode) for each row in the result set.
3527 **
3528 **     SRT_Mem         Only valid if the result is a single column.
3529 **                     Store the first column of the first result row
3530 **                     in register pDest->iParm then abandon the rest
3531 **                     of the query.  This destination implies "LIMIT 1".
3532 **
3533 **     SRT_Set         The result must be a single column.  Store each
3534 **                     row of result as the key in table pDest->iParm.
3535 **                     Apply the affinity pDest->affinity before storing
3536 **                     results.  Used to implement "IN (SELECT ...)".
3537 **
3538 **     SRT_Union       Store results as a key in a temporary table pDest->iParm.
3539 **
3540 **     SRT_Except      Remove results from the temporary table pDest->iParm.
3541 **
3542 **     SRT_Table       Store results in temporary table pDest->iParm.
3543 **                     This is like SRT_EphemTab except that the table
3544 **                     is assumed to already be open.
3545 **
3546 **     SRT_EphemTab    Create an temporary table pDest->iParm and store
3547 **                     the result there. The cursor is left open after
3548 **                     returning.  This is like SRT_Table except that
3549 **                     this destination uses OP_OpenEphemeral to create
3550 **                     the table first.
3551 **
3552 **     SRT_Coroutine   Generate a co-routine that returns a new row of
3553 **                     results each time it is invoked.  The entry point
3554 **                     of the co-routine is stored in register pDest->iParm.
3555 **
3556 **     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
3557 **                     set is not empty.
3558 **
3559 **     SRT_Discard     Throw the results away.  This is used by SELECT
3560 **                     statements within triggers whose only purpose is
3561 **                     the side-effects of functions.
3562 **
3563 ** This routine returns the number of errors.  If any errors are
3564 ** encountered, then an appropriate error message is left in
3565 ** pParse->zErrMsg.
3566 **
3567 ** This routine does NOT free the Select structure passed in.  The
3568 ** calling function needs to do that.
3569 */
3570 int sqlite3Select(
3571   Parse *pParse,         /* The parser context */
3572   Select *p,             /* The SELECT statement being coded. */
3573   SelectDest *pDest      /* What to do with the query results */
3574 ){
3575   int i, j;              /* Loop counters */
3576   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
3577   Vdbe *v;               /* The virtual machine under construction */
3578   int isAgg;             /* True for select lists like "count(*)" */
3579   ExprList *pEList;      /* List of columns to extract. */
3580   SrcList *pTabList;     /* List of tables to select from */
3581   Expr *pWhere;          /* The WHERE clause.  May be NULL */
3582   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
3583   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
3584   Expr *pHaving;         /* The HAVING clause.  May be NULL */
3585   int isDistinct;        /* True if the DISTINCT keyword is present */
3586   int distinct;          /* Table to use for the distinct set */
3587   int rc = 1;            /* Value to return from this function */
3588   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
3589   AggInfo sAggInfo;      /* Information used by aggregate queries */
3590   int iEnd;              /* Address of the end of the query */
3591   sqlite3 *db;           /* The database connection */
3592 
3593   db = pParse->db;
3594   if( p==0 || db->mallocFailed || pParse->nErr ){
3595     return 1;
3596   }
3597   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
3598   memset(&sAggInfo, 0, sizeof(sAggInfo));
3599 
3600   if( IgnorableOrderby(pDest) ){
3601     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
3602            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
3603     /* If ORDER BY makes no difference in the output then neither does
3604     ** DISTINCT so it can be removed too. */
3605     sqlite3ExprListDelete(db, p->pOrderBy);
3606     p->pOrderBy = 0;
3607     p->selFlags &= ~SF_Distinct;
3608   }
3609   sqlite3SelectPrep(pParse, p, 0);
3610   pOrderBy = p->pOrderBy;
3611   pTabList = p->pSrc;
3612   pEList = p->pEList;
3613   if( pParse->nErr || db->mallocFailed ){
3614     goto select_end;
3615   }
3616   isAgg = (p->selFlags & SF_Aggregate)!=0;
3617   assert( pEList!=0 );
3618 
3619   /* Begin generating code.
3620   */
3621   v = sqlite3GetVdbe(pParse);
3622   if( v==0 ) goto select_end;
3623 
3624   /* If writing to memory or generating a set
3625   ** only a single column may be output.
3626   */
3627 #ifndef SQLITE_OMIT_SUBQUERY
3628   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
3629     goto select_end;
3630   }
3631 #endif
3632 
3633   /* Generate code for all sub-queries in the FROM clause
3634   */
3635 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
3636   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
3637     struct SrcList_item *pItem = &pTabList->a[i];
3638     SelectDest dest;
3639     Select *pSub = pItem->pSelect;
3640     int isAggSub;
3641 
3642     if( pSub==0 || pItem->isPopulated ) continue;
3643 
3644     /* Increment Parse.nHeight by the height of the largest expression
3645     ** tree refered to by this, the parent select. The child select
3646     ** may contain expression trees of at most
3647     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
3648     ** more conservative than necessary, but much easier than enforcing
3649     ** an exact limit.
3650     */
3651     pParse->nHeight += sqlite3SelectExprHeight(p);
3652 
3653     /* Check to see if the subquery can be absorbed into the parent. */
3654     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
3655     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
3656       if( isAggSub ){
3657         isAgg = 1;
3658         p->selFlags |= SF_Aggregate;
3659       }
3660       i = -1;
3661     }else{
3662       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
3663       assert( pItem->isPopulated==0 );
3664       sqlite3Select(pParse, pSub, &dest);
3665       pItem->isPopulated = 1;
3666     }
3667     if( /*pParse->nErr ||*/ db->mallocFailed ){
3668       goto select_end;
3669     }
3670     pParse->nHeight -= sqlite3SelectExprHeight(p);
3671     pTabList = p->pSrc;
3672     if( !IgnorableOrderby(pDest) ){
3673       pOrderBy = p->pOrderBy;
3674     }
3675   }
3676   pEList = p->pEList;
3677 #endif
3678   pWhere = p->pWhere;
3679   pGroupBy = p->pGroupBy;
3680   pHaving = p->pHaving;
3681   isDistinct = (p->selFlags & SF_Distinct)!=0;
3682 
3683 #ifndef SQLITE_OMIT_COMPOUND_SELECT
3684   /* If there is are a sequence of queries, do the earlier ones first.
3685   */
3686   if( p->pPrior ){
3687     if( p->pRightmost==0 ){
3688       Select *pLoop, *pRight = 0;
3689       int cnt = 0;
3690       int mxSelect;
3691       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
3692         pLoop->pRightmost = p;
3693         pLoop->pNext = pRight;
3694         pRight = pLoop;
3695       }
3696       mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
3697       if( mxSelect && cnt>mxSelect ){
3698         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
3699         return 1;
3700       }
3701     }
3702     return multiSelect(pParse, p, pDest);
3703   }
3704 #endif
3705 
3706   /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
3707   ** GROUP BY might use an index, DISTINCT never does.
3708   */
3709   assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 );
3710   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){
3711     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
3712     pGroupBy = p->pGroupBy;
3713     p->selFlags &= ~SF_Distinct;
3714     isDistinct = 0;
3715   }
3716 
3717   /* If there is both a GROUP BY and an ORDER BY clause and they are
3718   ** identical, then disable the ORDER BY clause since the GROUP BY
3719   ** will cause elements to come out in the correct order.  This is
3720   ** an optimization - the correct answer should result regardless.
3721   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
3722   ** to disable this optimization for testing purposes.
3723   */
3724   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
3725          && (db->flags & SQLITE_GroupByOrder)==0 ){
3726     pOrderBy = 0;
3727   }
3728 
3729   /* If there is an ORDER BY clause, then this sorting
3730   ** index might end up being unused if the data can be
3731   ** extracted in pre-sorted order.  If that is the case, then the
3732   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
3733   ** we figure out that the sorting index is not needed.  The addrSortIndex
3734   ** variable is used to facilitate that change.
3735   */
3736   if( pOrderBy ){
3737     KeyInfo *pKeyInfo;
3738     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
3739     pOrderBy->iECursor = pParse->nTab++;
3740     p->addrOpenEphm[2] = addrSortIndex =
3741       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
3742                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
3743                            (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
3744   }else{
3745     addrSortIndex = -1;
3746   }
3747 
3748   /* If the output is destined for a temporary table, open that table.
3749   */
3750   if( pDest->eDest==SRT_EphemTab ){
3751     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
3752   }
3753 
3754   /* Set the limiter.
3755   */
3756   iEnd = sqlite3VdbeMakeLabel(v);
3757   computeLimitRegisters(pParse, p, iEnd);
3758 
3759   /* Open a virtual index to use for the distinct set.
3760   */
3761   if( isDistinct ){
3762     KeyInfo *pKeyInfo;
3763     assert( isAgg || pGroupBy );
3764     distinct = pParse->nTab++;
3765     pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
3766     sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
3767                         (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
3768     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
3769   }else{
3770     distinct = -1;
3771   }
3772 
3773   /* Aggregate and non-aggregate queries are handled differently */
3774   if( !isAgg && pGroupBy==0 ){
3775     /* This case is for non-aggregate queries
3776     ** Begin the database scan
3777     */
3778     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, 0);
3779     if( pWInfo==0 ) goto select_end;
3780 
3781     /* If sorting index that was created by a prior OP_OpenEphemeral
3782     ** instruction ended up not being needed, then change the OP_OpenEphemeral
3783     ** into an OP_Noop.
3784     */
3785     if( addrSortIndex>=0 && pOrderBy==0 ){
3786       sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
3787       p->addrOpenEphm[2] = -1;
3788     }
3789 
3790     /* Use the standard inner loop
3791     */
3792     assert(!isDistinct);
3793     selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, pDest,
3794                     pWInfo->iContinue, pWInfo->iBreak);
3795 
3796     /* End the database scan loop.
3797     */
3798     sqlite3WhereEnd(pWInfo);
3799   }else{
3800     /* This is the processing for aggregate queries */
3801     NameContext sNC;    /* Name context for processing aggregate information */
3802     int iAMem;          /* First Mem address for storing current GROUP BY */
3803     int iBMem;          /* First Mem address for previous GROUP BY */
3804     int iUseFlag;       /* Mem address holding flag indicating that at least
3805                         ** one row of the input to the aggregator has been
3806                         ** processed */
3807     int iAbortFlag;     /* Mem address which causes query abort if positive */
3808     int groupBySort;    /* Rows come from source in GROUP BY order */
3809     int addrEnd;        /* End of processing for this SELECT */
3810 
3811     /* Remove any and all aliases between the result set and the
3812     ** GROUP BY clause.
3813     */
3814     if( pGroupBy ){
3815       int k;                        /* Loop counter */
3816       struct ExprList_item *pItem;  /* For looping over expression in a list */
3817 
3818       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
3819         pItem->iAlias = 0;
3820       }
3821       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
3822         pItem->iAlias = 0;
3823       }
3824     }
3825 
3826 
3827     /* Create a label to jump to when we want to abort the query */
3828     addrEnd = sqlite3VdbeMakeLabel(v);
3829 
3830     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
3831     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
3832     ** SELECT statement.
3833     */
3834     memset(&sNC, 0, sizeof(sNC));
3835     sNC.pParse = pParse;
3836     sNC.pSrcList = pTabList;
3837     sNC.pAggInfo = &sAggInfo;
3838     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
3839     sAggInfo.pGroupBy = pGroupBy;
3840     sqlite3ExprAnalyzeAggList(&sNC, pEList);
3841     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
3842     if( pHaving ){
3843       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
3844     }
3845     sAggInfo.nAccumulator = sAggInfo.nColumn;
3846     for(i=0; i<sAggInfo.nFunc; i++){
3847       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
3848       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
3849     }
3850     if( db->mallocFailed ) goto select_end;
3851 
3852     /* Processing for aggregates with GROUP BY is very different and
3853     ** much more complex than aggregates without a GROUP BY.
3854     */
3855     if( pGroupBy ){
3856       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
3857       int j1;             /* A-vs-B comparision jump */
3858       int addrOutputRow;  /* Start of subroutine that outputs a result row */
3859       int regOutputRow;   /* Return address register for output subroutine */
3860       int addrSetAbort;   /* Set the abort flag and return */
3861       int addrTopOfLoop;  /* Top of the input loop */
3862       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
3863       int addrReset;      /* Subroutine for resetting the accumulator */
3864       int regReset;       /* Return address register for reset subroutine */
3865 
3866       /* If there is a GROUP BY clause we might need a sorting index to
3867       ** implement it.  Allocate that sorting index now.  If it turns out
3868       ** that we do not need it after all, the OpenEphemeral instruction
3869       ** will be converted into a Noop.
3870       */
3871       sAggInfo.sortingIdx = pParse->nTab++;
3872       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
3873       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
3874           sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
3875           0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
3876 
3877       /* Initialize memory locations used by GROUP BY aggregate processing
3878       */
3879       iUseFlag = ++pParse->nMem;
3880       iAbortFlag = ++pParse->nMem;
3881       regOutputRow = ++pParse->nMem;
3882       addrOutputRow = sqlite3VdbeMakeLabel(v);
3883       regReset = ++pParse->nMem;
3884       addrReset = sqlite3VdbeMakeLabel(v);
3885       iAMem = pParse->nMem + 1;
3886       pParse->nMem += pGroupBy->nExpr;
3887       iBMem = pParse->nMem + 1;
3888       pParse->nMem += pGroupBy->nExpr;
3889       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
3890       VdbeComment((v, "clear abort flag"));
3891       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
3892       VdbeComment((v, "indicate accumulator empty"));
3893 
3894       /* Begin a loop that will extract all source rows in GROUP BY order.
3895       ** This might involve two separate loops with an OP_Sort in between, or
3896       ** it might be a single loop that uses an index to extract information
3897       ** in the right order to begin with.
3898       */
3899       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
3900       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0);
3901       if( pWInfo==0 ) goto select_end;
3902       if( pGroupBy==0 ){
3903         /* The optimizer is able to deliver rows in group by order so
3904         ** we do not have to sort.  The OP_OpenEphemeral table will be
3905         ** cancelled later because we still need to use the pKeyInfo
3906         */
3907         pGroupBy = p->pGroupBy;
3908         groupBySort = 0;
3909       }else{
3910         /* Rows are coming out in undetermined order.  We have to push
3911         ** each row into a sorting index, terminate the first loop,
3912         ** then loop over the sorting index in order to get the output
3913         ** in sorted order
3914         */
3915         int regBase;
3916         int regRecord;
3917         int nCol;
3918         int nGroupBy;
3919 
3920         groupBySort = 1;
3921         nGroupBy = pGroupBy->nExpr;
3922         nCol = nGroupBy + 1;
3923         j = nGroupBy+1;
3924         for(i=0; i<sAggInfo.nColumn; i++){
3925           if( sAggInfo.aCol[i].iSorterColumn>=j ){
3926             nCol++;
3927             j++;
3928           }
3929         }
3930         regBase = sqlite3GetTempRange(pParse, nCol);
3931         sqlite3ExprCacheClear(pParse);
3932         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
3933         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
3934         j = nGroupBy+1;
3935         for(i=0; i<sAggInfo.nColumn; i++){
3936           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
3937           if( pCol->iSorterColumn>=j ){
3938             int r1 = j + regBase;
3939             int r2;
3940 
3941             r2 = sqlite3ExprCodeGetColumn(pParse,
3942                                pCol->pTab, pCol->iColumn, pCol->iTable, r1);
3943             if( r1!=r2 ){
3944               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
3945             }
3946             j++;
3947           }
3948         }
3949         regRecord = sqlite3GetTempReg(pParse);
3950         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
3951         sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
3952         sqlite3ReleaseTempReg(pParse, regRecord);
3953         sqlite3ReleaseTempRange(pParse, regBase, nCol);
3954         sqlite3WhereEnd(pWInfo);
3955         sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
3956         VdbeComment((v, "GROUP BY sort"));
3957         sAggInfo.useSortingIdx = 1;
3958         sqlite3ExprCacheClear(pParse);
3959       }
3960 
3961       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
3962       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
3963       ** Then compare the current GROUP BY terms against the GROUP BY terms
3964       ** from the previous row currently stored in a0, a1, a2...
3965       */
3966       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
3967       sqlite3ExprCacheClear(pParse);
3968       for(j=0; j<pGroupBy->nExpr; j++){
3969         if( groupBySort ){
3970           sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
3971         }else{
3972           sAggInfo.directMode = 1;
3973           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
3974         }
3975       }
3976       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
3977                           (char*)pKeyInfo, P4_KEYINFO);
3978       j1 = sqlite3VdbeCurrentAddr(v);
3979       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
3980 
3981       /* Generate code that runs whenever the GROUP BY changes.
3982       ** Changes in the GROUP BY are detected by the previous code
3983       ** block.  If there were no changes, this block is skipped.
3984       **
3985       ** This code copies current group by terms in b0,b1,b2,...
3986       ** over to a0,a1,a2.  It then calls the output subroutine
3987       ** and resets the aggregate accumulator registers in preparation
3988       ** for the next GROUP BY batch.
3989       */
3990       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
3991       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
3992       VdbeComment((v, "output one row"));
3993       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
3994       VdbeComment((v, "check abort flag"));
3995       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
3996       VdbeComment((v, "reset accumulator"));
3997 
3998       /* Update the aggregate accumulators based on the content of
3999       ** the current row
4000       */
4001       sqlite3VdbeJumpHere(v, j1);
4002       updateAccumulator(pParse, &sAggInfo);
4003       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
4004       VdbeComment((v, "indicate data in accumulator"));
4005 
4006       /* End of the loop
4007       */
4008       if( groupBySort ){
4009         sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
4010       }else{
4011         sqlite3WhereEnd(pWInfo);
4012         sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
4013       }
4014 
4015       /* Output the final row of result
4016       */
4017       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
4018       VdbeComment((v, "output final row"));
4019 
4020       /* Jump over the subroutines
4021       */
4022       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
4023 
4024       /* Generate a subroutine that outputs a single row of the result
4025       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
4026       ** is less than or equal to zero, the subroutine is a no-op.  If
4027       ** the processing calls for the query to abort, this subroutine
4028       ** increments the iAbortFlag memory location before returning in
4029       ** order to signal the caller to abort.
4030       */
4031       addrSetAbort = sqlite3VdbeCurrentAddr(v);
4032       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
4033       VdbeComment((v, "set abort flag"));
4034       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
4035       sqlite3VdbeResolveLabel(v, addrOutputRow);
4036       addrOutputRow = sqlite3VdbeCurrentAddr(v);
4037       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
4038       VdbeComment((v, "Groupby result generator entry point"));
4039       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
4040       finalizeAggFunctions(pParse, &sAggInfo);
4041       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
4042       selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
4043                       distinct, pDest,
4044                       addrOutputRow+1, addrSetAbort);
4045       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
4046       VdbeComment((v, "end groupby result generator"));
4047 
4048       /* Generate a subroutine that will reset the group-by accumulator
4049       */
4050       sqlite3VdbeResolveLabel(v, addrReset);
4051       resetAccumulator(pParse, &sAggInfo);
4052       sqlite3VdbeAddOp1(v, OP_Return, regReset);
4053 
4054     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
4055     else {
4056       ExprList *pDel = 0;
4057 #ifndef SQLITE_OMIT_BTREECOUNT
4058       Table *pTab;
4059       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
4060         /* If isSimpleCount() returns a pointer to a Table structure, then
4061         ** the SQL statement is of the form:
4062         **
4063         **   SELECT count(*) FROM <tbl>
4064         **
4065         ** where the Table structure returned represents table <tbl>.
4066         **
4067         ** This statement is so common that it is optimized specially. The
4068         ** OP_Count instruction is executed either on the intkey table that
4069         ** contains the data for table <tbl> or on one of its indexes. It
4070         ** is better to execute the op on an index, as indexes are almost
4071         ** always spread across less pages than their corresponding tables.
4072         */
4073         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
4074         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
4075         Index *pIdx;                         /* Iterator variable */
4076         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
4077         Index *pBest = 0;                    /* Best index found so far */
4078         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
4079 
4080         sqlite3CodeVerifySchema(pParse, iDb);
4081         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
4082 
4083         /* Search for the index that has the least amount of columns. If
4084         ** there is such an index, and it has less columns than the table
4085         ** does, then we can assume that it consumes less space on disk and
4086         ** will therefore be cheaper to scan to determine the query result.
4087         ** In this case set iRoot to the root page number of the index b-tree
4088         ** and pKeyInfo to the KeyInfo structure required to navigate the
4089         ** index.
4090         **
4091         ** In practice the KeyInfo structure will not be used. It is only
4092         ** passed to keep OP_OpenRead happy.
4093         */
4094         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
4095           if( !pBest || pIdx->nColumn<pBest->nColumn ){
4096             pBest = pIdx;
4097           }
4098         }
4099         if( pBest && pBest->nColumn<pTab->nCol ){
4100           iRoot = pBest->tnum;
4101           pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
4102         }
4103 
4104         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
4105         sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
4106         if( pKeyInfo ){
4107           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
4108         }
4109         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
4110         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
4111       }else
4112 #endif /* SQLITE_OMIT_BTREECOUNT */
4113       {
4114         /* Check if the query is of one of the following forms:
4115         **
4116         **   SELECT min(x) FROM ...
4117         **   SELECT max(x) FROM ...
4118         **
4119         ** If it is, then ask the code in where.c to attempt to sort results
4120         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
4121         ** If where.c is able to produce results sorted in this order, then
4122         ** add vdbe code to break out of the processing loop after the
4123         ** first iteration (since the first iteration of the loop is
4124         ** guaranteed to operate on the row with the minimum or maximum
4125         ** value of x, the only row required).
4126         **
4127         ** A special flag must be passed to sqlite3WhereBegin() to slightly
4128         ** modify behaviour as follows:
4129         **
4130         **   + If the query is a "SELECT min(x)", then the loop coded by
4131         **     where.c should not iterate over any values with a NULL value
4132         **     for x.
4133         **
4134         **   + The optimizer code in where.c (the thing that decides which
4135         **     index or indices to use) should place a different priority on
4136         **     satisfying the 'ORDER BY' clause than it does in other cases.
4137         **     Refer to code and comments in where.c for details.
4138         */
4139         ExprList *pMinMax = 0;
4140         u8 flag = minMaxQuery(p);
4141         if( flag ){
4142           assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
4143           pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
4144           pDel = pMinMax;
4145           if( pMinMax && !db->mallocFailed ){
4146             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
4147             pMinMax->a[0].pExpr->op = TK_COLUMN;
4148           }
4149         }
4150 
4151         /* This case runs if the aggregate has no GROUP BY clause.  The
4152         ** processing is much simpler since there is only a single row
4153         ** of output.
4154         */
4155         resetAccumulator(pParse, &sAggInfo);
4156         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, flag);
4157         if( pWInfo==0 ){
4158           sqlite3ExprListDelete(db, pDel);
4159           goto select_end;
4160         }
4161         updateAccumulator(pParse, &sAggInfo);
4162         if( !pMinMax && flag ){
4163           sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
4164           VdbeComment((v, "%s() by index",
4165                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
4166         }
4167         sqlite3WhereEnd(pWInfo);
4168         finalizeAggFunctions(pParse, &sAggInfo);
4169       }
4170 
4171       pOrderBy = 0;
4172       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
4173       selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
4174                       pDest, addrEnd, addrEnd);
4175       sqlite3ExprListDelete(db, pDel);
4176     }
4177     sqlite3VdbeResolveLabel(v, addrEnd);
4178 
4179   } /* endif aggregate query */
4180 
4181   /* If there is an ORDER BY clause, then we need to sort the results
4182   ** and send them to the callback one by one.
4183   */
4184   if( pOrderBy ){
4185     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
4186   }
4187 
4188   /* Jump here to skip this query
4189   */
4190   sqlite3VdbeResolveLabel(v, iEnd);
4191 
4192   /* The SELECT was successfully coded.   Set the return code to 0
4193   ** to indicate no errors.
4194   */
4195   rc = 0;
4196 
4197   /* Control jumps to here if an error is encountered above, or upon
4198   ** successful coding of the SELECT.
4199   */
4200 select_end:
4201 
4202   /* Identify column names if results of the SELECT are to be output.
4203   */
4204   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
4205     generateColumnNames(pParse, pTabList, pEList);
4206   }
4207 
4208   sqlite3DbFree(db, sAggInfo.aCol);
4209   sqlite3DbFree(db, sAggInfo.aFunc);
4210   return rc;
4211 }
4212 
4213 #if defined(SQLITE_DEBUG)
4214 /*
4215 *******************************************************************************
4216 ** The following code is used for testing and debugging only.  The code
4217 ** that follows does not appear in normal builds.
4218 **
4219 ** These routines are used to print out the content of all or part of a
4220 ** parse structures such as Select or Expr.  Such printouts are useful
4221 ** for helping to understand what is happening inside the code generator
4222 ** during the execution of complex SELECT statements.
4223 **
4224 ** These routine are not called anywhere from within the normal
4225 ** code base.  Then are intended to be called from within the debugger
4226 ** or from temporary "printf" statements inserted for debugging.
4227 */
4228 void sqlite3PrintExpr(Expr *p){
4229   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
4230     sqlite3DebugPrintf("(%s", p->u.zToken);
4231   }else{
4232     sqlite3DebugPrintf("(%d", p->op);
4233   }
4234   if( p->pLeft ){
4235     sqlite3DebugPrintf(" ");
4236     sqlite3PrintExpr(p->pLeft);
4237   }
4238   if( p->pRight ){
4239     sqlite3DebugPrintf(" ");
4240     sqlite3PrintExpr(p->pRight);
4241   }
4242   sqlite3DebugPrintf(")");
4243 }
4244 void sqlite3PrintExprList(ExprList *pList){
4245   int i;
4246   for(i=0; i<pList->nExpr; i++){
4247     sqlite3PrintExpr(pList->a[i].pExpr);
4248     if( i<pList->nExpr-1 ){
4249       sqlite3DebugPrintf(", ");
4250     }
4251   }
4252 }
4253 void sqlite3PrintSelect(Select *p, int indent){
4254   sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
4255   sqlite3PrintExprList(p->pEList);
4256   sqlite3DebugPrintf("\n");
4257   if( p->pSrc ){
4258     char *zPrefix;
4259     int i;
4260     zPrefix = "FROM";
4261     for(i=0; i<p->pSrc->nSrc; i++){
4262       struct SrcList_item *pItem = &p->pSrc->a[i];
4263       sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
4264       zPrefix = "";
4265       if( pItem->pSelect ){
4266         sqlite3DebugPrintf("(\n");
4267         sqlite3PrintSelect(pItem->pSelect, indent+10);
4268         sqlite3DebugPrintf("%*s)", indent+8, "");
4269       }else if( pItem->zName ){
4270         sqlite3DebugPrintf("%s", pItem->zName);
4271       }
4272       if( pItem->pTab ){
4273         sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
4274       }
4275       if( pItem->zAlias ){
4276         sqlite3DebugPrintf(" AS %s", pItem->zAlias);
4277       }
4278       if( i<p->pSrc->nSrc-1 ){
4279         sqlite3DebugPrintf(",");
4280       }
4281       sqlite3DebugPrintf("\n");
4282     }
4283   }
4284   if( p->pWhere ){
4285     sqlite3DebugPrintf("%*s WHERE ", indent, "");
4286     sqlite3PrintExpr(p->pWhere);
4287     sqlite3DebugPrintf("\n");
4288   }
4289   if( p->pGroupBy ){
4290     sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
4291     sqlite3PrintExprList(p->pGroupBy);
4292     sqlite3DebugPrintf("\n");
4293   }
4294   if( p->pHaving ){
4295     sqlite3DebugPrintf("%*s HAVING ", indent, "");
4296     sqlite3PrintExpr(p->pHaving);
4297     sqlite3DebugPrintf("\n");
4298   }
4299   if( p->pOrderBy ){
4300     sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
4301     sqlite3PrintExprList(p->pOrderBy);
4302     sqlite3DebugPrintf("\n");
4303   }
4304 }
4305 /* End of the structure debug printing code
4306 *****************************************************************************/
4307 #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
4308