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*d70dc52dSdrh ** $Id: select.c,v 1.249 2005/06/06 16:34:33 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 } 87522f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 87622f70c32Sdrh pEList = pSelect->pEList; 87722f70c32Sdrh pTab->nCol = pEList->nExpr; 878417be79cSdrh assert( pTab->nCol>0 ); 879b733d037Sdrh pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 880290c1948Sdrh for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){ 88179d5f63fSdrh Expr *p, *pR; 882517eb646Sdanielk1977 char *zType; 88391bb0eedSdrh char *zName; 88479d5f63fSdrh char *zBasename; 88579d5f63fSdrh int cnt; 886b3bce662Sdanielk1977 NameContext sNC; 88779d5f63fSdrh 88879d5f63fSdrh /* Get an appropriate name for the column 88979d5f63fSdrh */ 89079d5f63fSdrh p = pEList->a[i].pExpr; 891290c1948Sdrh assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 ); 89291bb0eedSdrh if( (zName = pEList->a[i].zName)!=0 ){ 89379d5f63fSdrh /* If the column contains an "AS <name>" phrase, use <name> as the name */ 89491bb0eedSdrh zName = sqliteStrDup(zName); 895517eb646Sdanielk1977 }else if( p->op==TK_DOT 896b733d037Sdrh && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ 89779d5f63fSdrh /* For columns of the from A.B use B as the name */ 89891bb0eedSdrh zName = sqlite3MPrintf("%T", &pR->token); 899b733d037Sdrh }else if( p->span.z && p->span.z[0] ){ 90079d5f63fSdrh /* Use the original text of the column expression as its name */ 90191bb0eedSdrh zName = sqlite3MPrintf("%T", &p->span); 90222f70c32Sdrh }else{ 90379d5f63fSdrh /* If all else fails, make up a name */ 90491bb0eedSdrh zName = sqlite3MPrintf("column%d", i+1); 90522f70c32Sdrh } 90691bb0eedSdrh sqlite3Dequote(zName); 907dd5b2fa5Sdrh if( sqlite3_malloc_failed ){ 908dd5b2fa5Sdrh sqliteFree(zName); 909dd5b2fa5Sdrh sqlite3DeleteTable(0, pTab); 910dd5b2fa5Sdrh return 0; 911dd5b2fa5Sdrh } 91279d5f63fSdrh 91379d5f63fSdrh /* Make sure the column name is unique. If the name is not unique, 91479d5f63fSdrh ** append a integer to the name so that it becomes unique. 91579d5f63fSdrh */ 91679d5f63fSdrh zBasename = zName; 91779d5f63fSdrh for(j=cnt=0; j<i; j++){ 91879d5f63fSdrh if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ 91979d5f63fSdrh zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt); 92079d5f63fSdrh j = -1; 921dd5b2fa5Sdrh if( zName==0 ) break; 92279d5f63fSdrh } 92379d5f63fSdrh } 92479d5f63fSdrh if( zBasename!=zName ){ 92579d5f63fSdrh sqliteFree(zBasename); 92679d5f63fSdrh } 92791bb0eedSdrh pCol->zName = zName; 928e014a838Sdanielk1977 92979d5f63fSdrh /* Get the typename, type affinity, and collating sequence for the 93079d5f63fSdrh ** column. 93179d5f63fSdrh */ 932c43e8be8Sdrh memset(&sNC, 0, sizeof(sNC)); 933b3bce662Sdanielk1977 sNC.pSrcList = pSelect->pSrc; 934b3bce662Sdanielk1977 zType = sqliteStrDup(columnType(&sNC, p)); 935290c1948Sdrh pCol->zType = zType; 936c60e9b82Sdanielk1977 pCol->affinity = sqlite3ExprAffinity(p); 937290c1948Sdrh pCol->pColl = sqlite3ExprCollSeq(pParse, p); 938290c1948Sdrh if( !pCol->pColl ){ 939290c1948Sdrh pCol->pColl = pParse->db->pDfltColl; 9400202b29eSdanielk1977 } 94122f70c32Sdrh } 94222f70c32Sdrh pTab->iPKey = -1; 94322f70c32Sdrh return pTab; 94422f70c32Sdrh } 94522f70c32Sdrh 94622f70c32Sdrh /* 9479b3187e1Sdrh ** Prepare a SELECT statement for processing by doing the following 9489b3187e1Sdrh ** things: 949d8bc7086Sdrh ** 9509b3187e1Sdrh ** (1) Make sure VDBE cursor numbers have been assigned to every 9519b3187e1Sdrh ** element of the FROM clause. 9529b3187e1Sdrh ** 9539b3187e1Sdrh ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that 9549b3187e1Sdrh ** defines FROM clause. When views appear in the FROM clause, 95563eb5f29Sdrh ** fill pTabList->a[].pSelect with a copy of the SELECT statement 95663eb5f29Sdrh ** that implements the view. A copy is made of the view's SELECT 95763eb5f29Sdrh ** statement so that we can freely modify or delete that statement 95863eb5f29Sdrh ** without worrying about messing up the presistent representation 95963eb5f29Sdrh ** of the view. 960d8bc7086Sdrh ** 9619b3187e1Sdrh ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword 962ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 963ad2d8307Sdrh ** 9649b3187e1Sdrh ** (4) Scan the list of columns in the result set (pEList) looking 96554473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 96654473229Sdrh ** If found, expand each "*" to be every column in every table 96754473229Sdrh ** and TABLE.* to be every column in TABLE. 968d8bc7086Sdrh ** 969d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 970d8bc7086Sdrh ** in pParse and return non-zero. 971d8bc7086Sdrh */ 9729b3187e1Sdrh static int prepSelectStmt(Parse *pParse, Select *p){ 97354473229Sdrh int i, j, k, rc; 974ad3cab52Sdrh SrcList *pTabList; 975daffd0e5Sdrh ExprList *pEList; 976a76b5dfcSdrh Table *pTab; 977290c1948Sdrh struct SrcList_item *pFrom; 978daffd0e5Sdrh 979e94ddc9eSdanielk1977 if( p==0 || p->pSrc==0 || sqlite3_malloc_failed ) return 1; 980daffd0e5Sdrh pTabList = p->pSrc; 981daffd0e5Sdrh pEList = p->pEList; 982d8bc7086Sdrh 9839b3187e1Sdrh /* Make sure cursor numbers have been assigned to all entries in 9849b3187e1Sdrh ** the FROM clause of the SELECT statement. 9859b3187e1Sdrh */ 9869b3187e1Sdrh sqlite3SrcListAssignCursors(pParse, p->pSrc); 9879b3187e1Sdrh 9889b3187e1Sdrh /* Look up every table named in the FROM clause of the select. If 9899b3187e1Sdrh ** an entry of the FROM clause is a subquery instead of a table or view, 9909b3187e1Sdrh ** then create a transient table structure to describe the subquery. 991d8bc7086Sdrh */ 992290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 9939b3187e1Sdrh if( pFrom->pTab!=0 ){ 9949b3187e1Sdrh /* This statement has already been prepared. There is no need 9959b3187e1Sdrh ** to go further. */ 9969b3187e1Sdrh assert( i==0 ); 997d8bc7086Sdrh return 0; 998d8bc7086Sdrh } 999290c1948Sdrh if( pFrom->zName==0 ){ 100093758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 100122f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 1002290c1948Sdrh assert( pFrom->pSelect!=0 ); 1003290c1948Sdrh if( pFrom->zAlias==0 ){ 100491bb0eedSdrh pFrom->zAlias = 100591bb0eedSdrh sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect); 1006ad2d8307Sdrh } 1007290c1948Sdrh pFrom->pTab = pTab = 1008290c1948Sdrh sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect); 100922f70c32Sdrh if( pTab==0 ){ 1010daffd0e5Sdrh return 1; 1011daffd0e5Sdrh } 10125cf590c1Sdrh /* The isTransient flag indicates that the Table structure has been 10135cf590c1Sdrh ** dynamically allocated and may be freed at any time. In other words, 10145cf590c1Sdrh ** pTab is not pointing to a persistent table structure that defines 10155cf590c1Sdrh ** part of the schema. */ 101622f70c32Sdrh pTab->isTransient = 1; 101793758c8dSdanielk1977 #endif 101822f70c32Sdrh }else{ 1019a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 1020290c1948Sdrh pFrom->pTab = pTab = 1021290c1948Sdrh sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); 1022a76b5dfcSdrh if( pTab==0 ){ 1023d8bc7086Sdrh return 1; 1024d8bc7086Sdrh } 102593758c8dSdanielk1977 #ifndef SQLITE_OMIT_VIEW 1026a76b5dfcSdrh if( pTab->pSelect ){ 102763eb5f29Sdrh /* We reach here if the named table is a really a view */ 10284adee20fSdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 1029417be79cSdrh return 1; 1030417be79cSdrh } 1031290c1948Sdrh /* If pFrom->pSelect!=0 it means we are dealing with a 103263eb5f29Sdrh ** view within a view. The SELECT structure has already been 103363eb5f29Sdrh ** copied by the outer view so we can skip the copy step here 103463eb5f29Sdrh ** in the inner view. 103563eb5f29Sdrh */ 1036290c1948Sdrh if( pFrom->pSelect==0 ){ 1037290c1948Sdrh pFrom->pSelect = sqlite3SelectDup(pTab->pSelect); 1038a76b5dfcSdrh } 1039d8bc7086Sdrh } 104093758c8dSdanielk1977 #endif 104122f70c32Sdrh } 104263eb5f29Sdrh } 1043d8bc7086Sdrh 1044ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 1045ad2d8307Sdrh */ 1046ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 1047ad2d8307Sdrh 10487c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 104954473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 105054473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 10517c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 10527c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 10537c917d19Sdrh ** each one to the list of all columns in all tables. 105454473229Sdrh ** 105554473229Sdrh ** The first loop just checks to see if there are any "*" operators 105654473229Sdrh ** that need expanding. 1057d8bc7086Sdrh */ 10587c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 105954473229Sdrh Expr *pE = pEList->a[k].pExpr; 106054473229Sdrh if( pE->op==TK_ALL ) break; 106154473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 106254473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 10637c917d19Sdrh } 106454473229Sdrh rc = 0; 10657c917d19Sdrh if( k<pEList->nExpr ){ 106654473229Sdrh /* 106754473229Sdrh ** If we get here it means the result set contains one or more "*" 106854473229Sdrh ** operators that need to be expanded. Loop through each expression 106954473229Sdrh ** in the result set and expand them one by one. 107054473229Sdrh */ 10717c917d19Sdrh struct ExprList_item *a = pEList->a; 10727c917d19Sdrh ExprList *pNew = 0; 1073*d70dc52dSdrh int flags = pParse->db->flags; 1074*d70dc52dSdrh int longNames = (flags & SQLITE_FullColNames)!=0 && 1075*d70dc52dSdrh (flags & SQLITE_ShortColNames)==0; 1076*d70dc52dSdrh 10777c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 107854473229Sdrh Expr *pE = a[k].pExpr; 107954473229Sdrh if( pE->op!=TK_ALL && 108054473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 108154473229Sdrh /* This particular expression does not need to be expanded. 108254473229Sdrh */ 10834adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 10847c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 10857c917d19Sdrh a[k].pExpr = 0; 10867c917d19Sdrh a[k].zName = 0; 10877c917d19Sdrh }else{ 108854473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 108954473229Sdrh ** expanded. */ 109054473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 1091cf55b7aeSdrh char *zTName; /* text of name of TABLE */ 109254473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 1093cf55b7aeSdrh zTName = sqlite3NameFromToken(&pE->pLeft->token); 109454473229Sdrh }else{ 1095cf55b7aeSdrh zTName = 0; 109654473229Sdrh } 1097290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1098290c1948Sdrh Table *pTab = pFrom->pTab; 1099290c1948Sdrh char *zTabName = pFrom->zAlias; 110054473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 110154473229Sdrh zTabName = pTab->zName; 110254473229Sdrh } 1103cf55b7aeSdrh if( zTName && (zTabName==0 || zTabName[0]==0 || 1104cf55b7aeSdrh sqlite3StrICmp(zTName, zTabName)!=0) ){ 110554473229Sdrh continue; 110654473229Sdrh } 110754473229Sdrh tableSeen = 1; 1108d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 110922f70c32Sdrh Expr *pExpr, *pLeft, *pRight; 1110ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 1111ad2d8307Sdrh 111291bb0eedSdrh if( i>0 ){ 111391bb0eedSdrh struct SrcList_item *pLeft = &pTabList->a[i-1]; 111491bb0eedSdrh if( (pLeft->jointype & JT_NATURAL)!=0 && 111591bb0eedSdrh columnIndex(pLeft->pTab, zName)>=0 ){ 1116ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 1117ad2d8307Sdrh ** table on the right */ 1118ad2d8307Sdrh continue; 1119ad2d8307Sdrh } 112091bb0eedSdrh if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ 1121ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 1122ad2d8307Sdrh ** using clause from the table on the right. */ 1123ad2d8307Sdrh continue; 1124ad2d8307Sdrh } 112591bb0eedSdrh } 11264adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 112722f70c32Sdrh if( pRight==0 ) break; 112891bb0eedSdrh setToken(&pRight->token, zName); 1129*d70dc52dSdrh if( zTabName && (longNames || pTabList->nSrc>1) ){ 11304adee20fSdanielk1977 pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 11314adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 113222f70c32Sdrh if( pExpr==0 ) break; 113391bb0eedSdrh setToken(&pLeft->token, zTabName); 113491bb0eedSdrh setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); 11356977fea8Sdrh pExpr->span.dyn = 1; 11366977fea8Sdrh pExpr->token.z = 0; 11376977fea8Sdrh pExpr->token.n = 0; 11386977fea8Sdrh pExpr->token.dyn = 0; 113922f70c32Sdrh }else{ 114022f70c32Sdrh pExpr = pRight; 11416977fea8Sdrh pExpr->span = pExpr->token; 114222f70c32Sdrh } 1143*d70dc52dSdrh if( longNames ){ 1144*d70dc52dSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); 1145*d70dc52dSdrh }else{ 114679d5f63fSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); 1147d8bc7086Sdrh } 1148d8bc7086Sdrh } 1149*d70dc52dSdrh } 115054473229Sdrh if( !tableSeen ){ 1151cf55b7aeSdrh if( zTName ){ 1152cf55b7aeSdrh sqlite3ErrorMsg(pParse, "no such table: %s", zTName); 1153f5db2d3eSdrh }else{ 11544adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1155f5db2d3eSdrh } 115654473229Sdrh rc = 1; 115754473229Sdrh } 1158cf55b7aeSdrh sqliteFree(zTName); 11597c917d19Sdrh } 11607c917d19Sdrh } 11614adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 11627c917d19Sdrh p->pEList = pNew; 1163d8bc7086Sdrh } 116454473229Sdrh return rc; 1165d8bc7086Sdrh } 1166d8bc7086Sdrh 1167d8bc7086Sdrh /* 1168ff78bd2fSdrh ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers 1169ff78bd2fSdrh ** in a select structure. It just sets the pointers to NULL. This 1170ff78bd2fSdrh ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect 1171ff78bd2fSdrh ** pointer is not NULL, this routine is called recursively on that pointer. 1172ff78bd2fSdrh ** 1173ff78bd2fSdrh ** This routine is called on the Select structure that defines a 1174ff78bd2fSdrh ** VIEW in order to undo any bindings to tables. This is necessary 1175ff78bd2fSdrh ** because those tables might be DROPed by a subsequent SQL command. 11765cf590c1Sdrh ** If the bindings are not removed, then the Select.pSrc->a[].pTab field 11775cf590c1Sdrh ** will be left pointing to a deallocated Table structure after the 11785cf590c1Sdrh ** DROP and a coredump will occur the next time the VIEW is used. 1179ff78bd2fSdrh */ 11805338a5f7Sdanielk1977 #if 0 11814adee20fSdanielk1977 void sqlite3SelectUnbind(Select *p){ 1182ff78bd2fSdrh int i; 1183ad3cab52Sdrh SrcList *pSrc = p->pSrc; 118491bb0eedSdrh struct SrcList_item *pItem; 1185ff78bd2fSdrh Table *pTab; 1186ff78bd2fSdrh if( p==0 ) return; 118791bb0eedSdrh for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){ 118891bb0eedSdrh if( (pTab = pItem->pTab)!=0 ){ 1189ff78bd2fSdrh if( pTab->isTransient ){ 11904adee20fSdanielk1977 sqlite3DeleteTable(0, pTab); 1191ff78bd2fSdrh } 119291bb0eedSdrh pItem->pTab = 0; 119391bb0eedSdrh if( pItem->pSelect ){ 119491bb0eedSdrh sqlite3SelectUnbind(pItem->pSelect); 1195ff78bd2fSdrh } 1196ff78bd2fSdrh } 1197ff78bd2fSdrh } 1198ff78bd2fSdrh } 11995338a5f7Sdanielk1977 #endif 1200ff78bd2fSdrh 120193758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 1202ff78bd2fSdrh /* 1203d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1204d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1205967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1206d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1207d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1208d8bc7086Sdrh ** 1209d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1210d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1211d8bc7086Sdrh ** 1212d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1213d8bc7086Sdrh ** of errors is returned. 1214d8bc7086Sdrh */ 1215d8bc7086Sdrh static int matchOrderbyToColumn( 1216d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1217d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1218d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1219e4de1febSdrh int iTable, /* Insert this value in iTable */ 1220d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1221d8bc7086Sdrh ){ 1222d8bc7086Sdrh int nErr = 0; 1223d8bc7086Sdrh int i, j; 1224d8bc7086Sdrh ExprList *pEList; 1225d8bc7086Sdrh 1226daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1227d8bc7086Sdrh if( mustComplete ){ 1228d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1229d8bc7086Sdrh } 12309b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 1231d8bc7086Sdrh return 1; 1232d8bc7086Sdrh } 1233d8bc7086Sdrh if( pSelect->pPrior ){ 123492cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 123592cd52f5Sdrh return 1; 123692cd52f5Sdrh } 1237d8bc7086Sdrh } 1238d8bc7086Sdrh pEList = pSelect->pEList; 1239d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1240d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1241e4de1febSdrh int iCol = -1; 1242d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 12434adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1244e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 12454adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1246da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1247e4de1febSdrh iCol, pEList->nExpr); 1248e4de1febSdrh nErr++; 1249e4de1febSdrh break; 1250e4de1febSdrh } 1251fcb78a49Sdrh if( !mustComplete ) continue; 1252e4de1febSdrh iCol--; 1253e4de1febSdrh } 1254e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 12554cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1256a76b5dfcSdrh char *zName, *zLabel; 1257a76b5dfcSdrh zName = pEList->a[j].zName; 1258a99db3b6Sdrh zLabel = sqlite3NameFromToken(&pE->token); 1259a99db3b6Sdrh assert( zLabel!=0 ); 12604adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1261e4de1febSdrh iCol = j; 1262d8bc7086Sdrh } 12636e142f54Sdrh sqliteFree(zLabel); 1264d8bc7086Sdrh } 12654adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1266e4de1febSdrh iCol = j; 1267d8bc7086Sdrh } 1268e4de1febSdrh } 1269e4de1febSdrh if( iCol>=0 ){ 1270967e8b73Sdrh pE->op = TK_COLUMN; 1271e4de1febSdrh pE->iColumn = iCol; 1272d8bc7086Sdrh pE->iTable = iTable; 1273a58fdfb1Sdanielk1977 pE->iAgg = -1; 1274d8bc7086Sdrh pOrderBy->a[i].done = 1; 1275d8bc7086Sdrh } 1276e4de1febSdrh if( iCol<0 && mustComplete ){ 12774adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1278da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1279d8bc7086Sdrh nErr++; 1280d8bc7086Sdrh break; 1281d8bc7086Sdrh } 1282d8bc7086Sdrh } 1283d8bc7086Sdrh return nErr; 1284d8bc7086Sdrh } 128593758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */ 1286d8bc7086Sdrh 1287d8bc7086Sdrh /* 1288d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1289d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1290d8bc7086Sdrh */ 12914adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1292d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1293d8bc7086Sdrh if( v==0 ){ 12944adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1295d8bc7086Sdrh } 1296d8bc7086Sdrh return v; 1297d8bc7086Sdrh } 1298d8bc7086Sdrh 1299d8bc7086Sdrh /* 13007b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 1301a2dc3b1aSdanielk1977 ** pLimit and pOffset expressions. nLimit and nOffset hold the expressions 13027b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 1303a2dc3b1aSdanielk1977 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset 1304a2dc3b1aSdanielk1977 ** are the integer memory register numbers for counters used to compute 1305a2dc3b1aSdanielk1977 ** the limit and offset. If there is no limit and/or offset, then 1306a2dc3b1aSdanielk1977 ** iLimit and iOffset are negative. 13077b58daeaSdrh ** 13087b58daeaSdrh ** This routine changes the values if iLimit and iOffset only if 13097b58daeaSdrh ** a limit or offset is defined by nLimit and nOffset. iLimit and 13107b58daeaSdrh ** iOffset should have been preset to appropriate default values 13117b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 13127b58daeaSdrh ** Only if nLimit>=0 or nOffset>0 do the limit registers get 13137b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 13147b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 13157b58daeaSdrh ** SELECT statements. 13167b58daeaSdrh */ 13177b58daeaSdrh static void computeLimitRegisters(Parse *pParse, Select *p){ 13187b58daeaSdrh /* 13197b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 13207b58daeaSdrh ** contraversy about what the correct behavior should be. 13217b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 13227b58daeaSdrh ** no rows. 13237b58daeaSdrh */ 1324a2dc3b1aSdanielk1977 if( p->pLimit ){ 13257b58daeaSdrh int iMem = pParse->nMem++; 13264adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13277b58daeaSdrh if( v==0 ) return; 1328a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pLimit); 1329a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1330a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1332ad6d9460Sdrh VdbeComment((v, "# LIMIT counter")); 13337b58daeaSdrh p->iLimit = iMem; 13347b58daeaSdrh } 1335a2dc3b1aSdanielk1977 if( p->pOffset ){ 13367b58daeaSdrh int iMem = pParse->nMem++; 13374adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13387b58daeaSdrh if( v==0 ) return; 1339a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pOffset); 1340a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1341a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1343ad6d9460Sdrh VdbeComment((v, "# OFFSET counter")); 13447b58daeaSdrh p->iOffset = iMem; 13457b58daeaSdrh } 13467b58daeaSdrh } 13477b58daeaSdrh 13487b58daeaSdrh /* 1349d3d39e93Sdrh ** Generate VDBE instructions that will open a transient table that 1350d3d39e93Sdrh ** will be used for an index or to store keyed results for a compound 1351d3d39e93Sdrh ** select. In other words, open a transient table that needs a 1352d3d39e93Sdrh ** KeyInfo structure. The number of columns in the KeyInfo is determined 1353d3d39e93Sdrh ** by the result set of the SELECT statement in the second argument. 1354d3d39e93Sdrh ** 1355dc1bdc4fSdanielk1977 ** Specifically, this routine is called to open an index table for 1356dc1bdc4fSdanielk1977 ** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not 1357dc1bdc4fSdanielk1977 ** UNION ALL). 1358dc1bdc4fSdanielk1977 ** 1359d3d39e93Sdrh ** Make the new table a KeyAsData table if keyAsData is true. 1360dc1bdc4fSdanielk1977 ** 1361dc1bdc4fSdanielk1977 ** The value returned is the address of the OP_OpenTemp instruction. 1362d3d39e93Sdrh */ 1363dc1bdc4fSdanielk1977 static int openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){ 1364d3d39e93Sdrh KeyInfo *pKeyInfo; 1365736c22b8Sdrh int nColumn; 13669bb575fdSdrh sqlite3 *db = pParse->db; 1367d3d39e93Sdrh int i; 1368d3d39e93Sdrh Vdbe *v = pParse->pVdbe; 1369dc1bdc4fSdanielk1977 int addr; 1370d3d39e93Sdrh 13719b3187e1Sdrh if( prepSelectStmt(pParse, p) ){ 1372dc1bdc4fSdanielk1977 return 0; 1373736c22b8Sdrh } 1374736c22b8Sdrh nColumn = p->pEList->nExpr; 1375d3d39e93Sdrh pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) ); 1376dc1bdc4fSdanielk1977 if( pKeyInfo==0 ) return 0; 137791bb0eedSdrh pKeyInfo->enc = db->enc; 1378d3d39e93Sdrh pKeyInfo->nField = nColumn; 1379d3d39e93Sdrh for(i=0; i<nColumn; i++){ 1380dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr); 1381dc1bdc4fSdanielk1977 if( !pKeyInfo->aColl[i] ){ 1382d3d39e93Sdrh pKeyInfo->aColl[i] = db->pDfltColl; 1383d3d39e93Sdrh } 1384dc1bdc4fSdanielk1977 } 1385dc1bdc4fSdanielk1977 addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0, 1386dc1bdc4fSdanielk1977 (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 1387d3d39e93Sdrh if( keyAsData ){ 1388d3d39e93Sdrh sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1); 1389d3d39e93Sdrh } 1390dc1bdc4fSdanielk1977 return addr; 1391dc1bdc4fSdanielk1977 } 1392dc1bdc4fSdanielk1977 1393b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1394fbc4ee7bSdrh /* 13958cdbf836Sdrh ** Add the address "addr" to the set of all OpenTemp opcode addresses 13968cdbf836Sdrh ** that are being accumulated in p->ppOpenTemp. 1397fbc4ee7bSdrh */ 13988cdbf836Sdrh static int multiSelectOpenTempAddr(Select *p, int addr){ 13998cdbf836Sdrh IdList *pList = *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0); 1400fbc4ee7bSdrh if( pList==0 ){ 1401dc1bdc4fSdanielk1977 return SQLITE_NOMEM; 1402dc1bdc4fSdanielk1977 } 1403fbc4ee7bSdrh pList->a[pList->nId-1].idx = addr; 1404dc1bdc4fSdanielk1977 return SQLITE_OK; 1405dc1bdc4fSdanielk1977 } 1406b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1407dc1bdc4fSdanielk1977 1408b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1409fbc4ee7bSdrh /* 1410fbc4ee7bSdrh ** Return the appropriate collating sequence for the iCol-th column of 1411fbc4ee7bSdrh ** the result set for the compound-select statement "p". Return NULL if 1412fbc4ee7bSdrh ** the column has no default collating sequence. 1413fbc4ee7bSdrh ** 1414fbc4ee7bSdrh ** The collating sequence for the compound select is taken from the 1415fbc4ee7bSdrh ** left-most term of the select that has a collating sequence. 1416fbc4ee7bSdrh */ 1417dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ 1418fbc4ee7bSdrh CollSeq *pRet; 1419dc1bdc4fSdanielk1977 if( p->pPrior ){ 1420dc1bdc4fSdanielk1977 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); 1421fbc4ee7bSdrh }else{ 1422fbc4ee7bSdrh pRet = 0; 1423dc1bdc4fSdanielk1977 } 1424fbc4ee7bSdrh if( pRet==0 ){ 1425dc1bdc4fSdanielk1977 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); 1426dc1bdc4fSdanielk1977 } 1427dc1bdc4fSdanielk1977 return pRet; 1428d3d39e93Sdrh } 1429b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1430d3d39e93Sdrh 1431b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1432d3d39e93Sdrh /* 143382c3d636Sdrh ** This routine is called to process a query that is really the union 143482c3d636Sdrh ** or intersection of two or more separate queries. 1435c926afbcSdrh ** 1436e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1437e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1438e78e8284Sdrh ** in which case this routine will be called recursively. 1439e78e8284Sdrh ** 1440e78e8284Sdrh ** The results of the total query are to be written into a destination 1441e78e8284Sdrh ** of type eDest with parameter iParm. 1442e78e8284Sdrh ** 1443e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1444e78e8284Sdrh ** 1445e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1446e78e8284Sdrh ** 1447e78e8284Sdrh ** This statement is parsed up as follows: 1448e78e8284Sdrh ** 1449e78e8284Sdrh ** SELECT c FROM t3 1450e78e8284Sdrh ** | 1451e78e8284Sdrh ** `-----> SELECT b FROM t2 1452e78e8284Sdrh ** | 14534b11c6d3Sjplyon ** `------> SELECT a FROM t1 1454e78e8284Sdrh ** 1455e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1456e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1457e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1458e78e8284Sdrh ** 1459e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1460e78e8284Sdrh ** individual selects always group from left to right. 146182c3d636Sdrh */ 146284ac9d02Sdanielk1977 static int multiSelect( 1463fbc4ee7bSdrh Parse *pParse, /* Parsing context */ 1464fbc4ee7bSdrh Select *p, /* The right-most of SELECTs to be coded */ 1465fbc4ee7bSdrh int eDest, /* \___ Store query results as specified */ 1466fbc4ee7bSdrh int iParm, /* / by these two parameters. */ 146784ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 146884ac9d02Sdanielk1977 ){ 146984ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 147010e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 147110e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 1472fbc4ee7bSdrh IdList *pOpenTemp = 0;/* OP_OpenTemp opcodes that need a KeyInfo */ 14738cdbf836Sdrh int aAddr[5]; /* Addresses of SetNumColumns operators */ 14748cdbf836Sdrh int nAddr = 0; /* Number used */ 14758cdbf836Sdrh int nCol; /* Number of columns in the result set */ 147682c3d636Sdrh 14777b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 1478fbc4ee7bSdrh ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. 147982c3d636Sdrh */ 148084ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 148184ac9d02Sdanielk1977 rc = 1; 148284ac9d02Sdanielk1977 goto multi_select_end; 148384ac9d02Sdanielk1977 } 1484d8bc7086Sdrh pPrior = p->pPrior; 1485d8bc7086Sdrh if( pPrior->pOrderBy ){ 14864adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1487da93d238Sdrh selectOpName(p->op)); 148884ac9d02Sdanielk1977 rc = 1; 148984ac9d02Sdanielk1977 goto multi_select_end; 149082c3d636Sdrh } 1491a2dc3b1aSdanielk1977 if( pPrior->pLimit ){ 14924adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 14937b58daeaSdrh selectOpName(p->op)); 149484ac9d02Sdanielk1977 rc = 1; 149584ac9d02Sdanielk1977 goto multi_select_end; 14967b58daeaSdrh } 149782c3d636Sdrh 1498d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1499d8bc7086Sdrh */ 15004adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 150184ac9d02Sdanielk1977 if( v==0 ){ 150284ac9d02Sdanielk1977 rc = 1; 150384ac9d02Sdanielk1977 goto multi_select_end; 150484ac9d02Sdanielk1977 } 1505d8bc7086Sdrh 15068cdbf836Sdrh /* If *p this is the right-most select statement, then initialize 15078cdbf836Sdrh ** p->ppOpenTemp to point to pOpenTemp. If *p is not the right most 15088cdbf836Sdrh ** statement then p->ppOpenTemp will have already been initialized 15098cdbf836Sdrh ** by a prior call to this same procedure. Pass along the pOpenTemp 15108cdbf836Sdrh ** pointer to pPrior, the next statement to our left. 1511fbc4ee7bSdrh */ 1512fbc4ee7bSdrh if( p->ppOpenTemp==0 ){ 1513fbc4ee7bSdrh p->ppOpenTemp = &pOpenTemp; 1514fbc4ee7bSdrh } 1515fbc4ee7bSdrh pPrior->ppOpenTemp = p->ppOpenTemp; 1516fbc4ee7bSdrh 15171cc3d75fSdrh /* Create the destination temporary table if necessary 15181cc3d75fSdrh */ 15191cc3d75fSdrh if( eDest==SRT_TempTable ){ 1520b4964b72Sdanielk1977 assert( p->pEList ); 15214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 15228cdbf836Sdrh assert( nAddr==0 ); 15238cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 0); 15241cc3d75fSdrh eDest = SRT_Table; 15251cc3d75fSdrh } 15261cc3d75fSdrh 1527f46f905aSdrh /* Generate code for the left and right SELECT statements. 1528d8bc7086Sdrh */ 152982c3d636Sdrh switch( p->op ){ 1530f46f905aSdrh case TK_ALL: { 1531f46f905aSdrh if( p->pOrderBy==0 ){ 1532a2dc3b1aSdanielk1977 assert( !pPrior->pLimit ); 1533a2dc3b1aSdanielk1977 pPrior->pLimit = p->pLimit; 1534a2dc3b1aSdanielk1977 pPrior->pOffset = p->pOffset; 1535b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 153684ac9d02Sdanielk1977 if( rc ){ 153784ac9d02Sdanielk1977 goto multi_select_end; 153884ac9d02Sdanielk1977 } 1539f46f905aSdrh p->pPrior = 0; 15407b58daeaSdrh p->iLimit = pPrior->iLimit; 15417b58daeaSdrh p->iOffset = pPrior->iOffset; 1542a2dc3b1aSdanielk1977 p->pLimit = 0; 1543a2dc3b1aSdanielk1977 p->pOffset = 0; 1544b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1545f46f905aSdrh p->pPrior = pPrior; 154684ac9d02Sdanielk1977 if( rc ){ 154784ac9d02Sdanielk1977 goto multi_select_end; 154884ac9d02Sdanielk1977 } 1549f46f905aSdrh break; 1550f46f905aSdrh } 1551f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1552f46f905aSdrh } 155382c3d636Sdrh case TK_EXCEPT: 155482c3d636Sdrh case TK_UNION: { 1555d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1556742f947bSdanielk1977 int op = 0; /* One of the SRT_ operations to apply to self */ 1557d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1558a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ 1559c926afbcSdrh ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ 1560dc1bdc4fSdanielk1977 int addr; 156182c3d636Sdrh 1562d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 1563a2dc3b1aSdanielk1977 if( eDest==priorOp && p->pOrderBy==0 && !p->pLimit && !p->pOffset ){ 1564d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1565c926afbcSdrh ** right. 1566d8bc7086Sdrh */ 156782c3d636Sdrh unionTab = iParm; 156882c3d636Sdrh }else{ 1569d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1570d8bc7086Sdrh ** intermediate results. 1571d8bc7086Sdrh */ 157282c3d636Sdrh unionTab = pParse->nTab++; 1573d8bc7086Sdrh if( p->pOrderBy 1574d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 157584ac9d02Sdanielk1977 rc = 1; 157684ac9d02Sdanielk1977 goto multi_select_end; 1577d8bc7086Sdrh } 1578dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0); 1579d8bc7086Sdrh if( p->op!=TK_ALL ){ 15808cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1581dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1582dc1bdc4fSdanielk1977 goto multi_select_end; 1583dc1bdc4fSdanielk1977 } 1584dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, unionTab, 1); 158582c3d636Sdrh } 15868cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 15878cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, 0); 158884ac9d02Sdanielk1977 assert( p->pEList ); 1589d8bc7086Sdrh } 1590d8bc7086Sdrh 1591d8bc7086Sdrh /* Code the SELECT statements to our left 1592d8bc7086Sdrh */ 1593b3bce662Sdanielk1977 assert( !pPrior->pOrderBy ); 1594b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 159584ac9d02Sdanielk1977 if( rc ){ 159684ac9d02Sdanielk1977 goto multi_select_end; 159784ac9d02Sdanielk1977 } 1598d8bc7086Sdrh 1599d8bc7086Sdrh /* Code the current SELECT statement 1600d8bc7086Sdrh */ 1601d8bc7086Sdrh switch( p->op ){ 1602d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1603d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1604d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1605d8bc7086Sdrh } 160682c3d636Sdrh p->pPrior = 0; 1607c926afbcSdrh pOrderBy = p->pOrderBy; 1608c926afbcSdrh p->pOrderBy = 0; 1609a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1610a2dc3b1aSdanielk1977 p->pLimit = 0; 1611a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1612a2dc3b1aSdanielk1977 p->pOffset = 0; 1613b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 161482c3d636Sdrh p->pPrior = pPrior; 1615c926afbcSdrh p->pOrderBy = pOrderBy; 1616a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1617a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1618a2dc3b1aSdanielk1977 p->pOffset = pOffset; 1619be5fd490Sdrh p->iLimit = -1; 1620be5fd490Sdrh p->iOffset = -1; 162184ac9d02Sdanielk1977 if( rc ){ 162284ac9d02Sdanielk1977 goto multi_select_end; 162384ac9d02Sdanielk1977 } 162484ac9d02Sdanielk1977 1625d8bc7086Sdrh 1626d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1627d8bc7086Sdrh ** it is that we currently need. 1628d8bc7086Sdrh */ 1629c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 16306b56344dSdrh int iCont, iBreak, iStart; 163182c3d636Sdrh assert( p->pEList ); 163241202ccaSdrh if( eDest==SRT_Callback ){ 16336a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 163441202ccaSdrh } 16354adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 16364adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 16374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 16387b58daeaSdrh computeLimitRegisters(pParse, p); 16394adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 164038640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 1641d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 164284ac9d02Sdanielk1977 iCont, iBreak, 0); 164384ac9d02Sdanielk1977 if( rc ){ 164484ac9d02Sdanielk1977 rc = 1; 164584ac9d02Sdanielk1977 goto multi_select_end; 164684ac9d02Sdanielk1977 } 16474adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 16484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 16494adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 16504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 165182c3d636Sdrh } 165282c3d636Sdrh break; 165382c3d636Sdrh } 165482c3d636Sdrh case TK_INTERSECT: { 165582c3d636Sdrh int tab1, tab2; 16566b56344dSdrh int iCont, iBreak, iStart; 1657a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; 1658dc1bdc4fSdanielk1977 int addr; 165982c3d636Sdrh 1660d8bc7086Sdrh /* INTERSECT is different from the others since it requires 16616206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1662d8bc7086Sdrh ** by allocating the tables we will need. 1663d8bc7086Sdrh */ 166482c3d636Sdrh tab1 = pParse->nTab++; 166582c3d636Sdrh tab2 = pParse->nTab++; 1666d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 166784ac9d02Sdanielk1977 rc = 1; 166884ac9d02Sdanielk1977 goto multi_select_end; 1669d8bc7086Sdrh } 1670dc1bdc4fSdanielk1977 1671dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab1, 0); 16728cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1673dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1674dc1bdc4fSdanielk1977 goto multi_select_end; 1675dc1bdc4fSdanielk1977 } 1676dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, tab1, 1); 16778cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 16788cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab1, 0); 167984ac9d02Sdanielk1977 assert( p->pEList ); 1680d8bc7086Sdrh 1681d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1682d8bc7086Sdrh */ 1683b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 168484ac9d02Sdanielk1977 if( rc ){ 168584ac9d02Sdanielk1977 goto multi_select_end; 168684ac9d02Sdanielk1977 } 1687d8bc7086Sdrh 1688d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1689d8bc7086Sdrh */ 1690dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab2, 0); 16918cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1692dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1693dc1bdc4fSdanielk1977 goto multi_select_end; 1694dc1bdc4fSdanielk1977 } 1695dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, tab2, 1); 16968cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 16978cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab2, 0); 169882c3d636Sdrh p->pPrior = 0; 1699a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1700a2dc3b1aSdanielk1977 p->pLimit = 0; 1701a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1702a2dc3b1aSdanielk1977 p->pOffset = 0; 1703b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 170482c3d636Sdrh p->pPrior = pPrior; 1705a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1706a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1707a2dc3b1aSdanielk1977 p->pOffset = pOffset; 170884ac9d02Sdanielk1977 if( rc ){ 170984ac9d02Sdanielk1977 goto multi_select_end; 171084ac9d02Sdanielk1977 } 1711d8bc7086Sdrh 1712d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1713d8bc7086Sdrh ** tables. 1714d8bc7086Sdrh */ 171582c3d636Sdrh assert( p->pEList ); 171641202ccaSdrh if( eDest==SRT_Callback ){ 17176a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 171841202ccaSdrh } 17194adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 17204adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 17214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 17227b58daeaSdrh computeLimitRegisters(pParse, p); 17234adee20fSdanielk1977 iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0); 17244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 172538640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 1726d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 172784ac9d02Sdanielk1977 iCont, iBreak, 0); 172884ac9d02Sdanielk1977 if( rc ){ 172984ac9d02Sdanielk1977 rc = 1; 173084ac9d02Sdanielk1977 goto multi_select_end; 173184ac9d02Sdanielk1977 } 17324adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 17334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 17344adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 17354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 17364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 173782c3d636Sdrh break; 173882c3d636Sdrh } 173982c3d636Sdrh } 17408cdbf836Sdrh 17418cdbf836Sdrh /* Make sure all SELECTs in the statement have the same number of elements 17428cdbf836Sdrh ** in their result sets. 17438cdbf836Sdrh */ 174482c3d636Sdrh assert( p->pEList && pPrior->pEList ); 174582c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 17464adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1747da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 174884ac9d02Sdanielk1977 rc = 1; 174984ac9d02Sdanielk1977 goto multi_select_end; 17502282792aSdrh } 175184ac9d02Sdanielk1977 17528cdbf836Sdrh /* Set the number of columns in temporary tables 17538cdbf836Sdrh */ 17548cdbf836Sdrh nCol = p->pEList->nExpr; 17558cdbf836Sdrh while( nAddr>0 ){ 17568cdbf836Sdrh nAddr--; 17578cdbf836Sdrh sqlite3VdbeChangeP2(v, aAddr[nAddr], nCol); 17588cdbf836Sdrh } 17598cdbf836Sdrh 1760fbc4ee7bSdrh /* Compute collating sequences used by either the ORDER BY clause or 1761fbc4ee7bSdrh ** by any temporary tables needed to implement the compound select. 1762fbc4ee7bSdrh ** Attach the KeyInfo structure to all temporary tables. Invoke the 1763fbc4ee7bSdrh ** ORDER BY processing if there is an ORDER BY clause. 17648cdbf836Sdrh ** 17658cdbf836Sdrh ** This section is run by the right-most SELECT statement only. 17668cdbf836Sdrh ** SELECT statements to the left always skip this part. The right-most 17678cdbf836Sdrh ** SELECT might also skip this part if it has no ORDER BY clause and 17688cdbf836Sdrh ** no temp tables are required. 1769fbc4ee7bSdrh */ 1770dc1bdc4fSdanielk1977 if( p->pOrderBy || (pOpenTemp && pOpenTemp->nId>0) ){ 1771fbc4ee7bSdrh int i; /* Loop counter */ 1772fbc4ee7bSdrh KeyInfo *pKeyInfo; /* Collating sequence for the result set */ 1773fbc4ee7bSdrh 17748cdbf836Sdrh assert( p->ppOpenTemp == &pOpenTemp ); 1775fbc4ee7bSdrh pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*sizeof(CollSeq*)); 1776dc1bdc4fSdanielk1977 if( !pKeyInfo ){ 1777dc1bdc4fSdanielk1977 rc = SQLITE_NOMEM; 1778dc1bdc4fSdanielk1977 goto multi_select_end; 1779dc1bdc4fSdanielk1977 } 1780dc1bdc4fSdanielk1977 1781dc1bdc4fSdanielk1977 pKeyInfo->enc = pParse->db->enc; 1782dc1bdc4fSdanielk1977 pKeyInfo->nField = nCol; 1783dc1bdc4fSdanielk1977 1784dc1bdc4fSdanielk1977 for(i=0; i<nCol; i++){ 1785dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = multiSelectCollSeq(pParse, p, i); 1786dc1bdc4fSdanielk1977 if( !pKeyInfo->aColl[i] ){ 1787dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = pParse->db->pDfltColl; 1788dc1bdc4fSdanielk1977 } 1789dc1bdc4fSdanielk1977 } 1790dc1bdc4fSdanielk1977 1791dc1bdc4fSdanielk1977 for(i=0; pOpenTemp && i<pOpenTemp->nId; i++){ 1792dc1bdc4fSdanielk1977 int p3type = (i==0?P3_KEYINFO_HANDOFF:P3_KEYINFO); 1793dc1bdc4fSdanielk1977 int addr = pOpenTemp->a[i].idx; 1794dc1bdc4fSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKeyInfo, p3type); 1795dc1bdc4fSdanielk1977 } 1796dc1bdc4fSdanielk1977 1797dc1bdc4fSdanielk1977 if( p->pOrderBy ){ 1798fbc4ee7bSdrh struct ExprList_item *pOrderByTerm = p->pOrderBy->a; 1799fbc4ee7bSdrh for(i=0; i<p->pOrderBy->nExpr; i++, pOrderByTerm++){ 1800fbc4ee7bSdrh Expr *pExpr = pOrderByTerm->pExpr; 1801fbc4ee7bSdrh char *zName = pOrderByTerm->zName; 1802dc1bdc4fSdanielk1977 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); 1803b3bce662Sdanielk1977 /* assert( !pExpr->pColl ); */ 1804dc1bdc4fSdanielk1977 if( zName ){ 1805dc1bdc4fSdanielk1977 pExpr->pColl = sqlite3LocateCollSeq(pParse, zName, -1); 180684ac9d02Sdanielk1977 }else{ 1807dc1bdc4fSdanielk1977 pExpr->pColl = pKeyInfo->aColl[pExpr->iColumn]; 180884ac9d02Sdanielk1977 } 180984ac9d02Sdanielk1977 } 1810dc1bdc4fSdanielk1977 generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1811dc1bdc4fSdanielk1977 } 1812dc1bdc4fSdanielk1977 1813dc1bdc4fSdanielk1977 if( !pOpenTemp ){ 1814dc1bdc4fSdanielk1977 /* This happens for UNION ALL ... ORDER BY */ 1815dc1bdc4fSdanielk1977 sqliteFree(pKeyInfo); 1816dc1bdc4fSdanielk1977 } 1817dc1bdc4fSdanielk1977 } 1818dc1bdc4fSdanielk1977 1819dc1bdc4fSdanielk1977 multi_select_end: 1820dc1bdc4fSdanielk1977 if( pOpenTemp ){ 1821dc1bdc4fSdanielk1977 sqlite3IdListDelete(pOpenTemp); 1822dc1bdc4fSdanielk1977 } 1823dc1bdc4fSdanielk1977 p->ppOpenTemp = 0; 182484ac9d02Sdanielk1977 return rc; 18252282792aSdrh } 1826b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 18272282792aSdrh 1828b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 18292282792aSdrh /* 1830832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 18316a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 183284e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 18336a3ea0e6Sdrh ** unchanged.) 1834832508b7Sdrh ** 1835832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1836832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1837832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1838832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1839832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1840832508b7Sdrh ** of the subquery rather the result set of the subquery. 1841832508b7Sdrh */ 18426a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 1843b3bce662Sdanielk1977 static void substSelect(Select *, int, ExprList *); /* Forward Decl */ 18446a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1845832508b7Sdrh if( pExpr==0 ) return; 184650350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 184750350a15Sdrh if( pExpr->iColumn<0 ){ 184850350a15Sdrh pExpr->op = TK_NULL; 184950350a15Sdrh }else{ 1850832508b7Sdrh Expr *pNew; 185184e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 1852832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 1853832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 1854832508b7Sdrh assert( pNew!=0 ); 1855832508b7Sdrh pExpr->op = pNew->op; 1856d94a6698Sdrh assert( pExpr->pLeft==0 ); 18574adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 1858d94a6698Sdrh assert( pExpr->pRight==0 ); 18594adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 1860d94a6698Sdrh assert( pExpr->pList==0 ); 18614adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 1862832508b7Sdrh pExpr->iTable = pNew->iTable; 1863832508b7Sdrh pExpr->iColumn = pNew->iColumn; 1864832508b7Sdrh pExpr->iAgg = pNew->iAgg; 18654adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 18664adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 1867a1cb183dSdanielk1977 pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); 1868a1cb183dSdanielk1977 pExpr->flags = pNew->flags; 186950350a15Sdrh } 1870832508b7Sdrh }else{ 18716a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 18726a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 1873b3bce662Sdanielk1977 substSelect(pExpr->pSelect, iTable, pEList); 18746a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 1875832508b7Sdrh } 1876832508b7Sdrh } 1877b3bce662Sdanielk1977 static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ 1878832508b7Sdrh int i; 1879832508b7Sdrh if( pList==0 ) return; 1880832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 18816a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 1882832508b7Sdrh } 1883832508b7Sdrh } 1884b3bce662Sdanielk1977 static void substSelect(Select *p, int iTable, ExprList *pEList){ 1885b3bce662Sdanielk1977 if( !p ) return; 1886b3bce662Sdanielk1977 substExprList(p->pEList, iTable, pEList); 1887b3bce662Sdanielk1977 substExprList(p->pGroupBy, iTable, pEList); 1888b3bce662Sdanielk1977 substExprList(p->pOrderBy, iTable, pEList); 1889b3bce662Sdanielk1977 substExpr(p->pHaving, iTable, pEList); 1890b3bce662Sdanielk1977 substExpr(p->pWhere, iTable, pEList); 1891b3bce662Sdanielk1977 } 1892b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_VIEW) */ 1893832508b7Sdrh 1894b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 1895832508b7Sdrh /* 18961350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 18971350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 18981350b030Sdrh ** occurs. 18991350b030Sdrh ** 19001350b030Sdrh ** To understand the concept of flattening, consider the following 19011350b030Sdrh ** query: 19021350b030Sdrh ** 19031350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 19041350b030Sdrh ** 19051350b030Sdrh ** The default way of implementing this query is to execute the 19061350b030Sdrh ** subquery first and store the results in a temporary table, then 19071350b030Sdrh ** run the outer query on that temporary table. This requires two 19081350b030Sdrh ** passes over the data. Furthermore, because the temporary table 19091350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 1910832508b7Sdrh ** optimized. 19111350b030Sdrh ** 1912832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 19131350b030Sdrh ** a single flat select, like this: 19141350b030Sdrh ** 19151350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 19161350b030Sdrh ** 19171350b030Sdrh ** The code generated for this simpification gives the same result 1918832508b7Sdrh ** but only has to scan the data once. And because indices might 1919832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 1920832508b7Sdrh ** avoided. 19211350b030Sdrh ** 1922832508b7Sdrh ** Flattening is only attempted if all of the following are true: 19231350b030Sdrh ** 1924832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 19251350b030Sdrh ** 1926832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 1927832508b7Sdrh ** 19288af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 19298af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 1930832508b7Sdrh ** 1931832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 1932832508b7Sdrh ** 1933832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 1934832508b7Sdrh ** aggregates. 1935832508b7Sdrh ** 1936832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 1937832508b7Sdrh ** DISTINCT. 1938832508b7Sdrh ** 193908192d5fSdrh ** (7) The subquery has a FROM clause. 194008192d5fSdrh ** 1941df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 1942df199a25Sdrh ** 1943df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 1944df199a25Sdrh ** aggregates. 1945df199a25Sdrh ** 1946df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 1947df199a25Sdrh ** use LIMIT. 1948df199a25Sdrh ** 1949174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 1950174b6195Sdrh ** 19513fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 19523fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 19533fc673e6Sdrh ** 1954832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 1955832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 1956832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 1957832508b7Sdrh ** 1958665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 1959832508b7Sdrh ** If flattening is attempted this routine returns 1. 1960832508b7Sdrh ** 1961832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 1962832508b7Sdrh ** the subquery before this routine runs. 19631350b030Sdrh */ 19648c74a8caSdrh static int flattenSubquery( 19658c74a8caSdrh Parse *pParse, /* The parsing context */ 19668c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 19678c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 19688c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 19698c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 19708c74a8caSdrh ){ 19710bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 1972ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 1973ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 19740bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 19756a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 197691bb0eedSdrh int i; /* Loop counter */ 197791bb0eedSdrh Expr *pWhere; /* The WHERE clause */ 197891bb0eedSdrh struct SrcList_item *pSubitem; /* The subquery */ 19791350b030Sdrh 1980832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 1981832508b7Sdrh */ 1982832508b7Sdrh if( p==0 ) return 0; 1983832508b7Sdrh pSrc = p->pSrc; 1984ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 198591bb0eedSdrh pSubitem = &pSrc->a[iFrom]; 198691bb0eedSdrh pSub = pSubitem->pSelect; 1987832508b7Sdrh assert( pSub!=0 ); 1988832508b7Sdrh if( isAgg && subqueryIsAgg ) return 0; 1989ad3cab52Sdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; 1990832508b7Sdrh pSubSrc = pSub->pSrc; 1991832508b7Sdrh assert( pSubSrc ); 1992a2dc3b1aSdanielk1977 if( (pSub->pLimit && p->pLimit) || pSub->pOffset || 1993a2dc3b1aSdanielk1977 (pSub->pLimit && isAgg) ) return 0; 1994c31c2eb8Sdrh if( pSubSrc->nSrc==0 ) return 0; 1995a2dc3b1aSdanielk1977 if( pSub->isDistinct && (pSrc->nSrc>1 || isAgg) ){ 1996df199a25Sdrh return 0; 1997df199a25Sdrh } 1998a2dc3b1aSdanielk1977 if( p->isDistinct && subqueryIsAgg ) return 0; 1999174b6195Sdrh if( p->pOrderBy && pSub->pOrderBy ) return 0; 2000832508b7Sdrh 20018af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 20028af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 20038af4d3acSdrh ** is not allowed: 20048af4d3acSdrh ** 20058af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 20068af4d3acSdrh ** 20078af4d3acSdrh ** If we flatten the above, we would get 20088af4d3acSdrh ** 20098af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 20108af4d3acSdrh ** 20118af4d3acSdrh ** which is not at all the same thing. 20128af4d3acSdrh */ 20138af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 20148af4d3acSdrh return 0; 20158af4d3acSdrh } 20168af4d3acSdrh 20173fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 20183fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 20193fc673e6Sdrh ** An examples of why this is not allowed: 20203fc673e6Sdrh ** 20213fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 20223fc673e6Sdrh ** 20233fc673e6Sdrh ** If we flatten the above, we would get 20243fc673e6Sdrh ** 20253fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 20263fc673e6Sdrh ** 20273fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 20283fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 20293fc673e6Sdrh */ 20303fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 20313fc673e6Sdrh && pSub->pWhere!=0 ){ 20323fc673e6Sdrh return 0; 20333fc673e6Sdrh } 20343fc673e6Sdrh 20350bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 203663eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 2037832508b7Sdrh */ 2038c31c2eb8Sdrh 2039c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 2040c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 2041c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 2042c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 2043c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 2044c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 2045c31c2eb8Sdrh ** elements we are now copying in. 2046c31c2eb8Sdrh */ 204791bb0eedSdrh iParent = pSubitem->iCursor; 2048c31c2eb8Sdrh { 2049c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 205091bb0eedSdrh int jointype = pSubitem->jointype; 205191bb0eedSdrh Table *pTab = pSubitem->pTab; 2052c31c2eb8Sdrh 205391bb0eedSdrh if( pTab && pTab->isTransient ){ 205491bb0eedSdrh sqlite3DeleteTable(0, pSubitem->pTab); 2055c31c2eb8Sdrh } 205691bb0eedSdrh sqliteFree(pSubitem->zDatabase); 205791bb0eedSdrh sqliteFree(pSubitem->zName); 205891bb0eedSdrh sqliteFree(pSubitem->zAlias); 2059c31c2eb8Sdrh if( nSubSrc>1 ){ 2060c31c2eb8Sdrh int extra = nSubSrc - 1; 2061c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 20624adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 2063c31c2eb8Sdrh } 2064c31c2eb8Sdrh p->pSrc = pSrc; 2065c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 2066c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 2067c31c2eb8Sdrh } 2068c31c2eb8Sdrh } 2069c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 2070c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 2071c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 2072c31c2eb8Sdrh } 20738af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 2074c31c2eb8Sdrh } 2075c31c2eb8Sdrh 2076c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 2077c31c2eb8Sdrh ** references to the iParent in the outer query. 2078c31c2eb8Sdrh ** 2079c31c2eb8Sdrh ** Example: 2080c31c2eb8Sdrh ** 2081c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 2082c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 2083c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 2084c31c2eb8Sdrh ** 2085c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 2086c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 2087c31c2eb8Sdrh */ 20886a3ea0e6Sdrh substExprList(p->pEList, iParent, pSub->pEList); 2089832508b7Sdrh pList = p->pEList; 2090832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 20916977fea8Sdrh Expr *pExpr; 20926977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 20936977fea8Sdrh pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); 2094832508b7Sdrh } 2095832508b7Sdrh } 20961b2e0329Sdrh if( isAgg ){ 20976a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 20986a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 20991b2e0329Sdrh } 2100174b6195Sdrh if( pSub->pOrderBy ){ 2101174b6195Sdrh assert( p->pOrderBy==0 ); 2102174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 2103174b6195Sdrh pSub->pOrderBy = 0; 2104174b6195Sdrh }else if( p->pOrderBy ){ 21056a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 2106174b6195Sdrh } 2107832508b7Sdrh if( pSub->pWhere ){ 21084adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 2109832508b7Sdrh }else{ 2110832508b7Sdrh pWhere = 0; 2111832508b7Sdrh } 2112832508b7Sdrh if( subqueryIsAgg ){ 2113832508b7Sdrh assert( p->pHaving==0 ); 21141b2e0329Sdrh p->pHaving = p->pWhere; 21151b2e0329Sdrh p->pWhere = pWhere; 21166a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 211791bb0eedSdrh p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); 21181b2e0329Sdrh assert( p->pGroupBy==0 ); 21194adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 2120832508b7Sdrh }else{ 21216a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 212291bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); 2123832508b7Sdrh } 2124c31c2eb8Sdrh 2125c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 2126c31c2eb8Sdrh ** outer query is distinct. 2127c31c2eb8Sdrh */ 2128832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 21298c74a8caSdrh 2130a58fdfb1Sdanielk1977 /* 2131a58fdfb1Sdanielk1977 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; 2132a58fdfb1Sdanielk1977 */ 2133a2dc3b1aSdanielk1977 if( pSub->pLimit ){ 2134a2dc3b1aSdanielk1977 p->pLimit = pSub->pLimit; 2135a2dc3b1aSdanielk1977 pSub->pLimit = 0; 2136df199a25Sdrh } 21378c74a8caSdrh 2138c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 2139c31c2eb8Sdrh ** success. 2140c31c2eb8Sdrh */ 21414adee20fSdanielk1977 sqlite3SelectDelete(pSub); 2142832508b7Sdrh return 1; 21431350b030Sdrh } 2144b7f9164eSdrh #endif /* SQLITE_OMIT_VIEW */ 21451350b030Sdrh 21461350b030Sdrh /* 21479562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 21489562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 21499562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 2150e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 21519562b551Sdrh ** simple min() or max() query, then return 0; 21529562b551Sdrh ** 21539562b551Sdrh ** A simply min() or max() query looks like this: 21549562b551Sdrh ** 21559562b551Sdrh ** SELECT min(a) FROM table; 21569562b551Sdrh ** SELECT max(a) FROM table; 21579562b551Sdrh ** 21589562b551Sdrh ** The query may have only a single table in its FROM argument. There 21599562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 21609562b551Sdrh ** be the min() or max() of a single column of the table. The column 21619562b551Sdrh ** in the min() or max() function must be indexed. 21629562b551Sdrh ** 21634adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 21649562b551Sdrh ** See the header comment on that routine for additional information. 21659562b551Sdrh */ 21669562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 21679562b551Sdrh Expr *pExpr; 21689562b551Sdrh int iCol; 21699562b551Sdrh Table *pTab; 21709562b551Sdrh Index *pIdx; 21719562b551Sdrh int base; 21729562b551Sdrh Vdbe *v; 21739562b551Sdrh int seekOp; 21749562b551Sdrh int cont; 21756e17529eSdrh ExprList *pEList, *pList, eList; 21769562b551Sdrh struct ExprList_item eListItem; 21776e17529eSdrh SrcList *pSrc; 21786e17529eSdrh 21799562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 21809562b551Sdrh ** zero if it is not. 21819562b551Sdrh */ 21829562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 21836e17529eSdrh pSrc = p->pSrc; 21846e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 21856e17529eSdrh pEList = p->pEList; 21866e17529eSdrh if( pEList->nExpr!=1 ) return 0; 21876e17529eSdrh pExpr = pEList->a[0].pExpr; 21889562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 21896e17529eSdrh pList = pExpr->pList; 21906e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 21916977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 21924adee20fSdanielk1977 if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){ 21930bce8354Sdrh seekOp = OP_Rewind; 21944adee20fSdanielk1977 }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){ 21950bce8354Sdrh seekOp = OP_Last; 21960bce8354Sdrh }else{ 21970bce8354Sdrh return 0; 21980bce8354Sdrh } 21996e17529eSdrh pExpr = pList->a[0].pExpr; 22009562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 22019562b551Sdrh iCol = pExpr->iColumn; 22026e17529eSdrh pTab = pSrc->a[0].pTab; 22039562b551Sdrh 22049562b551Sdrh /* If we get to here, it means the query is of the correct form. 220517f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 220617f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 220717f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 220817f71934Sdrh ** usable index is found, return 0. 22099562b551Sdrh */ 22109562b551Sdrh if( iCol<0 ){ 22119562b551Sdrh pIdx = 0; 22129562b551Sdrh }else{ 2213dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 22149562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 22159562b551Sdrh assert( pIdx->nColumn>=1 ); 2216dc1bdc4fSdanielk1977 if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break; 22179562b551Sdrh } 22189562b551Sdrh if( pIdx==0 ) return 0; 22199562b551Sdrh } 22209562b551Sdrh 2221e5f50722Sdrh /* Identify column types if we will be using the callback. This 22229562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 2223e5f50722Sdrh ** The column names have already been generated in the calling function. 22249562b551Sdrh */ 22254adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 22269562b551Sdrh if( v==0 ) return 0; 22279562b551Sdrh 22280c37e630Sdrh /* If the output is destined for a temporary table, open that table. 22290c37e630Sdrh */ 22300c37e630Sdrh if( eDest==SRT_TempTable ){ 22314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2232b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1); 22330c37e630Sdrh } 22340c37e630Sdrh 223517f71934Sdrh /* Generating code to find the min or the max. Basically all we have 223617f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 223717f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 223817f71934Sdrh ** or last entry in the main table. 22399562b551Sdrh */ 22404adee20fSdanielk1977 sqlite3CodeVerifySchema(pParse, pTab->iDb); 22416e17529eSdrh base = pSrc->a[0].iCursor; 22427b58daeaSdrh computeLimitRegisters(pParse, p); 22436e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 2244ad6d9460Sdrh sqlite3OpenTableForReading(v, base, pTab); 22456e17529eSdrh } 22464adee20fSdanielk1977 cont = sqlite3VdbeMakeLabel(v); 22479562b551Sdrh if( pIdx==0 ){ 22484adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 22499562b551Sdrh }else{ 22503719d7f9Sdanielk1977 /* Even though the cursor used to open the index here is closed 22513719d7f9Sdanielk1977 ** as soon as a single value has been read from it, allocate it 22523719d7f9Sdanielk1977 ** using (pParse->nTab++) to prevent the cursor id from being 22533719d7f9Sdanielk1977 ** reused. This is important for statements of the form 22543719d7f9Sdanielk1977 ** "INSERT INTO x SELECT max() FROM x". 22553719d7f9Sdanielk1977 */ 22563719d7f9Sdanielk1977 int iIdx; 22573719d7f9Sdanielk1977 iIdx = pParse->nTab++; 22584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 22593719d7f9Sdanielk1977 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 2260d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 22619eb516c0Sdrh if( seekOp==OP_Rewind ){ 22621af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_String, 0, 0); 22631af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); 22641af3fdb4Sdrh seekOp = OP_MoveGt; 22659eb516c0Sdrh } 22663719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, seekOp, iIdx, 0); 22673719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_IdxRecno, iIdx, 0); 22683719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); 22697cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 22709562b551Sdrh } 22715cf8e8c7Sdrh eList.nExpr = 1; 22725cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 22735cf8e8c7Sdrh eList.a = &eListItem; 22745cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 227584ac9d02Sdanielk1977 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0); 22764adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, cont); 22774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 22786e17529eSdrh 22799562b551Sdrh return 1; 22809562b551Sdrh } 22819562b551Sdrh 22829562b551Sdrh /* 2283290c1948Sdrh ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return 2284290c1948Sdrh ** the number of errors seen. 2285290c1948Sdrh ** 2286290c1948Sdrh ** An ORDER BY or GROUP BY is a list of expressions. If any expression 2287290c1948Sdrh ** is an integer constant, then that expression is replaced by the 2288290c1948Sdrh ** corresponding entry in the result set. 2289290c1948Sdrh */ 2290290c1948Sdrh static int processOrderGroupBy( 2291b3bce662Sdanielk1977 NameContext *pNC, /* Name context of the SELECT statement. */ 2292290c1948Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 2293290c1948Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 2294290c1948Sdrh ){ 2295290c1948Sdrh int i; 2296b3bce662Sdanielk1977 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */ 2297b3bce662Sdanielk1977 Parse *pParse = pNC->pParse; /* The result set of the SELECT */ 2298b3bce662Sdanielk1977 assert( pEList ); 2299b3bce662Sdanielk1977 2300290c1948Sdrh if( pOrderBy==0 ) return 0; 2301290c1948Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2302290c1948Sdrh int iCol; 2303290c1948Sdrh Expr *pE = pOrderBy->a[i].pExpr; 2304b3bce662Sdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 2305b3bce662Sdanielk1977 if( iCol>0 && iCol<=pEList->nExpr ){ 2306290c1948Sdrh sqlite3ExprDelete(pE); 2307290c1948Sdrh pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 2308b3bce662Sdanielk1977 }else{ 2309290c1948Sdrh sqlite3ErrorMsg(pParse, 2310290c1948Sdrh "%s BY column number %d out of range - should be " 2311290c1948Sdrh "between 1 and %d", zType, iCol, pEList->nExpr); 2312290c1948Sdrh return 1; 2313290c1948Sdrh } 2314290c1948Sdrh } 2315b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(pNC, pE) ){ 2316b3bce662Sdanielk1977 return 1; 2317b3bce662Sdanielk1977 } 2318b3bce662Sdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 2319b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, 2320b3bce662Sdanielk1977 "%s BY terms must not be non-integer constants", zType); 2321b3bce662Sdanielk1977 return 1; 2322b3bce662Sdanielk1977 } 2323290c1948Sdrh } 2324290c1948Sdrh return 0; 2325290c1948Sdrh } 2326290c1948Sdrh 2327290c1948Sdrh /* 2328b3bce662Sdanielk1977 ** This routine resolves any names used in the result set of the 2329b3bce662Sdanielk1977 ** supplied SELECT statement. If the SELECT statement being resolved 2330b3bce662Sdanielk1977 ** is a sub-select, then pOuterNC is a pointer to the NameContext 2331b3bce662Sdanielk1977 ** of the parent SELECT. 2332b3bce662Sdanielk1977 */ 2333b3bce662Sdanielk1977 int sqlite3SelectResolve( 2334b3bce662Sdanielk1977 Parse *pParse, /* The parser context */ 2335b3bce662Sdanielk1977 Select *p, /* The SELECT statement being coded. */ 2336b3bce662Sdanielk1977 NameContext *pOuterNC /* The outer name context. May be NULL. */ 2337b3bce662Sdanielk1977 ){ 2338b3bce662Sdanielk1977 ExprList *pEList; /* Result set. */ 2339b3bce662Sdanielk1977 int i; /* For-loop variable used in multiple places */ 2340b3bce662Sdanielk1977 NameContext sNC; /* Local name-context */ 2341b3bce662Sdanielk1977 2342b3bce662Sdanielk1977 /* If this routine has run before, return immediately. */ 2343b3bce662Sdanielk1977 if( p->isResolved ){ 2344b3bce662Sdanielk1977 assert( !pOuterNC ); 2345b3bce662Sdanielk1977 return SQLITE_OK; 2346b3bce662Sdanielk1977 } 2347b3bce662Sdanielk1977 p->isResolved = 1; 2348b3bce662Sdanielk1977 2349b3bce662Sdanielk1977 /* If there have already been errors, do nothing. */ 2350b3bce662Sdanielk1977 if( pParse->nErr>0 ){ 2351b3bce662Sdanielk1977 return SQLITE_ERROR; 2352b3bce662Sdanielk1977 } 2353b3bce662Sdanielk1977 2354b3bce662Sdanielk1977 /* Prepare the select statement. This call will allocate all cursors 2355b3bce662Sdanielk1977 ** required to handle the tables and subqueries in the FROM clause. 2356b3bce662Sdanielk1977 */ 2357b3bce662Sdanielk1977 if( prepSelectStmt(pParse, p) ){ 2358b3bce662Sdanielk1977 return SQLITE_ERROR; 2359b3bce662Sdanielk1977 } 2360b3bce662Sdanielk1977 2361a2dc3b1aSdanielk1977 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 2362a2dc3b1aSdanielk1977 ** are not allowed to refer to any names, so pass an empty NameContext. 2363a2dc3b1aSdanielk1977 */ 2364b3bce662Sdanielk1977 sNC.pParse = pParse; 2365b3bce662Sdanielk1977 sNC.hasAgg = 0; 2366b3bce662Sdanielk1977 sNC.nErr = 0; 2367b3bce662Sdanielk1977 sNC.nRef = 0; 2368b3bce662Sdanielk1977 sNC.pEList = 0; 2369a2dc3b1aSdanielk1977 sNC.allowAgg = 0; 2370a2dc3b1aSdanielk1977 sNC.pSrcList = 0; 2371a2dc3b1aSdanielk1977 sNC.pNext = 0; 2372a2dc3b1aSdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pLimit) || 2373a2dc3b1aSdanielk1977 sqlite3ExprResolveNames(&sNC, p->pOffset) ){ 2374a2dc3b1aSdanielk1977 return SQLITE_ERROR; 2375a2dc3b1aSdanielk1977 } 2376a2dc3b1aSdanielk1977 2377a2dc3b1aSdanielk1977 /* Set up the local name-context to pass to ExprResolveNames() to 2378a2dc3b1aSdanielk1977 ** resolve the expression-list. 2379a2dc3b1aSdanielk1977 */ 2380a2dc3b1aSdanielk1977 sNC.allowAgg = 1; 2381a2dc3b1aSdanielk1977 sNC.pSrcList = p->pSrc; 2382a2dc3b1aSdanielk1977 sNC.pNext = pOuterNC; 2383b3bce662Sdanielk1977 2384b3bce662Sdanielk1977 /* NameContext.nDepth stores the depth of recursion for this query. For 2385b3bce662Sdanielk1977 ** an outer query (e.g. SELECT * FROM sqlite_master) this is 1. For 2386b3bce662Sdanielk1977 ** a subquery it is 2. For a subquery of a subquery, 3. And so on. 2387b3bce662Sdanielk1977 ** Parse.nMaxDepth is the maximum depth for any subquery resolved so 2388b3bce662Sdanielk1977 ** far. This is used to determine the number of aggregate contexts 2389b3bce662Sdanielk1977 ** required at runtime. 2390b3bce662Sdanielk1977 */ 2391b3bce662Sdanielk1977 sNC.nDepth = (pOuterNC?pOuterNC->nDepth+1:1); 2392b3bce662Sdanielk1977 if( sNC.nDepth>pParse->nMaxDepth ){ 2393b3bce662Sdanielk1977 pParse->nMaxDepth = sNC.nDepth; 2394b3bce662Sdanielk1977 } 2395b3bce662Sdanielk1977 2396b3bce662Sdanielk1977 /* Resolve names in the result set. */ 2397b3bce662Sdanielk1977 pEList = p->pEList; 2398b3bce662Sdanielk1977 if( !pEList ) return SQLITE_ERROR; 2399b3bce662Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 2400b3bce662Sdanielk1977 Expr *pX = pEList->a[i].pExpr; 2401b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pX) ){ 2402b3bce662Sdanielk1977 return SQLITE_ERROR; 2403b3bce662Sdanielk1977 } 2404b3bce662Sdanielk1977 } 2405b3bce662Sdanielk1977 2406b3bce662Sdanielk1977 /* If there are no aggregate functions in the result-set, and no GROUP BY 2407b3bce662Sdanielk1977 ** expression, do not allow aggregates in any of the other expressions. 2408b3bce662Sdanielk1977 */ 2409b3bce662Sdanielk1977 assert( !p->isAgg ); 2410b3bce662Sdanielk1977 if( p->pGroupBy || sNC.hasAgg ){ 2411b3bce662Sdanielk1977 p->isAgg = 1; 2412b3bce662Sdanielk1977 }else{ 2413b3bce662Sdanielk1977 sNC.allowAgg = 0; 2414b3bce662Sdanielk1977 } 2415b3bce662Sdanielk1977 2416b3bce662Sdanielk1977 /* If a HAVING clause is present, then there must be a GROUP BY clause. 2417b3bce662Sdanielk1977 */ 2418b3bce662Sdanielk1977 if( p->pHaving && !p->pGroupBy ){ 2419b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2420b3bce662Sdanielk1977 return SQLITE_ERROR; 2421b3bce662Sdanielk1977 } 2422b3bce662Sdanielk1977 2423b3bce662Sdanielk1977 /* Add the expression list to the name-context before parsing the 2424b3bce662Sdanielk1977 ** other expressions in the SELECT statement. This is so that 2425b3bce662Sdanielk1977 ** expressions in the WHERE clause (etc.) can refer to expressions by 2426b3bce662Sdanielk1977 ** aliases in the result set. 2427b3bce662Sdanielk1977 ** 2428b3bce662Sdanielk1977 ** Minor point: If this is the case, then the expression will be 2429b3bce662Sdanielk1977 ** re-evaluated for each reference to it. 2430b3bce662Sdanielk1977 */ 2431b3bce662Sdanielk1977 sNC.pEList = p->pEList; 2432b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pWhere) || 2433b3bce662Sdanielk1977 sqlite3ExprResolveNames(&sNC, p->pHaving) || 2434b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") || 2435b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pGroupBy, "GROUP") 2436b3bce662Sdanielk1977 ){ 2437b3bce662Sdanielk1977 return SQLITE_ERROR; 2438b3bce662Sdanielk1977 } 2439b3bce662Sdanielk1977 2440b3bce662Sdanielk1977 return SQLITE_OK; 2441b3bce662Sdanielk1977 } 2442b3bce662Sdanielk1977 2443b3bce662Sdanielk1977 /* 2444b3bce662Sdanielk1977 ** An instance of the following struct is used by sqlite3Select() 2445b3bce662Sdanielk1977 ** to save aggregate related information from the Parse object 2446b3bce662Sdanielk1977 ** at the start of each call and to restore it at the end. See 2447b3bce662Sdanielk1977 ** saveAggregateInfo() and restoreAggregateInfo(). 2448b3bce662Sdanielk1977 */ 2449b3bce662Sdanielk1977 struct AggregateInfo { 2450b3bce662Sdanielk1977 int nAgg; 2451b3bce662Sdanielk1977 AggExpr *aAgg; 2452b3bce662Sdanielk1977 }; 2453b3bce662Sdanielk1977 typedef struct AggregateInfo AggregateInfo; 2454b3bce662Sdanielk1977 2455b3bce662Sdanielk1977 /* 2456b3bce662Sdanielk1977 ** Copy aggregate related information from the Parse structure 2457b3bce662Sdanielk1977 ** into the AggregateInfo structure. Zero the aggregate related 2458b3bce662Sdanielk1977 ** values in the Parse struct. 2459b3bce662Sdanielk1977 */ 2460b3bce662Sdanielk1977 static void saveAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2461b3bce662Sdanielk1977 pInfo->aAgg = pParse->aAgg; 2462b3bce662Sdanielk1977 pInfo->nAgg = pParse->nAgg; 2463b3bce662Sdanielk1977 pParse->aAgg = 0; 2464b3bce662Sdanielk1977 pParse->nAgg = 0; 2465b3bce662Sdanielk1977 } 2466b3bce662Sdanielk1977 2467b3bce662Sdanielk1977 /* 2468b3bce662Sdanielk1977 ** Copy aggregate related information from the AggregateInfo struct 2469b3bce662Sdanielk1977 ** back into the Parse structure. The aggregate related information 2470b3bce662Sdanielk1977 ** currently stored in the Parse structure is deleted. 2471b3bce662Sdanielk1977 */ 2472b3bce662Sdanielk1977 static void restoreAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2473b3bce662Sdanielk1977 sqliteFree(pParse->aAgg); 2474b3bce662Sdanielk1977 pParse->aAgg = pInfo->aAgg; 2475b3bce662Sdanielk1977 pParse->nAgg = pInfo->nAgg; 2476b3bce662Sdanielk1977 } 2477b3bce662Sdanielk1977 2478b3bce662Sdanielk1977 /* 24799bb61fe7Sdrh ** Generate code for the given SELECT statement. 24809bb61fe7Sdrh ** 2481fef5208cSdrh ** The results are distributed in various ways depending on the 2482fef5208cSdrh ** value of eDest and iParm. 2483fef5208cSdrh ** 2484fef5208cSdrh ** eDest Value Result 2485fef5208cSdrh ** ------------ ------------------------------------------- 2486fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2487fef5208cSdrh ** 2488fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2489fef5208cSdrh ** 2490e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2491fef5208cSdrh ** 249282c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 249382c3d636Sdrh ** 24944b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2495c4a3c779Sdrh ** 2496c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 24979bb61fe7Sdrh ** 2498e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2499e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2500e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2501e78e8284Sdrh ** 25029bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 25039bb61fe7Sdrh ** encountered, then an appropriate error message is left in 25049bb61fe7Sdrh ** pParse->zErrMsg. 25059bb61fe7Sdrh ** 25069bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 25079bb61fe7Sdrh ** calling function needs to do that. 25081b2e0329Sdrh ** 25091b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 25101b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 25111b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 25121b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 25131b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 25141b2e0329Sdrh ** can be changed. 2515e78e8284Sdrh ** 2516e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2517e78e8284Sdrh ** 2518e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2519e78e8284Sdrh ** \ \_______ subquery _______/ / 2520e78e8284Sdrh ** \ / 2521e78e8284Sdrh ** \____________________ outer query ___________________/ 2522e78e8284Sdrh ** 2523e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2524e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2525e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2526e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2527e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2528e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 25299bb61fe7Sdrh */ 25304adee20fSdanielk1977 int sqlite3Select( 2531cce7d176Sdrh Parse *pParse, /* The parser context */ 25329bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2533e78e8284Sdrh int eDest, /* How to dispose of the results */ 2534e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2535832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2536832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 253784ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 2538b3bce662Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2539cce7d176Sdrh ){ 2540d8bc7086Sdrh int i; 2541cce7d176Sdrh WhereInfo *pWInfo; 2542cce7d176Sdrh Vdbe *v; 2543b3bce662Sdanielk1977 int isAgg; /* True for select lists like "count(*)" */ 2544a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2545ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 25469bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 25479bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 25482282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 25492282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 255019a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 255119a775c2Sdrh int distinct; /* Table to use for the distinct set */ 25521d83f052Sdrh int rc = 1; /* Value to return from this function */ 2553b3bce662Sdanielk1977 AggregateInfo sAggInfo; 25549bb61fe7Sdrh 25556f8a503dSdanielk1977 if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1; 25564adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 2557daffd0e5Sdrh 2558b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 255982c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 256082c3d636Sdrh */ 256182c3d636Sdrh if( p->pPrior ){ 256284ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 256382c3d636Sdrh } 2564b7f9164eSdrh #endif 256582c3d636Sdrh 2566b3bce662Sdanielk1977 saveAggregateInfo(pParse, &sAggInfo); 2567b3bce662Sdanielk1977 pOrderBy = p->pOrderBy; 2568b3bce662Sdanielk1977 if( eDest==SRT_Union || eDest==SRT_Except || eDest==SRT_Discard ){ 2569b3bce662Sdanielk1977 p->pOrderBy = 0; 2570b3bce662Sdanielk1977 } 2571b3bce662Sdanielk1977 if( sqlite3SelectResolve(pParse, p, 0) ){ 2572b3bce662Sdanielk1977 goto select_end; 2573b3bce662Sdanielk1977 } 2574b3bce662Sdanielk1977 p->pOrderBy = pOrderBy; 2575b3bce662Sdanielk1977 257682c3d636Sdrh /* Make local copies of the parameters for this query. 257782c3d636Sdrh */ 25789bb61fe7Sdrh pTabList = p->pSrc; 25799bb61fe7Sdrh pWhere = p->pWhere; 25802282792aSdrh pGroupBy = p->pGroupBy; 25812282792aSdrh pHaving = p->pHaving; 2582b3bce662Sdanielk1977 isAgg = p->isAgg; 258319a775c2Sdrh isDistinct = p->isDistinct; 2584b3bce662Sdanielk1977 pEList = p->pEList; 2585b3bce662Sdanielk1977 if( pEList==0 ) goto select_end; 25869bb61fe7Sdrh 25879bb61fe7Sdrh /* 25889bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 25899bb61fe7Sdrh ** errors before this routine starts. 25909bb61fe7Sdrh */ 25911d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2592cce7d176Sdrh 25932282792aSdrh /* If writing to memory or generating a set 25942282792aSdrh ** only a single column may be output. 259519a775c2Sdrh */ 259651522cd3Sdrh assert( eDest!=SRT_Exists || pEList->nExpr==1 ); 259793758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2598fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 25994adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2600da93d238Sdrh "a SELECT that is part of an expression"); 26011d83f052Sdrh goto select_end; 260219a775c2Sdrh } 260393758c8dSdanielk1977 #endif 260419a775c2Sdrh 2605c926afbcSdrh /* ORDER BY is ignored for some destinations. 26062282792aSdrh */ 2607c926afbcSdrh switch( eDest ){ 2608c926afbcSdrh case SRT_Union: 2609c926afbcSdrh case SRT_Except: 2610c926afbcSdrh case SRT_Discard: 2611acd4c695Sdrh pOrderBy = 0; 2612c926afbcSdrh break; 2613c926afbcSdrh default: 2614c926afbcSdrh break; 26152282792aSdrh } 26162282792aSdrh 2617d820cb1bSdrh /* Begin generating code. 2618d820cb1bSdrh */ 26194adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2620d820cb1bSdrh if( v==0 ) goto select_end; 2621d820cb1bSdrh 2622e78e8284Sdrh /* Identify column names if we will be using them in a callback. This 2623e78e8284Sdrh ** step is skipped if the output is going to some other destination. 26240bb28106Sdrh */ 26250bb28106Sdrh if( eDest==SRT_Callback ){ 26266a3ea0e6Sdrh generateColumnNames(pParse, pTabList, pEList); 26270bb28106Sdrh } 26280bb28106Sdrh 2629d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2630d820cb1bSdrh */ 263151522cd3Sdrh #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) 2632ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 2633742f947bSdanielk1977 const char *zSavedAuthContext = 0; 2634c31c2eb8Sdrh int needRestoreContext; 2635c31c2eb8Sdrh 2636a76b5dfcSdrh if( pTabList->a[i].pSelect==0 ) continue; 26375cf590c1Sdrh if( pTabList->a[i].zName!=0 ){ 26385cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 26395cf590c1Sdrh pParse->zAuthContext = pTabList->a[i].zName; 2640c31c2eb8Sdrh needRestoreContext = 1; 2641c31c2eb8Sdrh }else{ 2642c31c2eb8Sdrh needRestoreContext = 0; 26435cf590c1Sdrh } 26444adee20fSdanielk1977 sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable, 2645b3bce662Sdanielk1977 pTabList->a[i].iCursor, p, i, &isAgg, 0); 2646c31c2eb8Sdrh if( needRestoreContext ){ 26475cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 26485cf590c1Sdrh } 2649832508b7Sdrh pTabList = p->pSrc; 2650832508b7Sdrh pWhere = p->pWhere; 2651c31c2eb8Sdrh if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ 2652832508b7Sdrh pOrderBy = p->pOrderBy; 2653acd4c695Sdrh } 2654832508b7Sdrh pGroupBy = p->pGroupBy; 2655832508b7Sdrh pHaving = p->pHaving; 2656832508b7Sdrh isDistinct = p->isDistinct; 26571b2e0329Sdrh } 265851522cd3Sdrh #endif 26591b2e0329Sdrh 26606e17529eSdrh /* Check for the special case of a min() or max() function by itself 26616e17529eSdrh ** in the result set. 26626e17529eSdrh */ 26636e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 26646e17529eSdrh rc = 0; 26656e17529eSdrh goto select_end; 26666e17529eSdrh } 26676e17529eSdrh 26681b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 26691b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 26701b2e0329Sdrh */ 2671b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 26721b2e0329Sdrh if( pParent && pParentAgg && 26738c74a8caSdrh flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ 26741b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 2675b3bce662Sdanielk1977 goto select_end; 26761b2e0329Sdrh } 2677b7f9164eSdrh #endif 2678832508b7Sdrh 26797cedc8d4Sdanielk1977 /* If there is an ORDER BY clause, resolve any collation sequences 26807cedc8d4Sdanielk1977 ** names that have been explicitly specified. 26817cedc8d4Sdanielk1977 */ 26827cedc8d4Sdanielk1977 if( pOrderBy ){ 26837cedc8d4Sdanielk1977 for(i=0; i<pOrderBy->nExpr; i++){ 26847cedc8d4Sdanielk1977 if( pOrderBy->a[i].zName ){ 26857cedc8d4Sdanielk1977 pOrderBy->a[i].pExpr->pColl = 26867cedc8d4Sdanielk1977 sqlite3LocateCollSeq(pParse, pOrderBy->a[i].zName, -1); 26877cedc8d4Sdanielk1977 } 26887cedc8d4Sdanielk1977 } 26897cedc8d4Sdanielk1977 if( pParse->nErr ){ 26907cedc8d4Sdanielk1977 goto select_end; 26917cedc8d4Sdanielk1977 } 26927cedc8d4Sdanielk1977 } 26937cedc8d4Sdanielk1977 26947b58daeaSdrh /* Set the limiter. 26957b58daeaSdrh */ 26967b58daeaSdrh computeLimitRegisters(pParse, p); 26977b58daeaSdrh 26982d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 26992d0794e3Sdrh */ 27002d0794e3Sdrh if( eDest==SRT_TempTable ){ 27014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2702b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); 27032d0794e3Sdrh } 27042d0794e3Sdrh 27052282792aSdrh /* Do an analysis of aggregate expressions. 2706efb7251dSdrh */ 2707bb999ef6Sdrh if( isAgg || pGroupBy ){ 2708a58fdfb1Sdanielk1977 NameContext sNC; 2709a58fdfb1Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 2710a58fdfb1Sdanielk1977 sNC.pParse = pParse; 2711a58fdfb1Sdanielk1977 sNC.pSrcList = pTabList; 2712a58fdfb1Sdanielk1977 27130bce8354Sdrh assert( pParse->nAgg==0 ); 2714bb999ef6Sdrh isAgg = 1; 27152282792aSdrh for(i=0; i<pEList->nExpr; i++){ 2716a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pEList->a[i].pExpr) ){ 27171d83f052Sdrh goto select_end; 27182282792aSdrh } 27192282792aSdrh } 27202282792aSdrh if( pGroupBy ){ 27212282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 2722a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pGroupBy->a[i].pExpr) ){ 27231d83f052Sdrh goto select_end; 27242282792aSdrh } 27252282792aSdrh } 27262282792aSdrh } 2727a58fdfb1Sdanielk1977 if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ 27281d83f052Sdrh goto select_end; 27292282792aSdrh } 2730191b690eSdrh if( pOrderBy ){ 2731191b690eSdrh for(i=0; i<pOrderBy->nExpr; i++){ 2732a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pOrderBy->a[i].pExpr) ){ 27331d83f052Sdrh goto select_end; 2734191b690eSdrh } 2735191b690eSdrh } 2736191b690eSdrh } 2737efb7251dSdrh } 2738efb7251dSdrh 27392282792aSdrh /* Reset the aggregator 2740cce7d176Sdrh */ 2741cce7d176Sdrh if( isAgg ){ 2742e159fdf2Sdanielk1977 int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg); 2743e5095355Sdrh for(i=0; i<pParse->nAgg; i++){ 27440bce8354Sdrh FuncDef *pFunc; 27450bce8354Sdrh if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ 27465c2d9155Sdanielk1977 int nExpr = 0; 27475c2d9155Sdanielk1977 #ifdef SQLITE_SSE 27485c2d9155Sdanielk1977 Expr *pAggExpr = pParse->aAgg[i].pExpr; 27495c2d9155Sdanielk1977 if( pAggExpr && pAggExpr->pList ){ 27505c2d9155Sdanielk1977 nExpr = pAggExpr->pList->nExpr; 27515c2d9155Sdanielk1977 } 27525c2d9155Sdanielk1977 #endif 27535c2d9155Sdanielk1977 sqlite3VdbeOp3(v, OP_AggInit, nExpr, i, (char*)pFunc, P3_FUNCDEF); 2754e5095355Sdrh } 2755e5095355Sdrh } 2756e159fdf2Sdanielk1977 if( pGroupBy ){ 2757ce2663ccSdanielk1977 int sz = sizeof(KeyInfo) + pGroupBy->nExpr*sizeof(CollSeq*); 2758ce2663ccSdanielk1977 KeyInfo *pKey = (KeyInfo *)sqliteMalloc(sz); 2759ce2663ccSdanielk1977 if( 0==pKey ){ 2760ce2663ccSdanielk1977 goto select_end; 2761ce2663ccSdanielk1977 } 2762ce2663ccSdanielk1977 pKey->enc = pParse->db->enc; 2763ce2663ccSdanielk1977 pKey->nField = pGroupBy->nExpr; 2764ce2663ccSdanielk1977 for(i=0; i<pGroupBy->nExpr; i++){ 2765ce2663ccSdanielk1977 pKey->aColl[i] = sqlite3ExprCollSeq(pParse, pGroupBy->a[i].pExpr); 2766ce2663ccSdanielk1977 if( !pKey->aColl[i] ){ 2767ce2663ccSdanielk1977 pKey->aColl[i] = pParse->db->pDfltColl; 2768ce2663ccSdanielk1977 } 2769ce2663ccSdanielk1977 } 2770ce2663ccSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF); 27711bee3d7bSdrh } 2772cce7d176Sdrh } 2773cce7d176Sdrh 277451522cd3Sdrh /* Initialize the memory cell to NULL for SRT_Mem or 0 for SRT_Exists 277519a775c2Sdrh */ 277651522cd3Sdrh if( eDest==SRT_Mem || eDest==SRT_Exists ){ 277751522cd3Sdrh sqlite3VdbeAddOp(v, eDest==SRT_Mem ? OP_String8 : OP_Integer, 0, 0); 27784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 277919a775c2Sdrh } 278019a775c2Sdrh 2781832508b7Sdrh /* Open a temporary table to use for the distinct set. 2782cce7d176Sdrh */ 278319a775c2Sdrh if( isDistinct ){ 2784832508b7Sdrh distinct = pParse->nTab++; 2785d3d39e93Sdrh openTempIndex(pParse, p, distinct, 0); 2786832508b7Sdrh }else{ 2787832508b7Sdrh distinct = -1; 2788efb7251dSdrh } 2789832508b7Sdrh 2790832508b7Sdrh /* Begin the database scan 2791832508b7Sdrh */ 2792e6f85e71Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 2793f8db1bc0Sdrh pGroupBy ? 0 : &pOrderBy); 27941d83f052Sdrh if( pWInfo==0 ) goto select_end; 2795cce7d176Sdrh 27962282792aSdrh /* Use the standard inner loop if we are not dealing with 27972282792aSdrh ** aggregates 2798cce7d176Sdrh */ 2799da9d6c45Sdrh if( !isAgg ){ 2800df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 280184ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 28021d83f052Sdrh goto select_end; 2803cce7d176Sdrh } 2804da9d6c45Sdrh } 2805cce7d176Sdrh 2806e3184744Sdrh /* If we are dealing with aggregates, then do the special aggregate 28072282792aSdrh ** processing. 2808efb7251dSdrh */ 28092282792aSdrh else{ 2810268380caSdrh AggExpr *pAgg; 2811a58fdfb1Sdanielk1977 int lbl1 = 0; 2812a58fdfb1Sdanielk1977 pParse->fillAgg = 1; 28132282792aSdrh if( pGroupBy ){ 28142282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 28154adee20fSdanielk1977 sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr); 2816efb7251dSdrh } 2817ededfd5eSdanielk1977 /* No affinity string is attached to the following OP_MakeRecord 2818d3d39e93Sdrh ** because we do not need to do any coercion of datatypes. */ 2819ededfd5eSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0); 28204adee20fSdanielk1977 lbl1 = sqlite3VdbeMakeLabel(v); 28214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1); 2822a58fdfb1Sdanielk1977 } 2823268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 2824268380caSdrh if( pAgg->isAgg ) continue; 28254adee20fSdanielk1977 sqlite3ExprCode(pParse, pAgg->pExpr); 28264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggSet, 0, i); 28272282792aSdrh } 2828a58fdfb1Sdanielk1977 pParse->fillAgg = 0; 2829a58fdfb1Sdanielk1977 if( lbl1<0 ){ 28304adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, lbl1); 28312282792aSdrh } 2832268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 28332282792aSdrh Expr *pE; 2834268380caSdrh int nExpr; 2835268380caSdrh FuncDef *pDef; 2836268380caSdrh if( !pAgg->isAgg ) continue; 2837268380caSdrh assert( pAgg->pFunc!=0 ); 2838268380caSdrh assert( pAgg->pFunc->xStep!=0 ); 2839268380caSdrh pDef = pAgg->pFunc; 2840268380caSdrh pE = pAgg->pExpr; 2841268380caSdrh assert( pE!=0 ); 28422282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 2843f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pE->pList); 28444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 2845dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2846dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 2847dc1bdc4fSdanielk1977 int j; 2848dc1bdc4fSdanielk1977 for(j=0; !pColl && j<nExpr; j++){ 2849dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr); 2850dc1bdc4fSdanielk1977 } 2851dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 2852dc1bdc4fSdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 2853dc1bdc4fSdanielk1977 } 28541f55c056Sdanielk1977 sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_FUNCDEF); 28552282792aSdrh } 28562282792aSdrh } 28572282792aSdrh 2858cce7d176Sdrh /* End the database scan loop. 2859cce7d176Sdrh */ 28604adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 2861cce7d176Sdrh 28622282792aSdrh /* If we are processing aggregates, we need to set up a second loop 28632282792aSdrh ** over all of the aggregate values and process them. 28642282792aSdrh */ 28652282792aSdrh if( isAgg ){ 28664adee20fSdanielk1977 int endagg = sqlite3VdbeMakeLabel(v); 28672282792aSdrh int startagg; 28684adee20fSdanielk1977 startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg); 28692282792aSdrh if( pHaving ){ 28704adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pHaving, startagg, 1); 28712282792aSdrh } 2872df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 287384ac9d02Sdanielk1977 iParm, startagg, endagg, aff) ){ 28741d83f052Sdrh goto select_end; 28752282792aSdrh } 28764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, startagg); 28774adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endagg); 28784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Noop, 0, 0); 28792282792aSdrh } 28802282792aSdrh 2881cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 2882cce7d176Sdrh ** and send them to the callback one by one. 2883cce7d176Sdrh */ 2884cce7d176Sdrh if( pOrderBy ){ 2885ffbc3088Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 2886cce7d176Sdrh } 28876a535340Sdrh 288893758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2889f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 2890f620b4e2Sdrh ** temporary table. So delete the subquery structure from the parent 2891f620b4e2Sdrh ** to prevent this subquery from being evaluated again and to force the 2892f620b4e2Sdrh ** the use of the temporary table. 2893f620b4e2Sdrh */ 2894f620b4e2Sdrh if( pParent ){ 2895f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 2896f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 28974adee20fSdanielk1977 sqlite3SelectDelete(p); 2898f620b4e2Sdrh pParent->pSrc->a[parentTab].pSelect = 0; 2899f620b4e2Sdrh } 290093758c8dSdanielk1977 #endif 2901f620b4e2Sdrh 29021d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 29031d83f052Sdrh ** to indicate no errors. 29041d83f052Sdrh */ 29051d83f052Sdrh rc = 0; 29061d83f052Sdrh 29071d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 29081d83f052Sdrh ** successful coding of the SELECT. 29091d83f052Sdrh */ 29101d83f052Sdrh select_end: 2911b3bce662Sdanielk1977 restoreAggregateInfo(pParse, &sAggInfo); 29121d83f052Sdrh return rc; 2913cce7d176Sdrh } 2914