1cce7d176Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 3cce7d176Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 6cce7d176Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 10cce7d176Sdrh ** 11cce7d176Sdrh ************************************************************************* 12cce7d176Sdrh ** This file contains C code routines that are called by the parser 13b19a2bc6Sdrh ** to handle SELECT statements in SQLite. 14cce7d176Sdrh ** 15*99fcd718Sdrh ** $Id: select.c,v 1.39 2001/10/13 01:06:48 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19cce7d176Sdrh /* 209bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that 219bb61fe7Sdrh ** structure. 22cce7d176Sdrh */ 239bb61fe7Sdrh Select *sqliteSelectNew( 24daffd0e5Sdrh ExprList *pEList, /* which columns to include in the result */ 25daffd0e5Sdrh IdList *pSrc, /* the FROM clause -- which tables to scan */ 26daffd0e5Sdrh Expr *pWhere, /* the WHERE clause */ 27daffd0e5Sdrh ExprList *pGroupBy, /* the GROUP BY clause */ 28daffd0e5Sdrh Expr *pHaving, /* the HAVING clause */ 29daffd0e5Sdrh ExprList *pOrderBy, /* the ORDER BY clause */ 30daffd0e5Sdrh int isDistinct /* true if the DISTINCT keyword is present */ 319bb61fe7Sdrh ){ 329bb61fe7Sdrh Select *pNew; 339bb61fe7Sdrh pNew = sqliteMalloc( sizeof(*pNew) ); 34daffd0e5Sdrh if( pNew==0 ){ 35daffd0e5Sdrh sqliteExprListDelete(pEList); 36daffd0e5Sdrh sqliteIdListDelete(pSrc); 37daffd0e5Sdrh sqliteExprDelete(pWhere); 38daffd0e5Sdrh sqliteExprListDelete(pGroupBy); 39daffd0e5Sdrh sqliteExprDelete(pHaving); 40daffd0e5Sdrh sqliteExprListDelete(pOrderBy); 41daffd0e5Sdrh }else{ 429bb61fe7Sdrh pNew->pEList = pEList; 439bb61fe7Sdrh pNew->pSrc = pSrc; 449bb61fe7Sdrh pNew->pWhere = pWhere; 459bb61fe7Sdrh pNew->pGroupBy = pGroupBy; 469bb61fe7Sdrh pNew->pHaving = pHaving; 479bb61fe7Sdrh pNew->pOrderBy = pOrderBy; 489bb61fe7Sdrh pNew->isDistinct = isDistinct; 4982c3d636Sdrh pNew->op = TK_SELECT; 50daffd0e5Sdrh } 519bb61fe7Sdrh return pNew; 529bb61fe7Sdrh } 539bb61fe7Sdrh 549bb61fe7Sdrh /* 559bb61fe7Sdrh ** Delete the given Select structure and all of its substructures. 569bb61fe7Sdrh */ 579bb61fe7Sdrh void sqliteSelectDelete(Select *p){ 5882c3d636Sdrh if( p==0 ) return; 599bb61fe7Sdrh sqliteExprListDelete(p->pEList); 609bb61fe7Sdrh sqliteIdListDelete(p->pSrc); 619bb61fe7Sdrh sqliteExprDelete(p->pWhere); 629bb61fe7Sdrh sqliteExprListDelete(p->pGroupBy); 639bb61fe7Sdrh sqliteExprDelete(p->pHaving); 649bb61fe7Sdrh sqliteExprListDelete(p->pOrderBy); 6582c3d636Sdrh sqliteSelectDelete(p->pPrior); 669bb61fe7Sdrh sqliteFree(p); 679bb61fe7Sdrh } 689bb61fe7Sdrh 699bb61fe7Sdrh /* 702282792aSdrh ** Delete the aggregate information from the parse structure. 712282792aSdrh */ 722282792aSdrh void sqliteParseInfoReset(Parse *pParse){ 732282792aSdrh sqliteFree(pParse->aAgg); 742282792aSdrh pParse->aAgg = 0; 752282792aSdrh pParse->nAgg = 0; 762282792aSdrh pParse->iAggCount = -1; 772282792aSdrh pParse->useAgg = 0; 782282792aSdrh } 792282792aSdrh 802282792aSdrh /* 812282792aSdrh ** This routine generates the code for the inside of the inner loop 822282792aSdrh ** of a SELECT. 8382c3d636Sdrh ** 8482c3d636Sdrh ** The pEList is used to determine the values for each column in the 85967e8b73Sdrh ** result row. Except if pEList==NULL, then we just read nColumn 8682c3d636Sdrh ** elements from the srcTab table. 872282792aSdrh */ 882282792aSdrh static int selectInnerLoop( 892282792aSdrh Parse *pParse, /* The parser context */ 902282792aSdrh ExprList *pEList, /* List of values being extracted */ 9182c3d636Sdrh int srcTab, /* Pull data from this table */ 92967e8b73Sdrh int nColumn, /* Number of columns in the source table */ 932282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 942282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 952282792aSdrh int eDest, /* How to dispose of the results */ 962282792aSdrh int iParm, /* An argument to the disposal method */ 972282792aSdrh int iContinue, /* Jump here to continue with next row */ 982282792aSdrh int iBreak /* Jump here to break out of the inner loop */ 992282792aSdrh ){ 1002282792aSdrh Vdbe *v = pParse->pVdbe; 1012282792aSdrh int i; 102daffd0e5Sdrh if( v==0 ) return 0; 1032282792aSdrh 104967e8b73Sdrh /* Pull the requested columns. 1052282792aSdrh */ 10682c3d636Sdrh if( pEList ){ 1072282792aSdrh for(i=0; i<pEList->nExpr; i++){ 1082282792aSdrh sqliteExprCode(pParse, pEList->a[i].pExpr); 1092282792aSdrh } 110967e8b73Sdrh nColumn = pEList->nExpr; 11182c3d636Sdrh }else{ 112967e8b73Sdrh for(i=0; i<nColumn; i++){ 113*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i); 11482c3d636Sdrh } 11582c3d636Sdrh } 1162282792aSdrh 117daffd0e5Sdrh /* If the DISTINCT keyword was present on the SELECT statement 118daffd0e5Sdrh ** and this row has been seen before, then do not make this row 119daffd0e5Sdrh ** part of the result. 1202282792aSdrh */ 1212282792aSdrh if( distinct>=0 ){ 1222282792aSdrh int lbl = sqliteVdbeMakeLabel(v); 123*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1); 124*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl); 125*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); 126*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iContinue); 127*99fcd718Sdrh sqliteVdbeResolveLabel(v, lbl); 128*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 129*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, "", P3_STATIC); 130*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Put, distinct, 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; 138*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 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; 146*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0); 147*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder)); 1486e142f54Sdrh sqliteFree(zSortOrder); 149*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortPut, 0, 0); 1502282792aSdrh }else 1512282792aSdrh 15282c3d636Sdrh /* In this mode, write each query result to the key of the temporary 15382c3d636Sdrh ** table iParm. 1542282792aSdrh */ 15582c3d636Sdrh if( eDest==SRT_Union ){ 156*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); 157*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, iParm, 0); 158*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, "", P3_STATIC); 159*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Put, iParm, 0); 16082c3d636Sdrh }else 16182c3d636Sdrh 1625974a30fSdrh /* Store the result as data using a unique key. 1635974a30fSdrh */ 1645974a30fSdrh if( eDest==SRT_Table ){ 165*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); 166*99fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); 167*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Pull, 1, 0); 168*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Put, iParm, 0); 1695974a30fSdrh }else 1705974a30fSdrh 17182c3d636Sdrh /* Construct a record from the query result, but instead of 17282c3d636Sdrh ** saving that record, use it as a key to delete elements from 17382c3d636Sdrh ** the temporary table iParm. 17482c3d636Sdrh */ 17582c3d636Sdrh if( eDest==SRT_Except ){ 176*99fcd718Sdrh int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); 177*99fcd718Sdrh sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3); 178*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Delete, iParm, 0); 1792282792aSdrh }else 1802282792aSdrh 1812282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 1822282792aSdrh ** then there should be a single item on the stack. Write this 1832282792aSdrh ** item into the set table with bogus data. 1842282792aSdrh */ 1852282792aSdrh if( eDest==SRT_Set ){ 186967e8b73Sdrh assert( nColumn==1 ); 187*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 188*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, "", P3_STATIC); 189*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Put, iParm, 0); 1902282792aSdrh }else 1912282792aSdrh 19282c3d636Sdrh 1932282792aSdrh /* If this is a scalar select that is part of an expression, then 1942282792aSdrh ** store the results in the appropriate memory cell and break out 1952282792aSdrh ** of the scan loop. 1962282792aSdrh */ 1972282792aSdrh if( eDest==SRT_Mem ){ 198967e8b73Sdrh assert( nColumn==1 ); 199*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 0); 200*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iBreak); 2012282792aSdrh }else 2022282792aSdrh 2032282792aSdrh /* If none of the above, send the data to the callback function. 2042282792aSdrh */ 2052282792aSdrh { 206*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Callback, nColumn, 0); 20782c3d636Sdrh } 20882c3d636Sdrh return 0; 20982c3d636Sdrh } 21082c3d636Sdrh 21182c3d636Sdrh /* 212d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 213d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 214d8bc7086Sdrh ** we need to run the sorter and output the results. The following 215d8bc7086Sdrh ** routine generates the code needed to do that. 216d8bc7086Sdrh */ 217967e8b73Sdrh static void generateSortTail(Vdbe *v, int nColumn){ 218d8bc7086Sdrh int end = sqliteVdbeMakeLabel(v); 219d8bc7086Sdrh int addr; 220*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Sort, 0, 0); 221*99fcd718Sdrh addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end); 222*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0); 223*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, addr); 224*99fcd718Sdrh sqliteVdbeResolveLabel(v, end); 225*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortClose, 0, 0); 226d8bc7086Sdrh } 227d8bc7086Sdrh 228d8bc7086Sdrh /* 22982c3d636Sdrh ** Generate code that will tell the VDBE how many columns there 23082c3d636Sdrh ** are in the result and the name for each column. This information 23182c3d636Sdrh ** is used to provide "argc" and "azCol[]" values in the callback. 23282c3d636Sdrh */ 233d8bc7086Sdrh static 234d8bc7086Sdrh void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){ 235d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 23682c3d636Sdrh int i; 237daffd0e5Sdrh if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return; 238d8bc7086Sdrh pParse->colNamesSet = 1; 239*99fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0); 24082c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 24182c3d636Sdrh Expr *p; 24282c3d636Sdrh if( pEList->a[i].zName ){ 24382c3d636Sdrh char *zName = pEList->a[i].zName; 244*99fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 245*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); 24682c3d636Sdrh continue; 24782c3d636Sdrh } 24882c3d636Sdrh p = pEList->a[i].pExpr; 249daffd0e5Sdrh if( p==0 ) continue; 250e1b6a5b8Sdrh if( p->span.z && p->span.z[0] ){ 251*99fcd718Sdrh int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); 252*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); 253e1b6a5b8Sdrh sqliteVdbeCompressSpace(v, addr); 254e1b6a5b8Sdrh }else if( p->op!=TK_COLUMN || pTabList==0 ){ 25582c3d636Sdrh char zName[30]; 2565974a30fSdrh sprintf(zName, "column%d", i+1); 257*99fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 258*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); 25982c3d636Sdrh }else{ 260382c0247Sdrh if( pTabList->nId>1 || (pParse->db->flags & SQLITE_FullColNames)!=0 ){ 26182c3d636Sdrh char *zName = 0; 26282c3d636Sdrh Table *pTab = pTabList->a[p->iTable].pTab; 26382c3d636Sdrh char *zTab; 26482c3d636Sdrh 26582c3d636Sdrh zTab = pTabList->a[p->iTable].zAlias; 26682c3d636Sdrh if( zTab==0 ) zTab = pTab->zName; 267967e8b73Sdrh sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iColumn].zName, 0); 268*99fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 269*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); 27082c3d636Sdrh sqliteFree(zName); 27182c3d636Sdrh }else{ 27282c3d636Sdrh Table *pTab = pTabList->a[0].pTab; 273967e8b73Sdrh char *zName = pTab->aCol[p->iColumn].zName; 274*99fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 275*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, zName, P3_STATIC); 27682c3d636Sdrh } 27782c3d636Sdrh } 27882c3d636Sdrh } 27982c3d636Sdrh } 28082c3d636Sdrh 28182c3d636Sdrh /* 282d8bc7086Sdrh ** Name of the connection operator, used for error messages. 283d8bc7086Sdrh */ 284d8bc7086Sdrh static const char *selectOpName(int id){ 285d8bc7086Sdrh char *z; 286d8bc7086Sdrh switch( id ){ 287d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 288d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 289d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 290d8bc7086Sdrh default: z = "UNION"; break; 291d8bc7086Sdrh } 292d8bc7086Sdrh return z; 293d8bc7086Sdrh } 294d8bc7086Sdrh 295d8bc7086Sdrh /* 296d8bc7086Sdrh ** For the given SELECT statement, do two things. 297d8bc7086Sdrh ** 298967e8b73Sdrh ** (1) Fill in the pTabList->a[].pTab fields in the IdList that 299967e8b73Sdrh ** defines the set of tables that should be scanned. 300d8bc7086Sdrh ** 301d8bc7086Sdrh ** (2) If the columns to be extracted variable (pEList) is NULL 302d8bc7086Sdrh ** (meaning that a "*" was used in the SQL statement) then 303d8bc7086Sdrh ** create a fake pEList containing the names of all columns 304d8bc7086Sdrh ** of all tables. 305d8bc7086Sdrh ** 306d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 307d8bc7086Sdrh ** in pParse and return non-zero. 308d8bc7086Sdrh */ 309d8bc7086Sdrh static int fillInColumnList(Parse *pParse, Select *p){ 310d8bc7086Sdrh int i, j; 311daffd0e5Sdrh IdList *pTabList; 312daffd0e5Sdrh ExprList *pEList; 313daffd0e5Sdrh 314daffd0e5Sdrh if( p==0 || p->pSrc==0 ) return 1; 315daffd0e5Sdrh pTabList = p->pSrc; 316daffd0e5Sdrh pEList = p->pEList; 317d8bc7086Sdrh 318d8bc7086Sdrh /* Look up every table in the table list. 319d8bc7086Sdrh */ 320d8bc7086Sdrh for(i=0; i<pTabList->nId; i++){ 321d8bc7086Sdrh if( pTabList->a[i].pTab ){ 322d8bc7086Sdrh /* This routine has run before! No need to continue */ 323d8bc7086Sdrh return 0; 324d8bc7086Sdrh } 325daffd0e5Sdrh if( pTabList->a[i].zName==0 ){ 326daffd0e5Sdrh /* No table name is given. Instead, there is a (SELECT ...) statement 327daffd0e5Sdrh ** the results of which should be used in place of the table. The 328daffd0e5Sdrh ** was this is implemented is that the (SELECT ...) writes its results 329daffd0e5Sdrh ** into a temporary table which is then scanned like any other table. 330daffd0e5Sdrh */ 331daffd0e5Sdrh sqliteSetString(&pParse->zErrMsg, 332daffd0e5Sdrh "(SELECT...) in a FROM clause is not yet implemented.", 0); 333daffd0e5Sdrh pParse->nErr++; 334daffd0e5Sdrh return 1; 335daffd0e5Sdrh } 336d8bc7086Sdrh pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName); 337d8bc7086Sdrh if( pTabList->a[i].pTab==0 ){ 338d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "no such table: ", 339d8bc7086Sdrh pTabList->a[i].zName, 0); 340d8bc7086Sdrh pParse->nErr++; 341d8bc7086Sdrh return 1; 342d8bc7086Sdrh } 343d8bc7086Sdrh } 344d8bc7086Sdrh 345d8bc7086Sdrh /* If the list of columns to retrieve is "*" then replace it with 346d8bc7086Sdrh ** a list of all columns from all tables. 347d8bc7086Sdrh */ 348d8bc7086Sdrh if( pEList==0 ){ 349d8bc7086Sdrh for(i=0; i<pTabList->nId; i++){ 350d8bc7086Sdrh Table *pTab = pTabList->a[i].pTab; 351d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 352d8bc7086Sdrh Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0); 353daffd0e5Sdrh if( pExpr==0 ) break; 354d8bc7086Sdrh pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0); 355daffd0e5Sdrh if( pExpr->pLeft==0 ) break; 356d8bc7086Sdrh pExpr->pLeft->token.z = pTab->zName; 357d8bc7086Sdrh pExpr->pLeft->token.n = strlen(pTab->zName); 358d8bc7086Sdrh pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0); 359daffd0e5Sdrh if( pExpr->pRight==0 ) break; 360d8bc7086Sdrh pExpr->pRight->token.z = pTab->aCol[j].zName; 361d8bc7086Sdrh pExpr->pRight->token.n = strlen(pTab->aCol[j].zName); 362e1b6a5b8Sdrh pExpr->span.z = ""; 363e1b6a5b8Sdrh pExpr->span.n = 0; 364d8bc7086Sdrh pEList = sqliteExprListAppend(pEList, pExpr, 0); 365d8bc7086Sdrh } 366d8bc7086Sdrh } 367d8bc7086Sdrh p->pEList = pEList; 368d8bc7086Sdrh } 369d8bc7086Sdrh return 0; 370d8bc7086Sdrh } 371d8bc7086Sdrh 372d8bc7086Sdrh /* 373d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 374d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 375967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 376d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 377d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 378d8bc7086Sdrh ** 379d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 380d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 381d8bc7086Sdrh ** 382d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 383d8bc7086Sdrh ** of errors is returned. 384d8bc7086Sdrh */ 385d8bc7086Sdrh static int matchOrderbyToColumn( 386d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 387d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 388d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 389d8bc7086Sdrh int iTable, /* Insert this this value in iTable */ 390d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 391d8bc7086Sdrh ){ 392d8bc7086Sdrh int nErr = 0; 393d8bc7086Sdrh int i, j; 394d8bc7086Sdrh ExprList *pEList; 395d8bc7086Sdrh 396daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 397d8bc7086Sdrh if( mustComplete ){ 398d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 399d8bc7086Sdrh } 400d8bc7086Sdrh if( fillInColumnList(pParse, pSelect) ){ 401d8bc7086Sdrh return 1; 402d8bc7086Sdrh } 403d8bc7086Sdrh if( pSelect->pPrior ){ 40492cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 40592cd52f5Sdrh return 1; 40692cd52f5Sdrh } 407d8bc7086Sdrh } 408d8bc7086Sdrh pEList = pSelect->pEList; 409d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 410d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 41192cd52f5Sdrh int match = 0; 412d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 413d8bc7086Sdrh for(j=0; j<pEList->nExpr; j++){ 4144cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 4154cfa7934Sdrh char *zName = pEList->a[j].zName; 4166e142f54Sdrh char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n); 417d8bc7086Sdrh sqliteDequote(zLabel); 418d8bc7086Sdrh if( sqliteStrICmp(zName, zLabel)==0 ){ 419d8bc7086Sdrh match = 1; 420d8bc7086Sdrh } 4216e142f54Sdrh sqliteFree(zLabel); 422d8bc7086Sdrh } 4234cfa7934Sdrh if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ 424d8bc7086Sdrh match = 1; 425d8bc7086Sdrh } 426d8bc7086Sdrh if( match ){ 427967e8b73Sdrh pE->op = TK_COLUMN; 428967e8b73Sdrh pE->iColumn = j; 429d8bc7086Sdrh pE->iTable = iTable; 430d8bc7086Sdrh pOrderBy->a[i].done = 1; 431d8bc7086Sdrh break; 432d8bc7086Sdrh } 433d8bc7086Sdrh } 43492cd52f5Sdrh if( !match && mustComplete ){ 435d8bc7086Sdrh char zBuf[30]; 436d8bc7086Sdrh sprintf(zBuf,"%d",i+1); 437d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, 438d8bc7086Sdrh " does not match any result column", 0); 439d8bc7086Sdrh pParse->nErr++; 440d8bc7086Sdrh nErr++; 441d8bc7086Sdrh break; 442d8bc7086Sdrh } 443d8bc7086Sdrh } 444d8bc7086Sdrh return nErr; 445d8bc7086Sdrh } 446d8bc7086Sdrh 447d8bc7086Sdrh /* 448d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 449d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 450d8bc7086Sdrh */ 451d8bc7086Sdrh Vdbe *sqliteGetVdbe(Parse *pParse){ 452d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 453d8bc7086Sdrh if( v==0 ){ 4544c504391Sdrh v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); 455d8bc7086Sdrh } 456d8bc7086Sdrh return v; 457d8bc7086Sdrh } 458d8bc7086Sdrh 459d8bc7086Sdrh 460d8bc7086Sdrh /* 46182c3d636Sdrh ** This routine is called to process a query that is really the union 46282c3d636Sdrh ** or intersection of two or more separate queries. 46382c3d636Sdrh */ 46482c3d636Sdrh static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ 46510e5e3cfSdrh int rc; /* Success code from a subroutine */ 46610e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 46710e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 46810e5e3cfSdrh int base; /* Baseline value for pParse->nTab */ 46982c3d636Sdrh 470d8bc7086Sdrh /* Make sure there is no ORDER BY clause on prior SELECTs. Only the 471d8bc7086Sdrh ** last SELECT in the series may have an ORDER BY. 47282c3d636Sdrh */ 473daffd0e5Sdrh if( p==0 || p->pPrior==0 ) return 1; 474d8bc7086Sdrh pPrior = p->pPrior; 475d8bc7086Sdrh if( pPrior->pOrderBy ){ 476d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", 477d8bc7086Sdrh selectOpName(p->op), " not before", 0); 47882c3d636Sdrh pParse->nErr++; 47982c3d636Sdrh return 1; 48082c3d636Sdrh } 48182c3d636Sdrh 482d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 483d8bc7086Sdrh */ 484d8bc7086Sdrh v = sqliteGetVdbe(pParse); 485d8bc7086Sdrh if( v==0 ) return 1; 486d8bc7086Sdrh 487d8bc7086Sdrh /* Process the UNION or INTERSECTION 488d8bc7086Sdrh */ 48910e5e3cfSdrh base = pParse->nTab; 49082c3d636Sdrh switch( p->op ){ 491d8bc7086Sdrh case TK_ALL: 49282c3d636Sdrh case TK_EXCEPT: 49382c3d636Sdrh case TK_UNION: { 494d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 495d8bc7086Sdrh int op; /* One of the SRT_ operations to apply to self */ 496d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 49782c3d636Sdrh 498d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 499d8bc7086Sdrh if( eDest==priorOp ){ 500d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 501d8bc7086Sdrh ** right. This also means we are not the right-most select and so 502d8bc7086Sdrh ** we cannot have an ORDER BY clause 503d8bc7086Sdrh */ 50482c3d636Sdrh unionTab = iParm; 505d8bc7086Sdrh assert( p->pOrderBy==0 ); 50682c3d636Sdrh }else{ 507d8bc7086Sdrh /* We will need to create our own temporary table to hold the 508d8bc7086Sdrh ** intermediate results. 509d8bc7086Sdrh */ 51082c3d636Sdrh unionTab = pParse->nTab++; 511d8bc7086Sdrh if( p->pOrderBy 512d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 513d8bc7086Sdrh return 1; 514d8bc7086Sdrh } 515d8bc7086Sdrh if( p->op!=TK_ALL ){ 516*99fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); 517*99fcd718Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); 518345fda3eSdrh }else{ 519*99fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); 52082c3d636Sdrh } 521d8bc7086Sdrh } 522d8bc7086Sdrh 523d8bc7086Sdrh /* Code the SELECT statements to our left 524d8bc7086Sdrh */ 525d8bc7086Sdrh rc = sqliteSelect(pParse, pPrior, priorOp, unionTab); 52682c3d636Sdrh if( rc ) return rc; 527d8bc7086Sdrh 528d8bc7086Sdrh /* Code the current SELECT statement 529d8bc7086Sdrh */ 530d8bc7086Sdrh switch( p->op ){ 531d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 532d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 533d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 534d8bc7086Sdrh } 53582c3d636Sdrh p->pPrior = 0; 53682c3d636Sdrh rc = sqliteSelect(pParse, p, op, unionTab); 53782c3d636Sdrh p->pPrior = pPrior; 53882c3d636Sdrh if( rc ) return rc; 539d8bc7086Sdrh 540d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 541d8bc7086Sdrh ** it is that we currently need. 542d8bc7086Sdrh */ 543d8bc7086Sdrh if( eDest!=priorOp ){ 54482c3d636Sdrh int iCont, iBreak; 54582c3d636Sdrh assert( p->pEList ); 546d8bc7086Sdrh generateColumnNames(pParse, 0, p->pEList); 54792dba24bSdrh if( p->pOrderBy ){ 548*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortOpen, 0, 0); 54992dba24bSdrh } 550*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Rewind, unionTab, 0); 55182c3d636Sdrh iBreak = sqliteVdbeMakeLabel(v); 552*99fcd718Sdrh iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak); 55382c3d636Sdrh rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, 554d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 55582c3d636Sdrh iCont, iBreak); 55682c3d636Sdrh if( rc ) return 1; 557*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCont); 558*99fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 559*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Close, unionTab, 0); 560d8bc7086Sdrh if( p->pOrderBy ){ 561d8bc7086Sdrh generateSortTail(v, p->pEList->nExpr); 562d8bc7086Sdrh } 56382c3d636Sdrh } 56482c3d636Sdrh break; 56582c3d636Sdrh } 56682c3d636Sdrh case TK_INTERSECT: { 56782c3d636Sdrh int tab1, tab2; 56882c3d636Sdrh int iCont, iBreak; 56982c3d636Sdrh 570d8bc7086Sdrh /* INTERSECT is different from the others since it requires 5716206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 572d8bc7086Sdrh ** by allocating the tables we will need. 573d8bc7086Sdrh */ 57482c3d636Sdrh tab1 = pParse->nTab++; 57582c3d636Sdrh tab2 = pParse->nTab++; 576d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 577d8bc7086Sdrh return 1; 578d8bc7086Sdrh } 579*99fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0); 580*99fcd718Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); 581d8bc7086Sdrh 582d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 583d8bc7086Sdrh */ 58482c3d636Sdrh rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1); 58582c3d636Sdrh if( rc ) return rc; 586d8bc7086Sdrh 587d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 588d8bc7086Sdrh */ 589*99fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0); 590*99fcd718Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); 59182c3d636Sdrh p->pPrior = 0; 59282c3d636Sdrh rc = sqliteSelect(pParse, p, SRT_Union, tab2); 59382c3d636Sdrh p->pPrior = pPrior; 59482c3d636Sdrh if( rc ) return rc; 595d8bc7086Sdrh 596d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 597d8bc7086Sdrh ** tables. 598d8bc7086Sdrh */ 59982c3d636Sdrh assert( p->pEList ); 600d8bc7086Sdrh generateColumnNames(pParse, 0, p->pEList); 60192dba24bSdrh if( p->pOrderBy ){ 602*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortOpen, 0, 0); 60392dba24bSdrh } 604*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Rewind, tab1, 0); 60582c3d636Sdrh iBreak = sqliteVdbeMakeLabel(v); 606*99fcd718Sdrh iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak); 607*99fcd718Sdrh sqliteVdbeAddOp(v, OP_FullKey, tab1, 0); 608*99fcd718Sdrh sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont); 60982c3d636Sdrh rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, 610d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 61182c3d636Sdrh iCont, iBreak); 61282c3d636Sdrh if( rc ) return 1; 613*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCont); 614*99fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 615*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Close, tab2, 0); 616*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Close, tab1, 0); 617d8bc7086Sdrh if( p->pOrderBy ){ 618d8bc7086Sdrh generateSortTail(v, p->pEList->nExpr); 619d8bc7086Sdrh } 62082c3d636Sdrh break; 62182c3d636Sdrh } 62282c3d636Sdrh } 62382c3d636Sdrh assert( p->pEList && pPrior->pEList ); 62482c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 625d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", 626d8bc7086Sdrh selectOpName(p->op), " do not have the same number of result columns", 0); 62782c3d636Sdrh pParse->nErr++; 62882c3d636Sdrh return 1; 6292282792aSdrh } 63010e5e3cfSdrh pParse->nTab = base; 6312282792aSdrh return 0; 6322282792aSdrh } 6332282792aSdrh 6342282792aSdrh /* 6359bb61fe7Sdrh ** Generate code for the given SELECT statement. 6369bb61fe7Sdrh ** 637fef5208cSdrh ** The results are distributed in various ways depending on the 638fef5208cSdrh ** value of eDest and iParm. 639fef5208cSdrh ** 640fef5208cSdrh ** eDest Value Result 641fef5208cSdrh ** ------------ ------------------------------------------- 642fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 643fef5208cSdrh ** 644fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 645fef5208cSdrh ** 646fef5208cSdrh ** SRT_Set Store results as keys of a table with cursor iParm 647fef5208cSdrh ** 64882c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 64982c3d636Sdrh ** 650c4a3c779Sdrh ** SRT_Except Remove results form the temporary table iParm. 651c4a3c779Sdrh ** 652c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 6539bb61fe7Sdrh ** 6549bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 6559bb61fe7Sdrh ** encountered, then an appropriate error message is left in 6569bb61fe7Sdrh ** pParse->zErrMsg. 6579bb61fe7Sdrh ** 6589bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 6599bb61fe7Sdrh ** calling function needs to do that. 6609bb61fe7Sdrh */ 6619bb61fe7Sdrh int sqliteSelect( 662cce7d176Sdrh Parse *pParse, /* The parser context */ 6639bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 66482c3d636Sdrh int eDest, /* One of: SRT_Callback Mem Set Union Except */ 665fef5208cSdrh int iParm /* Save result in this memory location, if >=0 */ 666cce7d176Sdrh ){ 667d8bc7086Sdrh int i; 668cce7d176Sdrh WhereInfo *pWInfo; 669cce7d176Sdrh Vdbe *v; 670cce7d176Sdrh int isAgg = 0; /* True for select lists like "count(*)" */ 671967e8b73Sdrh ExprList *pEList; /* List of columns to extract. NULL means "*" */ 6729bb61fe7Sdrh IdList *pTabList; /* List of tables to select from */ 6739bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 6749bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 6752282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 6762282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 67719a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 67819a775c2Sdrh int distinct; /* Table to use for the distinct set */ 67910e5e3cfSdrh int base; /* First cursor available for use */ 6809bb61fe7Sdrh 681daffd0e5Sdrh if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; 682daffd0e5Sdrh 68382c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 68482c3d636Sdrh */ 68582c3d636Sdrh if( p->pPrior ){ 68682c3d636Sdrh return multiSelect(pParse, p, eDest, iParm); 68782c3d636Sdrh } 68882c3d636Sdrh 68982c3d636Sdrh /* Make local copies of the parameters for this query. 69082c3d636Sdrh */ 6919bb61fe7Sdrh pTabList = p->pSrc; 6929bb61fe7Sdrh pWhere = p->pWhere; 6939bb61fe7Sdrh pOrderBy = p->pOrderBy; 6942282792aSdrh pGroupBy = p->pGroupBy; 6952282792aSdrh pHaving = p->pHaving; 69619a775c2Sdrh isDistinct = p->isDistinct; 6979bb61fe7Sdrh 69810e5e3cfSdrh /* Save the current value of pParse->nTab. Restore this value before 69910e5e3cfSdrh ** we exit. 70010e5e3cfSdrh */ 70110e5e3cfSdrh base = pParse->nTab; 70210e5e3cfSdrh 7039bb61fe7Sdrh /* 7049bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 7059bb61fe7Sdrh ** errors before this routine starts. 7069bb61fe7Sdrh */ 70710e5e3cfSdrh if( pParse->nErr>0 ) return 1; 7082282792aSdrh sqliteParseInfoReset(pParse); 709cce7d176Sdrh 710d8bc7086Sdrh /* Look up every table in the table list and create an appropriate 711d8bc7086Sdrh ** columnlist in pEList if there isn't one already. (The parser leaves 712967e8b73Sdrh ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") 713cce7d176Sdrh */ 714d8bc7086Sdrh if( fillInColumnList(pParse, p) ){ 7159bb61fe7Sdrh return 1; 716cce7d176Sdrh } 717d8bc7086Sdrh pEList = p->pEList; 718daffd0e5Sdrh if( pEList==0 ) return 1; 719cce7d176Sdrh 72019a775c2Sdrh /* Allocate a temporary table to use for the DISTINCT set, if 7212282792aSdrh ** necessary. This must be done early to allocate the cursor before 7222282792aSdrh ** any calls to sqliteExprResolveIds(). 72319a775c2Sdrh */ 72419a775c2Sdrh if( isDistinct ){ 72519a775c2Sdrh distinct = pParse->nTab++; 7262282792aSdrh }else{ 7272282792aSdrh distinct = -1; 72819a775c2Sdrh } 72919a775c2Sdrh 7302282792aSdrh /* If writing to memory or generating a set 7312282792aSdrh ** only a single column may be output. 73219a775c2Sdrh */ 733fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 73419a775c2Sdrh sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " 73519a775c2Sdrh "a SELECT that is part of an expression", 0); 73619a775c2Sdrh pParse->nErr++; 73719a775c2Sdrh return 1; 73819a775c2Sdrh } 73919a775c2Sdrh 7402282792aSdrh /* ORDER BY is ignored if we are not sending the result to a callback. 7412282792aSdrh */ 7422282792aSdrh if( eDest!=SRT_Callback ){ 7432282792aSdrh pOrderBy = 0; 7442282792aSdrh } 7452282792aSdrh 7462282792aSdrh /* Allocate cursors for "expr IN (SELECT ...)" constructs. 747cce7d176Sdrh */ 748cce7d176Sdrh for(i=0; i<pEList->nExpr; i++){ 7494794b980Sdrh sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr); 7504794b980Sdrh } 7514794b980Sdrh if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere); 7524794b980Sdrh if( pOrderBy ){ 7534794b980Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 7544794b980Sdrh sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr); 7554794b980Sdrh } 7564794b980Sdrh } 7572282792aSdrh if( pGroupBy ){ 7582282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 7592282792aSdrh sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr); 7602282792aSdrh } 7612282792aSdrh } 7622282792aSdrh if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving); 7632282792aSdrh 76410e5e3cfSdrh /* At this point, we should have allocated all the cursors that we 76510e5e3cfSdrh ** need to handle subquerys and temporary tables. From here on we 76610e5e3cfSdrh ** are committed to keeping the same value for pParse->nTab. 76710e5e3cfSdrh ** 768967e8b73Sdrh ** Resolve the column names and do a semantics check on all the expressions. 7692282792aSdrh */ 7704794b980Sdrh for(i=0; i<pEList->nExpr; i++){ 771cce7d176Sdrh if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){ 7729bb61fe7Sdrh return 1; 773cce7d176Sdrh } 7742282792aSdrh if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ 7759bb61fe7Sdrh return 1; 776cce7d176Sdrh } 777cce7d176Sdrh } 778cce7d176Sdrh if( pWhere ){ 779cce7d176Sdrh if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ 7809bb61fe7Sdrh return 1; 781cce7d176Sdrh } 782cce7d176Sdrh if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ 7839bb61fe7Sdrh return 1; 784cce7d176Sdrh } 785cce7d176Sdrh } 786cce7d176Sdrh if( pOrderBy ){ 787cce7d176Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 7882282792aSdrh Expr *pE = pOrderBy->a[i].pExpr; 7892282792aSdrh if( sqliteExprResolveIds(pParse, pTabList, pE) ){ 7909bb61fe7Sdrh return 1; 791cce7d176Sdrh } 7922282792aSdrh if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ 7939bb61fe7Sdrh return 1; 794cce7d176Sdrh } 795cce7d176Sdrh } 796cce7d176Sdrh } 7972282792aSdrh if( pGroupBy ){ 7982282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 7992282792aSdrh Expr *pE = pGroupBy->a[i].pExpr; 8002282792aSdrh if( sqliteExprResolveIds(pParse, pTabList, pE) ){ 8012282792aSdrh return 1; 8022282792aSdrh } 8032282792aSdrh if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ 8042282792aSdrh return 1; 8052282792aSdrh } 8062282792aSdrh } 8072282792aSdrh } 8082282792aSdrh if( pHaving ){ 8092282792aSdrh if( pGroupBy==0 ){ 810da93281eSdrh sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " 811da93281eSdrh "before HAVING", 0); 8122282792aSdrh pParse->nErr++; 8132282792aSdrh return 1; 8142282792aSdrh } 8152282792aSdrh if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){ 8162282792aSdrh return 1; 8172282792aSdrh } 818da93281eSdrh if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ 8192282792aSdrh return 1; 8202282792aSdrh } 821cce7d176Sdrh } 822cce7d176Sdrh 8232282792aSdrh /* Do an analysis of aggregate expressions. 824efb7251dSdrh */ 8252282792aSdrh if( isAgg ){ 826aaf88729Sdrh assert( pParse->nAgg==0 && pParse->iAggCount<0 ); 8272282792aSdrh for(i=0; i<pEList->nExpr; i++){ 8282282792aSdrh if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ 8292282792aSdrh return 1; 8302282792aSdrh } 8312282792aSdrh } 8322282792aSdrh if( pGroupBy ){ 8332282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 8342282792aSdrh if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ 8352282792aSdrh return 1; 8362282792aSdrh } 8372282792aSdrh } 8382282792aSdrh } 8392282792aSdrh if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ 8402282792aSdrh return 1; 8412282792aSdrh } 842191b690eSdrh if( pOrderBy ){ 843191b690eSdrh for(i=0; i<pOrderBy->nExpr; i++){ 844191b690eSdrh if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ 845191b690eSdrh return 1; 846191b690eSdrh } 847191b690eSdrh } 848191b690eSdrh } 849efb7251dSdrh } 850efb7251dSdrh 851cce7d176Sdrh /* Begin generating code. 852cce7d176Sdrh */ 853daffd0e5Sdrh v = sqliteGetVdbe(pParse); 854daffd0e5Sdrh if( v==0 ) return 1; 855cce7d176Sdrh if( pOrderBy ){ 856*99fcd718Sdrh sqliteVdbeAddOp(v, OP_SortOpen, 0, 0); 857cce7d176Sdrh } 858cce7d176Sdrh 8592282792aSdrh /* Identify column names if we will be using in the callback. This 86019a775c2Sdrh ** step is skipped if the output is going to a table or a memory cell. 861cce7d176Sdrh */ 862fef5208cSdrh if( eDest==SRT_Callback ){ 863d8bc7086Sdrh generateColumnNames(pParse, pTabList, pEList); 864cce7d176Sdrh } 865cce7d176Sdrh 8662282792aSdrh /* Reset the aggregator 867cce7d176Sdrh */ 868cce7d176Sdrh if( isAgg ){ 869*99fcd718Sdrh sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); 870cce7d176Sdrh } 871cce7d176Sdrh 87219a775c2Sdrh /* Initialize the memory cell to NULL 87319a775c2Sdrh */ 874fef5208cSdrh if( eDest==SRT_Mem ){ 875*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 876*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 0); 87719a775c2Sdrh } 87819a775c2Sdrh 879cce7d176Sdrh /* Begin the database scan 880cce7d176Sdrh */ 88119a775c2Sdrh if( isDistinct ){ 882*99fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0); 883efb7251dSdrh } 884cce7d176Sdrh pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); 8859bb61fe7Sdrh if( pWInfo==0 ) return 1; 886cce7d176Sdrh 8872282792aSdrh /* Use the standard inner loop if we are not dealing with 8882282792aSdrh ** aggregates 889cce7d176Sdrh */ 890da9d6c45Sdrh if( !isAgg ){ 89182c3d636Sdrh if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, 8922282792aSdrh pWInfo->iContinue, pWInfo->iBreak) ){ 8932282792aSdrh return 1; 894cce7d176Sdrh } 895da9d6c45Sdrh } 896cce7d176Sdrh 8972282792aSdrh /* If we are dealing with aggregates, then to the special aggregate 8982282792aSdrh ** processing. 899efb7251dSdrh */ 9002282792aSdrh else{ 9012282792aSdrh int doFocus; 9022282792aSdrh if( pGroupBy ){ 9032282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 9042282792aSdrh sqliteExprCode(pParse, pGroupBy->a[i].pExpr); 905efb7251dSdrh } 906*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); 9072282792aSdrh doFocus = 1; 908cce7d176Sdrh }else{ 9092282792aSdrh doFocus = 0; 9102282792aSdrh for(i=0; i<pParse->nAgg; i++){ 9112282792aSdrh if( !pParse->aAgg[i].isAgg ){ 9122282792aSdrh doFocus = 1; 9132282792aSdrh break; 914cce7d176Sdrh } 9152282792aSdrh } 9162282792aSdrh if( doFocus ){ 917*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 918*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, "", P3_STATIC); 9192282792aSdrh } 9202282792aSdrh } 9212282792aSdrh if( doFocus ){ 9222282792aSdrh int lbl1 = sqliteVdbeMakeLabel(v); 923*99fcd718Sdrh sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1); 9242282792aSdrh for(i=0; i<pParse->nAgg; i++){ 9252282792aSdrh if( pParse->aAgg[i].isAgg ) continue; 9262282792aSdrh sqliteExprCode(pParse, pParse->aAgg[i].pExpr); 927*99fcd718Sdrh sqliteVdbeAddOp(v, OP_AggSet, 0, i); 9282282792aSdrh } 9292282792aSdrh sqliteVdbeResolveLabel(v, lbl1); 9302282792aSdrh } 9312282792aSdrh for(i=0; i<pParse->nAgg; i++){ 9322282792aSdrh Expr *pE; 9332282792aSdrh int op; 9342282792aSdrh if( !pParse->aAgg[i].isAgg ) continue; 9352282792aSdrh pE = pParse->aAgg[i].pExpr; 9362282792aSdrh if( pE==0 ){ 937*99fcd718Sdrh sqliteVdbeAddOp(v, OP_AggIncr, 1, i); 9382282792aSdrh continue; 9392282792aSdrh } 9402282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 9412282792aSdrh assert( pE->pList!=0 && pE->pList->nExpr==1 ); 9422282792aSdrh sqliteExprCode(pParse, pE->pList->a[0].pExpr); 943*99fcd718Sdrh sqliteVdbeAddOp(v, OP_AggGet, 0, i); 944967e8b73Sdrh switch( pE->iColumn ){ 9452282792aSdrh case FN_Min: op = OP_Min; break; 9462282792aSdrh case FN_Max: op = OP_Max; break; 9472282792aSdrh case FN_Avg: op = OP_Add; break; 9482282792aSdrh case FN_Sum: op = OP_Add; break; 9492282792aSdrh } 950*99fcd718Sdrh sqliteVdbeAddOp(v, op, 0, 0); 951*99fcd718Sdrh sqliteVdbeAddOp(v, OP_AggSet, 0, i); 9522282792aSdrh } 9532282792aSdrh } 9542282792aSdrh 955cce7d176Sdrh 956cce7d176Sdrh /* End the database scan loop. 957cce7d176Sdrh */ 958cce7d176Sdrh sqliteWhereEnd(pWInfo); 959cce7d176Sdrh 9602282792aSdrh /* If we are processing aggregates, we need to set up a second loop 9612282792aSdrh ** over all of the aggregate values and process them. 9622282792aSdrh */ 9632282792aSdrh if( isAgg ){ 9642282792aSdrh int endagg = sqliteVdbeMakeLabel(v); 9652282792aSdrh int startagg; 966*99fcd718Sdrh startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg); 9672282792aSdrh pParse->useAgg = 1; 9682282792aSdrh if( pHaving ){ 9692282792aSdrh sqliteExprIfFalse(pParse, pHaving, startagg); 9702282792aSdrh } 97182c3d636Sdrh if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, 9722282792aSdrh startagg, endagg) ){ 9732282792aSdrh return 1; 9742282792aSdrh } 975*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, startagg); 976*99fcd718Sdrh sqliteVdbeResolveLabel(v, endagg); 977*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Noop, 0, 0); 9782282792aSdrh pParse->useAgg = 0; 9792282792aSdrh } 9802282792aSdrh 981cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 982cce7d176Sdrh ** and send them to the callback one by one. 983cce7d176Sdrh */ 984cce7d176Sdrh if( pOrderBy ){ 985d8bc7086Sdrh generateSortTail(v, pEList->nExpr); 986cce7d176Sdrh } 98710e5e3cfSdrh pParse->nTab = base; 9889bb61fe7Sdrh return 0; 989cce7d176Sdrh } 990