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*f9b596ebSdrh ** $Id: select.c,v 1.178 2004/05/26 16:54:44 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19315555caSdrh 20cce7d176Sdrh /* 219bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that 229bb61fe7Sdrh ** structure. 23cce7d176Sdrh */ 244adee20fSdanielk1977 Select *sqlite3SelectNew( 25daffd0e5Sdrh ExprList *pEList, /* which columns to include in the result */ 26ad3cab52Sdrh SrcList *pSrc, /* the FROM clause -- which tables to scan */ 27daffd0e5Sdrh Expr *pWhere, /* the WHERE clause */ 28daffd0e5Sdrh ExprList *pGroupBy, /* the GROUP BY clause */ 29daffd0e5Sdrh Expr *pHaving, /* the HAVING clause */ 30daffd0e5Sdrh ExprList *pOrderBy, /* the ORDER BY clause */ 319bbca4c1Sdrh int isDistinct, /* true if the DISTINCT keyword is present */ 329bbca4c1Sdrh int nLimit, /* LIMIT value. -1 means not used */ 33ef0cae50Sdrh int nOffset /* OFFSET value. 0 means no offset */ 349bb61fe7Sdrh ){ 359bb61fe7Sdrh Select *pNew; 369bb61fe7Sdrh pNew = sqliteMalloc( sizeof(*pNew) ); 37daffd0e5Sdrh if( pNew==0 ){ 384adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 394adee20fSdanielk1977 sqlite3SrcListDelete(pSrc); 404adee20fSdanielk1977 sqlite3ExprDelete(pWhere); 414adee20fSdanielk1977 sqlite3ExprListDelete(pGroupBy); 424adee20fSdanielk1977 sqlite3ExprDelete(pHaving); 434adee20fSdanielk1977 sqlite3ExprListDelete(pOrderBy); 44daffd0e5Sdrh }else{ 45b733d037Sdrh if( pEList==0 ){ 464adee20fSdanielk1977 pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0); 47b733d037Sdrh } 489bb61fe7Sdrh pNew->pEList = pEList; 499bb61fe7Sdrh pNew->pSrc = pSrc; 509bb61fe7Sdrh pNew->pWhere = pWhere; 519bb61fe7Sdrh pNew->pGroupBy = pGroupBy; 529bb61fe7Sdrh pNew->pHaving = pHaving; 539bb61fe7Sdrh pNew->pOrderBy = pOrderBy; 549bb61fe7Sdrh pNew->isDistinct = isDistinct; 5582c3d636Sdrh pNew->op = TK_SELECT; 569bbca4c1Sdrh pNew->nLimit = nLimit; 579bbca4c1Sdrh pNew->nOffset = nOffset; 587b58daeaSdrh pNew->iLimit = -1; 597b58daeaSdrh pNew->iOffset = -1; 60daffd0e5Sdrh } 619bb61fe7Sdrh return pNew; 629bb61fe7Sdrh } 639bb61fe7Sdrh 649bb61fe7Sdrh /* 6501f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the 6601f3f253Sdrh ** type of join. Return an integer constant that expresses that type 6701f3f253Sdrh ** in terms of the following bit values: 6801f3f253Sdrh ** 6901f3f253Sdrh ** JT_INNER 7001f3f253Sdrh ** JT_OUTER 7101f3f253Sdrh ** JT_NATURAL 7201f3f253Sdrh ** JT_LEFT 7301f3f253Sdrh ** JT_RIGHT 7401f3f253Sdrh ** 7501f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT. 7601f3f253Sdrh ** 7701f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return 7801f3f253Sdrh ** a join type, but put an error in the pParse structure. 7901f3f253Sdrh */ 804adee20fSdanielk1977 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ 8101f3f253Sdrh int jointype = 0; 8201f3f253Sdrh Token *apAll[3]; 8301f3f253Sdrh Token *p; 8401f3f253Sdrh static struct { 8501f3f253Sdrh const char *zKeyword; 8601f3f253Sdrh int nChar; 8701f3f253Sdrh int code; 8801f3f253Sdrh } keywords[] = { 8901f3f253Sdrh { "natural", 7, JT_NATURAL }, 90195e6967Sdrh { "left", 4, JT_LEFT|JT_OUTER }, 91195e6967Sdrh { "right", 5, JT_RIGHT|JT_OUTER }, 92195e6967Sdrh { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, 9301f3f253Sdrh { "outer", 5, JT_OUTER }, 9401f3f253Sdrh { "inner", 5, JT_INNER }, 9501f3f253Sdrh { "cross", 5, JT_INNER }, 9601f3f253Sdrh }; 9701f3f253Sdrh int i, j; 9801f3f253Sdrh apAll[0] = pA; 9901f3f253Sdrh apAll[1] = pB; 10001f3f253Sdrh apAll[2] = pC; 101195e6967Sdrh for(i=0; i<3 && apAll[i]; i++){ 10201f3f253Sdrh p = apAll[i]; 10301f3f253Sdrh for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ 10401f3f253Sdrh if( p->n==keywords[j].nChar 1054adee20fSdanielk1977 && sqlite3StrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ 10601f3f253Sdrh jointype |= keywords[j].code; 10701f3f253Sdrh break; 10801f3f253Sdrh } 10901f3f253Sdrh } 11001f3f253Sdrh if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ 11101f3f253Sdrh jointype |= JT_ERROR; 11201f3f253Sdrh break; 11301f3f253Sdrh } 11401f3f253Sdrh } 115ad2d8307Sdrh if( 116ad2d8307Sdrh (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || 117195e6967Sdrh (jointype & JT_ERROR)!=0 118ad2d8307Sdrh ){ 11901f3f253Sdrh static Token dummy = { 0, 0 }; 12001f3f253Sdrh char *zSp1 = " ", *zSp2 = " "; 12101f3f253Sdrh if( pB==0 ){ pB = &dummy; zSp1 = 0; } 12201f3f253Sdrh if( pC==0 ){ pC = &dummy; zSp2 = 0; } 1234adee20fSdanielk1977 sqlite3SetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0, 12401f3f253Sdrh pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0); 12501f3f253Sdrh pParse->nErr++; 12601f3f253Sdrh jointype = JT_INNER; 127195e6967Sdrh }else if( jointype & JT_RIGHT ){ 1284adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 129da93d238Sdrh "RIGHT and FULL OUTER JOINs are not currently supported"); 130195e6967Sdrh jointype = JT_INNER; 13101f3f253Sdrh } 13201f3f253Sdrh return jointype; 13301f3f253Sdrh } 13401f3f253Sdrh 13501f3f253Sdrh /* 136ad2d8307Sdrh ** Return the index of a column in a table. Return -1 if the column 137ad2d8307Sdrh ** is not contained in the table. 138ad2d8307Sdrh */ 139ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){ 140ad2d8307Sdrh int i; 141ad2d8307Sdrh for(i=0; i<pTab->nCol; i++){ 1424adee20fSdanielk1977 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; 143ad2d8307Sdrh } 144ad2d8307Sdrh return -1; 145ad2d8307Sdrh } 146ad2d8307Sdrh 147ad2d8307Sdrh /* 148ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the 149ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2. 150ad2d8307Sdrh */ 151ad2d8307Sdrh static void addWhereTerm( 152ad2d8307Sdrh const char *zCol, /* Name of the column */ 153ad2d8307Sdrh const Table *pTab1, /* First table */ 154ad2d8307Sdrh const Table *pTab2, /* Second table */ 155ad2d8307Sdrh Expr **ppExpr /* Add the equality term to this expression */ 156ad2d8307Sdrh ){ 157ad2d8307Sdrh Token dummy; 158ad2d8307Sdrh Expr *pE1a, *pE1b, *pE1c; 159ad2d8307Sdrh Expr *pE2a, *pE2b, *pE2c; 160ad2d8307Sdrh Expr *pE; 161ad2d8307Sdrh 162ad2d8307Sdrh dummy.z = zCol; 163ad2d8307Sdrh dummy.n = strlen(zCol); 1644b59ab5eSdrh dummy.dyn = 0; 1654adee20fSdanielk1977 pE1a = sqlite3Expr(TK_ID, 0, 0, &dummy); 1664adee20fSdanielk1977 pE2a = sqlite3Expr(TK_ID, 0, 0, &dummy); 167ad2d8307Sdrh dummy.z = pTab1->zName; 168ad2d8307Sdrh dummy.n = strlen(dummy.z); 1694adee20fSdanielk1977 pE1b = sqlite3Expr(TK_ID, 0, 0, &dummy); 170ad2d8307Sdrh dummy.z = pTab2->zName; 171ad2d8307Sdrh dummy.n = strlen(dummy.z); 1724adee20fSdanielk1977 pE2b = sqlite3Expr(TK_ID, 0, 0, &dummy); 1734adee20fSdanielk1977 pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0); 1744adee20fSdanielk1977 pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0); 1754adee20fSdanielk1977 pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0); 1761f16230bSdrh ExprSetProperty(pE, EP_FromJoin); 177ad2d8307Sdrh if( *ppExpr ){ 1784adee20fSdanielk1977 *ppExpr = sqlite3Expr(TK_AND, *ppExpr, pE, 0); 179ad2d8307Sdrh }else{ 180ad2d8307Sdrh *ppExpr = pE; 181ad2d8307Sdrh } 182ad2d8307Sdrh } 183ad2d8307Sdrh 184ad2d8307Sdrh /* 1851f16230bSdrh ** Set the EP_FromJoin property on all terms of the given expression. 1861cc093c2Sdrh ** 187e78e8284Sdrh ** The EP_FromJoin property is used on terms of an expression to tell 1881cc093c2Sdrh ** the LEFT OUTER JOIN processing logic that this term is part of the 1891f16230bSdrh ** join restriction specified in the ON or USING clause and not a part 1901f16230bSdrh ** of the more general WHERE clause. These terms are moved over to the 1911f16230bSdrh ** WHERE clause during join processing but we need to remember that they 1921f16230bSdrh ** originated in the ON or USING clause. 1931cc093c2Sdrh */ 1941cc093c2Sdrh static void setJoinExpr(Expr *p){ 1951cc093c2Sdrh while( p ){ 1961f16230bSdrh ExprSetProperty(p, EP_FromJoin); 1971cc093c2Sdrh setJoinExpr(p->pLeft); 1981cc093c2Sdrh p = p->pRight; 1991cc093c2Sdrh } 2001cc093c2Sdrh } 2011cc093c2Sdrh 2021cc093c2Sdrh /* 203ad2d8307Sdrh ** This routine processes the join information for a SELECT statement. 204ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause. 205ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms. 206ad2d8307Sdrh ** 207ad2d8307Sdrh ** This routine returns the number of errors encountered. 208ad2d8307Sdrh */ 209ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){ 210ad2d8307Sdrh SrcList *pSrc; 211ad2d8307Sdrh int i, j; 212ad2d8307Sdrh pSrc = p->pSrc; 213ad2d8307Sdrh for(i=0; i<pSrc->nSrc-1; i++){ 214ad2d8307Sdrh struct SrcList_item *pTerm = &pSrc->a[i]; 215ad2d8307Sdrh struct SrcList_item *pOther = &pSrc->a[i+1]; 216ad2d8307Sdrh 217ad2d8307Sdrh if( pTerm->pTab==0 || pOther->pTab==0 ) continue; 218ad2d8307Sdrh 219ad2d8307Sdrh /* When the NATURAL keyword is present, add WHERE clause terms for 220ad2d8307Sdrh ** every column that the two tables have in common. 221ad2d8307Sdrh */ 222ad2d8307Sdrh if( pTerm->jointype & JT_NATURAL ){ 223ad2d8307Sdrh Table *pTab; 224ad2d8307Sdrh if( pTerm->pOn || pTerm->pUsing ){ 2254adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "a NATURAL join may not have " 226ad2d8307Sdrh "an ON or USING clause", 0); 227ad2d8307Sdrh return 1; 228ad2d8307Sdrh } 229ad2d8307Sdrh pTab = pTerm->pTab; 230ad2d8307Sdrh for(j=0; j<pTab->nCol; j++){ 231ad2d8307Sdrh if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){ 232ad2d8307Sdrh addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere); 233ad2d8307Sdrh } 234ad2d8307Sdrh } 235ad2d8307Sdrh } 236ad2d8307Sdrh 237ad2d8307Sdrh /* Disallow both ON and USING clauses in the same join 238ad2d8307Sdrh */ 239ad2d8307Sdrh if( pTerm->pOn && pTerm->pUsing ){ 2404adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot have both ON and USING " 241da93d238Sdrh "clauses in the same join"); 242ad2d8307Sdrh return 1; 243ad2d8307Sdrh } 244ad2d8307Sdrh 245ad2d8307Sdrh /* Add the ON clause to the end of the WHERE clause, connected by 246ad2d8307Sdrh ** and AND operator. 247ad2d8307Sdrh */ 248ad2d8307Sdrh if( pTerm->pOn ){ 2491cc093c2Sdrh setJoinExpr(pTerm->pOn); 250ad2d8307Sdrh if( p->pWhere==0 ){ 251ad2d8307Sdrh p->pWhere = pTerm->pOn; 252ad2d8307Sdrh }else{ 2534adee20fSdanielk1977 p->pWhere = sqlite3Expr(TK_AND, p->pWhere, pTerm->pOn, 0); 254ad2d8307Sdrh } 255ad2d8307Sdrh pTerm->pOn = 0; 256ad2d8307Sdrh } 257ad2d8307Sdrh 258ad2d8307Sdrh /* Create extra terms on the WHERE clause for each column named 259ad2d8307Sdrh ** in the USING clause. Example: If the two tables to be joined are 260ad2d8307Sdrh ** A and B and the USING clause names X, Y, and Z, then add this 261ad2d8307Sdrh ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z 262ad2d8307Sdrh ** Report an error if any column mentioned in the USING clause is 263ad2d8307Sdrh ** not contained in both tables to be joined. 264ad2d8307Sdrh */ 265ad2d8307Sdrh if( pTerm->pUsing ){ 266ad2d8307Sdrh IdList *pList; 267ad2d8307Sdrh int j; 268ad2d8307Sdrh assert( i<pSrc->nSrc-1 ); 269ad2d8307Sdrh pList = pTerm->pUsing; 270ad2d8307Sdrh for(j=0; j<pList->nId; j++){ 271bf5cd97eSdrh if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 || 272bf5cd97eSdrh columnIndex(pOther->pTab, pList->a[j].zName)<0 ){ 2734adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot join using column %s - column " 274da93d238Sdrh "not present in both tables", pList->a[j].zName); 275ad2d8307Sdrh return 1; 276ad2d8307Sdrh } 277bf5cd97eSdrh addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere); 278ad2d8307Sdrh } 279ad2d8307Sdrh } 280ad2d8307Sdrh } 281ad2d8307Sdrh return 0; 282ad2d8307Sdrh } 283ad2d8307Sdrh 284ad2d8307Sdrh /* 2859bb61fe7Sdrh ** Delete the given Select structure and all of its substructures. 2869bb61fe7Sdrh */ 2874adee20fSdanielk1977 void sqlite3SelectDelete(Select *p){ 28882c3d636Sdrh if( p==0 ) return; 2894adee20fSdanielk1977 sqlite3ExprListDelete(p->pEList); 2904adee20fSdanielk1977 sqlite3SrcListDelete(p->pSrc); 2914adee20fSdanielk1977 sqlite3ExprDelete(p->pWhere); 2924adee20fSdanielk1977 sqlite3ExprListDelete(p->pGroupBy); 2934adee20fSdanielk1977 sqlite3ExprDelete(p->pHaving); 2944adee20fSdanielk1977 sqlite3ExprListDelete(p->pOrderBy); 2954adee20fSdanielk1977 sqlite3SelectDelete(p->pPrior); 296a76b5dfcSdrh sqliteFree(p->zSelect); 2979bb61fe7Sdrh sqliteFree(p); 2989bb61fe7Sdrh } 2999bb61fe7Sdrh 3009bb61fe7Sdrh /* 3012282792aSdrh ** Delete the aggregate information from the parse structure. 3022282792aSdrh */ 3031d83f052Sdrh static void sqliteAggregateInfoReset(Parse *pParse){ 3042282792aSdrh sqliteFree(pParse->aAgg); 3052282792aSdrh pParse->aAgg = 0; 3062282792aSdrh pParse->nAgg = 0; 3072282792aSdrh pParse->useAgg = 0; 3082282792aSdrh } 3092282792aSdrh 3102282792aSdrh /* 311c926afbcSdrh ** Insert code into "v" that will push the record on the top of the 312c926afbcSdrh ** stack into the sorter. 313428702d7Sdrh ** 314428702d7Sdrh ** FIX ME: Change this so that it uses the OP_MakeKey opcode 315428702d7Sdrh ** instead of OP_SortMakeKey. Delete the OP_SortMakeKey opcode. 316428702d7Sdrh ** All columns should have affinity NONE. Handle ASC versus 317428702d7Sdrh ** DESC sort order by defining a list of comparison functions to 318428702d7Sdrh ** be used by the OP_Sort opcode. 319c926afbcSdrh */ 320c926afbcSdrh static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){ 321c926afbcSdrh int i; 322c926afbcSdrh for(i=0; i<pOrderBy->nExpr; i++){ 3234adee20fSdanielk1977 sqlite3ExprCode(pParse, pOrderBy->a[i].pExpr); 324c926afbcSdrh } 325ffbc3088Sdrh sqlite3VdbeAddOp(v, OP_MakeKey, pOrderBy->nExpr, 0); 3264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SortPut, 0, 0); 327c926afbcSdrh } 328c926afbcSdrh 329c926afbcSdrh /* 3302282792aSdrh ** This routine generates the code for the inside of the inner loop 3312282792aSdrh ** of a SELECT. 33282c3d636Sdrh ** 33338640e15Sdrh ** If srcTab and nColumn are both zero, then the pEList expressions 33438640e15Sdrh ** are evaluated in order to get the data for this row. If nColumn>0 33538640e15Sdrh ** then data is pulled from srcTab and pEList is used only to get the 33638640e15Sdrh ** datatypes for each column. 3372282792aSdrh */ 3382282792aSdrh static int selectInnerLoop( 3392282792aSdrh Parse *pParse, /* The parser context */ 340df199a25Sdrh Select *p, /* The complete select statement being coded */ 3412282792aSdrh ExprList *pEList, /* List of values being extracted */ 34282c3d636Sdrh int srcTab, /* Pull data from this table */ 343967e8b73Sdrh int nColumn, /* Number of columns in the source table */ 3442282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 3452282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 3462282792aSdrh int eDest, /* How to dispose of the results */ 3472282792aSdrh int iParm, /* An argument to the disposal method */ 3482282792aSdrh int iContinue, /* Jump here to continue with next row */ 34984ac9d02Sdanielk1977 int iBreak, /* Jump here to break out of the inner loop */ 35084ac9d02Sdanielk1977 char *aff /* affinity string if eDest is SRT_Union */ 3512282792aSdrh ){ 3522282792aSdrh Vdbe *v = pParse->pVdbe; 3532282792aSdrh int i; 35438640e15Sdrh 355daffd0e5Sdrh if( v==0 ) return 0; 35638640e15Sdrh assert( pEList!=0 ); 3572282792aSdrh 358df199a25Sdrh /* If there was a LIMIT clause on the SELECT statement, then do the check 359df199a25Sdrh ** to see if this row should be output. 360df199a25Sdrh */ 361df199a25Sdrh if( pOrderBy==0 ){ 3627b58daeaSdrh if( p->iOffset>=0 ){ 3634adee20fSdanielk1977 int addr = sqlite3VdbeCurrentAddr(v); 3644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr+2); 3654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 366df199a25Sdrh } 3677b58daeaSdrh if( p->iLimit>=0 ){ 3684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak); 369df199a25Sdrh } 370df199a25Sdrh } 371df199a25Sdrh 372967e8b73Sdrh /* Pull the requested columns. 3732282792aSdrh */ 37438640e15Sdrh if( nColumn>0 ){ 375967e8b73Sdrh for(i=0; i<nColumn; i++){ 3764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, i); 37782c3d636Sdrh } 37838640e15Sdrh }else{ 37938640e15Sdrh nColumn = pEList->nExpr; 38038640e15Sdrh for(i=0; i<pEList->nExpr; i++){ 3814adee20fSdanielk1977 sqlite3ExprCode(pParse, pEList->a[i].pExpr); 38238640e15Sdrh } 38382c3d636Sdrh } 3842282792aSdrh 385daffd0e5Sdrh /* If the DISTINCT keyword was present on the SELECT statement 386daffd0e5Sdrh ** and this row has been seen before, then do not make this row 387daffd0e5Sdrh ** part of the result. 3882282792aSdrh */ 389f5905aa7Sdrh if( distinct>=0 && pEList && pEList->nExpr>0 ){ 3900bd1f4eaSdrh #if NULL_ALWAYS_DISTINCT 3914adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7); 3920bd1f4eaSdrh #endif 393d3d39e93Sdrh /* Deliberately leave the affinity string off of the following OP_MakeKey */ 3944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1); 3954adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3); 3964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); 3974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 3984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 3994adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, distinct, 0); 4002282792aSdrh } 40182c3d636Sdrh 402c926afbcSdrh switch( eDest ){ 40382c3d636Sdrh /* In this mode, write each query result to the key of the temporary 40482c3d636Sdrh ** table iParm. 4052282792aSdrh */ 406c926afbcSdrh case SRT_Union: { 4074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 40884ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 4094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 4104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, iParm, 0); 411c926afbcSdrh break; 412c926afbcSdrh } 41382c3d636Sdrh 4145974a30fSdrh /* Store the result as data using a unique key. 4155974a30fSdrh */ 416c926afbcSdrh case SRT_Table: 417c926afbcSdrh case SRT_TempTable: { 4184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 419c926afbcSdrh if( pOrderBy ){ 420c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 421c926afbcSdrh }else{ 4224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0); 4234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 4244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0); 425c926afbcSdrh } 426c926afbcSdrh break; 427c926afbcSdrh } 4285974a30fSdrh 42982c3d636Sdrh /* Construct a record from the query result, but instead of 43082c3d636Sdrh ** saving that record, use it as a key to delete elements from 43182c3d636Sdrh ** the temporary table iParm. 43282c3d636Sdrh */ 433c926afbcSdrh case SRT_Except: { 4340bd1f4eaSdrh int addr; 4354adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 43684ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 4374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3); 4384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Delete, iParm, 0); 439c926afbcSdrh break; 440c926afbcSdrh } 4412282792aSdrh 4422282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 4432282792aSdrh ** then there should be a single item on the stack. Write this 4442282792aSdrh ** item into the set table with bogus data. 4452282792aSdrh */ 446c926afbcSdrh case SRT_Set: { 4474adee20fSdanielk1977 int addr1 = sqlite3VdbeCurrentAddr(v); 44852b36cabSdrh int addr2; 449e014a838Sdanielk1977 450967e8b73Sdrh assert( nColumn==1 ); 4514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3); 4524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 4534adee20fSdanielk1977 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 454c926afbcSdrh if( pOrderBy ){ 455c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 456c926afbcSdrh }else{ 457e014a838Sdanielk1977 char const *affStr; 458e014a838Sdanielk1977 char aff = (iParm>>16)&0xFF; 459e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff); 460e014a838Sdanielk1977 affStr = sqlite3AffinityString(aff); 46184ac9d02Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); 4624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 463e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0); 464c926afbcSdrh } 4654adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v)); 466c926afbcSdrh break; 467c926afbcSdrh } 46882c3d636Sdrh 4692282792aSdrh /* If this is a scalar select that is part of an expression, then 4702282792aSdrh ** store the results in the appropriate memory cell and break out 4712282792aSdrh ** of the scan loop. 4722282792aSdrh */ 473c926afbcSdrh case SRT_Mem: { 474967e8b73Sdrh assert( nColumn==1 ); 475c926afbcSdrh if( pOrderBy ){ 476c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 477c926afbcSdrh }else{ 4784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 4794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iBreak); 480c926afbcSdrh } 481c926afbcSdrh break; 482c926afbcSdrh } 4832282792aSdrh 484f46f905aSdrh /* Send the data to the callback function. 485f46f905aSdrh */ 486f46f905aSdrh case SRT_Callback: 487f46f905aSdrh case SRT_Sorter: { 488f46f905aSdrh if( pOrderBy ){ 489ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 490f46f905aSdrh pushOntoSorter(pParse, v, pOrderBy); 491f46f905aSdrh }else{ 492f46f905aSdrh assert( eDest==SRT_Callback ); 4934adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 494f46f905aSdrh } 495f46f905aSdrh break; 496f46f905aSdrh } 497f46f905aSdrh 498142e30dfSdrh /* Invoke a subroutine to handle the results. The subroutine itself 499142e30dfSdrh ** is responsible for popping the results off of the stack. 500142e30dfSdrh */ 501142e30dfSdrh case SRT_Subroutine: { 502ac82fcf5Sdrh if( pOrderBy ){ 5034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 504ac82fcf5Sdrh pushOntoSorter(pParse, v, pOrderBy); 505ac82fcf5Sdrh }else{ 5064adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 507ac82fcf5Sdrh } 508142e30dfSdrh break; 509142e30dfSdrh } 510142e30dfSdrh 511d7489c39Sdrh /* Discard the results. This is used for SELECT statements inside 512d7489c39Sdrh ** the body of a TRIGGER. The purpose of such selects is to call 513d7489c39Sdrh ** user-defined functions that have side effects. We do not care 514d7489c39Sdrh ** about the actual results of the select. 515d7489c39Sdrh */ 516c926afbcSdrh default: { 517f46f905aSdrh assert( eDest==SRT_Discard ); 5184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 519c926afbcSdrh break; 520c926afbcSdrh } 521c926afbcSdrh } 52282c3d636Sdrh return 0; 52382c3d636Sdrh } 52482c3d636Sdrh 52582c3d636Sdrh /* 526d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 527d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 528d8bc7086Sdrh ** we need to run the sorter and output the results. The following 529d8bc7086Sdrh ** routine generates the code needed to do that. 530d8bc7086Sdrh */ 531c926afbcSdrh static void generateSortTail( 532ffbc3088Sdrh Parse *pParse, /* The parsing context */ 533c926afbcSdrh Select *p, /* The SELECT statement */ 534c926afbcSdrh Vdbe *v, /* Generate code into this VDBE */ 535c926afbcSdrh int nColumn, /* Number of columns of data */ 536c926afbcSdrh int eDest, /* Write the sorted results here */ 537c926afbcSdrh int iParm /* Optional parameter associated with eDest */ 538c926afbcSdrh ){ 5394adee20fSdanielk1977 int end1 = sqlite3VdbeMakeLabel(v); 5404adee20fSdanielk1977 int end2 = sqlite3VdbeMakeLabel(v); 541d8bc7086Sdrh int addr; 542ffbc3088Sdrh KeyInfo *pInfo; 543ffbc3088Sdrh ExprList *pOrderBy; 544ffbc3088Sdrh int nCol, i; 545ffbc3088Sdrh sqlite *db = pParse->db; 546ffbc3088Sdrh 547f46f905aSdrh if( eDest==SRT_Sorter ) return; 548ffbc3088Sdrh pOrderBy = p->pOrderBy; 549ffbc3088Sdrh nCol = pOrderBy->nExpr; 550ffbc3088Sdrh pInfo = sqliteMalloc( sizeof(*pInfo) + nCol*(sizeof(CollSeq*)+1) ); 551ffbc3088Sdrh if( pInfo==0 ) return; 552ffbc3088Sdrh pInfo->aSortOrder = (char*)&pInfo->aColl[nCol]; 553ffbc3088Sdrh pInfo->nField = nCol; 554ffbc3088Sdrh for(i=0; i<nCol; i++){ 555ffbc3088Sdrh pInfo->aColl[i] = db->pDfltColl; 556ffbc3088Sdrh pInfo->aSortOrder[i] = pOrderBy->a[i].sortOrder; 557ffbc3088Sdrh } 558ffbc3088Sdrh sqlite3VdbeOp3(v, OP_Sort, 0, 0, (char*)pInfo, P3_KEYINFO_HANDOFF); 5594adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_SortNext, 0, end1); 5607b58daeaSdrh if( p->iOffset>=0 ){ 5614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr+4); 5624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 5634adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr); 564df199a25Sdrh } 5657b58daeaSdrh if( p->iLimit>=0 ){ 5664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, end2); 567df199a25Sdrh } 568c926afbcSdrh switch( eDest ){ 569c926afbcSdrh case SRT_Table: 570c926afbcSdrh case SRT_TempTable: { 5714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0); 5724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 5734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0); 574c926afbcSdrh break; 575c926afbcSdrh } 576c926afbcSdrh case SRT_Set: { 577c926afbcSdrh assert( nColumn==1 ); 5784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 5794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 5804adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3); 581d6861792Sdrh sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, "n", P3_STATIC); 5824adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 583e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0); 584c926afbcSdrh break; 585c926afbcSdrh } 586c926afbcSdrh case SRT_Mem: { 587c926afbcSdrh assert( nColumn==1 ); 5884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 5894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, end1); 590c926afbcSdrh break; 591c926afbcSdrh } 592ce665cf6Sdrh case SRT_Callback: 593ac82fcf5Sdrh case SRT_Subroutine: { 594ac82fcf5Sdrh int i; 59584ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0); 59684ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 597ac82fcf5Sdrh for(i=0; i<nColumn; i++){ 5984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, -1-i, i); 599ac82fcf5Sdrh } 600ce665cf6Sdrh if( eDest==SRT_Callback ){ 601ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 602ce665cf6Sdrh }else{ 6034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 604ce665cf6Sdrh } 60584ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 606ac82fcf5Sdrh break; 607ac82fcf5Sdrh } 608c926afbcSdrh default: { 609f46f905aSdrh /* Do nothing */ 610c926afbcSdrh break; 611c926afbcSdrh } 612c926afbcSdrh } 6134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr); 6144adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, end2); 6154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 6164adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, end1); 6174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SortReset, 0, 0); 618d8bc7086Sdrh } 619d8bc7086Sdrh 620d8bc7086Sdrh /* 621fcb78a49Sdrh ** Generate code that will tell the VDBE the datatypes of 622fcb78a49Sdrh ** columns in the result set. 623e78e8284Sdrh ** 624e78e8284Sdrh ** This routine only generates code if the "PRAGMA show_datatypes=on" 625e78e8284Sdrh ** has been executed. The datatypes are reported out in the azCol 626e78e8284Sdrh ** parameter to the callback function. The first N azCol[] entries 627e78e8284Sdrh ** are the names of the columns, and the second N entries are the 628e78e8284Sdrh ** datatypes for the columns. 629e78e8284Sdrh ** 630e78e8284Sdrh ** The "datatype" for a result that is a column of a type is the 631e78e8284Sdrh ** datatype definition extracted from the CREATE TABLE statement. 632e78e8284Sdrh ** The datatype for an expression is either TEXT or NUMERIC. The 633e78e8284Sdrh ** datatype for a ROWID field is INTEGER. 634fcb78a49Sdrh */ 635fcb78a49Sdrh static void generateColumnTypes( 636fcb78a49Sdrh Parse *pParse, /* Parser context */ 637fcb78a49Sdrh SrcList *pTabList, /* List of tables */ 638fcb78a49Sdrh ExprList *pEList /* Expressions defining the result set */ 639fcb78a49Sdrh ){ 640fcb78a49Sdrh Vdbe *v = pParse->pVdbe; 6416a3ea0e6Sdrh int i, j; 642fcb78a49Sdrh for(i=0; i<pEList->nExpr; i++){ 643fcb78a49Sdrh Expr *p = pEList->a[i].pExpr; 644fcb78a49Sdrh char *zType = 0; 645fcb78a49Sdrh if( p==0 ) continue; 646fcb78a49Sdrh if( p->op==TK_COLUMN && pTabList ){ 6476a3ea0e6Sdrh Table *pTab; 648fcb78a49Sdrh int iCol = p->iColumn; 6496a3ea0e6Sdrh for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} 6506a3ea0e6Sdrh assert( j<pTabList->nSrc ); 6516a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 652fcb78a49Sdrh if( iCol<0 ) iCol = pTab->iPKey; 653fcb78a49Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 654fcb78a49Sdrh if( iCol<0 ){ 655fcb78a49Sdrh zType = "INTEGER"; 656fcb78a49Sdrh }else{ 657fcb78a49Sdrh zType = pTab->aCol[iCol].zType; 658fcb78a49Sdrh } 659fcb78a49Sdrh }else{ 660736c22b8Sdrh switch( sqlite3ExprType(p) ){ 661736c22b8Sdrh case SQLITE_AFF_TEXT: zType = "TEXT"; break; 662736c22b8Sdrh case SQLITE_AFF_NUMERIC: zType = "NUMERIC"; break; 663736c22b8Sdrh default: zType = "ANY"; break; 664736c22b8Sdrh } 665fcb78a49Sdrh } 6664adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_ColumnName, i + pEList->nExpr, 0, zType, 0); 667fcb78a49Sdrh } 668fcb78a49Sdrh } 669fcb78a49Sdrh 670fcb78a49Sdrh /* 671fcb78a49Sdrh ** Generate code that will tell the VDBE the names of columns 672fcb78a49Sdrh ** in the result set. This information is used to provide the 673fcabd464Sdrh ** azCol[] values in the callback. 67482c3d636Sdrh */ 675832508b7Sdrh static void generateColumnNames( 676832508b7Sdrh Parse *pParse, /* Parser context */ 677ad3cab52Sdrh SrcList *pTabList, /* List of tables */ 678832508b7Sdrh ExprList *pEList /* Expressions defining the result set */ 679832508b7Sdrh ){ 680d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 6816a3ea0e6Sdrh int i, j; 682fcabd464Sdrh sqlite *db = pParse->db; 683fcabd464Sdrh int fullNames, shortNames; 684fcabd464Sdrh 6853cf86063Sdanielk1977 /* If this is an EXPLAIN, skip this step */ 6863cf86063Sdanielk1977 if( pParse->explain ){ 6873cf86063Sdanielk1977 return SQLITE_OK; 6883cf86063Sdanielk1977 } 6893cf86063Sdanielk1977 690d6502758Sdrh assert( v!=0 ); 6916f8a503dSdanielk1977 if( pParse->colNamesSet || v==0 || sqlite3_malloc_failed ) return; 692d8bc7086Sdrh pParse->colNamesSet = 1; 693fcabd464Sdrh fullNames = (db->flags & SQLITE_FullColNames)!=0; 694fcabd464Sdrh shortNames = (db->flags & SQLITE_ShortColNames)!=0; 69522322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, pEList->nExpr); 69682c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 69782c3d636Sdrh Expr *p; 698d6502758Sdrh int p2 = i==pEList->nExpr-1; 6995a38705eSdrh p = pEList->a[i].pExpr; 7005a38705eSdrh if( p==0 ) continue; 70182c3d636Sdrh if( pEList->a[i].zName ){ 70282c3d636Sdrh char *zName = pEList->a[i].zName; 7033cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, 0); 70482c3d636Sdrh continue; 70582c3d636Sdrh } 706fa173a76Sdrh if( p->op==TK_COLUMN && pTabList ){ 7076a3ea0e6Sdrh Table *pTab; 70897665873Sdrh char *zCol; 7098aff1015Sdrh int iCol = p->iColumn; 7106a3ea0e6Sdrh for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} 7116a3ea0e6Sdrh assert( j<pTabList->nSrc ); 7126a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 7138aff1015Sdrh if( iCol<0 ) iCol = pTab->iPKey; 71497665873Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 715b1363206Sdrh if( iCol<0 ){ 716b1363206Sdrh zCol = "_ROWID_"; 717b1363206Sdrh }else{ 718b1363206Sdrh zCol = pTab->aCol[iCol].zName; 719b1363206Sdrh } 720fcabd464Sdrh if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ 7213cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); 7223cf86063Sdanielk1977 /* sqlite3VdbeCompressSpace(v, addr); */ 723fcabd464Sdrh }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ 72482c3d636Sdrh char *zName = 0; 72582c3d636Sdrh char *zTab; 72682c3d636Sdrh 7276a3ea0e6Sdrh zTab = pTabList->a[j].zAlias; 728fcabd464Sdrh if( fullNames || zTab==0 ) zTab = pTab->zName; 7294adee20fSdanielk1977 sqlite3SetString(&zName, zTab, ".", zCol, 0); 7303cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, P3_DYNAMIC); 73182c3d636Sdrh }else{ 7323cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zCol, 0); 73382c3d636Sdrh } 7346977fea8Sdrh }else if( p->span.z && p->span.z[0] ){ 7353cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); 7363cf86063Sdanielk1977 /* sqlite3VdbeCompressSpace(v, addr); */ 7371bee3d7bSdrh }else{ 7381bee3d7bSdrh char zName[30]; 7391bee3d7bSdrh assert( p->op!=TK_COLUMN || pTabList==0 ); 7401bee3d7bSdrh sprintf(zName, "column%d", i+1); 7413cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, 0); 74282c3d636Sdrh } 74382c3d636Sdrh } 7445080aaa7Sdrh } 74582c3d636Sdrh 74682c3d636Sdrh /* 747d8bc7086Sdrh ** Name of the connection operator, used for error messages. 748d8bc7086Sdrh */ 749d8bc7086Sdrh static const char *selectOpName(int id){ 750d8bc7086Sdrh char *z; 751d8bc7086Sdrh switch( id ){ 752d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 753d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 754d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 755d8bc7086Sdrh default: z = "UNION"; break; 756d8bc7086Sdrh } 757d8bc7086Sdrh return z; 758d8bc7086Sdrh } 759d8bc7086Sdrh 760d8bc7086Sdrh /* 761315555caSdrh ** Forward declaration 762315555caSdrh */ 763315555caSdrh static int fillInColumnList(Parse*, Select*); 764315555caSdrh 765315555caSdrh /* 76622f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes 76722f70c32Sdrh ** the result set of that SELECT. 76822f70c32Sdrh */ 7694adee20fSdanielk1977 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ 77022f70c32Sdrh Table *pTab; 771b733d037Sdrh int i, j; 77222f70c32Sdrh ExprList *pEList; 773b733d037Sdrh Column *aCol; 77422f70c32Sdrh 77522f70c32Sdrh if( fillInColumnList(pParse, pSelect) ){ 77622f70c32Sdrh return 0; 77722f70c32Sdrh } 77822f70c32Sdrh pTab = sqliteMalloc( sizeof(Table) ); 77922f70c32Sdrh if( pTab==0 ){ 78022f70c32Sdrh return 0; 78122f70c32Sdrh } 78222f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 78322f70c32Sdrh pEList = pSelect->pEList; 78422f70c32Sdrh pTab->nCol = pEList->nExpr; 785417be79cSdrh assert( pTab->nCol>0 ); 786b733d037Sdrh pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 78722f70c32Sdrh for(i=0; i<pTab->nCol; i++){ 788b733d037Sdrh Expr *p, *pR; 78922f70c32Sdrh if( pEList->a[i].zName ){ 790b733d037Sdrh aCol[i].zName = sqliteStrDup(pEList->a[i].zName); 791b733d037Sdrh }else if( (p=pEList->a[i].pExpr)->op==TK_DOT 792b733d037Sdrh && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ 793b733d037Sdrh int cnt; 7944adee20fSdanielk1977 sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0); 795b733d037Sdrh for(j=cnt=0; j<i; j++){ 7964adee20fSdanielk1977 if( sqlite3StrICmp(aCol[j].zName, aCol[i].zName)==0 ){ 797b733d037Sdrh int n; 798b733d037Sdrh char zBuf[30]; 799b733d037Sdrh sprintf(zBuf,"_%d",++cnt); 800b733d037Sdrh n = strlen(zBuf); 8014adee20fSdanielk1977 sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf,n,0); 802b733d037Sdrh j = -1; 803b733d037Sdrh } 804b733d037Sdrh } 805b733d037Sdrh }else if( p->span.z && p->span.z[0] ){ 8064adee20fSdanielk1977 sqlite3SetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); 80722f70c32Sdrh }else{ 80822f70c32Sdrh char zBuf[30]; 80922f70c32Sdrh sprintf(zBuf, "column%d", i+1); 81022f70c32Sdrh pTab->aCol[i].zName = sqliteStrDup(zBuf); 81122f70c32Sdrh } 812e014a838Sdanielk1977 813e014a838Sdanielk1977 /* Affinity is always NONE, as there is no type name. */ 814e014a838Sdanielk1977 pTab->aCol[i].affinity = SQLITE_AFF_NONE; 81522f70c32Sdrh } 81622f70c32Sdrh pTab->iPKey = -1; 81722f70c32Sdrh return pTab; 81822f70c32Sdrh } 81922f70c32Sdrh 82022f70c32Sdrh /* 821ad2d8307Sdrh ** For the given SELECT statement, do three things. 822d8bc7086Sdrh ** 823ad3cab52Sdrh ** (1) Fill in the pTabList->a[].pTab fields in the SrcList that 82463eb5f29Sdrh ** defines the set of tables that should be scanned. For views, 82563eb5f29Sdrh ** fill pTabList->a[].pSelect with a copy of the SELECT statement 82663eb5f29Sdrh ** that implements the view. A copy is made of the view's SELECT 82763eb5f29Sdrh ** statement so that we can freely modify or delete that statement 82863eb5f29Sdrh ** without worrying about messing up the presistent representation 82963eb5f29Sdrh ** of the view. 830d8bc7086Sdrh ** 831ad2d8307Sdrh ** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword 832ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 833ad2d8307Sdrh ** 834ad2d8307Sdrh ** (3) Scan the list of columns in the result set (pEList) looking 83554473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 83654473229Sdrh ** If found, expand each "*" to be every column in every table 83754473229Sdrh ** and TABLE.* to be every column in TABLE. 838d8bc7086Sdrh ** 839d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 840d8bc7086Sdrh ** in pParse and return non-zero. 841d8bc7086Sdrh */ 842d8bc7086Sdrh static int fillInColumnList(Parse *pParse, Select *p){ 84354473229Sdrh int i, j, k, rc; 844ad3cab52Sdrh SrcList *pTabList; 845daffd0e5Sdrh ExprList *pEList; 846a76b5dfcSdrh Table *pTab; 847daffd0e5Sdrh 848daffd0e5Sdrh if( p==0 || p->pSrc==0 ) return 1; 849daffd0e5Sdrh pTabList = p->pSrc; 850daffd0e5Sdrh pEList = p->pEList; 851d8bc7086Sdrh 852d8bc7086Sdrh /* Look up every table in the table list. 853d8bc7086Sdrh */ 854ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 855d8bc7086Sdrh if( pTabList->a[i].pTab ){ 856d8bc7086Sdrh /* This routine has run before! No need to continue */ 857d8bc7086Sdrh return 0; 858d8bc7086Sdrh } 859daffd0e5Sdrh if( pTabList->a[i].zName==0 ){ 86022f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 86122f70c32Sdrh assert( pTabList->a[i].pSelect!=0 ); 862ad2d8307Sdrh if( pTabList->a[i].zAlias==0 ){ 863ad2d8307Sdrh char zFakeName[60]; 864ad2d8307Sdrh sprintf(zFakeName, "sqlite_subquery_%p_", 865ad2d8307Sdrh (void*)pTabList->a[i].pSelect); 8664adee20fSdanielk1977 sqlite3SetString(&pTabList->a[i].zAlias, zFakeName, 0); 867ad2d8307Sdrh } 86822f70c32Sdrh pTabList->a[i].pTab = pTab = 8694adee20fSdanielk1977 sqlite3ResultSetOfSelect(pParse, pTabList->a[i].zAlias, 87022f70c32Sdrh pTabList->a[i].pSelect); 87122f70c32Sdrh if( pTab==0 ){ 872daffd0e5Sdrh return 1; 873daffd0e5Sdrh } 8745cf590c1Sdrh /* The isTransient flag indicates that the Table structure has been 8755cf590c1Sdrh ** dynamically allocated and may be freed at any time. In other words, 8765cf590c1Sdrh ** pTab is not pointing to a persistent table structure that defines 8775cf590c1Sdrh ** part of the schema. */ 87822f70c32Sdrh pTab->isTransient = 1; 87922f70c32Sdrh }else{ 880a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 881a76b5dfcSdrh pTabList->a[i].pTab = pTab = 8824adee20fSdanielk1977 sqlite3LocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase); 883a76b5dfcSdrh if( pTab==0 ){ 884d8bc7086Sdrh return 1; 885d8bc7086Sdrh } 886a76b5dfcSdrh if( pTab->pSelect ){ 88763eb5f29Sdrh /* We reach here if the named table is a really a view */ 8884adee20fSdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 889417be79cSdrh return 1; 890417be79cSdrh } 89163eb5f29Sdrh /* If pTabList->a[i].pSelect!=0 it means we are dealing with a 89263eb5f29Sdrh ** view within a view. The SELECT structure has already been 89363eb5f29Sdrh ** copied by the outer view so we can skip the copy step here 89463eb5f29Sdrh ** in the inner view. 89563eb5f29Sdrh */ 89663eb5f29Sdrh if( pTabList->a[i].pSelect==0 ){ 8974adee20fSdanielk1977 pTabList->a[i].pSelect = sqlite3SelectDup(pTab->pSelect); 898a76b5dfcSdrh } 899d8bc7086Sdrh } 90022f70c32Sdrh } 90163eb5f29Sdrh } 902d8bc7086Sdrh 903ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 904ad2d8307Sdrh */ 905ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 906ad2d8307Sdrh 9077c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 90854473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 90954473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 9107c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 9117c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 9127c917d19Sdrh ** each one to the list of all columns in all tables. 91354473229Sdrh ** 91454473229Sdrh ** The first loop just checks to see if there are any "*" operators 91554473229Sdrh ** that need expanding. 916d8bc7086Sdrh */ 9177c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 91854473229Sdrh Expr *pE = pEList->a[k].pExpr; 91954473229Sdrh if( pE->op==TK_ALL ) break; 92054473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 92154473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 9227c917d19Sdrh } 92354473229Sdrh rc = 0; 9247c917d19Sdrh if( k<pEList->nExpr ){ 92554473229Sdrh /* 92654473229Sdrh ** If we get here it means the result set contains one or more "*" 92754473229Sdrh ** operators that need to be expanded. Loop through each expression 92854473229Sdrh ** in the result set and expand them one by one. 92954473229Sdrh */ 9307c917d19Sdrh struct ExprList_item *a = pEList->a; 9317c917d19Sdrh ExprList *pNew = 0; 9327c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 93354473229Sdrh Expr *pE = a[k].pExpr; 93454473229Sdrh if( pE->op!=TK_ALL && 93554473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 93654473229Sdrh /* This particular expression does not need to be expanded. 93754473229Sdrh */ 9384adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 9397c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 9407c917d19Sdrh a[k].pExpr = 0; 9417c917d19Sdrh a[k].zName = 0; 9427c917d19Sdrh }else{ 94354473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 94454473229Sdrh ** expanded. */ 94554473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 94654473229Sdrh Token *pName; /* text of name of TABLE */ 94754473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 94854473229Sdrh pName = &pE->pLeft->token; 94954473229Sdrh }else{ 95054473229Sdrh pName = 0; 95154473229Sdrh } 952ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 953d8bc7086Sdrh Table *pTab = pTabList->a[i].pTab; 95454473229Sdrh char *zTabName = pTabList->a[i].zAlias; 95554473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 95654473229Sdrh zTabName = pTab->zName; 95754473229Sdrh } 95854473229Sdrh if( pName && (zTabName==0 || zTabName[0]==0 || 9594adee20fSdanielk1977 sqlite3StrNICmp(pName->z, zTabName, pName->n)!=0 || 960c754fa54Sdrh zTabName[pName->n]!=0) ){ 96154473229Sdrh continue; 96254473229Sdrh } 96354473229Sdrh tableSeen = 1; 964d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 96522f70c32Sdrh Expr *pExpr, *pLeft, *pRight; 966ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 967ad2d8307Sdrh 968ad2d8307Sdrh if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 && 969ad2d8307Sdrh columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){ 970ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 971ad2d8307Sdrh ** table on the right */ 972ad2d8307Sdrh continue; 973ad2d8307Sdrh } 9744adee20fSdanielk1977 if( i>0 && sqlite3IdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){ 975ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 976ad2d8307Sdrh ** using clause from the table on the right. */ 977ad2d8307Sdrh continue; 978ad2d8307Sdrh } 9794adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 98022f70c32Sdrh if( pRight==0 ) break; 981ad2d8307Sdrh pRight->token.z = zName; 982ad2d8307Sdrh pRight->token.n = strlen(zName); 9834b59ab5eSdrh pRight->token.dyn = 0; 9844b59ab5eSdrh if( zTabName && pTabList->nSrc>1 ){ 9854adee20fSdanielk1977 pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 9864adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 98722f70c32Sdrh if( pExpr==0 ) break; 9884b59ab5eSdrh pLeft->token.z = zTabName; 9894b59ab5eSdrh pLeft->token.n = strlen(zTabName); 9904b59ab5eSdrh pLeft->token.dyn = 0; 9914adee20fSdanielk1977 sqlite3SetString((char**)&pExpr->span.z, zTabName, ".", zName, 0); 9926977fea8Sdrh pExpr->span.n = strlen(pExpr->span.z); 9936977fea8Sdrh pExpr->span.dyn = 1; 9946977fea8Sdrh pExpr->token.z = 0; 9956977fea8Sdrh pExpr->token.n = 0; 9966977fea8Sdrh pExpr->token.dyn = 0; 99722f70c32Sdrh }else{ 99822f70c32Sdrh pExpr = pRight; 9996977fea8Sdrh pExpr->span = pExpr->token; 100022f70c32Sdrh } 10014adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, pExpr, 0); 1002d8bc7086Sdrh } 1003d8bc7086Sdrh } 100454473229Sdrh if( !tableSeen ){ 1005f5db2d3eSdrh if( pName ){ 10064adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such table: %T", pName); 1007f5db2d3eSdrh }else{ 10084adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1009f5db2d3eSdrh } 101054473229Sdrh rc = 1; 101154473229Sdrh } 10127c917d19Sdrh } 10137c917d19Sdrh } 10144adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 10157c917d19Sdrh p->pEList = pNew; 1016d8bc7086Sdrh } 101754473229Sdrh return rc; 1018d8bc7086Sdrh } 1019d8bc7086Sdrh 1020d8bc7086Sdrh /* 1021ff78bd2fSdrh ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers 1022ff78bd2fSdrh ** in a select structure. It just sets the pointers to NULL. This 1023ff78bd2fSdrh ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect 1024ff78bd2fSdrh ** pointer is not NULL, this routine is called recursively on that pointer. 1025ff78bd2fSdrh ** 1026ff78bd2fSdrh ** This routine is called on the Select structure that defines a 1027ff78bd2fSdrh ** VIEW in order to undo any bindings to tables. This is necessary 1028ff78bd2fSdrh ** because those tables might be DROPed by a subsequent SQL command. 10295cf590c1Sdrh ** If the bindings are not removed, then the Select.pSrc->a[].pTab field 10305cf590c1Sdrh ** will be left pointing to a deallocated Table structure after the 10315cf590c1Sdrh ** DROP and a coredump will occur the next time the VIEW is used. 1032ff78bd2fSdrh */ 10334adee20fSdanielk1977 void sqlite3SelectUnbind(Select *p){ 1034ff78bd2fSdrh int i; 1035ad3cab52Sdrh SrcList *pSrc = p->pSrc; 1036ff78bd2fSdrh Table *pTab; 1037ff78bd2fSdrh if( p==0 ) return; 1038ad3cab52Sdrh for(i=0; i<pSrc->nSrc; i++){ 1039ff78bd2fSdrh if( (pTab = pSrc->a[i].pTab)!=0 ){ 1040ff78bd2fSdrh if( pTab->isTransient ){ 10414adee20fSdanielk1977 sqlite3DeleteTable(0, pTab); 1042ff78bd2fSdrh } 1043ff78bd2fSdrh pSrc->a[i].pTab = 0; 1044ff78bd2fSdrh if( pSrc->a[i].pSelect ){ 10454adee20fSdanielk1977 sqlite3SelectUnbind(pSrc->a[i].pSelect); 1046ff78bd2fSdrh } 1047ff78bd2fSdrh } 1048ff78bd2fSdrh } 1049ff78bd2fSdrh } 1050ff78bd2fSdrh 1051ff78bd2fSdrh /* 1052d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1053d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1054967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1055d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1056d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1057d8bc7086Sdrh ** 1058d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1059d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1060d8bc7086Sdrh ** 1061d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1062d8bc7086Sdrh ** of errors is returned. 1063d8bc7086Sdrh */ 1064d8bc7086Sdrh static int matchOrderbyToColumn( 1065d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1066d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1067d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1068e4de1febSdrh int iTable, /* Insert this value in iTable */ 1069d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1070d8bc7086Sdrh ){ 1071d8bc7086Sdrh int nErr = 0; 1072d8bc7086Sdrh int i, j; 1073d8bc7086Sdrh ExprList *pEList; 1074d8bc7086Sdrh 1075daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1076d8bc7086Sdrh if( mustComplete ){ 1077d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1078d8bc7086Sdrh } 1079d8bc7086Sdrh if( fillInColumnList(pParse, pSelect) ){ 1080d8bc7086Sdrh return 1; 1081d8bc7086Sdrh } 1082d8bc7086Sdrh if( pSelect->pPrior ){ 108392cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 108492cd52f5Sdrh return 1; 108592cd52f5Sdrh } 1086d8bc7086Sdrh } 1087d8bc7086Sdrh pEList = pSelect->pEList; 1088d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1089d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1090e4de1febSdrh int iCol = -1; 1091d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 10924adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1093e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 10944adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1095da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1096e4de1febSdrh iCol, pEList->nExpr); 1097e4de1febSdrh nErr++; 1098e4de1febSdrh break; 1099e4de1febSdrh } 1100fcb78a49Sdrh if( !mustComplete ) continue; 1101e4de1febSdrh iCol--; 1102e4de1febSdrh } 1103e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 11044cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1105a76b5dfcSdrh char *zName, *zLabel; 1106a76b5dfcSdrh zName = pEList->a[j].zName; 1107a76b5dfcSdrh assert( pE->token.z ); 1108a76b5dfcSdrh zLabel = sqliteStrNDup(pE->token.z, pE->token.n); 11094adee20fSdanielk1977 sqlite3Dequote(zLabel); 11104adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1111e4de1febSdrh iCol = j; 1112d8bc7086Sdrh } 11136e142f54Sdrh sqliteFree(zLabel); 1114d8bc7086Sdrh } 11154adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1116e4de1febSdrh iCol = j; 1117d8bc7086Sdrh } 1118e4de1febSdrh } 1119e4de1febSdrh if( iCol>=0 ){ 1120967e8b73Sdrh pE->op = TK_COLUMN; 1121e4de1febSdrh pE->iColumn = iCol; 1122d8bc7086Sdrh pE->iTable = iTable; 1123d8bc7086Sdrh pOrderBy->a[i].done = 1; 1124d8bc7086Sdrh } 1125e4de1febSdrh if( iCol<0 && mustComplete ){ 11264adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1127da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1128d8bc7086Sdrh nErr++; 1129d8bc7086Sdrh break; 1130d8bc7086Sdrh } 1131d8bc7086Sdrh } 1132d8bc7086Sdrh return nErr; 1133d8bc7086Sdrh } 1134d8bc7086Sdrh 1135d8bc7086Sdrh /* 1136d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1137d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1138d8bc7086Sdrh */ 11394adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1140d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1141d8bc7086Sdrh if( v==0 ){ 11424adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1143d8bc7086Sdrh } 1144d8bc7086Sdrh return v; 1145d8bc7086Sdrh } 1146d8bc7086Sdrh 1147d3d39e93Sdrh #if 0 /***** This routine needs deleting *****/ 114884ac9d02Sdanielk1977 static void multiSelectAffinity(Select *p, char *zAff){ 114984ac9d02Sdanielk1977 int i; 115084ac9d02Sdanielk1977 115184ac9d02Sdanielk1977 if( !p ) return; 115284ac9d02Sdanielk1977 multiSelectAffinity(p->pPrior, zAff); 115384ac9d02Sdanielk1977 115484ac9d02Sdanielk1977 for(i=0; i<p->pEList->nExpr; i++){ 115584ac9d02Sdanielk1977 if( zAff[i]=='\0' ){ 115684ac9d02Sdanielk1977 zAff[i] = sqlite3ExprAffinity(p->pEList->a[i].pExpr); 115784ac9d02Sdanielk1977 } 115884ac9d02Sdanielk1977 } 115984ac9d02Sdanielk1977 } 1160d3d39e93Sdrh #endif 116184ac9d02Sdanielk1977 1162d8bc7086Sdrh /* 11637b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 11647b58daeaSdrh ** nLimit and nOffset fields. nLimit and nOffset hold the integers 11657b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 11667b58daeaSdrh ** keywords. Or that hold -1 and 0 if those keywords are omitted. 11677b58daeaSdrh ** iLimit and iOffset are the integer memory register numbers for 11687b58daeaSdrh ** counters used to compute the limit and offset. If there is no 11697b58daeaSdrh ** limit and/or offset, then iLimit and iOffset are negative. 11707b58daeaSdrh ** 11717b58daeaSdrh ** This routine changes the values if iLimit and iOffset only if 11727b58daeaSdrh ** a limit or offset is defined by nLimit and nOffset. iLimit and 11737b58daeaSdrh ** iOffset should have been preset to appropriate default values 11747b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 11757b58daeaSdrh ** Only if nLimit>=0 or nOffset>0 do the limit registers get 11767b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 11777b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 11787b58daeaSdrh ** SELECT statements. 11797b58daeaSdrh */ 11807b58daeaSdrh static void computeLimitRegisters(Parse *pParse, Select *p){ 11817b58daeaSdrh /* 11827b58daeaSdrh ** If the comparison is p->nLimit>0 then "LIMIT 0" shows 11837b58daeaSdrh ** all rows. It is the same as no limit. If the comparision is 11847b58daeaSdrh ** p->nLimit>=0 then "LIMIT 0" show no rows at all. 11857b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 11867b58daeaSdrh ** contraversy about what the correct behavior should be. 11877b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 11887b58daeaSdrh ** no rows. 11897b58daeaSdrh */ 11907b58daeaSdrh if( p->nLimit>=0 ){ 11917b58daeaSdrh int iMem = pParse->nMem++; 11924adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 11937b58daeaSdrh if( v==0 ) return; 11944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -p->nLimit, 0); 11954adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 11967b58daeaSdrh p->iLimit = iMem; 11977b58daeaSdrh } 11987b58daeaSdrh if( p->nOffset>0 ){ 11997b58daeaSdrh int iMem = pParse->nMem++; 12004adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 12017b58daeaSdrh if( v==0 ) return; 12024adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -p->nOffset, 0); 12034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 12047b58daeaSdrh p->iOffset = iMem; 12057b58daeaSdrh } 12067b58daeaSdrh } 12077b58daeaSdrh 12087b58daeaSdrh /* 1209d3d39e93Sdrh ** Generate VDBE instructions that will open a transient table that 1210d3d39e93Sdrh ** will be used for an index or to store keyed results for a compound 1211d3d39e93Sdrh ** select. In other words, open a transient table that needs a 1212d3d39e93Sdrh ** KeyInfo structure. The number of columns in the KeyInfo is determined 1213d3d39e93Sdrh ** by the result set of the SELECT statement in the second argument. 1214d3d39e93Sdrh ** 1215d3d39e93Sdrh ** Make the new table a KeyAsData table if keyAsData is true. 1216d3d39e93Sdrh */ 1217d3d39e93Sdrh static void openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){ 1218d3d39e93Sdrh KeyInfo *pKeyInfo; 1219736c22b8Sdrh int nColumn; 1220d3d39e93Sdrh sqlite *db = pParse->db; 1221d3d39e93Sdrh int i; 1222d3d39e93Sdrh Vdbe *v = pParse->pVdbe; 1223d3d39e93Sdrh 1224736c22b8Sdrh if( fillInColumnList(pParse, p) ){ 1225736c22b8Sdrh return; 1226736c22b8Sdrh } 1227736c22b8Sdrh nColumn = p->pEList->nExpr; 1228d3d39e93Sdrh pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) ); 1229d3d39e93Sdrh if( pKeyInfo==0 ) return; 1230d3d39e93Sdrh pKeyInfo->nField = nColumn; 1231d3d39e93Sdrh for(i=0; i<nColumn; i++){ 1232d3d39e93Sdrh pKeyInfo->aColl[i] = db->pDfltColl; 1233d3d39e93Sdrh } 1234ffbc3088Sdrh sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 1235d3d39e93Sdrh if( keyAsData ){ 1236d3d39e93Sdrh sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1); 1237d3d39e93Sdrh } 1238d3d39e93Sdrh } 1239d3d39e93Sdrh 1240d3d39e93Sdrh /* 124182c3d636Sdrh ** This routine is called to process a query that is really the union 124282c3d636Sdrh ** or intersection of two or more separate queries. 1243c926afbcSdrh ** 1244e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1245e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1246e78e8284Sdrh ** in which case this routine will be called recursively. 1247e78e8284Sdrh ** 1248e78e8284Sdrh ** The results of the total query are to be written into a destination 1249e78e8284Sdrh ** of type eDest with parameter iParm. 1250e78e8284Sdrh ** 1251e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1252e78e8284Sdrh ** 1253e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1254e78e8284Sdrh ** 1255e78e8284Sdrh ** This statement is parsed up as follows: 1256e78e8284Sdrh ** 1257e78e8284Sdrh ** SELECT c FROM t3 1258e78e8284Sdrh ** | 1259e78e8284Sdrh ** `-----> SELECT b FROM t2 1260e78e8284Sdrh ** | 12614b11c6d3Sjplyon ** `------> SELECT a FROM t1 1262e78e8284Sdrh ** 1263e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1264e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1265e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1266e78e8284Sdrh ** 1267e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1268e78e8284Sdrh ** individual selects always group from left to right. 126982c3d636Sdrh */ 127084ac9d02Sdanielk1977 static int multiSelect( 127184ac9d02Sdanielk1977 Parse *pParse, 127284ac9d02Sdanielk1977 Select *p, 127384ac9d02Sdanielk1977 int eDest, 127484ac9d02Sdanielk1977 int iParm, 127584ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 127684ac9d02Sdanielk1977 ){ 127784ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 127810e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 127910e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 12806590493dSdanielk1977 #if 0 /* NOT USED */ 128184ac9d02Sdanielk1977 char *affStr = 0; 128284ac9d02Sdanielk1977 128384ac9d02Sdanielk1977 if( !aff ){ 128484ac9d02Sdanielk1977 int len; 128584ac9d02Sdanielk1977 rc = fillInColumnList(pParse, p); 128684ac9d02Sdanielk1977 if( rc!=SQLITE_OK ){ 128784ac9d02Sdanielk1977 goto multi_select_end; 128884ac9d02Sdanielk1977 } 128984ac9d02Sdanielk1977 len = p->pEList->nExpr+1; 129084ac9d02Sdanielk1977 affStr = (char *)sqliteMalloc(p->pEList->nExpr+1); 129184ac9d02Sdanielk1977 if( !affStr ){ 129284ac9d02Sdanielk1977 rc = SQLITE_NOMEM; 129384ac9d02Sdanielk1977 goto multi_select_end; 129484ac9d02Sdanielk1977 } 129584ac9d02Sdanielk1977 memset(affStr, (int)SQLITE_AFF_NUMERIC, len-1); 129684ac9d02Sdanielk1977 aff = affStr; 129784ac9d02Sdanielk1977 } 1298d3d39e93Sdrh #endif 129982c3d636Sdrh 13007b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 13017b58daeaSdrh ** the last SELECT in the series may have an ORDER BY or LIMIT. 130282c3d636Sdrh */ 130384ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 130484ac9d02Sdanielk1977 rc = 1; 130584ac9d02Sdanielk1977 goto multi_select_end; 130684ac9d02Sdanielk1977 } 1307d8bc7086Sdrh pPrior = p->pPrior; 1308d8bc7086Sdrh if( pPrior->pOrderBy ){ 13094adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1310da93d238Sdrh selectOpName(p->op)); 131184ac9d02Sdanielk1977 rc = 1; 131284ac9d02Sdanielk1977 goto multi_select_end; 131382c3d636Sdrh } 13147b58daeaSdrh if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){ 13154adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 13167b58daeaSdrh selectOpName(p->op)); 131784ac9d02Sdanielk1977 rc = 1; 131884ac9d02Sdanielk1977 goto multi_select_end; 13197b58daeaSdrh } 132082c3d636Sdrh 1321d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1322d8bc7086Sdrh */ 13234adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 132484ac9d02Sdanielk1977 if( v==0 ){ 132584ac9d02Sdanielk1977 rc = 1; 132684ac9d02Sdanielk1977 goto multi_select_end; 132784ac9d02Sdanielk1977 } 1328d8bc7086Sdrh 13291cc3d75fSdrh /* Create the destination temporary table if necessary 13301cc3d75fSdrh */ 13311cc3d75fSdrh if( eDest==SRT_TempTable ){ 1332b4964b72Sdanielk1977 assert( p->pEList ); 13334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 1334b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, p->pEList->nExpr); 13351cc3d75fSdrh eDest = SRT_Table; 13361cc3d75fSdrh } 13371cc3d75fSdrh 1338f46f905aSdrh /* Generate code for the left and right SELECT statements. 1339d8bc7086Sdrh */ 134082c3d636Sdrh switch( p->op ){ 1341f46f905aSdrh case TK_ALL: { 1342f46f905aSdrh if( p->pOrderBy==0 ){ 13437b58daeaSdrh pPrior->nLimit = p->nLimit; 13447b58daeaSdrh pPrior->nOffset = p->nOffset; 134584ac9d02Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 134684ac9d02Sdanielk1977 if( rc ){ 134784ac9d02Sdanielk1977 goto multi_select_end; 134884ac9d02Sdanielk1977 } 1349f46f905aSdrh p->pPrior = 0; 13507b58daeaSdrh p->iLimit = pPrior->iLimit; 13517b58daeaSdrh p->iOffset = pPrior->iOffset; 13527b58daeaSdrh p->nLimit = -1; 13537b58daeaSdrh p->nOffset = 0; 135484ac9d02Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1355f46f905aSdrh p->pPrior = pPrior; 135684ac9d02Sdanielk1977 if( rc ){ 135784ac9d02Sdanielk1977 goto multi_select_end; 135884ac9d02Sdanielk1977 } 1359f46f905aSdrh break; 1360f46f905aSdrh } 1361f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1362f46f905aSdrh } 136382c3d636Sdrh case TK_EXCEPT: 136482c3d636Sdrh case TK_UNION: { 1365d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1366d8bc7086Sdrh int op; /* One of the SRT_ operations to apply to self */ 1367d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 13687b58daeaSdrh int nLimit, nOffset; /* Saved values of p->nLimit and p->nOffset */ 1369c926afbcSdrh ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ 137082c3d636Sdrh 1371d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 13727b58daeaSdrh if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){ 1373d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1374c926afbcSdrh ** right. 1375d8bc7086Sdrh */ 137682c3d636Sdrh unionTab = iParm; 137782c3d636Sdrh }else{ 1378d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1379d8bc7086Sdrh ** intermediate results. 1380d8bc7086Sdrh */ 138182c3d636Sdrh unionTab = pParse->nTab++; 1382d8bc7086Sdrh if( p->pOrderBy 1383d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 138484ac9d02Sdanielk1977 rc = 1; 138584ac9d02Sdanielk1977 goto multi_select_end; 1386d8bc7086Sdrh } 1387d8bc7086Sdrh if( p->op!=TK_ALL ){ 1388d3d39e93Sdrh openTempIndex(pParse, p, unionTab, 1); 1389345fda3eSdrh }else{ 13904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0); 139182c3d636Sdrh } 139284ac9d02Sdanielk1977 assert( p->pEList ); 1393d8bc7086Sdrh } 1394d8bc7086Sdrh 1395d8bc7086Sdrh /* Code the SELECT statements to our left 1396d8bc7086Sdrh */ 139784ac9d02Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 139884ac9d02Sdanielk1977 if( rc ){ 139984ac9d02Sdanielk1977 goto multi_select_end; 140084ac9d02Sdanielk1977 } 140184ac9d02Sdanielk1977 if( p->op==TK_ALL ){ 140284ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, pPrior->pEList->nExpr); 140384ac9d02Sdanielk1977 } 1404d8bc7086Sdrh 1405d8bc7086Sdrh /* Code the current SELECT statement 1406d8bc7086Sdrh */ 1407d8bc7086Sdrh switch( p->op ){ 1408d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1409d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1410d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1411d8bc7086Sdrh } 141282c3d636Sdrh p->pPrior = 0; 1413c926afbcSdrh pOrderBy = p->pOrderBy; 1414c926afbcSdrh p->pOrderBy = 0; 14157b58daeaSdrh nLimit = p->nLimit; 14167b58daeaSdrh p->nLimit = -1; 14177b58daeaSdrh nOffset = p->nOffset; 14187b58daeaSdrh p->nOffset = 0; 141984ac9d02Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 142082c3d636Sdrh p->pPrior = pPrior; 1421c926afbcSdrh p->pOrderBy = pOrderBy; 14227b58daeaSdrh p->nLimit = nLimit; 14237b58daeaSdrh p->nOffset = nOffset; 142484ac9d02Sdanielk1977 if( rc ){ 142584ac9d02Sdanielk1977 goto multi_select_end; 142684ac9d02Sdanielk1977 } 142784ac9d02Sdanielk1977 1428d8bc7086Sdrh 1429d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1430d8bc7086Sdrh ** it is that we currently need. 1431d8bc7086Sdrh */ 1432c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 14336b56344dSdrh int iCont, iBreak, iStart; 143482c3d636Sdrh assert( p->pEList ); 143541202ccaSdrh if( eDest==SRT_Callback ){ 14366a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 143741202ccaSdrh } 14384adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 14394adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 14404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 14417b58daeaSdrh computeLimitRegisters(pParse, p); 14424adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 144338640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 1444d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 144584ac9d02Sdanielk1977 iCont, iBreak, 0); 144684ac9d02Sdanielk1977 if( rc ){ 144784ac9d02Sdanielk1977 rc = 1; 144884ac9d02Sdanielk1977 goto multi_select_end; 144984ac9d02Sdanielk1977 } 14504adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 14514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 14524adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 14534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 1454d8bc7086Sdrh if( p->pOrderBy ){ 1455ffbc3088Sdrh generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1456d8bc7086Sdrh } 145782c3d636Sdrh } 145882c3d636Sdrh break; 145982c3d636Sdrh } 146082c3d636Sdrh case TK_INTERSECT: { 146182c3d636Sdrh int tab1, tab2; 14626b56344dSdrh int iCont, iBreak, iStart; 14637b58daeaSdrh int nLimit, nOffset; 146482c3d636Sdrh 1465d8bc7086Sdrh /* INTERSECT is different from the others since it requires 14666206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1467d8bc7086Sdrh ** by allocating the tables we will need. 1468d8bc7086Sdrh */ 146982c3d636Sdrh tab1 = pParse->nTab++; 147082c3d636Sdrh tab2 = pParse->nTab++; 1471d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 147284ac9d02Sdanielk1977 rc = 1; 147384ac9d02Sdanielk1977 goto multi_select_end; 1474d8bc7086Sdrh } 1475d3d39e93Sdrh openTempIndex(pParse, p, tab1, 1); 147684ac9d02Sdanielk1977 assert( p->pEList ); 1477d8bc7086Sdrh 1478d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1479d8bc7086Sdrh */ 148084ac9d02Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 148184ac9d02Sdanielk1977 if( rc ){ 148284ac9d02Sdanielk1977 goto multi_select_end; 148384ac9d02Sdanielk1977 } 1484d8bc7086Sdrh 1485d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1486d8bc7086Sdrh */ 1487d3d39e93Sdrh openTempIndex(pParse, p, tab2, 1); 148882c3d636Sdrh p->pPrior = 0; 14897b58daeaSdrh nLimit = p->nLimit; 14907b58daeaSdrh p->nLimit = -1; 14917b58daeaSdrh nOffset = p->nOffset; 14927b58daeaSdrh p->nOffset = 0; 149384ac9d02Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 149482c3d636Sdrh p->pPrior = pPrior; 14957b58daeaSdrh p->nLimit = nLimit; 14967b58daeaSdrh p->nOffset = nOffset; 149784ac9d02Sdanielk1977 if( rc ){ 149884ac9d02Sdanielk1977 goto multi_select_end; 149984ac9d02Sdanielk1977 } 1500d8bc7086Sdrh 1501d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1502d8bc7086Sdrh ** tables. 1503d8bc7086Sdrh */ 150482c3d636Sdrh assert( p->pEList ); 150541202ccaSdrh if( eDest==SRT_Callback ){ 15066a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 150741202ccaSdrh } 15084adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 15094adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 15104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 15117b58daeaSdrh computeLimitRegisters(pParse, p); 15124adee20fSdanielk1977 iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0); 15134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 151438640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 1515d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 151684ac9d02Sdanielk1977 iCont, iBreak, 0); 151784ac9d02Sdanielk1977 if( rc ){ 151884ac9d02Sdanielk1977 rc = 1; 151984ac9d02Sdanielk1977 goto multi_select_end; 152084ac9d02Sdanielk1977 } 15214adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 15224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 15234adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 15244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 15254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 1526d8bc7086Sdrh if( p->pOrderBy ){ 1527ffbc3088Sdrh generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1528d8bc7086Sdrh } 152982c3d636Sdrh break; 153082c3d636Sdrh } 153182c3d636Sdrh } 153282c3d636Sdrh assert( p->pEList && pPrior->pEList ); 153382c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 15344adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1535da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 153684ac9d02Sdanielk1977 rc = 1; 153784ac9d02Sdanielk1977 goto multi_select_end; 15382282792aSdrh } 153984ac9d02Sdanielk1977 154084ac9d02Sdanielk1977 multi_select_end: 1541d3d39e93Sdrh #if 0 /*** NOT USED ****/ 154284ac9d02Sdanielk1977 if( affStr ){ 154384ac9d02Sdanielk1977 if( rc!=SQLITE_OK ){ 154484ac9d02Sdanielk1977 sqliteFree(affStr); 154584ac9d02Sdanielk1977 }else{ 154684ac9d02Sdanielk1977 multiSelectAffinity(p, affStr); 154784ac9d02Sdanielk1977 sqlite3VdbeOp3(v, OP_Noop, 0, 0, affStr, P3_DYNAMIC); 154884ac9d02Sdanielk1977 } 154984ac9d02Sdanielk1977 } 1550d3d39e93Sdrh #endif 155184ac9d02Sdanielk1977 return rc; 15522282792aSdrh } 15532282792aSdrh 15542282792aSdrh /* 1555832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 15566a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 155784e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 15586a3ea0e6Sdrh ** unchanged.) 1559832508b7Sdrh ** 1560832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1561832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1562832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1563832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1564832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1565832508b7Sdrh ** of the subquery rather the result set of the subquery. 1566832508b7Sdrh */ 15676a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 15686a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1569832508b7Sdrh if( pExpr==0 ) return; 157050350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 157150350a15Sdrh if( pExpr->iColumn<0 ){ 157250350a15Sdrh pExpr->op = TK_NULL; 157350350a15Sdrh }else{ 1574832508b7Sdrh Expr *pNew; 157584e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 1576832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 1577832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 1578832508b7Sdrh assert( pNew!=0 ); 1579832508b7Sdrh pExpr->op = pNew->op; 1580d94a6698Sdrh assert( pExpr->pLeft==0 ); 15814adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 1582d94a6698Sdrh assert( pExpr->pRight==0 ); 15834adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 1584d94a6698Sdrh assert( pExpr->pList==0 ); 15854adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 1586832508b7Sdrh pExpr->iTable = pNew->iTable; 1587832508b7Sdrh pExpr->iColumn = pNew->iColumn; 1588832508b7Sdrh pExpr->iAgg = pNew->iAgg; 15894adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 15904adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 159150350a15Sdrh } 1592832508b7Sdrh }else{ 15936a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 15946a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 15956a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 1596832508b7Sdrh } 1597832508b7Sdrh } 1598832508b7Sdrh static void 15996a3ea0e6Sdrh substExprList(ExprList *pList, int iTable, ExprList *pEList){ 1600832508b7Sdrh int i; 1601832508b7Sdrh if( pList==0 ) return; 1602832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 16036a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 1604832508b7Sdrh } 1605832508b7Sdrh } 1606832508b7Sdrh 1607832508b7Sdrh /* 16081350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 16091350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 16101350b030Sdrh ** occurs. 16111350b030Sdrh ** 16121350b030Sdrh ** To understand the concept of flattening, consider the following 16131350b030Sdrh ** query: 16141350b030Sdrh ** 16151350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 16161350b030Sdrh ** 16171350b030Sdrh ** The default way of implementing this query is to execute the 16181350b030Sdrh ** subquery first and store the results in a temporary table, then 16191350b030Sdrh ** run the outer query on that temporary table. This requires two 16201350b030Sdrh ** passes over the data. Furthermore, because the temporary table 16211350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 1622832508b7Sdrh ** optimized. 16231350b030Sdrh ** 1624832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 16251350b030Sdrh ** a single flat select, like this: 16261350b030Sdrh ** 16271350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 16281350b030Sdrh ** 16291350b030Sdrh ** The code generated for this simpification gives the same result 1630832508b7Sdrh ** but only has to scan the data once. And because indices might 1631832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 1632832508b7Sdrh ** avoided. 16331350b030Sdrh ** 1634832508b7Sdrh ** Flattening is only attempted if all of the following are true: 16351350b030Sdrh ** 1636832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 16371350b030Sdrh ** 1638832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 1639832508b7Sdrh ** 16408af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 16418af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 1642832508b7Sdrh ** 1643832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 1644832508b7Sdrh ** 1645832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 1646832508b7Sdrh ** aggregates. 1647832508b7Sdrh ** 1648832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 1649832508b7Sdrh ** DISTINCT. 1650832508b7Sdrh ** 165108192d5fSdrh ** (7) The subquery has a FROM clause. 165208192d5fSdrh ** 1653df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 1654df199a25Sdrh ** 1655df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 1656df199a25Sdrh ** aggregates. 1657df199a25Sdrh ** 1658df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 1659df199a25Sdrh ** use LIMIT. 1660df199a25Sdrh ** 1661174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 1662174b6195Sdrh ** 16633fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 16643fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 16653fc673e6Sdrh ** 1666832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 1667832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 1668832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 1669832508b7Sdrh ** 1670665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 1671832508b7Sdrh ** If flattening is attempted this routine returns 1. 1672832508b7Sdrh ** 1673832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 1674832508b7Sdrh ** the subquery before this routine runs. 16751350b030Sdrh */ 16768c74a8caSdrh static int flattenSubquery( 16778c74a8caSdrh Parse *pParse, /* The parsing context */ 16788c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 16798c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 16808c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 16818c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 16828c74a8caSdrh ){ 16830bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 1684ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 1685ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 16860bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 16876a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 1688832508b7Sdrh int i; 1689832508b7Sdrh Expr *pWhere; 16901350b030Sdrh 1691832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 1692832508b7Sdrh */ 1693832508b7Sdrh if( p==0 ) return 0; 1694832508b7Sdrh pSrc = p->pSrc; 1695ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 1696832508b7Sdrh pSub = pSrc->a[iFrom].pSelect; 1697832508b7Sdrh assert( pSub!=0 ); 1698832508b7Sdrh if( isAgg && subqueryIsAgg ) return 0; 1699ad3cab52Sdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; 1700832508b7Sdrh pSubSrc = pSub->pSrc; 1701832508b7Sdrh assert( pSubSrc ); 1702c31c2eb8Sdrh if( pSubSrc->nSrc==0 ) return 0; 1703df199a25Sdrh if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){ 1704df199a25Sdrh return 0; 1705df199a25Sdrh } 1706d11d382cSdrh if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0; 1707174b6195Sdrh if( p->pOrderBy && pSub->pOrderBy ) return 0; 1708832508b7Sdrh 17098af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 17108af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 17118af4d3acSdrh ** is not allowed: 17128af4d3acSdrh ** 17138af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 17148af4d3acSdrh ** 17158af4d3acSdrh ** If we flatten the above, we would get 17168af4d3acSdrh ** 17178af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 17188af4d3acSdrh ** 17198af4d3acSdrh ** which is not at all the same thing. 17208af4d3acSdrh */ 17218af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 17228af4d3acSdrh return 0; 17238af4d3acSdrh } 17248af4d3acSdrh 17253fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 17263fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 17273fc673e6Sdrh ** An examples of why this is not allowed: 17283fc673e6Sdrh ** 17293fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 17303fc673e6Sdrh ** 17313fc673e6Sdrh ** If we flatten the above, we would get 17323fc673e6Sdrh ** 17333fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 17343fc673e6Sdrh ** 17353fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 17363fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 17373fc673e6Sdrh */ 17383fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 17393fc673e6Sdrh && pSub->pWhere!=0 ){ 17403fc673e6Sdrh return 0; 17413fc673e6Sdrh } 17423fc673e6Sdrh 17430bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 174463eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 1745832508b7Sdrh */ 1746c31c2eb8Sdrh 1747c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 1748c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 1749c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 1750c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 1751c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 1752c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 1753c31c2eb8Sdrh ** elements we are now copying in. 1754c31c2eb8Sdrh */ 17556a3ea0e6Sdrh iParent = pSrc->a[iFrom].iCursor; 1756c31c2eb8Sdrh { 1757c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 17588af4d3acSdrh int jointype = pSrc->a[iFrom].jointype; 1759c31c2eb8Sdrh 1760c31c2eb8Sdrh if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ 17614adee20fSdanielk1977 sqlite3DeleteTable(0, pSrc->a[iFrom].pTab); 1762c31c2eb8Sdrh } 1763f26e09c8Sdrh sqliteFree(pSrc->a[iFrom].zDatabase); 1764c31c2eb8Sdrh sqliteFree(pSrc->a[iFrom].zName); 1765c31c2eb8Sdrh sqliteFree(pSrc->a[iFrom].zAlias); 1766c31c2eb8Sdrh if( nSubSrc>1 ){ 1767c31c2eb8Sdrh int extra = nSubSrc - 1; 1768c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 17694adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 1770c31c2eb8Sdrh } 1771c31c2eb8Sdrh p->pSrc = pSrc; 1772c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 1773c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 1774c31c2eb8Sdrh } 1775c31c2eb8Sdrh } 1776c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 1777c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 1778c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 1779c31c2eb8Sdrh } 17808af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 1781c31c2eb8Sdrh } 1782c31c2eb8Sdrh 1783c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 1784c31c2eb8Sdrh ** references to the iParent in the outer query. 1785c31c2eb8Sdrh ** 1786c31c2eb8Sdrh ** Example: 1787c31c2eb8Sdrh ** 1788c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 1789c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 1790c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 1791c31c2eb8Sdrh ** 1792c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 1793c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 1794c31c2eb8Sdrh */ 17956a3ea0e6Sdrh substExprList(p->pEList, iParent, pSub->pEList); 1796832508b7Sdrh pList = p->pEList; 1797832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 17986977fea8Sdrh Expr *pExpr; 17996977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 18006977fea8Sdrh pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); 1801832508b7Sdrh } 1802832508b7Sdrh } 18031b2e0329Sdrh if( isAgg ){ 18046a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 18056a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 18061b2e0329Sdrh } 1807174b6195Sdrh if( pSub->pOrderBy ){ 1808174b6195Sdrh assert( p->pOrderBy==0 ); 1809174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 1810174b6195Sdrh pSub->pOrderBy = 0; 1811174b6195Sdrh }else if( p->pOrderBy ){ 18126a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 1813174b6195Sdrh } 1814832508b7Sdrh if( pSub->pWhere ){ 18154adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 1816832508b7Sdrh }else{ 1817832508b7Sdrh pWhere = 0; 1818832508b7Sdrh } 1819832508b7Sdrh if( subqueryIsAgg ){ 1820832508b7Sdrh assert( p->pHaving==0 ); 18211b2e0329Sdrh p->pHaving = p->pWhere; 18221b2e0329Sdrh p->pWhere = pWhere; 18236a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 18241b2e0329Sdrh if( pSub->pHaving ){ 18254adee20fSdanielk1977 Expr *pHaving = sqlite3ExprDup(pSub->pHaving); 18261b2e0329Sdrh if( p->pHaving ){ 18274adee20fSdanielk1977 p->pHaving = sqlite3Expr(TK_AND, p->pHaving, pHaving, 0); 18281b2e0329Sdrh }else{ 18291b2e0329Sdrh p->pHaving = pHaving; 18301b2e0329Sdrh } 18311b2e0329Sdrh } 18321b2e0329Sdrh assert( p->pGroupBy==0 ); 18334adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 1834832508b7Sdrh }else if( p->pWhere==0 ){ 1835832508b7Sdrh p->pWhere = pWhere; 1836832508b7Sdrh }else{ 18376a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 1838832508b7Sdrh if( pWhere ){ 18394adee20fSdanielk1977 p->pWhere = sqlite3Expr(TK_AND, p->pWhere, pWhere, 0); 1840832508b7Sdrh } 1841832508b7Sdrh } 1842c31c2eb8Sdrh 1843c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 1844c31c2eb8Sdrh ** outer query is distinct. 1845c31c2eb8Sdrh */ 1846832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 18478c74a8caSdrh 1848c31c2eb8Sdrh /* Transfer the limit expression from the subquery to the outer 1849c31c2eb8Sdrh ** query. 1850c31c2eb8Sdrh */ 1851df199a25Sdrh if( pSub->nLimit>=0 ){ 1852df199a25Sdrh if( p->nLimit<0 ){ 1853df199a25Sdrh p->nLimit = pSub->nLimit; 1854df199a25Sdrh }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){ 1855df199a25Sdrh p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset; 1856df199a25Sdrh } 1857df199a25Sdrh } 1858df199a25Sdrh p->nOffset += pSub->nOffset; 18598c74a8caSdrh 1860c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 1861c31c2eb8Sdrh ** success. 1862c31c2eb8Sdrh */ 18634adee20fSdanielk1977 sqlite3SelectDelete(pSub); 1864832508b7Sdrh return 1; 18651350b030Sdrh } 18661350b030Sdrh 18671350b030Sdrh /* 18689562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 18699562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 18709562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 1871e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 18729562b551Sdrh ** simple min() or max() query, then return 0; 18739562b551Sdrh ** 18749562b551Sdrh ** A simply min() or max() query looks like this: 18759562b551Sdrh ** 18769562b551Sdrh ** SELECT min(a) FROM table; 18779562b551Sdrh ** SELECT max(a) FROM table; 18789562b551Sdrh ** 18799562b551Sdrh ** The query may have only a single table in its FROM argument. There 18809562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 18819562b551Sdrh ** be the min() or max() of a single column of the table. The column 18829562b551Sdrh ** in the min() or max() function must be indexed. 18839562b551Sdrh ** 18844adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 18859562b551Sdrh ** See the header comment on that routine for additional information. 18869562b551Sdrh */ 18879562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 18889562b551Sdrh Expr *pExpr; 18899562b551Sdrh int iCol; 18909562b551Sdrh Table *pTab; 18919562b551Sdrh Index *pIdx; 18929562b551Sdrh int base; 18939562b551Sdrh Vdbe *v; 18949562b551Sdrh int seekOp; 18959562b551Sdrh int cont; 18966e17529eSdrh ExprList *pEList, *pList, eList; 18979562b551Sdrh struct ExprList_item eListItem; 18986e17529eSdrh SrcList *pSrc; 18996e17529eSdrh 19009562b551Sdrh 19019562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 19029562b551Sdrh ** zero if it is not. 19039562b551Sdrh */ 19049562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 19056e17529eSdrh pSrc = p->pSrc; 19066e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 19076e17529eSdrh pEList = p->pEList; 19086e17529eSdrh if( pEList->nExpr!=1 ) return 0; 19096e17529eSdrh pExpr = pEList->a[0].pExpr; 19109562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 19116e17529eSdrh pList = pExpr->pList; 19126e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 19136977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 19144adee20fSdanielk1977 if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){ 19150bce8354Sdrh seekOp = OP_Rewind; 19164adee20fSdanielk1977 }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){ 19170bce8354Sdrh seekOp = OP_Last; 19180bce8354Sdrh }else{ 19190bce8354Sdrh return 0; 19200bce8354Sdrh } 19216e17529eSdrh pExpr = pList->a[0].pExpr; 19229562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 19239562b551Sdrh iCol = pExpr->iColumn; 19246e17529eSdrh pTab = pSrc->a[0].pTab; 19259562b551Sdrh 19269562b551Sdrh /* If we get to here, it means the query is of the correct form. 192717f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 192817f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 192917f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 193017f71934Sdrh ** usable index is found, return 0. 19319562b551Sdrh */ 19329562b551Sdrh if( iCol<0 ){ 19339562b551Sdrh pIdx = 0; 19349562b551Sdrh }else{ 19359562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 19369562b551Sdrh assert( pIdx->nColumn>=1 ); 19379562b551Sdrh if( pIdx->aiColumn[0]==iCol ) break; 19389562b551Sdrh } 19399562b551Sdrh if( pIdx==0 ) return 0; 19409562b551Sdrh } 19419562b551Sdrh 1942e5f50722Sdrh /* Identify column types if we will be using the callback. This 19439562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 1944e5f50722Sdrh ** The column names have already been generated in the calling function. 19459562b551Sdrh */ 19464adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 19479562b551Sdrh if( v==0 ) return 0; 19489562b551Sdrh 19490c37e630Sdrh /* If the output is destined for a temporary table, open that table. 19500c37e630Sdrh */ 19510c37e630Sdrh if( eDest==SRT_TempTable ){ 19524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 1953b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1); 19540c37e630Sdrh } 19550c37e630Sdrh 195617f71934Sdrh /* Generating code to find the min or the max. Basically all we have 195717f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 195817f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 195917f71934Sdrh ** or last entry in the main table. 19609562b551Sdrh */ 19614adee20fSdanielk1977 sqlite3CodeVerifySchema(pParse, pTab->iDb); 19626e17529eSdrh base = pSrc->a[0].iCursor; 19637b58daeaSdrh computeLimitRegisters(pParse, p); 19646e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 19654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 1966d3d39e93Sdrh sqlite3VdbeAddOp(v, OP_OpenRead, base, pTab->tnum); 1967b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol); 19686e17529eSdrh } 19694adee20fSdanielk1977 cont = sqlite3VdbeMakeLabel(v); 19709562b551Sdrh if( pIdx==0 ){ 19714adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 19729562b551Sdrh }else{ 19734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 1974d3d39e93Sdrh sqlite3VdbeOp3(v, OP_OpenRead, base+1, pIdx->tnum, 1975d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 19764adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base+1, 0); 19774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IdxRecno, base+1, 0); 19784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base+1, 0); 19797cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 19809562b551Sdrh } 19815cf8e8c7Sdrh eList.nExpr = 1; 19825cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 19835cf8e8c7Sdrh eList.a = &eListItem; 19845cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 198584ac9d02Sdanielk1977 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0); 19864adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, cont); 19874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 19886e17529eSdrh 19899562b551Sdrh return 1; 19909562b551Sdrh } 19919562b551Sdrh 19929562b551Sdrh /* 19939bb61fe7Sdrh ** Generate code for the given SELECT statement. 19949bb61fe7Sdrh ** 1995fef5208cSdrh ** The results are distributed in various ways depending on the 1996fef5208cSdrh ** value of eDest and iParm. 1997fef5208cSdrh ** 1998fef5208cSdrh ** eDest Value Result 1999fef5208cSdrh ** ------------ ------------------------------------------- 2000fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2001fef5208cSdrh ** 2002fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2003fef5208cSdrh ** 2004e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2005fef5208cSdrh ** 200682c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 200782c3d636Sdrh ** 20084b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2009c4a3c779Sdrh ** 2010c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 20119bb61fe7Sdrh ** 2012e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2013e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2014e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2015e78e8284Sdrh ** 20169bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 20179bb61fe7Sdrh ** encountered, then an appropriate error message is left in 20189bb61fe7Sdrh ** pParse->zErrMsg. 20199bb61fe7Sdrh ** 20209bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 20219bb61fe7Sdrh ** calling function needs to do that. 20221b2e0329Sdrh ** 20231b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 20241b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 20251b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 20261b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 20271b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 20281b2e0329Sdrh ** can be changed. 2029e78e8284Sdrh ** 2030e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2031e78e8284Sdrh ** 2032e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2033e78e8284Sdrh ** \ \_______ subquery _______/ / 2034e78e8284Sdrh ** \ / 2035e78e8284Sdrh ** \____________________ outer query ___________________/ 2036e78e8284Sdrh ** 2037e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2038e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2039e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2040e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2041e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2042e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 20439bb61fe7Sdrh */ 20444adee20fSdanielk1977 int sqlite3Select( 2045cce7d176Sdrh Parse *pParse, /* The parser context */ 20469bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2047e78e8284Sdrh int eDest, /* How to dispose of the results */ 2048e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2049832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2050832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 205184ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 205284ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2053cce7d176Sdrh ){ 2054d8bc7086Sdrh int i; 2055cce7d176Sdrh WhereInfo *pWInfo; 2056cce7d176Sdrh Vdbe *v; 2057cce7d176Sdrh int isAgg = 0; /* True for select lists like "count(*)" */ 2058a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2059ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 20609bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 20619bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 20622282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 20632282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 206419a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 206519a775c2Sdrh int distinct; /* Table to use for the distinct set */ 20661d83f052Sdrh int rc = 1; /* Value to return from this function */ 20679bb61fe7Sdrh 20686f8a503dSdanielk1977 if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1; 20694adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 2070daffd0e5Sdrh 207182c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 207282c3d636Sdrh */ 207382c3d636Sdrh if( p->pPrior ){ 207484ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 207582c3d636Sdrh } 207682c3d636Sdrh 207782c3d636Sdrh /* Make local copies of the parameters for this query. 207882c3d636Sdrh */ 20799bb61fe7Sdrh pTabList = p->pSrc; 20809bb61fe7Sdrh pWhere = p->pWhere; 20819bb61fe7Sdrh pOrderBy = p->pOrderBy; 20822282792aSdrh pGroupBy = p->pGroupBy; 20832282792aSdrh pHaving = p->pHaving; 208419a775c2Sdrh isDistinct = p->isDistinct; 20859bb61fe7Sdrh 20866a3ea0e6Sdrh /* Allocate VDBE cursors for each table in the FROM clause 208710e5e3cfSdrh */ 20884adee20fSdanielk1977 sqlite3SrcListAssignCursors(pParse, pTabList); 208910e5e3cfSdrh 20909bb61fe7Sdrh /* 20919bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 20929bb61fe7Sdrh ** errors before this routine starts. 20939bb61fe7Sdrh */ 20941d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2095cce7d176Sdrh 2096e78e8284Sdrh /* Expand any "*" terms in the result set. (For example the "*" in 2097e78e8284Sdrh ** "SELECT * FROM t1") The fillInColumnlist() routine also does some 2098e78e8284Sdrh ** other housekeeping - see the header comment for details. 2099cce7d176Sdrh */ 2100d8bc7086Sdrh if( fillInColumnList(pParse, p) ){ 21011d83f052Sdrh goto select_end; 2102cce7d176Sdrh } 2103ad2d8307Sdrh pWhere = p->pWhere; 2104d8bc7086Sdrh pEList = p->pEList; 21051d83f052Sdrh if( pEList==0 ) goto select_end; 2106cce7d176Sdrh 21072282792aSdrh /* If writing to memory or generating a set 21082282792aSdrh ** only a single column may be output. 210919a775c2Sdrh */ 2110fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 21114adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2112da93d238Sdrh "a SELECT that is part of an expression"); 21131d83f052Sdrh goto select_end; 211419a775c2Sdrh } 211519a775c2Sdrh 2116c926afbcSdrh /* ORDER BY is ignored for some destinations. 21172282792aSdrh */ 2118c926afbcSdrh switch( eDest ){ 2119c926afbcSdrh case SRT_Union: 2120c926afbcSdrh case SRT_Except: 2121c926afbcSdrh case SRT_Discard: 2122acd4c695Sdrh pOrderBy = 0; 2123c926afbcSdrh break; 2124c926afbcSdrh default: 2125c926afbcSdrh break; 21262282792aSdrh } 21272282792aSdrh 212810e5e3cfSdrh /* At this point, we should have allocated all the cursors that we 2129832508b7Sdrh ** need to handle subquerys and temporary tables. 213010e5e3cfSdrh ** 2131967e8b73Sdrh ** Resolve the column names and do a semantics check on all the expressions. 21322282792aSdrh */ 21334794b980Sdrh for(i=0; i<pEList->nExpr; i++){ 21344adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){ 21351d83f052Sdrh goto select_end; 2136cce7d176Sdrh } 21374adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ 21381d83f052Sdrh goto select_end; 2139cce7d176Sdrh } 2140cce7d176Sdrh } 2141cce7d176Sdrh if( pWhere ){ 21424adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pWhere) ){ 21431d83f052Sdrh goto select_end; 2144cce7d176Sdrh } 21454adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pWhere, 0, 0) ){ 21461d83f052Sdrh goto select_end; 2147cce7d176Sdrh } 2148cce7d176Sdrh } 2149c66c5a26Sdrh if( pHaving ){ 2150c66c5a26Sdrh if( pGroupBy==0 ){ 21514adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2152c66c5a26Sdrh goto select_end; 2153c66c5a26Sdrh } 21544adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pHaving) ){ 2155c66c5a26Sdrh goto select_end; 2156c66c5a26Sdrh } 21574adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pHaving, 1, &isAgg) ){ 2158c66c5a26Sdrh goto select_end; 2159c66c5a26Sdrh } 2160c66c5a26Sdrh } 2161cce7d176Sdrh if( pOrderBy ){ 2162cce7d176Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2163e4de1febSdrh int iCol; 216488eee38aSdrh Expr *pE = pOrderBy->a[i].pExpr; 21654adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){ 21664adee20fSdanielk1977 sqlite3ExprDelete(pE); 21674adee20fSdanielk1977 pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 216888eee38aSdrh } 21694adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pE) ){ 217088eee38aSdrh goto select_end; 217188eee38aSdrh } 21724adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE, isAgg, 0) ){ 217388eee38aSdrh goto select_end; 217488eee38aSdrh } 21754adee20fSdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 21764adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol)==0 ){ 21774adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2178da93d238Sdrh "ORDER BY terms must not be non-integer constants"); 21791d83f052Sdrh goto select_end; 2180e4de1febSdrh }else if( iCol<=0 || iCol>pEList->nExpr ){ 21814adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2182da93d238Sdrh "ORDER BY column number %d out of range - should be " 2183e4de1febSdrh "between 1 and %d", iCol, pEList->nExpr); 2184e4de1febSdrh goto select_end; 2185e4de1febSdrh } 2186cce7d176Sdrh } 2187cce7d176Sdrh } 2188cce7d176Sdrh } 21892282792aSdrh if( pGroupBy ){ 21902282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 219188eee38aSdrh int iCol; 21922282792aSdrh Expr *pE = pGroupBy->a[i].pExpr; 21934adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){ 21944adee20fSdanielk1977 sqlite3ExprDelete(pE); 21954adee20fSdanielk1977 pE = pGroupBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 21969208643dSdrh } 21974adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pE) ){ 21981d83f052Sdrh goto select_end; 21992282792aSdrh } 22004adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE, isAgg, 0) ){ 22011d83f052Sdrh goto select_end; 22022282792aSdrh } 22034adee20fSdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 22044adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol)==0 ){ 22054adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2206da93d238Sdrh "GROUP BY terms must not be non-integer constants"); 220788eee38aSdrh goto select_end; 220888eee38aSdrh }else if( iCol<=0 || iCol>pEList->nExpr ){ 22094adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2210da93d238Sdrh "GROUP BY column number %d out of range - should be " 221188eee38aSdrh "between 1 and %d", iCol, pEList->nExpr); 221288eee38aSdrh goto select_end; 221388eee38aSdrh } 221488eee38aSdrh } 22152282792aSdrh } 22162282792aSdrh } 2217cce7d176Sdrh 2218d820cb1bSdrh /* Begin generating code. 2219d820cb1bSdrh */ 22204adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2221d820cb1bSdrh if( v==0 ) goto select_end; 2222d820cb1bSdrh 2223e78e8284Sdrh /* Identify column names if we will be using them in a callback. This 2224e78e8284Sdrh ** step is skipped if the output is going to some other destination. 22250bb28106Sdrh */ 22260bb28106Sdrh if( eDest==SRT_Callback ){ 22276a3ea0e6Sdrh generateColumnNames(pParse, pTabList, pEList); 22280bb28106Sdrh } 22290bb28106Sdrh 2230d3d39e93Sdrh #if 1 /* I do not think we need the following code any more.... */ 223184ac9d02Sdanielk1977 /* If the destination is SRT_Union, then set the number of columns in 223284ac9d02Sdanielk1977 ** the records that will be inserted into the temporary table. The caller 223384ac9d02Sdanielk1977 ** couldn't do this, in case the select statement is of the form 223484ac9d02Sdanielk1977 ** "SELECT * FROM ....". 223584ac9d02Sdanielk1977 ** 223684ac9d02Sdanielk1977 ** We need to do this before we start inserting records into the 223784ac9d02Sdanielk1977 ** temporary table (which has had OP_KeyAsData executed on it), because 223884ac9d02Sdanielk1977 ** it is required by the key comparison function. So do it now, even 223984ac9d02Sdanielk1977 ** though this means that OP_SetNumColumns may be executed on the same 224084ac9d02Sdanielk1977 ** cursor more than once. 224184ac9d02Sdanielk1977 */ 224284ac9d02Sdanielk1977 if( eDest==SRT_Union ){ 224384ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); 224484ac9d02Sdanielk1977 } 2245d3d39e93Sdrh #endif 224684ac9d02Sdanielk1977 2247d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2248d820cb1bSdrh */ 2249ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 22505cf590c1Sdrh const char *zSavedAuthContext; 2251c31c2eb8Sdrh int needRestoreContext; 2252c31c2eb8Sdrh 2253a76b5dfcSdrh if( pTabList->a[i].pSelect==0 ) continue; 22545cf590c1Sdrh if( pTabList->a[i].zName!=0 ){ 22555cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 22565cf590c1Sdrh pParse->zAuthContext = pTabList->a[i].zName; 2257c31c2eb8Sdrh needRestoreContext = 1; 2258c31c2eb8Sdrh }else{ 2259c31c2eb8Sdrh needRestoreContext = 0; 22605cf590c1Sdrh } 22614adee20fSdanielk1977 sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable, 226284ac9d02Sdanielk1977 pTabList->a[i].iCursor, p, i, &isAgg, 0); 2263c31c2eb8Sdrh if( needRestoreContext ){ 22645cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 22655cf590c1Sdrh } 2266832508b7Sdrh pTabList = p->pSrc; 2267832508b7Sdrh pWhere = p->pWhere; 2268c31c2eb8Sdrh if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ 2269832508b7Sdrh pOrderBy = p->pOrderBy; 2270acd4c695Sdrh } 2271832508b7Sdrh pGroupBy = p->pGroupBy; 2272832508b7Sdrh pHaving = p->pHaving; 2273832508b7Sdrh isDistinct = p->isDistinct; 22741b2e0329Sdrh } 22751b2e0329Sdrh 22766e17529eSdrh /* Check for the special case of a min() or max() function by itself 22776e17529eSdrh ** in the result set. 22786e17529eSdrh */ 22796e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 22806e17529eSdrh rc = 0; 22816e17529eSdrh goto select_end; 22826e17529eSdrh } 22836e17529eSdrh 22841b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 22851b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 22861b2e0329Sdrh */ 22871b2e0329Sdrh if( pParent && pParentAgg && 22888c74a8caSdrh flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ 22891b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 22901b2e0329Sdrh return rc; 22911b2e0329Sdrh } 2292832508b7Sdrh 22937b58daeaSdrh /* Set the limiter. 22947b58daeaSdrh */ 22957b58daeaSdrh computeLimitRegisters(pParse, p); 22967b58daeaSdrh 22972d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 22982d0794e3Sdrh */ 22992d0794e3Sdrh if( eDest==SRT_TempTable ){ 23004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2301b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); 23022d0794e3Sdrh } 23032d0794e3Sdrh 23042282792aSdrh /* Do an analysis of aggregate expressions. 2305efb7251dSdrh */ 2306d820cb1bSdrh sqliteAggregateInfoReset(pParse); 2307bb999ef6Sdrh if( isAgg || pGroupBy ){ 23080bce8354Sdrh assert( pParse->nAgg==0 ); 2309bb999ef6Sdrh isAgg = 1; 23102282792aSdrh for(i=0; i<pEList->nExpr; i++){ 23114adee20fSdanielk1977 if( sqlite3ExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ 23121d83f052Sdrh goto select_end; 23132282792aSdrh } 23142282792aSdrh } 23152282792aSdrh if( pGroupBy ){ 23162282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 23174adee20fSdanielk1977 if( sqlite3ExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ 23181d83f052Sdrh goto select_end; 23192282792aSdrh } 23202282792aSdrh } 23212282792aSdrh } 23224adee20fSdanielk1977 if( pHaving && sqlite3ExprAnalyzeAggregates(pParse, pHaving) ){ 23231d83f052Sdrh goto select_end; 23242282792aSdrh } 2325191b690eSdrh if( pOrderBy ){ 2326191b690eSdrh for(i=0; i<pOrderBy->nExpr; i++){ 23274adee20fSdanielk1977 if( sqlite3ExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ 23281d83f052Sdrh goto select_end; 2329191b690eSdrh } 2330191b690eSdrh } 2331191b690eSdrh } 2332efb7251dSdrh } 2333efb7251dSdrh 23342282792aSdrh /* Reset the aggregator 2335cce7d176Sdrh */ 2336cce7d176Sdrh if( isAgg ){ 23374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); 2338e5095355Sdrh for(i=0; i<pParse->nAgg; i++){ 23390bce8354Sdrh FuncDef *pFunc; 23400bce8354Sdrh if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ 2341*f9b596ebSdrh sqlite3VdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_FUNCDEF); 2342e5095355Sdrh } 2343e5095355Sdrh } 23441bee3d7bSdrh if( pGroupBy==0 ){ 23454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 23464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggFocus, 0, 0); 23471bee3d7bSdrh } 2348cce7d176Sdrh } 2349cce7d176Sdrh 235019a775c2Sdrh /* Initialize the memory cell to NULL 235119a775c2Sdrh */ 2352fef5208cSdrh if( eDest==SRT_Mem ){ 23534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 23544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 235519a775c2Sdrh } 235619a775c2Sdrh 2357832508b7Sdrh /* Open a temporary table to use for the distinct set. 2358cce7d176Sdrh */ 235919a775c2Sdrh if( isDistinct ){ 2360832508b7Sdrh distinct = pParse->nTab++; 2361d3d39e93Sdrh openTempIndex(pParse, p, distinct, 0); 2362832508b7Sdrh }else{ 2363832508b7Sdrh distinct = -1; 2364efb7251dSdrh } 2365832508b7Sdrh 2366832508b7Sdrh /* Begin the database scan 2367832508b7Sdrh */ 23684adee20fSdanielk1977 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 236968d2e591Sdrh pGroupBy ? 0 : &pOrderBy); 23701d83f052Sdrh if( pWInfo==0 ) goto select_end; 2371cce7d176Sdrh 23722282792aSdrh /* Use the standard inner loop if we are not dealing with 23732282792aSdrh ** aggregates 2374cce7d176Sdrh */ 2375da9d6c45Sdrh if( !isAgg ){ 2376df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 237784ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 23781d83f052Sdrh goto select_end; 2379cce7d176Sdrh } 2380da9d6c45Sdrh } 2381cce7d176Sdrh 2382e3184744Sdrh /* If we are dealing with aggregates, then do the special aggregate 23832282792aSdrh ** processing. 2384efb7251dSdrh */ 23852282792aSdrh else{ 2386268380caSdrh AggExpr *pAgg; 23872282792aSdrh if( pGroupBy ){ 23881bee3d7bSdrh int lbl1; 23892282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 23904adee20fSdanielk1977 sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr); 2391efb7251dSdrh } 2392d3d39e93Sdrh /* No affinity string is attached to the following OP_MakeKey 2393d3d39e93Sdrh ** because we do not need to do any coercion of datatypes. */ 23944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); 23954adee20fSdanielk1977 lbl1 = sqlite3VdbeMakeLabel(v); 23964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1); 2397268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 2398268380caSdrh if( pAgg->isAgg ) continue; 23994adee20fSdanielk1977 sqlite3ExprCode(pParse, pAgg->pExpr); 24004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggSet, 0, i); 24012282792aSdrh } 24024adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, lbl1); 24032282792aSdrh } 2404268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 24052282792aSdrh Expr *pE; 2406268380caSdrh int nExpr; 2407268380caSdrh FuncDef *pDef; 2408268380caSdrh if( !pAgg->isAgg ) continue; 2409268380caSdrh assert( pAgg->pFunc!=0 ); 2410268380caSdrh assert( pAgg->pFunc->xStep!=0 ); 2411268380caSdrh pDef = pAgg->pFunc; 2412268380caSdrh pE = pAgg->pExpr; 2413268380caSdrh assert( pE!=0 ); 24142282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 2415*f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pE->pList); 24164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 24174adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER); 24182282792aSdrh } 24192282792aSdrh } 24202282792aSdrh 2421cce7d176Sdrh /* End the database scan loop. 2422cce7d176Sdrh */ 24234adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 2424cce7d176Sdrh 24252282792aSdrh /* If we are processing aggregates, we need to set up a second loop 24262282792aSdrh ** over all of the aggregate values and process them. 24272282792aSdrh */ 24282282792aSdrh if( isAgg ){ 24294adee20fSdanielk1977 int endagg = sqlite3VdbeMakeLabel(v); 24302282792aSdrh int startagg; 24314adee20fSdanielk1977 startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg); 24322282792aSdrh pParse->useAgg = 1; 24332282792aSdrh if( pHaving ){ 24344adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pHaving, startagg, 1); 24352282792aSdrh } 2436df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 243784ac9d02Sdanielk1977 iParm, startagg, endagg, aff) ){ 24381d83f052Sdrh goto select_end; 24392282792aSdrh } 24404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, startagg); 24414adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endagg); 24424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Noop, 0, 0); 24432282792aSdrh pParse->useAgg = 0; 24442282792aSdrh } 24452282792aSdrh 2446cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 2447cce7d176Sdrh ** and send them to the callback one by one. 2448cce7d176Sdrh */ 2449cce7d176Sdrh if( pOrderBy ){ 2450ffbc3088Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 2451cce7d176Sdrh } 24526a535340Sdrh 2453f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 2454f620b4e2Sdrh ** temporary table. So delete the subquery structure from the parent 2455f620b4e2Sdrh ** to prevent this subquery from being evaluated again and to force the 2456f620b4e2Sdrh ** the use of the temporary table. 2457f620b4e2Sdrh */ 2458f620b4e2Sdrh if( pParent ){ 2459f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 2460f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 24614adee20fSdanielk1977 sqlite3SelectDelete(p); 2462f620b4e2Sdrh pParent->pSrc->a[parentTab].pSelect = 0; 2463f620b4e2Sdrh } 2464f620b4e2Sdrh 24651d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 24661d83f052Sdrh ** to indicate no errors. 24671d83f052Sdrh */ 24681d83f052Sdrh rc = 0; 24691d83f052Sdrh 24701d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 24711d83f052Sdrh ** successful coding of the SELECT. 24721d83f052Sdrh */ 24731d83f052Sdrh select_end: 24741d83f052Sdrh sqliteAggregateInfoReset(pParse); 24751d83f052Sdrh return rc; 2476cce7d176Sdrh } 2477