xref: /sqlite-3.40.0/src/select.c (revision da93281e)
1cce7d176Sdrh /*
2cce7d176Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp
3cce7d176Sdrh **
4cce7d176Sdrh ** This program is free software; you can redistribute it and/or
5cce7d176Sdrh ** modify it under the terms of the GNU General Public
6cce7d176Sdrh ** License as published by the Free Software Foundation; either
7cce7d176Sdrh ** version 2 of the License, or (at your option) any later version.
8cce7d176Sdrh **
9cce7d176Sdrh ** This program is distributed in the hope that it will be useful,
10cce7d176Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11cce7d176Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cce7d176Sdrh ** General Public License for more details.
13cce7d176Sdrh **
14cce7d176Sdrh ** You should have received a copy of the GNU General Public
15cce7d176Sdrh ** License along with this library; if not, write to the
16cce7d176Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17cce7d176Sdrh ** Boston, MA  02111-1307, USA.
18cce7d176Sdrh **
19cce7d176Sdrh ** Author contact information:
20cce7d176Sdrh **   [email protected]
21cce7d176Sdrh **   http://www.hwaci.com/drh/
22cce7d176Sdrh **
23cce7d176Sdrh *************************************************************************
24cce7d176Sdrh ** This file contains C code routines that are called by the parser
25cce7d176Sdrh ** to handle SELECT statements.
26cce7d176Sdrh **
27*da93281eSdrh ** $Id: select.c,v 1.12 2000/06/06 18:00:16 drh Exp $
28cce7d176Sdrh */
29cce7d176Sdrh #include "sqliteInt.h"
30cce7d176Sdrh 
31cce7d176Sdrh /*
329bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that
339bb61fe7Sdrh ** structure.
34cce7d176Sdrh */
359bb61fe7Sdrh Select *sqliteSelectNew(
369bb61fe7Sdrh   ExprList *pEList,
379bb61fe7Sdrh   IdList *pSrc,
389bb61fe7Sdrh   Expr *pWhere,
399bb61fe7Sdrh   ExprList *pGroupBy,
409bb61fe7Sdrh   Expr *pHaving,
419bb61fe7Sdrh   ExprList *pOrderBy,
429bb61fe7Sdrh   int isDistinct
439bb61fe7Sdrh ){
449bb61fe7Sdrh   Select *pNew;
459bb61fe7Sdrh   pNew = sqliteMalloc( sizeof(*pNew) );
469bb61fe7Sdrh   if( pNew==0 ) return 0;
479bb61fe7Sdrh   pNew->pEList = pEList;
489bb61fe7Sdrh   pNew->pSrc = pSrc;
499bb61fe7Sdrh   pNew->pWhere = pWhere;
509bb61fe7Sdrh   pNew->pGroupBy = pGroupBy;
519bb61fe7Sdrh   pNew->pHaving = pHaving;
529bb61fe7Sdrh   pNew->pOrderBy = pOrderBy;
539bb61fe7Sdrh   pNew->isDistinct = isDistinct;
549bb61fe7Sdrh   return pNew;
559bb61fe7Sdrh }
569bb61fe7Sdrh 
579bb61fe7Sdrh /*
589bb61fe7Sdrh ** Delete the given Select structure and all of its substructures.
599bb61fe7Sdrh */
609bb61fe7Sdrh void sqliteSelectDelete(Select *p){
619bb61fe7Sdrh   sqliteExprListDelete(p->pEList);
629bb61fe7Sdrh   sqliteIdListDelete(p->pSrc);
639bb61fe7Sdrh   sqliteExprDelete(p->pWhere);
649bb61fe7Sdrh   sqliteExprListDelete(p->pGroupBy);
659bb61fe7Sdrh   sqliteExprDelete(p->pHaving);
669bb61fe7Sdrh   sqliteExprListDelete(p->pOrderBy);
679bb61fe7Sdrh   sqliteFree(p);
689bb61fe7Sdrh }
699bb61fe7Sdrh 
709bb61fe7Sdrh /*
712282792aSdrh ** Delete the aggregate information from the parse structure.
722282792aSdrh */
732282792aSdrh void sqliteParseInfoReset(Parse *pParse){
742282792aSdrh   sqliteFree(pParse->aAgg);
752282792aSdrh   pParse->aAgg = 0;
762282792aSdrh   pParse->nAgg = 0;
772282792aSdrh   pParse->iAggCount = -1;
782282792aSdrh   pParse->useAgg = 0;
792282792aSdrh }
802282792aSdrh 
812282792aSdrh /*
822282792aSdrh ** This routine generates the code for the inside of the inner loop
832282792aSdrh ** of a SELECT.
842282792aSdrh */
852282792aSdrh static int selectInnerLoop(
862282792aSdrh   Parse *pParse,          /* The parser context */
872282792aSdrh   ExprList *pEList,       /* List of values being extracted */
882282792aSdrh   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
892282792aSdrh   int distinct,           /* If >=0, make sure results are distinct */
902282792aSdrh   int eDest,              /* How to dispose of the results */
912282792aSdrh   int iParm,              /* An argument to the disposal method */
922282792aSdrh   int iContinue,          /* Jump here to continue with next row */
932282792aSdrh   int iBreak              /* Jump here to break out of the inner loop */
942282792aSdrh ){
952282792aSdrh   Vdbe *v = pParse->pVdbe;
962282792aSdrh   int i;
972282792aSdrh 
982282792aSdrh   /* Pull the requested fields.
992282792aSdrh   */
1002282792aSdrh   for(i=0; i<pEList->nExpr; i++){
1012282792aSdrh     sqliteExprCode(pParse, pEList->a[i].pExpr);
1022282792aSdrh   }
1032282792aSdrh 
1042282792aSdrh   /* If the current result is not distinct, skip the rest
1052282792aSdrh   ** of the processing for the current row.
1062282792aSdrh   */
1072282792aSdrh   if( distinct>=0 ){
1082282792aSdrh     int lbl = sqliteVdbeMakeLabel(v);
1092282792aSdrh     sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0);
1102282792aSdrh     sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0);
1112282792aSdrh     sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0);
1122282792aSdrh     sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0);
1132282792aSdrh     sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl);
1142282792aSdrh     sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0);
1152282792aSdrh   }
1162282792aSdrh   /* If there is an ORDER BY clause, then store the results
1172282792aSdrh   ** in a sorter.
1182282792aSdrh   */
1192282792aSdrh   if( pOrderBy ){
1202282792aSdrh     char *zSortOrder;
1212282792aSdrh     sqliteVdbeAddOp(v, OP_SortMakeRec, pEList->nExpr, 0, 0, 0);
1222282792aSdrh     zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
1232282792aSdrh     if( zSortOrder==0 ) return 1;
1242282792aSdrh     for(i=0; i<pOrderBy->nExpr; i++){
1252282792aSdrh       zSortOrder[i] = pOrderBy->a[i].idx ? '-' : '+';
1262282792aSdrh       sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
1272282792aSdrh     }
1282282792aSdrh     zSortOrder[pOrderBy->nExpr] = 0;
1292282792aSdrh     sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0);
1302282792aSdrh     sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0);
1312282792aSdrh   }else
1322282792aSdrh 
1332282792aSdrh   /* If we are writing to a table, then write the results to the table.
1342282792aSdrh   */
1352282792aSdrh   if( eDest==SRT_Table ){
1362282792aSdrh     sqliteVdbeAddOp(v, OP_MakeRecord, pEList->nExpr, 0, 0, 0);
1372282792aSdrh     sqliteVdbeAddOp(v, OP_New, iParm, 0, 0, 0);
1382282792aSdrh     sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0);
1392282792aSdrh     sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
1402282792aSdrh   }else
1412282792aSdrh 
1422282792aSdrh   /* If we are creating a set for an "expr IN (SELECT ...)" construct,
1432282792aSdrh   ** then there should be a single item on the stack.  Write this
1442282792aSdrh   ** item into the set table with bogus data.
1452282792aSdrh   */
1462282792aSdrh   if( eDest==SRT_Set ){
1472282792aSdrh     assert( pEList->nExpr==1 );
1482282792aSdrh     sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
1492282792aSdrh     sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0);
1502282792aSdrh   }else
1512282792aSdrh 
1522282792aSdrh   /* If this is a scalar select that is part of an expression, then
1532282792aSdrh   ** store the results in the appropriate memory cell and break out
1542282792aSdrh   ** of the scan loop.
1552282792aSdrh   */
1562282792aSdrh   if( eDest==SRT_Mem ){
1572282792aSdrh     sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
1582282792aSdrh     sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0);
1592282792aSdrh   }else
1602282792aSdrh 
1612282792aSdrh   /* If none of the above, send the data to the callback function.
1622282792aSdrh   */
1632282792aSdrh   {
1642282792aSdrh     sqliteVdbeAddOp(v, OP_Callback, pEList->nExpr, 0, 0, 0);
1652282792aSdrh   }
1662282792aSdrh   return 0;
1672282792aSdrh }
1682282792aSdrh 
1692282792aSdrh /*
1709bb61fe7Sdrh ** Generate code for the given SELECT statement.
1719bb61fe7Sdrh **
172fef5208cSdrh ** The results are distributed in various ways depending on the
173fef5208cSdrh ** value of eDest and iParm.
174fef5208cSdrh **
175fef5208cSdrh **     eDest Value       Result
176fef5208cSdrh **     ------------    -------------------------------------------
177fef5208cSdrh **     SRT_Callback    Invoke the callback for each row of the result.
178fef5208cSdrh **
179fef5208cSdrh **     SRT_Mem         Store first result in memory cell iParm
180fef5208cSdrh **
181fef5208cSdrh **     SRT_Set         Store results as keys of a table with cursor iParm
182fef5208cSdrh **
183fef5208cSdrh **     SRT_Table       Store results in a regular table with cursor iParm
1849bb61fe7Sdrh **
1859bb61fe7Sdrh ** This routine returns the number of errors.  If any errors are
1869bb61fe7Sdrh ** encountered, then an appropriate error message is left in
1879bb61fe7Sdrh ** pParse->zErrMsg.
1889bb61fe7Sdrh **
1899bb61fe7Sdrh ** This routine does NOT free the Select structure passed in.  The
1909bb61fe7Sdrh ** calling function needs to do that.
1919bb61fe7Sdrh */
1929bb61fe7Sdrh int sqliteSelect(
193cce7d176Sdrh   Parse *pParse,         /* The parser context */
1949bb61fe7Sdrh   Select *p,             /* The SELECT statement being coded. */
195fef5208cSdrh   int eDest,             /* One of SRT_Callback, SRT_Mem, SRT_Set, SRT_Table */
196fef5208cSdrh   int iParm              /* Save result in this memory location, if >=0 */
197cce7d176Sdrh ){
198cce7d176Sdrh   int i, j;
199cce7d176Sdrh   WhereInfo *pWInfo;
200cce7d176Sdrh   Vdbe *v;
201cce7d176Sdrh   int isAgg = 0;         /* True for select lists like "count(*)" */
2029bb61fe7Sdrh   ExprList *pEList;      /* List of fields to extract.  NULL means "*" */
2039bb61fe7Sdrh   IdList *pTabList;      /* List of tables to select from */
2049bb61fe7Sdrh   Expr *pWhere;          /* The WHERE clause.  May be NULL */
2059bb61fe7Sdrh   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
2062282792aSdrh   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
2072282792aSdrh   Expr *pHaving;         /* The HAVING clause.  May be NULL */
20819a775c2Sdrh   int isDistinct;        /* True if the DISTINCT keyword is present */
20919a775c2Sdrh   int distinct;          /* Table to use for the distinct set */
2109bb61fe7Sdrh 
2119bb61fe7Sdrh   pEList = p->pEList;
2129bb61fe7Sdrh   pTabList = p->pSrc;
2139bb61fe7Sdrh   pWhere = p->pWhere;
2149bb61fe7Sdrh   pOrderBy = p->pOrderBy;
2152282792aSdrh   pGroupBy = p->pGroupBy;
2162282792aSdrh   pHaving = p->pHaving;
21719a775c2Sdrh   isDistinct = p->isDistinct;
2189bb61fe7Sdrh 
2199bb61fe7Sdrh   /*
2209bb61fe7Sdrh   ** Do not even attempt to generate any code if we have already seen
2219bb61fe7Sdrh   ** errors before this routine starts.
2229bb61fe7Sdrh   */
2239bb61fe7Sdrh   if( pParse->nErr>0 ) return 0;
2242282792aSdrh   sqliteParseInfoReset(pParse);
225cce7d176Sdrh 
226cce7d176Sdrh   /* Look up every table in the table list.
227cce7d176Sdrh   */
228cce7d176Sdrh   for(i=0; i<pTabList->nId; i++){
229cce7d176Sdrh     pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
230cce7d176Sdrh     if( pTabList->a[i].pTab==0 ){
231cce7d176Sdrh       sqliteSetString(&pParse->zErrMsg, "no such table: ",
232cce7d176Sdrh          pTabList->a[i].zName, 0);
233cce7d176Sdrh       pParse->nErr++;
2349bb61fe7Sdrh       return 1;
235cce7d176Sdrh     }
236cce7d176Sdrh   }
237cce7d176Sdrh 
23819a775c2Sdrh   /* Allocate a temporary table to use for the DISTINCT set, if
2392282792aSdrh   ** necessary.  This must be done early to allocate the cursor before
2402282792aSdrh   ** any calls to sqliteExprResolveIds().
24119a775c2Sdrh   */
24219a775c2Sdrh   if( isDistinct ){
24319a775c2Sdrh     distinct = pParse->nTab++;
2442282792aSdrh   }else{
2452282792aSdrh     distinct = -1;
24619a775c2Sdrh   }
24719a775c2Sdrh 
248cce7d176Sdrh   /* If the list of fields to retrieve is "*" then replace it with
249cce7d176Sdrh   ** a list of all fields from all tables.
250cce7d176Sdrh   */
251cce7d176Sdrh   if( pEList==0 ){
252cce7d176Sdrh     for(i=0; i<pTabList->nId; i++){
253cce7d176Sdrh       Table *pTab = pTabList->a[i].pTab;
254cce7d176Sdrh       for(j=0; j<pTab->nCol; j++){
255cce7d176Sdrh         Expr *pExpr = sqliteExpr(TK_FIELD, 0, 0, 0);
25619a775c2Sdrh         pExpr->iTable = i + pParse->nTab;
257cce7d176Sdrh         pExpr->iField = j;
258cce7d176Sdrh         pEList = sqliteExprListAppend(pEList, pExpr, 0);
259cce7d176Sdrh       }
260cce7d176Sdrh     }
261cce7d176Sdrh   }
262cce7d176Sdrh 
2632282792aSdrh   /* If writing to memory or generating a set
2642282792aSdrh   ** only a single column may be output.
26519a775c2Sdrh   */
266fef5208cSdrh   if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
26719a775c2Sdrh     sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
26819a775c2Sdrh        "a SELECT that is part of an expression", 0);
26919a775c2Sdrh     pParse->nErr++;
27019a775c2Sdrh     return 1;
27119a775c2Sdrh   }
27219a775c2Sdrh 
2732282792aSdrh   /* ORDER BY is ignored if we are not sending the result to a callback.
2742282792aSdrh   */
2752282792aSdrh   if( eDest!=SRT_Callback ){
2762282792aSdrh     pOrderBy = 0;
2772282792aSdrh   }
2782282792aSdrh 
2792282792aSdrh   /* Allocate cursors for "expr IN (SELECT ...)" constructs.
280cce7d176Sdrh   */
281cce7d176Sdrh   for(i=0; i<pEList->nExpr; i++){
2824794b980Sdrh     sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
2834794b980Sdrh   }
2844794b980Sdrh   if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
2854794b980Sdrh   if( pOrderBy ){
2864794b980Sdrh     for(i=0; i<pOrderBy->nExpr; i++){
2874794b980Sdrh       sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
2884794b980Sdrh     }
2894794b980Sdrh   }
2902282792aSdrh   if( pGroupBy ){
2912282792aSdrh     for(i=0; i<pGroupBy->nExpr; i++){
2922282792aSdrh       sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
2932282792aSdrh     }
2942282792aSdrh   }
2952282792aSdrh   if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
2962282792aSdrh 
2972282792aSdrh   /* Resolve the field names and do a semantics check on all the expressions.
2982282792aSdrh   */
2994794b980Sdrh   for(i=0; i<pEList->nExpr; i++){
300cce7d176Sdrh     if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
3019bb61fe7Sdrh       return 1;
302cce7d176Sdrh     }
3032282792aSdrh     if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
3049bb61fe7Sdrh       return 1;
305cce7d176Sdrh     }
306cce7d176Sdrh   }
307cce7d176Sdrh   if( pWhere ){
308cce7d176Sdrh     if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
3099bb61fe7Sdrh       return 1;
310cce7d176Sdrh     }
311cce7d176Sdrh     if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
3129bb61fe7Sdrh       return 1;
313cce7d176Sdrh     }
314cce7d176Sdrh   }
315cce7d176Sdrh   if( pOrderBy ){
316cce7d176Sdrh     for(i=0; i<pOrderBy->nExpr; i++){
3172282792aSdrh       Expr *pE = pOrderBy->a[i].pExpr;
3182282792aSdrh       if( sqliteExprResolveIds(pParse, pTabList, pE) ){
3199bb61fe7Sdrh         return 1;
320cce7d176Sdrh       }
3212282792aSdrh       if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
3229bb61fe7Sdrh         return 1;
323cce7d176Sdrh       }
324cce7d176Sdrh     }
325cce7d176Sdrh   }
3262282792aSdrh   if( pGroupBy ){
3272282792aSdrh     for(i=0; i<pGroupBy->nExpr; i++){
3282282792aSdrh       Expr *pE = pGroupBy->a[i].pExpr;
3292282792aSdrh       if( sqliteExprResolveIds(pParse, pTabList, pE) ){
3302282792aSdrh         return 1;
3312282792aSdrh       }
3322282792aSdrh       if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
3332282792aSdrh         return 1;
3342282792aSdrh       }
3352282792aSdrh     }
3362282792aSdrh   }
3372282792aSdrh   if( pHaving ){
3382282792aSdrh     if( pGroupBy==0 ){
339*da93281eSdrh       sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
340*da93281eSdrh          "before HAVING", 0);
3412282792aSdrh       pParse->nErr++;
3422282792aSdrh       return 1;
3432282792aSdrh     }
3442282792aSdrh     if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
3452282792aSdrh       return 1;
3462282792aSdrh     }
347*da93281eSdrh     if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
3482282792aSdrh       return 1;
3492282792aSdrh     }
350cce7d176Sdrh   }
351cce7d176Sdrh 
3522282792aSdrh   /* Do an analysis of aggregate expressions.
353efb7251dSdrh   */
3542282792aSdrh   if( isAgg ){
3552282792aSdrh     for(i=0; i<pEList->nExpr; i++){
3562282792aSdrh       if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
3572282792aSdrh         return 1;
3582282792aSdrh       }
3592282792aSdrh     }
3602282792aSdrh     if( pGroupBy ){
3612282792aSdrh       for(i=0; i<pGroupBy->nExpr; i++){
3622282792aSdrh         if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
3632282792aSdrh           return 1;
3642282792aSdrh         }
3652282792aSdrh       }
3662282792aSdrh     }
3672282792aSdrh     if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
3682282792aSdrh       return 1;
3692282792aSdrh     }
370efb7251dSdrh   }
371efb7251dSdrh 
372cce7d176Sdrh   /* Begin generating code.
373cce7d176Sdrh   */
374cce7d176Sdrh   v = pParse->pVdbe;
375cce7d176Sdrh   if( v==0 ){
376cce7d176Sdrh     v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
377cce7d176Sdrh   }
3789bb61fe7Sdrh   if( v==0 ){
3799bb61fe7Sdrh     sqliteSetString(&pParse->zErrMsg, "out of memory", 0);
3809bb61fe7Sdrh     pParse->nErr++;
3819bb61fe7Sdrh     return 1;
3829bb61fe7Sdrh   }
383cce7d176Sdrh   if( pOrderBy ){
384cce7d176Sdrh     sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0);
385cce7d176Sdrh   }
386cce7d176Sdrh 
3872282792aSdrh   /* Identify column names if we will be using in the callback.  This
38819a775c2Sdrh   ** step is skipped if the output is going to a table or a memory cell.
389cce7d176Sdrh   */
390fef5208cSdrh   if( eDest==SRT_Callback ){
391cce7d176Sdrh     sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0);
392cce7d176Sdrh     for(i=0; i<pEList->nExpr; i++){
393cce7d176Sdrh       Expr *p;
394cce7d176Sdrh       if( pEList->a[i].zName ){
395cce7d176Sdrh         char *zName = pEList->a[i].zName;
396cce7d176Sdrh         int addr = sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
397cce7d176Sdrh         if( zName[0]=='\'' || zName[0]=='"' ){
398cce7d176Sdrh           sqliteVdbeDequoteP3(v, addr);
399cce7d176Sdrh         }
400cce7d176Sdrh         continue;
401cce7d176Sdrh       }
402cce7d176Sdrh       p = pEList->a[i].pExpr;
403cce7d176Sdrh       if( p->op!=TK_FIELD ){
404cce7d176Sdrh         char zName[30];
405cce7d176Sdrh         sprintf(zName, "field%d", i+1);
406cce7d176Sdrh         sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
407cce7d176Sdrh       }else{
408cce7d176Sdrh         if( pTabList->nId>1 ){
409cce7d176Sdrh           char *zName = 0;
410cce7d176Sdrh           Table *pTab = pTabList->a[p->iTable].pTab;
411da9d6c45Sdrh           char *zTab;
412da9d6c45Sdrh 
413da9d6c45Sdrh           zTab = pTabList->a[p->iTable].zAlias;
414da9d6c45Sdrh           if( zTab==0 ) zTab = pTab->zName;
4157020f651Sdrh           sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iField].zName, 0);
416cce7d176Sdrh           sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
417cce7d176Sdrh           sqliteFree(zName);
418cce7d176Sdrh         }else{
419cce7d176Sdrh           Table *pTab = pTabList->a[0].pTab;
42019a775c2Sdrh           char *zName = pTab->aCol[p->iField].zName;
42119a775c2Sdrh           sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0);
42219a775c2Sdrh         }
423cce7d176Sdrh       }
424cce7d176Sdrh     }
425cce7d176Sdrh   }
426cce7d176Sdrh 
4272282792aSdrh   /* Reset the aggregator
428cce7d176Sdrh   */
429cce7d176Sdrh   if( isAgg ){
4302282792aSdrh     sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0);
431cce7d176Sdrh   }
432cce7d176Sdrh 
43319a775c2Sdrh   /* Initialize the memory cell to NULL
43419a775c2Sdrh   */
435fef5208cSdrh   if( eDest==SRT_Mem ){
43619a775c2Sdrh     sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
437fef5208cSdrh     sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0);
43819a775c2Sdrh   }
43919a775c2Sdrh 
440cce7d176Sdrh   /* Begin the database scan
441cce7d176Sdrh   */
44219a775c2Sdrh   if( isDistinct ){
443eec553b6Sdrh     sqliteVdbeAddOp(v, OP_Open, distinct, 1, 0, 0);
444efb7251dSdrh   }
445cce7d176Sdrh   pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
4469bb61fe7Sdrh   if( pWInfo==0 ) return 1;
447cce7d176Sdrh 
4482282792aSdrh   /* Use the standard inner loop if we are not dealing with
4492282792aSdrh   ** aggregates
450cce7d176Sdrh   */
451da9d6c45Sdrh   if( !isAgg ){
4522282792aSdrh     if( selectInnerLoop(pParse, pEList, pOrderBy, distinct, eDest, iParm,
4532282792aSdrh                     pWInfo->iContinue, pWInfo->iBreak) ){
4542282792aSdrh        return 1;
455cce7d176Sdrh     }
456da9d6c45Sdrh   }
457cce7d176Sdrh 
4582282792aSdrh   /* If we are dealing with aggregates, then to the special aggregate
4592282792aSdrh   ** processing.
460efb7251dSdrh   */
4612282792aSdrh   else{
4622282792aSdrh     int doFocus;
4632282792aSdrh     if( pGroupBy ){
4642282792aSdrh       for(i=0; i<pGroupBy->nExpr; i++){
4652282792aSdrh         sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
466efb7251dSdrh       }
4672282792aSdrh       sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0);
4682282792aSdrh       doFocus = 1;
469cce7d176Sdrh     }else{
4702282792aSdrh       doFocus = 0;
4712282792aSdrh       for(i=0; i<pParse->nAgg; i++){
4722282792aSdrh         if( !pParse->aAgg[i].isAgg ){
4732282792aSdrh           doFocus = 1;
4742282792aSdrh           break;
475cce7d176Sdrh         }
4762282792aSdrh       }
4772282792aSdrh       if( doFocus ){
4782282792aSdrh         sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
4792282792aSdrh       }
4802282792aSdrh     }
4812282792aSdrh     if( doFocus ){
4822282792aSdrh       int lbl1 = sqliteVdbeMakeLabel(v);
4832282792aSdrh       sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0);
4842282792aSdrh       for(i=0; i<pParse->nAgg; i++){
4852282792aSdrh         if( pParse->aAgg[i].isAgg ) continue;
4862282792aSdrh         sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
4872282792aSdrh         sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
4882282792aSdrh       }
4892282792aSdrh       sqliteVdbeResolveLabel(v, lbl1);
4902282792aSdrh     }
4912282792aSdrh     for(i=0; i<pParse->nAgg; i++){
4922282792aSdrh       Expr *pE;
4932282792aSdrh       int op;
4942282792aSdrh       if( !pParse->aAgg[i].isAgg ) continue;
4952282792aSdrh       pE = pParse->aAgg[i].pExpr;
4962282792aSdrh       if( pE==0 ){
4972282792aSdrh         sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0);
4982282792aSdrh         continue;
4992282792aSdrh       }
5002282792aSdrh       assert( pE->op==TK_AGG_FUNCTION );
5012282792aSdrh       assert( pE->pList!=0 && pE->pList->nExpr==1 );
5022282792aSdrh       sqliteExprCode(pParse, pE->pList->a[0].pExpr);
5032282792aSdrh       sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0);
5042282792aSdrh       switch( pE->iField ){
5052282792aSdrh         case FN_Min:  op = OP_Min;   break;
5062282792aSdrh         case FN_Max:  op = OP_Max;   break;
5072282792aSdrh         case FN_Avg:  op = OP_Add;   break;
5082282792aSdrh         case FN_Sum:  op = OP_Add;   break;
5092282792aSdrh       }
5102282792aSdrh       sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
5112282792aSdrh       sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0);
5122282792aSdrh     }
5132282792aSdrh   }
5142282792aSdrh 
515cce7d176Sdrh 
516cce7d176Sdrh   /* End the database scan loop.
517cce7d176Sdrh   */
518cce7d176Sdrh   sqliteWhereEnd(pWInfo);
519cce7d176Sdrh 
5202282792aSdrh   /* If we are processing aggregates, we need to set up a second loop
5212282792aSdrh   ** over all of the aggregate values and process them.
5222282792aSdrh   */
5232282792aSdrh   if( isAgg ){
5242282792aSdrh     int endagg = sqliteVdbeMakeLabel(v);
5252282792aSdrh     int startagg;
5262282792aSdrh     startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0);
5272282792aSdrh     pParse->useAgg = 1;
5282282792aSdrh     if( pHaving ){
5292282792aSdrh       sqliteExprIfFalse(pParse, pHaving, startagg);
5302282792aSdrh     }
5312282792aSdrh     if( selectInnerLoop(pParse, pEList, pOrderBy, distinct, eDest, iParm,
5322282792aSdrh                     startagg, endagg) ){
5332282792aSdrh       return 1;
5342282792aSdrh     }
5352282792aSdrh     sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0);
5362282792aSdrh     sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg);
5372282792aSdrh     pParse->useAgg = 0;
5382282792aSdrh   }
5392282792aSdrh 
540cce7d176Sdrh   /* If there is an ORDER BY clause, then we need to sort the results
541cce7d176Sdrh   ** and send them to the callback one by one.
542cce7d176Sdrh   */
543cce7d176Sdrh   if( pOrderBy ){
544cce7d176Sdrh     int end = sqliteVdbeMakeLabel(v);
545cce7d176Sdrh     int addr;
546cce7d176Sdrh     sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0);
547cce7d176Sdrh     addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0);
548cce7d176Sdrh     sqliteVdbeAddOp(v, OP_SortCallback, pEList->nExpr, 0, 0, 0);
549cce7d176Sdrh     sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
55019a775c2Sdrh     sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end);
551cce7d176Sdrh   }
5529bb61fe7Sdrh   return 0;
553cce7d176Sdrh }
554