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*ed8a3bb1Sdrh ** $Id: select.c,v 1.250 2005/06/06 21:19:57 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 */ 32a2dc3b1aSdanielk1977 Expr *pLimit, /* LIMIT value. NULL means not used */ 33a2dc3b1aSdanielk1977 Expr *pOffset /* OFFSET value. NULL means no offset */ 349bb61fe7Sdrh ){ 359bb61fe7Sdrh Select *pNew; 369bb61fe7Sdrh pNew = sqliteMalloc( sizeof(*pNew) ); 37a2dc3b1aSdanielk1977 assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */ 38daffd0e5Sdrh if( pNew==0 ){ 394adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 404adee20fSdanielk1977 sqlite3SrcListDelete(pSrc); 414adee20fSdanielk1977 sqlite3ExprDelete(pWhere); 424adee20fSdanielk1977 sqlite3ExprListDelete(pGroupBy); 434adee20fSdanielk1977 sqlite3ExprDelete(pHaving); 444adee20fSdanielk1977 sqlite3ExprListDelete(pOrderBy); 45a2dc3b1aSdanielk1977 sqlite3ExprDelete(pLimit); 46a2dc3b1aSdanielk1977 sqlite3ExprDelete(pOffset); 47daffd0e5Sdrh }else{ 48b733d037Sdrh if( pEList==0 ){ 494adee20fSdanielk1977 pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0); 50b733d037Sdrh } 519bb61fe7Sdrh pNew->pEList = pEList; 529bb61fe7Sdrh pNew->pSrc = pSrc; 539bb61fe7Sdrh pNew->pWhere = pWhere; 549bb61fe7Sdrh pNew->pGroupBy = pGroupBy; 559bb61fe7Sdrh pNew->pHaving = pHaving; 569bb61fe7Sdrh pNew->pOrderBy = pOrderBy; 579bb61fe7Sdrh pNew->isDistinct = isDistinct; 5882c3d636Sdrh pNew->op = TK_SELECT; 59a2dc3b1aSdanielk1977 pNew->pLimit = pLimit; 60a2dc3b1aSdanielk1977 pNew->pOffset = pOffset; 617b58daeaSdrh pNew->iLimit = -1; 627b58daeaSdrh pNew->iOffset = -1; 63daffd0e5Sdrh } 649bb61fe7Sdrh return pNew; 659bb61fe7Sdrh } 669bb61fe7Sdrh 679bb61fe7Sdrh /* 6801f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the 6901f3f253Sdrh ** type of join. Return an integer constant that expresses that type 7001f3f253Sdrh ** in terms of the following bit values: 7101f3f253Sdrh ** 7201f3f253Sdrh ** JT_INNER 7301f3f253Sdrh ** JT_OUTER 7401f3f253Sdrh ** JT_NATURAL 7501f3f253Sdrh ** JT_LEFT 7601f3f253Sdrh ** JT_RIGHT 7701f3f253Sdrh ** 7801f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT. 7901f3f253Sdrh ** 8001f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return 8101f3f253Sdrh ** a join type, but put an error in the pParse structure. 8201f3f253Sdrh */ 834adee20fSdanielk1977 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ 8401f3f253Sdrh int jointype = 0; 8501f3f253Sdrh Token *apAll[3]; 8601f3f253Sdrh Token *p; 875719628aSdrh static const struct { 8801f3f253Sdrh const char *zKeyword; 89290c1948Sdrh u8 nChar; 90290c1948Sdrh u8 code; 9101f3f253Sdrh } keywords[] = { 9201f3f253Sdrh { "natural", 7, JT_NATURAL }, 93195e6967Sdrh { "left", 4, JT_LEFT|JT_OUTER }, 94195e6967Sdrh { "right", 5, JT_RIGHT|JT_OUTER }, 95195e6967Sdrh { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, 9601f3f253Sdrh { "outer", 5, JT_OUTER }, 9701f3f253Sdrh { "inner", 5, JT_INNER }, 9801f3f253Sdrh { "cross", 5, JT_INNER }, 9901f3f253Sdrh }; 10001f3f253Sdrh int i, j; 10101f3f253Sdrh apAll[0] = pA; 10201f3f253Sdrh apAll[1] = pB; 10301f3f253Sdrh apAll[2] = pC; 104195e6967Sdrh for(i=0; i<3 && apAll[i]; i++){ 10501f3f253Sdrh p = apAll[i]; 10601f3f253Sdrh for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ 10701f3f253Sdrh if( p->n==keywords[j].nChar 1084adee20fSdanielk1977 && sqlite3StrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ 10901f3f253Sdrh jointype |= keywords[j].code; 11001f3f253Sdrh break; 11101f3f253Sdrh } 11201f3f253Sdrh } 11301f3f253Sdrh if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ 11401f3f253Sdrh jointype |= JT_ERROR; 11501f3f253Sdrh break; 11601f3f253Sdrh } 11701f3f253Sdrh } 118ad2d8307Sdrh if( 119ad2d8307Sdrh (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || 120195e6967Sdrh (jointype & JT_ERROR)!=0 121ad2d8307Sdrh ){ 122ae29ffbeSdrh const char *zSp1 = " "; 123ae29ffbeSdrh const char *zSp2 = " "; 124ae29ffbeSdrh if( pB==0 ){ zSp1++; } 125ae29ffbeSdrh if( pC==0 ){ zSp2++; } 126ae29ffbeSdrh sqlite3ErrorMsg(pParse, "unknown or unsupported join type: " 127ae29ffbeSdrh "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC); 12801f3f253Sdrh jointype = JT_INNER; 129195e6967Sdrh }else if( jointype & JT_RIGHT ){ 1304adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 131da93d238Sdrh "RIGHT and FULL OUTER JOINs are not currently supported"); 132195e6967Sdrh jointype = JT_INNER; 13301f3f253Sdrh } 13401f3f253Sdrh return jointype; 13501f3f253Sdrh } 13601f3f253Sdrh 13701f3f253Sdrh /* 138ad2d8307Sdrh ** Return the index of a column in a table. Return -1 if the column 139ad2d8307Sdrh ** is not contained in the table. 140ad2d8307Sdrh */ 141ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){ 142ad2d8307Sdrh int i; 143ad2d8307Sdrh for(i=0; i<pTab->nCol; i++){ 1444adee20fSdanielk1977 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; 145ad2d8307Sdrh } 146ad2d8307Sdrh return -1; 147ad2d8307Sdrh } 148ad2d8307Sdrh 149ad2d8307Sdrh /* 15091bb0eedSdrh ** Set the value of a token to a '\000'-terminated string. 15191bb0eedSdrh */ 15291bb0eedSdrh static void setToken(Token *p, const char *z){ 15391bb0eedSdrh p->z = z; 15491bb0eedSdrh p->n = strlen(z); 15591bb0eedSdrh p->dyn = 0; 15691bb0eedSdrh } 15791bb0eedSdrh 15891bb0eedSdrh 15991bb0eedSdrh /* 160ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the 161ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2. 162ad2d8307Sdrh */ 163ad2d8307Sdrh static void addWhereTerm( 164ad2d8307Sdrh const char *zCol, /* Name of the column */ 165ad2d8307Sdrh const Table *pTab1, /* First table */ 166030530deSdrh const char *zAlias1, /* Alias for first table. May be NULL */ 167ad2d8307Sdrh const Table *pTab2, /* Second table */ 168030530deSdrh const char *zAlias2, /* Alias for second table. May be NULL */ 169ad2d8307Sdrh Expr **ppExpr /* Add the equality term to this expression */ 170ad2d8307Sdrh ){ 171ad2d8307Sdrh Token dummy; 172ad2d8307Sdrh Expr *pE1a, *pE1b, *pE1c; 173ad2d8307Sdrh Expr *pE2a, *pE2b, *pE2c; 174ad2d8307Sdrh Expr *pE; 175ad2d8307Sdrh 17691bb0eedSdrh setToken(&dummy, zCol); 1774adee20fSdanielk1977 pE1a = sqlite3Expr(TK_ID, 0, 0, &dummy); 1784adee20fSdanielk1977 pE2a = sqlite3Expr(TK_ID, 0, 0, &dummy); 179030530deSdrh if( zAlias1==0 ){ 180030530deSdrh zAlias1 = pTab1->zName; 181030530deSdrh } 182030530deSdrh setToken(&dummy, zAlias1); 1834adee20fSdanielk1977 pE1b = sqlite3Expr(TK_ID, 0, 0, &dummy); 184030530deSdrh if( zAlias2==0 ){ 185030530deSdrh zAlias2 = pTab2->zName; 186030530deSdrh } 187030530deSdrh setToken(&dummy, zAlias2); 1884adee20fSdanielk1977 pE2b = sqlite3Expr(TK_ID, 0, 0, &dummy); 1894adee20fSdanielk1977 pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0); 1904adee20fSdanielk1977 pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0); 1914adee20fSdanielk1977 pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0); 1921f16230bSdrh ExprSetProperty(pE, EP_FromJoin); 19391bb0eedSdrh *ppExpr = sqlite3ExprAnd(*ppExpr, pE); 194ad2d8307Sdrh } 195ad2d8307Sdrh 196ad2d8307Sdrh /* 1971f16230bSdrh ** Set the EP_FromJoin property on all terms of the given expression. 1981cc093c2Sdrh ** 199e78e8284Sdrh ** The EP_FromJoin property is used on terms of an expression to tell 2001cc093c2Sdrh ** the LEFT OUTER JOIN processing logic that this term is part of the 2011f16230bSdrh ** join restriction specified in the ON or USING clause and not a part 2021f16230bSdrh ** of the more general WHERE clause. These terms are moved over to the 2031f16230bSdrh ** WHERE clause during join processing but we need to remember that they 2041f16230bSdrh ** originated in the ON or USING clause. 2051cc093c2Sdrh */ 2061cc093c2Sdrh static void setJoinExpr(Expr *p){ 2071cc093c2Sdrh while( p ){ 2081f16230bSdrh ExprSetProperty(p, EP_FromJoin); 2091cc093c2Sdrh setJoinExpr(p->pLeft); 2101cc093c2Sdrh p = p->pRight; 2111cc093c2Sdrh } 2121cc093c2Sdrh } 2131cc093c2Sdrh 2141cc093c2Sdrh /* 215ad2d8307Sdrh ** This routine processes the join information for a SELECT statement. 216ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause. 217ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms. 218ad2d8307Sdrh ** 21991bb0eedSdrh ** The terms of a FROM clause are contained in the Select.pSrc structure. 22091bb0eedSdrh ** The left most table is the first entry in Select.pSrc. The right-most 22191bb0eedSdrh ** table is the last entry. The join operator is held in the entry to 22291bb0eedSdrh ** the left. Thus entry 0 contains the join operator for the join between 22391bb0eedSdrh ** entries 0 and 1. Any ON or USING clauses associated with the join are 22491bb0eedSdrh ** also attached to the left entry. 22591bb0eedSdrh ** 226ad2d8307Sdrh ** This routine returns the number of errors encountered. 227ad2d8307Sdrh */ 228ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){ 22991bb0eedSdrh SrcList *pSrc; /* All tables in the FROM clause */ 23091bb0eedSdrh int i, j; /* Loop counters */ 23191bb0eedSdrh struct SrcList_item *pLeft; /* Left table being joined */ 23291bb0eedSdrh struct SrcList_item *pRight; /* Right table being joined */ 233ad2d8307Sdrh 23491bb0eedSdrh pSrc = p->pSrc; 23591bb0eedSdrh pLeft = &pSrc->a[0]; 23691bb0eedSdrh pRight = &pLeft[1]; 23791bb0eedSdrh for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){ 23891bb0eedSdrh Table *pLeftTab = pLeft->pTab; 23991bb0eedSdrh Table *pRightTab = pRight->pTab; 24091bb0eedSdrh 24191bb0eedSdrh if( pLeftTab==0 || pRightTab==0 ) continue; 242ad2d8307Sdrh 243ad2d8307Sdrh /* When the NATURAL keyword is present, add WHERE clause terms for 244ad2d8307Sdrh ** every column that the two tables have in common. 245ad2d8307Sdrh */ 24691bb0eedSdrh if( pLeft->jointype & JT_NATURAL ){ 24791bb0eedSdrh if( pLeft->pOn || pLeft->pUsing ){ 2484adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "a NATURAL join may not have " 249ad2d8307Sdrh "an ON or USING clause", 0); 250ad2d8307Sdrh return 1; 251ad2d8307Sdrh } 25291bb0eedSdrh for(j=0; j<pLeftTab->nCol; j++){ 25391bb0eedSdrh char *zName = pLeftTab->aCol[j].zName; 25491bb0eedSdrh if( columnIndex(pRightTab, zName)>=0 ){ 255030530deSdrh addWhereTerm(zName, pLeftTab, pLeft->zAlias, 256030530deSdrh pRightTab, pRight->zAlias, &p->pWhere); 257ad2d8307Sdrh } 258ad2d8307Sdrh } 259ad2d8307Sdrh } 260ad2d8307Sdrh 261ad2d8307Sdrh /* Disallow both ON and USING clauses in the same join 262ad2d8307Sdrh */ 26391bb0eedSdrh if( pLeft->pOn && pLeft->pUsing ){ 2644adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot have both ON and USING " 265da93d238Sdrh "clauses in the same join"); 266ad2d8307Sdrh return 1; 267ad2d8307Sdrh } 268ad2d8307Sdrh 269ad2d8307Sdrh /* Add the ON clause to the end of the WHERE clause, connected by 27091bb0eedSdrh ** an AND operator. 271ad2d8307Sdrh */ 27291bb0eedSdrh if( pLeft->pOn ){ 27391bb0eedSdrh setJoinExpr(pLeft->pOn); 27491bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pLeft->pOn); 27591bb0eedSdrh pLeft->pOn = 0; 276ad2d8307Sdrh } 277ad2d8307Sdrh 278ad2d8307Sdrh /* Create extra terms on the WHERE clause for each column named 279ad2d8307Sdrh ** in the USING clause. Example: If the two tables to be joined are 280ad2d8307Sdrh ** A and B and the USING clause names X, Y, and Z, then add this 281ad2d8307Sdrh ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z 282ad2d8307Sdrh ** Report an error if any column mentioned in the USING clause is 283ad2d8307Sdrh ** not contained in both tables to be joined. 284ad2d8307Sdrh */ 28591bb0eedSdrh if( pLeft->pUsing ){ 28691bb0eedSdrh IdList *pList = pLeft->pUsing; 287ad2d8307Sdrh for(j=0; j<pList->nId; j++){ 28891bb0eedSdrh char *zName = pList->a[j].zName; 28991bb0eedSdrh if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){ 2904adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot join using column %s - column " 29191bb0eedSdrh "not present in both tables", zName); 292ad2d8307Sdrh return 1; 293ad2d8307Sdrh } 294030530deSdrh addWhereTerm(zName, pLeftTab, pLeft->zAlias, 295030530deSdrh pRightTab, pRight->zAlias, &p->pWhere); 296ad2d8307Sdrh } 297ad2d8307Sdrh } 298ad2d8307Sdrh } 299ad2d8307Sdrh return 0; 300ad2d8307Sdrh } 301ad2d8307Sdrh 302ad2d8307Sdrh /* 3039bb61fe7Sdrh ** Delete the given Select structure and all of its substructures. 3049bb61fe7Sdrh */ 3054adee20fSdanielk1977 void sqlite3SelectDelete(Select *p){ 30682c3d636Sdrh if( p==0 ) return; 3074adee20fSdanielk1977 sqlite3ExprListDelete(p->pEList); 3084adee20fSdanielk1977 sqlite3SrcListDelete(p->pSrc); 3094adee20fSdanielk1977 sqlite3ExprDelete(p->pWhere); 3104adee20fSdanielk1977 sqlite3ExprListDelete(p->pGroupBy); 3114adee20fSdanielk1977 sqlite3ExprDelete(p->pHaving); 3124adee20fSdanielk1977 sqlite3ExprListDelete(p->pOrderBy); 3134adee20fSdanielk1977 sqlite3SelectDelete(p->pPrior); 314a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 315a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pOffset); 3169bb61fe7Sdrh sqliteFree(p); 3179bb61fe7Sdrh } 3189bb61fe7Sdrh 3199bb61fe7Sdrh /* 320c926afbcSdrh ** Insert code into "v" that will push the record on the top of the 321c926afbcSdrh ** stack into the sorter. 322c926afbcSdrh */ 323c926afbcSdrh static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){ 324c926afbcSdrh int i; 325c926afbcSdrh for(i=0; i<pOrderBy->nExpr; i++){ 3264adee20fSdanielk1977 sqlite3ExprCode(pParse, pOrderBy->a[i].pExpr); 327c926afbcSdrh } 328ededfd5eSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr, 0); 3294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SortPut, 0, 0); 330c926afbcSdrh } 331c926afbcSdrh 332c926afbcSdrh /* 333ea48eb2eSdrh ** Add code to implement the OFFSET and LIMIT 334ea48eb2eSdrh */ 335ea48eb2eSdrh static void codeLimiter( 336bab39e13Sdrh Vdbe *v, /* Generate code into this VM */ 337ea48eb2eSdrh Select *p, /* The SELECT statement being coded */ 338ea48eb2eSdrh int iContinue, /* Jump here to skip the current record */ 339ea48eb2eSdrh int iBreak, /* Jump here to end the loop */ 340ea48eb2eSdrh int nPop /* Number of times to pop stack when jumping */ 341ea48eb2eSdrh ){ 342ea48eb2eSdrh if( p->iOffset>=0 ){ 343a2dc3b1aSdanielk1977 int addr = sqlite3VdbeCurrentAddr(v) + 3; 344ea48eb2eSdrh if( nPop>0 ) addr++; 345a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, 0); 346a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_IfMemPos, p->iOffset, addr); 347ea48eb2eSdrh if( nPop>0 ){ 348ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_Pop, nPop, 0); 349ea48eb2eSdrh } 350ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 351ad6d9460Sdrh VdbeComment((v, "# skip OFFSET records")); 352ea48eb2eSdrh } 353ea48eb2eSdrh if( p->iLimit>=0 ){ 354ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak); 355ad6d9460Sdrh VdbeComment((v, "# exit when LIMIT reached")); 356ea48eb2eSdrh } 357ea48eb2eSdrh } 358ea48eb2eSdrh 359ea48eb2eSdrh /* 3602282792aSdrh ** This routine generates the code for the inside of the inner loop 3612282792aSdrh ** of a SELECT. 36282c3d636Sdrh ** 36338640e15Sdrh ** If srcTab and nColumn are both zero, then the pEList expressions 36438640e15Sdrh ** are evaluated in order to get the data for this row. If nColumn>0 36538640e15Sdrh ** then data is pulled from srcTab and pEList is used only to get the 36638640e15Sdrh ** datatypes for each column. 3672282792aSdrh */ 3682282792aSdrh static int selectInnerLoop( 3692282792aSdrh Parse *pParse, /* The parser context */ 370df199a25Sdrh Select *p, /* The complete select statement being coded */ 3712282792aSdrh ExprList *pEList, /* List of values being extracted */ 37282c3d636Sdrh int srcTab, /* Pull data from this table */ 373967e8b73Sdrh int nColumn, /* Number of columns in the source table */ 3742282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 3752282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 3762282792aSdrh int eDest, /* How to dispose of the results */ 3772282792aSdrh int iParm, /* An argument to the disposal method */ 3782282792aSdrh int iContinue, /* Jump here to continue with next row */ 37984ac9d02Sdanielk1977 int iBreak, /* Jump here to break out of the inner loop */ 38084ac9d02Sdanielk1977 char *aff /* affinity string if eDest is SRT_Union */ 3812282792aSdrh ){ 3822282792aSdrh Vdbe *v = pParse->pVdbe; 3832282792aSdrh int i; 384ea48eb2eSdrh int hasDistinct; /* True if the DISTINCT keyword is present */ 38538640e15Sdrh 386daffd0e5Sdrh if( v==0 ) return 0; 38738640e15Sdrh assert( pEList!=0 ); 3882282792aSdrh 389df199a25Sdrh /* If there was a LIMIT clause on the SELECT statement, then do the check 390df199a25Sdrh ** to see if this row should be output. 391df199a25Sdrh */ 392ea48eb2eSdrh hasDistinct = distinct>=0 && pEList && pEList->nExpr>0; 393ea48eb2eSdrh if( pOrderBy==0 && !hasDistinct ){ 394bab39e13Sdrh codeLimiter(v, p, iContinue, iBreak, 0); 395df199a25Sdrh } 396df199a25Sdrh 397967e8b73Sdrh /* Pull the requested columns. 3982282792aSdrh */ 39938640e15Sdrh if( nColumn>0 ){ 400967e8b73Sdrh for(i=0; i<nColumn; i++){ 4014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, i); 40282c3d636Sdrh } 40338640e15Sdrh }else{ 40438640e15Sdrh nColumn = pEList->nExpr; 40538640e15Sdrh for(i=0; i<pEList->nExpr; i++){ 4064adee20fSdanielk1977 sqlite3ExprCode(pParse, pEList->a[i].pExpr); 40738640e15Sdrh } 40882c3d636Sdrh } 4092282792aSdrh 410daffd0e5Sdrh /* If the DISTINCT keyword was present on the SELECT statement 411daffd0e5Sdrh ** and this row has been seen before, then do not make this row 412daffd0e5Sdrh ** part of the result. 4132282792aSdrh */ 414ea48eb2eSdrh if( hasDistinct ){ 4150bd1f4eaSdrh #if NULL_ALWAYS_DISTINCT 4164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7); 4170bd1f4eaSdrh #endif 418ededfd5eSdanielk1977 /* Deliberately leave the affinity string off of the following 419ededfd5eSdanielk1977 ** OP_MakeRecord */ 420ededfd5eSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pEList->nExpr * -1, 0); 4214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3); 4224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); 4234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 424ad6d9460Sdrh VdbeComment((v, "# skip indistinct records")); 4250f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 4264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, distinct, 0); 427ea48eb2eSdrh if( pOrderBy==0 ){ 428bab39e13Sdrh codeLimiter(v, p, iContinue, iBreak, nColumn); 429ea48eb2eSdrh } 4302282792aSdrh } 43182c3d636Sdrh 432c926afbcSdrh switch( eDest ){ 4335338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 43482c3d636Sdrh /* In this mode, write each query result to the key of the temporary 43582c3d636Sdrh ** table iParm. 4362282792aSdrh */ 437c926afbcSdrh case SRT_Union: { 4384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 43984ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 4400f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 4414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, iParm, 0); 442c926afbcSdrh break; 443c926afbcSdrh } 44482c3d636Sdrh 44582c3d636Sdrh /* Construct a record from the query result, but instead of 44682c3d636Sdrh ** saving that record, use it as a key to delete elements from 44782c3d636Sdrh ** the temporary table iParm. 44882c3d636Sdrh */ 449c926afbcSdrh case SRT_Except: { 4500bd1f4eaSdrh int addr; 4514adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 45284ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 4534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3); 4544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Delete, iParm, 0); 455c926afbcSdrh break; 456c926afbcSdrh } 4575338a5f7Sdanielk1977 #endif 4585338a5f7Sdanielk1977 4595338a5f7Sdanielk1977 /* Store the result as data using a unique key. 4605338a5f7Sdanielk1977 */ 4615338a5f7Sdanielk1977 case SRT_Table: 4625338a5f7Sdanielk1977 case SRT_TempTable: { 4635338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 4645338a5f7Sdanielk1977 if( pOrderBy ){ 4655338a5f7Sdanielk1977 pushOntoSorter(pParse, v, pOrderBy); 4665338a5f7Sdanielk1977 }else{ 4675338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0); 4685338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 4695338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0); 4705338a5f7Sdanielk1977 } 4715338a5f7Sdanielk1977 break; 4725338a5f7Sdanielk1977 } 4732282792aSdrh 47493758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 4752282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 4762282792aSdrh ** then there should be a single item on the stack. Write this 4772282792aSdrh ** item into the set table with bogus data. 4782282792aSdrh */ 479c926afbcSdrh case SRT_Set: { 4804adee20fSdanielk1977 int addr1 = sqlite3VdbeCurrentAddr(v); 48152b36cabSdrh int addr2; 482e014a838Sdanielk1977 483967e8b73Sdrh assert( nColumn==1 ); 4844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3); 4854adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 4864adee20fSdanielk1977 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 487c926afbcSdrh if( pOrderBy ){ 488c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 489c926afbcSdrh }else{ 490e014a838Sdanielk1977 char aff = (iParm>>16)&0xFF; 491e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff); 49294a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &aff, 1); 4930f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 494e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0); 495c926afbcSdrh } 4964adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v)); 497c926afbcSdrh break; 498c926afbcSdrh } 49982c3d636Sdrh 5002282792aSdrh /* If this is a scalar select that is part of an expression, then 5012282792aSdrh ** store the results in the appropriate memory cell and break out 5022282792aSdrh ** of the scan loop. 5032282792aSdrh */ 50451522cd3Sdrh case SRT_Exists: 505c926afbcSdrh case SRT_Mem: { 506967e8b73Sdrh assert( nColumn==1 ); 507c926afbcSdrh if( pOrderBy ){ 508c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 509c926afbcSdrh }else{ 5104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 5114adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iBreak); 512c926afbcSdrh } 513c926afbcSdrh break; 514c926afbcSdrh } 51593758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ 5162282792aSdrh 517f46f905aSdrh /* Send the data to the callback function. 518f46f905aSdrh */ 519f46f905aSdrh case SRT_Callback: 520f46f905aSdrh case SRT_Sorter: { 521f46f905aSdrh if( pOrderBy ){ 522ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 523f46f905aSdrh pushOntoSorter(pParse, v, pOrderBy); 524f46f905aSdrh }else{ 525f46f905aSdrh assert( eDest==SRT_Callback ); 5264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 527f46f905aSdrh } 528f46f905aSdrh break; 529f46f905aSdrh } 530f46f905aSdrh 531142e30dfSdrh /* Invoke a subroutine to handle the results. The subroutine itself 532142e30dfSdrh ** is responsible for popping the results off of the stack. 533142e30dfSdrh */ 534142e30dfSdrh case SRT_Subroutine: { 535ac82fcf5Sdrh if( pOrderBy ){ 5364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 537ac82fcf5Sdrh pushOntoSorter(pParse, v, pOrderBy); 538ac82fcf5Sdrh }else{ 5394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 540ac82fcf5Sdrh } 541142e30dfSdrh break; 542142e30dfSdrh } 543142e30dfSdrh 5446a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_TRIGGER) 545d7489c39Sdrh /* Discard the results. This is used for SELECT statements inside 546d7489c39Sdrh ** the body of a TRIGGER. The purpose of such selects is to call 547d7489c39Sdrh ** user-defined functions that have side effects. We do not care 548d7489c39Sdrh ** about the actual results of the select. 549d7489c39Sdrh */ 550c926afbcSdrh default: { 551f46f905aSdrh assert( eDest==SRT_Discard ); 5524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 553c926afbcSdrh break; 554c926afbcSdrh } 55593758c8dSdanielk1977 #endif 556c926afbcSdrh } 55782c3d636Sdrh return 0; 55882c3d636Sdrh } 55982c3d636Sdrh 56082c3d636Sdrh /* 561d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 562d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 563d8bc7086Sdrh ** we need to run the sorter and output the results. The following 564d8bc7086Sdrh ** routine generates the code needed to do that. 565d8bc7086Sdrh */ 566c926afbcSdrh static void generateSortTail( 567ffbc3088Sdrh Parse *pParse, /* The parsing context */ 568c926afbcSdrh Select *p, /* The SELECT statement */ 569c926afbcSdrh Vdbe *v, /* Generate code into this VDBE */ 570c926afbcSdrh int nColumn, /* Number of columns of data */ 571c926afbcSdrh int eDest, /* Write the sorted results here */ 572c926afbcSdrh int iParm /* Optional parameter associated with eDest */ 573c926afbcSdrh ){ 5744adee20fSdanielk1977 int end1 = sqlite3VdbeMakeLabel(v); 5754adee20fSdanielk1977 int end2 = sqlite3VdbeMakeLabel(v); 576d8bc7086Sdrh int addr; 577ffbc3088Sdrh KeyInfo *pInfo; 578ffbc3088Sdrh ExprList *pOrderBy; 579ffbc3088Sdrh int nCol, i; 5809bb575fdSdrh sqlite3 *db = pParse->db; 581ffbc3088Sdrh 582f46f905aSdrh if( eDest==SRT_Sorter ) return; 583ffbc3088Sdrh pOrderBy = p->pOrderBy; 584ffbc3088Sdrh nCol = pOrderBy->nExpr; 585ffbc3088Sdrh pInfo = sqliteMalloc( sizeof(*pInfo) + nCol*(sizeof(CollSeq*)+1) ); 586ffbc3088Sdrh if( pInfo==0 ) return; 587ffbc3088Sdrh pInfo->aSortOrder = (char*)&pInfo->aColl[nCol]; 588ffbc3088Sdrh pInfo->nField = nCol; 589ffbc3088Sdrh for(i=0; i<nCol; i++){ 5900202b29eSdanielk1977 /* If a collation sequence was specified explicity, then it 5910202b29eSdanielk1977 ** is stored in pOrderBy->a[i].zName. Otherwise, use the default 5920202b29eSdanielk1977 ** collation type for the expression. 5930202b29eSdanielk1977 */ 5947cedc8d4Sdanielk1977 pInfo->aColl[i] = sqlite3ExprCollSeq(pParse, pOrderBy->a[i].pExpr); 5950202b29eSdanielk1977 if( !pInfo->aColl[i] ){ 596ffbc3088Sdrh pInfo->aColl[i] = db->pDfltColl; 5970202b29eSdanielk1977 } 598ffbc3088Sdrh pInfo->aSortOrder[i] = pOrderBy->a[i].sortOrder; 599ffbc3088Sdrh } 600ffbc3088Sdrh sqlite3VdbeOp3(v, OP_Sort, 0, 0, (char*)pInfo, P3_KEYINFO_HANDOFF); 6014adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_SortNext, 0, end1); 602bab39e13Sdrh codeLimiter(v, p, addr, end2, 1); 603c926afbcSdrh switch( eDest ){ 604c926afbcSdrh case SRT_Table: 605c926afbcSdrh case SRT_TempTable: { 6064adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0); 6074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 6084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0); 609c926afbcSdrh break; 610c926afbcSdrh } 61193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 612c926afbcSdrh case SRT_Set: { 613c926afbcSdrh assert( nColumn==1 ); 6144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 6154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 6164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3); 617ededfd5eSdanielk1977 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, "n", P3_STATIC); 6180f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 619e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0); 620c926afbcSdrh break; 621c926afbcSdrh } 62251522cd3Sdrh case SRT_Exists: 623c926afbcSdrh case SRT_Mem: { 624c926afbcSdrh assert( nColumn==1 ); 6254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 6264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, end1); 627c926afbcSdrh break; 628c926afbcSdrh } 62993758c8dSdanielk1977 #endif 630ce665cf6Sdrh case SRT_Callback: 631ac82fcf5Sdrh case SRT_Subroutine: { 632ac82fcf5Sdrh int i; 63384ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0); 63484ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 635ac82fcf5Sdrh for(i=0; i<nColumn; i++){ 6364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, -1-i, i); 637ac82fcf5Sdrh } 638ce665cf6Sdrh if( eDest==SRT_Callback ){ 639ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 640ce665cf6Sdrh }else{ 6414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 642ce665cf6Sdrh } 64384ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 644ac82fcf5Sdrh break; 645ac82fcf5Sdrh } 646c926afbcSdrh default: { 647f46f905aSdrh /* Do nothing */ 648c926afbcSdrh break; 649c926afbcSdrh } 650c926afbcSdrh } 6514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr); 6524adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, end2); 6534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 6544adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, end1); 6554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SortReset, 0, 0); 656d8bc7086Sdrh } 657d8bc7086Sdrh 658d8bc7086Sdrh /* 659517eb646Sdanielk1977 ** Return a pointer to a string containing the 'declaration type' of the 660517eb646Sdanielk1977 ** expression pExpr. The string may be treated as static by the caller. 661e78e8284Sdrh ** 662517eb646Sdanielk1977 ** If the declaration type is the exact datatype definition extracted from 663517eb646Sdanielk1977 ** the original CREATE TABLE statement if the expression is a column. 664e78e8284Sdrh ** 665517eb646Sdanielk1977 ** The declaration type for an expression is either TEXT, NUMERIC or ANY. 666517eb646Sdanielk1977 ** The declaration type for a ROWID field is INTEGER. 667fcb78a49Sdrh */ 668b3bce662Sdanielk1977 static const char *columnType(NameContext *pNC, Expr *pExpr){ 66900e279d9Sdanielk1977 char const *zType; 670517eb646Sdanielk1977 int j; 671b3bce662Sdanielk1977 if( pExpr==0 || pNC->pSrcList==0 ) return 0; 6725338a5f7Sdanielk1977 6735338a5f7Sdanielk1977 /* The TK_AS operator can only occur in ORDER BY, GROUP BY, HAVING, 6745338a5f7Sdanielk1977 ** and LIMIT clauses. But pExpr originates in the result set of a 6755338a5f7Sdanielk1977 ** SELECT. So pExpr can never contain an AS operator. 6765338a5f7Sdanielk1977 */ 6775338a5f7Sdanielk1977 assert( pExpr->op!=TK_AS ); 6785338a5f7Sdanielk1977 67900e279d9Sdanielk1977 switch( pExpr->op ){ 68000e279d9Sdanielk1977 case TK_COLUMN: { 681b3bce662Sdanielk1977 Table *pTab = 0; 682517eb646Sdanielk1977 int iCol = pExpr->iColumn; 683b3bce662Sdanielk1977 while( pNC && !pTab ){ 684b3bce662Sdanielk1977 SrcList *pTabList = pNC->pSrcList; 685b3bce662Sdanielk1977 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); 686b3bce662Sdanielk1977 if( j<pTabList->nSrc ){ 6876a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 688b3bce662Sdanielk1977 }else{ 689b3bce662Sdanielk1977 pNC = pNC->pNext; 690b3bce662Sdanielk1977 } 691b3bce662Sdanielk1977 } 6927e62779aSdrh if( pTab==0 ){ 6937e62779aSdrh /* FIX ME: 6947e62779aSdrh ** This can occurs if you have something like "SELECT new.x;" inside 6957e62779aSdrh ** a trigger. In other words, if you reference the special "new" 6967e62779aSdrh ** table in the result set of a select. We do not have a good way 6977e62779aSdrh ** to find the actual table type, so call it "TEXT". This is really 6987e62779aSdrh ** something of a bug, but I do not know how to fix it. 6997e62779aSdrh ** 7007e62779aSdrh ** This code does not produce the correct answer - it just prevents 7017e62779aSdrh ** a segfault. See ticket #1229. 7027e62779aSdrh */ 7037e62779aSdrh zType = "TEXT"; 7047e62779aSdrh break; 7057e62779aSdrh } 706b3bce662Sdanielk1977 assert( pTab ); 707fcb78a49Sdrh if( iCol<0 ) iCol = pTab->iPKey; 708fcb78a49Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 709fcb78a49Sdrh if( iCol<0 ){ 710fcb78a49Sdrh zType = "INTEGER"; 711fcb78a49Sdrh }else{ 712fcb78a49Sdrh zType = pTab->aCol[iCol].zType; 713fcb78a49Sdrh } 71400e279d9Sdanielk1977 break; 715736c22b8Sdrh } 71693758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 71700e279d9Sdanielk1977 case TK_SELECT: { 718b3bce662Sdanielk1977 NameContext sNC; 71900e279d9Sdanielk1977 Select *pS = pExpr->pSelect; 720b3bce662Sdanielk1977 sNC.pSrcList = pExpr->pSelect->pSrc; 721b3bce662Sdanielk1977 sNC.pNext = pNC; 722b3bce662Sdanielk1977 zType = columnType(&sNC, pS->pEList->a[0].pExpr); 72300e279d9Sdanielk1977 break; 724fcb78a49Sdrh } 72593758c8dSdanielk1977 #endif 72600e279d9Sdanielk1977 default: 72700e279d9Sdanielk1977 zType = 0; 72800e279d9Sdanielk1977 } 72900e279d9Sdanielk1977 730517eb646Sdanielk1977 return zType; 731517eb646Sdanielk1977 } 732517eb646Sdanielk1977 733517eb646Sdanielk1977 /* 734517eb646Sdanielk1977 ** Generate code that will tell the VDBE the declaration types of columns 735517eb646Sdanielk1977 ** in the result set. 736517eb646Sdanielk1977 */ 737517eb646Sdanielk1977 static void generateColumnTypes( 738517eb646Sdanielk1977 Parse *pParse, /* Parser context */ 739517eb646Sdanielk1977 SrcList *pTabList, /* List of tables */ 740517eb646Sdanielk1977 ExprList *pEList /* Expressions defining the result set */ 741517eb646Sdanielk1977 ){ 742517eb646Sdanielk1977 Vdbe *v = pParse->pVdbe; 743517eb646Sdanielk1977 int i; 744b3bce662Sdanielk1977 NameContext sNC; 745b3bce662Sdanielk1977 sNC.pSrcList = pTabList; 746517eb646Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 747517eb646Sdanielk1977 Expr *p = pEList->a[i].pExpr; 748b3bce662Sdanielk1977 const char *zType = columnType(&sNC, p); 74900e279d9Sdanielk1977 if( zType==0 ) continue; 750fbcd585fSdanielk1977 /* The vdbe must make it's own copy of the column-type, in case the 751fbcd585fSdanielk1977 ** schema is reset before this virtual machine is deleted. 752fbcd585fSdanielk1977 */ 753fbcd585fSdanielk1977 sqlite3VdbeSetColName(v, i+pEList->nExpr, zType, strlen(zType)); 754fcb78a49Sdrh } 755fcb78a49Sdrh } 756fcb78a49Sdrh 757fcb78a49Sdrh /* 758fcb78a49Sdrh ** Generate code that will tell the VDBE the names of columns 759fcb78a49Sdrh ** in the result set. This information is used to provide the 760fcabd464Sdrh ** azCol[] values in the callback. 76182c3d636Sdrh */ 762832508b7Sdrh static void generateColumnNames( 763832508b7Sdrh Parse *pParse, /* Parser context */ 764ad3cab52Sdrh SrcList *pTabList, /* List of tables */ 765832508b7Sdrh ExprList *pEList /* Expressions defining the result set */ 766832508b7Sdrh ){ 767d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 7686a3ea0e6Sdrh int i, j; 7699bb575fdSdrh sqlite3 *db = pParse->db; 770fcabd464Sdrh int fullNames, shortNames; 771fcabd464Sdrh 772fe2093d7Sdrh #ifndef SQLITE_OMIT_EXPLAIN 7733cf86063Sdanielk1977 /* If this is an EXPLAIN, skip this step */ 7743cf86063Sdanielk1977 if( pParse->explain ){ 77561de0d1bSdanielk1977 return; 7763cf86063Sdanielk1977 } 7775338a5f7Sdanielk1977 #endif 7783cf86063Sdanielk1977 779d6502758Sdrh assert( v!=0 ); 7806f8a503dSdanielk1977 if( pParse->colNamesSet || v==0 || sqlite3_malloc_failed ) return; 781d8bc7086Sdrh pParse->colNamesSet = 1; 782fcabd464Sdrh fullNames = (db->flags & SQLITE_FullColNames)!=0; 783fcabd464Sdrh shortNames = (db->flags & SQLITE_ShortColNames)!=0; 78422322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, pEList->nExpr); 78582c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 78682c3d636Sdrh Expr *p; 7875a38705eSdrh p = pEList->a[i].pExpr; 7885a38705eSdrh if( p==0 ) continue; 78982c3d636Sdrh if( pEList->a[i].zName ){ 79082c3d636Sdrh char *zName = pEList->a[i].zName; 791d8123366Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, strlen(zName)); 79282c3d636Sdrh continue; 79382c3d636Sdrh } 794fa173a76Sdrh if( p->op==TK_COLUMN && pTabList ){ 7956a3ea0e6Sdrh Table *pTab; 79697665873Sdrh char *zCol; 7978aff1015Sdrh int iCol = p->iColumn; 7986a3ea0e6Sdrh for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} 7996a3ea0e6Sdrh assert( j<pTabList->nSrc ); 8006a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 8018aff1015Sdrh if( iCol<0 ) iCol = pTab->iPKey; 80297665873Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 803b1363206Sdrh if( iCol<0 ){ 80447a6db2bSdrh zCol = "rowid"; 805b1363206Sdrh }else{ 806b1363206Sdrh zCol = pTab->aCol[iCol].zName; 807b1363206Sdrh } 808fcabd464Sdrh if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ 8093cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); 810fcabd464Sdrh }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ 81182c3d636Sdrh char *zName = 0; 81282c3d636Sdrh char *zTab; 81382c3d636Sdrh 8146a3ea0e6Sdrh zTab = pTabList->a[j].zAlias; 815fcabd464Sdrh if( fullNames || zTab==0 ) zTab = pTab->zName; 8164adee20fSdanielk1977 sqlite3SetString(&zName, zTab, ".", zCol, 0); 8173cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, P3_DYNAMIC); 81882c3d636Sdrh }else{ 81947a6db2bSdrh sqlite3VdbeSetColName(v, i, zCol, strlen(zCol)); 82082c3d636Sdrh } 8216977fea8Sdrh }else if( p->span.z && p->span.z[0] ){ 8223cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); 8233cf86063Sdanielk1977 /* sqlite3VdbeCompressSpace(v, addr); */ 8241bee3d7bSdrh }else{ 8251bee3d7bSdrh char zName[30]; 8261bee3d7bSdrh assert( p->op!=TK_COLUMN || pTabList==0 ); 8271bee3d7bSdrh sprintf(zName, "column%d", i+1); 8283cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, 0); 82982c3d636Sdrh } 83082c3d636Sdrh } 83176d505baSdanielk1977 generateColumnTypes(pParse, pTabList, pEList); 8325080aaa7Sdrh } 83382c3d636Sdrh 83493758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 83582c3d636Sdrh /* 836d8bc7086Sdrh ** Name of the connection operator, used for error messages. 837d8bc7086Sdrh */ 838d8bc7086Sdrh static const char *selectOpName(int id){ 839d8bc7086Sdrh char *z; 840d8bc7086Sdrh switch( id ){ 841d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 842d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 843d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 844d8bc7086Sdrh default: z = "UNION"; break; 845d8bc7086Sdrh } 846d8bc7086Sdrh return z; 847d8bc7086Sdrh } 84893758c8dSdanielk1977 #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 849d8bc7086Sdrh 850d8bc7086Sdrh /* 851315555caSdrh ** Forward declaration 852315555caSdrh */ 8539b3187e1Sdrh static int prepSelectStmt(Parse*, Select*); 854315555caSdrh 855315555caSdrh /* 85622f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes 85722f70c32Sdrh ** the result set of that SELECT. 85822f70c32Sdrh */ 8594adee20fSdanielk1977 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ 86022f70c32Sdrh Table *pTab; 861b733d037Sdrh int i, j; 86222f70c32Sdrh ExprList *pEList; 863290c1948Sdrh Column *aCol, *pCol; 86422f70c32Sdrh 8659b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 86622f70c32Sdrh return 0; 86722f70c32Sdrh } 868142bdf40Sdanielk1977 if( sqlite3SelectResolve(pParse, pSelect, 0) ){ 869142bdf40Sdanielk1977 return 0; 870142bdf40Sdanielk1977 } 87122f70c32Sdrh pTab = sqliteMalloc( sizeof(Table) ); 87222f70c32Sdrh if( pTab==0 ){ 87322f70c32Sdrh return 0; 87422f70c32Sdrh } 875*ed8a3bb1Sdrh pTab->nRef = 1; 87622f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 87722f70c32Sdrh pEList = pSelect->pEList; 87822f70c32Sdrh pTab->nCol = pEList->nExpr; 879417be79cSdrh assert( pTab->nCol>0 ); 880b733d037Sdrh pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 881290c1948Sdrh for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){ 88279d5f63fSdrh Expr *p, *pR; 883517eb646Sdanielk1977 char *zType; 88491bb0eedSdrh char *zName; 88579d5f63fSdrh char *zBasename; 88679d5f63fSdrh int cnt; 887b3bce662Sdanielk1977 NameContext sNC; 88879d5f63fSdrh 88979d5f63fSdrh /* Get an appropriate name for the column 89079d5f63fSdrh */ 89179d5f63fSdrh p = pEList->a[i].pExpr; 892290c1948Sdrh assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 ); 89391bb0eedSdrh if( (zName = pEList->a[i].zName)!=0 ){ 89479d5f63fSdrh /* If the column contains an "AS <name>" phrase, use <name> as the name */ 89591bb0eedSdrh zName = sqliteStrDup(zName); 896517eb646Sdanielk1977 }else if( p->op==TK_DOT 897b733d037Sdrh && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ 89879d5f63fSdrh /* For columns of the from A.B use B as the name */ 89991bb0eedSdrh zName = sqlite3MPrintf("%T", &pR->token); 900b733d037Sdrh }else if( p->span.z && p->span.z[0] ){ 90179d5f63fSdrh /* Use the original text of the column expression as its name */ 90291bb0eedSdrh zName = sqlite3MPrintf("%T", &p->span); 90322f70c32Sdrh }else{ 90479d5f63fSdrh /* If all else fails, make up a name */ 90591bb0eedSdrh zName = sqlite3MPrintf("column%d", i+1); 90622f70c32Sdrh } 90791bb0eedSdrh sqlite3Dequote(zName); 908dd5b2fa5Sdrh if( sqlite3_malloc_failed ){ 909dd5b2fa5Sdrh sqliteFree(zName); 910dd5b2fa5Sdrh sqlite3DeleteTable(0, pTab); 911dd5b2fa5Sdrh return 0; 912dd5b2fa5Sdrh } 91379d5f63fSdrh 91479d5f63fSdrh /* Make sure the column name is unique. If the name is not unique, 91579d5f63fSdrh ** append a integer to the name so that it becomes unique. 91679d5f63fSdrh */ 91779d5f63fSdrh zBasename = zName; 91879d5f63fSdrh for(j=cnt=0; j<i; j++){ 91979d5f63fSdrh if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ 92079d5f63fSdrh zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt); 92179d5f63fSdrh j = -1; 922dd5b2fa5Sdrh if( zName==0 ) break; 92379d5f63fSdrh } 92479d5f63fSdrh } 92579d5f63fSdrh if( zBasename!=zName ){ 92679d5f63fSdrh sqliteFree(zBasename); 92779d5f63fSdrh } 92891bb0eedSdrh pCol->zName = zName; 929e014a838Sdanielk1977 93079d5f63fSdrh /* Get the typename, type affinity, and collating sequence for the 93179d5f63fSdrh ** column. 93279d5f63fSdrh */ 933c43e8be8Sdrh memset(&sNC, 0, sizeof(sNC)); 934b3bce662Sdanielk1977 sNC.pSrcList = pSelect->pSrc; 935b3bce662Sdanielk1977 zType = sqliteStrDup(columnType(&sNC, p)); 936290c1948Sdrh pCol->zType = zType; 937c60e9b82Sdanielk1977 pCol->affinity = sqlite3ExprAffinity(p); 938290c1948Sdrh pCol->pColl = sqlite3ExprCollSeq(pParse, p); 939290c1948Sdrh if( !pCol->pColl ){ 940290c1948Sdrh pCol->pColl = pParse->db->pDfltColl; 9410202b29eSdanielk1977 } 94222f70c32Sdrh } 94322f70c32Sdrh pTab->iPKey = -1; 94422f70c32Sdrh return pTab; 94522f70c32Sdrh } 94622f70c32Sdrh 94722f70c32Sdrh /* 9489b3187e1Sdrh ** Prepare a SELECT statement for processing by doing the following 9499b3187e1Sdrh ** things: 950d8bc7086Sdrh ** 9519b3187e1Sdrh ** (1) Make sure VDBE cursor numbers have been assigned to every 9529b3187e1Sdrh ** element of the FROM clause. 9539b3187e1Sdrh ** 9549b3187e1Sdrh ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that 9559b3187e1Sdrh ** defines FROM clause. When views appear in the FROM clause, 95663eb5f29Sdrh ** fill pTabList->a[].pSelect with a copy of the SELECT statement 95763eb5f29Sdrh ** that implements the view. A copy is made of the view's SELECT 95863eb5f29Sdrh ** statement so that we can freely modify or delete that statement 95963eb5f29Sdrh ** without worrying about messing up the presistent representation 96063eb5f29Sdrh ** of the view. 961d8bc7086Sdrh ** 9629b3187e1Sdrh ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword 963ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 964ad2d8307Sdrh ** 9659b3187e1Sdrh ** (4) Scan the list of columns in the result set (pEList) looking 96654473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 96754473229Sdrh ** If found, expand each "*" to be every column in every table 96854473229Sdrh ** and TABLE.* to be every column in TABLE. 969d8bc7086Sdrh ** 970d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 971d8bc7086Sdrh ** in pParse and return non-zero. 972d8bc7086Sdrh */ 9739b3187e1Sdrh static int prepSelectStmt(Parse *pParse, Select *p){ 97454473229Sdrh int i, j, k, rc; 975ad3cab52Sdrh SrcList *pTabList; 976daffd0e5Sdrh ExprList *pEList; 977a76b5dfcSdrh Table *pTab; 978290c1948Sdrh struct SrcList_item *pFrom; 979daffd0e5Sdrh 980e94ddc9eSdanielk1977 if( p==0 || p->pSrc==0 || sqlite3_malloc_failed ) return 1; 981daffd0e5Sdrh pTabList = p->pSrc; 982daffd0e5Sdrh pEList = p->pEList; 983d8bc7086Sdrh 9849b3187e1Sdrh /* Make sure cursor numbers have been assigned to all entries in 9859b3187e1Sdrh ** the FROM clause of the SELECT statement. 9869b3187e1Sdrh */ 9879b3187e1Sdrh sqlite3SrcListAssignCursors(pParse, p->pSrc); 9889b3187e1Sdrh 9899b3187e1Sdrh /* Look up every table named in the FROM clause of the select. If 9909b3187e1Sdrh ** an entry of the FROM clause is a subquery instead of a table or view, 9919b3187e1Sdrh ** then create a transient table structure to describe the subquery. 992d8bc7086Sdrh */ 993290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 9949b3187e1Sdrh if( pFrom->pTab!=0 ){ 9959b3187e1Sdrh /* This statement has already been prepared. There is no need 9969b3187e1Sdrh ** to go further. */ 9979b3187e1Sdrh assert( i==0 ); 998d8bc7086Sdrh return 0; 999d8bc7086Sdrh } 1000290c1948Sdrh if( pFrom->zName==0 ){ 100193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 100222f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 1003290c1948Sdrh assert( pFrom->pSelect!=0 ); 1004290c1948Sdrh if( pFrom->zAlias==0 ){ 100591bb0eedSdrh pFrom->zAlias = 100691bb0eedSdrh sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect); 1007ad2d8307Sdrh } 1008*ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1009290c1948Sdrh pFrom->pTab = pTab = 1010290c1948Sdrh sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect); 101122f70c32Sdrh if( pTab==0 ){ 1012daffd0e5Sdrh return 1; 1013daffd0e5Sdrh } 10145cf590c1Sdrh /* The isTransient flag indicates that the Table structure has been 10155cf590c1Sdrh ** dynamically allocated and may be freed at any time. In other words, 10165cf590c1Sdrh ** pTab is not pointing to a persistent table structure that defines 10175cf590c1Sdrh ** part of the schema. */ 101822f70c32Sdrh pTab->isTransient = 1; 101993758c8dSdanielk1977 #endif 102022f70c32Sdrh }else{ 1021a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 1022*ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1023290c1948Sdrh pFrom->pTab = pTab = 1024290c1948Sdrh sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); 1025a76b5dfcSdrh if( pTab==0 ){ 1026d8bc7086Sdrh return 1; 1027d8bc7086Sdrh } 1028*ed8a3bb1Sdrh pTab->nRef++; 102993758c8dSdanielk1977 #ifndef SQLITE_OMIT_VIEW 1030a76b5dfcSdrh if( pTab->pSelect ){ 103163eb5f29Sdrh /* We reach here if the named table is a really a view */ 10324adee20fSdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 1033417be79cSdrh return 1; 1034417be79cSdrh } 1035290c1948Sdrh /* If pFrom->pSelect!=0 it means we are dealing with a 103663eb5f29Sdrh ** view within a view. The SELECT structure has already been 103763eb5f29Sdrh ** copied by the outer view so we can skip the copy step here 103863eb5f29Sdrh ** in the inner view. 103963eb5f29Sdrh */ 1040290c1948Sdrh if( pFrom->pSelect==0 ){ 1041290c1948Sdrh pFrom->pSelect = sqlite3SelectDup(pTab->pSelect); 1042a76b5dfcSdrh } 1043d8bc7086Sdrh } 104493758c8dSdanielk1977 #endif 104522f70c32Sdrh } 104663eb5f29Sdrh } 1047d8bc7086Sdrh 1048ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 1049ad2d8307Sdrh */ 1050ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 1051ad2d8307Sdrh 10527c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 105354473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 105454473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 10557c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 10567c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 10577c917d19Sdrh ** each one to the list of all columns in all tables. 105854473229Sdrh ** 105954473229Sdrh ** The first loop just checks to see if there are any "*" operators 106054473229Sdrh ** that need expanding. 1061d8bc7086Sdrh */ 10627c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 106354473229Sdrh Expr *pE = pEList->a[k].pExpr; 106454473229Sdrh if( pE->op==TK_ALL ) break; 106554473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 106654473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 10677c917d19Sdrh } 106854473229Sdrh rc = 0; 10697c917d19Sdrh if( k<pEList->nExpr ){ 107054473229Sdrh /* 107154473229Sdrh ** If we get here it means the result set contains one or more "*" 107254473229Sdrh ** operators that need to be expanded. Loop through each expression 107354473229Sdrh ** in the result set and expand them one by one. 107454473229Sdrh */ 10757c917d19Sdrh struct ExprList_item *a = pEList->a; 10767c917d19Sdrh ExprList *pNew = 0; 1077d70dc52dSdrh int flags = pParse->db->flags; 1078d70dc52dSdrh int longNames = (flags & SQLITE_FullColNames)!=0 && 1079d70dc52dSdrh (flags & SQLITE_ShortColNames)==0; 1080d70dc52dSdrh 10817c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 108254473229Sdrh Expr *pE = a[k].pExpr; 108354473229Sdrh if( pE->op!=TK_ALL && 108454473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 108554473229Sdrh /* This particular expression does not need to be expanded. 108654473229Sdrh */ 10874adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 10887c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 10897c917d19Sdrh a[k].pExpr = 0; 10907c917d19Sdrh a[k].zName = 0; 10917c917d19Sdrh }else{ 109254473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 109354473229Sdrh ** expanded. */ 109454473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 1095cf55b7aeSdrh char *zTName; /* text of name of TABLE */ 109654473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 1097cf55b7aeSdrh zTName = sqlite3NameFromToken(&pE->pLeft->token); 109854473229Sdrh }else{ 1099cf55b7aeSdrh zTName = 0; 110054473229Sdrh } 1101290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1102290c1948Sdrh Table *pTab = pFrom->pTab; 1103290c1948Sdrh char *zTabName = pFrom->zAlias; 110454473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 110554473229Sdrh zTabName = pTab->zName; 110654473229Sdrh } 1107cf55b7aeSdrh if( zTName && (zTabName==0 || zTabName[0]==0 || 1108cf55b7aeSdrh sqlite3StrICmp(zTName, zTabName)!=0) ){ 110954473229Sdrh continue; 111054473229Sdrh } 111154473229Sdrh tableSeen = 1; 1112d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 111322f70c32Sdrh Expr *pExpr, *pLeft, *pRight; 1114ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 1115ad2d8307Sdrh 111691bb0eedSdrh if( i>0 ){ 111791bb0eedSdrh struct SrcList_item *pLeft = &pTabList->a[i-1]; 111891bb0eedSdrh if( (pLeft->jointype & JT_NATURAL)!=0 && 111991bb0eedSdrh columnIndex(pLeft->pTab, zName)>=0 ){ 1120ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 1121ad2d8307Sdrh ** table on the right */ 1122ad2d8307Sdrh continue; 1123ad2d8307Sdrh } 112491bb0eedSdrh if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ 1125ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 1126ad2d8307Sdrh ** using clause from the table on the right. */ 1127ad2d8307Sdrh continue; 1128ad2d8307Sdrh } 112991bb0eedSdrh } 11304adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 113122f70c32Sdrh if( pRight==0 ) break; 113291bb0eedSdrh setToken(&pRight->token, zName); 1133d70dc52dSdrh if( zTabName && (longNames || pTabList->nSrc>1) ){ 11344adee20fSdanielk1977 pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 11354adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 113622f70c32Sdrh if( pExpr==0 ) break; 113791bb0eedSdrh setToken(&pLeft->token, zTabName); 113891bb0eedSdrh setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); 11396977fea8Sdrh pExpr->span.dyn = 1; 11406977fea8Sdrh pExpr->token.z = 0; 11416977fea8Sdrh pExpr->token.n = 0; 11426977fea8Sdrh pExpr->token.dyn = 0; 114322f70c32Sdrh }else{ 114422f70c32Sdrh pExpr = pRight; 11456977fea8Sdrh pExpr->span = pExpr->token; 114622f70c32Sdrh } 1147d70dc52dSdrh if( longNames ){ 1148d70dc52dSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); 1149d70dc52dSdrh }else{ 115079d5f63fSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); 1151d8bc7086Sdrh } 1152d8bc7086Sdrh } 1153d70dc52dSdrh } 115454473229Sdrh if( !tableSeen ){ 1155cf55b7aeSdrh if( zTName ){ 1156cf55b7aeSdrh sqlite3ErrorMsg(pParse, "no such table: %s", zTName); 1157f5db2d3eSdrh }else{ 11584adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1159f5db2d3eSdrh } 116054473229Sdrh rc = 1; 116154473229Sdrh } 1162cf55b7aeSdrh sqliteFree(zTName); 11637c917d19Sdrh } 11647c917d19Sdrh } 11654adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 11667c917d19Sdrh p->pEList = pNew; 1167d8bc7086Sdrh } 116854473229Sdrh return rc; 1169d8bc7086Sdrh } 1170d8bc7086Sdrh 117193758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 1172ff78bd2fSdrh /* 1173d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1174d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1175967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1176d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1177d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1178d8bc7086Sdrh ** 1179d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1180d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1181d8bc7086Sdrh ** 1182d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1183d8bc7086Sdrh ** of errors is returned. 1184d8bc7086Sdrh */ 1185d8bc7086Sdrh static int matchOrderbyToColumn( 1186d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1187d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1188d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1189e4de1febSdrh int iTable, /* Insert this value in iTable */ 1190d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1191d8bc7086Sdrh ){ 1192d8bc7086Sdrh int nErr = 0; 1193d8bc7086Sdrh int i, j; 1194d8bc7086Sdrh ExprList *pEList; 1195d8bc7086Sdrh 1196daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1197d8bc7086Sdrh if( mustComplete ){ 1198d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1199d8bc7086Sdrh } 12009b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 1201d8bc7086Sdrh return 1; 1202d8bc7086Sdrh } 1203d8bc7086Sdrh if( pSelect->pPrior ){ 120492cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 120592cd52f5Sdrh return 1; 120692cd52f5Sdrh } 1207d8bc7086Sdrh } 1208d8bc7086Sdrh pEList = pSelect->pEList; 1209d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1210d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1211e4de1febSdrh int iCol = -1; 1212d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 12134adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1214e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 12154adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1216da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1217e4de1febSdrh iCol, pEList->nExpr); 1218e4de1febSdrh nErr++; 1219e4de1febSdrh break; 1220e4de1febSdrh } 1221fcb78a49Sdrh if( !mustComplete ) continue; 1222e4de1febSdrh iCol--; 1223e4de1febSdrh } 1224e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 12254cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1226a76b5dfcSdrh char *zName, *zLabel; 1227a76b5dfcSdrh zName = pEList->a[j].zName; 1228a99db3b6Sdrh zLabel = sqlite3NameFromToken(&pE->token); 1229a99db3b6Sdrh assert( zLabel!=0 ); 12304adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1231e4de1febSdrh iCol = j; 1232d8bc7086Sdrh } 12336e142f54Sdrh sqliteFree(zLabel); 1234d8bc7086Sdrh } 12354adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1236e4de1febSdrh iCol = j; 1237d8bc7086Sdrh } 1238e4de1febSdrh } 1239e4de1febSdrh if( iCol>=0 ){ 1240967e8b73Sdrh pE->op = TK_COLUMN; 1241e4de1febSdrh pE->iColumn = iCol; 1242d8bc7086Sdrh pE->iTable = iTable; 1243a58fdfb1Sdanielk1977 pE->iAgg = -1; 1244d8bc7086Sdrh pOrderBy->a[i].done = 1; 1245d8bc7086Sdrh } 1246e4de1febSdrh if( iCol<0 && mustComplete ){ 12474adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1248da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1249d8bc7086Sdrh nErr++; 1250d8bc7086Sdrh break; 1251d8bc7086Sdrh } 1252d8bc7086Sdrh } 1253d8bc7086Sdrh return nErr; 1254d8bc7086Sdrh } 125593758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */ 1256d8bc7086Sdrh 1257d8bc7086Sdrh /* 1258d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1259d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1260d8bc7086Sdrh */ 12614adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1262d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1263d8bc7086Sdrh if( v==0 ){ 12644adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1265d8bc7086Sdrh } 1266d8bc7086Sdrh return v; 1267d8bc7086Sdrh } 1268d8bc7086Sdrh 1269d8bc7086Sdrh /* 12707b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 1271a2dc3b1aSdanielk1977 ** pLimit and pOffset expressions. nLimit and nOffset hold the expressions 12727b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 1273a2dc3b1aSdanielk1977 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset 1274a2dc3b1aSdanielk1977 ** are the integer memory register numbers for counters used to compute 1275a2dc3b1aSdanielk1977 ** the limit and offset. If there is no limit and/or offset, then 1276a2dc3b1aSdanielk1977 ** iLimit and iOffset are negative. 12777b58daeaSdrh ** 12787b58daeaSdrh ** This routine changes the values if iLimit and iOffset only if 12797b58daeaSdrh ** a limit or offset is defined by nLimit and nOffset. iLimit and 12807b58daeaSdrh ** iOffset should have been preset to appropriate default values 12817b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 12827b58daeaSdrh ** Only if nLimit>=0 or nOffset>0 do the limit registers get 12837b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 12847b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 12857b58daeaSdrh ** SELECT statements. 12867b58daeaSdrh */ 12877b58daeaSdrh static void computeLimitRegisters(Parse *pParse, Select *p){ 12887b58daeaSdrh /* 12897b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 12907b58daeaSdrh ** contraversy about what the correct behavior should be. 12917b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 12927b58daeaSdrh ** no rows. 12937b58daeaSdrh */ 1294a2dc3b1aSdanielk1977 if( p->pLimit ){ 12957b58daeaSdrh int iMem = pParse->nMem++; 12964adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 12977b58daeaSdrh if( v==0 ) return; 1298a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pLimit); 1299a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1300a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1302ad6d9460Sdrh VdbeComment((v, "# LIMIT counter")); 13037b58daeaSdrh p->iLimit = iMem; 13047b58daeaSdrh } 1305a2dc3b1aSdanielk1977 if( p->pOffset ){ 13067b58daeaSdrh int iMem = pParse->nMem++; 13074adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13087b58daeaSdrh if( v==0 ) return; 1309a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pOffset); 1310a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1311a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1313ad6d9460Sdrh VdbeComment((v, "# OFFSET counter")); 13147b58daeaSdrh p->iOffset = iMem; 13157b58daeaSdrh } 13167b58daeaSdrh } 13177b58daeaSdrh 13187b58daeaSdrh /* 1319d3d39e93Sdrh ** Generate VDBE instructions that will open a transient table that 1320d3d39e93Sdrh ** will be used for an index or to store keyed results for a compound 1321d3d39e93Sdrh ** select. In other words, open a transient table that needs a 1322d3d39e93Sdrh ** KeyInfo structure. The number of columns in the KeyInfo is determined 1323d3d39e93Sdrh ** by the result set of the SELECT statement in the second argument. 1324d3d39e93Sdrh ** 1325dc1bdc4fSdanielk1977 ** Specifically, this routine is called to open an index table for 1326dc1bdc4fSdanielk1977 ** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not 1327dc1bdc4fSdanielk1977 ** UNION ALL). 1328dc1bdc4fSdanielk1977 ** 1329d3d39e93Sdrh ** Make the new table a KeyAsData table if keyAsData is true. 1330dc1bdc4fSdanielk1977 ** 1331dc1bdc4fSdanielk1977 ** The value returned is the address of the OP_OpenTemp instruction. 1332d3d39e93Sdrh */ 1333dc1bdc4fSdanielk1977 static int openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){ 1334d3d39e93Sdrh KeyInfo *pKeyInfo; 1335736c22b8Sdrh int nColumn; 13369bb575fdSdrh sqlite3 *db = pParse->db; 1337d3d39e93Sdrh int i; 1338d3d39e93Sdrh Vdbe *v = pParse->pVdbe; 1339dc1bdc4fSdanielk1977 int addr; 1340d3d39e93Sdrh 13419b3187e1Sdrh if( prepSelectStmt(pParse, p) ){ 1342dc1bdc4fSdanielk1977 return 0; 1343736c22b8Sdrh } 1344736c22b8Sdrh nColumn = p->pEList->nExpr; 1345d3d39e93Sdrh pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) ); 1346dc1bdc4fSdanielk1977 if( pKeyInfo==0 ) return 0; 134791bb0eedSdrh pKeyInfo->enc = db->enc; 1348d3d39e93Sdrh pKeyInfo->nField = nColumn; 1349d3d39e93Sdrh for(i=0; i<nColumn; i++){ 1350dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr); 1351dc1bdc4fSdanielk1977 if( !pKeyInfo->aColl[i] ){ 1352d3d39e93Sdrh pKeyInfo->aColl[i] = db->pDfltColl; 1353d3d39e93Sdrh } 1354dc1bdc4fSdanielk1977 } 1355dc1bdc4fSdanielk1977 addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0, 1356dc1bdc4fSdanielk1977 (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 1357d3d39e93Sdrh if( keyAsData ){ 1358d3d39e93Sdrh sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1); 1359d3d39e93Sdrh } 1360dc1bdc4fSdanielk1977 return addr; 1361dc1bdc4fSdanielk1977 } 1362dc1bdc4fSdanielk1977 1363b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1364fbc4ee7bSdrh /* 13658cdbf836Sdrh ** Add the address "addr" to the set of all OpenTemp opcode addresses 13668cdbf836Sdrh ** that are being accumulated in p->ppOpenTemp. 1367fbc4ee7bSdrh */ 13688cdbf836Sdrh static int multiSelectOpenTempAddr(Select *p, int addr){ 13698cdbf836Sdrh IdList *pList = *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0); 1370fbc4ee7bSdrh if( pList==0 ){ 1371dc1bdc4fSdanielk1977 return SQLITE_NOMEM; 1372dc1bdc4fSdanielk1977 } 1373fbc4ee7bSdrh pList->a[pList->nId-1].idx = addr; 1374dc1bdc4fSdanielk1977 return SQLITE_OK; 1375dc1bdc4fSdanielk1977 } 1376b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1377dc1bdc4fSdanielk1977 1378b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1379fbc4ee7bSdrh /* 1380fbc4ee7bSdrh ** Return the appropriate collating sequence for the iCol-th column of 1381fbc4ee7bSdrh ** the result set for the compound-select statement "p". Return NULL if 1382fbc4ee7bSdrh ** the column has no default collating sequence. 1383fbc4ee7bSdrh ** 1384fbc4ee7bSdrh ** The collating sequence for the compound select is taken from the 1385fbc4ee7bSdrh ** left-most term of the select that has a collating sequence. 1386fbc4ee7bSdrh */ 1387dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ 1388fbc4ee7bSdrh CollSeq *pRet; 1389dc1bdc4fSdanielk1977 if( p->pPrior ){ 1390dc1bdc4fSdanielk1977 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); 1391fbc4ee7bSdrh }else{ 1392fbc4ee7bSdrh pRet = 0; 1393dc1bdc4fSdanielk1977 } 1394fbc4ee7bSdrh if( pRet==0 ){ 1395dc1bdc4fSdanielk1977 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); 1396dc1bdc4fSdanielk1977 } 1397dc1bdc4fSdanielk1977 return pRet; 1398d3d39e93Sdrh } 1399b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1400d3d39e93Sdrh 1401b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1402d3d39e93Sdrh /* 140382c3d636Sdrh ** This routine is called to process a query that is really the union 140482c3d636Sdrh ** or intersection of two or more separate queries. 1405c926afbcSdrh ** 1406e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1407e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1408e78e8284Sdrh ** in which case this routine will be called recursively. 1409e78e8284Sdrh ** 1410e78e8284Sdrh ** The results of the total query are to be written into a destination 1411e78e8284Sdrh ** of type eDest with parameter iParm. 1412e78e8284Sdrh ** 1413e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1414e78e8284Sdrh ** 1415e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1416e78e8284Sdrh ** 1417e78e8284Sdrh ** This statement is parsed up as follows: 1418e78e8284Sdrh ** 1419e78e8284Sdrh ** SELECT c FROM t3 1420e78e8284Sdrh ** | 1421e78e8284Sdrh ** `-----> SELECT b FROM t2 1422e78e8284Sdrh ** | 14234b11c6d3Sjplyon ** `------> SELECT a FROM t1 1424e78e8284Sdrh ** 1425e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1426e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1427e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1428e78e8284Sdrh ** 1429e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1430e78e8284Sdrh ** individual selects always group from left to right. 143182c3d636Sdrh */ 143284ac9d02Sdanielk1977 static int multiSelect( 1433fbc4ee7bSdrh Parse *pParse, /* Parsing context */ 1434fbc4ee7bSdrh Select *p, /* The right-most of SELECTs to be coded */ 1435fbc4ee7bSdrh int eDest, /* \___ Store query results as specified */ 1436fbc4ee7bSdrh int iParm, /* / by these two parameters. */ 143784ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 143884ac9d02Sdanielk1977 ){ 143984ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 144010e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 144110e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 1442fbc4ee7bSdrh IdList *pOpenTemp = 0;/* OP_OpenTemp opcodes that need a KeyInfo */ 14438cdbf836Sdrh int aAddr[5]; /* Addresses of SetNumColumns operators */ 14448cdbf836Sdrh int nAddr = 0; /* Number used */ 14458cdbf836Sdrh int nCol; /* Number of columns in the result set */ 144682c3d636Sdrh 14477b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 1448fbc4ee7bSdrh ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. 144982c3d636Sdrh */ 145084ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 145184ac9d02Sdanielk1977 rc = 1; 145284ac9d02Sdanielk1977 goto multi_select_end; 145384ac9d02Sdanielk1977 } 1454d8bc7086Sdrh pPrior = p->pPrior; 1455d8bc7086Sdrh if( pPrior->pOrderBy ){ 14564adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1457da93d238Sdrh selectOpName(p->op)); 145884ac9d02Sdanielk1977 rc = 1; 145984ac9d02Sdanielk1977 goto multi_select_end; 146082c3d636Sdrh } 1461a2dc3b1aSdanielk1977 if( pPrior->pLimit ){ 14624adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 14637b58daeaSdrh selectOpName(p->op)); 146484ac9d02Sdanielk1977 rc = 1; 146584ac9d02Sdanielk1977 goto multi_select_end; 14667b58daeaSdrh } 146782c3d636Sdrh 1468d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1469d8bc7086Sdrh */ 14704adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 147184ac9d02Sdanielk1977 if( v==0 ){ 147284ac9d02Sdanielk1977 rc = 1; 147384ac9d02Sdanielk1977 goto multi_select_end; 147484ac9d02Sdanielk1977 } 1475d8bc7086Sdrh 14768cdbf836Sdrh /* If *p this is the right-most select statement, then initialize 14778cdbf836Sdrh ** p->ppOpenTemp to point to pOpenTemp. If *p is not the right most 14788cdbf836Sdrh ** statement then p->ppOpenTemp will have already been initialized 14798cdbf836Sdrh ** by a prior call to this same procedure. Pass along the pOpenTemp 14808cdbf836Sdrh ** pointer to pPrior, the next statement to our left. 1481fbc4ee7bSdrh */ 1482fbc4ee7bSdrh if( p->ppOpenTemp==0 ){ 1483fbc4ee7bSdrh p->ppOpenTemp = &pOpenTemp; 1484fbc4ee7bSdrh } 1485fbc4ee7bSdrh pPrior->ppOpenTemp = p->ppOpenTemp; 1486fbc4ee7bSdrh 14871cc3d75fSdrh /* Create the destination temporary table if necessary 14881cc3d75fSdrh */ 14891cc3d75fSdrh if( eDest==SRT_TempTable ){ 1490b4964b72Sdanielk1977 assert( p->pEList ); 14914adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 14928cdbf836Sdrh assert( nAddr==0 ); 14938cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 0); 14941cc3d75fSdrh eDest = SRT_Table; 14951cc3d75fSdrh } 14961cc3d75fSdrh 1497f46f905aSdrh /* Generate code for the left and right SELECT statements. 1498d8bc7086Sdrh */ 149982c3d636Sdrh switch( p->op ){ 1500f46f905aSdrh case TK_ALL: { 1501f46f905aSdrh if( p->pOrderBy==0 ){ 1502a2dc3b1aSdanielk1977 assert( !pPrior->pLimit ); 1503a2dc3b1aSdanielk1977 pPrior->pLimit = p->pLimit; 1504a2dc3b1aSdanielk1977 pPrior->pOffset = p->pOffset; 1505b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 150684ac9d02Sdanielk1977 if( rc ){ 150784ac9d02Sdanielk1977 goto multi_select_end; 150884ac9d02Sdanielk1977 } 1509f46f905aSdrh p->pPrior = 0; 15107b58daeaSdrh p->iLimit = pPrior->iLimit; 15117b58daeaSdrh p->iOffset = pPrior->iOffset; 1512a2dc3b1aSdanielk1977 p->pLimit = 0; 1513a2dc3b1aSdanielk1977 p->pOffset = 0; 1514b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1515f46f905aSdrh p->pPrior = pPrior; 151684ac9d02Sdanielk1977 if( rc ){ 151784ac9d02Sdanielk1977 goto multi_select_end; 151884ac9d02Sdanielk1977 } 1519f46f905aSdrh break; 1520f46f905aSdrh } 1521f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1522f46f905aSdrh } 152382c3d636Sdrh case TK_EXCEPT: 152482c3d636Sdrh case TK_UNION: { 1525d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1526742f947bSdanielk1977 int op = 0; /* One of the SRT_ operations to apply to self */ 1527d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1528a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ 1529c926afbcSdrh ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ 1530dc1bdc4fSdanielk1977 int addr; 153182c3d636Sdrh 1532d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 1533a2dc3b1aSdanielk1977 if( eDest==priorOp && p->pOrderBy==0 && !p->pLimit && !p->pOffset ){ 1534d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1535c926afbcSdrh ** right. 1536d8bc7086Sdrh */ 153782c3d636Sdrh unionTab = iParm; 153882c3d636Sdrh }else{ 1539d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1540d8bc7086Sdrh ** intermediate results. 1541d8bc7086Sdrh */ 154282c3d636Sdrh unionTab = pParse->nTab++; 1543d8bc7086Sdrh if( p->pOrderBy 1544d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 154584ac9d02Sdanielk1977 rc = 1; 154684ac9d02Sdanielk1977 goto multi_select_end; 1547d8bc7086Sdrh } 1548dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0); 1549d8bc7086Sdrh if( p->op!=TK_ALL ){ 15508cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1551dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1552dc1bdc4fSdanielk1977 goto multi_select_end; 1553dc1bdc4fSdanielk1977 } 1554dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, unionTab, 1); 155582c3d636Sdrh } 15568cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 15578cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, 0); 155884ac9d02Sdanielk1977 assert( p->pEList ); 1559d8bc7086Sdrh } 1560d8bc7086Sdrh 1561d8bc7086Sdrh /* Code the SELECT statements to our left 1562d8bc7086Sdrh */ 1563b3bce662Sdanielk1977 assert( !pPrior->pOrderBy ); 1564b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 156584ac9d02Sdanielk1977 if( rc ){ 156684ac9d02Sdanielk1977 goto multi_select_end; 156784ac9d02Sdanielk1977 } 1568d8bc7086Sdrh 1569d8bc7086Sdrh /* Code the current SELECT statement 1570d8bc7086Sdrh */ 1571d8bc7086Sdrh switch( p->op ){ 1572d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1573d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1574d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1575d8bc7086Sdrh } 157682c3d636Sdrh p->pPrior = 0; 1577c926afbcSdrh pOrderBy = p->pOrderBy; 1578c926afbcSdrh p->pOrderBy = 0; 1579a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1580a2dc3b1aSdanielk1977 p->pLimit = 0; 1581a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1582a2dc3b1aSdanielk1977 p->pOffset = 0; 1583b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 158482c3d636Sdrh p->pPrior = pPrior; 1585c926afbcSdrh p->pOrderBy = pOrderBy; 1586a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1587a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1588a2dc3b1aSdanielk1977 p->pOffset = pOffset; 1589be5fd490Sdrh p->iLimit = -1; 1590be5fd490Sdrh p->iOffset = -1; 159184ac9d02Sdanielk1977 if( rc ){ 159284ac9d02Sdanielk1977 goto multi_select_end; 159384ac9d02Sdanielk1977 } 159484ac9d02Sdanielk1977 1595d8bc7086Sdrh 1596d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1597d8bc7086Sdrh ** it is that we currently need. 1598d8bc7086Sdrh */ 1599c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 16006b56344dSdrh int iCont, iBreak, iStart; 160182c3d636Sdrh assert( p->pEList ); 160241202ccaSdrh if( eDest==SRT_Callback ){ 16036a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 160441202ccaSdrh } 16054adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 16064adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 16074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 16087b58daeaSdrh computeLimitRegisters(pParse, p); 16094adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 161038640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 1611d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 161284ac9d02Sdanielk1977 iCont, iBreak, 0); 161384ac9d02Sdanielk1977 if( rc ){ 161484ac9d02Sdanielk1977 rc = 1; 161584ac9d02Sdanielk1977 goto multi_select_end; 161684ac9d02Sdanielk1977 } 16174adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 16184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 16194adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 16204adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 162182c3d636Sdrh } 162282c3d636Sdrh break; 162382c3d636Sdrh } 162482c3d636Sdrh case TK_INTERSECT: { 162582c3d636Sdrh int tab1, tab2; 16266b56344dSdrh int iCont, iBreak, iStart; 1627a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; 1628dc1bdc4fSdanielk1977 int addr; 162982c3d636Sdrh 1630d8bc7086Sdrh /* INTERSECT is different from the others since it requires 16316206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1632d8bc7086Sdrh ** by allocating the tables we will need. 1633d8bc7086Sdrh */ 163482c3d636Sdrh tab1 = pParse->nTab++; 163582c3d636Sdrh tab2 = pParse->nTab++; 1636d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 163784ac9d02Sdanielk1977 rc = 1; 163884ac9d02Sdanielk1977 goto multi_select_end; 1639d8bc7086Sdrh } 1640dc1bdc4fSdanielk1977 1641dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab1, 0); 16428cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1643dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1644dc1bdc4fSdanielk1977 goto multi_select_end; 1645dc1bdc4fSdanielk1977 } 1646dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, tab1, 1); 16478cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 16488cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab1, 0); 164984ac9d02Sdanielk1977 assert( p->pEList ); 1650d8bc7086Sdrh 1651d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1652d8bc7086Sdrh */ 1653b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 165484ac9d02Sdanielk1977 if( rc ){ 165584ac9d02Sdanielk1977 goto multi_select_end; 165684ac9d02Sdanielk1977 } 1657d8bc7086Sdrh 1658d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1659d8bc7086Sdrh */ 1660dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab2, 0); 16618cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1662dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1663dc1bdc4fSdanielk1977 goto multi_select_end; 1664dc1bdc4fSdanielk1977 } 1665dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, tab2, 1); 16668cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 16678cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab2, 0); 166882c3d636Sdrh p->pPrior = 0; 1669a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1670a2dc3b1aSdanielk1977 p->pLimit = 0; 1671a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1672a2dc3b1aSdanielk1977 p->pOffset = 0; 1673b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 167482c3d636Sdrh p->pPrior = pPrior; 1675a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1676a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1677a2dc3b1aSdanielk1977 p->pOffset = pOffset; 167884ac9d02Sdanielk1977 if( rc ){ 167984ac9d02Sdanielk1977 goto multi_select_end; 168084ac9d02Sdanielk1977 } 1681d8bc7086Sdrh 1682d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1683d8bc7086Sdrh ** tables. 1684d8bc7086Sdrh */ 168582c3d636Sdrh assert( p->pEList ); 168641202ccaSdrh if( eDest==SRT_Callback ){ 16876a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 168841202ccaSdrh } 16894adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 16904adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 16914adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 16927b58daeaSdrh computeLimitRegisters(pParse, p); 16934adee20fSdanielk1977 iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0); 16944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 169538640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 1696d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 169784ac9d02Sdanielk1977 iCont, iBreak, 0); 169884ac9d02Sdanielk1977 if( rc ){ 169984ac9d02Sdanielk1977 rc = 1; 170084ac9d02Sdanielk1977 goto multi_select_end; 170184ac9d02Sdanielk1977 } 17024adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 17034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 17044adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 17054adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 17064adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 170782c3d636Sdrh break; 170882c3d636Sdrh } 170982c3d636Sdrh } 17108cdbf836Sdrh 17118cdbf836Sdrh /* Make sure all SELECTs in the statement have the same number of elements 17128cdbf836Sdrh ** in their result sets. 17138cdbf836Sdrh */ 171482c3d636Sdrh assert( p->pEList && pPrior->pEList ); 171582c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 17164adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1717da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 171884ac9d02Sdanielk1977 rc = 1; 171984ac9d02Sdanielk1977 goto multi_select_end; 17202282792aSdrh } 172184ac9d02Sdanielk1977 17228cdbf836Sdrh /* Set the number of columns in temporary tables 17238cdbf836Sdrh */ 17248cdbf836Sdrh nCol = p->pEList->nExpr; 17258cdbf836Sdrh while( nAddr>0 ){ 17268cdbf836Sdrh nAddr--; 17278cdbf836Sdrh sqlite3VdbeChangeP2(v, aAddr[nAddr], nCol); 17288cdbf836Sdrh } 17298cdbf836Sdrh 1730fbc4ee7bSdrh /* Compute collating sequences used by either the ORDER BY clause or 1731fbc4ee7bSdrh ** by any temporary tables needed to implement the compound select. 1732fbc4ee7bSdrh ** Attach the KeyInfo structure to all temporary tables. Invoke the 1733fbc4ee7bSdrh ** ORDER BY processing if there is an ORDER BY clause. 17348cdbf836Sdrh ** 17358cdbf836Sdrh ** This section is run by the right-most SELECT statement only. 17368cdbf836Sdrh ** SELECT statements to the left always skip this part. The right-most 17378cdbf836Sdrh ** SELECT might also skip this part if it has no ORDER BY clause and 17388cdbf836Sdrh ** no temp tables are required. 1739fbc4ee7bSdrh */ 1740dc1bdc4fSdanielk1977 if( p->pOrderBy || (pOpenTemp && pOpenTemp->nId>0) ){ 1741fbc4ee7bSdrh int i; /* Loop counter */ 1742fbc4ee7bSdrh KeyInfo *pKeyInfo; /* Collating sequence for the result set */ 1743fbc4ee7bSdrh 17448cdbf836Sdrh assert( p->ppOpenTemp == &pOpenTemp ); 1745fbc4ee7bSdrh pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*sizeof(CollSeq*)); 1746dc1bdc4fSdanielk1977 if( !pKeyInfo ){ 1747dc1bdc4fSdanielk1977 rc = SQLITE_NOMEM; 1748dc1bdc4fSdanielk1977 goto multi_select_end; 1749dc1bdc4fSdanielk1977 } 1750dc1bdc4fSdanielk1977 1751dc1bdc4fSdanielk1977 pKeyInfo->enc = pParse->db->enc; 1752dc1bdc4fSdanielk1977 pKeyInfo->nField = nCol; 1753dc1bdc4fSdanielk1977 1754dc1bdc4fSdanielk1977 for(i=0; i<nCol; i++){ 1755dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = multiSelectCollSeq(pParse, p, i); 1756dc1bdc4fSdanielk1977 if( !pKeyInfo->aColl[i] ){ 1757dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = pParse->db->pDfltColl; 1758dc1bdc4fSdanielk1977 } 1759dc1bdc4fSdanielk1977 } 1760dc1bdc4fSdanielk1977 1761dc1bdc4fSdanielk1977 for(i=0; pOpenTemp && i<pOpenTemp->nId; i++){ 1762dc1bdc4fSdanielk1977 int p3type = (i==0?P3_KEYINFO_HANDOFF:P3_KEYINFO); 1763dc1bdc4fSdanielk1977 int addr = pOpenTemp->a[i].idx; 1764dc1bdc4fSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKeyInfo, p3type); 1765dc1bdc4fSdanielk1977 } 1766dc1bdc4fSdanielk1977 1767dc1bdc4fSdanielk1977 if( p->pOrderBy ){ 1768fbc4ee7bSdrh struct ExprList_item *pOrderByTerm = p->pOrderBy->a; 1769fbc4ee7bSdrh for(i=0; i<p->pOrderBy->nExpr; i++, pOrderByTerm++){ 1770fbc4ee7bSdrh Expr *pExpr = pOrderByTerm->pExpr; 1771fbc4ee7bSdrh char *zName = pOrderByTerm->zName; 1772dc1bdc4fSdanielk1977 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); 1773b3bce662Sdanielk1977 /* assert( !pExpr->pColl ); */ 1774dc1bdc4fSdanielk1977 if( zName ){ 1775dc1bdc4fSdanielk1977 pExpr->pColl = sqlite3LocateCollSeq(pParse, zName, -1); 177684ac9d02Sdanielk1977 }else{ 1777dc1bdc4fSdanielk1977 pExpr->pColl = pKeyInfo->aColl[pExpr->iColumn]; 177884ac9d02Sdanielk1977 } 177984ac9d02Sdanielk1977 } 1780dc1bdc4fSdanielk1977 generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1781dc1bdc4fSdanielk1977 } 1782dc1bdc4fSdanielk1977 1783dc1bdc4fSdanielk1977 if( !pOpenTemp ){ 1784dc1bdc4fSdanielk1977 /* This happens for UNION ALL ... ORDER BY */ 1785dc1bdc4fSdanielk1977 sqliteFree(pKeyInfo); 1786dc1bdc4fSdanielk1977 } 1787dc1bdc4fSdanielk1977 } 1788dc1bdc4fSdanielk1977 1789dc1bdc4fSdanielk1977 multi_select_end: 1790dc1bdc4fSdanielk1977 if( pOpenTemp ){ 1791dc1bdc4fSdanielk1977 sqlite3IdListDelete(pOpenTemp); 1792dc1bdc4fSdanielk1977 } 1793dc1bdc4fSdanielk1977 p->ppOpenTemp = 0; 179484ac9d02Sdanielk1977 return rc; 17952282792aSdrh } 1796b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 17972282792aSdrh 1798b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 17992282792aSdrh /* 1800832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 18016a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 180284e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 18036a3ea0e6Sdrh ** unchanged.) 1804832508b7Sdrh ** 1805832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1806832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1807832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1808832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1809832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1810832508b7Sdrh ** of the subquery rather the result set of the subquery. 1811832508b7Sdrh */ 18126a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 1813b3bce662Sdanielk1977 static void substSelect(Select *, int, ExprList *); /* Forward Decl */ 18146a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1815832508b7Sdrh if( pExpr==0 ) return; 181650350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 181750350a15Sdrh if( pExpr->iColumn<0 ){ 181850350a15Sdrh pExpr->op = TK_NULL; 181950350a15Sdrh }else{ 1820832508b7Sdrh Expr *pNew; 182184e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 1822832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 1823832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 1824832508b7Sdrh assert( pNew!=0 ); 1825832508b7Sdrh pExpr->op = pNew->op; 1826d94a6698Sdrh assert( pExpr->pLeft==0 ); 18274adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 1828d94a6698Sdrh assert( pExpr->pRight==0 ); 18294adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 1830d94a6698Sdrh assert( pExpr->pList==0 ); 18314adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 1832832508b7Sdrh pExpr->iTable = pNew->iTable; 1833832508b7Sdrh pExpr->iColumn = pNew->iColumn; 1834832508b7Sdrh pExpr->iAgg = pNew->iAgg; 18354adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 18364adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 1837a1cb183dSdanielk1977 pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); 1838a1cb183dSdanielk1977 pExpr->flags = pNew->flags; 183950350a15Sdrh } 1840832508b7Sdrh }else{ 18416a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 18426a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 1843b3bce662Sdanielk1977 substSelect(pExpr->pSelect, iTable, pEList); 18446a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 1845832508b7Sdrh } 1846832508b7Sdrh } 1847b3bce662Sdanielk1977 static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ 1848832508b7Sdrh int i; 1849832508b7Sdrh if( pList==0 ) return; 1850832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 18516a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 1852832508b7Sdrh } 1853832508b7Sdrh } 1854b3bce662Sdanielk1977 static void substSelect(Select *p, int iTable, ExprList *pEList){ 1855b3bce662Sdanielk1977 if( !p ) return; 1856b3bce662Sdanielk1977 substExprList(p->pEList, iTable, pEList); 1857b3bce662Sdanielk1977 substExprList(p->pGroupBy, iTable, pEList); 1858b3bce662Sdanielk1977 substExprList(p->pOrderBy, iTable, pEList); 1859b3bce662Sdanielk1977 substExpr(p->pHaving, iTable, pEList); 1860b3bce662Sdanielk1977 substExpr(p->pWhere, iTable, pEList); 1861b3bce662Sdanielk1977 } 1862b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_VIEW) */ 1863832508b7Sdrh 1864b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 1865832508b7Sdrh /* 18661350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 18671350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 18681350b030Sdrh ** occurs. 18691350b030Sdrh ** 18701350b030Sdrh ** To understand the concept of flattening, consider the following 18711350b030Sdrh ** query: 18721350b030Sdrh ** 18731350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 18741350b030Sdrh ** 18751350b030Sdrh ** The default way of implementing this query is to execute the 18761350b030Sdrh ** subquery first and store the results in a temporary table, then 18771350b030Sdrh ** run the outer query on that temporary table. This requires two 18781350b030Sdrh ** passes over the data. Furthermore, because the temporary table 18791350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 1880832508b7Sdrh ** optimized. 18811350b030Sdrh ** 1882832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 18831350b030Sdrh ** a single flat select, like this: 18841350b030Sdrh ** 18851350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 18861350b030Sdrh ** 18871350b030Sdrh ** The code generated for this simpification gives the same result 1888832508b7Sdrh ** but only has to scan the data once. And because indices might 1889832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 1890832508b7Sdrh ** avoided. 18911350b030Sdrh ** 1892832508b7Sdrh ** Flattening is only attempted if all of the following are true: 18931350b030Sdrh ** 1894832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 18951350b030Sdrh ** 1896832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 1897832508b7Sdrh ** 18988af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 18998af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 1900832508b7Sdrh ** 1901832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 1902832508b7Sdrh ** 1903832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 1904832508b7Sdrh ** aggregates. 1905832508b7Sdrh ** 1906832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 1907832508b7Sdrh ** DISTINCT. 1908832508b7Sdrh ** 190908192d5fSdrh ** (7) The subquery has a FROM clause. 191008192d5fSdrh ** 1911df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 1912df199a25Sdrh ** 1913df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 1914df199a25Sdrh ** aggregates. 1915df199a25Sdrh ** 1916df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 1917df199a25Sdrh ** use LIMIT. 1918df199a25Sdrh ** 1919174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 1920174b6195Sdrh ** 19213fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 19223fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 19233fc673e6Sdrh ** 1924832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 1925832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 1926832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 1927832508b7Sdrh ** 1928665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 1929832508b7Sdrh ** If flattening is attempted this routine returns 1. 1930832508b7Sdrh ** 1931832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 1932832508b7Sdrh ** the subquery before this routine runs. 19331350b030Sdrh */ 19348c74a8caSdrh static int flattenSubquery( 19358c74a8caSdrh Parse *pParse, /* The parsing context */ 19368c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 19378c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 19388c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 19398c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 19408c74a8caSdrh ){ 19410bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 1942ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 1943ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 19440bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 19456a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 194691bb0eedSdrh int i; /* Loop counter */ 194791bb0eedSdrh Expr *pWhere; /* The WHERE clause */ 194891bb0eedSdrh struct SrcList_item *pSubitem; /* The subquery */ 19491350b030Sdrh 1950832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 1951832508b7Sdrh */ 1952832508b7Sdrh if( p==0 ) return 0; 1953832508b7Sdrh pSrc = p->pSrc; 1954ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 195591bb0eedSdrh pSubitem = &pSrc->a[iFrom]; 195691bb0eedSdrh pSub = pSubitem->pSelect; 1957832508b7Sdrh assert( pSub!=0 ); 1958832508b7Sdrh if( isAgg && subqueryIsAgg ) return 0; 1959ad3cab52Sdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; 1960832508b7Sdrh pSubSrc = pSub->pSrc; 1961832508b7Sdrh assert( pSubSrc ); 1962a2dc3b1aSdanielk1977 if( (pSub->pLimit && p->pLimit) || pSub->pOffset || 1963a2dc3b1aSdanielk1977 (pSub->pLimit && isAgg) ) return 0; 1964c31c2eb8Sdrh if( pSubSrc->nSrc==0 ) return 0; 1965a2dc3b1aSdanielk1977 if( pSub->isDistinct && (pSrc->nSrc>1 || isAgg) ){ 1966df199a25Sdrh return 0; 1967df199a25Sdrh } 1968a2dc3b1aSdanielk1977 if( p->isDistinct && subqueryIsAgg ) return 0; 1969174b6195Sdrh if( p->pOrderBy && pSub->pOrderBy ) return 0; 1970832508b7Sdrh 19718af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 19728af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 19738af4d3acSdrh ** is not allowed: 19748af4d3acSdrh ** 19758af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 19768af4d3acSdrh ** 19778af4d3acSdrh ** If we flatten the above, we would get 19788af4d3acSdrh ** 19798af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 19808af4d3acSdrh ** 19818af4d3acSdrh ** which is not at all the same thing. 19828af4d3acSdrh */ 19838af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 19848af4d3acSdrh return 0; 19858af4d3acSdrh } 19868af4d3acSdrh 19873fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 19883fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 19893fc673e6Sdrh ** An examples of why this is not allowed: 19903fc673e6Sdrh ** 19913fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 19923fc673e6Sdrh ** 19933fc673e6Sdrh ** If we flatten the above, we would get 19943fc673e6Sdrh ** 19953fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 19963fc673e6Sdrh ** 19973fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 19983fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 19993fc673e6Sdrh */ 20003fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 20013fc673e6Sdrh && pSub->pWhere!=0 ){ 20023fc673e6Sdrh return 0; 20033fc673e6Sdrh } 20043fc673e6Sdrh 20050bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 200663eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 2007832508b7Sdrh */ 2008c31c2eb8Sdrh 2009c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 2010c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 2011c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 2012c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 2013c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 2014c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 2015c31c2eb8Sdrh ** elements we are now copying in. 2016c31c2eb8Sdrh */ 201791bb0eedSdrh iParent = pSubitem->iCursor; 2018c31c2eb8Sdrh { 2019c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 202091bb0eedSdrh int jointype = pSubitem->jointype; 2021c31c2eb8Sdrh 202291bb0eedSdrh sqlite3DeleteTable(0, pSubitem->pTab); 202391bb0eedSdrh sqliteFree(pSubitem->zDatabase); 202491bb0eedSdrh sqliteFree(pSubitem->zName); 202591bb0eedSdrh sqliteFree(pSubitem->zAlias); 2026c31c2eb8Sdrh if( nSubSrc>1 ){ 2027c31c2eb8Sdrh int extra = nSubSrc - 1; 2028c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 20294adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 2030c31c2eb8Sdrh } 2031c31c2eb8Sdrh p->pSrc = pSrc; 2032c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 2033c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 2034c31c2eb8Sdrh } 2035c31c2eb8Sdrh } 2036c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 2037c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 2038c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 2039c31c2eb8Sdrh } 20408af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 2041c31c2eb8Sdrh } 2042c31c2eb8Sdrh 2043c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 2044c31c2eb8Sdrh ** references to the iParent in the outer query. 2045c31c2eb8Sdrh ** 2046c31c2eb8Sdrh ** Example: 2047c31c2eb8Sdrh ** 2048c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 2049c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 2050c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 2051c31c2eb8Sdrh ** 2052c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 2053c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 2054c31c2eb8Sdrh */ 20556a3ea0e6Sdrh substExprList(p->pEList, iParent, pSub->pEList); 2056832508b7Sdrh pList = p->pEList; 2057832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 20586977fea8Sdrh Expr *pExpr; 20596977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 20606977fea8Sdrh pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); 2061832508b7Sdrh } 2062832508b7Sdrh } 20631b2e0329Sdrh if( isAgg ){ 20646a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 20656a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 20661b2e0329Sdrh } 2067174b6195Sdrh if( pSub->pOrderBy ){ 2068174b6195Sdrh assert( p->pOrderBy==0 ); 2069174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 2070174b6195Sdrh pSub->pOrderBy = 0; 2071174b6195Sdrh }else if( p->pOrderBy ){ 20726a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 2073174b6195Sdrh } 2074832508b7Sdrh if( pSub->pWhere ){ 20754adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 2076832508b7Sdrh }else{ 2077832508b7Sdrh pWhere = 0; 2078832508b7Sdrh } 2079832508b7Sdrh if( subqueryIsAgg ){ 2080832508b7Sdrh assert( p->pHaving==0 ); 20811b2e0329Sdrh p->pHaving = p->pWhere; 20821b2e0329Sdrh p->pWhere = pWhere; 20836a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 208491bb0eedSdrh p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); 20851b2e0329Sdrh assert( p->pGroupBy==0 ); 20864adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 2087832508b7Sdrh }else{ 20886a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 208991bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); 2090832508b7Sdrh } 2091c31c2eb8Sdrh 2092c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 2093c31c2eb8Sdrh ** outer query is distinct. 2094c31c2eb8Sdrh */ 2095832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 20968c74a8caSdrh 2097a58fdfb1Sdanielk1977 /* 2098a58fdfb1Sdanielk1977 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; 2099a58fdfb1Sdanielk1977 */ 2100a2dc3b1aSdanielk1977 if( pSub->pLimit ){ 2101a2dc3b1aSdanielk1977 p->pLimit = pSub->pLimit; 2102a2dc3b1aSdanielk1977 pSub->pLimit = 0; 2103df199a25Sdrh } 21048c74a8caSdrh 2105c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 2106c31c2eb8Sdrh ** success. 2107c31c2eb8Sdrh */ 21084adee20fSdanielk1977 sqlite3SelectDelete(pSub); 2109832508b7Sdrh return 1; 21101350b030Sdrh } 2111b7f9164eSdrh #endif /* SQLITE_OMIT_VIEW */ 21121350b030Sdrh 21131350b030Sdrh /* 21149562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 21159562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 21169562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 2117e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 21189562b551Sdrh ** simple min() or max() query, then return 0; 21199562b551Sdrh ** 21209562b551Sdrh ** A simply min() or max() query looks like this: 21219562b551Sdrh ** 21229562b551Sdrh ** SELECT min(a) FROM table; 21239562b551Sdrh ** SELECT max(a) FROM table; 21249562b551Sdrh ** 21259562b551Sdrh ** The query may have only a single table in its FROM argument. There 21269562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 21279562b551Sdrh ** be the min() or max() of a single column of the table. The column 21289562b551Sdrh ** in the min() or max() function must be indexed. 21299562b551Sdrh ** 21304adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 21319562b551Sdrh ** See the header comment on that routine for additional information. 21329562b551Sdrh */ 21339562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 21349562b551Sdrh Expr *pExpr; 21359562b551Sdrh int iCol; 21369562b551Sdrh Table *pTab; 21379562b551Sdrh Index *pIdx; 21389562b551Sdrh int base; 21399562b551Sdrh Vdbe *v; 21409562b551Sdrh int seekOp; 21419562b551Sdrh int cont; 21426e17529eSdrh ExprList *pEList, *pList, eList; 21439562b551Sdrh struct ExprList_item eListItem; 21446e17529eSdrh SrcList *pSrc; 21456e17529eSdrh 21469562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 21479562b551Sdrh ** zero if it is not. 21489562b551Sdrh */ 21499562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 21506e17529eSdrh pSrc = p->pSrc; 21516e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 21526e17529eSdrh pEList = p->pEList; 21536e17529eSdrh if( pEList->nExpr!=1 ) return 0; 21546e17529eSdrh pExpr = pEList->a[0].pExpr; 21559562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 21566e17529eSdrh pList = pExpr->pList; 21576e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 21586977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 21594adee20fSdanielk1977 if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){ 21600bce8354Sdrh seekOp = OP_Rewind; 21614adee20fSdanielk1977 }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){ 21620bce8354Sdrh seekOp = OP_Last; 21630bce8354Sdrh }else{ 21640bce8354Sdrh return 0; 21650bce8354Sdrh } 21666e17529eSdrh pExpr = pList->a[0].pExpr; 21679562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 21689562b551Sdrh iCol = pExpr->iColumn; 21696e17529eSdrh pTab = pSrc->a[0].pTab; 21709562b551Sdrh 21719562b551Sdrh /* If we get to here, it means the query is of the correct form. 217217f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 217317f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 217417f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 217517f71934Sdrh ** usable index is found, return 0. 21769562b551Sdrh */ 21779562b551Sdrh if( iCol<0 ){ 21789562b551Sdrh pIdx = 0; 21799562b551Sdrh }else{ 2180dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 21819562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 21829562b551Sdrh assert( pIdx->nColumn>=1 ); 2183dc1bdc4fSdanielk1977 if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break; 21849562b551Sdrh } 21859562b551Sdrh if( pIdx==0 ) return 0; 21869562b551Sdrh } 21879562b551Sdrh 2188e5f50722Sdrh /* Identify column types if we will be using the callback. This 21899562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 2190e5f50722Sdrh ** The column names have already been generated in the calling function. 21919562b551Sdrh */ 21924adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 21939562b551Sdrh if( v==0 ) return 0; 21949562b551Sdrh 21950c37e630Sdrh /* If the output is destined for a temporary table, open that table. 21960c37e630Sdrh */ 21970c37e630Sdrh if( eDest==SRT_TempTable ){ 21984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2199b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1); 22000c37e630Sdrh } 22010c37e630Sdrh 220217f71934Sdrh /* Generating code to find the min or the max. Basically all we have 220317f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 220417f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 220517f71934Sdrh ** or last entry in the main table. 22069562b551Sdrh */ 22074adee20fSdanielk1977 sqlite3CodeVerifySchema(pParse, pTab->iDb); 22086e17529eSdrh base = pSrc->a[0].iCursor; 22097b58daeaSdrh computeLimitRegisters(pParse, p); 22106e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 2211ad6d9460Sdrh sqlite3OpenTableForReading(v, base, pTab); 22126e17529eSdrh } 22134adee20fSdanielk1977 cont = sqlite3VdbeMakeLabel(v); 22149562b551Sdrh if( pIdx==0 ){ 22154adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 22169562b551Sdrh }else{ 22173719d7f9Sdanielk1977 /* Even though the cursor used to open the index here is closed 22183719d7f9Sdanielk1977 ** as soon as a single value has been read from it, allocate it 22193719d7f9Sdanielk1977 ** using (pParse->nTab++) to prevent the cursor id from being 22203719d7f9Sdanielk1977 ** reused. This is important for statements of the form 22213719d7f9Sdanielk1977 ** "INSERT INTO x SELECT max() FROM x". 22223719d7f9Sdanielk1977 */ 22233719d7f9Sdanielk1977 int iIdx; 22243719d7f9Sdanielk1977 iIdx = pParse->nTab++; 22254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 22263719d7f9Sdanielk1977 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 2227d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 22289eb516c0Sdrh if( seekOp==OP_Rewind ){ 22291af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_String, 0, 0); 22301af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); 22311af3fdb4Sdrh seekOp = OP_MoveGt; 22329eb516c0Sdrh } 22333719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, seekOp, iIdx, 0); 22343719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_IdxRecno, iIdx, 0); 22353719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); 22367cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 22379562b551Sdrh } 22385cf8e8c7Sdrh eList.nExpr = 1; 22395cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 22405cf8e8c7Sdrh eList.a = &eListItem; 22415cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 224284ac9d02Sdanielk1977 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0); 22434adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, cont); 22444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 22456e17529eSdrh 22469562b551Sdrh return 1; 22479562b551Sdrh } 22489562b551Sdrh 22499562b551Sdrh /* 2250290c1948Sdrh ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return 2251290c1948Sdrh ** the number of errors seen. 2252290c1948Sdrh ** 2253290c1948Sdrh ** An ORDER BY or GROUP BY is a list of expressions. If any expression 2254290c1948Sdrh ** is an integer constant, then that expression is replaced by the 2255290c1948Sdrh ** corresponding entry in the result set. 2256290c1948Sdrh */ 2257290c1948Sdrh static int processOrderGroupBy( 2258b3bce662Sdanielk1977 NameContext *pNC, /* Name context of the SELECT statement. */ 2259290c1948Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 2260290c1948Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 2261290c1948Sdrh ){ 2262290c1948Sdrh int i; 2263b3bce662Sdanielk1977 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */ 2264b3bce662Sdanielk1977 Parse *pParse = pNC->pParse; /* The result set of the SELECT */ 2265b3bce662Sdanielk1977 assert( pEList ); 2266b3bce662Sdanielk1977 2267290c1948Sdrh if( pOrderBy==0 ) return 0; 2268290c1948Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2269290c1948Sdrh int iCol; 2270290c1948Sdrh Expr *pE = pOrderBy->a[i].pExpr; 2271b3bce662Sdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 2272b3bce662Sdanielk1977 if( iCol>0 && iCol<=pEList->nExpr ){ 2273290c1948Sdrh sqlite3ExprDelete(pE); 2274290c1948Sdrh pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 2275b3bce662Sdanielk1977 }else{ 2276290c1948Sdrh sqlite3ErrorMsg(pParse, 2277290c1948Sdrh "%s BY column number %d out of range - should be " 2278290c1948Sdrh "between 1 and %d", zType, iCol, pEList->nExpr); 2279290c1948Sdrh return 1; 2280290c1948Sdrh } 2281290c1948Sdrh } 2282b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(pNC, pE) ){ 2283b3bce662Sdanielk1977 return 1; 2284b3bce662Sdanielk1977 } 2285b3bce662Sdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 2286b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, 2287b3bce662Sdanielk1977 "%s BY terms must not be non-integer constants", zType); 2288b3bce662Sdanielk1977 return 1; 2289b3bce662Sdanielk1977 } 2290290c1948Sdrh } 2291290c1948Sdrh return 0; 2292290c1948Sdrh } 2293290c1948Sdrh 2294290c1948Sdrh /* 2295b3bce662Sdanielk1977 ** This routine resolves any names used in the result set of the 2296b3bce662Sdanielk1977 ** supplied SELECT statement. If the SELECT statement being resolved 2297b3bce662Sdanielk1977 ** is a sub-select, then pOuterNC is a pointer to the NameContext 2298b3bce662Sdanielk1977 ** of the parent SELECT. 2299b3bce662Sdanielk1977 */ 2300b3bce662Sdanielk1977 int sqlite3SelectResolve( 2301b3bce662Sdanielk1977 Parse *pParse, /* The parser context */ 2302b3bce662Sdanielk1977 Select *p, /* The SELECT statement being coded. */ 2303b3bce662Sdanielk1977 NameContext *pOuterNC /* The outer name context. May be NULL. */ 2304b3bce662Sdanielk1977 ){ 2305b3bce662Sdanielk1977 ExprList *pEList; /* Result set. */ 2306b3bce662Sdanielk1977 int i; /* For-loop variable used in multiple places */ 2307b3bce662Sdanielk1977 NameContext sNC; /* Local name-context */ 2308b3bce662Sdanielk1977 2309b3bce662Sdanielk1977 /* If this routine has run before, return immediately. */ 2310b3bce662Sdanielk1977 if( p->isResolved ){ 2311b3bce662Sdanielk1977 assert( !pOuterNC ); 2312b3bce662Sdanielk1977 return SQLITE_OK; 2313b3bce662Sdanielk1977 } 2314b3bce662Sdanielk1977 p->isResolved = 1; 2315b3bce662Sdanielk1977 2316b3bce662Sdanielk1977 /* If there have already been errors, do nothing. */ 2317b3bce662Sdanielk1977 if( pParse->nErr>0 ){ 2318b3bce662Sdanielk1977 return SQLITE_ERROR; 2319b3bce662Sdanielk1977 } 2320b3bce662Sdanielk1977 2321b3bce662Sdanielk1977 /* Prepare the select statement. This call will allocate all cursors 2322b3bce662Sdanielk1977 ** required to handle the tables and subqueries in the FROM clause. 2323b3bce662Sdanielk1977 */ 2324b3bce662Sdanielk1977 if( prepSelectStmt(pParse, p) ){ 2325b3bce662Sdanielk1977 return SQLITE_ERROR; 2326b3bce662Sdanielk1977 } 2327b3bce662Sdanielk1977 2328a2dc3b1aSdanielk1977 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 2329a2dc3b1aSdanielk1977 ** are not allowed to refer to any names, so pass an empty NameContext. 2330a2dc3b1aSdanielk1977 */ 2331b3bce662Sdanielk1977 sNC.pParse = pParse; 2332b3bce662Sdanielk1977 sNC.hasAgg = 0; 2333b3bce662Sdanielk1977 sNC.nErr = 0; 2334b3bce662Sdanielk1977 sNC.nRef = 0; 2335b3bce662Sdanielk1977 sNC.pEList = 0; 2336a2dc3b1aSdanielk1977 sNC.allowAgg = 0; 2337a2dc3b1aSdanielk1977 sNC.pSrcList = 0; 2338a2dc3b1aSdanielk1977 sNC.pNext = 0; 2339a2dc3b1aSdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pLimit) || 2340a2dc3b1aSdanielk1977 sqlite3ExprResolveNames(&sNC, p->pOffset) ){ 2341a2dc3b1aSdanielk1977 return SQLITE_ERROR; 2342a2dc3b1aSdanielk1977 } 2343a2dc3b1aSdanielk1977 2344a2dc3b1aSdanielk1977 /* Set up the local name-context to pass to ExprResolveNames() to 2345a2dc3b1aSdanielk1977 ** resolve the expression-list. 2346a2dc3b1aSdanielk1977 */ 2347a2dc3b1aSdanielk1977 sNC.allowAgg = 1; 2348a2dc3b1aSdanielk1977 sNC.pSrcList = p->pSrc; 2349a2dc3b1aSdanielk1977 sNC.pNext = pOuterNC; 2350b3bce662Sdanielk1977 2351b3bce662Sdanielk1977 /* NameContext.nDepth stores the depth of recursion for this query. For 2352b3bce662Sdanielk1977 ** an outer query (e.g. SELECT * FROM sqlite_master) this is 1. For 2353b3bce662Sdanielk1977 ** a subquery it is 2. For a subquery of a subquery, 3. And so on. 2354b3bce662Sdanielk1977 ** Parse.nMaxDepth is the maximum depth for any subquery resolved so 2355b3bce662Sdanielk1977 ** far. This is used to determine the number of aggregate contexts 2356b3bce662Sdanielk1977 ** required at runtime. 2357b3bce662Sdanielk1977 */ 2358b3bce662Sdanielk1977 sNC.nDepth = (pOuterNC?pOuterNC->nDepth+1:1); 2359b3bce662Sdanielk1977 if( sNC.nDepth>pParse->nMaxDepth ){ 2360b3bce662Sdanielk1977 pParse->nMaxDepth = sNC.nDepth; 2361b3bce662Sdanielk1977 } 2362b3bce662Sdanielk1977 2363b3bce662Sdanielk1977 /* Resolve names in the result set. */ 2364b3bce662Sdanielk1977 pEList = p->pEList; 2365b3bce662Sdanielk1977 if( !pEList ) return SQLITE_ERROR; 2366b3bce662Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 2367b3bce662Sdanielk1977 Expr *pX = pEList->a[i].pExpr; 2368b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pX) ){ 2369b3bce662Sdanielk1977 return SQLITE_ERROR; 2370b3bce662Sdanielk1977 } 2371b3bce662Sdanielk1977 } 2372b3bce662Sdanielk1977 2373b3bce662Sdanielk1977 /* If there are no aggregate functions in the result-set, and no GROUP BY 2374b3bce662Sdanielk1977 ** expression, do not allow aggregates in any of the other expressions. 2375b3bce662Sdanielk1977 */ 2376b3bce662Sdanielk1977 assert( !p->isAgg ); 2377b3bce662Sdanielk1977 if( p->pGroupBy || sNC.hasAgg ){ 2378b3bce662Sdanielk1977 p->isAgg = 1; 2379b3bce662Sdanielk1977 }else{ 2380b3bce662Sdanielk1977 sNC.allowAgg = 0; 2381b3bce662Sdanielk1977 } 2382b3bce662Sdanielk1977 2383b3bce662Sdanielk1977 /* If a HAVING clause is present, then there must be a GROUP BY clause. 2384b3bce662Sdanielk1977 */ 2385b3bce662Sdanielk1977 if( p->pHaving && !p->pGroupBy ){ 2386b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2387b3bce662Sdanielk1977 return SQLITE_ERROR; 2388b3bce662Sdanielk1977 } 2389b3bce662Sdanielk1977 2390b3bce662Sdanielk1977 /* Add the expression list to the name-context before parsing the 2391b3bce662Sdanielk1977 ** other expressions in the SELECT statement. This is so that 2392b3bce662Sdanielk1977 ** expressions in the WHERE clause (etc.) can refer to expressions by 2393b3bce662Sdanielk1977 ** aliases in the result set. 2394b3bce662Sdanielk1977 ** 2395b3bce662Sdanielk1977 ** Minor point: If this is the case, then the expression will be 2396b3bce662Sdanielk1977 ** re-evaluated for each reference to it. 2397b3bce662Sdanielk1977 */ 2398b3bce662Sdanielk1977 sNC.pEList = p->pEList; 2399b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pWhere) || 2400b3bce662Sdanielk1977 sqlite3ExprResolveNames(&sNC, p->pHaving) || 2401b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") || 2402b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pGroupBy, "GROUP") 2403b3bce662Sdanielk1977 ){ 2404b3bce662Sdanielk1977 return SQLITE_ERROR; 2405b3bce662Sdanielk1977 } 2406b3bce662Sdanielk1977 2407b3bce662Sdanielk1977 return SQLITE_OK; 2408b3bce662Sdanielk1977 } 2409b3bce662Sdanielk1977 2410b3bce662Sdanielk1977 /* 2411b3bce662Sdanielk1977 ** An instance of the following struct is used by sqlite3Select() 2412b3bce662Sdanielk1977 ** to save aggregate related information from the Parse object 2413b3bce662Sdanielk1977 ** at the start of each call and to restore it at the end. See 2414b3bce662Sdanielk1977 ** saveAggregateInfo() and restoreAggregateInfo(). 2415b3bce662Sdanielk1977 */ 2416b3bce662Sdanielk1977 struct AggregateInfo { 2417b3bce662Sdanielk1977 int nAgg; 2418b3bce662Sdanielk1977 AggExpr *aAgg; 2419b3bce662Sdanielk1977 }; 2420b3bce662Sdanielk1977 typedef struct AggregateInfo AggregateInfo; 2421b3bce662Sdanielk1977 2422b3bce662Sdanielk1977 /* 2423b3bce662Sdanielk1977 ** Copy aggregate related information from the Parse structure 2424b3bce662Sdanielk1977 ** into the AggregateInfo structure. Zero the aggregate related 2425b3bce662Sdanielk1977 ** values in the Parse struct. 2426b3bce662Sdanielk1977 */ 2427b3bce662Sdanielk1977 static void saveAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2428b3bce662Sdanielk1977 pInfo->aAgg = pParse->aAgg; 2429b3bce662Sdanielk1977 pInfo->nAgg = pParse->nAgg; 2430b3bce662Sdanielk1977 pParse->aAgg = 0; 2431b3bce662Sdanielk1977 pParse->nAgg = 0; 2432b3bce662Sdanielk1977 } 2433b3bce662Sdanielk1977 2434b3bce662Sdanielk1977 /* 2435b3bce662Sdanielk1977 ** Copy aggregate related information from the AggregateInfo struct 2436b3bce662Sdanielk1977 ** back into the Parse structure. The aggregate related information 2437b3bce662Sdanielk1977 ** currently stored in the Parse structure is deleted. 2438b3bce662Sdanielk1977 */ 2439b3bce662Sdanielk1977 static void restoreAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2440b3bce662Sdanielk1977 sqliteFree(pParse->aAgg); 2441b3bce662Sdanielk1977 pParse->aAgg = pInfo->aAgg; 2442b3bce662Sdanielk1977 pParse->nAgg = pInfo->nAgg; 2443b3bce662Sdanielk1977 } 2444b3bce662Sdanielk1977 2445b3bce662Sdanielk1977 /* 24469bb61fe7Sdrh ** Generate code for the given SELECT statement. 24479bb61fe7Sdrh ** 2448fef5208cSdrh ** The results are distributed in various ways depending on the 2449fef5208cSdrh ** value of eDest and iParm. 2450fef5208cSdrh ** 2451fef5208cSdrh ** eDest Value Result 2452fef5208cSdrh ** ------------ ------------------------------------------- 2453fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2454fef5208cSdrh ** 2455fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2456fef5208cSdrh ** 2457e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2458fef5208cSdrh ** 245982c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 246082c3d636Sdrh ** 24614b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2462c4a3c779Sdrh ** 2463c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 24649bb61fe7Sdrh ** 2465e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2466e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2467e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2468e78e8284Sdrh ** 24699bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 24709bb61fe7Sdrh ** encountered, then an appropriate error message is left in 24719bb61fe7Sdrh ** pParse->zErrMsg. 24729bb61fe7Sdrh ** 24739bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 24749bb61fe7Sdrh ** calling function needs to do that. 24751b2e0329Sdrh ** 24761b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 24771b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 24781b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 24791b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 24801b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 24811b2e0329Sdrh ** can be changed. 2482e78e8284Sdrh ** 2483e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2484e78e8284Sdrh ** 2485e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2486e78e8284Sdrh ** \ \_______ subquery _______/ / 2487e78e8284Sdrh ** \ / 2488e78e8284Sdrh ** \____________________ outer query ___________________/ 2489e78e8284Sdrh ** 2490e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2491e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2492e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2493e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2494e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2495e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 24969bb61fe7Sdrh */ 24974adee20fSdanielk1977 int sqlite3Select( 2498cce7d176Sdrh Parse *pParse, /* The parser context */ 24999bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2500e78e8284Sdrh int eDest, /* How to dispose of the results */ 2501e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2502832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2503832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 250484ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 2505b3bce662Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2506cce7d176Sdrh ){ 2507d8bc7086Sdrh int i; 2508cce7d176Sdrh WhereInfo *pWInfo; 2509cce7d176Sdrh Vdbe *v; 2510b3bce662Sdanielk1977 int isAgg; /* True for select lists like "count(*)" */ 2511a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2512ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 25139bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 25149bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 25152282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 25162282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 251719a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 251819a775c2Sdrh int distinct; /* Table to use for the distinct set */ 25191d83f052Sdrh int rc = 1; /* Value to return from this function */ 2520b3bce662Sdanielk1977 AggregateInfo sAggInfo; 25219bb61fe7Sdrh 25226f8a503dSdanielk1977 if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1; 25234adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 2524daffd0e5Sdrh 2525b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 252682c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 252782c3d636Sdrh */ 252882c3d636Sdrh if( p->pPrior ){ 252984ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 253082c3d636Sdrh } 2531b7f9164eSdrh #endif 253282c3d636Sdrh 2533b3bce662Sdanielk1977 saveAggregateInfo(pParse, &sAggInfo); 2534b3bce662Sdanielk1977 pOrderBy = p->pOrderBy; 2535b3bce662Sdanielk1977 if( eDest==SRT_Union || eDest==SRT_Except || eDest==SRT_Discard ){ 2536b3bce662Sdanielk1977 p->pOrderBy = 0; 2537b3bce662Sdanielk1977 } 2538b3bce662Sdanielk1977 if( sqlite3SelectResolve(pParse, p, 0) ){ 2539b3bce662Sdanielk1977 goto select_end; 2540b3bce662Sdanielk1977 } 2541b3bce662Sdanielk1977 p->pOrderBy = pOrderBy; 2542b3bce662Sdanielk1977 254382c3d636Sdrh /* Make local copies of the parameters for this query. 254482c3d636Sdrh */ 25459bb61fe7Sdrh pTabList = p->pSrc; 25469bb61fe7Sdrh pWhere = p->pWhere; 25472282792aSdrh pGroupBy = p->pGroupBy; 25482282792aSdrh pHaving = p->pHaving; 2549b3bce662Sdanielk1977 isAgg = p->isAgg; 255019a775c2Sdrh isDistinct = p->isDistinct; 2551b3bce662Sdanielk1977 pEList = p->pEList; 2552b3bce662Sdanielk1977 if( pEList==0 ) goto select_end; 25539bb61fe7Sdrh 25549bb61fe7Sdrh /* 25559bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 25569bb61fe7Sdrh ** errors before this routine starts. 25579bb61fe7Sdrh */ 25581d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2559cce7d176Sdrh 25602282792aSdrh /* If writing to memory or generating a set 25612282792aSdrh ** only a single column may be output. 256219a775c2Sdrh */ 256351522cd3Sdrh assert( eDest!=SRT_Exists || pEList->nExpr==1 ); 256493758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2565fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 25664adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2567da93d238Sdrh "a SELECT that is part of an expression"); 25681d83f052Sdrh goto select_end; 256919a775c2Sdrh } 257093758c8dSdanielk1977 #endif 257119a775c2Sdrh 2572c926afbcSdrh /* ORDER BY is ignored for some destinations. 25732282792aSdrh */ 2574c926afbcSdrh switch( eDest ){ 2575c926afbcSdrh case SRT_Union: 2576c926afbcSdrh case SRT_Except: 2577c926afbcSdrh case SRT_Discard: 2578acd4c695Sdrh pOrderBy = 0; 2579c926afbcSdrh break; 2580c926afbcSdrh default: 2581c926afbcSdrh break; 25822282792aSdrh } 25832282792aSdrh 2584d820cb1bSdrh /* Begin generating code. 2585d820cb1bSdrh */ 25864adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2587d820cb1bSdrh if( v==0 ) goto select_end; 2588d820cb1bSdrh 2589e78e8284Sdrh /* Identify column names if we will be using them in a callback. This 2590e78e8284Sdrh ** step is skipped if the output is going to some other destination. 25910bb28106Sdrh */ 25920bb28106Sdrh if( eDest==SRT_Callback ){ 25936a3ea0e6Sdrh generateColumnNames(pParse, pTabList, pEList); 25940bb28106Sdrh } 25950bb28106Sdrh 2596d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2597d820cb1bSdrh */ 259851522cd3Sdrh #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) 2599ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 2600742f947bSdanielk1977 const char *zSavedAuthContext = 0; 2601c31c2eb8Sdrh int needRestoreContext; 2602c31c2eb8Sdrh 2603a76b5dfcSdrh if( pTabList->a[i].pSelect==0 ) continue; 26045cf590c1Sdrh if( pTabList->a[i].zName!=0 ){ 26055cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 26065cf590c1Sdrh pParse->zAuthContext = pTabList->a[i].zName; 2607c31c2eb8Sdrh needRestoreContext = 1; 2608c31c2eb8Sdrh }else{ 2609c31c2eb8Sdrh needRestoreContext = 0; 26105cf590c1Sdrh } 26114adee20fSdanielk1977 sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable, 2612b3bce662Sdanielk1977 pTabList->a[i].iCursor, p, i, &isAgg, 0); 2613c31c2eb8Sdrh if( needRestoreContext ){ 26145cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 26155cf590c1Sdrh } 2616832508b7Sdrh pTabList = p->pSrc; 2617832508b7Sdrh pWhere = p->pWhere; 2618c31c2eb8Sdrh if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ 2619832508b7Sdrh pOrderBy = p->pOrderBy; 2620acd4c695Sdrh } 2621832508b7Sdrh pGroupBy = p->pGroupBy; 2622832508b7Sdrh pHaving = p->pHaving; 2623832508b7Sdrh isDistinct = p->isDistinct; 26241b2e0329Sdrh } 262551522cd3Sdrh #endif 26261b2e0329Sdrh 26276e17529eSdrh /* Check for the special case of a min() or max() function by itself 26286e17529eSdrh ** in the result set. 26296e17529eSdrh */ 26306e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 26316e17529eSdrh rc = 0; 26326e17529eSdrh goto select_end; 26336e17529eSdrh } 26346e17529eSdrh 26351b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 26361b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 26371b2e0329Sdrh */ 2638b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 26391b2e0329Sdrh if( pParent && pParentAgg && 26408c74a8caSdrh flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ 26411b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 2642b3bce662Sdanielk1977 goto select_end; 26431b2e0329Sdrh } 2644b7f9164eSdrh #endif 2645832508b7Sdrh 26467cedc8d4Sdanielk1977 /* If there is an ORDER BY clause, resolve any collation sequences 26477cedc8d4Sdanielk1977 ** names that have been explicitly specified. 26487cedc8d4Sdanielk1977 */ 26497cedc8d4Sdanielk1977 if( pOrderBy ){ 26507cedc8d4Sdanielk1977 for(i=0; i<pOrderBy->nExpr; i++){ 26517cedc8d4Sdanielk1977 if( pOrderBy->a[i].zName ){ 26527cedc8d4Sdanielk1977 pOrderBy->a[i].pExpr->pColl = 26537cedc8d4Sdanielk1977 sqlite3LocateCollSeq(pParse, pOrderBy->a[i].zName, -1); 26547cedc8d4Sdanielk1977 } 26557cedc8d4Sdanielk1977 } 26567cedc8d4Sdanielk1977 if( pParse->nErr ){ 26577cedc8d4Sdanielk1977 goto select_end; 26587cedc8d4Sdanielk1977 } 26597cedc8d4Sdanielk1977 } 26607cedc8d4Sdanielk1977 26617b58daeaSdrh /* Set the limiter. 26627b58daeaSdrh */ 26637b58daeaSdrh computeLimitRegisters(pParse, p); 26647b58daeaSdrh 26652d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 26662d0794e3Sdrh */ 26672d0794e3Sdrh if( eDest==SRT_TempTable ){ 26684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2669b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); 26702d0794e3Sdrh } 26712d0794e3Sdrh 26722282792aSdrh /* Do an analysis of aggregate expressions. 2673efb7251dSdrh */ 2674bb999ef6Sdrh if( isAgg || pGroupBy ){ 2675a58fdfb1Sdanielk1977 NameContext sNC; 2676a58fdfb1Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 2677a58fdfb1Sdanielk1977 sNC.pParse = pParse; 2678a58fdfb1Sdanielk1977 sNC.pSrcList = pTabList; 2679a58fdfb1Sdanielk1977 26800bce8354Sdrh assert( pParse->nAgg==0 ); 2681bb999ef6Sdrh isAgg = 1; 26822282792aSdrh for(i=0; i<pEList->nExpr; i++){ 2683a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pEList->a[i].pExpr) ){ 26841d83f052Sdrh goto select_end; 26852282792aSdrh } 26862282792aSdrh } 26872282792aSdrh if( pGroupBy ){ 26882282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 2689a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pGroupBy->a[i].pExpr) ){ 26901d83f052Sdrh goto select_end; 26912282792aSdrh } 26922282792aSdrh } 26932282792aSdrh } 2694a58fdfb1Sdanielk1977 if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ 26951d83f052Sdrh goto select_end; 26962282792aSdrh } 2697191b690eSdrh if( pOrderBy ){ 2698191b690eSdrh for(i=0; i<pOrderBy->nExpr; i++){ 2699a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pOrderBy->a[i].pExpr) ){ 27001d83f052Sdrh goto select_end; 2701191b690eSdrh } 2702191b690eSdrh } 2703191b690eSdrh } 2704efb7251dSdrh } 2705efb7251dSdrh 27062282792aSdrh /* Reset the aggregator 2707cce7d176Sdrh */ 2708cce7d176Sdrh if( isAgg ){ 2709e159fdf2Sdanielk1977 int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg); 2710e5095355Sdrh for(i=0; i<pParse->nAgg; i++){ 27110bce8354Sdrh FuncDef *pFunc; 27120bce8354Sdrh if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ 27135c2d9155Sdanielk1977 int nExpr = 0; 27145c2d9155Sdanielk1977 #ifdef SQLITE_SSE 27155c2d9155Sdanielk1977 Expr *pAggExpr = pParse->aAgg[i].pExpr; 27165c2d9155Sdanielk1977 if( pAggExpr && pAggExpr->pList ){ 27175c2d9155Sdanielk1977 nExpr = pAggExpr->pList->nExpr; 27185c2d9155Sdanielk1977 } 27195c2d9155Sdanielk1977 #endif 27205c2d9155Sdanielk1977 sqlite3VdbeOp3(v, OP_AggInit, nExpr, i, (char*)pFunc, P3_FUNCDEF); 2721e5095355Sdrh } 2722e5095355Sdrh } 2723e159fdf2Sdanielk1977 if( pGroupBy ){ 2724ce2663ccSdanielk1977 int sz = sizeof(KeyInfo) + pGroupBy->nExpr*sizeof(CollSeq*); 2725ce2663ccSdanielk1977 KeyInfo *pKey = (KeyInfo *)sqliteMalloc(sz); 2726ce2663ccSdanielk1977 if( 0==pKey ){ 2727ce2663ccSdanielk1977 goto select_end; 2728ce2663ccSdanielk1977 } 2729ce2663ccSdanielk1977 pKey->enc = pParse->db->enc; 2730ce2663ccSdanielk1977 pKey->nField = pGroupBy->nExpr; 2731ce2663ccSdanielk1977 for(i=0; i<pGroupBy->nExpr; i++){ 2732ce2663ccSdanielk1977 pKey->aColl[i] = sqlite3ExprCollSeq(pParse, pGroupBy->a[i].pExpr); 2733ce2663ccSdanielk1977 if( !pKey->aColl[i] ){ 2734ce2663ccSdanielk1977 pKey->aColl[i] = pParse->db->pDfltColl; 2735ce2663ccSdanielk1977 } 2736ce2663ccSdanielk1977 } 2737ce2663ccSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF); 27381bee3d7bSdrh } 2739cce7d176Sdrh } 2740cce7d176Sdrh 274151522cd3Sdrh /* Initialize the memory cell to NULL for SRT_Mem or 0 for SRT_Exists 274219a775c2Sdrh */ 274351522cd3Sdrh if( eDest==SRT_Mem || eDest==SRT_Exists ){ 274451522cd3Sdrh sqlite3VdbeAddOp(v, eDest==SRT_Mem ? OP_String8 : OP_Integer, 0, 0); 27454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 274619a775c2Sdrh } 274719a775c2Sdrh 2748832508b7Sdrh /* Open a temporary table to use for the distinct set. 2749cce7d176Sdrh */ 275019a775c2Sdrh if( isDistinct ){ 2751832508b7Sdrh distinct = pParse->nTab++; 2752d3d39e93Sdrh openTempIndex(pParse, p, distinct, 0); 2753832508b7Sdrh }else{ 2754832508b7Sdrh distinct = -1; 2755efb7251dSdrh } 2756832508b7Sdrh 2757832508b7Sdrh /* Begin the database scan 2758832508b7Sdrh */ 2759e6f85e71Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 2760f8db1bc0Sdrh pGroupBy ? 0 : &pOrderBy); 27611d83f052Sdrh if( pWInfo==0 ) goto select_end; 2762cce7d176Sdrh 27632282792aSdrh /* Use the standard inner loop if we are not dealing with 27642282792aSdrh ** aggregates 2765cce7d176Sdrh */ 2766da9d6c45Sdrh if( !isAgg ){ 2767df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 276884ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 27691d83f052Sdrh goto select_end; 2770cce7d176Sdrh } 2771da9d6c45Sdrh } 2772cce7d176Sdrh 2773e3184744Sdrh /* If we are dealing with aggregates, then do the special aggregate 27742282792aSdrh ** processing. 2775efb7251dSdrh */ 27762282792aSdrh else{ 2777268380caSdrh AggExpr *pAgg; 2778a58fdfb1Sdanielk1977 int lbl1 = 0; 2779a58fdfb1Sdanielk1977 pParse->fillAgg = 1; 27802282792aSdrh if( pGroupBy ){ 27812282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 27824adee20fSdanielk1977 sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr); 2783efb7251dSdrh } 2784ededfd5eSdanielk1977 /* No affinity string is attached to the following OP_MakeRecord 2785d3d39e93Sdrh ** because we do not need to do any coercion of datatypes. */ 2786ededfd5eSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0); 27874adee20fSdanielk1977 lbl1 = sqlite3VdbeMakeLabel(v); 27884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1); 2789a58fdfb1Sdanielk1977 } 2790268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 2791268380caSdrh if( pAgg->isAgg ) continue; 27924adee20fSdanielk1977 sqlite3ExprCode(pParse, pAgg->pExpr); 27934adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggSet, 0, i); 27942282792aSdrh } 2795a58fdfb1Sdanielk1977 pParse->fillAgg = 0; 2796a58fdfb1Sdanielk1977 if( lbl1<0 ){ 27974adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, lbl1); 27982282792aSdrh } 2799268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 28002282792aSdrh Expr *pE; 2801268380caSdrh int nExpr; 2802268380caSdrh FuncDef *pDef; 2803268380caSdrh if( !pAgg->isAgg ) continue; 2804268380caSdrh assert( pAgg->pFunc!=0 ); 2805268380caSdrh assert( pAgg->pFunc->xStep!=0 ); 2806268380caSdrh pDef = pAgg->pFunc; 2807268380caSdrh pE = pAgg->pExpr; 2808268380caSdrh assert( pE!=0 ); 28092282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 2810f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pE->pList); 28114adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 2812dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2813dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 2814dc1bdc4fSdanielk1977 int j; 2815dc1bdc4fSdanielk1977 for(j=0; !pColl && j<nExpr; j++){ 2816dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr); 2817dc1bdc4fSdanielk1977 } 2818dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 2819dc1bdc4fSdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 2820dc1bdc4fSdanielk1977 } 28211f55c056Sdanielk1977 sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_FUNCDEF); 28222282792aSdrh } 28232282792aSdrh } 28242282792aSdrh 2825cce7d176Sdrh /* End the database scan loop. 2826cce7d176Sdrh */ 28274adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 2828cce7d176Sdrh 28292282792aSdrh /* If we are processing aggregates, we need to set up a second loop 28302282792aSdrh ** over all of the aggregate values and process them. 28312282792aSdrh */ 28322282792aSdrh if( isAgg ){ 28334adee20fSdanielk1977 int endagg = sqlite3VdbeMakeLabel(v); 28342282792aSdrh int startagg; 28354adee20fSdanielk1977 startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg); 28362282792aSdrh if( pHaving ){ 28374adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pHaving, startagg, 1); 28382282792aSdrh } 2839df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 284084ac9d02Sdanielk1977 iParm, startagg, endagg, aff) ){ 28411d83f052Sdrh goto select_end; 28422282792aSdrh } 28434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, startagg); 28444adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endagg); 28454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Noop, 0, 0); 28462282792aSdrh } 28472282792aSdrh 2848cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 2849cce7d176Sdrh ** and send them to the callback one by one. 2850cce7d176Sdrh */ 2851cce7d176Sdrh if( pOrderBy ){ 2852ffbc3088Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 2853cce7d176Sdrh } 28546a535340Sdrh 285593758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2856f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 2857f620b4e2Sdrh ** temporary table. So delete the subquery structure from the parent 2858f620b4e2Sdrh ** to prevent this subquery from being evaluated again and to force the 2859f620b4e2Sdrh ** the use of the temporary table. 2860f620b4e2Sdrh */ 2861f620b4e2Sdrh if( pParent ){ 2862f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 2863f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 28644adee20fSdanielk1977 sqlite3SelectDelete(p); 2865f620b4e2Sdrh pParent->pSrc->a[parentTab].pSelect = 0; 2866f620b4e2Sdrh } 286793758c8dSdanielk1977 #endif 2868f620b4e2Sdrh 28691d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 28701d83f052Sdrh ** to indicate no errors. 28711d83f052Sdrh */ 28721d83f052Sdrh rc = 0; 28731d83f052Sdrh 28741d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 28751d83f052Sdrh ** successful coding of the SELECT. 28761d83f052Sdrh */ 28771d83f052Sdrh select_end: 2878b3bce662Sdanielk1977 restoreAggregateInfo(pParse, &sAggInfo); 28791d83f052Sdrh return rc; 2880cce7d176Sdrh } 2881