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*68d2e591Sdrh ** $Id: select.c,v 1.107 2002/08/04 00:52:38 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 */ 25ad3cab52Sdrh SrcList *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 */ 309bbca4c1Sdrh int isDistinct, /* true if the DISTINCT keyword is present */ 319bbca4c1Sdrh int nLimit, /* LIMIT value. -1 means not used */ 329bbca4c1Sdrh int nOffset /* OFFSET value. -1 means not used */ 339bb61fe7Sdrh ){ 349bb61fe7Sdrh Select *pNew; 359bb61fe7Sdrh pNew = sqliteMalloc( sizeof(*pNew) ); 36daffd0e5Sdrh if( pNew==0 ){ 37daffd0e5Sdrh sqliteExprListDelete(pEList); 38ad3cab52Sdrh sqliteSrcListDelete(pSrc); 39daffd0e5Sdrh sqliteExprDelete(pWhere); 40daffd0e5Sdrh sqliteExprListDelete(pGroupBy); 41daffd0e5Sdrh sqliteExprDelete(pHaving); 42daffd0e5Sdrh sqliteExprListDelete(pOrderBy); 43daffd0e5Sdrh }else{ 449bb61fe7Sdrh pNew->pEList = pEList; 459bb61fe7Sdrh pNew->pSrc = pSrc; 469bb61fe7Sdrh pNew->pWhere = pWhere; 479bb61fe7Sdrh pNew->pGroupBy = pGroupBy; 489bb61fe7Sdrh pNew->pHaving = pHaving; 499bb61fe7Sdrh pNew->pOrderBy = pOrderBy; 509bb61fe7Sdrh pNew->isDistinct = isDistinct; 5182c3d636Sdrh pNew->op = TK_SELECT; 529bbca4c1Sdrh pNew->nLimit = nLimit; 539bbca4c1Sdrh pNew->nOffset = nOffset; 54daffd0e5Sdrh } 559bb61fe7Sdrh return pNew; 569bb61fe7Sdrh } 579bb61fe7Sdrh 589bb61fe7Sdrh /* 5901f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the 6001f3f253Sdrh ** type of join. Return an integer constant that expresses that type 6101f3f253Sdrh ** in terms of the following bit values: 6201f3f253Sdrh ** 6301f3f253Sdrh ** JT_INNER 6401f3f253Sdrh ** JT_OUTER 6501f3f253Sdrh ** JT_NATURAL 6601f3f253Sdrh ** JT_LEFT 6701f3f253Sdrh ** JT_RIGHT 6801f3f253Sdrh ** 6901f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT. 7001f3f253Sdrh ** 7101f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return 7201f3f253Sdrh ** a join type, but put an error in the pParse structure. 7301f3f253Sdrh */ 7401f3f253Sdrh int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ 7501f3f253Sdrh int jointype = 0; 7601f3f253Sdrh Token *apAll[3]; 7701f3f253Sdrh Token *p; 7801f3f253Sdrh static struct { 7901f3f253Sdrh const char *zKeyword; 8001f3f253Sdrh int nChar; 8101f3f253Sdrh int code; 8201f3f253Sdrh } keywords[] = { 8301f3f253Sdrh { "natural", 7, JT_NATURAL }, 84195e6967Sdrh { "left", 4, JT_LEFT|JT_OUTER }, 85195e6967Sdrh { "right", 5, JT_RIGHT|JT_OUTER }, 86195e6967Sdrh { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, 8701f3f253Sdrh { "outer", 5, JT_OUTER }, 8801f3f253Sdrh { "inner", 5, JT_INNER }, 8901f3f253Sdrh { "cross", 5, JT_INNER }, 9001f3f253Sdrh }; 9101f3f253Sdrh int i, j; 9201f3f253Sdrh apAll[0] = pA; 9301f3f253Sdrh apAll[1] = pB; 9401f3f253Sdrh apAll[2] = pC; 95195e6967Sdrh for(i=0; i<3 && apAll[i]; i++){ 9601f3f253Sdrh p = apAll[i]; 9701f3f253Sdrh for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ 9801f3f253Sdrh if( p->n==keywords[j].nChar 9901f3f253Sdrh && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ 10001f3f253Sdrh jointype |= keywords[j].code; 10101f3f253Sdrh break; 10201f3f253Sdrh } 10301f3f253Sdrh } 10401f3f253Sdrh if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ 10501f3f253Sdrh jointype |= JT_ERROR; 10601f3f253Sdrh break; 10701f3f253Sdrh } 10801f3f253Sdrh } 109ad2d8307Sdrh if( 110ad2d8307Sdrh (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || 111195e6967Sdrh (jointype & JT_ERROR)!=0 112ad2d8307Sdrh ){ 11301f3f253Sdrh static Token dummy = { 0, 0 }; 11401f3f253Sdrh char *zSp1 = " ", *zSp2 = " "; 11501f3f253Sdrh if( pB==0 ){ pB = &dummy; zSp1 = 0; } 11601f3f253Sdrh if( pC==0 ){ pC = &dummy; zSp2 = 0; } 11701f3f253Sdrh sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0, 11801f3f253Sdrh pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0); 11901f3f253Sdrh pParse->nErr++; 12001f3f253Sdrh jointype = JT_INNER; 121195e6967Sdrh }else if( jointype & JT_RIGHT ){ 122195e6967Sdrh sqliteSetString(&pParse->zErrMsg, 123195e6967Sdrh "RIGHT and FULL OUTER JOINs are not currently supported", 0); 124195e6967Sdrh pParse->nErr++; 125195e6967Sdrh jointype = JT_INNER; 12601f3f253Sdrh } 12701f3f253Sdrh return jointype; 12801f3f253Sdrh } 12901f3f253Sdrh 13001f3f253Sdrh /* 131ad2d8307Sdrh ** Return the index of a column in a table. Return -1 if the column 132ad2d8307Sdrh ** is not contained in the table. 133ad2d8307Sdrh */ 134ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){ 135ad2d8307Sdrh int i; 136ad2d8307Sdrh for(i=0; i<pTab->nCol; i++){ 137ad2d8307Sdrh if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; 138ad2d8307Sdrh } 139ad2d8307Sdrh return -1; 140ad2d8307Sdrh } 141ad2d8307Sdrh 142ad2d8307Sdrh /* 143ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the 144ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2. 145ad2d8307Sdrh */ 146ad2d8307Sdrh static void addWhereTerm( 147ad2d8307Sdrh const char *zCol, /* Name of the column */ 148ad2d8307Sdrh const Table *pTab1, /* First table */ 149ad2d8307Sdrh const Table *pTab2, /* Second table */ 150ad2d8307Sdrh Expr **ppExpr /* Add the equality term to this expression */ 151ad2d8307Sdrh ){ 152ad2d8307Sdrh Token dummy; 153ad2d8307Sdrh Expr *pE1a, *pE1b, *pE1c; 154ad2d8307Sdrh Expr *pE2a, *pE2b, *pE2c; 155ad2d8307Sdrh Expr *pE; 156ad2d8307Sdrh 157ad2d8307Sdrh dummy.z = zCol; 158ad2d8307Sdrh dummy.n = strlen(zCol); 159ad2d8307Sdrh pE1a = sqliteExpr(TK_ID, 0, 0, &dummy); 1603b167c75Sdrh pE1a->staticToken = 1; 161ad2d8307Sdrh pE2a = sqliteExpr(TK_ID, 0, 0, &dummy); 1623b167c75Sdrh pE2a->staticToken = 1; 163ad2d8307Sdrh dummy.z = pTab1->zName; 164ad2d8307Sdrh dummy.n = strlen(dummy.z); 165ad2d8307Sdrh pE1b = sqliteExpr(TK_ID, 0, 0, &dummy); 1663b167c75Sdrh pE1b->staticToken = 1; 167ad2d8307Sdrh dummy.z = pTab2->zName; 168ad2d8307Sdrh dummy.n = strlen(dummy.z); 169ad2d8307Sdrh pE2b = sqliteExpr(TK_ID, 0, 0, &dummy); 1703b167c75Sdrh pE2b->staticToken = 1; 171ad2d8307Sdrh pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0); 172ad2d8307Sdrh pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0); 173ad2d8307Sdrh pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0); 1741cc093c2Sdrh pE->isJoinExpr = 1; 175ad2d8307Sdrh if( *ppExpr ){ 176ad2d8307Sdrh *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0); 177ad2d8307Sdrh }else{ 178ad2d8307Sdrh *ppExpr = pE; 179ad2d8307Sdrh } 180ad2d8307Sdrh } 181ad2d8307Sdrh 182ad2d8307Sdrh /* 1831cc093c2Sdrh ** Set the Expr.isJoinExpr flag on all terms of the given expression. 1841cc093c2Sdrh ** 1851cc093c2Sdrh ** The Expr.isJoinExpr flag is used at on terms of an expression to tell 1861cc093c2Sdrh ** the LEFT OUTER JOIN processing logic that this term is part of the 1871cc093c2Sdrh ** join restriction and not a part of the more general WHERE clause. 1881cc093c2Sdrh */ 1891cc093c2Sdrh static void setJoinExpr(Expr *p){ 1901cc093c2Sdrh while( p ){ 1911cc093c2Sdrh p->isJoinExpr = 1; 1921cc093c2Sdrh setJoinExpr(p->pLeft); 1931cc093c2Sdrh p = p->pRight; 1941cc093c2Sdrh } 1951cc093c2Sdrh } 1961cc093c2Sdrh 1971cc093c2Sdrh /* 198ad2d8307Sdrh ** This routine processes the join information for a SELECT statement. 199ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause. 200ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms. 201ad2d8307Sdrh ** 202ad2d8307Sdrh ** This routine returns the number of errors encountered. 203ad2d8307Sdrh */ 204ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){ 205ad2d8307Sdrh SrcList *pSrc; 206ad2d8307Sdrh int i, j; 207ad2d8307Sdrh pSrc = p->pSrc; 208ad2d8307Sdrh for(i=0; i<pSrc->nSrc-1; i++){ 209ad2d8307Sdrh struct SrcList_item *pTerm = &pSrc->a[i]; 210ad2d8307Sdrh struct SrcList_item *pOther = &pSrc->a[i+1]; 211ad2d8307Sdrh 212ad2d8307Sdrh if( pTerm->pTab==0 || pOther->pTab==0 ) continue; 213ad2d8307Sdrh 214ad2d8307Sdrh /* When the NATURAL keyword is present, add WHERE clause terms for 215ad2d8307Sdrh ** every column that the two tables have in common. 216ad2d8307Sdrh */ 217ad2d8307Sdrh if( pTerm->jointype & JT_NATURAL ){ 218ad2d8307Sdrh Table *pTab; 219ad2d8307Sdrh if( pTerm->pOn || pTerm->pUsing ){ 220ad2d8307Sdrh sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have " 221ad2d8307Sdrh "an ON or USING clause", 0); 222ad2d8307Sdrh pParse->nErr++; 223ad2d8307Sdrh return 1; 224ad2d8307Sdrh } 225ad2d8307Sdrh pTab = pTerm->pTab; 226ad2d8307Sdrh for(j=0; j<pTab->nCol; j++){ 227ad2d8307Sdrh if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){ 228ad2d8307Sdrh addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere); 229ad2d8307Sdrh } 230ad2d8307Sdrh } 231ad2d8307Sdrh } 232ad2d8307Sdrh 233ad2d8307Sdrh /* Disallow both ON and USING clauses in the same join 234ad2d8307Sdrh */ 235ad2d8307Sdrh if( pTerm->pOn && pTerm->pUsing ){ 236ad2d8307Sdrh sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING " 237ad2d8307Sdrh "clauses in the same join", 0); 238ad2d8307Sdrh pParse->nErr++; 239ad2d8307Sdrh return 1; 240ad2d8307Sdrh } 241ad2d8307Sdrh 242ad2d8307Sdrh /* Add the ON clause to the end of the WHERE clause, connected by 243ad2d8307Sdrh ** and AND operator. 244ad2d8307Sdrh */ 245ad2d8307Sdrh if( pTerm->pOn ){ 2461cc093c2Sdrh setJoinExpr(pTerm->pOn); 247ad2d8307Sdrh if( p->pWhere==0 ){ 248ad2d8307Sdrh p->pWhere = pTerm->pOn; 249ad2d8307Sdrh }else{ 250ad2d8307Sdrh p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0); 251ad2d8307Sdrh } 252ad2d8307Sdrh pTerm->pOn = 0; 253ad2d8307Sdrh } 254ad2d8307Sdrh 255ad2d8307Sdrh /* Create extra terms on the WHERE clause for each column named 256ad2d8307Sdrh ** in the USING clause. Example: If the two tables to be joined are 257ad2d8307Sdrh ** A and B and the USING clause names X, Y, and Z, then add this 258ad2d8307Sdrh ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z 259ad2d8307Sdrh ** Report an error if any column mentioned in the USING clause is 260ad2d8307Sdrh ** not contained in both tables to be joined. 261ad2d8307Sdrh */ 262ad2d8307Sdrh if( pTerm->pUsing ){ 263ad2d8307Sdrh IdList *pList; 264ad2d8307Sdrh int j; 265ad2d8307Sdrh assert( i<pSrc->nSrc-1 ); 266ad2d8307Sdrh pList = pTerm->pUsing; 267ad2d8307Sdrh for(j=0; j<pList->nId; j++){ 268bf5cd97eSdrh if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 || 269bf5cd97eSdrh columnIndex(pOther->pTab, pList->a[j].zName)<0 ){ 270ad2d8307Sdrh sqliteSetString(&pParse->zErrMsg, "cannot join using column ", 271bf5cd97eSdrh pList->a[j].zName, " - column not present in both tables", 0); 272ad2d8307Sdrh pParse->nErr++; 273ad2d8307Sdrh return 1; 274ad2d8307Sdrh } 275bf5cd97eSdrh addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere); 276ad2d8307Sdrh } 277ad2d8307Sdrh } 278ad2d8307Sdrh } 279ad2d8307Sdrh return 0; 280ad2d8307Sdrh } 281ad2d8307Sdrh 282ad2d8307Sdrh /* 2839bb61fe7Sdrh ** Delete the given Select structure and all of its substructures. 2849bb61fe7Sdrh */ 2859bb61fe7Sdrh void sqliteSelectDelete(Select *p){ 28682c3d636Sdrh if( p==0 ) return; 2879bb61fe7Sdrh sqliteExprListDelete(p->pEList); 288ad3cab52Sdrh sqliteSrcListDelete(p->pSrc); 2899bb61fe7Sdrh sqliteExprDelete(p->pWhere); 2909bb61fe7Sdrh sqliteExprListDelete(p->pGroupBy); 2919bb61fe7Sdrh sqliteExprDelete(p->pHaving); 2929bb61fe7Sdrh sqliteExprListDelete(p->pOrderBy); 29382c3d636Sdrh sqliteSelectDelete(p->pPrior); 294a76b5dfcSdrh sqliteFree(p->zSelect); 2959bb61fe7Sdrh sqliteFree(p); 2969bb61fe7Sdrh } 2979bb61fe7Sdrh 2989bb61fe7Sdrh /* 2992282792aSdrh ** Delete the aggregate information from the parse structure. 3002282792aSdrh */ 3011d83f052Sdrh static void sqliteAggregateInfoReset(Parse *pParse){ 3022282792aSdrh sqliteFree(pParse->aAgg); 3032282792aSdrh pParse->aAgg = 0; 3042282792aSdrh pParse->nAgg = 0; 3052282792aSdrh pParse->useAgg = 0; 3062282792aSdrh } 3072282792aSdrh 3082282792aSdrh /* 309c926afbcSdrh ** Insert code into "v" that will push the record on the top of the 310c926afbcSdrh ** stack into the sorter. 311c926afbcSdrh */ 312c926afbcSdrh static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){ 313c926afbcSdrh char *zSortOrder; 314c926afbcSdrh int i; 315c926afbcSdrh zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); 316c926afbcSdrh if( zSortOrder==0 ) return; 317c926afbcSdrh for(i=0; i<pOrderBy->nExpr; i++){ 31838640e15Sdrh int order = pOrderBy->a[i].sortOrder; 31938640e15Sdrh int type; 32038640e15Sdrh int c; 32138640e15Sdrh if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){ 32238640e15Sdrh type = SQLITE_SO_TEXT; 32338640e15Sdrh }else if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_NUM ){ 32438640e15Sdrh type = SQLITE_SO_NUM; 325491791a8Sdrh }else if( pParse->db->file_format>=4 ){ 32638640e15Sdrh type = sqliteExprType(pOrderBy->a[i].pExpr); 32738640e15Sdrh }else{ 32838640e15Sdrh type = SQLITE_SO_NUM; 32938640e15Sdrh } 33038640e15Sdrh if( (order & SQLITE_SO_DIRMASK)==SQLITE_SO_ASC ){ 33138640e15Sdrh c = type==SQLITE_SO_TEXT ? 'A' : '+'; 33238640e15Sdrh }else{ 33338640e15Sdrh c = type==SQLITE_SO_TEXT ? 'D' : '-'; 33438640e15Sdrh } 33538640e15Sdrh zSortOrder[i] = c; 336c926afbcSdrh sqliteExprCode(pParse, pOrderBy->a[i].pExpr); 337c926afbcSdrh } 338c926afbcSdrh zSortOrder[pOrderBy->nExpr] = 0; 339c926afbcSdrh sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0); 340c926afbcSdrh sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder)); 341c926afbcSdrh sqliteFree(zSortOrder); 342c926afbcSdrh sqliteVdbeAddOp(v, OP_SortPut, 0, 0); 343c926afbcSdrh } 344c926afbcSdrh 345c926afbcSdrh /* 34638640e15Sdrh ** This routine adds a P3 argument to the last VDBE opcode that was 34738640e15Sdrh ** inserted. The P3 argument added is a string suitable for the 34838640e15Sdrh ** OP_MakeKey or OP_MakeIdxKey opcodes. The string consists of 34938640e15Sdrh ** characters 't' or 'n' depending on whether or not the various 35038640e15Sdrh ** fields of the key to be generated should be treated as numeric 35138640e15Sdrh ** or as text. See the OP_MakeKey and OP_MakeIdxKey opcode 35238640e15Sdrh ** documentation for additional information about the P3 string. 35338640e15Sdrh ** See also the sqliteAddIdxKeyType() routine. 35438640e15Sdrh */ 35538640e15Sdrh void sqliteAddKeyType(Vdbe *v, ExprList *pEList){ 35638640e15Sdrh int nColumn = pEList->nExpr; 35738640e15Sdrh char *zType = sqliteMalloc( nColumn+1 ); 35838640e15Sdrh int i; 35938640e15Sdrh if( zType==0 ) return; 36038640e15Sdrh for(i=0; i<nColumn; i++){ 36138640e15Sdrh zType[i] = sqliteExprType(pEList->a[i].pExpr)==SQLITE_SO_NUM ? 'n' : 't'; 36238640e15Sdrh } 36338640e15Sdrh zType[i] = 0; 36438640e15Sdrh sqliteVdbeChangeP3(v, -1, zType, nColumn); 36538640e15Sdrh sqliteFree(zType); 36638640e15Sdrh } 36738640e15Sdrh 36838640e15Sdrh /* 3692282792aSdrh ** This routine generates the code for the inside of the inner loop 3702282792aSdrh ** of a SELECT. 37182c3d636Sdrh ** 37238640e15Sdrh ** If srcTab and nColumn are both zero, then the pEList expressions 37338640e15Sdrh ** are evaluated in order to get the data for this row. If nColumn>0 37438640e15Sdrh ** then data is pulled from srcTab and pEList is used only to get the 37538640e15Sdrh ** datatypes for each column. 3762282792aSdrh */ 3772282792aSdrh static int selectInnerLoop( 3782282792aSdrh Parse *pParse, /* The parser context */ 379df199a25Sdrh Select *p, /* The complete select statement being coded */ 3802282792aSdrh ExprList *pEList, /* List of values being extracted */ 38182c3d636Sdrh int srcTab, /* Pull data from this table */ 382967e8b73Sdrh int nColumn, /* Number of columns in the source table */ 3832282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 3842282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 3852282792aSdrh int eDest, /* How to dispose of the results */ 3862282792aSdrh int iParm, /* An argument to the disposal method */ 3872282792aSdrh int iContinue, /* Jump here to continue with next row */ 3882282792aSdrh int iBreak /* Jump here to break out of the inner loop */ 3892282792aSdrh ){ 3902282792aSdrh Vdbe *v = pParse->pVdbe; 3912282792aSdrh int i; 39238640e15Sdrh 393daffd0e5Sdrh if( v==0 ) return 0; 39438640e15Sdrh assert( pEList!=0 ); 3952282792aSdrh 396df199a25Sdrh /* If there was a LIMIT clause on the SELECT statement, then do the check 397df199a25Sdrh ** to see if this row should be output. 398df199a25Sdrh */ 399df199a25Sdrh if( pOrderBy==0 ){ 400df199a25Sdrh if( p->nOffset>0 ){ 401d11d382cSdrh int addr = sqliteVdbeCurrentAddr(v); 402d11d382cSdrh sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+2); 403d11d382cSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iContinue); 404df199a25Sdrh } 405d11d382cSdrh if( p->nLimit>=0 ){ 406d11d382cSdrh sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, iBreak); 407df199a25Sdrh } 408df199a25Sdrh } 409df199a25Sdrh 410967e8b73Sdrh /* Pull the requested columns. 4112282792aSdrh */ 41238640e15Sdrh if( nColumn>0 ){ 413967e8b73Sdrh for(i=0; i<nColumn; i++){ 41499fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i); 41582c3d636Sdrh } 41638640e15Sdrh }else{ 41738640e15Sdrh nColumn = pEList->nExpr; 41838640e15Sdrh for(i=0; i<pEList->nExpr; i++){ 41938640e15Sdrh sqliteExprCode(pParse, pEList->a[i].pExpr); 42038640e15Sdrh } 42182c3d636Sdrh } 4222282792aSdrh 423daffd0e5Sdrh /* If the DISTINCT keyword was present on the SELECT statement 424daffd0e5Sdrh ** and this row has been seen before, then do not make this row 425daffd0e5Sdrh ** part of the result. 4262282792aSdrh */ 427f5905aa7Sdrh if( distinct>=0 && pEList && pEList->nExpr>0 ){ 4280bd1f4eaSdrh #if NULL_ALWAYS_DISTINCT 4290bd1f4eaSdrh sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7); 4300bd1f4eaSdrh #endif 43199fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1); 432491791a8Sdrh if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pEList); 433f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3); 43499fcd718Sdrh sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); 43599fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iContinue); 43699fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4376b12545fSdrh sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0); 4382282792aSdrh } 43982c3d636Sdrh 440c926afbcSdrh switch( eDest ){ 44182c3d636Sdrh /* In this mode, write each query result to the key of the temporary 44282c3d636Sdrh ** table iParm. 4432282792aSdrh */ 444c926afbcSdrh case SRT_Union: { 4450bd1f4eaSdrh sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 446f5905aa7Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4476b12545fSdrh sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); 448c926afbcSdrh break; 449c926afbcSdrh } 45082c3d636Sdrh 4515974a30fSdrh /* Store the result as data using a unique key. 4525974a30fSdrh */ 453c926afbcSdrh case SRT_Table: 454c926afbcSdrh case SRT_TempTable: { 45599fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); 456c926afbcSdrh if( pOrderBy ){ 457c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 458c926afbcSdrh }else{ 45999fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); 46099fcd718Sdrh sqliteVdbeAddOp(v, OP_Pull, 1, 0); 4616b12545fSdrh sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0); 462c926afbcSdrh } 463c926afbcSdrh break; 464c926afbcSdrh } 4655974a30fSdrh 46682c3d636Sdrh /* Construct a record from the query result, but instead of 46782c3d636Sdrh ** saving that record, use it as a key to delete elements from 46882c3d636Sdrh ** the temporary table iParm. 46982c3d636Sdrh */ 470c926afbcSdrh case SRT_Except: { 4710bd1f4eaSdrh int addr; 4720bd1f4eaSdrh addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 47399fcd718Sdrh sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3); 47499fcd718Sdrh sqliteVdbeAddOp(v, OP_Delete, iParm, 0); 475c926afbcSdrh break; 476c926afbcSdrh } 4772282792aSdrh 4782282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 4792282792aSdrh ** then there should be a single item on the stack. Write this 4802282792aSdrh ** item into the set table with bogus data. 4812282792aSdrh */ 482c926afbcSdrh case SRT_Set: { 483a9f9d1c0Sdrh int lbl = sqliteVdbeMakeLabel(v); 484967e8b73Sdrh assert( nColumn==1 ); 485a9f9d1c0Sdrh sqliteVdbeAddOp(v, OP_IsNull, -1, lbl); 486c926afbcSdrh if( pOrderBy ){ 487c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 488c926afbcSdrh }else{ 489a9f9d1c0Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4906b12545fSdrh sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); 491c926afbcSdrh } 492a9f9d1c0Sdrh sqliteVdbeResolveLabel(v, lbl); 493c926afbcSdrh break; 494c926afbcSdrh } 49582c3d636Sdrh 4962282792aSdrh /* If this is a scalar select that is part of an expression, then 4972282792aSdrh ** store the results in the appropriate memory cell and break out 4982282792aSdrh ** of the scan loop. 4992282792aSdrh */ 500c926afbcSdrh case SRT_Mem: { 501967e8b73Sdrh assert( nColumn==1 ); 502c926afbcSdrh if( pOrderBy ){ 503c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 504c926afbcSdrh }else{ 5058721ce4aSdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); 50699fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iBreak); 507c926afbcSdrh } 508c926afbcSdrh break; 509c926afbcSdrh } 5102282792aSdrh 511f46f905aSdrh /* Send the data to the callback function. 512f46f905aSdrh */ 513f46f905aSdrh case SRT_Callback: 514f46f905aSdrh case SRT_Sorter: { 515f46f905aSdrh if( pOrderBy ){ 516f46f905aSdrh sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0); 517f46f905aSdrh pushOntoSorter(pParse, v, pOrderBy); 518f46f905aSdrh }else{ 519f46f905aSdrh assert( eDest==SRT_Callback ); 520f46f905aSdrh sqliteVdbeAddOp(v, OP_Callback, nColumn, 0); 521f46f905aSdrh } 522f46f905aSdrh break; 523f46f905aSdrh } 524f46f905aSdrh 525d7489c39Sdrh /* Discard the results. This is used for SELECT statements inside 526d7489c39Sdrh ** the body of a TRIGGER. The purpose of such selects is to call 527d7489c39Sdrh ** user-defined functions that have side effects. We do not care 528d7489c39Sdrh ** about the actual results of the select. 529d7489c39Sdrh */ 530c926afbcSdrh default: { 531f46f905aSdrh assert( eDest==SRT_Discard ); 532f46f905aSdrh sqliteVdbeAddOp(v, OP_Pop, nColumn, 0); 533c926afbcSdrh break; 534c926afbcSdrh } 535c926afbcSdrh } 53682c3d636Sdrh return 0; 53782c3d636Sdrh } 53882c3d636Sdrh 53982c3d636Sdrh /* 540d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 541d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 542d8bc7086Sdrh ** we need to run the sorter and output the results. The following 543d8bc7086Sdrh ** routine generates the code needed to do that. 544d8bc7086Sdrh */ 545c926afbcSdrh static void generateSortTail( 546c926afbcSdrh Select *p, /* The SELECT statement */ 547c926afbcSdrh Vdbe *v, /* Generate code into this VDBE */ 548c926afbcSdrh int nColumn, /* Number of columns of data */ 549c926afbcSdrh int eDest, /* Write the sorted results here */ 550c926afbcSdrh int iParm /* Optional parameter associated with eDest */ 551c926afbcSdrh ){ 552d8bc7086Sdrh int end = sqliteVdbeMakeLabel(v); 553d8bc7086Sdrh int addr; 554f46f905aSdrh if( eDest==SRT_Sorter ) return; 55599fcd718Sdrh sqliteVdbeAddOp(v, OP_Sort, 0, 0); 55699fcd718Sdrh addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end); 557df199a25Sdrh if( p->nOffset>0 ){ 558d11d382cSdrh sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+4); 559d11d382cSdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 560d11d382cSdrh sqliteVdbeAddOp(v, OP_Goto, 0, addr); 561df199a25Sdrh } 562d11d382cSdrh if( p->nLimit>=0 ){ 563d11d382cSdrh sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, end); 564df199a25Sdrh } 565c926afbcSdrh switch( eDest ){ 566c926afbcSdrh case SRT_Callback: { 567df199a25Sdrh sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0); 568c926afbcSdrh break; 569c926afbcSdrh } 570c926afbcSdrh case SRT_Table: 571c926afbcSdrh case SRT_TempTable: { 572c926afbcSdrh sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); 573c926afbcSdrh sqliteVdbeAddOp(v, OP_Pull, 1, 0); 574c926afbcSdrh sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0); 575c926afbcSdrh break; 576c926afbcSdrh } 577c926afbcSdrh case SRT_Set: { 578c926afbcSdrh assert( nColumn==1 ); 579c926afbcSdrh sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3); 580c926afbcSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 581c926afbcSdrh sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); 582c926afbcSdrh break; 583c926afbcSdrh } 584c926afbcSdrh case SRT_Mem: { 585c926afbcSdrh assert( nColumn==1 ); 586c926afbcSdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); 587c926afbcSdrh sqliteVdbeAddOp(v, OP_Goto, 0, end); 588c926afbcSdrh break; 589c926afbcSdrh } 590c926afbcSdrh default: { 591f46f905aSdrh /* Do nothing */ 592c926afbcSdrh break; 593c926afbcSdrh } 594c926afbcSdrh } 59599fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, addr); 59699fcd718Sdrh sqliteVdbeResolveLabel(v, end); 597a8b38d28Sdrh sqliteVdbeAddOp(v, OP_SortReset, 0, 0); 598d8bc7086Sdrh } 599d8bc7086Sdrh 600d8bc7086Sdrh /* 60182c3d636Sdrh ** Generate code that will tell the VDBE how many columns there 60282c3d636Sdrh ** are in the result and the name for each column. This information 60382c3d636Sdrh ** is used to provide "argc" and "azCol[]" values in the callback. 60482c3d636Sdrh */ 605832508b7Sdrh static void generateColumnNames( 606832508b7Sdrh Parse *pParse, /* Parser context */ 607832508b7Sdrh int base, /* VDBE cursor corresponding to first entry in pTabList */ 608ad3cab52Sdrh SrcList *pTabList, /* List of tables */ 609832508b7Sdrh ExprList *pEList /* Expressions defining the result set */ 610832508b7Sdrh ){ 611d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 61282c3d636Sdrh int i; 613daffd0e5Sdrh if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return; 614d8bc7086Sdrh pParse->colNamesSet = 1; 6155080aaa7Sdrh if( pParse->db->flags & SQLITE_ReportTypes ){ 6165080aaa7Sdrh sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr*2, 0); 6175080aaa7Sdrh }else{ 6185080aaa7Sdrh sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0); 6195080aaa7Sdrh } 62082c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 62182c3d636Sdrh Expr *p; 622b1363206Sdrh char *zType = 0; 6231bee3d7bSdrh int showFullNames; 62482c3d636Sdrh if( pEList->a[i].zName ){ 62582c3d636Sdrh char *zName = pEList->a[i].zName; 62699fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 62799fcd718Sdrh sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); 62882c3d636Sdrh continue; 62982c3d636Sdrh } 63082c3d636Sdrh p = pEList->a[i].pExpr; 631daffd0e5Sdrh if( p==0 ) continue; 6321bee3d7bSdrh showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0; 633fa173a76Sdrh if( p->op==TK_COLUMN && pTabList ){ 634832508b7Sdrh Table *pTab = pTabList->a[p->iTable - base].pTab; 63597665873Sdrh char *zCol; 6368aff1015Sdrh int iCol = p->iColumn; 6378aff1015Sdrh if( iCol<0 ) iCol = pTab->iPKey; 63897665873Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 639b1363206Sdrh if( iCol<0 ){ 640b1363206Sdrh zCol = "_ROWID_"; 641b1363206Sdrh zType = "INTEGER"; 642b1363206Sdrh }else{ 643b1363206Sdrh zCol = pTab->aCol[iCol].zName; 644b1363206Sdrh zType = pTab->aCol[iCol].zType; 645b1363206Sdrh } 646fa173a76Sdrh if( p->span.z && p->span.z[0] && !showFullNames ){ 647fa173a76Sdrh int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); 648fa173a76Sdrh sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); 649fa173a76Sdrh sqliteVdbeCompressSpace(v, addr); 650fa173a76Sdrh }else if( pTabList->nSrc>1 || showFullNames ){ 65182c3d636Sdrh char *zName = 0; 65282c3d636Sdrh char *zTab; 65382c3d636Sdrh 654832508b7Sdrh zTab = pTabList->a[p->iTable - base].zAlias; 65501a34661Sdrh if( showFullNames || zTab==0 ) zTab = pTab->zName; 65697665873Sdrh sqliteSetString(&zName, zTab, ".", zCol, 0); 65799fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 65899fcd718Sdrh sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); 65982c3d636Sdrh sqliteFree(zName); 66082c3d636Sdrh }else{ 66199fcd718Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 66222f70c32Sdrh sqliteVdbeChangeP3(v, -1, zCol, 0); 66382c3d636Sdrh } 664fa173a76Sdrh }else if( p->span.z && p->span.z[0] && !showFullNames ){ 665fa173a76Sdrh int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); 666fa173a76Sdrh sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); 667fa173a76Sdrh sqliteVdbeCompressSpace(v, addr); 6681bee3d7bSdrh }else if( p->span.z && p->span.z[0] ){ 6691bee3d7bSdrh int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); 6701bee3d7bSdrh sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); 6711bee3d7bSdrh sqliteVdbeCompressSpace(v, addr); 6721bee3d7bSdrh }else{ 6731bee3d7bSdrh char zName[30]; 6741bee3d7bSdrh assert( p->op!=TK_COLUMN || pTabList==0 ); 6751bee3d7bSdrh sprintf(zName, "column%d", i+1); 6761bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, i, 0); 6771bee3d7bSdrh sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); 67882c3d636Sdrh } 6795080aaa7Sdrh if( pParse->db->flags & SQLITE_ReportTypes ){ 680b1363206Sdrh if( zType==0 ){ 681b1363206Sdrh if( sqliteExprType(p)==SQLITE_SO_TEXT ){ 682b1363206Sdrh zType = "TEXT"; 683b1363206Sdrh }else{ 684b1363206Sdrh zType = "NUMERIC"; 685b1363206Sdrh } 686b1363206Sdrh } 6875080aaa7Sdrh sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0); 688b1363206Sdrh sqliteVdbeChangeP3(v, -1, zType, P3_STATIC); 68982c3d636Sdrh } 69082c3d636Sdrh } 6915080aaa7Sdrh } 69282c3d636Sdrh 69382c3d636Sdrh /* 694d8bc7086Sdrh ** Name of the connection operator, used for error messages. 695d8bc7086Sdrh */ 696d8bc7086Sdrh static const char *selectOpName(int id){ 697d8bc7086Sdrh char *z; 698d8bc7086Sdrh switch( id ){ 699d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 700d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 701d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 702d8bc7086Sdrh default: z = "UNION"; break; 703d8bc7086Sdrh } 704d8bc7086Sdrh return z; 705d8bc7086Sdrh } 706d8bc7086Sdrh 707d8bc7086Sdrh /* 70822f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes 70922f70c32Sdrh ** the result set of that SELECT. 71022f70c32Sdrh */ 71122f70c32Sdrh Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ 71222f70c32Sdrh Table *pTab; 71322f70c32Sdrh int i; 71422f70c32Sdrh ExprList *pEList; 71522f70c32Sdrh static int fillInColumnList(Parse*, Select*); 71622f70c32Sdrh 71722f70c32Sdrh if( fillInColumnList(pParse, pSelect) ){ 71822f70c32Sdrh return 0; 71922f70c32Sdrh } 72022f70c32Sdrh pTab = sqliteMalloc( sizeof(Table) ); 72122f70c32Sdrh if( pTab==0 ){ 72222f70c32Sdrh return 0; 72322f70c32Sdrh } 72422f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 72522f70c32Sdrh pEList = pSelect->pEList; 72622f70c32Sdrh pTab->nCol = pEList->nExpr; 727417be79cSdrh assert( pTab->nCol>0 ); 72822f70c32Sdrh pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 72922f70c32Sdrh for(i=0; i<pTab->nCol; i++){ 73022f70c32Sdrh Expr *p; 73122f70c32Sdrh if( pEList->a[i].zName ){ 73222f70c32Sdrh pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName); 73322f70c32Sdrh }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){ 73422f70c32Sdrh sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); 735d820cb1bSdrh }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z && 736d820cb1bSdrh p->pRight->token.z[0] ){ 737d820cb1bSdrh sqliteSetNString(&pTab->aCol[i].zName, 738d820cb1bSdrh p->pRight->token.z, p->pRight->token.n, 0); 73922f70c32Sdrh }else{ 74022f70c32Sdrh char zBuf[30]; 74122f70c32Sdrh sprintf(zBuf, "column%d", i+1); 74222f70c32Sdrh pTab->aCol[i].zName = sqliteStrDup(zBuf); 74322f70c32Sdrh } 74422f70c32Sdrh } 74522f70c32Sdrh pTab->iPKey = -1; 74622f70c32Sdrh return pTab; 74722f70c32Sdrh } 74822f70c32Sdrh 74922f70c32Sdrh /* 750ad2d8307Sdrh ** For the given SELECT statement, do three things. 751d8bc7086Sdrh ** 752ad3cab52Sdrh ** (1) Fill in the pTabList->a[].pTab fields in the SrcList that 753967e8b73Sdrh ** defines the set of tables that should be scanned. 754d8bc7086Sdrh ** 755ad2d8307Sdrh ** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword 756ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 757ad2d8307Sdrh ** 758ad2d8307Sdrh ** (3) Scan the list of columns in the result set (pEList) looking 75954473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 76054473229Sdrh ** If found, expand each "*" to be every column in every table 76154473229Sdrh ** and TABLE.* to be every column in TABLE. 762d8bc7086Sdrh ** 763d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 764d8bc7086Sdrh ** in pParse and return non-zero. 765d8bc7086Sdrh */ 766d8bc7086Sdrh static int fillInColumnList(Parse *pParse, Select *p){ 76754473229Sdrh int i, j, k, rc; 768ad3cab52Sdrh SrcList *pTabList; 769daffd0e5Sdrh ExprList *pEList; 770a76b5dfcSdrh Table *pTab; 771daffd0e5Sdrh 772daffd0e5Sdrh if( p==0 || p->pSrc==0 ) return 1; 773daffd0e5Sdrh pTabList = p->pSrc; 774daffd0e5Sdrh pEList = p->pEList; 775d8bc7086Sdrh 776d8bc7086Sdrh /* Look up every table in the table list. 777d8bc7086Sdrh */ 778ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 779d8bc7086Sdrh if( pTabList->a[i].pTab ){ 780d8bc7086Sdrh /* This routine has run before! No need to continue */ 781d8bc7086Sdrh return 0; 782d8bc7086Sdrh } 783daffd0e5Sdrh if( pTabList->a[i].zName==0 ){ 78422f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 78522f70c32Sdrh assert( pTabList->a[i].pSelect!=0 ); 786ad2d8307Sdrh if( pTabList->a[i].zAlias==0 ){ 787ad2d8307Sdrh char zFakeName[60]; 788ad2d8307Sdrh sprintf(zFakeName, "sqlite_subquery_%p_", 789ad2d8307Sdrh (void*)pTabList->a[i].pSelect); 790ad2d8307Sdrh sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0); 791ad2d8307Sdrh } 79222f70c32Sdrh pTabList->a[i].pTab = pTab = 79322f70c32Sdrh sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias, 79422f70c32Sdrh pTabList->a[i].pSelect); 79522f70c32Sdrh if( pTab==0 ){ 796daffd0e5Sdrh return 1; 797daffd0e5Sdrh } 79822f70c32Sdrh pTab->isTransient = 1; 79922f70c32Sdrh }else{ 800a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 801a76b5dfcSdrh pTabList->a[i].pTab = pTab = 802a76b5dfcSdrh sqliteFindTable(pParse->db, pTabList->a[i].zName); 803a76b5dfcSdrh if( pTab==0 ){ 804d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "no such table: ", 805d8bc7086Sdrh pTabList->a[i].zName, 0); 806d8bc7086Sdrh pParse->nErr++; 807d8bc7086Sdrh return 1; 808d8bc7086Sdrh } 809a76b5dfcSdrh if( pTab->pSelect ){ 810417be79cSdrh if( sqliteViewGetColumnNames(pParse, pTab) ){ 811417be79cSdrh return 1; 812417be79cSdrh } 813ff78bd2fSdrh pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect); 814a76b5dfcSdrh } 815d8bc7086Sdrh } 81622f70c32Sdrh } 817d8bc7086Sdrh 818ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 819ad2d8307Sdrh */ 820ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 821ad2d8307Sdrh 8227c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 82354473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 82454473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 8257c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 8267c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 8277c917d19Sdrh ** each one to the list of all columns in all tables. 82854473229Sdrh ** 82954473229Sdrh ** The first loop just checks to see if there are any "*" operators 83054473229Sdrh ** that need expanding. 831d8bc7086Sdrh */ 8327c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 83354473229Sdrh Expr *pE = pEList->a[k].pExpr; 83454473229Sdrh if( pE->op==TK_ALL ) break; 83554473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 83654473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 8377c917d19Sdrh } 83854473229Sdrh rc = 0; 8397c917d19Sdrh if( k<pEList->nExpr ){ 84054473229Sdrh /* 84154473229Sdrh ** If we get here it means the result set contains one or more "*" 84254473229Sdrh ** operators that need to be expanded. Loop through each expression 84354473229Sdrh ** in the result set and expand them one by one. 84454473229Sdrh */ 8457c917d19Sdrh struct ExprList_item *a = pEList->a; 8467c917d19Sdrh ExprList *pNew = 0; 8477c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 84854473229Sdrh Expr *pE = a[k].pExpr; 84954473229Sdrh if( pE->op!=TK_ALL && 85054473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 85154473229Sdrh /* This particular expression does not need to be expanded. 85254473229Sdrh */ 8537c917d19Sdrh pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0); 8547c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 8557c917d19Sdrh a[k].pExpr = 0; 8567c917d19Sdrh a[k].zName = 0; 8577c917d19Sdrh }else{ 85854473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 85954473229Sdrh ** expanded. */ 86054473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 86154473229Sdrh Token *pName; /* text of name of TABLE */ 86254473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 86354473229Sdrh pName = &pE->pLeft->token; 86454473229Sdrh }else{ 86554473229Sdrh pName = 0; 86654473229Sdrh } 867ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 868d8bc7086Sdrh Table *pTab = pTabList->a[i].pTab; 86954473229Sdrh char *zTabName = pTabList->a[i].zAlias; 87054473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 87154473229Sdrh zTabName = pTab->zName; 87254473229Sdrh } 87354473229Sdrh if( pName && (zTabName==0 || zTabName[0]==0 || 874c754fa54Sdrh sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 || 875c754fa54Sdrh zTabName[pName->n]!=0) ){ 87654473229Sdrh continue; 87754473229Sdrh } 87854473229Sdrh tableSeen = 1; 879d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 88022f70c32Sdrh Expr *pExpr, *pLeft, *pRight; 881ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 882ad2d8307Sdrh 883ad2d8307Sdrh if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 && 884ad2d8307Sdrh columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){ 885ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 886ad2d8307Sdrh ** table on the right */ 887ad2d8307Sdrh continue; 888ad2d8307Sdrh } 889ad2d8307Sdrh if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){ 890ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 891ad2d8307Sdrh ** using clause from the table on the right. */ 892ad2d8307Sdrh continue; 893ad2d8307Sdrh } 89422f70c32Sdrh pRight = sqliteExpr(TK_ID, 0, 0, 0); 89522f70c32Sdrh if( pRight==0 ) break; 896ad2d8307Sdrh pRight->token.z = zName; 897ad2d8307Sdrh pRight->token.n = strlen(zName); 89854473229Sdrh if( zTabName ){ 89922f70c32Sdrh pLeft = sqliteExpr(TK_ID, 0, 0, 0); 90022f70c32Sdrh if( pLeft==0 ) break; 90154473229Sdrh pLeft->token.z = zTabName; 90254473229Sdrh pLeft->token.n = strlen(zTabName); 90322f70c32Sdrh pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0); 90422f70c32Sdrh if( pExpr==0 ) break; 90522f70c32Sdrh }else{ 90622f70c32Sdrh pExpr = pRight; 90722f70c32Sdrh pExpr->span = pExpr->token; 90822f70c32Sdrh } 9097c917d19Sdrh pNew = sqliteExprListAppend(pNew, pExpr, 0); 910d8bc7086Sdrh } 911d8bc7086Sdrh } 91254473229Sdrh if( !tableSeen ){ 913f5db2d3eSdrh if( pName ){ 91454473229Sdrh sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1, 91554473229Sdrh pName->z, pName->n, 0); 916f5db2d3eSdrh }else{ 917f5db2d3eSdrh sqliteSetString(&pParse->zErrMsg, "no tables specified", 0); 918f5db2d3eSdrh } 91954473229Sdrh rc = 1; 92054473229Sdrh } 9217c917d19Sdrh } 9227c917d19Sdrh } 9237c917d19Sdrh sqliteExprListDelete(pEList); 9247c917d19Sdrh p->pEList = pNew; 925d8bc7086Sdrh } 92654473229Sdrh return rc; 927d8bc7086Sdrh } 928d8bc7086Sdrh 929d8bc7086Sdrh /* 930ff78bd2fSdrh ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers 931ff78bd2fSdrh ** in a select structure. It just sets the pointers to NULL. This 932ff78bd2fSdrh ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect 933ff78bd2fSdrh ** pointer is not NULL, this routine is called recursively on that pointer. 934ff78bd2fSdrh ** 935ff78bd2fSdrh ** This routine is called on the Select structure that defines a 936ff78bd2fSdrh ** VIEW in order to undo any bindings to tables. This is necessary 937ff78bd2fSdrh ** because those tables might be DROPed by a subsequent SQL command. 938ff78bd2fSdrh */ 939ff78bd2fSdrh void sqliteSelectUnbind(Select *p){ 940ff78bd2fSdrh int i; 941ad3cab52Sdrh SrcList *pSrc = p->pSrc; 942ff78bd2fSdrh Table *pTab; 943ff78bd2fSdrh if( p==0 ) return; 944ad3cab52Sdrh for(i=0; i<pSrc->nSrc; i++){ 945ff78bd2fSdrh if( (pTab = pSrc->a[i].pTab)!=0 ){ 946ff78bd2fSdrh if( pTab->isTransient ){ 947ff78bd2fSdrh sqliteDeleteTable(0, pTab); 948ff78bd2fSdrh sqliteSelectDelete(pSrc->a[i].pSelect); 949ff78bd2fSdrh pSrc->a[i].pSelect = 0; 950ff78bd2fSdrh } 951ff78bd2fSdrh pSrc->a[i].pTab = 0; 952ff78bd2fSdrh if( pSrc->a[i].pSelect ){ 953ff78bd2fSdrh sqliteSelectUnbind(pSrc->a[i].pSelect); 954ff78bd2fSdrh } 955ff78bd2fSdrh } 956ff78bd2fSdrh } 957ff78bd2fSdrh } 958ff78bd2fSdrh 959ff78bd2fSdrh /* 960d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 961d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 962967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 963d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 964d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 965d8bc7086Sdrh ** 966d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 967d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 968d8bc7086Sdrh ** 969d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 970d8bc7086Sdrh ** of errors is returned. 971d8bc7086Sdrh */ 972d8bc7086Sdrh static int matchOrderbyToColumn( 973d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 974d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 975d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 976e4de1febSdrh int iTable, /* Insert this value in iTable */ 977d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 978d8bc7086Sdrh ){ 979d8bc7086Sdrh int nErr = 0; 980d8bc7086Sdrh int i, j; 981d8bc7086Sdrh ExprList *pEList; 982d8bc7086Sdrh 983daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 984d8bc7086Sdrh if( mustComplete ){ 985d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 986d8bc7086Sdrh } 987d8bc7086Sdrh if( fillInColumnList(pParse, pSelect) ){ 988d8bc7086Sdrh return 1; 989d8bc7086Sdrh } 990d8bc7086Sdrh if( pSelect->pPrior ){ 99192cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 99292cd52f5Sdrh return 1; 99392cd52f5Sdrh } 994d8bc7086Sdrh } 995d8bc7086Sdrh pEList = pSelect->pEList; 996d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 997d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 998e4de1febSdrh int iCol = -1; 999d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 1000e4de1febSdrh if( sqliteExprIsInteger(pE, &iCol) ){ 1001e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 1002e4de1febSdrh char zBuf[200]; 1003e4de1febSdrh sprintf(zBuf,"ORDER BY position %d should be between 1 and %d", 1004e4de1febSdrh iCol, pEList->nExpr); 1005e4de1febSdrh sqliteSetString(&pParse->zErrMsg, zBuf, 0); 1006e4de1febSdrh pParse->nErr++; 1007e4de1febSdrh nErr++; 1008e4de1febSdrh break; 1009e4de1febSdrh } 1010e4de1febSdrh iCol--; 1011e4de1febSdrh } 1012e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 10134cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1014a76b5dfcSdrh char *zName, *zLabel; 1015a76b5dfcSdrh zName = pEList->a[j].zName; 1016a76b5dfcSdrh assert( pE->token.z ); 1017a76b5dfcSdrh zLabel = sqliteStrNDup(pE->token.z, pE->token.n); 1018d8bc7086Sdrh sqliteDequote(zLabel); 1019d8bc7086Sdrh if( sqliteStrICmp(zName, zLabel)==0 ){ 1020e4de1febSdrh iCol = j; 1021d8bc7086Sdrh } 10226e142f54Sdrh sqliteFree(zLabel); 1023d8bc7086Sdrh } 1024e4de1febSdrh if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ 1025e4de1febSdrh iCol = j; 1026d8bc7086Sdrh } 1027e4de1febSdrh } 1028e4de1febSdrh if( iCol>=0 ){ 1029967e8b73Sdrh pE->op = TK_COLUMN; 1030e4de1febSdrh pE->iColumn = iCol; 1031d8bc7086Sdrh pE->iTable = iTable; 1032d8bc7086Sdrh pOrderBy->a[i].done = 1; 1033d8bc7086Sdrh } 1034e4de1febSdrh if( iCol<0 && mustComplete ){ 1035d8bc7086Sdrh char zBuf[30]; 1036d8bc7086Sdrh sprintf(zBuf,"%d",i+1); 1037d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, 1038d8bc7086Sdrh " does not match any result column", 0); 1039d8bc7086Sdrh pParse->nErr++; 1040d8bc7086Sdrh nErr++; 1041d8bc7086Sdrh break; 1042d8bc7086Sdrh } 1043d8bc7086Sdrh } 1044d8bc7086Sdrh return nErr; 1045d8bc7086Sdrh } 1046d8bc7086Sdrh 1047d8bc7086Sdrh /* 1048d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1049d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1050d8bc7086Sdrh */ 1051d8bc7086Sdrh Vdbe *sqliteGetVdbe(Parse *pParse){ 1052d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1053d8bc7086Sdrh if( v==0 ){ 10544c504391Sdrh v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); 1055d8bc7086Sdrh } 1056d8bc7086Sdrh return v; 1057d8bc7086Sdrh } 1058d8bc7086Sdrh 1059d8bc7086Sdrh 1060d8bc7086Sdrh /* 106182c3d636Sdrh ** This routine is called to process a query that is really the union 106282c3d636Sdrh ** or intersection of two or more separate queries. 1063c926afbcSdrh ** 1064c926afbcSdrh ** "p" points to the right-most of the two queries. The results should 1065c926afbcSdrh ** be stored in eDest with parameter iParm. 106682c3d636Sdrh */ 106782c3d636Sdrh static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ 106810e5e3cfSdrh int rc; /* Success code from a subroutine */ 106910e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 107010e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 107110e5e3cfSdrh int base; /* Baseline value for pParse->nTab */ 107282c3d636Sdrh 1073d8bc7086Sdrh /* Make sure there is no ORDER BY clause on prior SELECTs. Only the 1074d8bc7086Sdrh ** last SELECT in the series may have an ORDER BY. 107582c3d636Sdrh */ 1076daffd0e5Sdrh if( p==0 || p->pPrior==0 ) return 1; 1077d8bc7086Sdrh pPrior = p->pPrior; 1078d8bc7086Sdrh if( pPrior->pOrderBy ){ 1079d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", 1080d8bc7086Sdrh selectOpName(p->op), " not before", 0); 108182c3d636Sdrh pParse->nErr++; 108282c3d636Sdrh return 1; 108382c3d636Sdrh } 108482c3d636Sdrh 1085d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1086d8bc7086Sdrh */ 1087d8bc7086Sdrh v = sqliteGetVdbe(pParse); 1088d8bc7086Sdrh if( v==0 ) return 1; 1089d8bc7086Sdrh 10901cc3d75fSdrh /* Create the destination temporary table if necessary 10911cc3d75fSdrh */ 10921cc3d75fSdrh if( eDest==SRT_TempTable ){ 10931cc3d75fSdrh sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); 10941cc3d75fSdrh eDest = SRT_Table; 10951cc3d75fSdrh } 10961cc3d75fSdrh 1097f46f905aSdrh /* Generate code for the left and right SELECT statements. 1098d8bc7086Sdrh */ 109910e5e3cfSdrh base = pParse->nTab; 110082c3d636Sdrh switch( p->op ){ 1101f46f905aSdrh case TK_ALL: { 1102f46f905aSdrh if( p->pOrderBy==0 ){ 1103f46f905aSdrh rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0); 1104f46f905aSdrh if( rc ) return rc; 1105f46f905aSdrh p->pPrior = 0; 1106f46f905aSdrh rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0); 1107f46f905aSdrh p->pPrior = pPrior; 1108f46f905aSdrh if( rc ) return rc; 1109f46f905aSdrh break; 1110f46f905aSdrh } 1111f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1112f46f905aSdrh } 111382c3d636Sdrh case TK_EXCEPT: 111482c3d636Sdrh case TK_UNION: { 1115d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1116d8bc7086Sdrh int op; /* One of the SRT_ operations to apply to self */ 1117d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1118c926afbcSdrh ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ 111982c3d636Sdrh 1120d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 1121c926afbcSdrh if( eDest==priorOp && p->pOrderBy==0 ){ 1122d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1123c926afbcSdrh ** right. 1124d8bc7086Sdrh */ 112582c3d636Sdrh unionTab = iParm; 112682c3d636Sdrh }else{ 1127d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1128d8bc7086Sdrh ** intermediate results. 1129d8bc7086Sdrh */ 113082c3d636Sdrh unionTab = pParse->nTab++; 1131d8bc7086Sdrh if( p->pOrderBy 1132d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 1133d8bc7086Sdrh return 1; 1134d8bc7086Sdrh } 1135d8bc7086Sdrh if( p->op!=TK_ALL ){ 1136c6b52df3Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1); 113799fcd718Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); 1138345fda3eSdrh }else{ 113999fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); 114082c3d636Sdrh } 1141d8bc7086Sdrh } 1142d8bc7086Sdrh 1143d8bc7086Sdrh /* Code the SELECT statements to our left 1144d8bc7086Sdrh */ 1145832508b7Sdrh rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0); 114682c3d636Sdrh if( rc ) return rc; 1147d8bc7086Sdrh 1148d8bc7086Sdrh /* Code the current SELECT statement 1149d8bc7086Sdrh */ 1150d8bc7086Sdrh switch( p->op ){ 1151d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1152d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1153d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1154d8bc7086Sdrh } 115582c3d636Sdrh p->pPrior = 0; 1156c926afbcSdrh pOrderBy = p->pOrderBy; 1157c926afbcSdrh p->pOrderBy = 0; 1158832508b7Sdrh rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0); 115982c3d636Sdrh p->pPrior = pPrior; 1160c926afbcSdrh p->pOrderBy = pOrderBy; 116182c3d636Sdrh if( rc ) return rc; 1162d8bc7086Sdrh 1163d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1164d8bc7086Sdrh ** it is that we currently need. 1165d8bc7086Sdrh */ 1166c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 11676b56344dSdrh int iCont, iBreak, iStart; 116882c3d636Sdrh assert( p->pEList ); 116941202ccaSdrh if( eDest==SRT_Callback ){ 1170832508b7Sdrh generateColumnNames(pParse, p->base, 0, p->pEList); 117141202ccaSdrh } 117282c3d636Sdrh iBreak = sqliteVdbeMakeLabel(v); 11736b56344dSdrh iCont = sqliteVdbeMakeLabel(v); 11746b56344dSdrh sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak); 11756b56344dSdrh iStart = sqliteVdbeCurrentAddr(v); 117638640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 1177d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 117882c3d636Sdrh iCont, iBreak); 117982c3d636Sdrh if( rc ) return 1; 11806b56344dSdrh sqliteVdbeResolveLabel(v, iCont); 11816b56344dSdrh sqliteVdbeAddOp(v, OP_Next, unionTab, iStart); 118299fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 118399fcd718Sdrh sqliteVdbeAddOp(v, OP_Close, unionTab, 0); 1184d8bc7086Sdrh if( p->pOrderBy ){ 1185c926afbcSdrh generateSortTail(p, v, p->pEList->nExpr, eDest, iParm); 1186d8bc7086Sdrh } 118782c3d636Sdrh } 118882c3d636Sdrh break; 118982c3d636Sdrh } 119082c3d636Sdrh case TK_INTERSECT: { 119182c3d636Sdrh int tab1, tab2; 11926b56344dSdrh int iCont, iBreak, iStart; 119382c3d636Sdrh 1194d8bc7086Sdrh /* INTERSECT is different from the others since it requires 11956206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1196d8bc7086Sdrh ** by allocating the tables we will need. 1197d8bc7086Sdrh */ 119882c3d636Sdrh tab1 = pParse->nTab++; 119982c3d636Sdrh tab2 = pParse->nTab++; 1200d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 1201d8bc7086Sdrh return 1; 1202d8bc7086Sdrh } 1203c6b52df3Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1); 120499fcd718Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); 1205d8bc7086Sdrh 1206d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1207d8bc7086Sdrh */ 1208832508b7Sdrh rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0); 120982c3d636Sdrh if( rc ) return rc; 1210d8bc7086Sdrh 1211d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1212d8bc7086Sdrh */ 1213c6b52df3Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1); 121499fcd718Sdrh sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); 121582c3d636Sdrh p->pPrior = 0; 1216832508b7Sdrh rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0); 121782c3d636Sdrh p->pPrior = pPrior; 121882c3d636Sdrh if( rc ) return rc; 1219d8bc7086Sdrh 1220d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1221d8bc7086Sdrh ** tables. 1222d8bc7086Sdrh */ 122382c3d636Sdrh assert( p->pEList ); 122441202ccaSdrh if( eDest==SRT_Callback ){ 1225832508b7Sdrh generateColumnNames(pParse, p->base, 0, p->pEList); 122641202ccaSdrh } 122782c3d636Sdrh iBreak = sqliteVdbeMakeLabel(v); 12286b56344dSdrh iCont = sqliteVdbeMakeLabel(v); 12296b56344dSdrh sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak); 12306b56344dSdrh iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0); 123199fcd718Sdrh sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont); 123238640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 1233d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 123482c3d636Sdrh iCont, iBreak); 123582c3d636Sdrh if( rc ) return 1; 12366b56344dSdrh sqliteVdbeResolveLabel(v, iCont); 12376b56344dSdrh sqliteVdbeAddOp(v, OP_Next, tab1, iStart); 123899fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 123999fcd718Sdrh sqliteVdbeAddOp(v, OP_Close, tab2, 0); 124099fcd718Sdrh sqliteVdbeAddOp(v, OP_Close, tab1, 0); 1241d8bc7086Sdrh if( p->pOrderBy ){ 1242c926afbcSdrh generateSortTail(p, v, p->pEList->nExpr, eDest, iParm); 1243d8bc7086Sdrh } 124482c3d636Sdrh break; 124582c3d636Sdrh } 124682c3d636Sdrh } 124782c3d636Sdrh assert( p->pEList && pPrior->pEList ); 124882c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 1249d8bc7086Sdrh sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", 1250d8bc7086Sdrh selectOpName(p->op), " do not have the same number of result columns", 0); 125182c3d636Sdrh pParse->nErr++; 125282c3d636Sdrh return 1; 12532282792aSdrh } 125410e5e3cfSdrh pParse->nTab = base; 12552282792aSdrh return 0; 12562282792aSdrh } 12572282792aSdrh 12582282792aSdrh /* 1259832508b7Sdrh ** Recursively scan through an expression tree. For every reference 1260832508b7Sdrh ** to a column in table number iFrom, change that reference to the 1261832508b7Sdrh ** same column in table number iTo. 1262832508b7Sdrh */ 1263832508b7Sdrh static void changeTables(Expr *pExpr, int iFrom, int iTo){ 1264832508b7Sdrh if( pExpr==0 ) return; 1265832508b7Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){ 1266832508b7Sdrh pExpr->iTable = iTo; 1267832508b7Sdrh }else{ 12681b2e0329Sdrh static void changeTablesInList(ExprList*, int, int); 1269832508b7Sdrh changeTables(pExpr->pLeft, iFrom, iTo); 1270832508b7Sdrh changeTables(pExpr->pRight, iFrom, iTo); 12711b2e0329Sdrh changeTablesInList(pExpr->pList, iFrom, iTo); 1272832508b7Sdrh } 1273832508b7Sdrh } 12741b2e0329Sdrh static void changeTablesInList(ExprList *pList, int iFrom, int iTo){ 12751b2e0329Sdrh if( pList ){ 12761b2e0329Sdrh int i; 12771b2e0329Sdrh for(i=0; i<pList->nExpr; i++){ 12781b2e0329Sdrh changeTables(pList->a[i].pExpr, iFrom, iTo); 12791b2e0329Sdrh } 1280832508b7Sdrh } 1281832508b7Sdrh } 1282832508b7Sdrh 1283832508b7Sdrh /* 1284832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 1285832508b7Sdrh ** a column in table number iTable with a copy of the corresponding 128684e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 128784e59207Sdrh ** unchanged.) When making a copy of an expression in pEList, change 128884e59207Sdrh ** references to columns in table iSub into references to table iTable. 1289832508b7Sdrh ** 1290832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1291832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1292832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1293832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1294832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1295832508b7Sdrh ** of the subquery rather the result set of the subquery. 1296832508b7Sdrh */ 1297832508b7Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){ 1298832508b7Sdrh if( pExpr==0 ) return; 129984e59207Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){ 1300832508b7Sdrh Expr *pNew; 130184e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 1302832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 1303832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 1304832508b7Sdrh assert( pNew!=0 ); 1305832508b7Sdrh pExpr->op = pNew->op; 1306832508b7Sdrh pExpr->pLeft = sqliteExprDup(pNew->pLeft); 1307832508b7Sdrh pExpr->pRight = sqliteExprDup(pNew->pRight); 1308832508b7Sdrh pExpr->pList = sqliteExprListDup(pNew->pList); 1309832508b7Sdrh pExpr->iTable = pNew->iTable; 1310832508b7Sdrh pExpr->iColumn = pNew->iColumn; 1311832508b7Sdrh pExpr->iAgg = pNew->iAgg; 13121b2e0329Sdrh pExpr->token = pNew->token; 1313832508b7Sdrh if( iSub!=iTable ){ 1314832508b7Sdrh changeTables(pExpr, iSub, iTable); 1315832508b7Sdrh } 1316832508b7Sdrh }else{ 1317832508b7Sdrh static void substExprList(ExprList*,int,ExprList*,int); 1318832508b7Sdrh substExpr(pExpr->pLeft, iTable, pEList, iSub); 1319832508b7Sdrh substExpr(pExpr->pRight, iTable, pEList, iSub); 1320832508b7Sdrh substExprList(pExpr->pList, iTable, pEList, iSub); 1321832508b7Sdrh } 1322832508b7Sdrh } 1323832508b7Sdrh static void 1324832508b7Sdrh substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){ 1325832508b7Sdrh int i; 1326832508b7Sdrh if( pList==0 ) return; 1327832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 1328832508b7Sdrh substExpr(pList->a[i].pExpr, iTable, pEList, iSub); 1329832508b7Sdrh } 1330832508b7Sdrh } 1331832508b7Sdrh 1332832508b7Sdrh /* 13331350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 13341350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 13351350b030Sdrh ** occurs. 13361350b030Sdrh ** 13371350b030Sdrh ** To understand the concept of flattening, consider the following 13381350b030Sdrh ** query: 13391350b030Sdrh ** 13401350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 13411350b030Sdrh ** 13421350b030Sdrh ** The default way of implementing this query is to execute the 13431350b030Sdrh ** subquery first and store the results in a temporary table, then 13441350b030Sdrh ** run the outer query on that temporary table. This requires two 13451350b030Sdrh ** passes over the data. Furthermore, because the temporary table 13461350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 1347832508b7Sdrh ** optimized. 13481350b030Sdrh ** 1349832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 13501350b030Sdrh ** a single flat select, like this: 13511350b030Sdrh ** 13521350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 13531350b030Sdrh ** 13541350b030Sdrh ** The code generated for this simpification gives the same result 1355832508b7Sdrh ** but only has to scan the data once. And because indices might 1356832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 1357832508b7Sdrh ** avoided. 13581350b030Sdrh ** 1359832508b7Sdrh ** Flattening is only attempted if all of the following are true: 13601350b030Sdrh ** 1361832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 13621350b030Sdrh ** 1363832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 1364832508b7Sdrh ** 1365832508b7Sdrh ** (3) The subquery is not a join. 1366832508b7Sdrh ** 1367832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 1368832508b7Sdrh ** 1369832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 1370832508b7Sdrh ** aggregates. 1371832508b7Sdrh ** 1372832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 1373832508b7Sdrh ** DISTINCT. 1374832508b7Sdrh ** 137508192d5fSdrh ** (7) The subquery has a FROM clause. 137608192d5fSdrh ** 1377df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 1378df199a25Sdrh ** 1379df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 1380df199a25Sdrh ** aggregates. 1381df199a25Sdrh ** 1382df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 1383df199a25Sdrh ** use LIMIT. 1384df199a25Sdrh ** 1385832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 1386832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 1387832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 1388832508b7Sdrh ** 1389832508b7Sdrh ** If flattening is not attempted, this routine is a no-op and return 0. 1390832508b7Sdrh ** If flattening is attempted this routine returns 1. 1391832508b7Sdrh ** 1392832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 1393832508b7Sdrh ** the subquery before this routine runs. 13941350b030Sdrh */ 1395832508b7Sdrh int flattenSubquery(Select *p, int iFrom, int isAgg, int subqueryIsAgg){ 13960bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 1397ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 1398ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 13990bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 1400832508b7Sdrh int i; 1401832508b7Sdrh int iParent, iSub; 1402832508b7Sdrh Expr *pWhere; 14031350b030Sdrh 1404832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 1405832508b7Sdrh */ 1406832508b7Sdrh if( p==0 ) return 0; 1407832508b7Sdrh pSrc = p->pSrc; 1408ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 1409832508b7Sdrh pSub = pSrc->a[iFrom].pSelect; 1410832508b7Sdrh assert( pSub!=0 ); 1411832508b7Sdrh if( isAgg && subqueryIsAgg ) return 0; 1412ad3cab52Sdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; 1413832508b7Sdrh pSubSrc = pSub->pSrc; 1414832508b7Sdrh assert( pSubSrc ); 1415ad3cab52Sdrh if( pSubSrc->nSrc!=1 ) return 0; 1416df199a25Sdrh if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){ 1417df199a25Sdrh return 0; 1418df199a25Sdrh } 1419d11d382cSdrh if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0; 1420832508b7Sdrh 14210bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 1422832508b7Sdrh ** i-th entry of the FROM clause in the outer query. 1423832508b7Sdrh */ 1424832508b7Sdrh iParent = p->base + iFrom; 1425832508b7Sdrh iSub = pSub->base; 1426832508b7Sdrh substExprList(p->pEList, iParent, pSub->pEList, iSub); 1427832508b7Sdrh pList = p->pEList; 1428832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 1429832508b7Sdrh if( pList->a[i].zName==0 ){ 1430832508b7Sdrh Expr *pExpr = pList->a[i].pExpr; 1431832508b7Sdrh pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); 1432832508b7Sdrh } 1433832508b7Sdrh } 14341b2e0329Sdrh if( isAgg ){ 1435832508b7Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList, iSub); 1436832508b7Sdrh substExpr(p->pHaving, iParent, pSub->pEList, iSub); 14371b2e0329Sdrh } 1438832508b7Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList, iSub); 1439832508b7Sdrh if( pSub->pWhere ){ 1440832508b7Sdrh pWhere = sqliteExprDup(pSub->pWhere); 1441832508b7Sdrh if( iParent!=iSub ){ 1442832508b7Sdrh changeTables(pWhere, iSub, iParent); 1443832508b7Sdrh } 1444832508b7Sdrh }else{ 1445832508b7Sdrh pWhere = 0; 1446832508b7Sdrh } 1447832508b7Sdrh if( subqueryIsAgg ){ 1448832508b7Sdrh assert( p->pHaving==0 ); 14491b2e0329Sdrh p->pHaving = p->pWhere; 14501b2e0329Sdrh p->pWhere = pWhere; 1451832508b7Sdrh substExpr(p->pHaving, iParent, pSub->pEList, iSub); 14521b2e0329Sdrh if( pSub->pHaving ){ 14531b2e0329Sdrh Expr *pHaving = sqliteExprDup(pSub->pHaving); 14541b2e0329Sdrh if( iParent!=iSub ){ 14551b2e0329Sdrh changeTables(pHaving, iSub, iParent); 14561b2e0329Sdrh } 14571b2e0329Sdrh if( p->pHaving ){ 14581b2e0329Sdrh p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0); 14591b2e0329Sdrh }else{ 14601b2e0329Sdrh p->pHaving = pHaving; 14611b2e0329Sdrh } 14621b2e0329Sdrh } 14631b2e0329Sdrh assert( p->pGroupBy==0 ); 14641b2e0329Sdrh p->pGroupBy = sqliteExprListDup(pSub->pGroupBy); 14651b2e0329Sdrh if( iParent!=iSub ){ 14661b2e0329Sdrh changeTablesInList(p->pGroupBy, iSub, iParent); 14671b2e0329Sdrh } 1468832508b7Sdrh }else if( p->pWhere==0 ){ 1469832508b7Sdrh p->pWhere = pWhere; 1470832508b7Sdrh }else{ 1471832508b7Sdrh substExpr(p->pWhere, iParent, pSub->pEList, iSub); 1472832508b7Sdrh if( pWhere ){ 1473832508b7Sdrh p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0); 1474832508b7Sdrh } 1475832508b7Sdrh } 1476832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 1477df199a25Sdrh if( pSub->nLimit>=0 ){ 1478df199a25Sdrh if( p->nLimit<0 ){ 1479df199a25Sdrh p->nLimit = pSub->nLimit; 1480df199a25Sdrh }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){ 1481df199a25Sdrh p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset; 1482df199a25Sdrh } 1483df199a25Sdrh } 1484df199a25Sdrh p->nOffset += pSub->nOffset; 1485832508b7Sdrh if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ 1486832508b7Sdrh sqliteDeleteTable(0, pSrc->a[iFrom].pTab); 1487832508b7Sdrh } 1488832508b7Sdrh pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab; 1489832508b7Sdrh pSubSrc->a[0].pTab = 0; 1490832508b7Sdrh pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect; 1491832508b7Sdrh pSubSrc->a[0].pSelect = 0; 1492832508b7Sdrh sqliteSelectDelete(pSub); 1493832508b7Sdrh return 1; 14941350b030Sdrh } 14951350b030Sdrh 14961350b030Sdrh /* 14979562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 14989562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 14999562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 15009562b551Sdrh ** then generate the code for this SELECT return 1. If this is not a 15019562b551Sdrh ** simple min() or max() query, then return 0; 15029562b551Sdrh ** 15039562b551Sdrh ** A simply min() or max() query looks like this: 15049562b551Sdrh ** 15059562b551Sdrh ** SELECT min(a) FROM table; 15069562b551Sdrh ** SELECT max(a) FROM table; 15079562b551Sdrh ** 15089562b551Sdrh ** The query may have only a single table in its FROM argument. There 15099562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 15109562b551Sdrh ** be the min() or max() of a single column of the table. The column 15119562b551Sdrh ** in the min() or max() function must be indexed. 15129562b551Sdrh ** 15139562b551Sdrh ** The parameters to this routine are the same as for sqliteSelect(). 15149562b551Sdrh ** See the header comment on that routine for additional information. 15159562b551Sdrh */ 15169562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 15179562b551Sdrh Expr *pExpr; 15189562b551Sdrh int iCol; 15199562b551Sdrh Table *pTab; 15209562b551Sdrh Index *pIdx; 15219562b551Sdrh int base; 15229562b551Sdrh Vdbe *v; 15239562b551Sdrh int openOp; 15249562b551Sdrh int seekOp; 15259562b551Sdrh int cont; 15269562b551Sdrh ExprList eList; 15279562b551Sdrh struct ExprList_item eListItem; 15289562b551Sdrh 15299562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 15309562b551Sdrh ** zero if it is not. 15319562b551Sdrh */ 15329562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 1533ad3cab52Sdrh if( p->pSrc->nSrc!=1 ) return 0; 15349562b551Sdrh if( p->pEList->nExpr!=1 ) return 0; 15359562b551Sdrh pExpr = p->pEList->a[0].pExpr; 15369562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 15379562b551Sdrh if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0; 15380bce8354Sdrh if( pExpr->token.n!=3 ) return 0; 15390bce8354Sdrh if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){ 15400bce8354Sdrh seekOp = OP_Rewind; 15410bce8354Sdrh }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){ 15420bce8354Sdrh seekOp = OP_Last; 15430bce8354Sdrh }else{ 15440bce8354Sdrh return 0; 15450bce8354Sdrh } 15469562b551Sdrh pExpr = pExpr->pList->a[0].pExpr; 15479562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 15489562b551Sdrh iCol = pExpr->iColumn; 15499562b551Sdrh pTab = p->pSrc->a[0].pTab; 15509562b551Sdrh 15519562b551Sdrh /* If we get to here, it means the query is of the correct form. 155217f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 155317f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 155417f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 155517f71934Sdrh ** usable index is found, return 0. 15569562b551Sdrh */ 15579562b551Sdrh if( iCol<0 ){ 15589562b551Sdrh pIdx = 0; 15599562b551Sdrh }else{ 15609562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 15619562b551Sdrh assert( pIdx->nColumn>=1 ); 15629562b551Sdrh if( pIdx->aiColumn[0]==iCol ) break; 15639562b551Sdrh } 15649562b551Sdrh if( pIdx==0 ) return 0; 15659562b551Sdrh } 15669562b551Sdrh 156717f71934Sdrh /* Identify column names if we will be using the callback. This 15689562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 15699562b551Sdrh */ 15709562b551Sdrh v = sqliteGetVdbe(pParse); 15719562b551Sdrh if( v==0 ) return 0; 15729562b551Sdrh if( eDest==SRT_Callback ){ 1573832508b7Sdrh generateColumnNames(pParse, p->base, p->pSrc, p->pEList); 15749562b551Sdrh } 15759562b551Sdrh 157617f71934Sdrh /* Generating code to find the min or the max. Basically all we have 157717f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 157817f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 157917f71934Sdrh ** or last entry in the main table. 15809562b551Sdrh */ 15815cf8e8c7Sdrh if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){ 15825cf8e8c7Sdrh sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); 15835cf8e8c7Sdrh pParse->schemaVerified = 1; 15845cf8e8c7Sdrh } 15859562b551Sdrh openOp = pTab->isTemp ? OP_OpenAux : OP_Open; 1586832508b7Sdrh base = p->base; 15879562b551Sdrh sqliteVdbeAddOp(v, openOp, base, pTab->tnum); 15885cf8e8c7Sdrh sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 15899562b551Sdrh if( pIdx==0 ){ 15909562b551Sdrh sqliteVdbeAddOp(v, seekOp, base, 0); 15919562b551Sdrh }else{ 15929562b551Sdrh sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum); 15935cf8e8c7Sdrh sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 15949562b551Sdrh sqliteVdbeAddOp(v, seekOp, base+1, 0); 15959562b551Sdrh sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0); 15969562b551Sdrh sqliteVdbeAddOp(v, OP_Close, base+1, 0); 15979562b551Sdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 15989562b551Sdrh } 15995cf8e8c7Sdrh eList.nExpr = 1; 16005cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 16015cf8e8c7Sdrh eList.a = &eListItem; 16025cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 16039562b551Sdrh cont = sqliteVdbeMakeLabel(v); 160438640e15Sdrh selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont); 16059562b551Sdrh sqliteVdbeResolveLabel(v, cont); 16069562b551Sdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 16079562b551Sdrh return 1; 16089562b551Sdrh } 16099562b551Sdrh 16109562b551Sdrh /* 16119bb61fe7Sdrh ** Generate code for the given SELECT statement. 16129bb61fe7Sdrh ** 1613fef5208cSdrh ** The results are distributed in various ways depending on the 1614fef5208cSdrh ** value of eDest and iParm. 1615fef5208cSdrh ** 1616fef5208cSdrh ** eDest Value Result 1617fef5208cSdrh ** ------------ ------------------------------------------- 1618fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 1619fef5208cSdrh ** 1620fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 1621fef5208cSdrh ** 1622fef5208cSdrh ** SRT_Set Store results as keys of a table with cursor iParm 1623fef5208cSdrh ** 162482c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 162582c3d636Sdrh ** 1626c4a3c779Sdrh ** SRT_Except Remove results form the temporary table iParm. 1627c4a3c779Sdrh ** 1628c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 16299bb61fe7Sdrh ** 16309bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 16319bb61fe7Sdrh ** encountered, then an appropriate error message is left in 16329bb61fe7Sdrh ** pParse->zErrMsg. 16339bb61fe7Sdrh ** 16349bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 16359bb61fe7Sdrh ** calling function needs to do that. 16361b2e0329Sdrh ** 16371b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 16381b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 16391b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 16401b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 16411b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 16421b2e0329Sdrh ** can be changed. 16439bb61fe7Sdrh */ 16449bb61fe7Sdrh int sqliteSelect( 1645cce7d176Sdrh Parse *pParse, /* The parser context */ 16469bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 164782c3d636Sdrh int eDest, /* One of: SRT_Callback Mem Set Union Except */ 1648832508b7Sdrh int iParm, /* Save result in this memory location, if >=0 */ 1649832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 1650832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 16511b2e0329Sdrh int *pParentAgg /* True if pParent uses aggregate functions */ 1652cce7d176Sdrh ){ 1653d8bc7086Sdrh int i; 1654cce7d176Sdrh WhereInfo *pWInfo; 1655cce7d176Sdrh Vdbe *v; 1656cce7d176Sdrh int isAgg = 0; /* True for select lists like "count(*)" */ 1657a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 1658ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 16599bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 16609bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 16612282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 16622282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 166319a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 166419a775c2Sdrh int distinct; /* Table to use for the distinct set */ 166510e5e3cfSdrh int base; /* First cursor available for use */ 16661d83f052Sdrh int rc = 1; /* Value to return from this function */ 16679bb61fe7Sdrh 1668daffd0e5Sdrh if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; 1669daffd0e5Sdrh 167082c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 167182c3d636Sdrh */ 167282c3d636Sdrh if( p->pPrior ){ 167382c3d636Sdrh return multiSelect(pParse, p, eDest, iParm); 167482c3d636Sdrh } 167582c3d636Sdrh 167682c3d636Sdrh /* Make local copies of the parameters for this query. 167782c3d636Sdrh */ 16789bb61fe7Sdrh pTabList = p->pSrc; 16799bb61fe7Sdrh pWhere = p->pWhere; 16809bb61fe7Sdrh pOrderBy = p->pOrderBy; 16812282792aSdrh pGroupBy = p->pGroupBy; 16822282792aSdrh pHaving = p->pHaving; 168319a775c2Sdrh isDistinct = p->isDistinct; 16849bb61fe7Sdrh 1685832508b7Sdrh /* Allocate a block of VDBE cursors, one for each table in the FROM clause. 1686832508b7Sdrh ** The WHERE processing requires that the cursors for the tables in the 1687832508b7Sdrh ** FROM clause be consecutive. 168810e5e3cfSdrh */ 1689832508b7Sdrh base = p->base = pParse->nTab; 1690ad3cab52Sdrh pParse->nTab += pTabList->nSrc; 169110e5e3cfSdrh 16929bb61fe7Sdrh /* 16939bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 16949bb61fe7Sdrh ** errors before this routine starts. 16959bb61fe7Sdrh */ 16961d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 1697cce7d176Sdrh 1698d8bc7086Sdrh /* Look up every table in the table list and create an appropriate 1699d8bc7086Sdrh ** columnlist in pEList if there isn't one already. (The parser leaves 1700967e8b73Sdrh ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") 1701cce7d176Sdrh */ 1702d8bc7086Sdrh if( fillInColumnList(pParse, p) ){ 17031d83f052Sdrh goto select_end; 1704cce7d176Sdrh } 1705ad2d8307Sdrh pWhere = p->pWhere; 1706d8bc7086Sdrh pEList = p->pEList; 17071d83f052Sdrh if( pEList==0 ) goto select_end; 1708cce7d176Sdrh 17092282792aSdrh /* If writing to memory or generating a set 17102282792aSdrh ** only a single column may be output. 171119a775c2Sdrh */ 1712fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 171319a775c2Sdrh sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " 171419a775c2Sdrh "a SELECT that is part of an expression", 0); 171519a775c2Sdrh pParse->nErr++; 17161d83f052Sdrh goto select_end; 171719a775c2Sdrh } 171819a775c2Sdrh 1719c926afbcSdrh /* ORDER BY is ignored for some destinations. 17202282792aSdrh */ 1721c926afbcSdrh switch( eDest ){ 1722c926afbcSdrh case SRT_Union: 1723c926afbcSdrh case SRT_Except: 1724c926afbcSdrh case SRT_Discard: 1725acd4c695Sdrh pOrderBy = 0; 1726c926afbcSdrh break; 1727c926afbcSdrh default: 1728c926afbcSdrh break; 17292282792aSdrh } 17302282792aSdrh 173110e5e3cfSdrh /* At this point, we should have allocated all the cursors that we 1732832508b7Sdrh ** need to handle subquerys and temporary tables. 173310e5e3cfSdrh ** 1734967e8b73Sdrh ** Resolve the column names and do a semantics check on all the expressions. 17352282792aSdrh */ 17364794b980Sdrh for(i=0; i<pEList->nExpr; i++){ 1737832508b7Sdrh if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){ 17381d83f052Sdrh goto select_end; 1739cce7d176Sdrh } 17402282792aSdrh if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ 17411d83f052Sdrh goto select_end; 1742cce7d176Sdrh } 1743cce7d176Sdrh } 1744cce7d176Sdrh if( pWhere ){ 1745832508b7Sdrh if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){ 17461d83f052Sdrh goto select_end; 1747cce7d176Sdrh } 1748cce7d176Sdrh if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ 17491d83f052Sdrh goto select_end; 1750cce7d176Sdrh } 1751cce7d176Sdrh } 1752cce7d176Sdrh if( pOrderBy ){ 1753cce7d176Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 17542282792aSdrh Expr *pE = pOrderBy->a[i].pExpr; 17559208643dSdrh if( sqliteExprIsConstant(pE) ){ 1756e4de1febSdrh int iCol; 1757e4de1febSdrh if( sqliteExprIsInteger(pE, &iCol)==0 ){ 17589208643dSdrh sqliteSetString(&pParse->zErrMsg, 1759e4de1febSdrh "ORDER BY terms must not be non-integer constants", 0); 17609208643dSdrh pParse->nErr++; 17611d83f052Sdrh goto select_end; 1762e4de1febSdrh }else if( iCol<=0 || iCol>pEList->nExpr ){ 1763e4de1febSdrh char zBuf[2000]; 1764e4de1febSdrh sprintf(zBuf,"ORDER BY column number %d out of range - should be " 1765e4de1febSdrh "between 1 and %d", iCol, pEList->nExpr); 1766e4de1febSdrh sqliteSetString(&pParse->zErrMsg, zBuf, 0); 1767e4de1febSdrh pParse->nErr++; 1768e4de1febSdrh goto select_end; 1769e4de1febSdrh } 1770e4de1febSdrh sqliteExprDelete(pE); 1771e4de1febSdrh pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr); 17729208643dSdrh } 1773832508b7Sdrh if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){ 17741d83f052Sdrh goto select_end; 1775cce7d176Sdrh } 17762282792aSdrh if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ 17771d83f052Sdrh goto select_end; 1778cce7d176Sdrh } 1779cce7d176Sdrh } 1780cce7d176Sdrh } 17812282792aSdrh if( pGroupBy ){ 17822282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 17832282792aSdrh Expr *pE = pGroupBy->a[i].pExpr; 17849208643dSdrh if( sqliteExprIsConstant(pE) ){ 17859208643dSdrh sqliteSetString(&pParse->zErrMsg, 17869208643dSdrh "GROUP BY expressions should not be constant", 0); 17879208643dSdrh pParse->nErr++; 17881d83f052Sdrh goto select_end; 17899208643dSdrh } 1790832508b7Sdrh if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){ 17911d83f052Sdrh goto select_end; 17922282792aSdrh } 17932282792aSdrh if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ 17941d83f052Sdrh goto select_end; 17952282792aSdrh } 17962282792aSdrh } 17972282792aSdrh } 17982282792aSdrh if( pHaving ){ 17992282792aSdrh if( pGroupBy==0 ){ 1800da93281eSdrh sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " 1801da93281eSdrh "before HAVING", 0); 18022282792aSdrh pParse->nErr++; 18031d83f052Sdrh goto select_end; 18042282792aSdrh } 1805832508b7Sdrh if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){ 18061d83f052Sdrh goto select_end; 18072282792aSdrh } 1808da93281eSdrh if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ 18091d83f052Sdrh goto select_end; 18102282792aSdrh } 1811cce7d176Sdrh } 1812cce7d176Sdrh 18139562b551Sdrh /* Check for the special case of a min() or max() function by itself 18149562b551Sdrh ** in the result set. 18159562b551Sdrh */ 18169562b551Sdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 18175cf8e8c7Sdrh rc = 0; 18189562b551Sdrh goto select_end; 18199562b551Sdrh } 18209562b551Sdrh 1821d820cb1bSdrh /* Begin generating code. 1822d820cb1bSdrh */ 1823d820cb1bSdrh v = sqliteGetVdbe(pParse); 1824d820cb1bSdrh if( v==0 ) goto select_end; 1825d820cb1bSdrh 18260bb28106Sdrh /* Identify column names if we will be using in the callback. This 18270bb28106Sdrh ** step is skipped if the output is going to a table or a memory cell. 18280bb28106Sdrh */ 18290bb28106Sdrh if( eDest==SRT_Callback ){ 18300bb28106Sdrh generateColumnNames(pParse, p->base, pTabList, pEList); 18310bb28106Sdrh } 18320bb28106Sdrh 18330bb28106Sdrh /* Set the limiter 18340bb28106Sdrh */ 18350bb28106Sdrh if( p->nLimit<=0 ){ 1836d11d382cSdrh p->nLimit = -1; 18370bb28106Sdrh p->nOffset = 0; 18380bb28106Sdrh }else{ 1839d11d382cSdrh int iMem = pParse->nMem++; 1840d11d382cSdrh sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0); 1841bf5cd97eSdrh sqliteVdbeAddOp(v, OP_MemStore, iMem, 1); 1842d11d382cSdrh p->nLimit = iMem; 1843d11d382cSdrh if( p->nOffset<=0 ){ 1844d11d382cSdrh p->nOffset = 0; 1845d11d382cSdrh }else{ 1846d11d382cSdrh iMem = pParse->nMem++; 1847d11d382cSdrh sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0); 1848bf5cd97eSdrh sqliteVdbeAddOp(v, OP_MemStore, iMem, 1); 1849d11d382cSdrh p->nOffset = iMem; 1850d11d382cSdrh } 18510bb28106Sdrh } 18520bb28106Sdrh 1853d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 1854d820cb1bSdrh */ 1855ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 1856a76b5dfcSdrh if( pTabList->a[i].pSelect==0 ) continue; 18572d0794e3Sdrh sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i, 18581b2e0329Sdrh p, i, &isAgg); 1859832508b7Sdrh pTabList = p->pSrc; 1860832508b7Sdrh pWhere = p->pWhere; 1861acd4c695Sdrh if( eDest==SRT_Callback ){ 1862832508b7Sdrh pOrderBy = p->pOrderBy; 1863acd4c695Sdrh } 1864832508b7Sdrh pGroupBy = p->pGroupBy; 1865832508b7Sdrh pHaving = p->pHaving; 1866832508b7Sdrh isDistinct = p->isDistinct; 18671b2e0329Sdrh } 18681b2e0329Sdrh 18691b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 18701b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 18711b2e0329Sdrh */ 18721b2e0329Sdrh if( pParent && pParentAgg && 18731b2e0329Sdrh flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){ 18741b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 18751b2e0329Sdrh return rc; 18761b2e0329Sdrh } 1877832508b7Sdrh 18782d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 18792d0794e3Sdrh */ 18802d0794e3Sdrh if( eDest==SRT_TempTable ){ 18812d0794e3Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); 18822d0794e3Sdrh } 18832d0794e3Sdrh 18842282792aSdrh /* Do an analysis of aggregate expressions. 1885efb7251dSdrh */ 1886d820cb1bSdrh sqliteAggregateInfoReset(pParse); 18872282792aSdrh if( isAgg ){ 18880bce8354Sdrh assert( pParse->nAgg==0 ); 18892282792aSdrh for(i=0; i<pEList->nExpr; i++){ 18902282792aSdrh if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ 18911d83f052Sdrh goto select_end; 18922282792aSdrh } 18932282792aSdrh } 18942282792aSdrh if( pGroupBy ){ 18952282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 18962282792aSdrh if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ 18971d83f052Sdrh goto select_end; 18982282792aSdrh } 18992282792aSdrh } 19002282792aSdrh } 19012282792aSdrh if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ 19021d83f052Sdrh goto select_end; 19032282792aSdrh } 1904191b690eSdrh if( pOrderBy ){ 1905191b690eSdrh for(i=0; i<pOrderBy->nExpr; i++){ 1906191b690eSdrh if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ 19071d83f052Sdrh goto select_end; 1908191b690eSdrh } 1909191b690eSdrh } 1910191b690eSdrh } 1911efb7251dSdrh } 1912efb7251dSdrh 19132282792aSdrh /* Reset the aggregator 1914cce7d176Sdrh */ 1915cce7d176Sdrh if( isAgg ){ 191699fcd718Sdrh sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); 1917e5095355Sdrh for(i=0; i<pParse->nAgg; i++){ 19180bce8354Sdrh FuncDef *pFunc; 19190bce8354Sdrh if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ 19201350b030Sdrh sqliteVdbeAddOp(v, OP_AggInit, 0, i); 19210bce8354Sdrh sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER); 1922e5095355Sdrh } 1923e5095355Sdrh } 19241bee3d7bSdrh if( pGroupBy==0 ){ 19251bee3d7bSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 19261bee3d7bSdrh sqliteVdbeAddOp(v, OP_AggFocus, 0, 0); 19271bee3d7bSdrh } 1928cce7d176Sdrh } 1929cce7d176Sdrh 193019a775c2Sdrh /* Initialize the memory cell to NULL 193119a775c2Sdrh */ 1932fef5208cSdrh if( eDest==SRT_Mem ){ 193399fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 19348721ce4aSdrh sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); 193519a775c2Sdrh } 193619a775c2Sdrh 1937832508b7Sdrh /* Open a temporary table to use for the distinct set. 1938cce7d176Sdrh */ 193919a775c2Sdrh if( isDistinct ){ 1940832508b7Sdrh distinct = pParse->nTab++; 1941c6b52df3Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1); 1942832508b7Sdrh }else{ 1943832508b7Sdrh distinct = -1; 1944efb7251dSdrh } 1945832508b7Sdrh 1946832508b7Sdrh /* Begin the database scan 1947832508b7Sdrh */ 1948*68d2e591Sdrh pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0, 1949*68d2e591Sdrh pGroupBy ? 0 : &pOrderBy); 19501d83f052Sdrh if( pWInfo==0 ) goto select_end; 1951cce7d176Sdrh 19522282792aSdrh /* Use the standard inner loop if we are not dealing with 19532282792aSdrh ** aggregates 1954cce7d176Sdrh */ 1955da9d6c45Sdrh if( !isAgg ){ 1956df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 1957df199a25Sdrh iParm, pWInfo->iContinue, pWInfo->iBreak) ){ 19581d83f052Sdrh goto select_end; 1959cce7d176Sdrh } 1960da9d6c45Sdrh } 1961cce7d176Sdrh 1962e3184744Sdrh /* If we are dealing with aggregates, then do the special aggregate 19632282792aSdrh ** processing. 1964efb7251dSdrh */ 19652282792aSdrh else{ 19662282792aSdrh if( pGroupBy ){ 19671bee3d7bSdrh int lbl1; 19682282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 19692282792aSdrh sqliteExprCode(pParse, pGroupBy->a[i].pExpr); 1970efb7251dSdrh } 197199fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); 1972491791a8Sdrh if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy); 19731bee3d7bSdrh lbl1 = sqliteVdbeMakeLabel(v); 197499fcd718Sdrh sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1); 19752282792aSdrh for(i=0; i<pParse->nAgg; i++){ 19762282792aSdrh if( pParse->aAgg[i].isAgg ) continue; 19772282792aSdrh sqliteExprCode(pParse, pParse->aAgg[i].pExpr); 197899fcd718Sdrh sqliteVdbeAddOp(v, OP_AggSet, 0, i); 19792282792aSdrh } 19802282792aSdrh sqliteVdbeResolveLabel(v, lbl1); 19812282792aSdrh } 19822282792aSdrh for(i=0; i<pParse->nAgg; i++){ 19832282792aSdrh Expr *pE; 19840bce8354Sdrh int j; 19852282792aSdrh if( !pParse->aAgg[i].isAgg ) continue; 19862282792aSdrh pE = pParse->aAgg[i].pExpr; 19872282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 19880bce8354Sdrh if( pE->pList ){ 1989e5095355Sdrh for(j=0; j<pE->pList->nExpr; j++){ 1990e5095355Sdrh sqliteExprCode(pParse, pE->pList->a[j].pExpr); 1991e5095355Sdrh } 19922282792aSdrh } 19931350b030Sdrh sqliteVdbeAddOp(v, OP_Integer, i, 0); 1994f55f25f0Sdrh sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0); 19950bce8354Sdrh assert( pParse->aAgg[i].pFunc!=0 ); 19960bce8354Sdrh assert( pParse->aAgg[i].pFunc->xStep!=0 ); 19970bce8354Sdrh sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER); 19982282792aSdrh } 19992282792aSdrh } 20002282792aSdrh 2001cce7d176Sdrh /* End the database scan loop. 2002cce7d176Sdrh */ 2003cce7d176Sdrh sqliteWhereEnd(pWInfo); 2004cce7d176Sdrh 20052282792aSdrh /* If we are processing aggregates, we need to set up a second loop 20062282792aSdrh ** over all of the aggregate values and process them. 20072282792aSdrh */ 20082282792aSdrh if( isAgg ){ 20092282792aSdrh int endagg = sqliteVdbeMakeLabel(v); 20102282792aSdrh int startagg; 201199fcd718Sdrh startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg); 20122282792aSdrh pParse->useAgg = 1; 20132282792aSdrh if( pHaving ){ 2014f5905aa7Sdrh sqliteExprIfFalse(pParse, pHaving, startagg, 1); 20152282792aSdrh } 2016df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 2017df199a25Sdrh iParm, startagg, endagg) ){ 20181d83f052Sdrh goto select_end; 20192282792aSdrh } 202099fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, startagg); 202199fcd718Sdrh sqliteVdbeResolveLabel(v, endagg); 202299fcd718Sdrh sqliteVdbeAddOp(v, OP_Noop, 0, 0); 20232282792aSdrh pParse->useAgg = 0; 20242282792aSdrh } 20252282792aSdrh 2026cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 2027cce7d176Sdrh ** and send them to the callback one by one. 2028cce7d176Sdrh */ 2029cce7d176Sdrh if( pOrderBy ){ 2030c926afbcSdrh generateSortTail(p, v, pEList->nExpr, eDest, iParm); 2031cce7d176Sdrh } 20326a535340Sdrh 20336a535340Sdrh 20346a535340Sdrh /* Issue a null callback if that is what the user wants. 20356a535340Sdrh */ 20366a535340Sdrh if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){ 20376a535340Sdrh sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0); 20386a535340Sdrh } 20396a535340Sdrh 20401d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 20411d83f052Sdrh ** to indicate no errors. 20421d83f052Sdrh */ 20431d83f052Sdrh rc = 0; 20441d83f052Sdrh 20451d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 20461d83f052Sdrh ** successful coding of the SELECT. 20471d83f052Sdrh */ 20481d83f052Sdrh select_end: 2049832508b7Sdrh pParse->nTab = base; 20501d83f052Sdrh sqliteAggregateInfoReset(pParse); 20511d83f052Sdrh return rc; 2052cce7d176Sdrh } 2053