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*92cd52f5Sdrh ** $Id: select.c,v 1.19 2000/06/08 01:55:30 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; 5482c3d636Sdrh pNew->op = TK_SELECT; 559bb61fe7Sdrh return pNew; 569bb61fe7Sdrh } 579bb61fe7Sdrh 589bb61fe7Sdrh /* 599bb61fe7Sdrh ** Delete the given Select structure and all of its substructures. 609bb61fe7Sdrh */ 619bb61fe7Sdrh void sqliteSelectDelete(Select *p){ 6282c3d636Sdrh if( p==0 ) return; 639bb61fe7Sdrh sqliteExprListDelete(p->pEList); 649bb61fe7Sdrh sqliteIdListDelete(p->pSrc); 659bb61fe7Sdrh sqliteExprDelete(p->pWhere); 669bb61fe7Sdrh sqliteExprListDelete(p->pGroupBy); 679bb61fe7Sdrh sqliteExprDelete(p->pHaving); 689bb61fe7Sdrh sqliteExprListDelete(p->pOrderBy); 6982c3d636Sdrh sqliteSelectDelete(p->pPrior); 709bb61fe7Sdrh sqliteFree(p); 719bb61fe7Sdrh } 729bb61fe7Sdrh 739bb61fe7Sdrh /* 742282792aSdrh ** Delete the aggregate information from the parse structure. 752282792aSdrh */ 762282792aSdrh void sqliteParseInfoReset(Parse *pParse){ 772282792aSdrh sqliteFree(pParse->aAgg); 782282792aSdrh pParse->aAgg = 0; 792282792aSdrh pParse->nAgg = 0; 802282792aSdrh pParse->iAggCount = -1; 812282792aSdrh pParse->useAgg = 0; 822282792aSdrh } 832282792aSdrh 842282792aSdrh /* 852282792aSdrh ** This routine generates the code for the inside of the inner loop 862282792aSdrh ** of a SELECT. 8782c3d636Sdrh ** 8882c3d636Sdrh ** The pEList is used to determine the values for each column in the 8982c3d636Sdrh ** result row. Except if pEList==NULL, then we just read nField 9082c3d636Sdrh ** elements from the srcTab table. 912282792aSdrh */ 922282792aSdrh static int selectInnerLoop( 932282792aSdrh Parse *pParse, /* The parser context */ 942282792aSdrh ExprList *pEList, /* List of values being extracted */ 9582c3d636Sdrh int srcTab, /* Pull data from this table */ 9682c3d636Sdrh int nField, /* Number of fields in the source table */ 972282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 982282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 992282792aSdrh int eDest, /* How to dispose of the results */ 1002282792aSdrh int iParm, /* An argument to the disposal method */ 1012282792aSdrh int iContinue, /* Jump here to continue with next row */ 1022282792aSdrh int iBreak /* Jump here to break out of the inner loop */ 1032282792aSdrh ){ 1042282792aSdrh Vdbe *v = pParse->pVdbe; 1052282792aSdrh int i; 1062282792aSdrh 1072282792aSdrh /* Pull the requested fields. 1082282792aSdrh */ 10982c3d636Sdrh if( pEList ){ 1102282792aSdrh for(i=0; i<pEList->nExpr; i++){ 1112282792aSdrh sqliteExprCode(pParse, pEList->a[i].pExpr); 1122282792aSdrh } 11382c3d636Sdrh nField = pEList->nExpr; 11482c3d636Sdrh }else{ 11582c3d636Sdrh for(i=0; i<nField; i++){ 11682c3d636Sdrh sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0); 11782c3d636Sdrh } 11882c3d636Sdrh } 1192282792aSdrh 1202282792aSdrh /* If the current result is not distinct, skip the rest 1212282792aSdrh ** of the processing for the current row. 1222282792aSdrh */ 1232282792aSdrh if( distinct>=0 ){ 1242282792aSdrh int lbl = sqliteVdbeMakeLabel(v); 1252282792aSdrh sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0); 1262282792aSdrh sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0); 1272282792aSdrh sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0); 1282282792aSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0); 1292282792aSdrh sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl); 1302282792aSdrh sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0); 1312282792aSdrh } 13282c3d636Sdrh 1332282792aSdrh /* If there is an ORDER BY clause, then store the results 1342282792aSdrh ** in a sorter. 1352282792aSdrh */ 1362282792aSdrh if( pOrderBy ){ 1372282792aSdrh char *zSortOrder; 138d8bc7086Sdrh sqliteVdbeAddOp(v, OP_SortMakeRec, nField, 0, 0, 0); 1392282792aSdrh zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); 1402282792aSdrh if( zSortOrder==0 ) return 1; 1412282792aSdrh for(i=0; i<pOrderBy->nExpr; i++){ 142d8bc7086Sdrh zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+'; 1432282792aSdrh sqliteExprCode(pParse, pOrderBy->a[i].pExpr); 1442282792aSdrh } 1452282792aSdrh zSortOrder[pOrderBy->nExpr] = 0; 1462282792aSdrh sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0); 1472282792aSdrh sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0); 1482282792aSdrh }else 1492282792aSdrh 15082c3d636Sdrh /* In this mode, write each query result to the key of the temporary 15182c3d636Sdrh ** table iParm. 1522282792aSdrh */ 15382c3d636Sdrh if( eDest==SRT_Union ){ 15482c3d636Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0); 15582c3d636Sdrh sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0); 15682c3d636Sdrh sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); 15782c3d636Sdrh }else 15882c3d636Sdrh 1595974a30fSdrh /* Store the result as data using a unique key. 1605974a30fSdrh */ 1615974a30fSdrh if( eDest==SRT_Table ){ 1625974a30fSdrh sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0); 1635974a30fSdrh sqliteVdbeAddOp(v, OP_New, iParm, 0, 0, 0); 1645974a30fSdrh sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0); 1655974a30fSdrh sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); 1665974a30fSdrh }else 1675974a30fSdrh 16882c3d636Sdrh /* Construct a record from the query result, but instead of 16982c3d636Sdrh ** saving that record, use it as a key to delete elements from 17082c3d636Sdrh ** the temporary table iParm. 17182c3d636Sdrh */ 17282c3d636Sdrh if( eDest==SRT_Except ){ 1731ecec3c0Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0); 1741ecec3c0Sdrh sqliteVdbeAddOp(v, OP_Delete, iParm, 0, 0, 0); 1752282792aSdrh }else 1762282792aSdrh 1772282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 1782282792aSdrh ** then there should be a single item on the stack. Write this 1792282792aSdrh ** item into the set table with bogus data. 1802282792aSdrh */ 1812282792aSdrh if( eDest==SRT_Set ){ 182d8bc7086Sdrh assert( nField==1 ); 1832282792aSdrh sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); 1842282792aSdrh sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); 1852282792aSdrh }else 1862282792aSdrh 18782c3d636Sdrh 1882282792aSdrh /* If this is a scalar select that is part of an expression, then 1892282792aSdrh ** store the results in the appropriate memory cell and break out 1902282792aSdrh ** of the scan loop. 1912282792aSdrh */ 1922282792aSdrh if( eDest==SRT_Mem ){ 193d8bc7086Sdrh assert( nField==1 ); 1942282792aSdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); 1952282792aSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0); 1962282792aSdrh }else 1972282792aSdrh 1982282792aSdrh /* If none of the above, send the data to the callback function. 1992282792aSdrh */ 2002282792aSdrh { 20182c3d636Sdrh sqliteVdbeAddOp(v, OP_Callback, nField, 0, 0, 0); 20282c3d636Sdrh } 20382c3d636Sdrh return 0; 20482c3d636Sdrh } 20582c3d636Sdrh 20682c3d636Sdrh /* 207d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 208d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 209d8bc7086Sdrh ** we need to run the sorter and output the results. The following 210d8bc7086Sdrh ** routine generates the code needed to do that. 211d8bc7086Sdrh */ 212d8bc7086Sdrh static void generateSortTail(Vdbe *v, int nField){ 213d8bc7086Sdrh int end = sqliteVdbeMakeLabel(v); 214d8bc7086Sdrh int addr; 215d8bc7086Sdrh sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0); 216d8bc7086Sdrh addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0); 217d8bc7086Sdrh sqliteVdbeAddOp(v, OP_SortCallback, nField, 0, 0, 0); 218d8bc7086Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); 219d8bc7086Sdrh sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end); 220d8bc7086Sdrh } 221d8bc7086Sdrh 222d8bc7086Sdrh /* 22382c3d636Sdrh ** Generate code that will tell the VDBE how many columns there 22482c3d636Sdrh ** are in the result and the name for each column. This information 22582c3d636Sdrh ** is used to provide "argc" and "azCol[]" values in the callback. 22682c3d636Sdrh */ 227d8bc7086Sdrh static 228d8bc7086Sdrh void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){ 229d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 23082c3d636Sdrh int i; 231d8bc7086Sdrh if( pParse->colNamesSet ) return; 232d8bc7086Sdrh pParse->colNamesSet = 1; 23382c3d636Sdrh sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0); 23482c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 23582c3d636Sdrh Expr *p; 23682c3d636Sdrh if( pEList->a[i].zName ){ 23782c3d636Sdrh char *zName = pEList->a[i].zName; 238d8bc7086Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); 23982c3d636Sdrh continue; 24082c3d636Sdrh } 24182c3d636Sdrh p = pEList->a[i].pExpr; 24282c3d636Sdrh if( p->op!=TK_FIELD || pTabList==0 ){ 24382c3d636Sdrh char zName[30]; 2445974a30fSdrh sprintf(zName, "column%d", i+1); 24582c3d636Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); 24682c3d636Sdrh }else{ 24782c3d636Sdrh if( pTabList->nId>1 ){ 24882c3d636Sdrh char *zName = 0; 24982c3d636Sdrh Table *pTab = pTabList->a[p->iTable].pTab; 25082c3d636Sdrh char *zTab; 25182c3d636Sdrh 25282c3d636Sdrh zTab = pTabList->a[p->iTable].zAlias; 25382c3d636Sdrh if( zTab==0 ) zTab = pTab->zName; 25482c3d636Sdrh sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iField].zName, 0); 25582c3d636Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); 25682c3d636Sdrh sqliteFree(zName); 25782c3d636Sdrh }else{ 25882c3d636Sdrh Table *pTab = pTabList->a[0].pTab; 25982c3d636Sdrh char *zName = pTab->aCol[p->iField].zName; 26082c3d636Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); 26182c3d636Sdrh } 26282c3d636Sdrh } 26382c3d636Sdrh } 26482c3d636Sdrh } 26582c3d636Sdrh 26682c3d636Sdrh /* 267d8bc7086Sdrh ** Name of the connection operator, used for error messages. 268d8bc7086Sdrh */ 269d8bc7086Sdrh static const char *selectOpName(int id){ 270d8bc7086Sdrh char *z; 271d8bc7086Sdrh switch( id ){ 272d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 273d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 274d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 275d8bc7086Sdrh default: z = "UNION"; break; 276d8bc7086Sdrh } 277d8bc7086Sdrh return z; 278d8bc7086Sdrh } 279d8bc7086Sdrh 280d8bc7086Sdrh /* 281d8bc7086Sdrh ** For the given SELECT statement, do two things. 282d8bc7086Sdrh ** 283d8bc7086Sdrh ** (1) Fill in the pTab fields of the IdList that defines the set 284d8bc7086Sdrh ** of tables we are scanning. 285d8bc7086Sdrh ** 286d8bc7086Sdrh ** (2) If the columns to be extracted variable (pEList) is NULL 287d8bc7086Sdrh ** (meaning that a "*" was used in the SQL statement) then 288d8bc7086Sdrh ** create a fake pEList containing the names of all columns 289d8bc7086Sdrh ** of all tables. 290d8bc7086Sdrh ** 291d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 292d8bc7086Sdrh ** in pParse and return non-zero. 293d8bc7086Sdrh */ 294d8bc7086Sdrh static int fillInColumnList(Parse *pParse, Select *p){ 295d8bc7086Sdrh int i, j; 296d8bc7086Sdrh IdList *pTabList = p->pSrc; 297d8bc7086Sdrh ExprList *pEList = p->pEList; 298d8bc7086Sdrh 299d8bc7086Sdrh /* Look up every table in the table list. 300d8bc7086Sdrh */ 301d8bc7086Sdrh for(i=0; i<pTabList->nId; i++){ 302d8bc7086Sdrh if( pTabList->a[i].pTab ){ 303d8bc7086Sdrh /* This routine has run before! No need to continue */ 304d8bc7086Sdrh return 0; 305d8bc7086Sdrh } 306d8bc7086Sdrh pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName); 307d8bc7086Sdrh if( pTabList->a[i].pTab==0 ){ 308d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "no such table: ", 309d8bc7086Sdrh pTabList->a[i].zName, 0); 310d8bc7086Sdrh pParse->nErr++; 311d8bc7086Sdrh return 1; 312d8bc7086Sdrh } 313d8bc7086Sdrh } 314d8bc7086Sdrh 315d8bc7086Sdrh /* If the list of columns to retrieve is "*" then replace it with 316d8bc7086Sdrh ** a list of all columns from all tables. 317d8bc7086Sdrh */ 318d8bc7086Sdrh if( pEList==0 ){ 319d8bc7086Sdrh for(i=0; i<pTabList->nId; i++){ 320d8bc7086Sdrh Table *pTab = pTabList->a[i].pTab; 321d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 322d8bc7086Sdrh Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0); 323d8bc7086Sdrh pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0); 324d8bc7086Sdrh pExpr->pLeft->token.z = pTab->zName; 325d8bc7086Sdrh pExpr->pLeft->token.n = strlen(pTab->zName); 326d8bc7086Sdrh pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0); 327d8bc7086Sdrh pExpr->pRight->token.z = pTab->aCol[j].zName; 328d8bc7086Sdrh pExpr->pRight->token.n = strlen(pTab->aCol[j].zName); 329d8bc7086Sdrh pEList = sqliteExprListAppend(pEList, pExpr, 0); 330d8bc7086Sdrh } 331d8bc7086Sdrh } 332d8bc7086Sdrh p->pEList = pEList; 333d8bc7086Sdrh } 334d8bc7086Sdrh return 0; 335d8bc7086Sdrh } 336d8bc7086Sdrh 337d8bc7086Sdrh /* 338d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 339d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 340d8bc7086Sdrh ** the top-level node is changed to TK_FIELD and the iField value of 341d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 342d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 343d8bc7086Sdrh ** 344d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 345d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 346d8bc7086Sdrh ** 347d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 348d8bc7086Sdrh ** of errors is returned. 349d8bc7086Sdrh */ 350d8bc7086Sdrh static int matchOrderbyToColumn( 351d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 352d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 353d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 354d8bc7086Sdrh int iTable, /* Insert this this value in iTable */ 355d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 356d8bc7086Sdrh ){ 357d8bc7086Sdrh int nErr = 0; 358d8bc7086Sdrh int i, j; 359d8bc7086Sdrh ExprList *pEList; 360d8bc7086Sdrh 361d8bc7086Sdrh assert( pSelect && pOrderBy ); 362d8bc7086Sdrh if( mustComplete ){ 363d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 364d8bc7086Sdrh } 365d8bc7086Sdrh if( fillInColumnList(pParse, pSelect) ){ 366d8bc7086Sdrh return 1; 367d8bc7086Sdrh } 368d8bc7086Sdrh if( pSelect->pPrior ){ 369*92cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 370*92cd52f5Sdrh return 1; 371*92cd52f5Sdrh } 372d8bc7086Sdrh } 373d8bc7086Sdrh pEList = pSelect->pEList; 374d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 375d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 376*92cd52f5Sdrh int match = 0; 377d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 378d8bc7086Sdrh for(j=0; j<pEList->nExpr; j++){ 379d8bc7086Sdrh if( pEList->a[i].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 380d8bc7086Sdrh char *zName = pEList->a[i].zName; 381d8bc7086Sdrh char *zLabel = 0; 382*92cd52f5Sdrh sqliteSetNString(&zLabel, pE->token.z, pE->token.n, 0); 383d8bc7086Sdrh sqliteDequote(zLabel); 384d8bc7086Sdrh if( sqliteStrICmp(zName, zLabel)==0 ){ 385d8bc7086Sdrh match = 1; 386d8bc7086Sdrh } 387d8bc7086Sdrh } 388d8bc7086Sdrh if( match==0 && sqliteExprCompare(pE, pEList->a[i].pExpr) ){ 389d8bc7086Sdrh match = 1; 390d8bc7086Sdrh } 391d8bc7086Sdrh if( match ){ 392d8bc7086Sdrh pE->op = TK_FIELD; 393d8bc7086Sdrh pE->iField = j; 394d8bc7086Sdrh pE->iTable = iTable; 395d8bc7086Sdrh pOrderBy->a[i].done = 1; 396d8bc7086Sdrh break; 397d8bc7086Sdrh } 398d8bc7086Sdrh } 399*92cd52f5Sdrh if( !match && mustComplete ){ 400d8bc7086Sdrh char zBuf[30]; 401d8bc7086Sdrh sprintf(zBuf,"%d",i+1); 402d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, 403d8bc7086Sdrh " does not match any result column", 0); 404d8bc7086Sdrh pParse->nErr++; 405d8bc7086Sdrh nErr++; 406d8bc7086Sdrh break; 407d8bc7086Sdrh } 408d8bc7086Sdrh } 409d8bc7086Sdrh return nErr; 410d8bc7086Sdrh } 411d8bc7086Sdrh 412d8bc7086Sdrh /* 413d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 414d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 415d8bc7086Sdrh */ 416d8bc7086Sdrh Vdbe *sqliteGetVdbe(Parse *pParse){ 417d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 418d8bc7086Sdrh if( v==0 ){ 419d8bc7086Sdrh v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); 420d8bc7086Sdrh } 421d8bc7086Sdrh if( v==0 ){ 422d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "out of memory", 0); 423d8bc7086Sdrh pParse->nErr++; 424d8bc7086Sdrh } 425d8bc7086Sdrh return v; 426d8bc7086Sdrh } 427d8bc7086Sdrh 428d8bc7086Sdrh 429d8bc7086Sdrh /* 43082c3d636Sdrh ** This routine is called to process a query that is really the union 43182c3d636Sdrh ** or intersection of two or more separate queries. 43282c3d636Sdrh */ 43382c3d636Sdrh static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ 43410e5e3cfSdrh int rc; /* Success code from a subroutine */ 43510e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 43610e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 43710e5e3cfSdrh int base; /* Baseline value for pParse->nTab */ 43882c3d636Sdrh 439d8bc7086Sdrh /* Make sure there is no ORDER BY clause on prior SELECTs. Only the 440d8bc7086Sdrh ** last SELECT in the series may have an ORDER BY. 44182c3d636Sdrh */ 442d8bc7086Sdrh assert( p->pPrior!=0 ); 443d8bc7086Sdrh pPrior = p->pPrior; 444d8bc7086Sdrh if( pPrior->pOrderBy ){ 445d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", 446d8bc7086Sdrh selectOpName(p->op), " not before", 0); 44782c3d636Sdrh pParse->nErr++; 44882c3d636Sdrh return 1; 44982c3d636Sdrh } 45082c3d636Sdrh 451d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 452d8bc7086Sdrh */ 453d8bc7086Sdrh v = sqliteGetVdbe(pParse); 454d8bc7086Sdrh if( v==0 ) return 1; 455d8bc7086Sdrh 456d8bc7086Sdrh /* Process the UNION or INTERSECTION 457d8bc7086Sdrh */ 45810e5e3cfSdrh base = pParse->nTab; 45982c3d636Sdrh switch( p->op ){ 460d8bc7086Sdrh case TK_ALL: 46182c3d636Sdrh case TK_EXCEPT: 46282c3d636Sdrh case TK_UNION: { 463d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 464d8bc7086Sdrh int op; /* One of the SRT_ operations to apply to self */ 465d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 46682c3d636Sdrh 467d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 468d8bc7086Sdrh if( eDest==priorOp ){ 469d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 470d8bc7086Sdrh ** right. This also means we are not the right-most select and so 471d8bc7086Sdrh ** we cannot have an ORDER BY clause 472d8bc7086Sdrh */ 47382c3d636Sdrh unionTab = iParm; 474d8bc7086Sdrh assert( p->pOrderBy==0 ); 47582c3d636Sdrh }else{ 476d8bc7086Sdrh /* We will need to create our own temporary table to hold the 477d8bc7086Sdrh ** intermediate results. 478d8bc7086Sdrh */ 47982c3d636Sdrh unionTab = pParse->nTab++; 480d8bc7086Sdrh if( p->pOrderBy 481d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 482d8bc7086Sdrh return 1; 483d8bc7086Sdrh } 48482c3d636Sdrh sqliteVdbeAddOp(v, OP_Open, unionTab, 1, 0, 0); 485d8bc7086Sdrh if( p->op!=TK_ALL ){ 48682c3d636Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0); 48782c3d636Sdrh } 488d8bc7086Sdrh } 489d8bc7086Sdrh 490d8bc7086Sdrh /* Code the SELECT statements to our left 491d8bc7086Sdrh */ 492d8bc7086Sdrh rc = sqliteSelect(pParse, pPrior, priorOp, unionTab); 49382c3d636Sdrh if( rc ) return rc; 494d8bc7086Sdrh 495d8bc7086Sdrh /* Code the current SELECT statement 496d8bc7086Sdrh */ 497d8bc7086Sdrh switch( p->op ){ 498d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 499d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 500d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 501d8bc7086Sdrh } 50282c3d636Sdrh p->pPrior = 0; 50382c3d636Sdrh rc = sqliteSelect(pParse, p, op, unionTab); 50482c3d636Sdrh p->pPrior = pPrior; 50582c3d636Sdrh if( rc ) return rc; 506d8bc7086Sdrh 507d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 508d8bc7086Sdrh ** it is that we currently need. 509d8bc7086Sdrh */ 510d8bc7086Sdrh if( eDest!=priorOp ){ 51182c3d636Sdrh int iCont, iBreak; 51282c3d636Sdrh assert( p->pEList ); 513d8bc7086Sdrh generateColumnNames(pParse, 0, p->pEList); 51492dba24bSdrh if( p->pOrderBy ){ 51592dba24bSdrh sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); 51692dba24bSdrh } 51782c3d636Sdrh iBreak = sqliteVdbeMakeLabel(v); 51882c3d636Sdrh iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0); 51982c3d636Sdrh rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, 520d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 52182c3d636Sdrh iCont, iBreak); 52282c3d636Sdrh if( rc ) return 1; 52382c3d636Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); 52482c3d636Sdrh sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak); 525d8bc7086Sdrh if( p->pOrderBy ){ 526d8bc7086Sdrh generateSortTail(v, p->pEList->nExpr); 527d8bc7086Sdrh } 52882c3d636Sdrh } 52982c3d636Sdrh break; 53082c3d636Sdrh } 53182c3d636Sdrh case TK_INTERSECT: { 53282c3d636Sdrh int tab1, tab2; 53382c3d636Sdrh int iCont, iBreak; 53482c3d636Sdrh 535d8bc7086Sdrh /* INTERSECT is different from the others since it requires 536d8bc7086Sdrh ** two temporary tables. Hence it has its own case. Begine 537d8bc7086Sdrh ** by allocating the tables we will need. 538d8bc7086Sdrh */ 53982c3d636Sdrh tab1 = pParse->nTab++; 54082c3d636Sdrh tab2 = pParse->nTab++; 541d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 542d8bc7086Sdrh return 1; 543d8bc7086Sdrh } 54482c3d636Sdrh sqliteVdbeAddOp(v, OP_Open, tab1, 1, 0, 0); 54582c3d636Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0); 546d8bc7086Sdrh 547d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 548d8bc7086Sdrh */ 54982c3d636Sdrh rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1); 55082c3d636Sdrh if( rc ) return rc; 551d8bc7086Sdrh 552d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 553d8bc7086Sdrh */ 55482c3d636Sdrh sqliteVdbeAddOp(v, OP_Open, tab2, 1, 0, 0); 55582c3d636Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0); 55682c3d636Sdrh p->pPrior = 0; 55782c3d636Sdrh rc = sqliteSelect(pParse, p, SRT_Union, tab2); 55882c3d636Sdrh p->pPrior = pPrior; 55982c3d636Sdrh if( rc ) return rc; 560d8bc7086Sdrh 561d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 562d8bc7086Sdrh ** tables. 563d8bc7086Sdrh */ 56482c3d636Sdrh assert( p->pEList ); 565d8bc7086Sdrh generateColumnNames(pParse, 0, p->pEList); 56692dba24bSdrh if( p->pOrderBy ){ 56792dba24bSdrh sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); 56892dba24bSdrh } 56982c3d636Sdrh iBreak = sqliteVdbeMakeLabel(v); 57082c3d636Sdrh iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0); 57182c3d636Sdrh sqliteVdbeAddOp(v, OP_Key, tab1, 0, 0, 0); 57282c3d636Sdrh sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0); 57382c3d636Sdrh rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, 574d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 57582c3d636Sdrh iCont, iBreak); 57682c3d636Sdrh if( rc ) return 1; 57782c3d636Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); 57882c3d636Sdrh sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak); 57982c3d636Sdrh sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0); 580d8bc7086Sdrh if( p->pOrderBy ){ 581d8bc7086Sdrh generateSortTail(v, p->pEList->nExpr); 582d8bc7086Sdrh } 58382c3d636Sdrh break; 58482c3d636Sdrh } 58582c3d636Sdrh } 58682c3d636Sdrh assert( p->pEList && pPrior->pEList ); 58782c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 588d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", 589d8bc7086Sdrh selectOpName(p->op), " do not have the same number of result columns", 0); 59082c3d636Sdrh pParse->nErr++; 59182c3d636Sdrh return 1; 5922282792aSdrh } 59310e5e3cfSdrh pParse->nTab = base; 5942282792aSdrh return 0; 5952282792aSdrh } 5962282792aSdrh 5972282792aSdrh /* 5989bb61fe7Sdrh ** Generate code for the given SELECT statement. 5999bb61fe7Sdrh ** 600fef5208cSdrh ** The results are distributed in various ways depending on the 601fef5208cSdrh ** value of eDest and iParm. 602fef5208cSdrh ** 603fef5208cSdrh ** eDest Value Result 604fef5208cSdrh ** ------------ ------------------------------------------- 605fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 606fef5208cSdrh ** 607fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 608fef5208cSdrh ** 609fef5208cSdrh ** SRT_Set Store results as keys of a table with cursor iParm 610fef5208cSdrh ** 61182c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 61282c3d636Sdrh ** 61382c3d636Sdrh ** SRT_Except Remove results form the temporary talbe iParm. 6149bb61fe7Sdrh ** 6159bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 6169bb61fe7Sdrh ** encountered, then an appropriate error message is left in 6179bb61fe7Sdrh ** pParse->zErrMsg. 6189bb61fe7Sdrh ** 6199bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 6209bb61fe7Sdrh ** calling function needs to do that. 6219bb61fe7Sdrh */ 6229bb61fe7Sdrh int sqliteSelect( 623cce7d176Sdrh Parse *pParse, /* The parser context */ 6249bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 62582c3d636Sdrh int eDest, /* One of: SRT_Callback Mem Set Union Except */ 626fef5208cSdrh int iParm /* Save result in this memory location, if >=0 */ 627cce7d176Sdrh ){ 628d8bc7086Sdrh int i; 629cce7d176Sdrh WhereInfo *pWInfo; 630cce7d176Sdrh Vdbe *v; 631cce7d176Sdrh int isAgg = 0; /* True for select lists like "count(*)" */ 6329bb61fe7Sdrh ExprList *pEList; /* List of fields to extract. NULL means "*" */ 6339bb61fe7Sdrh IdList *pTabList; /* List of tables to select from */ 6349bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 6359bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 6362282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 6372282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 63819a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 63919a775c2Sdrh int distinct; /* Table to use for the distinct set */ 64010e5e3cfSdrh int base; /* First cursor available for use */ 6419bb61fe7Sdrh 64282c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 64382c3d636Sdrh */ 64482c3d636Sdrh if( p->pPrior ){ 64582c3d636Sdrh return multiSelect(pParse, p, eDest, iParm); 64682c3d636Sdrh } 64782c3d636Sdrh 64882c3d636Sdrh /* Make local copies of the parameters for this query. 64982c3d636Sdrh */ 6509bb61fe7Sdrh pTabList = p->pSrc; 6519bb61fe7Sdrh pWhere = p->pWhere; 6529bb61fe7Sdrh pOrderBy = p->pOrderBy; 6532282792aSdrh pGroupBy = p->pGroupBy; 6542282792aSdrh pHaving = p->pHaving; 65519a775c2Sdrh isDistinct = p->isDistinct; 6569bb61fe7Sdrh 65710e5e3cfSdrh /* Save the current value of pParse->nTab. Restore this value before 65810e5e3cfSdrh ** we exit. 65910e5e3cfSdrh */ 66010e5e3cfSdrh base = pParse->nTab; 66110e5e3cfSdrh 6629bb61fe7Sdrh /* 6639bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 6649bb61fe7Sdrh ** errors before this routine starts. 6659bb61fe7Sdrh */ 66610e5e3cfSdrh if( pParse->nErr>0 ) return 1; 6672282792aSdrh sqliteParseInfoReset(pParse); 668cce7d176Sdrh 669d8bc7086Sdrh /* Look up every table in the table list and create an appropriate 670d8bc7086Sdrh ** columnlist in pEList if there isn't one already. (The parser leaves 671d8bc7086Sdrh ** a NULL in the pEList field if the SQL said "SELECT * FROM ...") 672cce7d176Sdrh */ 673d8bc7086Sdrh if( fillInColumnList(pParse, p) ){ 6749bb61fe7Sdrh return 1; 675cce7d176Sdrh } 676d8bc7086Sdrh pEList = p->pEList; 677cce7d176Sdrh 67819a775c2Sdrh /* Allocate a temporary table to use for the DISTINCT set, if 6792282792aSdrh ** necessary. This must be done early to allocate the cursor before 6802282792aSdrh ** any calls to sqliteExprResolveIds(). 68119a775c2Sdrh */ 68219a775c2Sdrh if( isDistinct ){ 68319a775c2Sdrh distinct = pParse->nTab++; 6842282792aSdrh }else{ 6852282792aSdrh distinct = -1; 68619a775c2Sdrh } 68719a775c2Sdrh 6882282792aSdrh /* If writing to memory or generating a set 6892282792aSdrh ** only a single column may be output. 69019a775c2Sdrh */ 691fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 69219a775c2Sdrh sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " 69319a775c2Sdrh "a SELECT that is part of an expression", 0); 69419a775c2Sdrh pParse->nErr++; 69519a775c2Sdrh return 1; 69619a775c2Sdrh } 69719a775c2Sdrh 6982282792aSdrh /* ORDER BY is ignored if we are not sending the result to a callback. 6992282792aSdrh */ 7002282792aSdrh if( eDest!=SRT_Callback ){ 7012282792aSdrh pOrderBy = 0; 7022282792aSdrh } 7032282792aSdrh 7042282792aSdrh /* Allocate cursors for "expr IN (SELECT ...)" constructs. 705cce7d176Sdrh */ 706cce7d176Sdrh for(i=0; i<pEList->nExpr; i++){ 7074794b980Sdrh sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr); 7084794b980Sdrh } 7094794b980Sdrh if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere); 7104794b980Sdrh if( pOrderBy ){ 7114794b980Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 7124794b980Sdrh sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr); 7134794b980Sdrh } 7144794b980Sdrh } 7152282792aSdrh if( pGroupBy ){ 7162282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 7172282792aSdrh sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr); 7182282792aSdrh } 7192282792aSdrh } 7202282792aSdrh if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving); 7212282792aSdrh 72210e5e3cfSdrh /* At this point, we should have allocated all the cursors that we 72310e5e3cfSdrh ** need to handle subquerys and temporary tables. From here on we 72410e5e3cfSdrh ** are committed to keeping the same value for pParse->nTab. 72510e5e3cfSdrh ** 72610e5e3cfSdrh ** Resolve the field names and do a semantics check on all the expressions. 7272282792aSdrh */ 7284794b980Sdrh for(i=0; i<pEList->nExpr; i++){ 729cce7d176Sdrh if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){ 7309bb61fe7Sdrh return 1; 731cce7d176Sdrh } 7322282792aSdrh if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ 7339bb61fe7Sdrh return 1; 734cce7d176Sdrh } 735cce7d176Sdrh } 736cce7d176Sdrh if( pWhere ){ 737cce7d176Sdrh if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ 7389bb61fe7Sdrh return 1; 739cce7d176Sdrh } 740cce7d176Sdrh if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ 7419bb61fe7Sdrh return 1; 742cce7d176Sdrh } 743cce7d176Sdrh } 744cce7d176Sdrh if( pOrderBy ){ 745cce7d176Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 7462282792aSdrh Expr *pE = pOrderBy->a[i].pExpr; 7472282792aSdrh if( sqliteExprResolveIds(pParse, pTabList, pE) ){ 7489bb61fe7Sdrh return 1; 749cce7d176Sdrh } 7502282792aSdrh if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ 7519bb61fe7Sdrh return 1; 752cce7d176Sdrh } 753cce7d176Sdrh } 754cce7d176Sdrh } 7552282792aSdrh if( pGroupBy ){ 7562282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 7572282792aSdrh Expr *pE = pGroupBy->a[i].pExpr; 7582282792aSdrh if( sqliteExprResolveIds(pParse, pTabList, pE) ){ 7592282792aSdrh return 1; 7602282792aSdrh } 7612282792aSdrh if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ 7622282792aSdrh return 1; 7632282792aSdrh } 7642282792aSdrh } 7652282792aSdrh } 7662282792aSdrh if( pHaving ){ 7672282792aSdrh if( pGroupBy==0 ){ 768da93281eSdrh sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " 769da93281eSdrh "before HAVING", 0); 7702282792aSdrh pParse->nErr++; 7712282792aSdrh return 1; 7722282792aSdrh } 7732282792aSdrh if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){ 7742282792aSdrh return 1; 7752282792aSdrh } 776da93281eSdrh if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ 7772282792aSdrh return 1; 7782282792aSdrh } 779cce7d176Sdrh } 780cce7d176Sdrh 7812282792aSdrh /* Do an analysis of aggregate expressions. 782efb7251dSdrh */ 7832282792aSdrh if( isAgg ){ 7842282792aSdrh for(i=0; i<pEList->nExpr; i++){ 7852282792aSdrh if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ 7862282792aSdrh return 1; 7872282792aSdrh } 7882282792aSdrh } 7892282792aSdrh if( pGroupBy ){ 7902282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 7912282792aSdrh if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ 7922282792aSdrh return 1; 7932282792aSdrh } 7942282792aSdrh } 7952282792aSdrh } 7962282792aSdrh if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ 7972282792aSdrh return 1; 7982282792aSdrh } 799efb7251dSdrh } 800efb7251dSdrh 801cce7d176Sdrh /* Begin generating code. 802cce7d176Sdrh */ 803cce7d176Sdrh v = pParse->pVdbe; 804cce7d176Sdrh if( v==0 ){ 805cce7d176Sdrh v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); 806cce7d176Sdrh } 8079bb61fe7Sdrh if( v==0 ){ 8089bb61fe7Sdrh sqliteSetString(&pParse->zErrMsg, "out of memory", 0); 8099bb61fe7Sdrh pParse->nErr++; 8109bb61fe7Sdrh return 1; 8119bb61fe7Sdrh } 812cce7d176Sdrh if( pOrderBy ){ 813cce7d176Sdrh sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); 814cce7d176Sdrh } 815cce7d176Sdrh 8162282792aSdrh /* Identify column names if we will be using in the callback. This 81719a775c2Sdrh ** step is skipped if the output is going to a table or a memory cell. 818cce7d176Sdrh */ 819fef5208cSdrh if( eDest==SRT_Callback ){ 820d8bc7086Sdrh generateColumnNames(pParse, pTabList, pEList); 821cce7d176Sdrh } 822cce7d176Sdrh 8232282792aSdrh /* Reset the aggregator 824cce7d176Sdrh */ 825cce7d176Sdrh if( isAgg ){ 8262282792aSdrh sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0); 827cce7d176Sdrh } 828cce7d176Sdrh 82919a775c2Sdrh /* Initialize the memory cell to NULL 83019a775c2Sdrh */ 831fef5208cSdrh if( eDest==SRT_Mem ){ 83219a775c2Sdrh sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); 833fef5208cSdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); 83419a775c2Sdrh } 83519a775c2Sdrh 836cce7d176Sdrh /* Begin the database scan 837cce7d176Sdrh */ 83819a775c2Sdrh if( isDistinct ){ 839eec553b6Sdrh sqliteVdbeAddOp(v, OP_Open, distinct, 1, 0, 0); 840efb7251dSdrh } 841cce7d176Sdrh pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); 8429bb61fe7Sdrh if( pWInfo==0 ) return 1; 843cce7d176Sdrh 8442282792aSdrh /* Use the standard inner loop if we are not dealing with 8452282792aSdrh ** aggregates 846cce7d176Sdrh */ 847da9d6c45Sdrh if( !isAgg ){ 84882c3d636Sdrh if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, 8492282792aSdrh pWInfo->iContinue, pWInfo->iBreak) ){ 8502282792aSdrh return 1; 851cce7d176Sdrh } 852da9d6c45Sdrh } 853cce7d176Sdrh 8542282792aSdrh /* If we are dealing with aggregates, then to the special aggregate 8552282792aSdrh ** processing. 856efb7251dSdrh */ 8572282792aSdrh else{ 8582282792aSdrh int doFocus; 8592282792aSdrh if( pGroupBy ){ 8602282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 8612282792aSdrh sqliteExprCode(pParse, pGroupBy->a[i].pExpr); 862efb7251dSdrh } 8632282792aSdrh sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0); 8642282792aSdrh doFocus = 1; 865cce7d176Sdrh }else{ 8662282792aSdrh doFocus = 0; 8672282792aSdrh for(i=0; i<pParse->nAgg; i++){ 8682282792aSdrh if( !pParse->aAgg[i].isAgg ){ 8692282792aSdrh doFocus = 1; 8702282792aSdrh break; 871cce7d176Sdrh } 8722282792aSdrh } 8732282792aSdrh if( doFocus ){ 8742282792aSdrh sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); 8752282792aSdrh } 8762282792aSdrh } 8772282792aSdrh if( doFocus ){ 8782282792aSdrh int lbl1 = sqliteVdbeMakeLabel(v); 8792282792aSdrh sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0); 8802282792aSdrh for(i=0; i<pParse->nAgg; i++){ 8812282792aSdrh if( pParse->aAgg[i].isAgg ) continue; 8822282792aSdrh sqliteExprCode(pParse, pParse->aAgg[i].pExpr); 8832282792aSdrh sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); 8842282792aSdrh } 8852282792aSdrh sqliteVdbeResolveLabel(v, lbl1); 8862282792aSdrh } 8872282792aSdrh for(i=0; i<pParse->nAgg; i++){ 8882282792aSdrh Expr *pE; 8892282792aSdrh int op; 8902282792aSdrh if( !pParse->aAgg[i].isAgg ) continue; 8912282792aSdrh pE = pParse->aAgg[i].pExpr; 8922282792aSdrh if( pE==0 ){ 8932282792aSdrh sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0); 8942282792aSdrh continue; 8952282792aSdrh } 8962282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 8972282792aSdrh assert( pE->pList!=0 && pE->pList->nExpr==1 ); 8982282792aSdrh sqliteExprCode(pParse, pE->pList->a[0].pExpr); 8992282792aSdrh sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0); 9002282792aSdrh switch( pE->iField ){ 9012282792aSdrh case FN_Min: op = OP_Min; break; 9022282792aSdrh case FN_Max: op = OP_Max; break; 9032282792aSdrh case FN_Avg: op = OP_Add; break; 9042282792aSdrh case FN_Sum: op = OP_Add; break; 9052282792aSdrh } 9062282792aSdrh sqliteVdbeAddOp(v, op, 0, 0, 0, 0); 9072282792aSdrh sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); 9082282792aSdrh } 9092282792aSdrh } 9102282792aSdrh 911cce7d176Sdrh 912cce7d176Sdrh /* End the database scan loop. 913cce7d176Sdrh */ 914cce7d176Sdrh sqliteWhereEnd(pWInfo); 915cce7d176Sdrh 9162282792aSdrh /* If we are processing aggregates, we need to set up a second loop 9172282792aSdrh ** over all of the aggregate values and process them. 9182282792aSdrh */ 9192282792aSdrh if( isAgg ){ 9202282792aSdrh int endagg = sqliteVdbeMakeLabel(v); 9212282792aSdrh int startagg; 9222282792aSdrh startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0); 9232282792aSdrh pParse->useAgg = 1; 9242282792aSdrh if( pHaving ){ 9252282792aSdrh sqliteExprIfFalse(pParse, pHaving, startagg); 9262282792aSdrh } 92782c3d636Sdrh if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, 9282282792aSdrh startagg, endagg) ){ 9292282792aSdrh return 1; 9302282792aSdrh } 9312282792aSdrh sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0); 9322282792aSdrh sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg); 9332282792aSdrh pParse->useAgg = 0; 9342282792aSdrh } 9352282792aSdrh 936cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 937cce7d176Sdrh ** and send them to the callback one by one. 938cce7d176Sdrh */ 939cce7d176Sdrh if( pOrderBy ){ 940d8bc7086Sdrh generateSortTail(v, pEList->nExpr); 941cce7d176Sdrh } 94210e5e3cfSdrh pParse->nTab = base; 9439bb61fe7Sdrh return 0; 944cce7d176Sdrh } 945