xref: /sqlite-3.40.0/src/select.c (revision 85b623f2)
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 ** $Id: select.c,v 1.370 2007/12/13 21:54:11 drh Exp $
16 */
17 #include "sqliteInt.h"
18 
19 
20 /*
21 ** Delete all the content of a Select structure but do not deallocate
22 ** the select structure itself.
23 */
24 static void clearSelect(Select *p){
25   sqlite3ExprListDelete(p->pEList);
26   sqlite3SrcListDelete(p->pSrc);
27   sqlite3ExprDelete(p->pWhere);
28   sqlite3ExprListDelete(p->pGroupBy);
29   sqlite3ExprDelete(p->pHaving);
30   sqlite3ExprListDelete(p->pOrderBy);
31   sqlite3SelectDelete(p->pPrior);
32   sqlite3ExprDelete(p->pLimit);
33   sqlite3ExprDelete(p->pOffset);
34 }
35 
36 
37 /*
38 ** Allocate a new Select structure and return a pointer to that
39 ** structure.
40 */
41 Select *sqlite3SelectNew(
42   Parse *pParse,        /* Parsing context */
43   ExprList *pEList,     /* which columns to include in the result */
44   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
45   Expr *pWhere,         /* the WHERE clause */
46   ExprList *pGroupBy,   /* the GROUP BY clause */
47   Expr *pHaving,        /* the HAVING clause */
48   ExprList *pOrderBy,   /* the ORDER BY clause */
49   int isDistinct,       /* true if the DISTINCT keyword is present */
50   Expr *pLimit,         /* LIMIT value.  NULL means not used */
51   Expr *pOffset         /* OFFSET value.  NULL means no offset */
52 ){
53   Select *pNew;
54   Select standin;
55   sqlite3 *db = pParse->db;
56   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
57   assert( !pOffset || pLimit );   /* Can't have OFFSET without LIMIT. */
58   if( pNew==0 ){
59     pNew = &standin;
60     memset(pNew, 0, sizeof(*pNew));
61   }
62   if( pEList==0 ){
63     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0,0,0), 0);
64   }
65   pNew->pEList = pEList;
66   pNew->pSrc = pSrc;
67   pNew->pWhere = pWhere;
68   pNew->pGroupBy = pGroupBy;
69   pNew->pHaving = pHaving;
70   pNew->pOrderBy = pOrderBy;
71   pNew->isDistinct = isDistinct;
72   pNew->op = TK_SELECT;
73   assert( pOffset==0 || pLimit!=0 );
74   pNew->pLimit = pLimit;
75   pNew->pOffset = pOffset;
76   pNew->iLimit = -1;
77   pNew->iOffset = -1;
78   pNew->addrOpenEphm[0] = -1;
79   pNew->addrOpenEphm[1] = -1;
80   pNew->addrOpenEphm[2] = -1;
81   if( pNew==&standin) {
82     clearSelect(pNew);
83     pNew = 0;
84   }
85   return pNew;
86 }
87 
88 /*
89 ** Delete the given Select structure and all of its substructures.
90 */
91 void sqlite3SelectDelete(Select *p){
92   if( p ){
93     clearSelect(p);
94     sqlite3_free(p);
95   }
96 }
97 
98 /*
99 ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
100 ** type of join.  Return an integer constant that expresses that type
101 ** in terms of the following bit values:
102 **
103 **     JT_INNER
104 **     JT_CROSS
105 **     JT_OUTER
106 **     JT_NATURAL
107 **     JT_LEFT
108 **     JT_RIGHT
109 **
110 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
111 **
112 ** If an illegal or unsupported join type is seen, then still return
113 ** a join type, but put an error in the pParse structure.
114 */
115 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
116   int jointype = 0;
117   Token *apAll[3];
118   Token *p;
119   static const struct {
120     const char zKeyword[8];
121     u8 nChar;
122     u8 code;
123   } keywords[] = {
124     { "natural", 7, JT_NATURAL },
125     { "left",    4, JT_LEFT|JT_OUTER },
126     { "right",   5, JT_RIGHT|JT_OUTER },
127     { "full",    4, JT_LEFT|JT_RIGHT|JT_OUTER },
128     { "outer",   5, JT_OUTER },
129     { "inner",   5, JT_INNER },
130     { "cross",   5, JT_INNER|JT_CROSS },
131   };
132   int i, j;
133   apAll[0] = pA;
134   apAll[1] = pB;
135   apAll[2] = pC;
136   for(i=0; i<3 && apAll[i]; i++){
137     p = apAll[i];
138     for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
139       if( p->n==keywords[j].nChar
140           && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){
141         jointype |= keywords[j].code;
142         break;
143       }
144     }
145     if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
146       jointype |= JT_ERROR;
147       break;
148     }
149   }
150   if(
151      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
152      (jointype & JT_ERROR)!=0
153   ){
154     const char *zSp1 = " ";
155     const char *zSp2 = " ";
156     if( pB==0 ){ zSp1++; }
157     if( pC==0 ){ zSp2++; }
158     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
159        "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
160     jointype = JT_INNER;
161   }else if( jointype & JT_RIGHT ){
162     sqlite3ErrorMsg(pParse,
163       "RIGHT and FULL OUTER JOINs are not currently supported");
164     jointype = JT_INNER;
165   }
166   return jointype;
167 }
168 
169 /*
170 ** Return the index of a column in a table.  Return -1 if the column
171 ** is not contained in the table.
172 */
173 static int columnIndex(Table *pTab, const char *zCol){
174   int i;
175   for(i=0; i<pTab->nCol; i++){
176     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
177   }
178   return -1;
179 }
180 
181 /*
182 ** Set the value of a token to a '\000'-terminated string.
183 */
184 static void setToken(Token *p, const char *z){
185   p->z = (u8*)z;
186   p->n = z ? strlen(z) : 0;
187   p->dyn = 0;
188 }
189 
190 /*
191 ** Set the token to the double-quoted and escaped version of the string pointed
192 ** to by z. For example;
193 **
194 **    {a"bc}  ->  {"a""bc"}
195 */
196 static void setQuotedToken(Parse *pParse, Token *p, const char *z){
197   p->z = (u8 *)sqlite3MPrintf(0, "\"%w\"", z);
198   p->dyn = 1;
199   if( p->z ){
200     p->n = strlen((char *)p->z);
201   }else{
202     pParse->db->mallocFailed = 1;
203   }
204 }
205 
206 /*
207 ** Create an expression node for an identifier with the name of zName
208 */
209 Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){
210   Token dummy;
211   setToken(&dummy, zName);
212   return sqlite3PExpr(pParse, TK_ID, 0, 0, &dummy);
213 }
214 
215 
216 /*
217 ** Add a term to the WHERE expression in *ppExpr that requires the
218 ** zCol column to be equal in the two tables pTab1 and pTab2.
219 */
220 static void addWhereTerm(
221   Parse *pParse,           /* Parsing context */
222   const char *zCol,        /* Name of the column */
223   const Table *pTab1,      /* First table */
224   const char *zAlias1,     /* Alias for first table.  May be NULL */
225   const Table *pTab2,      /* Second table */
226   const char *zAlias2,     /* Alias for second table.  May be NULL */
227   int iRightJoinTable,     /* VDBE cursor for the right table */
228   Expr **ppExpr            /* Add the equality term to this expression */
229 ){
230   Expr *pE1a, *pE1b, *pE1c;
231   Expr *pE2a, *pE2b, *pE2c;
232   Expr *pE;
233 
234   pE1a = sqlite3CreateIdExpr(pParse, zCol);
235   pE2a = sqlite3CreateIdExpr(pParse, zCol);
236   if( zAlias1==0 ){
237     zAlias1 = pTab1->zName;
238   }
239   pE1b = sqlite3CreateIdExpr(pParse, zAlias1);
240   if( zAlias2==0 ){
241     zAlias2 = pTab2->zName;
242   }
243   pE2b = sqlite3CreateIdExpr(pParse, zAlias2);
244   pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0);
245   pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0);
246   pE = sqlite3PExpr(pParse, TK_EQ, pE1c, pE2c, 0);
247   if( pE ){
248     ExprSetProperty(pE, EP_FromJoin);
249     pE->iRightJoinTable = iRightJoinTable;
250   }
251   *ppExpr = sqlite3ExprAnd(pParse->db,*ppExpr, pE);
252 }
253 
254 /*
255 ** Set the EP_FromJoin property on all terms of the given expression.
256 ** And set the Expr.iRightJoinTable to iTable for every term in the
257 ** expression.
258 **
259 ** The EP_FromJoin property is used on terms of an expression to tell
260 ** the LEFT OUTER JOIN processing logic that this term is part of the
261 ** join restriction specified in the ON or USING clause and not a part
262 ** of the more general WHERE clause.  These terms are moved over to the
263 ** WHERE clause during join processing but we need to remember that they
264 ** originated in the ON or USING clause.
265 **
266 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
267 ** expression depends on table iRightJoinTable even if that table is not
268 ** explicitly mentioned in the expression.  That information is needed
269 ** for cases like this:
270 **
271 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
272 **
273 ** The where clause needs to defer the handling of the t1.x=5
274 ** term until after the t2 loop of the join.  In that way, a
275 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
276 ** defer the handling of t1.x=5, it will be processed immediately
277 ** after the t1 loop and rows with t1.x!=5 will never appear in
278 ** the output, which is incorrect.
279 */
280 static void setJoinExpr(Expr *p, int iTable){
281   while( p ){
282     ExprSetProperty(p, EP_FromJoin);
283     p->iRightJoinTable = iTable;
284     setJoinExpr(p->pLeft, iTable);
285     p = p->pRight;
286   }
287 }
288 
289 /*
290 ** This routine processes the join information for a SELECT statement.
291 ** ON and USING clauses are converted into extra terms of the WHERE clause.
292 ** NATURAL joins also create extra WHERE clause terms.
293 **
294 ** The terms of a FROM clause are contained in the Select.pSrc structure.
295 ** The left most table is the first entry in Select.pSrc.  The right-most
296 ** table is the last entry.  The join operator is held in the entry to
297 ** the left.  Thus entry 0 contains the join operator for the join between
298 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
299 ** also attached to the left entry.
300 **
301 ** This routine returns the number of errors encountered.
302 */
303 static int sqliteProcessJoin(Parse *pParse, Select *p){
304   SrcList *pSrc;                  /* All tables in the FROM clause */
305   int i, j;                       /* Loop counters */
306   struct SrcList_item *pLeft;     /* Left table being joined */
307   struct SrcList_item *pRight;    /* Right table being joined */
308 
309   pSrc = p->pSrc;
310   pLeft = &pSrc->a[0];
311   pRight = &pLeft[1];
312   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
313     Table *pLeftTab = pLeft->pTab;
314     Table *pRightTab = pRight->pTab;
315 
316     if( pLeftTab==0 || pRightTab==0 ) continue;
317 
318     /* When the NATURAL keyword is present, add WHERE clause terms for
319     ** every column that the two tables have in common.
320     */
321     if( pRight->jointype & JT_NATURAL ){
322       if( pRight->pOn || pRight->pUsing ){
323         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
324            "an ON or USING clause", 0);
325         return 1;
326       }
327       for(j=0; j<pLeftTab->nCol; j++){
328         char *zName = pLeftTab->aCol[j].zName;
329         if( columnIndex(pRightTab, zName)>=0 ){
330           addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias,
331                               pRightTab, pRight->zAlias,
332                               pRight->iCursor, &p->pWhere);
333 
334         }
335       }
336     }
337 
338     /* Disallow both ON and USING clauses in the same join
339     */
340     if( pRight->pOn && pRight->pUsing ){
341       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
342         "clauses in the same join");
343       return 1;
344     }
345 
346     /* Add the ON clause to the end of the WHERE clause, connected by
347     ** an AND operator.
348     */
349     if( pRight->pOn ){
350       setJoinExpr(pRight->pOn, pRight->iCursor);
351       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
352       pRight->pOn = 0;
353     }
354 
355     /* Create extra terms on the WHERE clause for each column named
356     ** in the USING clause.  Example: If the two tables to be joined are
357     ** A and B and the USING clause names X, Y, and Z, then add this
358     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
359     ** Report an error if any column mentioned in the USING clause is
360     ** not contained in both tables to be joined.
361     */
362     if( pRight->pUsing ){
363       IdList *pList = pRight->pUsing;
364       for(j=0; j<pList->nId; j++){
365         char *zName = pList->a[j].zName;
366         if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){
367           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
368             "not present in both tables", zName);
369           return 1;
370         }
371         addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias,
372                             pRightTab, pRight->zAlias,
373                             pRight->iCursor, &p->pWhere);
374       }
375     }
376   }
377   return 0;
378 }
379 
380 /*
381 ** Insert code into "v" that will push the record on the top of the
382 ** stack into the sorter.
383 */
384 static void pushOntoSorter(
385   Parse *pParse,         /* Parser context */
386   ExprList *pOrderBy,    /* The ORDER BY clause */
387   Select *pSelect        /* The whole SELECT statement */
388 ){
389   Vdbe *v = pParse->pVdbe;
390   sqlite3ExprCodeExprList(pParse, pOrderBy);
391   sqlite3VdbeAddOp(v, OP_Sequence, pOrderBy->iECursor, 0);
392   sqlite3VdbeAddOp(v, OP_Pull, pOrderBy->nExpr + 1, 0);
393   sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr + 2, 0);
394   sqlite3VdbeAddOp(v, OP_IdxInsert, pOrderBy->iECursor, 0);
395   if( pSelect->iLimit>=0 ){
396     int addr1, addr2;
397     addr1 = sqlite3VdbeAddOp(v, OP_IfMemZero, pSelect->iLimit+1, 0);
398     sqlite3VdbeAddOp(v, OP_MemIncr, -1, pSelect->iLimit+1);
399     addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
400     sqlite3VdbeJumpHere(v, addr1);
401     sqlite3VdbeAddOp(v, OP_Last, pOrderBy->iECursor, 0);
402     sqlite3VdbeAddOp(v, OP_Delete, pOrderBy->iECursor, 0);
403     sqlite3VdbeJumpHere(v, addr2);
404     pSelect->iLimit = -1;
405   }
406 }
407 
408 /*
409 ** Add code to implement the OFFSET
410 */
411 static void codeOffset(
412   Vdbe *v,          /* Generate code into this VM */
413   Select *p,        /* The SELECT statement being coded */
414   int iContinue,    /* Jump here to skip the current record */
415   int nPop          /* Number of times to pop stack when jumping */
416 ){
417   if( p->iOffset>=0 && iContinue!=0 ){
418     int addr;
419     sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iOffset);
420     addr = sqlite3VdbeAddOp(v, OP_IfMemNeg, p->iOffset, 0);
421     if( nPop>0 ){
422       sqlite3VdbeAddOp(v, OP_Pop, nPop, 0);
423     }
424     sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
425     VdbeComment((v, "# skip OFFSET records"));
426     sqlite3VdbeJumpHere(v, addr);
427   }
428 }
429 
430 /*
431 ** Add code that will check to make sure the top N elements of the
432 ** stack are distinct.  iTab is a sorting index that holds previously
433 ** seen combinations of the N values.  A new entry is made in iTab
434 ** if the current N values are new.
435 **
436 ** A jump to addrRepeat is made and the N+1 values are popped from the
437 ** stack if the top N elements are not distinct.
438 */
439 static void codeDistinct(
440   Vdbe *v,           /* Generate code into this VM */
441   int iTab,          /* A sorting index used to test for distinctness */
442   int addrRepeat,    /* Jump to here if not distinct */
443   int N              /* The top N elements of the stack must be distinct */
444 ){
445   sqlite3VdbeAddOp(v, OP_MakeRecord, -N, 0);
446   sqlite3VdbeAddOp(v, OP_Distinct, iTab, sqlite3VdbeCurrentAddr(v)+3);
447   sqlite3VdbeAddOp(v, OP_Pop, N+1, 0);
448   sqlite3VdbeAddOp(v, OP_Goto, 0, addrRepeat);
449   VdbeComment((v, "# skip indistinct records"));
450   sqlite3VdbeAddOp(v, OP_IdxInsert, iTab, 0);
451 }
452 
453 /*
454 ** Generate an error message when a SELECT is used within a subexpression
455 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
456 ** column.  We do this in a subroutine because the error occurs in multiple
457 ** places.
458 */
459 static int checkForMultiColumnSelectError(Parse *pParse, int eDest, int nExpr){
460   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
461     sqlite3ErrorMsg(pParse, "only a single result allowed for "
462        "a SELECT that is part of an expression");
463     return 1;
464   }else{
465     return 0;
466   }
467 }
468 
469 /*
470 ** This routine generates the code for the inside of the inner loop
471 ** of a SELECT.
472 **
473 ** If srcTab and nColumn are both zero, then the pEList expressions
474 ** are evaluated in order to get the data for this row.  If nColumn>0
475 ** then data is pulled from srcTab and pEList is used only to get the
476 ** datatypes for each column.
477 */
478 static int selectInnerLoop(
479   Parse *pParse,          /* The parser context */
480   Select *p,              /* The complete select statement being coded */
481   ExprList *pEList,       /* List of values being extracted */
482   int srcTab,             /* Pull data from this table */
483   int nColumn,            /* Number of columns in the source table */
484   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
485   int distinct,           /* If >=0, make sure results are distinct */
486   int eDest,              /* How to dispose of the results */
487   int iParm,              /* An argument to the disposal method */
488   int iContinue,          /* Jump here to continue with next row */
489   int iBreak,             /* Jump here to break out of the inner loop */
490   char *aff               /* affinity string if eDest is SRT_Union */
491 ){
492   Vdbe *v = pParse->pVdbe;
493   int i;
494   int hasDistinct;        /* True if the DISTINCT keyword is present */
495 
496   if( v==0 ) return 0;
497   assert( pEList!=0 );
498 
499   /* If there was a LIMIT clause on the SELECT statement, then do the check
500   ** to see if this row should be output.
501   */
502   hasDistinct = distinct>=0 && pEList->nExpr>0;
503   if( pOrderBy==0 && !hasDistinct ){
504     codeOffset(v, p, iContinue, 0);
505   }
506 
507   /* Pull the requested columns.
508   */
509   if( nColumn>0 ){
510     for(i=0; i<nColumn; i++){
511       sqlite3VdbeAddOp(v, OP_Column, srcTab, i);
512     }
513   }else{
514     nColumn = pEList->nExpr;
515     sqlite3ExprCodeExprList(pParse, pEList);
516   }
517 
518   /* If the DISTINCT keyword was present on the SELECT statement
519   ** and this row has been seen before, then do not make this row
520   ** part of the result.
521   */
522   if( hasDistinct ){
523     assert( pEList!=0 );
524     assert( pEList->nExpr==nColumn );
525     codeDistinct(v, distinct, iContinue, nColumn);
526     if( pOrderBy==0 ){
527       codeOffset(v, p, iContinue, nColumn);
528     }
529   }
530 
531   if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){
532     return 0;
533   }
534 
535   switch( eDest ){
536     /* In this mode, write each query result to the key of the temporary
537     ** table iParm.
538     */
539 #ifndef SQLITE_OMIT_COMPOUND_SELECT
540     case SRT_Union: {
541       sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
542       if( aff ){
543         sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
544       }
545       sqlite3VdbeAddOp(v, OP_IdxInsert, iParm, 0);
546       break;
547     }
548 
549     /* Construct a record from the query result, but instead of
550     ** saving that record, use it as a key to delete elements from
551     ** the temporary table iParm.
552     */
553     case SRT_Except: {
554       int addr;
555       addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
556       sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
557       sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3);
558       sqlite3VdbeAddOp(v, OP_Delete, iParm, 0);
559       break;
560     }
561 #endif
562 
563     /* Store the result as data using a unique key.
564     */
565     case SRT_Table:
566     case SRT_EphemTab: {
567       sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
568       if( pOrderBy ){
569         pushOntoSorter(pParse, pOrderBy, p);
570       }else{
571         sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0);
572         sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
573         sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND);
574       }
575       break;
576     }
577 
578 #ifndef SQLITE_OMIT_SUBQUERY
579     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
580     ** then there should be a single item on the stack.  Write this
581     ** item into the set table with bogus data.
582     */
583     case SRT_Set: {
584       int addr1 = sqlite3VdbeCurrentAddr(v);
585       int addr2;
586 
587       assert( nColumn==1 );
588       sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3);
589       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
590       addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
591       p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr,(iParm>>16)&0xff);
592       if( pOrderBy ){
593         /* At first glance you would think we could optimize out the
594         ** ORDER BY in this case since the order of entries in the set
595         ** does not matter.  But there might be a LIMIT clause, in which
596         ** case the order does matter */
597         pushOntoSorter(pParse, pOrderBy, p);
598       }else{
599         sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1);
600         sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
601       }
602       sqlite3VdbeJumpHere(v, addr2);
603       break;
604     }
605 
606     /* If any row exist in the result set, record that fact and abort.
607     */
608     case SRT_Exists: {
609       sqlite3VdbeAddOp(v, OP_MemInt, 1, iParm);
610       sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
611       /* The LIMIT clause will terminate the loop for us */
612       break;
613     }
614 
615     /* If this is a scalar select that is part of an expression, then
616     ** store the results in the appropriate memory cell and break out
617     ** of the scan loop.
618     */
619     case SRT_Mem: {
620       assert( nColumn==1 );
621       if( pOrderBy ){
622         pushOntoSorter(pParse, pOrderBy, p);
623       }else{
624         sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
625         /* The LIMIT clause will jump out of the loop for us */
626       }
627       break;
628     }
629 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
630 
631     /* Send the data to the callback function or to a subroutine.  In the
632     ** case of a subroutine, the subroutine itself is responsible for
633     ** popping the data from the stack.
634     */
635     case SRT_Subroutine:
636     case SRT_Callback: {
637       if( pOrderBy ){
638         sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
639         pushOntoSorter(pParse, pOrderBy, p);
640       }else if( eDest==SRT_Subroutine ){
641         sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
642       }else{
643         sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
644       }
645       break;
646     }
647 
648 #if !defined(SQLITE_OMIT_TRIGGER)
649     /* Discard the results.  This is used for SELECT statements inside
650     ** the body of a TRIGGER.  The purpose of such selects is to call
651     ** user-defined functions that have side effects.  We do not care
652     ** about the actual results of the select.
653     */
654     default: {
655       assert( eDest==SRT_Discard );
656       sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
657       break;
658     }
659 #endif
660   }
661 
662   /* Jump to the end of the loop if the LIMIT is reached.
663   */
664   if( p->iLimit>=0 && pOrderBy==0 ){
665     sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit);
666     sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, iBreak);
667   }
668   return 0;
669 }
670 
671 /*
672 ** Given an expression list, generate a KeyInfo structure that records
673 ** the collating sequence for each expression in that expression list.
674 **
675 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
676 ** KeyInfo structure is appropriate for initializing a virtual index to
677 ** implement that clause.  If the ExprList is the result set of a SELECT
678 ** then the KeyInfo structure is appropriate for initializing a virtual
679 ** index to implement a DISTINCT test.
680 **
681 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
682 ** function is responsible for seeing that this structure is eventually
683 ** freed.  Add the KeyInfo structure to the P3 field of an opcode using
684 ** P3_KEYINFO_HANDOFF is the usual way of dealing with this.
685 */
686 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
687   sqlite3 *db = pParse->db;
688   int nExpr;
689   KeyInfo *pInfo;
690   struct ExprList_item *pItem;
691   int i;
692 
693   nExpr = pList->nExpr;
694   pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
695   if( pInfo ){
696     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
697     pInfo->nField = nExpr;
698     pInfo->enc = ENC(db);
699     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
700       CollSeq *pColl;
701       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
702       if( !pColl ){
703         pColl = db->pDfltColl;
704       }
705       pInfo->aColl[i] = pColl;
706       pInfo->aSortOrder[i] = pItem->sortOrder;
707     }
708   }
709   return pInfo;
710 }
711 
712 
713 /*
714 ** If the inner loop was generated using a non-null pOrderBy argument,
715 ** then the results were placed in a sorter.  After the loop is terminated
716 ** we need to run the sorter and output the results.  The following
717 ** routine generates the code needed to do that.
718 */
719 static void generateSortTail(
720   Parse *pParse,   /* Parsing context */
721   Select *p,       /* The SELECT statement */
722   Vdbe *v,         /* Generate code into this VDBE */
723   int nColumn,     /* Number of columns of data */
724   int eDest,       /* Write the sorted results here */
725   int iParm        /* Optional parameter associated with eDest */
726 ){
727   int brk = sqlite3VdbeMakeLabel(v);
728   int cont = sqlite3VdbeMakeLabel(v);
729   int addr;
730   int iTab;
731   int pseudoTab = 0;
732   ExprList *pOrderBy = p->pOrderBy;
733 
734   iTab = pOrderBy->iECursor;
735   if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
736     pseudoTab = pParse->nTab++;
737     sqlite3VdbeAddOp(v, OP_OpenPseudo, pseudoTab, 0);
738     sqlite3VdbeAddOp(v, OP_SetNumColumns, pseudoTab, nColumn);
739   }
740   addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk);
741   codeOffset(v, p, cont, 0);
742   if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
743     sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
744   }
745   sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1);
746   switch( eDest ){
747     case SRT_Table:
748     case SRT_EphemTab: {
749       sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0);
750       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
751       sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND);
752       break;
753     }
754 #ifndef SQLITE_OMIT_SUBQUERY
755     case SRT_Set: {
756       assert( nColumn==1 );
757       sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
758       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
759       sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
760       sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1);
761       sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
762       break;
763     }
764     case SRT_Mem: {
765       assert( nColumn==1 );
766       sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
767       /* The LIMIT clause will terminate the loop for us */
768       break;
769     }
770 #endif
771     case SRT_Callback:
772     case SRT_Subroutine: {
773       int i;
774       sqlite3VdbeAddOp(v, OP_Insert, pseudoTab, 0);
775       for(i=0; i<nColumn; i++){
776         sqlite3VdbeAddOp(v, OP_Column, pseudoTab, i);
777       }
778       if( eDest==SRT_Callback ){
779         sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
780       }else{
781         sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
782       }
783       break;
784     }
785     default: {
786       /* Do nothing */
787       break;
788     }
789   }
790 
791   /* Jump to the end of the loop when the LIMIT is reached
792   */
793   if( p->iLimit>=0 ){
794     sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit);
795     sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, brk);
796   }
797 
798   /* The bottom of the loop
799   */
800   sqlite3VdbeResolveLabel(v, cont);
801   sqlite3VdbeAddOp(v, OP_Next, iTab, addr);
802   sqlite3VdbeResolveLabel(v, brk);
803   if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
804     sqlite3VdbeAddOp(v, OP_Close, pseudoTab, 0);
805   }
806 
807 }
808 
809 /*
810 ** Return a pointer to a string containing the 'declaration type' of the
811 ** expression pExpr. The string may be treated as static by the caller.
812 **
813 ** The declaration type is the exact datatype definition extracted from the
814 ** original CREATE TABLE statement if the expression is a column. The
815 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
816 ** is considered a column can be complex in the presence of subqueries. The
817 ** result-set expression in all of the following SELECT statements is
818 ** considered a column by this function.
819 **
820 **   SELECT col FROM tbl;
821 **   SELECT (SELECT col FROM tbl;
822 **   SELECT (SELECT col FROM tbl);
823 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
824 **
825 ** The declaration type for any expression other than a column is NULL.
826 */
827 static const char *columnType(
828   NameContext *pNC,
829   Expr *pExpr,
830   const char **pzOriginDb,
831   const char **pzOriginTab,
832   const char **pzOriginCol
833 ){
834   char const *zType = 0;
835   char const *zOriginDb = 0;
836   char const *zOriginTab = 0;
837   char const *zOriginCol = 0;
838   int j;
839   if( pExpr==0 || pNC->pSrcList==0 ) return 0;
840 
841   switch( pExpr->op ){
842     case TK_AGG_COLUMN:
843     case TK_COLUMN: {
844       /* The expression is a column. Locate the table the column is being
845       ** extracted from in NameContext.pSrcList. This table may be real
846       ** database table or a subquery.
847       */
848       Table *pTab = 0;            /* Table structure column is extracted from */
849       Select *pS = 0;             /* Select the column is extracted from */
850       int iCol = pExpr->iColumn;  /* Index of column in pTab */
851       while( pNC && !pTab ){
852         SrcList *pTabList = pNC->pSrcList;
853         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
854         if( j<pTabList->nSrc ){
855           pTab = pTabList->a[j].pTab;
856           pS = pTabList->a[j].pSelect;
857         }else{
858           pNC = pNC->pNext;
859         }
860       }
861 
862       if( pTab==0 ){
863         /* FIX ME:
864         ** This can occurs if you have something like "SELECT new.x;" inside
865         ** a trigger.  In other words, if you reference the special "new"
866         ** table in the result set of a select.  We do not have a good way
867         ** to find the actual table type, so call it "TEXT".  This is really
868         ** something of a bug, but I do not know how to fix it.
869         **
870         ** This code does not produce the correct answer - it just prevents
871         ** a segfault.  See ticket #1229.
872         */
873         zType = "TEXT";
874         break;
875       }
876 
877       assert( pTab );
878       if( pS ){
879         /* The "table" is actually a sub-select or a view in the FROM clause
880         ** of the SELECT statement. Return the declaration type and origin
881         ** data for the result-set column of the sub-select.
882         */
883         if( iCol>=0 && iCol<pS->pEList->nExpr ){
884           /* If iCol is less than zero, then the expression requests the
885           ** rowid of the sub-select or view. This expression is legal (see
886           ** test case misc2.2.2) - it always evaluates to NULL.
887           */
888           NameContext sNC;
889           Expr *p = pS->pEList->a[iCol].pExpr;
890           sNC.pSrcList = pS->pSrc;
891           sNC.pNext = 0;
892           sNC.pParse = pNC->pParse;
893           zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
894         }
895       }else if( pTab->pSchema ){
896         /* A real table */
897         assert( !pS );
898         if( iCol<0 ) iCol = pTab->iPKey;
899         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
900         if( iCol<0 ){
901           zType = "INTEGER";
902           zOriginCol = "rowid";
903         }else{
904           zType = pTab->aCol[iCol].zType;
905           zOriginCol = pTab->aCol[iCol].zName;
906         }
907         zOriginTab = pTab->zName;
908         if( pNC->pParse ){
909           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
910           zOriginDb = pNC->pParse->db->aDb[iDb].zName;
911         }
912       }
913       break;
914     }
915 #ifndef SQLITE_OMIT_SUBQUERY
916     case TK_SELECT: {
917       /* The expression is a sub-select. Return the declaration type and
918       ** origin info for the single column in the result set of the SELECT
919       ** statement.
920       */
921       NameContext sNC;
922       Select *pS = pExpr->pSelect;
923       Expr *p = pS->pEList->a[0].pExpr;
924       sNC.pSrcList = pS->pSrc;
925       sNC.pNext = pNC;
926       sNC.pParse = pNC->pParse;
927       zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
928       break;
929     }
930 #endif
931   }
932 
933   if( pzOriginDb ){
934     assert( pzOriginTab && pzOriginCol );
935     *pzOriginDb = zOriginDb;
936     *pzOriginTab = zOriginTab;
937     *pzOriginCol = zOriginCol;
938   }
939   return zType;
940 }
941 
942 /*
943 ** Generate code that will tell the VDBE the declaration types of columns
944 ** in the result set.
945 */
946 static void generateColumnTypes(
947   Parse *pParse,      /* Parser context */
948   SrcList *pTabList,  /* List of tables */
949   ExprList *pEList    /* Expressions defining the result set */
950 ){
951   Vdbe *v = pParse->pVdbe;
952   int i;
953   NameContext sNC;
954   sNC.pSrcList = pTabList;
955   sNC.pParse = pParse;
956   for(i=0; i<pEList->nExpr; i++){
957     Expr *p = pEList->a[i].pExpr;
958     const char *zOrigDb = 0;
959     const char *zOrigTab = 0;
960     const char *zOrigCol = 0;
961     const char *zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
962 
963     /* The vdbe must make its own copy of the column-type and other
964     ** column specific strings, in case the schema is reset before this
965     ** virtual machine is deleted.
966     */
967     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P3_TRANSIENT);
968     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P3_TRANSIENT);
969     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P3_TRANSIENT);
970     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P3_TRANSIENT);
971   }
972 }
973 
974 /*
975 ** Generate code that will tell the VDBE the names of columns
976 ** in the result set.  This information is used to provide the
977 ** azCol[] values in the callback.
978 */
979 static void generateColumnNames(
980   Parse *pParse,      /* Parser context */
981   SrcList *pTabList,  /* List of tables */
982   ExprList *pEList    /* Expressions defining the result set */
983 ){
984   Vdbe *v = pParse->pVdbe;
985   int i, j;
986   sqlite3 *db = pParse->db;
987   int fullNames, shortNames;
988 
989 #ifndef SQLITE_OMIT_EXPLAIN
990   /* If this is an EXPLAIN, skip this step */
991   if( pParse->explain ){
992     return;
993   }
994 #endif
995 
996   assert( v!=0 );
997   if( pParse->colNamesSet || v==0 || db->mallocFailed ) return;
998   pParse->colNamesSet = 1;
999   fullNames = (db->flags & SQLITE_FullColNames)!=0;
1000   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
1001   sqlite3VdbeSetNumCols(v, pEList->nExpr);
1002   for(i=0; i<pEList->nExpr; i++){
1003     Expr *p;
1004     p = pEList->a[i].pExpr;
1005     if( p==0 ) continue;
1006     if( pEList->a[i].zName ){
1007       char *zName = pEList->a[i].zName;
1008       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, strlen(zName));
1009       continue;
1010     }
1011     if( p->op==TK_COLUMN && pTabList ){
1012       Table *pTab;
1013       char *zCol;
1014       int iCol = p->iColumn;
1015       for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
1016       assert( j<pTabList->nSrc );
1017       pTab = pTabList->a[j].pTab;
1018       if( iCol<0 ) iCol = pTab->iPKey;
1019       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
1020       if( iCol<0 ){
1021         zCol = "rowid";
1022       }else{
1023         zCol = pTab->aCol[iCol].zName;
1024       }
1025       if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){
1026         sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n);
1027       }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){
1028         char *zName = 0;
1029         char *zTab;
1030 
1031         zTab = pTabList->a[j].zAlias;
1032         if( fullNames || zTab==0 ) zTab = pTab->zName;
1033         sqlite3SetString(&zName, zTab, ".", zCol, (char*)0);
1034         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P3_DYNAMIC);
1035       }else{
1036         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol));
1037       }
1038     }else if( p->span.z && p->span.z[0] ){
1039       sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n);
1040       /* sqlite3VdbeCompressSpace(v, addr); */
1041     }else{
1042       char zName[30];
1043       assert( p->op!=TK_COLUMN || pTabList==0 );
1044       sqlite3_snprintf(sizeof(zName), zName, "column%d", i+1);
1045       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, 0);
1046     }
1047   }
1048   generateColumnTypes(pParse, pTabList, pEList);
1049 }
1050 
1051 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1052 /*
1053 ** Name of the connection operator, used for error messages.
1054 */
1055 static const char *selectOpName(int id){
1056   char *z;
1057   switch( id ){
1058     case TK_ALL:       z = "UNION ALL";   break;
1059     case TK_INTERSECT: z = "INTERSECT";   break;
1060     case TK_EXCEPT:    z = "EXCEPT";      break;
1061     default:           z = "UNION";       break;
1062   }
1063   return z;
1064 }
1065 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1066 
1067 /*
1068 ** Forward declaration
1069 */
1070 static int prepSelectStmt(Parse*, Select*);
1071 
1072 /*
1073 ** Given a SELECT statement, generate a Table structure that describes
1074 ** the result set of that SELECT.
1075 */
1076 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
1077   Table *pTab;
1078   int i, j;
1079   ExprList *pEList;
1080   Column *aCol, *pCol;
1081   sqlite3 *db = pParse->db;
1082 
1083   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
1084   if( prepSelectStmt(pParse, pSelect) ){
1085     return 0;
1086   }
1087   if( sqlite3SelectResolve(pParse, pSelect, 0) ){
1088     return 0;
1089   }
1090   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
1091   if( pTab==0 ){
1092     return 0;
1093   }
1094   pTab->nRef = 1;
1095   pTab->zName = zTabName ? sqlite3DbStrDup(db, zTabName) : 0;
1096   pEList = pSelect->pEList;
1097   pTab->nCol = pEList->nExpr;
1098   assert( pTab->nCol>0 );
1099   pTab->aCol = aCol = sqlite3DbMallocZero(db, sizeof(pTab->aCol[0])*pTab->nCol);
1100   for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){
1101     Expr *p, *pR;
1102     char *zType;
1103     char *zName;
1104     int nName;
1105     CollSeq *pColl;
1106     int cnt;
1107     NameContext sNC;
1108 
1109     /* Get an appropriate name for the column
1110     */
1111     p = pEList->a[i].pExpr;
1112     assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
1113     if( (zName = pEList->a[i].zName)!=0 ){
1114       /* If the column contains an "AS <name>" phrase, use <name> as the name */
1115       zName = sqlite3DbStrDup(db, zName);
1116     }else if( p->op==TK_DOT
1117               && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
1118       /* For columns of the from A.B use B as the name */
1119       zName = sqlite3MPrintf(db, "%T", &pR->token);
1120     }else if( p->span.z && p->span.z[0] ){
1121       /* Use the original text of the column expression as its name */
1122       zName = sqlite3MPrintf(db, "%T", &p->span);
1123     }else{
1124       /* If all else fails, make up a name */
1125       zName = sqlite3MPrintf(db, "column%d", i+1);
1126     }
1127     if( !zName || db->mallocFailed ){
1128       db->mallocFailed = 1;
1129       sqlite3_free(zName);
1130       sqlite3DeleteTable(pTab);
1131       return 0;
1132     }
1133     sqlite3Dequote(zName);
1134 
1135     /* Make sure the column name is unique.  If the name is not unique,
1136     ** append a integer to the name so that it becomes unique.
1137     */
1138     nName = strlen(zName);
1139     for(j=cnt=0; j<i; j++){
1140       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
1141         zName[nName] = 0;
1142         zName = sqlite3MPrintf(db, "%z:%d", zName, ++cnt);
1143         j = -1;
1144         if( zName==0 ) break;
1145       }
1146     }
1147     pCol->zName = zName;
1148 
1149     /* Get the typename, type affinity, and collating sequence for the
1150     ** column.
1151     */
1152     memset(&sNC, 0, sizeof(sNC));
1153     sNC.pSrcList = pSelect->pSrc;
1154     zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
1155     pCol->zType = zType;
1156     pCol->affinity = sqlite3ExprAffinity(p);
1157     pColl = sqlite3ExprCollSeq(pParse, p);
1158     if( pColl ){
1159       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
1160     }
1161   }
1162   pTab->iPKey = -1;
1163   return pTab;
1164 }
1165 
1166 /*
1167 ** Prepare a SELECT statement for processing by doing the following
1168 ** things:
1169 **
1170 **    (1)  Make sure VDBE cursor numbers have been assigned to every
1171 **         element of the FROM clause.
1172 **
1173 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
1174 **         defines FROM clause.  When views appear in the FROM clause,
1175 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
1176 **         that implements the view.  A copy is made of the view's SELECT
1177 **         statement so that we can freely modify or delete that statement
1178 **         without worrying about messing up the presistent representation
1179 **         of the view.
1180 **
1181 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
1182 **         on joins and the ON and USING clause of joins.
1183 **
1184 **    (4)  Scan the list of columns in the result set (pEList) looking
1185 **         for instances of the "*" operator or the TABLE.* operator.
1186 **         If found, expand each "*" to be every column in every table
1187 **         and TABLE.* to be every column in TABLE.
1188 **
1189 ** Return 0 on success.  If there are problems, leave an error message
1190 ** in pParse and return non-zero.
1191 */
1192 static int prepSelectStmt(Parse *pParse, Select *p){
1193   int i, j, k, rc;
1194   SrcList *pTabList;
1195   ExprList *pEList;
1196   struct SrcList_item *pFrom;
1197   sqlite3 *db = pParse->db;
1198 
1199   if( p==0 || p->pSrc==0 || db->mallocFailed ){
1200     return 1;
1201   }
1202   pTabList = p->pSrc;
1203   pEList = p->pEList;
1204 
1205   /* Make sure cursor numbers have been assigned to all entries in
1206   ** the FROM clause of the SELECT statement.
1207   */
1208   sqlite3SrcListAssignCursors(pParse, p->pSrc);
1209 
1210   /* Look up every table named in the FROM clause of the select.  If
1211   ** an entry of the FROM clause is a subquery instead of a table or view,
1212   ** then create a transient table structure to describe the subquery.
1213   */
1214   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
1215     Table *pTab;
1216     if( pFrom->pTab!=0 ){
1217       /* This statement has already been prepared.  There is no need
1218       ** to go further. */
1219       assert( i==0 );
1220       return 0;
1221     }
1222     if( pFrom->zName==0 ){
1223 #ifndef SQLITE_OMIT_SUBQUERY
1224       /* A sub-query in the FROM clause of a SELECT */
1225       assert( pFrom->pSelect!=0 );
1226       if( pFrom->zAlias==0 ){
1227         pFrom->zAlias =
1228           sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pFrom->pSelect);
1229       }
1230       assert( pFrom->pTab==0 );
1231       pFrom->pTab = pTab =
1232         sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect);
1233       if( pTab==0 ){
1234         return 1;
1235       }
1236       /* The isEphem flag indicates that the Table structure has been
1237       ** dynamically allocated and may be freed at any time.  In other words,
1238       ** pTab is not pointing to a persistent table structure that defines
1239       ** part of the schema. */
1240       pTab->isEphem = 1;
1241 #endif
1242     }else{
1243       /* An ordinary table or view name in the FROM clause */
1244       assert( pFrom->pTab==0 );
1245       pFrom->pTab = pTab =
1246         sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase);
1247       if( pTab==0 ){
1248         return 1;
1249       }
1250       pTab->nRef++;
1251 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
1252       if( pTab->pSelect || IsVirtual(pTab) ){
1253         /* We reach here if the named table is a really a view */
1254         if( sqlite3ViewGetColumnNames(pParse, pTab) ){
1255           return 1;
1256         }
1257         /* If pFrom->pSelect!=0 it means we are dealing with a
1258         ** view within a view.  The SELECT structure has already been
1259         ** copied by the outer view so we can skip the copy step here
1260         ** in the inner view.
1261         */
1262         if( pFrom->pSelect==0 ){
1263           pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect);
1264         }
1265       }
1266 #endif
1267     }
1268   }
1269 
1270   /* Process NATURAL keywords, and ON and USING clauses of joins.
1271   */
1272   if( sqliteProcessJoin(pParse, p) ) return 1;
1273 
1274   /* For every "*" that occurs in the column list, insert the names of
1275   ** all columns in all tables.  And for every TABLE.* insert the names
1276   ** of all columns in TABLE.  The parser inserted a special expression
1277   ** with the TK_ALL operator for each "*" that it found in the column list.
1278   ** The following code just has to locate the TK_ALL expressions and expand
1279   ** each one to the list of all columns in all tables.
1280   **
1281   ** The first loop just checks to see if there are any "*" operators
1282   ** that need expanding.
1283   */
1284   for(k=0; k<pEList->nExpr; k++){
1285     Expr *pE = pEList->a[k].pExpr;
1286     if( pE->op==TK_ALL ) break;
1287     if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
1288          && pE->pLeft && pE->pLeft->op==TK_ID ) break;
1289   }
1290   rc = 0;
1291   if( k<pEList->nExpr ){
1292     /*
1293     ** If we get here it means the result set contains one or more "*"
1294     ** operators that need to be expanded.  Loop through each expression
1295     ** in the result set and expand them one by one.
1296     */
1297     struct ExprList_item *a = pEList->a;
1298     ExprList *pNew = 0;
1299     int flags = pParse->db->flags;
1300     int longNames = (flags & SQLITE_FullColNames)!=0 &&
1301                       (flags & SQLITE_ShortColNames)==0;
1302 
1303     for(k=0; k<pEList->nExpr; k++){
1304       Expr *pE = a[k].pExpr;
1305       if( pE->op!=TK_ALL &&
1306            (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
1307         /* This particular expression does not need to be expanded.
1308         */
1309         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0);
1310         if( pNew ){
1311           pNew->a[pNew->nExpr-1].zName = a[k].zName;
1312         }else{
1313           rc = 1;
1314         }
1315         a[k].pExpr = 0;
1316         a[k].zName = 0;
1317       }else{
1318         /* This expression is a "*" or a "TABLE.*" and needs to be
1319         ** expanded. */
1320         int tableSeen = 0;      /* Set to 1 when TABLE matches */
1321         char *zTName;            /* text of name of TABLE */
1322         if( pE->op==TK_DOT && pE->pLeft ){
1323           zTName = sqlite3NameFromToken(db, &pE->pLeft->token);
1324         }else{
1325           zTName = 0;
1326         }
1327         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
1328           Table *pTab = pFrom->pTab;
1329           char *zTabName = pFrom->zAlias;
1330           if( zTabName==0 || zTabName[0]==0 ){
1331             zTabName = pTab->zName;
1332           }
1333           if( zTName && (zTabName==0 || zTabName[0]==0 ||
1334                  sqlite3StrICmp(zTName, zTabName)!=0) ){
1335             continue;
1336           }
1337           tableSeen = 1;
1338           for(j=0; j<pTab->nCol; j++){
1339             Expr *pExpr, *pRight;
1340             char *zName = pTab->aCol[j].zName;
1341 
1342             /* If a column is marked as 'hidden' (currently only possible
1343             ** for virtual tables), do not include it in the expanded
1344             ** result-set list.
1345             */
1346             if( IsHiddenColumn(&pTab->aCol[j]) ){
1347               assert(IsVirtual(pTab));
1348               continue;
1349             }
1350 
1351             if( i>0 ){
1352               struct SrcList_item *pLeft = &pTabList->a[i-1];
1353               if( (pLeft[1].jointype & JT_NATURAL)!=0 &&
1354                         columnIndex(pLeft->pTab, zName)>=0 ){
1355                 /* In a NATURAL join, omit the join columns from the
1356                 ** table on the right */
1357                 continue;
1358               }
1359               if( sqlite3IdListIndex(pLeft[1].pUsing, zName)>=0 ){
1360                 /* In a join with a USING clause, omit columns in the
1361                 ** using clause from the table on the right. */
1362                 continue;
1363               }
1364             }
1365             pRight = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
1366             if( pRight==0 ) break;
1367             setQuotedToken(pParse, &pRight->token, zName);
1368             if( zTabName && (longNames || pTabList->nSrc>1) ){
1369               Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
1370               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
1371               if( pExpr==0 ) break;
1372               setQuotedToken(pParse, &pLeft->token, zTabName);
1373               setToken(&pExpr->span,
1374                   sqlite3MPrintf(db, "%s.%s", zTabName, zName));
1375               pExpr->span.dyn = 1;
1376               pExpr->token.z = 0;
1377               pExpr->token.n = 0;
1378               pExpr->token.dyn = 0;
1379             }else{
1380               pExpr = pRight;
1381               pExpr->span = pExpr->token;
1382               pExpr->span.dyn = 0;
1383             }
1384             if( longNames ){
1385               pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span);
1386             }else{
1387               pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token);
1388             }
1389           }
1390         }
1391         if( !tableSeen ){
1392           if( zTName ){
1393             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
1394           }else{
1395             sqlite3ErrorMsg(pParse, "no tables specified");
1396           }
1397           rc = 1;
1398         }
1399         sqlite3_free(zTName);
1400       }
1401     }
1402     sqlite3ExprListDelete(pEList);
1403     p->pEList = pNew;
1404   }
1405   if( p->pEList && p->pEList->nExpr>SQLITE_MAX_COLUMN ){
1406     sqlite3ErrorMsg(pParse, "too many columns in result set");
1407     rc = SQLITE_ERROR;
1408   }
1409   if( db->mallocFailed ){
1410     rc = SQLITE_NOMEM;
1411   }
1412   return rc;
1413 }
1414 
1415 /*
1416 ** pE is a pointer to an expression which is a single term in
1417 ** ORDER BY or GROUP BY clause.
1418 **
1419 ** If pE evaluates to an integer constant i, then return i.
1420 ** This is an indication to the caller that it should sort
1421 ** by the i-th column of the result set.
1422 **
1423 ** If pE is a well-formed expression and the SELECT statement
1424 ** is not compound, then return 0.  This indicates to the
1425 ** caller that it should sort by the value of the ORDER BY
1426 ** expression.
1427 **
1428 ** If the SELECT is compound, then attempt to match pE against
1429 ** result set columns in the left-most SELECT statement.  Return
1430 ** the index i of the matching column, as an indication to the
1431 ** caller that it should sort by the i-th column.  If there is
1432 ** no match, return -1 and leave an error message in pParse.
1433 */
1434 static int matchOrderByTermToExprList(
1435   Parse *pParse,     /* Parsing context for error messages */
1436   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
1437   Expr *pE,          /* The specific ORDER BY term */
1438   int idx,           /* When ORDER BY term is this */
1439   int isCompound,    /* True if this is a compound SELECT */
1440   u8 *pHasAgg        /* True if expression contains aggregate functions */
1441 ){
1442   int i;             /* Loop counter */
1443   ExprList *pEList;  /* The columns of the result set */
1444   NameContext nc;    /* Name context for resolving pE */
1445 
1446 
1447   /* If the term is an integer constant, return the value of that
1448   ** constant */
1449   pEList = pSelect->pEList;
1450   if( sqlite3ExprIsInteger(pE, &i) ){
1451     if( i<=0 ){
1452       /* If i is too small, make it too big.  That way the calling
1453       ** function still sees a value that is out of range, but does
1454       ** not confuse the column number with 0 or -1 result code.
1455       */
1456       i = pEList->nExpr+1;
1457     }
1458     return i;
1459   }
1460 
1461   /* If the term is a simple identifier that try to match that identifier
1462   ** against a column name in the result set.
1463   */
1464   if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){
1465     sqlite3 *db = pParse->db;
1466     char *zCol = sqlite3NameFromToken(db, &pE->token);
1467     if( db->mallocFailed ){
1468       sqlite3_free(zCol);
1469       return -1;
1470     }
1471     for(i=0; i<pEList->nExpr; i++){
1472       char *zAs = pEList->a[i].zName;
1473       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1474         sqlite3_free(zCol);
1475         return i+1;
1476       }
1477     }
1478     sqlite3_free(zCol);
1479   }
1480 
1481   /* Resolve all names in the ORDER BY term expression
1482   */
1483   memset(&nc, 0, sizeof(nc));
1484   nc.pParse = pParse;
1485   nc.pSrcList = pSelect->pSrc;
1486   nc.pEList = pEList;
1487   nc.allowAgg = 1;
1488   nc.nErr = 0;
1489   if( sqlite3ExprResolveNames(&nc, pE) ){
1490     if( isCompound ){
1491       sqlite3ErrorClear(pParse);
1492       return 0;
1493     }else{
1494       return -1;
1495     }
1496   }
1497   if( nc.hasAgg && pHasAgg ){
1498     *pHasAgg = 1;
1499   }
1500 
1501   /* For a compound SELECT, we need to try to match the ORDER BY
1502   ** expression against an expression in the result set
1503   */
1504   if( isCompound ){
1505     for(i=0; i<pEList->nExpr; i++){
1506       if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){
1507         return i+1;
1508       }
1509     }
1510   }
1511   return 0;
1512 }
1513 
1514 
1515 /*
1516 ** Analyze and ORDER BY or GROUP BY clause in a simple SELECT statement.
1517 ** Return the number of errors seen.
1518 **
1519 ** Every term of the ORDER BY or GROUP BY clause needs to be an
1520 ** expression.  If any expression is an integer constant, then
1521 ** that expression is replaced by the corresponding
1522 ** expression from the result set.
1523 */
1524 static int processOrderGroupBy(
1525   Parse *pParse,        /* Parsing context.  Leave error messages here */
1526   Select *pSelect,      /* The SELECT statement containing the clause */
1527   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
1528   int isOrder,          /* 1 for ORDER BY.  0 for GROUP BY */
1529   u8 *pHasAgg           /* Set to TRUE if any term contains an aggregate */
1530 ){
1531   int i;
1532   sqlite3 *db = pParse->db;
1533   ExprList *pEList;
1534 
1535   if( pOrderBy==0 ) return 0;
1536   if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){
1537     const char *zType = isOrder ? "ORDER" : "GROUP";
1538     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
1539     return 1;
1540   }
1541   pEList = pSelect->pEList;
1542   if( pEList==0 ){
1543     return 0;
1544   }
1545   for(i=0; i<pOrderBy->nExpr; i++){
1546     int iCol;
1547     Expr *pE = pOrderBy->a[i].pExpr;
1548     iCol = matchOrderByTermToExprList(pParse, pSelect, pE, i+1, 0, pHasAgg);
1549     if( iCol<0 ){
1550       return 1;
1551     }
1552     if( iCol>pEList->nExpr ){
1553       const char *zType = isOrder ? "ORDER" : "GROUP";
1554       sqlite3ErrorMsg(pParse,
1555          "%r %s BY term out of range - should be "
1556          "between 1 and %d", i+1, zType, pEList->nExpr);
1557       return 1;
1558     }
1559     if( iCol>0 ){
1560       CollSeq *pColl = pE->pColl;
1561       int flags = pE->flags & EP_ExpCollate;
1562       sqlite3ExprDelete(pE);
1563       pE = sqlite3ExprDup(db, pEList->a[iCol-1].pExpr);
1564       pOrderBy->a[i].pExpr = pE;
1565       if( pColl && flags ){
1566         pE->pColl = pColl;
1567         pE->flags |= flags;
1568       }
1569     }
1570   }
1571   return 0;
1572 }
1573 
1574 /*
1575 ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement.  Return
1576 ** the number of errors seen.
1577 **
1578 ** The processing depends on whether the SELECT is simple or compound.
1579 ** For a simple SELECT statement, evry term of the ORDER BY or GROUP BY
1580 ** clause needs to be an expression.  If any expression is an integer
1581 ** constant, then that expression is replaced by the corresponding
1582 ** expression from the result set.
1583 **
1584 ** For compound SELECT statements, every expression needs to be of
1585 ** type TK_COLUMN with a iTable value as given in the 4th parameter.
1586 ** If any expression is an integer, that becomes the column number.
1587 ** Otherwise, match the expression against result set columns from
1588 ** the left-most SELECT.
1589 */
1590 static int processCompoundOrderBy(
1591   Parse *pParse,        /* Parsing context.  Leave error messages here */
1592   Select *pSelect,      /* The SELECT statement containing the ORDER BY */
1593   int iTable            /* Output table for compound SELECT statements */
1594 ){
1595   int i;
1596   ExprList *pOrderBy;
1597   ExprList *pEList;
1598   sqlite3 *db;
1599   int moreToDo = 1;
1600 
1601   pOrderBy = pSelect->pOrderBy;
1602   if( pOrderBy==0 ) return 0;
1603   if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){
1604     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
1605     return 1;
1606   }
1607   db = pParse->db;
1608   for(i=0; i<pOrderBy->nExpr; i++){
1609     pOrderBy->a[i].done = 0;
1610   }
1611   while( pSelect->pPrior ){
1612     pSelect = pSelect->pPrior;
1613   }
1614   while( pSelect && moreToDo ){
1615     moreToDo = 0;
1616     for(i=0; i<pOrderBy->nExpr; i++){
1617       int iCol;
1618       Expr *pE;
1619       if( pOrderBy->a[i].done ) continue;
1620       pE = pOrderBy->a[i].pExpr;
1621       Expr *pDup = sqlite3ExprDup(db, pE);
1622       if( pDup==0 ){
1623         return 1;
1624       }
1625       iCol = matchOrderByTermToExprList(pParse, pSelect, pDup, i+1, 1, 0);
1626       sqlite3ExprDelete(pDup);
1627       if( iCol<0 ){
1628         return 1;
1629       }
1630       pEList = pSelect->pEList;
1631       if( pEList==0 ){
1632         return 1;
1633       }
1634       if( iCol>pEList->nExpr ){
1635         sqlite3ErrorMsg(pParse,
1636            "%r ORDER BY term out of range - should be "
1637            "between 1 and %d", i+1, pEList->nExpr);
1638         return 1;
1639       }
1640       if( iCol>0 ){
1641         pE->op = TK_COLUMN;
1642         pE->iTable = iTable;
1643         pE->iAgg = -1;
1644         pE->iColumn = iCol-1;
1645         pE->pTab = 0;
1646         pOrderBy->a[i].done = 1;
1647       }else{
1648         moreToDo = 1;
1649       }
1650     }
1651     pSelect = pSelect->pNext;
1652   }
1653   for(i=0; i<pOrderBy->nExpr; i++){
1654     if( pOrderBy->a[i].done==0 ){
1655       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
1656             "column in the result set", i+1);
1657       return 1;
1658     }
1659   }
1660   return 0;
1661 }
1662 
1663 /*
1664 ** Get a VDBE for the given parser context.  Create a new one if necessary.
1665 ** If an error occurs, return NULL and leave a message in pParse.
1666 */
1667 Vdbe *sqlite3GetVdbe(Parse *pParse){
1668   Vdbe *v = pParse->pVdbe;
1669   if( v==0 ){
1670     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
1671   }
1672   return v;
1673 }
1674 
1675 
1676 /*
1677 ** Compute the iLimit and iOffset fields of the SELECT based on the
1678 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
1679 ** that appear in the original SQL statement after the LIMIT and OFFSET
1680 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
1681 ** are the integer memory register numbers for counters used to compute
1682 ** the limit and offset.  If there is no limit and/or offset, then
1683 ** iLimit and iOffset are negative.
1684 **
1685 ** This routine changes the values of iLimit and iOffset only if
1686 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
1687 ** iOffset should have been preset to appropriate default values
1688 ** (usually but not always -1) prior to calling this routine.
1689 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
1690 ** redefined.  The UNION ALL operator uses this property to force
1691 ** the reuse of the same limit and offset registers across multiple
1692 ** SELECT statements.
1693 */
1694 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
1695   Vdbe *v = 0;
1696   int iLimit = 0;
1697   int iOffset;
1698   int addr1, addr2;
1699 
1700   /*
1701   ** "LIMIT -1" always shows all rows.  There is some
1702   ** contraversy about what the correct behavior should be.
1703   ** The current implementation interprets "LIMIT 0" to mean
1704   ** no rows.
1705   */
1706   if( p->pLimit ){
1707     p->iLimit = iLimit = pParse->nMem;
1708     pParse->nMem += 2;
1709     v = sqlite3GetVdbe(pParse);
1710     if( v==0 ) return;
1711     sqlite3ExprCode(pParse, p->pLimit);
1712     sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
1713     sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 1);
1714     VdbeComment((v, "# LIMIT counter"));
1715     sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak);
1716     sqlite3VdbeAddOp(v, OP_MemLoad, iLimit, 0);
1717   }
1718   if( p->pOffset ){
1719     p->iOffset = iOffset = pParse->nMem++;
1720     v = sqlite3GetVdbe(pParse);
1721     if( v==0 ) return;
1722     sqlite3ExprCode(pParse, p->pOffset);
1723     sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
1724     sqlite3VdbeAddOp(v, OP_MemStore, iOffset, p->pLimit==0);
1725     VdbeComment((v, "# OFFSET counter"));
1726     addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iOffset, 0);
1727     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1728     sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1729     sqlite3VdbeJumpHere(v, addr1);
1730     if( p->pLimit ){
1731       sqlite3VdbeAddOp(v, OP_Add, 0, 0);
1732     }
1733   }
1734   if( p->pLimit ){
1735     addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iLimit, 0);
1736     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1737     sqlite3VdbeAddOp(v, OP_MemInt, -1, iLimit+1);
1738     addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
1739     sqlite3VdbeJumpHere(v, addr1);
1740     sqlite3VdbeAddOp(v, OP_MemStore, iLimit+1, 1);
1741     VdbeComment((v, "# LIMIT+OFFSET"));
1742     sqlite3VdbeJumpHere(v, addr2);
1743   }
1744 }
1745 
1746 /*
1747 ** Allocate a virtual index to use for sorting.
1748 */
1749 static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){
1750   if( pOrderBy ){
1751     int addr;
1752     assert( pOrderBy->iECursor==0 );
1753     pOrderBy->iECursor = pParse->nTab++;
1754     addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenEphemeral,
1755                             pOrderBy->iECursor, pOrderBy->nExpr+1);
1756     assert( p->addrOpenEphm[2] == -1 );
1757     p->addrOpenEphm[2] = addr;
1758   }
1759 }
1760 
1761 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1762 /*
1763 ** Return the appropriate collating sequence for the iCol-th column of
1764 ** the result set for the compound-select statement "p".  Return NULL if
1765 ** the column has no default collating sequence.
1766 **
1767 ** The collating sequence for the compound select is taken from the
1768 ** left-most term of the select that has a collating sequence.
1769 */
1770 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
1771   CollSeq *pRet;
1772   if( p->pPrior ){
1773     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
1774   }else{
1775     pRet = 0;
1776   }
1777   if( pRet==0 ){
1778     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
1779   }
1780   return pRet;
1781 }
1782 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1783 
1784 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1785 /*
1786 ** This routine is called to process a query that is really the union
1787 ** or intersection of two or more separate queries.
1788 **
1789 ** "p" points to the right-most of the two queries.  the query on the
1790 ** left is p->pPrior.  The left query could also be a compound query
1791 ** in which case this routine will be called recursively.
1792 **
1793 ** The results of the total query are to be written into a destination
1794 ** of type eDest with parameter iParm.
1795 **
1796 ** Example 1:  Consider a three-way compound SQL statement.
1797 **
1798 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1799 **
1800 ** This statement is parsed up as follows:
1801 **
1802 **     SELECT c FROM t3
1803 **      |
1804 **      `----->  SELECT b FROM t2
1805 **                |
1806 **                `------>  SELECT a FROM t1
1807 **
1808 ** The arrows in the diagram above represent the Select.pPrior pointer.
1809 ** So if this routine is called with p equal to the t3 query, then
1810 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
1811 **
1812 ** Notice that because of the way SQLite parses compound SELECTs, the
1813 ** individual selects always group from left to right.
1814 */
1815 static int multiSelect(
1816   Parse *pParse,        /* Parsing context */
1817   Select *p,            /* The right-most of SELECTs to be coded */
1818   int eDest,            /* \___  Store query results as specified */
1819   int iParm,            /* /     by these two parameters.         */
1820   char *aff             /* If eDest is SRT_Union, the affinity string */
1821 ){
1822   int rc = SQLITE_OK;   /* Success code from a subroutine */
1823   Select *pPrior;       /* Another SELECT immediately to our left */
1824   Vdbe *v;              /* Generate code to this VDBE */
1825   int nCol;             /* Number of columns in the result set */
1826   ExprList *pOrderBy;   /* The ORDER BY clause on p */
1827   int aSetP2[2];        /* Set P2 value of these op to number of columns */
1828   int nSetP2 = 0;       /* Number of slots in aSetP2[] used */
1829 
1830   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
1831   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
1832   */
1833   if( p==0 || p->pPrior==0 ){
1834     rc = 1;
1835     goto multi_select_end;
1836   }
1837   pPrior = p->pPrior;
1838   assert( pPrior->pRightmost!=pPrior );
1839   assert( pPrior->pRightmost==p->pRightmost );
1840   if( pPrior->pOrderBy ){
1841     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
1842       selectOpName(p->op));
1843     rc = 1;
1844     goto multi_select_end;
1845   }
1846   if( pPrior->pLimit ){
1847     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
1848       selectOpName(p->op));
1849     rc = 1;
1850     goto multi_select_end;
1851   }
1852 
1853   /* Make sure we have a valid query engine.  If not, create a new one.
1854   */
1855   v = sqlite3GetVdbe(pParse);
1856   if( v==0 ){
1857     rc = 1;
1858     goto multi_select_end;
1859   }
1860 
1861   /* Create the destination temporary table if necessary
1862   */
1863   if( eDest==SRT_EphemTab ){
1864     assert( p->pEList );
1865     assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );
1866     aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 0);
1867     eDest = SRT_Table;
1868   }
1869 
1870   /* Generate code for the left and right SELECT statements.
1871   */
1872   pOrderBy = p->pOrderBy;
1873   switch( p->op ){
1874     case TK_ALL: {
1875       if( pOrderBy==0 ){
1876         int addr = 0;
1877         assert( !pPrior->pLimit );
1878         pPrior->pLimit = p->pLimit;
1879         pPrior->pOffset = p->pOffset;
1880         rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff);
1881         p->pLimit = 0;
1882         p->pOffset = 0;
1883         if( rc ){
1884           goto multi_select_end;
1885         }
1886         p->pPrior = 0;
1887         p->iLimit = pPrior->iLimit;
1888         p->iOffset = pPrior->iOffset;
1889         if( p->iLimit>=0 ){
1890           addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0);
1891           VdbeComment((v, "# Jump ahead if LIMIT reached"));
1892         }
1893         rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff);
1894         p->pPrior = pPrior;
1895         if( rc ){
1896           goto multi_select_end;
1897         }
1898         if( addr ){
1899           sqlite3VdbeJumpHere(v, addr);
1900         }
1901         break;
1902       }
1903       /* For UNION ALL ... ORDER BY fall through to the next case */
1904     }
1905     case TK_EXCEPT:
1906     case TK_UNION: {
1907       int unionTab;    /* Cursor number of the temporary table holding result */
1908       int op = 0;      /* One of the SRT_ operations to apply to self */
1909       int priorOp;     /* The SRT_ operation to apply to prior selects */
1910       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
1911       int addr;
1912 
1913       priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
1914       if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){
1915         /* We can reuse a temporary table generated by a SELECT to our
1916         ** right.
1917         */
1918         unionTab = iParm;
1919       }else{
1920         /* We will need to create our own temporary table to hold the
1921         ** intermediate results.
1922         */
1923         unionTab = pParse->nTab++;
1924         if( processCompoundOrderBy(pParse, p, unionTab) ){
1925           rc = 1;
1926           goto multi_select_end;
1927         }
1928         addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, unionTab, 0);
1929         if( priorOp==SRT_Table ){
1930           assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );
1931           aSetP2[nSetP2++] = addr;
1932         }else{
1933           assert( p->addrOpenEphm[0] == -1 );
1934           p->addrOpenEphm[0] = addr;
1935           p->pRightmost->usesEphm = 1;
1936         }
1937         createSortingIndex(pParse, p, pOrderBy);
1938         assert( p->pEList );
1939       }
1940 
1941       /* Code the SELECT statements to our left
1942       */
1943       assert( !pPrior->pOrderBy );
1944       rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff);
1945       if( rc ){
1946         goto multi_select_end;
1947       }
1948 
1949       /* Code the current SELECT statement
1950       */
1951       switch( p->op ){
1952          case TK_EXCEPT:  op = SRT_Except;   break;
1953          case TK_UNION:   op = SRT_Union;    break;
1954          case TK_ALL:     op = SRT_Table;    break;
1955       }
1956       p->pPrior = 0;
1957       p->pOrderBy = 0;
1958       p->disallowOrderBy = pOrderBy!=0;
1959       pLimit = p->pLimit;
1960       p->pLimit = 0;
1961       pOffset = p->pOffset;
1962       p->pOffset = 0;
1963       rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff);
1964       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
1965       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
1966       sqlite3ExprListDelete(p->pOrderBy);
1967       p->pPrior = pPrior;
1968       p->pOrderBy = pOrderBy;
1969       sqlite3ExprDelete(p->pLimit);
1970       p->pLimit = pLimit;
1971       p->pOffset = pOffset;
1972       p->iLimit = -1;
1973       p->iOffset = -1;
1974       if( rc ){
1975         goto multi_select_end;
1976       }
1977 
1978 
1979       /* Convert the data in the temporary table into whatever form
1980       ** it is that we currently need.
1981       */
1982       if( eDest!=priorOp || unionTab!=iParm ){
1983         int iCont, iBreak, iStart;
1984         assert( p->pEList );
1985         if( eDest==SRT_Callback ){
1986           Select *pFirst = p;
1987           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
1988           generateColumnNames(pParse, 0, pFirst->pEList);
1989         }
1990         iBreak = sqlite3VdbeMakeLabel(v);
1991         iCont = sqlite3VdbeMakeLabel(v);
1992         computeLimitRegisters(pParse, p, iBreak);
1993         sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1994         iStart = sqlite3VdbeCurrentAddr(v);
1995         rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
1996                              pOrderBy, -1, eDest, iParm,
1997                              iCont, iBreak, 0);
1998         if( rc ){
1999           rc = 1;
2000           goto multi_select_end;
2001         }
2002         sqlite3VdbeResolveLabel(v, iCont);
2003         sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart);
2004         sqlite3VdbeResolveLabel(v, iBreak);
2005         sqlite3VdbeAddOp(v, OP_Close, unionTab, 0);
2006       }
2007       break;
2008     }
2009     case TK_INTERSECT: {
2010       int tab1, tab2;
2011       int iCont, iBreak, iStart;
2012       Expr *pLimit, *pOffset;
2013       int addr;
2014 
2015       /* INTERSECT is different from the others since it requires
2016       ** two temporary tables.  Hence it has its own case.  Begin
2017       ** by allocating the tables we will need.
2018       */
2019       tab1 = pParse->nTab++;
2020       tab2 = pParse->nTab++;
2021       if( processCompoundOrderBy(pParse, p, tab1) ){
2022         rc = 1;
2023         goto multi_select_end;
2024       }
2025       createSortingIndex(pParse, p, pOrderBy);
2026 
2027       addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab1, 0);
2028       assert( p->addrOpenEphm[0] == -1 );
2029       p->addrOpenEphm[0] = addr;
2030       p->pRightmost->usesEphm = 1;
2031       assert( p->pEList );
2032 
2033       /* Code the SELECTs to our left into temporary table "tab1".
2034       */
2035       rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff);
2036       if( rc ){
2037         goto multi_select_end;
2038       }
2039 
2040       /* Code the current SELECT into temporary table "tab2"
2041       */
2042       addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab2, 0);
2043       assert( p->addrOpenEphm[1] == -1 );
2044       p->addrOpenEphm[1] = addr;
2045       p->pPrior = 0;
2046       pLimit = p->pLimit;
2047       p->pLimit = 0;
2048       pOffset = p->pOffset;
2049       p->pOffset = 0;
2050       rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff);
2051       p->pPrior = pPrior;
2052       sqlite3ExprDelete(p->pLimit);
2053       p->pLimit = pLimit;
2054       p->pOffset = pOffset;
2055       if( rc ){
2056         goto multi_select_end;
2057       }
2058 
2059       /* Generate code to take the intersection of the two temporary
2060       ** tables.
2061       */
2062       assert( p->pEList );
2063       if( eDest==SRT_Callback ){
2064         Select *pFirst = p;
2065         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
2066         generateColumnNames(pParse, 0, pFirst->pEList);
2067       }
2068       iBreak = sqlite3VdbeMakeLabel(v);
2069       iCont = sqlite3VdbeMakeLabel(v);
2070       computeLimitRegisters(pParse, p, iBreak);
2071       sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak);
2072       iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0);
2073       sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont);
2074       rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
2075                              pOrderBy, -1, eDest, iParm,
2076                              iCont, iBreak, 0);
2077       if( rc ){
2078         rc = 1;
2079         goto multi_select_end;
2080       }
2081       sqlite3VdbeResolveLabel(v, iCont);
2082       sqlite3VdbeAddOp(v, OP_Next, tab1, iStart);
2083       sqlite3VdbeResolveLabel(v, iBreak);
2084       sqlite3VdbeAddOp(v, OP_Close, tab2, 0);
2085       sqlite3VdbeAddOp(v, OP_Close, tab1, 0);
2086       break;
2087     }
2088   }
2089 
2090   /* Make sure all SELECTs in the statement have the same number of elements
2091   ** in their result sets.
2092   */
2093   assert( p->pEList && pPrior->pEList );
2094   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
2095     sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
2096       " do not have the same number of result columns", selectOpName(p->op));
2097     rc = 1;
2098     goto multi_select_end;
2099   }
2100 
2101   /* Set the number of columns in temporary tables
2102   */
2103   nCol = p->pEList->nExpr;
2104   while( nSetP2 ){
2105     sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol);
2106   }
2107 
2108   /* Compute collating sequences used by either the ORDER BY clause or
2109   ** by any temporary tables needed to implement the compound select.
2110   ** Attach the KeyInfo structure to all temporary tables.  Invoke the
2111   ** ORDER BY processing if there is an ORDER BY clause.
2112   **
2113   ** This section is run by the right-most SELECT statement only.
2114   ** SELECT statements to the left always skip this part.  The right-most
2115   ** SELECT might also skip this part if it has no ORDER BY clause and
2116   ** no temp tables are required.
2117   */
2118   if( pOrderBy || p->usesEphm ){
2119     int i;                        /* Loop counter */
2120     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
2121     Select *pLoop;                /* For looping through SELECT statements */
2122     int nKeyCol;                  /* Number of entries in pKeyInfo->aCol[] */
2123     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
2124     CollSeq **aCopy;              /* A copy of pKeyInfo->aColl[] */
2125 
2126     assert( p->pRightmost==p );
2127     nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0);
2128     pKeyInfo = sqlite3DbMallocZero(pParse->db,
2129                        sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1));
2130     if( !pKeyInfo ){
2131       rc = SQLITE_NOMEM;
2132       goto multi_select_end;
2133     }
2134 
2135     pKeyInfo->enc = ENC(pParse->db);
2136     pKeyInfo->nField = nCol;
2137 
2138     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
2139       *apColl = multiSelectCollSeq(pParse, p, i);
2140       if( 0==*apColl ){
2141         *apColl = pParse->db->pDfltColl;
2142       }
2143     }
2144 
2145     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
2146       for(i=0; i<2; i++){
2147         int addr = pLoop->addrOpenEphm[i];
2148         if( addr<0 ){
2149           /* If [0] is unused then [1] is also unused.  So we can
2150           ** always safely abort as soon as the first unused slot is found */
2151           assert( pLoop->addrOpenEphm[1]<0 );
2152           break;
2153         }
2154         sqlite3VdbeChangeP2(v, addr, nCol);
2155         sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO);
2156         pLoop->addrOpenEphm[i] = -1;
2157       }
2158     }
2159 
2160     if( pOrderBy ){
2161       struct ExprList_item *pOTerm = pOrderBy->a;
2162       int nOrderByExpr = pOrderBy->nExpr;
2163       int addr;
2164       u8 *pSortOrder;
2165 
2166       /* Reuse the same pKeyInfo for the ORDER BY as was used above for
2167       ** the compound select statements.  Except we have to change out the
2168       ** pKeyInfo->aColl[] values.  Some of the aColl[] values will be
2169       ** reused when constructing the pKeyInfo for the ORDER BY, so make
2170       ** a copy.  Sufficient space to hold both the nCol entries for
2171       ** the compound select and the nOrderbyExpr entries for the ORDER BY
2172       ** was allocated above.  But we need to move the compound select
2173       ** entries out of the way before constructing the ORDER BY entries.
2174       ** Move the compound select entries into aCopy[] where they can be
2175       ** accessed and reused when constructing the ORDER BY entries.
2176       ** Because nCol might be greater than or less than nOrderByExpr
2177       ** we have to use memmove() when doing the copy.
2178       */
2179       aCopy = &pKeyInfo->aColl[nOrderByExpr];
2180       pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol];
2181       memmove(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*));
2182 
2183       apColl = pKeyInfo->aColl;
2184       for(i=0; i<nOrderByExpr; i++, pOTerm++, apColl++, pSortOrder++){
2185         Expr *pExpr = pOTerm->pExpr;
2186         if( (pExpr->flags & EP_ExpCollate) ){
2187           assert( pExpr->pColl!=0 );
2188           *apColl = pExpr->pColl;
2189         }else{
2190           *apColl = aCopy[pExpr->iColumn];
2191         }
2192         *pSortOrder = pOTerm->sortOrder;
2193       }
2194       assert( p->pRightmost==p );
2195       assert( p->addrOpenEphm[2]>=0 );
2196       addr = p->addrOpenEphm[2];
2197       sqlite3VdbeChangeP2(v, addr, p->pOrderBy->nExpr+2);
2198       pKeyInfo->nField = nOrderByExpr;
2199       sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
2200       pKeyInfo = 0;
2201       generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
2202     }
2203 
2204     sqlite3_free(pKeyInfo);
2205   }
2206 
2207 multi_select_end:
2208   return rc;
2209 }
2210 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
2211 
2212 #ifndef SQLITE_OMIT_VIEW
2213 /* Forward Declarations */
2214 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
2215 static void substSelect(sqlite3*, Select *, int, ExprList *);
2216 
2217 /*
2218 ** Scan through the expression pExpr.  Replace every reference to
2219 ** a column in table number iTable with a copy of the iColumn-th
2220 ** entry in pEList.  (But leave references to the ROWID column
2221 ** unchanged.)
2222 **
2223 ** This routine is part of the flattening procedure.  A subquery
2224 ** whose result set is defined by pEList appears as entry in the
2225 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
2226 ** FORM clause entry is iTable.  This routine make the necessary
2227 ** changes to pExpr so that it refers directly to the source table
2228 ** of the subquery rather the result set of the subquery.
2229 */
2230 static void substExpr(
2231   sqlite3 *db,        /* Report malloc errors to this connection */
2232   Expr *pExpr,        /* Expr in which substitution occurs */
2233   int iTable,         /* Table to be substituted */
2234   ExprList *pEList    /* Substitute expressions */
2235 ){
2236   if( pExpr==0 ) return;
2237   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
2238     if( pExpr->iColumn<0 ){
2239       pExpr->op = TK_NULL;
2240     }else{
2241       Expr *pNew;
2242       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
2243       assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
2244       pNew = pEList->a[pExpr->iColumn].pExpr;
2245       assert( pNew!=0 );
2246       pExpr->op = pNew->op;
2247       assert( pExpr->pLeft==0 );
2248       pExpr->pLeft = sqlite3ExprDup(db, pNew->pLeft);
2249       assert( pExpr->pRight==0 );
2250       pExpr->pRight = sqlite3ExprDup(db, pNew->pRight);
2251       assert( pExpr->pList==0 );
2252       pExpr->pList = sqlite3ExprListDup(db, pNew->pList);
2253       pExpr->iTable = pNew->iTable;
2254       pExpr->pTab = pNew->pTab;
2255       pExpr->iColumn = pNew->iColumn;
2256       pExpr->iAgg = pNew->iAgg;
2257       sqlite3TokenCopy(db, &pExpr->token, &pNew->token);
2258       sqlite3TokenCopy(db, &pExpr->span, &pNew->span);
2259       pExpr->pSelect = sqlite3SelectDup(db, pNew->pSelect);
2260       pExpr->flags = pNew->flags;
2261     }
2262   }else{
2263     substExpr(db, pExpr->pLeft, iTable, pEList);
2264     substExpr(db, pExpr->pRight, iTable, pEList);
2265     substSelect(db, pExpr->pSelect, iTable, pEList);
2266     substExprList(db, pExpr->pList, iTable, pEList);
2267   }
2268 }
2269 static void substExprList(
2270   sqlite3 *db,         /* Report malloc errors here */
2271   ExprList *pList,     /* List to scan and in which to make substitutes */
2272   int iTable,          /* Table to be substituted */
2273   ExprList *pEList     /* Substitute values */
2274 ){
2275   int i;
2276   if( pList==0 ) return;
2277   for(i=0; i<pList->nExpr; i++){
2278     substExpr(db, pList->a[i].pExpr, iTable, pEList);
2279   }
2280 }
2281 static void substSelect(
2282   sqlite3 *db,         /* Report malloc errors here */
2283   Select *p,           /* SELECT statement in which to make substitutions */
2284   int iTable,          /* Table to be replaced */
2285   ExprList *pEList     /* Substitute values */
2286 ){
2287   if( !p ) return;
2288   substExprList(db, p->pEList, iTable, pEList);
2289   substExprList(db, p->pGroupBy, iTable, pEList);
2290   substExprList(db, p->pOrderBy, iTable, pEList);
2291   substExpr(db, p->pHaving, iTable, pEList);
2292   substExpr(db, p->pWhere, iTable, pEList);
2293   substSelect(db, p->pPrior, iTable, pEList);
2294 }
2295 #endif /* !defined(SQLITE_OMIT_VIEW) */
2296 
2297 #ifndef SQLITE_OMIT_VIEW
2298 /*
2299 ** This routine attempts to flatten subqueries in order to speed
2300 ** execution.  It returns 1 if it makes changes and 0 if no flattening
2301 ** occurs.
2302 **
2303 ** To understand the concept of flattening, consider the following
2304 ** query:
2305 **
2306 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
2307 **
2308 ** The default way of implementing this query is to execute the
2309 ** subquery first and store the results in a temporary table, then
2310 ** run the outer query on that temporary table.  This requires two
2311 ** passes over the data.  Furthermore, because the temporary table
2312 ** has no indices, the WHERE clause on the outer query cannot be
2313 ** optimized.
2314 **
2315 ** This routine attempts to rewrite queries such as the above into
2316 ** a single flat select, like this:
2317 **
2318 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
2319 **
2320 ** The code generated for this simpification gives the same result
2321 ** but only has to scan the data once.  And because indices might
2322 ** exist on the table t1, a complete scan of the data might be
2323 ** avoided.
2324 **
2325 ** Flattening is only attempted if all of the following are true:
2326 **
2327 **   (1)  The subquery and the outer query do not both use aggregates.
2328 **
2329 **   (2)  The subquery is not an aggregate or the outer query is not a join.
2330 **
2331 **   (3)  The subquery is not the right operand of a left outer join, or
2332 **        the subquery is not itself a join.  (Ticket #306)
2333 **
2334 **   (4)  The subquery is not DISTINCT or the outer query is not a join.
2335 **
2336 **   (5)  The subquery is not DISTINCT or the outer query does not use
2337 **        aggregates.
2338 **
2339 **   (6)  The subquery does not use aggregates or the outer query is not
2340 **        DISTINCT.
2341 **
2342 **   (7)  The subquery has a FROM clause.
2343 **
2344 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
2345 **
2346 **   (9)  The subquery does not use LIMIT or the outer query does not use
2347 **        aggregates.
2348 **
2349 **  (10)  The subquery does not use aggregates or the outer query does not
2350 **        use LIMIT.
2351 **
2352 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
2353 **
2354 **  (12)  The subquery is not the right term of a LEFT OUTER JOIN or the
2355 **        subquery has no WHERE clause.  (added by ticket #350)
2356 **
2357 **  (13)  The subquery and outer query do not both use LIMIT
2358 **
2359 **  (14)  The subquery does not use OFFSET
2360 **
2361 **  (15)  The outer query is not part of a compound select or the
2362 **        subquery does not have both an ORDER BY and a LIMIT clause.
2363 **        (See ticket #2339)
2364 **
2365 ** In this routine, the "p" parameter is a pointer to the outer query.
2366 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
2367 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
2368 **
2369 ** If flattening is not attempted, this routine is a no-op and returns 0.
2370 ** If flattening is attempted this routine returns 1.
2371 **
2372 ** All of the expression analysis must occur on both the outer query and
2373 ** the subquery before this routine runs.
2374 */
2375 static int flattenSubquery(
2376   sqlite3 *db,         /* Database connection */
2377   Select *p,           /* The parent or outer SELECT statement */
2378   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
2379   int isAgg,           /* True if outer SELECT uses aggregate functions */
2380   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
2381 ){
2382   Select *pSub;       /* The inner query or "subquery" */
2383   SrcList *pSrc;      /* The FROM clause of the outer query */
2384   SrcList *pSubSrc;   /* The FROM clause of the subquery */
2385   ExprList *pList;    /* The result set of the outer query */
2386   int iParent;        /* VDBE cursor number of the pSub result set temp table */
2387   int i;              /* Loop counter */
2388   Expr *pWhere;                    /* The WHERE clause */
2389   struct SrcList_item *pSubitem;   /* The subquery */
2390 
2391   /* Check to see if flattening is permitted.  Return 0 if not.
2392   */
2393   if( p==0 ) return 0;
2394   pSrc = p->pSrc;
2395   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
2396   pSubitem = &pSrc->a[iFrom];
2397   pSub = pSubitem->pSelect;
2398   assert( pSub!=0 );
2399   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
2400   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
2401   pSubSrc = pSub->pSrc;
2402   assert( pSubSrc );
2403   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
2404   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
2405   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
2406   ** became arbitrary expressions, we were forced to add restrictions (13)
2407   ** and (14). */
2408   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
2409   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
2410   if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){
2411     return 0;                                            /* Restriction (15) */
2412   }
2413   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
2414   if( (pSub->isDistinct || pSub->pLimit)
2415          && (pSrc->nSrc>1 || isAgg) ){          /* Restrictions (4)(5)(8)(9) */
2416      return 0;
2417   }
2418   if( p->isDistinct && subqueryIsAgg ) return 0;         /* Restriction (6)  */
2419   if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){
2420      return 0;                                           /* Restriction (11) */
2421   }
2422 
2423   /* Restriction 3:  If the subquery is a join, make sure the subquery is
2424   ** not used as the right operand of an outer join.  Examples of why this
2425   ** is not allowed:
2426   **
2427   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
2428   **
2429   ** If we flatten the above, we would get
2430   **
2431   **         (t1 LEFT OUTER JOIN t2) JOIN t3
2432   **
2433   ** which is not at all the same thing.
2434   */
2435   if( pSubSrc->nSrc>1 && (pSubitem->jointype & JT_OUTER)!=0 ){
2436     return 0;
2437   }
2438 
2439   /* Restriction 12:  If the subquery is the right operand of a left outer
2440   ** join, make sure the subquery has no WHERE clause.
2441   ** An examples of why this is not allowed:
2442   **
2443   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
2444   **
2445   ** If we flatten the above, we would get
2446   **
2447   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
2448   **
2449   ** But the t2.x>0 test will always fail on a NULL row of t2, which
2450   ** effectively converts the OUTER JOIN into an INNER JOIN.
2451   */
2452   if( (pSubitem->jointype & JT_OUTER)!=0 && pSub->pWhere!=0 ){
2453     return 0;
2454   }
2455 
2456   /* If we reach this point, it means flattening is permitted for the
2457   ** iFrom-th entry of the FROM clause in the outer query.
2458   */
2459 
2460   /* Move all of the FROM elements of the subquery into the
2461   ** the FROM clause of the outer query.  Before doing this, remember
2462   ** the cursor number for the original outer query FROM element in
2463   ** iParent.  The iParent cursor will never be used.  Subsequent code
2464   ** will scan expressions looking for iParent references and replace
2465   ** those references with expressions that resolve to the subquery FROM
2466   ** elements we are now copying in.
2467   */
2468   iParent = pSubitem->iCursor;
2469   {
2470     int nSubSrc = pSubSrc->nSrc;
2471     int jointype = pSubitem->jointype;
2472 
2473     sqlite3DeleteTable(pSubitem->pTab);
2474     sqlite3_free(pSubitem->zDatabase);
2475     sqlite3_free(pSubitem->zName);
2476     sqlite3_free(pSubitem->zAlias);
2477     pSubitem->pTab = 0;
2478     pSubitem->zDatabase = 0;
2479     pSubitem->zName = 0;
2480     pSubitem->zAlias = 0;
2481     if( nSubSrc>1 ){
2482       int extra = nSubSrc - 1;
2483       for(i=1; i<nSubSrc; i++){
2484         pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0);
2485         if( pSrc==0 ){
2486           p->pSrc = 0;
2487           return 1;
2488         }
2489       }
2490       p->pSrc = pSrc;
2491       for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
2492         pSrc->a[i] = pSrc->a[i-extra];
2493       }
2494     }
2495     for(i=0; i<nSubSrc; i++){
2496       pSrc->a[i+iFrom] = pSubSrc->a[i];
2497       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
2498     }
2499     pSrc->a[iFrom].jointype = jointype;
2500   }
2501 
2502   /* Now begin substituting subquery result set expressions for
2503   ** references to the iParent in the outer query.
2504   **
2505   ** Example:
2506   **
2507   **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
2508   **   \                     \_____________ subquery __________/          /
2509   **    \_____________________ outer query ______________________________/
2510   **
2511   ** We look at every expression in the outer query and every place we see
2512   ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
2513   */
2514   pList = p->pEList;
2515   for(i=0; i<pList->nExpr; i++){
2516     Expr *pExpr;
2517     if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
2518       pList->a[i].zName =
2519              sqlite3DbStrNDup(db, (char*)pExpr->span.z, pExpr->span.n);
2520     }
2521   }
2522   substExprList(db, p->pEList, iParent, pSub->pEList);
2523   if( isAgg ){
2524     substExprList(db, p->pGroupBy, iParent, pSub->pEList);
2525     substExpr(db, p->pHaving, iParent, pSub->pEList);
2526   }
2527   if( pSub->pOrderBy ){
2528     assert( p->pOrderBy==0 );
2529     p->pOrderBy = pSub->pOrderBy;
2530     pSub->pOrderBy = 0;
2531   }else if( p->pOrderBy ){
2532     substExprList(db, p->pOrderBy, iParent, pSub->pEList);
2533   }
2534   if( pSub->pWhere ){
2535     pWhere = sqlite3ExprDup(db, pSub->pWhere);
2536   }else{
2537     pWhere = 0;
2538   }
2539   if( subqueryIsAgg ){
2540     assert( p->pHaving==0 );
2541     p->pHaving = p->pWhere;
2542     p->pWhere = pWhere;
2543     substExpr(db, p->pHaving, iParent, pSub->pEList);
2544     p->pHaving = sqlite3ExprAnd(db, p->pHaving,
2545                                 sqlite3ExprDup(db, pSub->pHaving));
2546     assert( p->pGroupBy==0 );
2547     p->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy);
2548   }else{
2549     substExpr(db, p->pWhere, iParent, pSub->pEList);
2550     p->pWhere = sqlite3ExprAnd(db, p->pWhere, pWhere);
2551   }
2552 
2553   /* The flattened query is distinct if either the inner or the
2554   ** outer query is distinct.
2555   */
2556   p->isDistinct = p->isDistinct || pSub->isDistinct;
2557 
2558   /*
2559   ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
2560   **
2561   ** One is tempted to try to add a and b to combine the limits.  But this
2562   ** does not work if either limit is negative.
2563   */
2564   if( pSub->pLimit ){
2565     p->pLimit = pSub->pLimit;
2566     pSub->pLimit = 0;
2567   }
2568 
2569   /* Finially, delete what is left of the subquery and return
2570   ** success.
2571   */
2572   sqlite3SelectDelete(pSub);
2573   return 1;
2574 }
2575 #endif /* SQLITE_OMIT_VIEW */
2576 
2577 /*
2578 ** Analyze the SELECT statement passed in as an argument to see if it
2579 ** is a simple min() or max() query.  If it is and this query can be
2580 ** satisfied using a single seek to the beginning or end of an index,
2581 ** then generate the code for this SELECT and return 1.  If this is not a
2582 ** simple min() or max() query, then return 0;
2583 **
2584 ** A simply min() or max() query looks like this:
2585 **
2586 **    SELECT min(a) FROM table;
2587 **    SELECT max(a) FROM table;
2588 **
2589 ** The query may have only a single table in its FROM argument.  There
2590 ** can be no GROUP BY or HAVING or WHERE clauses.  The result set must
2591 ** be the min() or max() of a single column of the table.  The column
2592 ** in the min() or max() function must be indexed.
2593 **
2594 ** The parameters to this routine are the same as for sqlite3Select().
2595 ** See the header comment on that routine for additional information.
2596 */
2597 static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
2598   Expr *pExpr;
2599   int iCol;
2600   Table *pTab;
2601   Index *pIdx;
2602   int base;
2603   Vdbe *v;
2604   int seekOp;
2605   ExprList *pEList, *pList, eList;
2606   struct ExprList_item eListItem;
2607   SrcList *pSrc;
2608   int brk;
2609   int iDb;
2610 
2611   /* Check to see if this query is a simple min() or max() query.  Return
2612   ** zero if it is  not.
2613   */
2614   if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
2615   pSrc = p->pSrc;
2616   if( pSrc->nSrc!=1 ) return 0;
2617   pEList = p->pEList;
2618   if( pEList->nExpr!=1 ) return 0;
2619   pExpr = pEList->a[0].pExpr;
2620   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
2621   pList = pExpr->pList;
2622   if( pList==0 || pList->nExpr!=1 ) return 0;
2623   if( pExpr->token.n!=3 ) return 0;
2624   if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){
2625     seekOp = OP_Rewind;
2626   }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
2627     seekOp = OP_Last;
2628   }else{
2629     return 0;
2630   }
2631   pExpr = pList->a[0].pExpr;
2632   if( pExpr->op!=TK_COLUMN ) return 0;
2633   iCol = pExpr->iColumn;
2634   pTab = pSrc->a[0].pTab;
2635 
2636   /* This optimization cannot be used with virtual tables. */
2637   if( IsVirtual(pTab) ) return 0;
2638 
2639   /* If we get to here, it means the query is of the correct form.
2640   ** Check to make sure we have an index and make pIdx point to the
2641   ** appropriate index.  If the min() or max() is on an INTEGER PRIMARY
2642   ** key column, no index is necessary so set pIdx to NULL.  If no
2643   ** usable index is found, return 0.
2644   */
2645   if( iCol<0 ){
2646     pIdx = 0;
2647   }else{
2648     CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
2649     if( pColl==0 ) return 0;
2650     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2651       assert( pIdx->nColumn>=1 );
2652       if( pIdx->aiColumn[0]==iCol &&
2653           0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){
2654         break;
2655       }
2656     }
2657     if( pIdx==0 ) return 0;
2658   }
2659 
2660   /* Identify column types if we will be using the callback.  This
2661   ** step is skipped if the output is going to a table or a memory cell.
2662   ** The column names have already been generated in the calling function.
2663   */
2664   v = sqlite3GetVdbe(pParse);
2665   if( v==0 ) return 0;
2666 
2667   /* If the output is destined for a temporary table, open that table.
2668   */
2669   if( eDest==SRT_EphemTab ){
2670     sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 1);
2671   }
2672 
2673   /* Generating code to find the min or the max.  Basically all we have
2674   ** to do is find the first or the last entry in the chosen index.  If
2675   ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
2676   ** or last entry in the main table.
2677   */
2678   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2679   assert( iDb>=0 || pTab->isEphem );
2680   sqlite3CodeVerifySchema(pParse, iDb);
2681   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
2682   base = pSrc->a[0].iCursor;
2683   brk = sqlite3VdbeMakeLabel(v);
2684   computeLimitRegisters(pParse, p, brk);
2685   if( pSrc->a[0].pSelect==0 ){
2686     sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead);
2687   }
2688   if( pIdx==0 ){
2689     sqlite3VdbeAddOp(v, seekOp, base, 0);
2690   }else{
2691     /* Even though the cursor used to open the index here is closed
2692     ** as soon as a single value has been read from it, allocate it
2693     ** using (pParse->nTab++) to prevent the cursor id from being
2694     ** reused. This is important for statements of the form
2695     ** "INSERT INTO x SELECT max() FROM x".
2696     */
2697     int iIdx;
2698     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
2699     iIdx = pParse->nTab++;
2700     assert( pIdx->pSchema==pTab->pSchema );
2701     sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
2702     sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum,
2703         (char*)pKey, P3_KEYINFO_HANDOFF);
2704     if( seekOp==OP_Rewind ){
2705       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
2706       sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
2707       seekOp = OP_MoveGt;
2708     }
2709     if( pIdx->aSortOrder[0]==SQLITE_SO_DESC ){
2710       /* Ticket #2514: invert the seek operator if we are using
2711       ** a descending index. */
2712       if( seekOp==OP_Last ){
2713         seekOp = OP_Rewind;
2714       }else{
2715         assert( seekOp==OP_MoveGt );
2716         seekOp = OP_MoveLt;
2717       }
2718     }
2719     sqlite3VdbeAddOp(v, seekOp, iIdx, 0);
2720     sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0);
2721     sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
2722     sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
2723   }
2724   eList.nExpr = 1;
2725   memset(&eListItem, 0, sizeof(eListItem));
2726   eList.a = &eListItem;
2727   eList.a[0].pExpr = pExpr;
2728   selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, brk, brk, 0);
2729   sqlite3VdbeResolveLabel(v, brk);
2730   sqlite3VdbeAddOp(v, OP_Close, base, 0);
2731 
2732   return 1;
2733 }
2734 
2735 /*
2736 ** This routine resolves any names used in the result set of the
2737 ** supplied SELECT statement. If the SELECT statement being resolved
2738 ** is a sub-select, then pOuterNC is a pointer to the NameContext
2739 ** of the parent SELECT.
2740 */
2741 int sqlite3SelectResolve(
2742   Parse *pParse,         /* The parser context */
2743   Select *p,             /* The SELECT statement being coded. */
2744   NameContext *pOuterNC  /* The outer name context. May be NULL. */
2745 ){
2746   ExprList *pEList;          /* Result set. */
2747   int i;                     /* For-loop variable used in multiple places */
2748   NameContext sNC;           /* Local name-context */
2749   ExprList *pGroupBy;        /* The group by clause */
2750 
2751   /* If this routine has run before, return immediately. */
2752   if( p->isResolved ){
2753     assert( !pOuterNC );
2754     return SQLITE_OK;
2755   }
2756   p->isResolved = 1;
2757 
2758   /* If there have already been errors, do nothing. */
2759   if( pParse->nErr>0 ){
2760     return SQLITE_ERROR;
2761   }
2762 
2763   /* Prepare the select statement. This call will allocate all cursors
2764   ** required to handle the tables and subqueries in the FROM clause.
2765   */
2766   if( prepSelectStmt(pParse, p) ){
2767     return SQLITE_ERROR;
2768   }
2769 
2770   /* Resolve the expressions in the LIMIT and OFFSET clauses. These
2771   ** are not allowed to refer to any names, so pass an empty NameContext.
2772   */
2773   memset(&sNC, 0, sizeof(sNC));
2774   sNC.pParse = pParse;
2775   if( sqlite3ExprResolveNames(&sNC, p->pLimit) ||
2776       sqlite3ExprResolveNames(&sNC, p->pOffset) ){
2777     return SQLITE_ERROR;
2778   }
2779 
2780   /* Set up the local name-context to pass to ExprResolveNames() to
2781   ** resolve the expression-list.
2782   */
2783   sNC.allowAgg = 1;
2784   sNC.pSrcList = p->pSrc;
2785   sNC.pNext = pOuterNC;
2786 
2787   /* Resolve names in the result set. */
2788   pEList = p->pEList;
2789   if( !pEList ) return SQLITE_ERROR;
2790   for(i=0; i<pEList->nExpr; i++){
2791     Expr *pX = pEList->a[i].pExpr;
2792     if( sqlite3ExprResolveNames(&sNC, pX) ){
2793       return SQLITE_ERROR;
2794     }
2795   }
2796 
2797   /* If there are no aggregate functions in the result-set, and no GROUP BY
2798   ** expression, do not allow aggregates in any of the other expressions.
2799   */
2800   assert( !p->isAgg );
2801   pGroupBy = p->pGroupBy;
2802   if( pGroupBy || sNC.hasAgg ){
2803     p->isAgg = 1;
2804   }else{
2805     sNC.allowAgg = 0;
2806   }
2807 
2808   /* If a HAVING clause is present, then there must be a GROUP BY clause.
2809   */
2810   if( p->pHaving && !pGroupBy ){
2811     sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
2812     return SQLITE_ERROR;
2813   }
2814 
2815   /* Add the expression list to the name-context before parsing the
2816   ** other expressions in the SELECT statement. This is so that
2817   ** expressions in the WHERE clause (etc.) can refer to expressions by
2818   ** aliases in the result set.
2819   **
2820   ** Minor point: If this is the case, then the expression will be
2821   ** re-evaluated for each reference to it.
2822   */
2823   sNC.pEList = p->pEList;
2824   if( sqlite3ExprResolveNames(&sNC, p->pWhere) ||
2825      sqlite3ExprResolveNames(&sNC, p->pHaving) ){
2826     return SQLITE_ERROR;
2827   }
2828   if( p->pPrior==0 ){
2829     if( processOrderGroupBy(pParse, p, p->pOrderBy, 1, &sNC.hasAgg) ){
2830       return SQLITE_ERROR;
2831     }
2832   }
2833   if( processOrderGroupBy(pParse, p, pGroupBy, 0, &sNC.hasAgg) ){
2834     return SQLITE_ERROR;
2835   }
2836 
2837   if( pParse->db->mallocFailed ){
2838     return SQLITE_NOMEM;
2839   }
2840 
2841   /* Make sure the GROUP BY clause does not contain aggregate functions.
2842   */
2843   if( pGroupBy ){
2844     struct ExprList_item *pItem;
2845 
2846     for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
2847       if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
2848         sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
2849             "the GROUP BY clause");
2850         return SQLITE_ERROR;
2851       }
2852     }
2853   }
2854 
2855   /* If this is one SELECT of a compound, be sure to resolve names
2856   ** in the other SELECTs.
2857   */
2858   if( p->pPrior ){
2859     return sqlite3SelectResolve(pParse, p->pPrior, pOuterNC);
2860   }else{
2861     return SQLITE_OK;
2862   }
2863 }
2864 
2865 /*
2866 ** Reset the aggregate accumulator.
2867 **
2868 ** The aggregate accumulator is a set of memory cells that hold
2869 ** intermediate results while calculating an aggregate.  This
2870 ** routine simply stores NULLs in all of those memory cells.
2871 */
2872 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
2873   Vdbe *v = pParse->pVdbe;
2874   int i;
2875   struct AggInfo_func *pFunc;
2876   if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
2877     return;
2878   }
2879   for(i=0; i<pAggInfo->nColumn; i++){
2880     sqlite3VdbeAddOp(v, OP_MemNull, pAggInfo->aCol[i].iMem, 0);
2881   }
2882   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
2883     sqlite3VdbeAddOp(v, OP_MemNull, pFunc->iMem, 0);
2884     if( pFunc->iDistinct>=0 ){
2885       Expr *pE = pFunc->pExpr;
2886       if( pE->pList==0 || pE->pList->nExpr!=1 ){
2887         sqlite3ErrorMsg(pParse, "DISTINCT in aggregate must be followed "
2888            "by an expression");
2889         pFunc->iDistinct = -1;
2890       }else{
2891         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->pList);
2892         sqlite3VdbeOp3(v, OP_OpenEphemeral, pFunc->iDistinct, 0,
2893                           (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
2894       }
2895     }
2896   }
2897 }
2898 
2899 /*
2900 ** Invoke the OP_AggFinalize opcode for every aggregate function
2901 ** in the AggInfo structure.
2902 */
2903 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
2904   Vdbe *v = pParse->pVdbe;
2905   int i;
2906   struct AggInfo_func *pF;
2907   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
2908     ExprList *pList = pF->pExpr->pList;
2909     sqlite3VdbeOp3(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0,
2910                       (void*)pF->pFunc, P3_FUNCDEF);
2911   }
2912 }
2913 
2914 /*
2915 ** Update the accumulator memory cells for an aggregate based on
2916 ** the current cursor position.
2917 */
2918 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
2919   Vdbe *v = pParse->pVdbe;
2920   int i;
2921   struct AggInfo_func *pF;
2922   struct AggInfo_col *pC;
2923 
2924   pAggInfo->directMode = 1;
2925   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
2926     int nArg;
2927     int addrNext = 0;
2928     ExprList *pList = pF->pExpr->pList;
2929     if( pList ){
2930       nArg = pList->nExpr;
2931       sqlite3ExprCodeExprList(pParse, pList);
2932     }else{
2933       nArg = 0;
2934     }
2935     if( pF->iDistinct>=0 ){
2936       addrNext = sqlite3VdbeMakeLabel(v);
2937       assert( nArg==1 );
2938       codeDistinct(v, pF->iDistinct, addrNext, 1);
2939     }
2940     if( pF->pFunc->needCollSeq ){
2941       CollSeq *pColl = 0;
2942       struct ExprList_item *pItem;
2943       int j;
2944       assert( pList!=0 );  /* pList!=0 if pF->pFunc->needCollSeq is true */
2945       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
2946         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
2947       }
2948       if( !pColl ){
2949         pColl = pParse->db->pDfltColl;
2950       }
2951       sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
2952     }
2953     sqlite3VdbeOp3(v, OP_AggStep, pF->iMem, nArg, (void*)pF->pFunc, P3_FUNCDEF);
2954     if( addrNext ){
2955       sqlite3VdbeResolveLabel(v, addrNext);
2956     }
2957   }
2958   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
2959     sqlite3ExprCode(pParse, pC->pExpr);
2960     sqlite3VdbeAddOp(v, OP_MemStore, pC->iMem, 1);
2961   }
2962   pAggInfo->directMode = 0;
2963 }
2964 
2965 
2966 /*
2967 ** Generate code for the given SELECT statement.
2968 **
2969 ** The results are distributed in various ways depending on the
2970 ** value of eDest and iParm.
2971 **
2972 **     eDest Value       Result
2973 **     ------------    -------------------------------------------
2974 **     SRT_Callback    Invoke the callback for each row of the result.
2975 **
2976 **     SRT_Mem         Store first result in memory cell iParm
2977 **
2978 **     SRT_Set         Store results as keys of table iParm.
2979 **
2980 **     SRT_Union       Store results as a key in a temporary table iParm
2981 **
2982 **     SRT_Except      Remove results from the temporary table iParm.
2983 **
2984 **     SRT_Table       Store results in temporary table iParm
2985 **
2986 ** The table above is incomplete.  Additional eDist value have be added
2987 ** since this comment was written.  See the selectInnerLoop() function for
2988 ** a complete listing of the allowed values of eDest and their meanings.
2989 **
2990 ** This routine returns the number of errors.  If any errors are
2991 ** encountered, then an appropriate error message is left in
2992 ** pParse->zErrMsg.
2993 **
2994 ** This routine does NOT free the Select structure passed in.  The
2995 ** calling function needs to do that.
2996 **
2997 ** The pParent, parentTab, and *pParentAgg fields are filled in if this
2998 ** SELECT is a subquery.  This routine may try to combine this SELECT
2999 ** with its parent to form a single flat query.  In so doing, it might
3000 ** change the parent query from a non-aggregate to an aggregate query.
3001 ** For that reason, the pParentAgg flag is passed as a pointer, so it
3002 ** can be changed.
3003 **
3004 ** Example 1:   The meaning of the pParent parameter.
3005 **
3006 **    SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
3007 **    \                      \_______ subquery _______/        /
3008 **     \                                                      /
3009 **      \____________________ outer query ___________________/
3010 **
3011 ** This routine is called for the outer query first.   For that call,
3012 ** pParent will be NULL.  During the processing of the outer query, this
3013 ** routine is called recursively to handle the subquery.  For the recursive
3014 ** call, pParent will point to the outer query.  Because the subquery is
3015 ** the second element in a three-way join, the parentTab parameter will
3016 ** be 1 (the 2nd value of a 0-indexed array.)
3017 */
3018 int sqlite3Select(
3019   Parse *pParse,         /* The parser context */
3020   Select *p,             /* The SELECT statement being coded. */
3021   int eDest,             /* How to dispose of the results */
3022   int iParm,             /* A parameter used by the eDest disposal method */
3023   Select *pParent,       /* Another SELECT for which this is a sub-query */
3024   int parentTab,         /* Index in pParent->pSrc of this query */
3025   int *pParentAgg,       /* True if pParent uses aggregate functions */
3026   char *aff              /* If eDest is SRT_Union, the affinity string */
3027 ){
3028   int i, j;              /* Loop counters */
3029   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
3030   Vdbe *v;               /* The virtual machine under construction */
3031   int isAgg;             /* True for select lists like "count(*)" */
3032   ExprList *pEList;      /* List of columns to extract. */
3033   SrcList *pTabList;     /* List of tables to select from */
3034   Expr *pWhere;          /* The WHERE clause.  May be NULL */
3035   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
3036   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
3037   Expr *pHaving;         /* The HAVING clause.  May be NULL */
3038   int isDistinct;        /* True if the DISTINCT keyword is present */
3039   int distinct;          /* Table to use for the distinct set */
3040   int rc = 1;            /* Value to return from this function */
3041   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
3042   AggInfo sAggInfo;      /* Information used by aggregate queries */
3043   int iEnd;              /* Address of the end of the query */
3044   sqlite3 *db;           /* The database connection */
3045 
3046   db = pParse->db;
3047   if( p==0 || db->mallocFailed || pParse->nErr ){
3048     return 1;
3049   }
3050   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
3051   memset(&sAggInfo, 0, sizeof(sAggInfo));
3052 
3053   pOrderBy = p->pOrderBy;
3054   if( IgnorableOrderby(eDest) ){
3055     p->pOrderBy = 0;
3056   }
3057   if( sqlite3SelectResolve(pParse, p, 0) ){
3058     goto select_end;
3059   }
3060   p->pOrderBy = pOrderBy;
3061 
3062 #ifndef SQLITE_OMIT_COMPOUND_SELECT
3063   /* If there is are a sequence of queries, do the earlier ones first.
3064   */
3065   if( p->pPrior ){
3066     if( p->pRightmost==0 ){
3067       Select *pLoop, *pRight = 0;
3068       int cnt = 0;
3069       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
3070         pLoop->pRightmost = p;
3071         pLoop->pNext = pRight;
3072         pRight = pLoop;
3073       }
3074       if( SQLITE_MAX_COMPOUND_SELECT>0 && cnt>SQLITE_MAX_COMPOUND_SELECT ){
3075         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
3076         return 1;
3077       }
3078     }
3079     return multiSelect(pParse, p, eDest, iParm, aff);
3080   }
3081 #endif
3082 
3083   /* Make local copies of the parameters for this query.
3084   */
3085   pTabList = p->pSrc;
3086   pWhere = p->pWhere;
3087   pGroupBy = p->pGroupBy;
3088   pHaving = p->pHaving;
3089   isAgg = p->isAgg;
3090   isDistinct = p->isDistinct;
3091   pEList = p->pEList;
3092   if( pEList==0 ) goto select_end;
3093 
3094   /*
3095   ** Do not even attempt to generate any code if we have already seen
3096   ** errors before this routine starts.
3097   */
3098   if( pParse->nErr>0 ) goto select_end;
3099 
3100   /* If writing to memory or generating a set
3101   ** only a single column may be output.
3102   */
3103 #ifndef SQLITE_OMIT_SUBQUERY
3104   if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){
3105     goto select_end;
3106   }
3107 #endif
3108 
3109   /* ORDER BY is ignored for some destinations.
3110   */
3111   if( IgnorableOrderby(eDest) ){
3112     pOrderBy = 0;
3113   }
3114 
3115   /* Begin generating code.
3116   */
3117   v = sqlite3GetVdbe(pParse);
3118   if( v==0 ) goto select_end;
3119 
3120   /* Generate code for all sub-queries in the FROM clause
3121   */
3122 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
3123   for(i=0; i<pTabList->nSrc; i++){
3124     const char *zSavedAuthContext = 0;
3125     int needRestoreContext;
3126     struct SrcList_item *pItem = &pTabList->a[i];
3127 
3128     if( pItem->pSelect==0 || pItem->isPopulated ) continue;
3129     if( pItem->zName!=0 ){
3130       zSavedAuthContext = pParse->zAuthContext;
3131       pParse->zAuthContext = pItem->zName;
3132       needRestoreContext = 1;
3133     }else{
3134       needRestoreContext = 0;
3135     }
3136 #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
3137     /* Increment Parse.nHeight by the height of the largest expression
3138     ** tree refered to by this, the parent select. The child select
3139     ** may contain expression trees of at most
3140     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
3141     ** more conservative than necessary, but much easier than enforcing
3142     ** an exact limit.
3143     */
3144     pParse->nHeight += sqlite3SelectExprHeight(p);
3145 #endif
3146     sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab,
3147                  pItem->iCursor, p, i, &isAgg, 0);
3148     if( db->mallocFailed ){
3149       goto select_end;
3150     }
3151 #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
3152     pParse->nHeight -= sqlite3SelectExprHeight(p);
3153 #endif
3154     if( needRestoreContext ){
3155       pParse->zAuthContext = zSavedAuthContext;
3156     }
3157     pTabList = p->pSrc;
3158     pWhere = p->pWhere;
3159     if( !IgnorableOrderby(eDest) ){
3160       pOrderBy = p->pOrderBy;
3161     }
3162     pGroupBy = p->pGroupBy;
3163     pHaving = p->pHaving;
3164     isDistinct = p->isDistinct;
3165   }
3166 #endif
3167 
3168   /* Check for the special case of a min() or max() function by itself
3169   ** in the result set.
3170   */
3171   if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
3172     rc = 0;
3173     goto select_end;
3174   }
3175 
3176   /* Check to see if this is a subquery that can be "flattened" into its parent.
3177   ** If flattening is a possiblity, do so and return immediately.
3178   */
3179 #ifndef SQLITE_OMIT_VIEW
3180   if( pParent && pParentAgg &&
3181       flattenSubquery(db, pParent, parentTab, *pParentAgg, isAgg) ){
3182     if( isAgg ) *pParentAgg = 1;
3183     goto select_end;
3184   }
3185 #endif
3186 
3187   /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
3188   ** GROUP BY may use an index, DISTINCT never does.
3189   */
3190   if( p->isDistinct && !p->isAgg && !p->pGroupBy ){
3191     p->pGroupBy = sqlite3ExprListDup(db, p->pEList);
3192     pGroupBy = p->pGroupBy;
3193     p->isDistinct = 0;
3194     isDistinct = 0;
3195   }
3196 
3197   /* If there is an ORDER BY clause, then this sorting
3198   ** index might end up being unused if the data can be
3199   ** extracted in pre-sorted order.  If that is the case, then the
3200   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
3201   ** we figure out that the sorting index is not needed.  The addrSortIndex
3202   ** variable is used to facilitate that change.
3203   */
3204   if( pOrderBy ){
3205     KeyInfo *pKeyInfo;
3206     if( pParse->nErr ){
3207       goto select_end;
3208     }
3209     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
3210     pOrderBy->iECursor = pParse->nTab++;
3211     p->addrOpenEphm[2] = addrSortIndex =
3212       sqlite3VdbeOp3(v, OP_OpenEphemeral, pOrderBy->iECursor, pOrderBy->nExpr+2,                     (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
3213   }else{
3214     addrSortIndex = -1;
3215   }
3216 
3217   /* If the output is destined for a temporary table, open that table.
3218   */
3219   if( eDest==SRT_EphemTab ){
3220     sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, pEList->nExpr);
3221   }
3222 
3223   /* Set the limiter.
3224   */
3225   iEnd = sqlite3VdbeMakeLabel(v);
3226   computeLimitRegisters(pParse, p, iEnd);
3227 
3228   /* Open a virtual index to use for the distinct set.
3229   */
3230   if( isDistinct ){
3231     KeyInfo *pKeyInfo;
3232     assert( isAgg || pGroupBy );
3233     distinct = pParse->nTab++;
3234     pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
3235     sqlite3VdbeOp3(v, OP_OpenEphemeral, distinct, 0,
3236                         (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
3237   }else{
3238     distinct = -1;
3239   }
3240 
3241   /* Aggregate and non-aggregate queries are handled differently */
3242   if( !isAgg && pGroupBy==0 ){
3243     /* This case is for non-aggregate queries
3244     ** Begin the database scan
3245     */
3246     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy);
3247     if( pWInfo==0 ) goto select_end;
3248 
3249     /* If sorting index that was created by a prior OP_OpenEphemeral
3250     ** instruction ended up not being needed, then change the OP_OpenEphemeral
3251     ** into an OP_Noop.
3252     */
3253     if( addrSortIndex>=0 && pOrderBy==0 ){
3254       sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
3255       p->addrOpenEphm[2] = -1;
3256     }
3257 
3258     /* Use the standard inner loop
3259     */
3260     assert(!isDistinct);
3261     if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, eDest,
3262                     iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){
3263        goto select_end;
3264     }
3265 
3266     /* End the database scan loop.
3267     */
3268     sqlite3WhereEnd(pWInfo);
3269   }else{
3270     /* This is the processing for aggregate queries */
3271     NameContext sNC;    /* Name context for processing aggregate information */
3272     int iAMem;          /* First Mem address for storing current GROUP BY */
3273     int iBMem;          /* First Mem address for previous GROUP BY */
3274     int iUseFlag;       /* Mem address holding flag indicating that at least
3275                         ** one row of the input to the aggregator has been
3276                         ** processed */
3277     int iAbortFlag;     /* Mem address which causes query abort if positive */
3278     int groupBySort;    /* Rows come from source in GROUP BY order */
3279 
3280 
3281     /* The following variables hold addresses or labels for parts of the
3282     ** virtual machine program we are putting together */
3283     int addrOutputRow;      /* Start of subroutine that outputs a result row */
3284     int addrSetAbort;       /* Set the abort flag and return */
3285     int addrInitializeLoop; /* Start of code that initializes the input loop */
3286     int addrTopOfLoop;      /* Top of the input loop */
3287     int addrGroupByChange;  /* Code that runs when any GROUP BY term changes */
3288     int addrProcessRow;     /* Code to process a single input row */
3289     int addrEnd;            /* End of all processing */
3290     int addrSortingIdx;     /* The OP_OpenEphemeral for the sorting index */
3291     int addrReset;          /* Subroutine for resetting the accumulator */
3292 
3293     addrEnd = sqlite3VdbeMakeLabel(v);
3294 
3295     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
3296     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
3297     ** SELECT statement.
3298     */
3299     memset(&sNC, 0, sizeof(sNC));
3300     sNC.pParse = pParse;
3301     sNC.pSrcList = pTabList;
3302     sNC.pAggInfo = &sAggInfo;
3303     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
3304     sAggInfo.pGroupBy = pGroupBy;
3305     if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){
3306       goto select_end;
3307     }
3308     if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){
3309       goto select_end;
3310     }
3311     if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){
3312       goto select_end;
3313     }
3314     sAggInfo.nAccumulator = sAggInfo.nColumn;
3315     for(i=0; i<sAggInfo.nFunc; i++){
3316       if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){
3317         goto select_end;
3318       }
3319     }
3320     if( db->mallocFailed ) goto select_end;
3321 
3322     /* Processing for aggregates with GROUP BY is very different and
3323     ** much more complex than aggregates without a GROUP BY.
3324     */
3325     if( pGroupBy ){
3326       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
3327 
3328       /* Create labels that we will be needing
3329       */
3330 
3331       addrInitializeLoop = sqlite3VdbeMakeLabel(v);
3332       addrGroupByChange = sqlite3VdbeMakeLabel(v);
3333       addrProcessRow = sqlite3VdbeMakeLabel(v);
3334 
3335       /* If there is a GROUP BY clause we might need a sorting index to
3336       ** implement it.  Allocate that sorting index now.  If it turns out
3337       ** that we do not need it after all, the OpenEphemeral instruction
3338       ** will be converted into a Noop.
3339       */
3340       sAggInfo.sortingIdx = pParse->nTab++;
3341       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
3342       addrSortingIdx =
3343           sqlite3VdbeOp3(v, OP_OpenEphemeral, sAggInfo.sortingIdx,
3344                          sAggInfo.nSortingColumn,
3345                          (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
3346 
3347       /* Initialize memory locations used by GROUP BY aggregate processing
3348       */
3349       iUseFlag = pParse->nMem++;
3350       iAbortFlag = pParse->nMem++;
3351       iAMem = pParse->nMem;
3352       pParse->nMem += pGroupBy->nExpr;
3353       iBMem = pParse->nMem;
3354       pParse->nMem += pGroupBy->nExpr;
3355       sqlite3VdbeAddOp(v, OP_MemInt, 0, iAbortFlag);
3356       VdbeComment((v, "# clear abort flag"));
3357       sqlite3VdbeAddOp(v, OP_MemInt, 0, iUseFlag);
3358       VdbeComment((v, "# indicate accumulator empty"));
3359       sqlite3VdbeAddOp(v, OP_Goto, 0, addrInitializeLoop);
3360 
3361       /* Generate a subroutine that outputs a single row of the result
3362       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
3363       ** is less than or equal to zero, the subroutine is a no-op.  If
3364       ** the processing calls for the query to abort, this subroutine
3365       ** increments the iAbortFlag memory location before returning in
3366       ** order to signal the caller to abort.
3367       */
3368       addrSetAbort = sqlite3VdbeCurrentAddr(v);
3369       sqlite3VdbeAddOp(v, OP_MemInt, 1, iAbortFlag);
3370       VdbeComment((v, "# set abort flag"));
3371       sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3372       addrOutputRow = sqlite3VdbeCurrentAddr(v);
3373       sqlite3VdbeAddOp(v, OP_IfMemPos, iUseFlag, addrOutputRow+2);
3374       VdbeComment((v, "# Groupby result generator entry point"));
3375       sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3376       finalizeAggFunctions(pParse, &sAggInfo);
3377       if( pHaving ){
3378         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, 1);
3379       }
3380       rc = selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
3381                            distinct, eDest, iParm,
3382                            addrOutputRow+1, addrSetAbort, aff);
3383       if( rc ){
3384         goto select_end;
3385       }
3386       sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3387       VdbeComment((v, "# end groupby result generator"));
3388 
3389       /* Generate a subroutine that will reset the group-by accumulator
3390       */
3391       addrReset = sqlite3VdbeCurrentAddr(v);
3392       resetAccumulator(pParse, &sAggInfo);
3393       sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3394 
3395       /* Begin a loop that will extract all source rows in GROUP BY order.
3396       ** This might involve two separate loops with an OP_Sort in between, or
3397       ** it might be a single loop that uses an index to extract information
3398       ** in the right order to begin with.
3399       */
3400       sqlite3VdbeResolveLabel(v, addrInitializeLoop);
3401       sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset);
3402       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy);
3403       if( pWInfo==0 ) goto select_end;
3404       if( pGroupBy==0 ){
3405         /* The optimizer is able to deliver rows in group by order so
3406         ** we do not have to sort.  The OP_OpenEphemeral table will be
3407         ** cancelled later because we still need to use the pKeyInfo
3408         */
3409         pGroupBy = p->pGroupBy;
3410         groupBySort = 0;
3411       }else{
3412         /* Rows are coming out in undetermined order.  We have to push
3413         ** each row into a sorting index, terminate the first loop,
3414         ** then loop over the sorting index in order to get the output
3415         ** in sorted order
3416         */
3417         groupBySort = 1;
3418         sqlite3ExprCodeExprList(pParse, pGroupBy);
3419         sqlite3VdbeAddOp(v, OP_Sequence, sAggInfo.sortingIdx, 0);
3420         j = pGroupBy->nExpr+1;
3421         for(i=0; i<sAggInfo.nColumn; i++){
3422           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
3423           if( pCol->iSorterColumn<j ) continue;
3424           sqlite3ExprCodeGetColumn(v, pCol->pTab, pCol->iColumn, pCol->iTable);
3425           j++;
3426         }
3427         sqlite3VdbeAddOp(v, OP_MakeRecord, j, 0);
3428         sqlite3VdbeAddOp(v, OP_IdxInsert, sAggInfo.sortingIdx, 0);
3429         sqlite3WhereEnd(pWInfo);
3430         sqlite3VdbeAddOp(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
3431         VdbeComment((v, "# GROUP BY sort"));
3432         sAggInfo.useSortingIdx = 1;
3433       }
3434 
3435       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
3436       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
3437       ** Then compare the current GROUP BY terms against the GROUP BY terms
3438       ** from the previous row currently stored in a0, a1, a2...
3439       */
3440       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
3441       for(j=0; j<pGroupBy->nExpr; j++){
3442         if( groupBySort ){
3443           sqlite3VdbeAddOp(v, OP_Column, sAggInfo.sortingIdx, j);
3444         }else{
3445           sAggInfo.directMode = 1;
3446           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr);
3447         }
3448         sqlite3VdbeAddOp(v, OP_MemStore, iBMem+j, j<pGroupBy->nExpr-1);
3449       }
3450       for(j=pGroupBy->nExpr-1; j>=0; j--){
3451         if( j<pGroupBy->nExpr-1 ){
3452           sqlite3VdbeAddOp(v, OP_MemLoad, iBMem+j, 0);
3453         }
3454         sqlite3VdbeAddOp(v, OP_MemLoad, iAMem+j, 0);
3455         if( j==0 ){
3456           sqlite3VdbeAddOp(v, OP_Eq, 0x200, addrProcessRow);
3457         }else{
3458           sqlite3VdbeAddOp(v, OP_Ne, 0x200, addrGroupByChange);
3459         }
3460         sqlite3VdbeChangeP3(v, -1, (void*)pKeyInfo->aColl[j], P3_COLLSEQ);
3461       }
3462 
3463       /* Generate code that runs whenever the GROUP BY changes.
3464       ** Change in the GROUP BY are detected by the previous code
3465       ** block.  If there were no changes, this block is skipped.
3466       **
3467       ** This code copies current group by terms in b0,b1,b2,...
3468       ** over to a0,a1,a2.  It then calls the output subroutine
3469       ** and resets the aggregate accumulator registers in preparation
3470       ** for the next GROUP BY batch.
3471       */
3472       sqlite3VdbeResolveLabel(v, addrGroupByChange);
3473       for(j=0; j<pGroupBy->nExpr; j++){
3474         sqlite3VdbeAddOp(v, OP_MemMove, iAMem+j, iBMem+j);
3475       }
3476       sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow);
3477       VdbeComment((v, "# output one row"));
3478       sqlite3VdbeAddOp(v, OP_IfMemPos, iAbortFlag, addrEnd);
3479       VdbeComment((v, "# check abort flag"));
3480       sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset);
3481       VdbeComment((v, "# reset accumulator"));
3482 
3483       /* Update the aggregate accumulators based on the content of
3484       ** the current row
3485       */
3486       sqlite3VdbeResolveLabel(v, addrProcessRow);
3487       updateAccumulator(pParse, &sAggInfo);
3488       sqlite3VdbeAddOp(v, OP_MemInt, 1, iUseFlag);
3489       VdbeComment((v, "# indicate data in accumulator"));
3490 
3491       /* End of the loop
3492       */
3493       if( groupBySort ){
3494         sqlite3VdbeAddOp(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
3495       }else{
3496         sqlite3WhereEnd(pWInfo);
3497         sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
3498       }
3499 
3500       /* Output the final row of result
3501       */
3502       sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow);
3503       VdbeComment((v, "# output final row"));
3504 
3505     } /* endif pGroupBy */
3506     else {
3507       /* This case runs if the aggregate has no GROUP BY clause.  The
3508       ** processing is much simpler since there is only a single row
3509       ** of output.
3510       */
3511       resetAccumulator(pParse, &sAggInfo);
3512       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
3513       if( pWInfo==0 ) goto select_end;
3514       updateAccumulator(pParse, &sAggInfo);
3515       sqlite3WhereEnd(pWInfo);
3516       finalizeAggFunctions(pParse, &sAggInfo);
3517       pOrderBy = 0;
3518       if( pHaving ){
3519         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, 1);
3520       }
3521       selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
3522                       eDest, iParm, addrEnd, addrEnd, aff);
3523     }
3524     sqlite3VdbeResolveLabel(v, addrEnd);
3525 
3526   } /* endif aggregate query */
3527 
3528   /* If there is an ORDER BY clause, then we need to sort the results
3529   ** and send them to the callback one by one.
3530   */
3531   if( pOrderBy ){
3532     generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm);
3533   }
3534 
3535 #ifndef SQLITE_OMIT_SUBQUERY
3536   /* If this was a subquery, we have now converted the subquery into a
3537   ** temporary table.  So set the SrcList_item.isPopulated flag to prevent
3538   ** this subquery from being evaluated again and to force the use of
3539   ** the temporary table.
3540   */
3541   if( pParent ){
3542     assert( pParent->pSrc->nSrc>parentTab );
3543     assert( pParent->pSrc->a[parentTab].pSelect==p );
3544     pParent->pSrc->a[parentTab].isPopulated = 1;
3545   }
3546 #endif
3547 
3548   /* Jump here to skip this query
3549   */
3550   sqlite3VdbeResolveLabel(v, iEnd);
3551 
3552   /* The SELECT was successfully coded.   Set the return code to 0
3553   ** to indicate no errors.
3554   */
3555   rc = 0;
3556 
3557   /* Control jumps to here if an error is encountered above, or upon
3558   ** successful coding of the SELECT.
3559   */
3560 select_end:
3561 
3562   /* Identify column names if we will be using them in a callback.  This
3563   ** step is skipped if the output is going to some other destination.
3564   */
3565   if( rc==SQLITE_OK && eDest==SRT_Callback ){
3566     generateColumnNames(pParse, pTabList, pEList);
3567   }
3568 
3569   sqlite3_free(sAggInfo.aCol);
3570   sqlite3_free(sAggInfo.aFunc);
3571   return rc;
3572 }
3573 
3574 #if defined(SQLITE_DEBUG)
3575 /*
3576 *******************************************************************************
3577 ** The following code is used for testing and debugging only.  The code
3578 ** that follows does not appear in normal builds.
3579 **
3580 ** These routines are used to print out the content of all or part of a
3581 ** parse structures such as Select or Expr.  Such printouts are useful
3582 ** for helping to understand what is happening inside the code generator
3583 ** during the execution of complex SELECT statements.
3584 **
3585 ** These routine are not called anywhere from within the normal
3586 ** code base.  Then are intended to be called from within the debugger
3587 ** or from temporary "printf" statements inserted for debugging.
3588 */
3589 void sqlite3PrintExpr(Expr *p){
3590   if( p->token.z && p->token.n>0 ){
3591     sqlite3DebugPrintf("(%.*s", p->token.n, p->token.z);
3592   }else{
3593     sqlite3DebugPrintf("(%d", p->op);
3594   }
3595   if( p->pLeft ){
3596     sqlite3DebugPrintf(" ");
3597     sqlite3PrintExpr(p->pLeft);
3598   }
3599   if( p->pRight ){
3600     sqlite3DebugPrintf(" ");
3601     sqlite3PrintExpr(p->pRight);
3602   }
3603   sqlite3DebugPrintf(")");
3604 }
3605 void sqlite3PrintExprList(ExprList *pList){
3606   int i;
3607   for(i=0; i<pList->nExpr; i++){
3608     sqlite3PrintExpr(pList->a[i].pExpr);
3609     if( i<pList->nExpr-1 ){
3610       sqlite3DebugPrintf(", ");
3611     }
3612   }
3613 }
3614 void sqlite3PrintSelect(Select *p, int indent){
3615   sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
3616   sqlite3PrintExprList(p->pEList);
3617   sqlite3DebugPrintf("\n");
3618   if( p->pSrc ){
3619     char *zPrefix;
3620     int i;
3621     zPrefix = "FROM";
3622     for(i=0; i<p->pSrc->nSrc; i++){
3623       struct SrcList_item *pItem = &p->pSrc->a[i];
3624       sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
3625       zPrefix = "";
3626       if( pItem->pSelect ){
3627         sqlite3DebugPrintf("(\n");
3628         sqlite3PrintSelect(pItem->pSelect, indent+10);
3629         sqlite3DebugPrintf("%*s)", indent+8, "");
3630       }else if( pItem->zName ){
3631         sqlite3DebugPrintf("%s", pItem->zName);
3632       }
3633       if( pItem->pTab ){
3634         sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
3635       }
3636       if( pItem->zAlias ){
3637         sqlite3DebugPrintf(" AS %s", pItem->zAlias);
3638       }
3639       if( i<p->pSrc->nSrc-1 ){
3640         sqlite3DebugPrintf(",");
3641       }
3642       sqlite3DebugPrintf("\n");
3643     }
3644   }
3645   if( p->pWhere ){
3646     sqlite3DebugPrintf("%*s WHERE ", indent, "");
3647     sqlite3PrintExpr(p->pWhere);
3648     sqlite3DebugPrintf("\n");
3649   }
3650   if( p->pGroupBy ){
3651     sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
3652     sqlite3PrintExprList(p->pGroupBy);
3653     sqlite3DebugPrintf("\n");
3654   }
3655   if( p->pHaving ){
3656     sqlite3DebugPrintf("%*s HAVING ", indent, "");
3657     sqlite3PrintExpr(p->pHaving);
3658     sqlite3DebugPrintf("\n");
3659   }
3660   if( p->pOrderBy ){
3661     sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
3662     sqlite3PrintExprList(p->pOrderBy);
3663     sqlite3DebugPrintf("\n");
3664   }
3665 }
3666 /* End of the structure debug printing code
3667 *****************************************************************************/
3668 #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
3669