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*5c2d9155Sdanielk1977 ** $Id: select.c,v 1.248 2005/05/26 14:41:47 danielk1977 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; 10737c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 107454473229Sdrh Expr *pE = a[k].pExpr; 107554473229Sdrh if( pE->op!=TK_ALL && 107654473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 107754473229Sdrh /* This particular expression does not need to be expanded. 107854473229Sdrh */ 10794adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 10807c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 10817c917d19Sdrh a[k].pExpr = 0; 10827c917d19Sdrh a[k].zName = 0; 10837c917d19Sdrh }else{ 108454473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 108554473229Sdrh ** expanded. */ 108654473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 1087cf55b7aeSdrh char *zTName; /* text of name of TABLE */ 108854473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 1089cf55b7aeSdrh zTName = sqlite3NameFromToken(&pE->pLeft->token); 109054473229Sdrh }else{ 1091cf55b7aeSdrh zTName = 0; 109254473229Sdrh } 1093290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1094290c1948Sdrh Table *pTab = pFrom->pTab; 1095290c1948Sdrh char *zTabName = pFrom->zAlias; 109654473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 109754473229Sdrh zTabName = pTab->zName; 109854473229Sdrh } 1099cf55b7aeSdrh if( zTName && (zTabName==0 || zTabName[0]==0 || 1100cf55b7aeSdrh sqlite3StrICmp(zTName, zTabName)!=0) ){ 110154473229Sdrh continue; 110254473229Sdrh } 110354473229Sdrh tableSeen = 1; 1104d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 110522f70c32Sdrh Expr *pExpr, *pLeft, *pRight; 1106ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 1107ad2d8307Sdrh 110891bb0eedSdrh if( i>0 ){ 110991bb0eedSdrh struct SrcList_item *pLeft = &pTabList->a[i-1]; 111091bb0eedSdrh if( (pLeft->jointype & JT_NATURAL)!=0 && 111191bb0eedSdrh columnIndex(pLeft->pTab, zName)>=0 ){ 1112ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 1113ad2d8307Sdrh ** table on the right */ 1114ad2d8307Sdrh continue; 1115ad2d8307Sdrh } 111691bb0eedSdrh if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ 1117ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 1118ad2d8307Sdrh ** using clause from the table on the right. */ 1119ad2d8307Sdrh continue; 1120ad2d8307Sdrh } 112191bb0eedSdrh } 11224adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 112322f70c32Sdrh if( pRight==0 ) break; 112491bb0eedSdrh setToken(&pRight->token, zName); 11254b59ab5eSdrh if( zTabName && pTabList->nSrc>1 ){ 11264adee20fSdanielk1977 pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 11274adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 112822f70c32Sdrh if( pExpr==0 ) break; 112991bb0eedSdrh setToken(&pLeft->token, zTabName); 113091bb0eedSdrh setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); 11316977fea8Sdrh pExpr->span.dyn = 1; 11326977fea8Sdrh pExpr->token.z = 0; 11336977fea8Sdrh pExpr->token.n = 0; 11346977fea8Sdrh pExpr->token.dyn = 0; 113522f70c32Sdrh }else{ 113622f70c32Sdrh pExpr = pRight; 11376977fea8Sdrh pExpr->span = pExpr->token; 113822f70c32Sdrh } 113979d5f63fSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); 1140d8bc7086Sdrh } 1141d8bc7086Sdrh } 114254473229Sdrh if( !tableSeen ){ 1143cf55b7aeSdrh if( zTName ){ 1144cf55b7aeSdrh sqlite3ErrorMsg(pParse, "no such table: %s", zTName); 1145f5db2d3eSdrh }else{ 11464adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1147f5db2d3eSdrh } 114854473229Sdrh rc = 1; 114954473229Sdrh } 1150cf55b7aeSdrh sqliteFree(zTName); 11517c917d19Sdrh } 11527c917d19Sdrh } 11534adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 11547c917d19Sdrh p->pEList = pNew; 1155d8bc7086Sdrh } 115654473229Sdrh return rc; 1157d8bc7086Sdrh } 1158d8bc7086Sdrh 1159d8bc7086Sdrh /* 1160ff78bd2fSdrh ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers 1161ff78bd2fSdrh ** in a select structure. It just sets the pointers to NULL. This 1162ff78bd2fSdrh ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect 1163ff78bd2fSdrh ** pointer is not NULL, this routine is called recursively on that pointer. 1164ff78bd2fSdrh ** 1165ff78bd2fSdrh ** This routine is called on the Select structure that defines a 1166ff78bd2fSdrh ** VIEW in order to undo any bindings to tables. This is necessary 1167ff78bd2fSdrh ** because those tables might be DROPed by a subsequent SQL command. 11685cf590c1Sdrh ** If the bindings are not removed, then the Select.pSrc->a[].pTab field 11695cf590c1Sdrh ** will be left pointing to a deallocated Table structure after the 11705cf590c1Sdrh ** DROP and a coredump will occur the next time the VIEW is used. 1171ff78bd2fSdrh */ 11725338a5f7Sdanielk1977 #if 0 11734adee20fSdanielk1977 void sqlite3SelectUnbind(Select *p){ 1174ff78bd2fSdrh int i; 1175ad3cab52Sdrh SrcList *pSrc = p->pSrc; 117691bb0eedSdrh struct SrcList_item *pItem; 1177ff78bd2fSdrh Table *pTab; 1178ff78bd2fSdrh if( p==0 ) return; 117991bb0eedSdrh for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){ 118091bb0eedSdrh if( (pTab = pItem->pTab)!=0 ){ 1181ff78bd2fSdrh if( pTab->isTransient ){ 11824adee20fSdanielk1977 sqlite3DeleteTable(0, pTab); 1183ff78bd2fSdrh } 118491bb0eedSdrh pItem->pTab = 0; 118591bb0eedSdrh if( pItem->pSelect ){ 118691bb0eedSdrh sqlite3SelectUnbind(pItem->pSelect); 1187ff78bd2fSdrh } 1188ff78bd2fSdrh } 1189ff78bd2fSdrh } 1190ff78bd2fSdrh } 11915338a5f7Sdanielk1977 #endif 1192ff78bd2fSdrh 119393758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 1194ff78bd2fSdrh /* 1195d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1196d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1197967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1198d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1199d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1200d8bc7086Sdrh ** 1201d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1202d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1203d8bc7086Sdrh ** 1204d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1205d8bc7086Sdrh ** of errors is returned. 1206d8bc7086Sdrh */ 1207d8bc7086Sdrh static int matchOrderbyToColumn( 1208d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1209d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1210d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1211e4de1febSdrh int iTable, /* Insert this value in iTable */ 1212d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1213d8bc7086Sdrh ){ 1214d8bc7086Sdrh int nErr = 0; 1215d8bc7086Sdrh int i, j; 1216d8bc7086Sdrh ExprList *pEList; 1217d8bc7086Sdrh 1218daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1219d8bc7086Sdrh if( mustComplete ){ 1220d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1221d8bc7086Sdrh } 12229b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 1223d8bc7086Sdrh return 1; 1224d8bc7086Sdrh } 1225d8bc7086Sdrh if( pSelect->pPrior ){ 122692cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 122792cd52f5Sdrh return 1; 122892cd52f5Sdrh } 1229d8bc7086Sdrh } 1230d8bc7086Sdrh pEList = pSelect->pEList; 1231d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1232d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1233e4de1febSdrh int iCol = -1; 1234d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 12354adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1236e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 12374adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1238da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1239e4de1febSdrh iCol, pEList->nExpr); 1240e4de1febSdrh nErr++; 1241e4de1febSdrh break; 1242e4de1febSdrh } 1243fcb78a49Sdrh if( !mustComplete ) continue; 1244e4de1febSdrh iCol--; 1245e4de1febSdrh } 1246e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 12474cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1248a76b5dfcSdrh char *zName, *zLabel; 1249a76b5dfcSdrh zName = pEList->a[j].zName; 1250a99db3b6Sdrh zLabel = sqlite3NameFromToken(&pE->token); 1251a99db3b6Sdrh assert( zLabel!=0 ); 12524adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1253e4de1febSdrh iCol = j; 1254d8bc7086Sdrh } 12556e142f54Sdrh sqliteFree(zLabel); 1256d8bc7086Sdrh } 12574adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1258e4de1febSdrh iCol = j; 1259d8bc7086Sdrh } 1260e4de1febSdrh } 1261e4de1febSdrh if( iCol>=0 ){ 1262967e8b73Sdrh pE->op = TK_COLUMN; 1263e4de1febSdrh pE->iColumn = iCol; 1264d8bc7086Sdrh pE->iTable = iTable; 1265a58fdfb1Sdanielk1977 pE->iAgg = -1; 1266d8bc7086Sdrh pOrderBy->a[i].done = 1; 1267d8bc7086Sdrh } 1268e4de1febSdrh if( iCol<0 && mustComplete ){ 12694adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1270da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1271d8bc7086Sdrh nErr++; 1272d8bc7086Sdrh break; 1273d8bc7086Sdrh } 1274d8bc7086Sdrh } 1275d8bc7086Sdrh return nErr; 1276d8bc7086Sdrh } 127793758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */ 1278d8bc7086Sdrh 1279d8bc7086Sdrh /* 1280d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1281d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1282d8bc7086Sdrh */ 12834adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1284d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1285d8bc7086Sdrh if( v==0 ){ 12864adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1287d8bc7086Sdrh } 1288d8bc7086Sdrh return v; 1289d8bc7086Sdrh } 1290d8bc7086Sdrh 1291d8bc7086Sdrh /* 12927b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 1293a2dc3b1aSdanielk1977 ** pLimit and pOffset expressions. nLimit and nOffset hold the expressions 12947b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 1295a2dc3b1aSdanielk1977 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset 1296a2dc3b1aSdanielk1977 ** are the integer memory register numbers for counters used to compute 1297a2dc3b1aSdanielk1977 ** the limit and offset. If there is no limit and/or offset, then 1298a2dc3b1aSdanielk1977 ** iLimit and iOffset are negative. 12997b58daeaSdrh ** 13007b58daeaSdrh ** This routine changes the values if iLimit and iOffset only if 13017b58daeaSdrh ** a limit or offset is defined by nLimit and nOffset. iLimit and 13027b58daeaSdrh ** iOffset should have been preset to appropriate default values 13037b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 13047b58daeaSdrh ** Only if nLimit>=0 or nOffset>0 do the limit registers get 13057b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 13067b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 13077b58daeaSdrh ** SELECT statements. 13087b58daeaSdrh */ 13097b58daeaSdrh static void computeLimitRegisters(Parse *pParse, Select *p){ 13107b58daeaSdrh /* 13117b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 13127b58daeaSdrh ** contraversy about what the correct behavior should be. 13137b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 13147b58daeaSdrh ** no rows. 13157b58daeaSdrh */ 1316a2dc3b1aSdanielk1977 if( p->pLimit ){ 13177b58daeaSdrh int iMem = pParse->nMem++; 13184adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13197b58daeaSdrh if( v==0 ) return; 1320a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pLimit); 1321a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1322a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1324ad6d9460Sdrh VdbeComment((v, "# LIMIT counter")); 13257b58daeaSdrh p->iLimit = iMem; 13267b58daeaSdrh } 1327a2dc3b1aSdanielk1977 if( p->pOffset ){ 13287b58daeaSdrh int iMem = pParse->nMem++; 13294adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13307b58daeaSdrh if( v==0 ) return; 1331a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pOffset); 1332a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1333a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1335ad6d9460Sdrh VdbeComment((v, "# OFFSET counter")); 13367b58daeaSdrh p->iOffset = iMem; 13377b58daeaSdrh } 13387b58daeaSdrh } 13397b58daeaSdrh 13407b58daeaSdrh /* 1341d3d39e93Sdrh ** Generate VDBE instructions that will open a transient table that 1342d3d39e93Sdrh ** will be used for an index or to store keyed results for a compound 1343d3d39e93Sdrh ** select. In other words, open a transient table that needs a 1344d3d39e93Sdrh ** KeyInfo structure. The number of columns in the KeyInfo is determined 1345d3d39e93Sdrh ** by the result set of the SELECT statement in the second argument. 1346d3d39e93Sdrh ** 1347dc1bdc4fSdanielk1977 ** Specifically, this routine is called to open an index table for 1348dc1bdc4fSdanielk1977 ** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not 1349dc1bdc4fSdanielk1977 ** UNION ALL). 1350dc1bdc4fSdanielk1977 ** 1351d3d39e93Sdrh ** Make the new table a KeyAsData table if keyAsData is true. 1352dc1bdc4fSdanielk1977 ** 1353dc1bdc4fSdanielk1977 ** The value returned is the address of the OP_OpenTemp instruction. 1354d3d39e93Sdrh */ 1355dc1bdc4fSdanielk1977 static int openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){ 1356d3d39e93Sdrh KeyInfo *pKeyInfo; 1357736c22b8Sdrh int nColumn; 13589bb575fdSdrh sqlite3 *db = pParse->db; 1359d3d39e93Sdrh int i; 1360d3d39e93Sdrh Vdbe *v = pParse->pVdbe; 1361dc1bdc4fSdanielk1977 int addr; 1362d3d39e93Sdrh 13639b3187e1Sdrh if( prepSelectStmt(pParse, p) ){ 1364dc1bdc4fSdanielk1977 return 0; 1365736c22b8Sdrh } 1366736c22b8Sdrh nColumn = p->pEList->nExpr; 1367d3d39e93Sdrh pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) ); 1368dc1bdc4fSdanielk1977 if( pKeyInfo==0 ) return 0; 136991bb0eedSdrh pKeyInfo->enc = db->enc; 1370d3d39e93Sdrh pKeyInfo->nField = nColumn; 1371d3d39e93Sdrh for(i=0; i<nColumn; i++){ 1372dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr); 1373dc1bdc4fSdanielk1977 if( !pKeyInfo->aColl[i] ){ 1374d3d39e93Sdrh pKeyInfo->aColl[i] = db->pDfltColl; 1375d3d39e93Sdrh } 1376dc1bdc4fSdanielk1977 } 1377dc1bdc4fSdanielk1977 addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0, 1378dc1bdc4fSdanielk1977 (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 1379d3d39e93Sdrh if( keyAsData ){ 1380d3d39e93Sdrh sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1); 1381d3d39e93Sdrh } 1382dc1bdc4fSdanielk1977 return addr; 1383dc1bdc4fSdanielk1977 } 1384dc1bdc4fSdanielk1977 1385b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1386fbc4ee7bSdrh /* 13878cdbf836Sdrh ** Add the address "addr" to the set of all OpenTemp opcode addresses 13888cdbf836Sdrh ** that are being accumulated in p->ppOpenTemp. 1389fbc4ee7bSdrh */ 13908cdbf836Sdrh static int multiSelectOpenTempAddr(Select *p, int addr){ 13918cdbf836Sdrh IdList *pList = *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0); 1392fbc4ee7bSdrh if( pList==0 ){ 1393dc1bdc4fSdanielk1977 return SQLITE_NOMEM; 1394dc1bdc4fSdanielk1977 } 1395fbc4ee7bSdrh pList->a[pList->nId-1].idx = addr; 1396dc1bdc4fSdanielk1977 return SQLITE_OK; 1397dc1bdc4fSdanielk1977 } 1398b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1399dc1bdc4fSdanielk1977 1400b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1401fbc4ee7bSdrh /* 1402fbc4ee7bSdrh ** Return the appropriate collating sequence for the iCol-th column of 1403fbc4ee7bSdrh ** the result set for the compound-select statement "p". Return NULL if 1404fbc4ee7bSdrh ** the column has no default collating sequence. 1405fbc4ee7bSdrh ** 1406fbc4ee7bSdrh ** The collating sequence for the compound select is taken from the 1407fbc4ee7bSdrh ** left-most term of the select that has a collating sequence. 1408fbc4ee7bSdrh */ 1409dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ 1410fbc4ee7bSdrh CollSeq *pRet; 1411dc1bdc4fSdanielk1977 if( p->pPrior ){ 1412dc1bdc4fSdanielk1977 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); 1413fbc4ee7bSdrh }else{ 1414fbc4ee7bSdrh pRet = 0; 1415dc1bdc4fSdanielk1977 } 1416fbc4ee7bSdrh if( pRet==0 ){ 1417dc1bdc4fSdanielk1977 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); 1418dc1bdc4fSdanielk1977 } 1419dc1bdc4fSdanielk1977 return pRet; 1420d3d39e93Sdrh } 1421b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1422d3d39e93Sdrh 1423b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1424d3d39e93Sdrh /* 142582c3d636Sdrh ** This routine is called to process a query that is really the union 142682c3d636Sdrh ** or intersection of two or more separate queries. 1427c926afbcSdrh ** 1428e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1429e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1430e78e8284Sdrh ** in which case this routine will be called recursively. 1431e78e8284Sdrh ** 1432e78e8284Sdrh ** The results of the total query are to be written into a destination 1433e78e8284Sdrh ** of type eDest with parameter iParm. 1434e78e8284Sdrh ** 1435e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1436e78e8284Sdrh ** 1437e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1438e78e8284Sdrh ** 1439e78e8284Sdrh ** This statement is parsed up as follows: 1440e78e8284Sdrh ** 1441e78e8284Sdrh ** SELECT c FROM t3 1442e78e8284Sdrh ** | 1443e78e8284Sdrh ** `-----> SELECT b FROM t2 1444e78e8284Sdrh ** | 14454b11c6d3Sjplyon ** `------> SELECT a FROM t1 1446e78e8284Sdrh ** 1447e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1448e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1449e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1450e78e8284Sdrh ** 1451e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1452e78e8284Sdrh ** individual selects always group from left to right. 145382c3d636Sdrh */ 145484ac9d02Sdanielk1977 static int multiSelect( 1455fbc4ee7bSdrh Parse *pParse, /* Parsing context */ 1456fbc4ee7bSdrh Select *p, /* The right-most of SELECTs to be coded */ 1457fbc4ee7bSdrh int eDest, /* \___ Store query results as specified */ 1458fbc4ee7bSdrh int iParm, /* / by these two parameters. */ 145984ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 146084ac9d02Sdanielk1977 ){ 146184ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 146210e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 146310e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 1464fbc4ee7bSdrh IdList *pOpenTemp = 0;/* OP_OpenTemp opcodes that need a KeyInfo */ 14658cdbf836Sdrh int aAddr[5]; /* Addresses of SetNumColumns operators */ 14668cdbf836Sdrh int nAddr = 0; /* Number used */ 14678cdbf836Sdrh int nCol; /* Number of columns in the result set */ 146882c3d636Sdrh 14697b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 1470fbc4ee7bSdrh ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. 147182c3d636Sdrh */ 147284ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 147384ac9d02Sdanielk1977 rc = 1; 147484ac9d02Sdanielk1977 goto multi_select_end; 147584ac9d02Sdanielk1977 } 1476d8bc7086Sdrh pPrior = p->pPrior; 1477d8bc7086Sdrh if( pPrior->pOrderBy ){ 14784adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1479da93d238Sdrh selectOpName(p->op)); 148084ac9d02Sdanielk1977 rc = 1; 148184ac9d02Sdanielk1977 goto multi_select_end; 148282c3d636Sdrh } 1483a2dc3b1aSdanielk1977 if( pPrior->pLimit ){ 14844adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 14857b58daeaSdrh selectOpName(p->op)); 148684ac9d02Sdanielk1977 rc = 1; 148784ac9d02Sdanielk1977 goto multi_select_end; 14887b58daeaSdrh } 148982c3d636Sdrh 1490d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1491d8bc7086Sdrh */ 14924adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 149384ac9d02Sdanielk1977 if( v==0 ){ 149484ac9d02Sdanielk1977 rc = 1; 149584ac9d02Sdanielk1977 goto multi_select_end; 149684ac9d02Sdanielk1977 } 1497d8bc7086Sdrh 14988cdbf836Sdrh /* If *p this is the right-most select statement, then initialize 14998cdbf836Sdrh ** p->ppOpenTemp to point to pOpenTemp. If *p is not the right most 15008cdbf836Sdrh ** statement then p->ppOpenTemp will have already been initialized 15018cdbf836Sdrh ** by a prior call to this same procedure. Pass along the pOpenTemp 15028cdbf836Sdrh ** pointer to pPrior, the next statement to our left. 1503fbc4ee7bSdrh */ 1504fbc4ee7bSdrh if( p->ppOpenTemp==0 ){ 1505fbc4ee7bSdrh p->ppOpenTemp = &pOpenTemp; 1506fbc4ee7bSdrh } 1507fbc4ee7bSdrh pPrior->ppOpenTemp = p->ppOpenTemp; 1508fbc4ee7bSdrh 15091cc3d75fSdrh /* Create the destination temporary table if necessary 15101cc3d75fSdrh */ 15111cc3d75fSdrh if( eDest==SRT_TempTable ){ 1512b4964b72Sdanielk1977 assert( p->pEList ); 15134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 15148cdbf836Sdrh assert( nAddr==0 ); 15158cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 0); 15161cc3d75fSdrh eDest = SRT_Table; 15171cc3d75fSdrh } 15181cc3d75fSdrh 1519f46f905aSdrh /* Generate code for the left and right SELECT statements. 1520d8bc7086Sdrh */ 152182c3d636Sdrh switch( p->op ){ 1522f46f905aSdrh case TK_ALL: { 1523f46f905aSdrh if( p->pOrderBy==0 ){ 1524a2dc3b1aSdanielk1977 assert( !pPrior->pLimit ); 1525a2dc3b1aSdanielk1977 pPrior->pLimit = p->pLimit; 1526a2dc3b1aSdanielk1977 pPrior->pOffset = p->pOffset; 1527b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 152884ac9d02Sdanielk1977 if( rc ){ 152984ac9d02Sdanielk1977 goto multi_select_end; 153084ac9d02Sdanielk1977 } 1531f46f905aSdrh p->pPrior = 0; 15327b58daeaSdrh p->iLimit = pPrior->iLimit; 15337b58daeaSdrh p->iOffset = pPrior->iOffset; 1534a2dc3b1aSdanielk1977 p->pLimit = 0; 1535a2dc3b1aSdanielk1977 p->pOffset = 0; 1536b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1537f46f905aSdrh p->pPrior = pPrior; 153884ac9d02Sdanielk1977 if( rc ){ 153984ac9d02Sdanielk1977 goto multi_select_end; 154084ac9d02Sdanielk1977 } 1541f46f905aSdrh break; 1542f46f905aSdrh } 1543f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1544f46f905aSdrh } 154582c3d636Sdrh case TK_EXCEPT: 154682c3d636Sdrh case TK_UNION: { 1547d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1548742f947bSdanielk1977 int op = 0; /* One of the SRT_ operations to apply to self */ 1549d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1550a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ 1551c926afbcSdrh ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ 1552dc1bdc4fSdanielk1977 int addr; 155382c3d636Sdrh 1554d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 1555a2dc3b1aSdanielk1977 if( eDest==priorOp && p->pOrderBy==0 && !p->pLimit && !p->pOffset ){ 1556d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1557c926afbcSdrh ** right. 1558d8bc7086Sdrh */ 155982c3d636Sdrh unionTab = iParm; 156082c3d636Sdrh }else{ 1561d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1562d8bc7086Sdrh ** intermediate results. 1563d8bc7086Sdrh */ 156482c3d636Sdrh unionTab = pParse->nTab++; 1565d8bc7086Sdrh if( p->pOrderBy 1566d8bc7086Sdrh && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ 156784ac9d02Sdanielk1977 rc = 1; 156884ac9d02Sdanielk1977 goto multi_select_end; 1569d8bc7086Sdrh } 1570dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0); 1571d8bc7086Sdrh if( p->op!=TK_ALL ){ 15728cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1573dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1574dc1bdc4fSdanielk1977 goto multi_select_end; 1575dc1bdc4fSdanielk1977 } 1576dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, unionTab, 1); 157782c3d636Sdrh } 15788cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 15798cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, 0); 158084ac9d02Sdanielk1977 assert( p->pEList ); 1581d8bc7086Sdrh } 1582d8bc7086Sdrh 1583d8bc7086Sdrh /* Code the SELECT statements to our left 1584d8bc7086Sdrh */ 1585b3bce662Sdanielk1977 assert( !pPrior->pOrderBy ); 1586b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 158784ac9d02Sdanielk1977 if( rc ){ 158884ac9d02Sdanielk1977 goto multi_select_end; 158984ac9d02Sdanielk1977 } 1590d8bc7086Sdrh 1591d8bc7086Sdrh /* Code the current SELECT statement 1592d8bc7086Sdrh */ 1593d8bc7086Sdrh switch( p->op ){ 1594d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1595d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1596d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1597d8bc7086Sdrh } 159882c3d636Sdrh p->pPrior = 0; 1599c926afbcSdrh pOrderBy = p->pOrderBy; 1600c926afbcSdrh p->pOrderBy = 0; 1601a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1602a2dc3b1aSdanielk1977 p->pLimit = 0; 1603a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1604a2dc3b1aSdanielk1977 p->pOffset = 0; 1605b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 160682c3d636Sdrh p->pPrior = pPrior; 1607c926afbcSdrh p->pOrderBy = pOrderBy; 1608a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1609a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1610a2dc3b1aSdanielk1977 p->pOffset = pOffset; 1611be5fd490Sdrh p->iLimit = -1; 1612be5fd490Sdrh p->iOffset = -1; 161384ac9d02Sdanielk1977 if( rc ){ 161484ac9d02Sdanielk1977 goto multi_select_end; 161584ac9d02Sdanielk1977 } 161684ac9d02Sdanielk1977 1617d8bc7086Sdrh 1618d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1619d8bc7086Sdrh ** it is that we currently need. 1620d8bc7086Sdrh */ 1621c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 16226b56344dSdrh int iCont, iBreak, iStart; 162382c3d636Sdrh assert( p->pEList ); 162441202ccaSdrh if( eDest==SRT_Callback ){ 16256a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 162641202ccaSdrh } 16274adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 16284adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 16294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 16307b58daeaSdrh computeLimitRegisters(pParse, p); 16314adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 163238640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 1633d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 163484ac9d02Sdanielk1977 iCont, iBreak, 0); 163584ac9d02Sdanielk1977 if( rc ){ 163684ac9d02Sdanielk1977 rc = 1; 163784ac9d02Sdanielk1977 goto multi_select_end; 163884ac9d02Sdanielk1977 } 16394adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 16404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 16414adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 16424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 164382c3d636Sdrh } 164482c3d636Sdrh break; 164582c3d636Sdrh } 164682c3d636Sdrh case TK_INTERSECT: { 164782c3d636Sdrh int tab1, tab2; 16486b56344dSdrh int iCont, iBreak, iStart; 1649a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; 1650dc1bdc4fSdanielk1977 int addr; 165182c3d636Sdrh 1652d8bc7086Sdrh /* INTERSECT is different from the others since it requires 16536206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1654d8bc7086Sdrh ** by allocating the tables we will need. 1655d8bc7086Sdrh */ 165682c3d636Sdrh tab1 = pParse->nTab++; 165782c3d636Sdrh tab2 = pParse->nTab++; 1658d8bc7086Sdrh if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ 165984ac9d02Sdanielk1977 rc = 1; 166084ac9d02Sdanielk1977 goto multi_select_end; 1661d8bc7086Sdrh } 1662dc1bdc4fSdanielk1977 1663dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab1, 0); 16648cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1665dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1666dc1bdc4fSdanielk1977 goto multi_select_end; 1667dc1bdc4fSdanielk1977 } 1668dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, tab1, 1); 16698cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 16708cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab1, 0); 167184ac9d02Sdanielk1977 assert( p->pEList ); 1672d8bc7086Sdrh 1673d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1674d8bc7086Sdrh */ 1675b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 167684ac9d02Sdanielk1977 if( rc ){ 167784ac9d02Sdanielk1977 goto multi_select_end; 167884ac9d02Sdanielk1977 } 1679d8bc7086Sdrh 1680d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1681d8bc7086Sdrh */ 1682dc1bdc4fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab2, 0); 16838cdbf836Sdrh rc = multiSelectOpenTempAddr(p, addr); 1684dc1bdc4fSdanielk1977 if( rc!=SQLITE_OK ){ 1685dc1bdc4fSdanielk1977 goto multi_select_end; 1686dc1bdc4fSdanielk1977 } 1687dc1bdc4fSdanielk1977 sqlite3VdbeAddOp(v, OP_KeyAsData, tab2, 1); 16888cdbf836Sdrh assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) ); 16898cdbf836Sdrh aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab2, 0); 169082c3d636Sdrh p->pPrior = 0; 1691a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1692a2dc3b1aSdanielk1977 p->pLimit = 0; 1693a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1694a2dc3b1aSdanielk1977 p->pOffset = 0; 1695b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 169682c3d636Sdrh p->pPrior = pPrior; 1697a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1698a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1699a2dc3b1aSdanielk1977 p->pOffset = pOffset; 170084ac9d02Sdanielk1977 if( rc ){ 170184ac9d02Sdanielk1977 goto multi_select_end; 170284ac9d02Sdanielk1977 } 1703d8bc7086Sdrh 1704d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1705d8bc7086Sdrh ** tables. 1706d8bc7086Sdrh */ 170782c3d636Sdrh assert( p->pEList ); 170841202ccaSdrh if( eDest==SRT_Callback ){ 17096a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 171041202ccaSdrh } 17114adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 17124adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 17134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 17147b58daeaSdrh computeLimitRegisters(pParse, p); 17154adee20fSdanielk1977 iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0); 17164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 171738640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 1718d8bc7086Sdrh p->pOrderBy, -1, eDest, iParm, 171984ac9d02Sdanielk1977 iCont, iBreak, 0); 172084ac9d02Sdanielk1977 if( rc ){ 172184ac9d02Sdanielk1977 rc = 1; 172284ac9d02Sdanielk1977 goto multi_select_end; 172384ac9d02Sdanielk1977 } 17244adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 17254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 17264adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 17274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 17284adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 172982c3d636Sdrh break; 173082c3d636Sdrh } 173182c3d636Sdrh } 17328cdbf836Sdrh 17338cdbf836Sdrh /* Make sure all SELECTs in the statement have the same number of elements 17348cdbf836Sdrh ** in their result sets. 17358cdbf836Sdrh */ 173682c3d636Sdrh assert( p->pEList && pPrior->pEList ); 173782c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 17384adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1739da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 174084ac9d02Sdanielk1977 rc = 1; 174184ac9d02Sdanielk1977 goto multi_select_end; 17422282792aSdrh } 174384ac9d02Sdanielk1977 17448cdbf836Sdrh /* Set the number of columns in temporary tables 17458cdbf836Sdrh */ 17468cdbf836Sdrh nCol = p->pEList->nExpr; 17478cdbf836Sdrh while( nAddr>0 ){ 17488cdbf836Sdrh nAddr--; 17498cdbf836Sdrh sqlite3VdbeChangeP2(v, aAddr[nAddr], nCol); 17508cdbf836Sdrh } 17518cdbf836Sdrh 1752fbc4ee7bSdrh /* Compute collating sequences used by either the ORDER BY clause or 1753fbc4ee7bSdrh ** by any temporary tables needed to implement the compound select. 1754fbc4ee7bSdrh ** Attach the KeyInfo structure to all temporary tables. Invoke the 1755fbc4ee7bSdrh ** ORDER BY processing if there is an ORDER BY clause. 17568cdbf836Sdrh ** 17578cdbf836Sdrh ** This section is run by the right-most SELECT statement only. 17588cdbf836Sdrh ** SELECT statements to the left always skip this part. The right-most 17598cdbf836Sdrh ** SELECT might also skip this part if it has no ORDER BY clause and 17608cdbf836Sdrh ** no temp tables are required. 1761fbc4ee7bSdrh */ 1762dc1bdc4fSdanielk1977 if( p->pOrderBy || (pOpenTemp && pOpenTemp->nId>0) ){ 1763fbc4ee7bSdrh int i; /* Loop counter */ 1764fbc4ee7bSdrh KeyInfo *pKeyInfo; /* Collating sequence for the result set */ 1765fbc4ee7bSdrh 17668cdbf836Sdrh assert( p->ppOpenTemp == &pOpenTemp ); 1767fbc4ee7bSdrh pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*sizeof(CollSeq*)); 1768dc1bdc4fSdanielk1977 if( !pKeyInfo ){ 1769dc1bdc4fSdanielk1977 rc = SQLITE_NOMEM; 1770dc1bdc4fSdanielk1977 goto multi_select_end; 1771dc1bdc4fSdanielk1977 } 1772dc1bdc4fSdanielk1977 1773dc1bdc4fSdanielk1977 pKeyInfo->enc = pParse->db->enc; 1774dc1bdc4fSdanielk1977 pKeyInfo->nField = nCol; 1775dc1bdc4fSdanielk1977 1776dc1bdc4fSdanielk1977 for(i=0; i<nCol; i++){ 1777dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = multiSelectCollSeq(pParse, p, i); 1778dc1bdc4fSdanielk1977 if( !pKeyInfo->aColl[i] ){ 1779dc1bdc4fSdanielk1977 pKeyInfo->aColl[i] = pParse->db->pDfltColl; 1780dc1bdc4fSdanielk1977 } 1781dc1bdc4fSdanielk1977 } 1782dc1bdc4fSdanielk1977 1783dc1bdc4fSdanielk1977 for(i=0; pOpenTemp && i<pOpenTemp->nId; i++){ 1784dc1bdc4fSdanielk1977 int p3type = (i==0?P3_KEYINFO_HANDOFF:P3_KEYINFO); 1785dc1bdc4fSdanielk1977 int addr = pOpenTemp->a[i].idx; 1786dc1bdc4fSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKeyInfo, p3type); 1787dc1bdc4fSdanielk1977 } 1788dc1bdc4fSdanielk1977 1789dc1bdc4fSdanielk1977 if( p->pOrderBy ){ 1790fbc4ee7bSdrh struct ExprList_item *pOrderByTerm = p->pOrderBy->a; 1791fbc4ee7bSdrh for(i=0; i<p->pOrderBy->nExpr; i++, pOrderByTerm++){ 1792fbc4ee7bSdrh Expr *pExpr = pOrderByTerm->pExpr; 1793fbc4ee7bSdrh char *zName = pOrderByTerm->zName; 1794dc1bdc4fSdanielk1977 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); 1795b3bce662Sdanielk1977 /* assert( !pExpr->pColl ); */ 1796dc1bdc4fSdanielk1977 if( zName ){ 1797dc1bdc4fSdanielk1977 pExpr->pColl = sqlite3LocateCollSeq(pParse, zName, -1); 179884ac9d02Sdanielk1977 }else{ 1799dc1bdc4fSdanielk1977 pExpr->pColl = pKeyInfo->aColl[pExpr->iColumn]; 180084ac9d02Sdanielk1977 } 180184ac9d02Sdanielk1977 } 1802dc1bdc4fSdanielk1977 generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1803dc1bdc4fSdanielk1977 } 1804dc1bdc4fSdanielk1977 1805dc1bdc4fSdanielk1977 if( !pOpenTemp ){ 1806dc1bdc4fSdanielk1977 /* This happens for UNION ALL ... ORDER BY */ 1807dc1bdc4fSdanielk1977 sqliteFree(pKeyInfo); 1808dc1bdc4fSdanielk1977 } 1809dc1bdc4fSdanielk1977 } 1810dc1bdc4fSdanielk1977 1811dc1bdc4fSdanielk1977 multi_select_end: 1812dc1bdc4fSdanielk1977 if( pOpenTemp ){ 1813dc1bdc4fSdanielk1977 sqlite3IdListDelete(pOpenTemp); 1814dc1bdc4fSdanielk1977 } 1815dc1bdc4fSdanielk1977 p->ppOpenTemp = 0; 181684ac9d02Sdanielk1977 return rc; 18172282792aSdrh } 1818b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 18192282792aSdrh 1820b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 18212282792aSdrh /* 1822832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 18236a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 182484e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 18256a3ea0e6Sdrh ** unchanged.) 1826832508b7Sdrh ** 1827832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1828832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1829832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1830832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1831832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1832832508b7Sdrh ** of the subquery rather the result set of the subquery. 1833832508b7Sdrh */ 18346a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 1835b3bce662Sdanielk1977 static void substSelect(Select *, int, ExprList *); /* Forward Decl */ 18366a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1837832508b7Sdrh if( pExpr==0 ) return; 183850350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 183950350a15Sdrh if( pExpr->iColumn<0 ){ 184050350a15Sdrh pExpr->op = TK_NULL; 184150350a15Sdrh }else{ 1842832508b7Sdrh Expr *pNew; 184384e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 1844832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 1845832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 1846832508b7Sdrh assert( pNew!=0 ); 1847832508b7Sdrh pExpr->op = pNew->op; 1848d94a6698Sdrh assert( pExpr->pLeft==0 ); 18494adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 1850d94a6698Sdrh assert( pExpr->pRight==0 ); 18514adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 1852d94a6698Sdrh assert( pExpr->pList==0 ); 18534adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 1854832508b7Sdrh pExpr->iTable = pNew->iTable; 1855832508b7Sdrh pExpr->iColumn = pNew->iColumn; 1856832508b7Sdrh pExpr->iAgg = pNew->iAgg; 18574adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 18584adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 1859a1cb183dSdanielk1977 pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); 1860a1cb183dSdanielk1977 pExpr->flags = pNew->flags; 186150350a15Sdrh } 1862832508b7Sdrh }else{ 18636a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 18646a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 1865b3bce662Sdanielk1977 substSelect(pExpr->pSelect, iTable, pEList); 18666a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 1867832508b7Sdrh } 1868832508b7Sdrh } 1869b3bce662Sdanielk1977 static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ 1870832508b7Sdrh int i; 1871832508b7Sdrh if( pList==0 ) return; 1872832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 18736a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 1874832508b7Sdrh } 1875832508b7Sdrh } 1876b3bce662Sdanielk1977 static void substSelect(Select *p, int iTable, ExprList *pEList){ 1877b3bce662Sdanielk1977 if( !p ) return; 1878b3bce662Sdanielk1977 substExprList(p->pEList, iTable, pEList); 1879b3bce662Sdanielk1977 substExprList(p->pGroupBy, iTable, pEList); 1880b3bce662Sdanielk1977 substExprList(p->pOrderBy, iTable, pEList); 1881b3bce662Sdanielk1977 substExpr(p->pHaving, iTable, pEList); 1882b3bce662Sdanielk1977 substExpr(p->pWhere, iTable, pEList); 1883b3bce662Sdanielk1977 } 1884b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_VIEW) */ 1885832508b7Sdrh 1886b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 1887832508b7Sdrh /* 18881350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 18891350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 18901350b030Sdrh ** occurs. 18911350b030Sdrh ** 18921350b030Sdrh ** To understand the concept of flattening, consider the following 18931350b030Sdrh ** query: 18941350b030Sdrh ** 18951350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 18961350b030Sdrh ** 18971350b030Sdrh ** The default way of implementing this query is to execute the 18981350b030Sdrh ** subquery first and store the results in a temporary table, then 18991350b030Sdrh ** run the outer query on that temporary table. This requires two 19001350b030Sdrh ** passes over the data. Furthermore, because the temporary table 19011350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 1902832508b7Sdrh ** optimized. 19031350b030Sdrh ** 1904832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 19051350b030Sdrh ** a single flat select, like this: 19061350b030Sdrh ** 19071350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 19081350b030Sdrh ** 19091350b030Sdrh ** The code generated for this simpification gives the same result 1910832508b7Sdrh ** but only has to scan the data once. And because indices might 1911832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 1912832508b7Sdrh ** avoided. 19131350b030Sdrh ** 1914832508b7Sdrh ** Flattening is only attempted if all of the following are true: 19151350b030Sdrh ** 1916832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 19171350b030Sdrh ** 1918832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 1919832508b7Sdrh ** 19208af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 19218af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 1922832508b7Sdrh ** 1923832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 1924832508b7Sdrh ** 1925832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 1926832508b7Sdrh ** aggregates. 1927832508b7Sdrh ** 1928832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 1929832508b7Sdrh ** DISTINCT. 1930832508b7Sdrh ** 193108192d5fSdrh ** (7) The subquery has a FROM clause. 193208192d5fSdrh ** 1933df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 1934df199a25Sdrh ** 1935df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 1936df199a25Sdrh ** aggregates. 1937df199a25Sdrh ** 1938df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 1939df199a25Sdrh ** use LIMIT. 1940df199a25Sdrh ** 1941174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 1942174b6195Sdrh ** 19433fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 19443fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 19453fc673e6Sdrh ** 1946832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 1947832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 1948832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 1949832508b7Sdrh ** 1950665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 1951832508b7Sdrh ** If flattening is attempted this routine returns 1. 1952832508b7Sdrh ** 1953832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 1954832508b7Sdrh ** the subquery before this routine runs. 19551350b030Sdrh */ 19568c74a8caSdrh static int flattenSubquery( 19578c74a8caSdrh Parse *pParse, /* The parsing context */ 19588c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 19598c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 19608c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 19618c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 19628c74a8caSdrh ){ 19630bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 1964ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 1965ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 19660bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 19676a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 196891bb0eedSdrh int i; /* Loop counter */ 196991bb0eedSdrh Expr *pWhere; /* The WHERE clause */ 197091bb0eedSdrh struct SrcList_item *pSubitem; /* The subquery */ 19711350b030Sdrh 1972832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 1973832508b7Sdrh */ 1974832508b7Sdrh if( p==0 ) return 0; 1975832508b7Sdrh pSrc = p->pSrc; 1976ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 197791bb0eedSdrh pSubitem = &pSrc->a[iFrom]; 197891bb0eedSdrh pSub = pSubitem->pSelect; 1979832508b7Sdrh assert( pSub!=0 ); 1980832508b7Sdrh if( isAgg && subqueryIsAgg ) return 0; 1981ad3cab52Sdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; 1982832508b7Sdrh pSubSrc = pSub->pSrc; 1983832508b7Sdrh assert( pSubSrc ); 1984a2dc3b1aSdanielk1977 if( (pSub->pLimit && p->pLimit) || pSub->pOffset || 1985a2dc3b1aSdanielk1977 (pSub->pLimit && isAgg) ) return 0; 1986c31c2eb8Sdrh if( pSubSrc->nSrc==0 ) return 0; 1987a2dc3b1aSdanielk1977 if( pSub->isDistinct && (pSrc->nSrc>1 || isAgg) ){ 1988df199a25Sdrh return 0; 1989df199a25Sdrh } 1990a2dc3b1aSdanielk1977 if( p->isDistinct && subqueryIsAgg ) return 0; 1991174b6195Sdrh if( p->pOrderBy && pSub->pOrderBy ) return 0; 1992832508b7Sdrh 19938af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 19948af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 19958af4d3acSdrh ** is not allowed: 19968af4d3acSdrh ** 19978af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 19988af4d3acSdrh ** 19998af4d3acSdrh ** If we flatten the above, we would get 20008af4d3acSdrh ** 20018af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 20028af4d3acSdrh ** 20038af4d3acSdrh ** which is not at all the same thing. 20048af4d3acSdrh */ 20058af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 20068af4d3acSdrh return 0; 20078af4d3acSdrh } 20088af4d3acSdrh 20093fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 20103fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 20113fc673e6Sdrh ** An examples of why this is not allowed: 20123fc673e6Sdrh ** 20133fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 20143fc673e6Sdrh ** 20153fc673e6Sdrh ** If we flatten the above, we would get 20163fc673e6Sdrh ** 20173fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 20183fc673e6Sdrh ** 20193fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 20203fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 20213fc673e6Sdrh */ 20223fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 20233fc673e6Sdrh && pSub->pWhere!=0 ){ 20243fc673e6Sdrh return 0; 20253fc673e6Sdrh } 20263fc673e6Sdrh 20270bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 202863eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 2029832508b7Sdrh */ 2030c31c2eb8Sdrh 2031c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 2032c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 2033c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 2034c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 2035c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 2036c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 2037c31c2eb8Sdrh ** elements we are now copying in. 2038c31c2eb8Sdrh */ 203991bb0eedSdrh iParent = pSubitem->iCursor; 2040c31c2eb8Sdrh { 2041c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 204291bb0eedSdrh int jointype = pSubitem->jointype; 204391bb0eedSdrh Table *pTab = pSubitem->pTab; 2044c31c2eb8Sdrh 204591bb0eedSdrh if( pTab && pTab->isTransient ){ 204691bb0eedSdrh sqlite3DeleteTable(0, pSubitem->pTab); 2047c31c2eb8Sdrh } 204891bb0eedSdrh sqliteFree(pSubitem->zDatabase); 204991bb0eedSdrh sqliteFree(pSubitem->zName); 205091bb0eedSdrh sqliteFree(pSubitem->zAlias); 2051c31c2eb8Sdrh if( nSubSrc>1 ){ 2052c31c2eb8Sdrh int extra = nSubSrc - 1; 2053c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 20544adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 2055c31c2eb8Sdrh } 2056c31c2eb8Sdrh p->pSrc = pSrc; 2057c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 2058c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 2059c31c2eb8Sdrh } 2060c31c2eb8Sdrh } 2061c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 2062c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 2063c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 2064c31c2eb8Sdrh } 20658af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 2066c31c2eb8Sdrh } 2067c31c2eb8Sdrh 2068c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 2069c31c2eb8Sdrh ** references to the iParent in the outer query. 2070c31c2eb8Sdrh ** 2071c31c2eb8Sdrh ** Example: 2072c31c2eb8Sdrh ** 2073c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 2074c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 2075c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 2076c31c2eb8Sdrh ** 2077c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 2078c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 2079c31c2eb8Sdrh */ 20806a3ea0e6Sdrh substExprList(p->pEList, iParent, pSub->pEList); 2081832508b7Sdrh pList = p->pEList; 2082832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 20836977fea8Sdrh Expr *pExpr; 20846977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 20856977fea8Sdrh pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); 2086832508b7Sdrh } 2087832508b7Sdrh } 20881b2e0329Sdrh if( isAgg ){ 20896a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 20906a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 20911b2e0329Sdrh } 2092174b6195Sdrh if( pSub->pOrderBy ){ 2093174b6195Sdrh assert( p->pOrderBy==0 ); 2094174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 2095174b6195Sdrh pSub->pOrderBy = 0; 2096174b6195Sdrh }else if( p->pOrderBy ){ 20976a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 2098174b6195Sdrh } 2099832508b7Sdrh if( pSub->pWhere ){ 21004adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 2101832508b7Sdrh }else{ 2102832508b7Sdrh pWhere = 0; 2103832508b7Sdrh } 2104832508b7Sdrh if( subqueryIsAgg ){ 2105832508b7Sdrh assert( p->pHaving==0 ); 21061b2e0329Sdrh p->pHaving = p->pWhere; 21071b2e0329Sdrh p->pWhere = pWhere; 21086a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 210991bb0eedSdrh p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); 21101b2e0329Sdrh assert( p->pGroupBy==0 ); 21114adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 2112832508b7Sdrh }else{ 21136a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 211491bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); 2115832508b7Sdrh } 2116c31c2eb8Sdrh 2117c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 2118c31c2eb8Sdrh ** outer query is distinct. 2119c31c2eb8Sdrh */ 2120832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 21218c74a8caSdrh 2122a58fdfb1Sdanielk1977 /* 2123a58fdfb1Sdanielk1977 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; 2124a58fdfb1Sdanielk1977 */ 2125a2dc3b1aSdanielk1977 if( pSub->pLimit ){ 2126a2dc3b1aSdanielk1977 p->pLimit = pSub->pLimit; 2127a2dc3b1aSdanielk1977 pSub->pLimit = 0; 2128df199a25Sdrh } 21298c74a8caSdrh 2130c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 2131c31c2eb8Sdrh ** success. 2132c31c2eb8Sdrh */ 21334adee20fSdanielk1977 sqlite3SelectDelete(pSub); 2134832508b7Sdrh return 1; 21351350b030Sdrh } 2136b7f9164eSdrh #endif /* SQLITE_OMIT_VIEW */ 21371350b030Sdrh 21381350b030Sdrh /* 21399562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 21409562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 21419562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 2142e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 21439562b551Sdrh ** simple min() or max() query, then return 0; 21449562b551Sdrh ** 21459562b551Sdrh ** A simply min() or max() query looks like this: 21469562b551Sdrh ** 21479562b551Sdrh ** SELECT min(a) FROM table; 21489562b551Sdrh ** SELECT max(a) FROM table; 21499562b551Sdrh ** 21509562b551Sdrh ** The query may have only a single table in its FROM argument. There 21519562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 21529562b551Sdrh ** be the min() or max() of a single column of the table. The column 21539562b551Sdrh ** in the min() or max() function must be indexed. 21549562b551Sdrh ** 21554adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 21569562b551Sdrh ** See the header comment on that routine for additional information. 21579562b551Sdrh */ 21589562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 21599562b551Sdrh Expr *pExpr; 21609562b551Sdrh int iCol; 21619562b551Sdrh Table *pTab; 21629562b551Sdrh Index *pIdx; 21639562b551Sdrh int base; 21649562b551Sdrh Vdbe *v; 21659562b551Sdrh int seekOp; 21669562b551Sdrh int cont; 21676e17529eSdrh ExprList *pEList, *pList, eList; 21689562b551Sdrh struct ExprList_item eListItem; 21696e17529eSdrh SrcList *pSrc; 21706e17529eSdrh 21719562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 21729562b551Sdrh ** zero if it is not. 21739562b551Sdrh */ 21749562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 21756e17529eSdrh pSrc = p->pSrc; 21766e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 21776e17529eSdrh pEList = p->pEList; 21786e17529eSdrh if( pEList->nExpr!=1 ) return 0; 21796e17529eSdrh pExpr = pEList->a[0].pExpr; 21809562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 21816e17529eSdrh pList = pExpr->pList; 21826e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 21836977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 21844adee20fSdanielk1977 if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){ 21850bce8354Sdrh seekOp = OP_Rewind; 21864adee20fSdanielk1977 }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){ 21870bce8354Sdrh seekOp = OP_Last; 21880bce8354Sdrh }else{ 21890bce8354Sdrh return 0; 21900bce8354Sdrh } 21916e17529eSdrh pExpr = pList->a[0].pExpr; 21929562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 21939562b551Sdrh iCol = pExpr->iColumn; 21946e17529eSdrh pTab = pSrc->a[0].pTab; 21959562b551Sdrh 21969562b551Sdrh /* If we get to here, it means the query is of the correct form. 219717f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 219817f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 219917f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 220017f71934Sdrh ** usable index is found, return 0. 22019562b551Sdrh */ 22029562b551Sdrh if( iCol<0 ){ 22039562b551Sdrh pIdx = 0; 22049562b551Sdrh }else{ 2205dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 22069562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 22079562b551Sdrh assert( pIdx->nColumn>=1 ); 2208dc1bdc4fSdanielk1977 if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break; 22099562b551Sdrh } 22109562b551Sdrh if( pIdx==0 ) return 0; 22119562b551Sdrh } 22129562b551Sdrh 2213e5f50722Sdrh /* Identify column types if we will be using the callback. This 22149562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 2215e5f50722Sdrh ** The column names have already been generated in the calling function. 22169562b551Sdrh */ 22174adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 22189562b551Sdrh if( v==0 ) return 0; 22199562b551Sdrh 22200c37e630Sdrh /* If the output is destined for a temporary table, open that table. 22210c37e630Sdrh */ 22220c37e630Sdrh if( eDest==SRT_TempTable ){ 22234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2224b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1); 22250c37e630Sdrh } 22260c37e630Sdrh 222717f71934Sdrh /* Generating code to find the min or the max. Basically all we have 222817f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 222917f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 223017f71934Sdrh ** or last entry in the main table. 22319562b551Sdrh */ 22324adee20fSdanielk1977 sqlite3CodeVerifySchema(pParse, pTab->iDb); 22336e17529eSdrh base = pSrc->a[0].iCursor; 22347b58daeaSdrh computeLimitRegisters(pParse, p); 22356e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 2236ad6d9460Sdrh sqlite3OpenTableForReading(v, base, pTab); 22376e17529eSdrh } 22384adee20fSdanielk1977 cont = sqlite3VdbeMakeLabel(v); 22399562b551Sdrh if( pIdx==0 ){ 22404adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 22419562b551Sdrh }else{ 22423719d7f9Sdanielk1977 /* Even though the cursor used to open the index here is closed 22433719d7f9Sdanielk1977 ** as soon as a single value has been read from it, allocate it 22443719d7f9Sdanielk1977 ** using (pParse->nTab++) to prevent the cursor id from being 22453719d7f9Sdanielk1977 ** reused. This is important for statements of the form 22463719d7f9Sdanielk1977 ** "INSERT INTO x SELECT max() FROM x". 22473719d7f9Sdanielk1977 */ 22483719d7f9Sdanielk1977 int iIdx; 22493719d7f9Sdanielk1977 iIdx = pParse->nTab++; 22504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 22513719d7f9Sdanielk1977 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 2252d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 22539eb516c0Sdrh if( seekOp==OP_Rewind ){ 22541af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_String, 0, 0); 22551af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); 22561af3fdb4Sdrh seekOp = OP_MoveGt; 22579eb516c0Sdrh } 22583719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, seekOp, iIdx, 0); 22593719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_IdxRecno, iIdx, 0); 22603719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); 22617cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 22629562b551Sdrh } 22635cf8e8c7Sdrh eList.nExpr = 1; 22645cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 22655cf8e8c7Sdrh eList.a = &eListItem; 22665cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 226784ac9d02Sdanielk1977 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0); 22684adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, cont); 22694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 22706e17529eSdrh 22719562b551Sdrh return 1; 22729562b551Sdrh } 22739562b551Sdrh 22749562b551Sdrh /* 2275290c1948Sdrh ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return 2276290c1948Sdrh ** the number of errors seen. 2277290c1948Sdrh ** 2278290c1948Sdrh ** An ORDER BY or GROUP BY is a list of expressions. If any expression 2279290c1948Sdrh ** is an integer constant, then that expression is replaced by the 2280290c1948Sdrh ** corresponding entry in the result set. 2281290c1948Sdrh */ 2282290c1948Sdrh static int processOrderGroupBy( 2283b3bce662Sdanielk1977 NameContext *pNC, /* Name context of the SELECT statement. */ 2284290c1948Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 2285290c1948Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 2286290c1948Sdrh ){ 2287290c1948Sdrh int i; 2288b3bce662Sdanielk1977 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */ 2289b3bce662Sdanielk1977 Parse *pParse = pNC->pParse; /* The result set of the SELECT */ 2290b3bce662Sdanielk1977 assert( pEList ); 2291b3bce662Sdanielk1977 2292290c1948Sdrh if( pOrderBy==0 ) return 0; 2293290c1948Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2294290c1948Sdrh int iCol; 2295290c1948Sdrh Expr *pE = pOrderBy->a[i].pExpr; 2296b3bce662Sdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 2297b3bce662Sdanielk1977 if( iCol>0 && iCol<=pEList->nExpr ){ 2298290c1948Sdrh sqlite3ExprDelete(pE); 2299290c1948Sdrh pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 2300b3bce662Sdanielk1977 }else{ 2301290c1948Sdrh sqlite3ErrorMsg(pParse, 2302290c1948Sdrh "%s BY column number %d out of range - should be " 2303290c1948Sdrh "between 1 and %d", zType, iCol, pEList->nExpr); 2304290c1948Sdrh return 1; 2305290c1948Sdrh } 2306290c1948Sdrh } 2307b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(pNC, pE) ){ 2308b3bce662Sdanielk1977 return 1; 2309b3bce662Sdanielk1977 } 2310b3bce662Sdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 2311b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, 2312b3bce662Sdanielk1977 "%s BY terms must not be non-integer constants", zType); 2313b3bce662Sdanielk1977 return 1; 2314b3bce662Sdanielk1977 } 2315290c1948Sdrh } 2316290c1948Sdrh return 0; 2317290c1948Sdrh } 2318290c1948Sdrh 2319290c1948Sdrh /* 2320b3bce662Sdanielk1977 ** This routine resolves any names used in the result set of the 2321b3bce662Sdanielk1977 ** supplied SELECT statement. If the SELECT statement being resolved 2322b3bce662Sdanielk1977 ** is a sub-select, then pOuterNC is a pointer to the NameContext 2323b3bce662Sdanielk1977 ** of the parent SELECT. 2324b3bce662Sdanielk1977 */ 2325b3bce662Sdanielk1977 int sqlite3SelectResolve( 2326b3bce662Sdanielk1977 Parse *pParse, /* The parser context */ 2327b3bce662Sdanielk1977 Select *p, /* The SELECT statement being coded. */ 2328b3bce662Sdanielk1977 NameContext *pOuterNC /* The outer name context. May be NULL. */ 2329b3bce662Sdanielk1977 ){ 2330b3bce662Sdanielk1977 ExprList *pEList; /* Result set. */ 2331b3bce662Sdanielk1977 int i; /* For-loop variable used in multiple places */ 2332b3bce662Sdanielk1977 NameContext sNC; /* Local name-context */ 2333b3bce662Sdanielk1977 2334b3bce662Sdanielk1977 /* If this routine has run before, return immediately. */ 2335b3bce662Sdanielk1977 if( p->isResolved ){ 2336b3bce662Sdanielk1977 assert( !pOuterNC ); 2337b3bce662Sdanielk1977 return SQLITE_OK; 2338b3bce662Sdanielk1977 } 2339b3bce662Sdanielk1977 p->isResolved = 1; 2340b3bce662Sdanielk1977 2341b3bce662Sdanielk1977 /* If there have already been errors, do nothing. */ 2342b3bce662Sdanielk1977 if( pParse->nErr>0 ){ 2343b3bce662Sdanielk1977 return SQLITE_ERROR; 2344b3bce662Sdanielk1977 } 2345b3bce662Sdanielk1977 2346b3bce662Sdanielk1977 /* Prepare the select statement. This call will allocate all cursors 2347b3bce662Sdanielk1977 ** required to handle the tables and subqueries in the FROM clause. 2348b3bce662Sdanielk1977 */ 2349b3bce662Sdanielk1977 if( prepSelectStmt(pParse, p) ){ 2350b3bce662Sdanielk1977 return SQLITE_ERROR; 2351b3bce662Sdanielk1977 } 2352b3bce662Sdanielk1977 2353a2dc3b1aSdanielk1977 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 2354a2dc3b1aSdanielk1977 ** are not allowed to refer to any names, so pass an empty NameContext. 2355a2dc3b1aSdanielk1977 */ 2356b3bce662Sdanielk1977 sNC.pParse = pParse; 2357b3bce662Sdanielk1977 sNC.hasAgg = 0; 2358b3bce662Sdanielk1977 sNC.nErr = 0; 2359b3bce662Sdanielk1977 sNC.nRef = 0; 2360b3bce662Sdanielk1977 sNC.pEList = 0; 2361a2dc3b1aSdanielk1977 sNC.allowAgg = 0; 2362a2dc3b1aSdanielk1977 sNC.pSrcList = 0; 2363a2dc3b1aSdanielk1977 sNC.pNext = 0; 2364a2dc3b1aSdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pLimit) || 2365a2dc3b1aSdanielk1977 sqlite3ExprResolveNames(&sNC, p->pOffset) ){ 2366a2dc3b1aSdanielk1977 return SQLITE_ERROR; 2367a2dc3b1aSdanielk1977 } 2368a2dc3b1aSdanielk1977 2369a2dc3b1aSdanielk1977 /* Set up the local name-context to pass to ExprResolveNames() to 2370a2dc3b1aSdanielk1977 ** resolve the expression-list. 2371a2dc3b1aSdanielk1977 */ 2372a2dc3b1aSdanielk1977 sNC.allowAgg = 1; 2373a2dc3b1aSdanielk1977 sNC.pSrcList = p->pSrc; 2374a2dc3b1aSdanielk1977 sNC.pNext = pOuterNC; 2375b3bce662Sdanielk1977 2376b3bce662Sdanielk1977 /* NameContext.nDepth stores the depth of recursion for this query. For 2377b3bce662Sdanielk1977 ** an outer query (e.g. SELECT * FROM sqlite_master) this is 1. For 2378b3bce662Sdanielk1977 ** a subquery it is 2. For a subquery of a subquery, 3. And so on. 2379b3bce662Sdanielk1977 ** Parse.nMaxDepth is the maximum depth for any subquery resolved so 2380b3bce662Sdanielk1977 ** far. This is used to determine the number of aggregate contexts 2381b3bce662Sdanielk1977 ** required at runtime. 2382b3bce662Sdanielk1977 */ 2383b3bce662Sdanielk1977 sNC.nDepth = (pOuterNC?pOuterNC->nDepth+1:1); 2384b3bce662Sdanielk1977 if( sNC.nDepth>pParse->nMaxDepth ){ 2385b3bce662Sdanielk1977 pParse->nMaxDepth = sNC.nDepth; 2386b3bce662Sdanielk1977 } 2387b3bce662Sdanielk1977 2388b3bce662Sdanielk1977 /* Resolve names in the result set. */ 2389b3bce662Sdanielk1977 pEList = p->pEList; 2390b3bce662Sdanielk1977 if( !pEList ) return SQLITE_ERROR; 2391b3bce662Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 2392b3bce662Sdanielk1977 Expr *pX = pEList->a[i].pExpr; 2393b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pX) ){ 2394b3bce662Sdanielk1977 return SQLITE_ERROR; 2395b3bce662Sdanielk1977 } 2396b3bce662Sdanielk1977 } 2397b3bce662Sdanielk1977 2398b3bce662Sdanielk1977 /* If there are no aggregate functions in the result-set, and no GROUP BY 2399b3bce662Sdanielk1977 ** expression, do not allow aggregates in any of the other expressions. 2400b3bce662Sdanielk1977 */ 2401b3bce662Sdanielk1977 assert( !p->isAgg ); 2402b3bce662Sdanielk1977 if( p->pGroupBy || sNC.hasAgg ){ 2403b3bce662Sdanielk1977 p->isAgg = 1; 2404b3bce662Sdanielk1977 }else{ 2405b3bce662Sdanielk1977 sNC.allowAgg = 0; 2406b3bce662Sdanielk1977 } 2407b3bce662Sdanielk1977 2408b3bce662Sdanielk1977 /* If a HAVING clause is present, then there must be a GROUP BY clause. 2409b3bce662Sdanielk1977 */ 2410b3bce662Sdanielk1977 if( p->pHaving && !p->pGroupBy ){ 2411b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2412b3bce662Sdanielk1977 return SQLITE_ERROR; 2413b3bce662Sdanielk1977 } 2414b3bce662Sdanielk1977 2415b3bce662Sdanielk1977 /* Add the expression list to the name-context before parsing the 2416b3bce662Sdanielk1977 ** other expressions in the SELECT statement. This is so that 2417b3bce662Sdanielk1977 ** expressions in the WHERE clause (etc.) can refer to expressions by 2418b3bce662Sdanielk1977 ** aliases in the result set. 2419b3bce662Sdanielk1977 ** 2420b3bce662Sdanielk1977 ** Minor point: If this is the case, then the expression will be 2421b3bce662Sdanielk1977 ** re-evaluated for each reference to it. 2422b3bce662Sdanielk1977 */ 2423b3bce662Sdanielk1977 sNC.pEList = p->pEList; 2424b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pWhere) || 2425b3bce662Sdanielk1977 sqlite3ExprResolveNames(&sNC, p->pHaving) || 2426b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") || 2427b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pGroupBy, "GROUP") 2428b3bce662Sdanielk1977 ){ 2429b3bce662Sdanielk1977 return SQLITE_ERROR; 2430b3bce662Sdanielk1977 } 2431b3bce662Sdanielk1977 2432b3bce662Sdanielk1977 return SQLITE_OK; 2433b3bce662Sdanielk1977 } 2434b3bce662Sdanielk1977 2435b3bce662Sdanielk1977 /* 2436b3bce662Sdanielk1977 ** An instance of the following struct is used by sqlite3Select() 2437b3bce662Sdanielk1977 ** to save aggregate related information from the Parse object 2438b3bce662Sdanielk1977 ** at the start of each call and to restore it at the end. See 2439b3bce662Sdanielk1977 ** saveAggregateInfo() and restoreAggregateInfo(). 2440b3bce662Sdanielk1977 */ 2441b3bce662Sdanielk1977 struct AggregateInfo { 2442b3bce662Sdanielk1977 int nAgg; 2443b3bce662Sdanielk1977 AggExpr *aAgg; 2444b3bce662Sdanielk1977 }; 2445b3bce662Sdanielk1977 typedef struct AggregateInfo AggregateInfo; 2446b3bce662Sdanielk1977 2447b3bce662Sdanielk1977 /* 2448b3bce662Sdanielk1977 ** Copy aggregate related information from the Parse structure 2449b3bce662Sdanielk1977 ** into the AggregateInfo structure. Zero the aggregate related 2450b3bce662Sdanielk1977 ** values in the Parse struct. 2451b3bce662Sdanielk1977 */ 2452b3bce662Sdanielk1977 static void saveAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2453b3bce662Sdanielk1977 pInfo->aAgg = pParse->aAgg; 2454b3bce662Sdanielk1977 pInfo->nAgg = pParse->nAgg; 2455b3bce662Sdanielk1977 pParse->aAgg = 0; 2456b3bce662Sdanielk1977 pParse->nAgg = 0; 2457b3bce662Sdanielk1977 } 2458b3bce662Sdanielk1977 2459b3bce662Sdanielk1977 /* 2460b3bce662Sdanielk1977 ** Copy aggregate related information from the AggregateInfo struct 2461b3bce662Sdanielk1977 ** back into the Parse structure. The aggregate related information 2462b3bce662Sdanielk1977 ** currently stored in the Parse structure is deleted. 2463b3bce662Sdanielk1977 */ 2464b3bce662Sdanielk1977 static void restoreAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2465b3bce662Sdanielk1977 sqliteFree(pParse->aAgg); 2466b3bce662Sdanielk1977 pParse->aAgg = pInfo->aAgg; 2467b3bce662Sdanielk1977 pParse->nAgg = pInfo->nAgg; 2468b3bce662Sdanielk1977 } 2469b3bce662Sdanielk1977 2470b3bce662Sdanielk1977 /* 24719bb61fe7Sdrh ** Generate code for the given SELECT statement. 24729bb61fe7Sdrh ** 2473fef5208cSdrh ** The results are distributed in various ways depending on the 2474fef5208cSdrh ** value of eDest and iParm. 2475fef5208cSdrh ** 2476fef5208cSdrh ** eDest Value Result 2477fef5208cSdrh ** ------------ ------------------------------------------- 2478fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2479fef5208cSdrh ** 2480fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2481fef5208cSdrh ** 2482e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2483fef5208cSdrh ** 248482c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 248582c3d636Sdrh ** 24864b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2487c4a3c779Sdrh ** 2488c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 24899bb61fe7Sdrh ** 2490e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2491e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2492e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2493e78e8284Sdrh ** 24949bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 24959bb61fe7Sdrh ** encountered, then an appropriate error message is left in 24969bb61fe7Sdrh ** pParse->zErrMsg. 24979bb61fe7Sdrh ** 24989bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 24999bb61fe7Sdrh ** calling function needs to do that. 25001b2e0329Sdrh ** 25011b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 25021b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 25031b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 25041b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 25051b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 25061b2e0329Sdrh ** can be changed. 2507e78e8284Sdrh ** 2508e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2509e78e8284Sdrh ** 2510e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2511e78e8284Sdrh ** \ \_______ subquery _______/ / 2512e78e8284Sdrh ** \ / 2513e78e8284Sdrh ** \____________________ outer query ___________________/ 2514e78e8284Sdrh ** 2515e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2516e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2517e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2518e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2519e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2520e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 25219bb61fe7Sdrh */ 25224adee20fSdanielk1977 int sqlite3Select( 2523cce7d176Sdrh Parse *pParse, /* The parser context */ 25249bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2525e78e8284Sdrh int eDest, /* How to dispose of the results */ 2526e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2527832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2528832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 252984ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 2530b3bce662Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2531cce7d176Sdrh ){ 2532d8bc7086Sdrh int i; 2533cce7d176Sdrh WhereInfo *pWInfo; 2534cce7d176Sdrh Vdbe *v; 2535b3bce662Sdanielk1977 int isAgg; /* True for select lists like "count(*)" */ 2536a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2537ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 25389bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 25399bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 25402282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 25412282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 254219a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 254319a775c2Sdrh int distinct; /* Table to use for the distinct set */ 25441d83f052Sdrh int rc = 1; /* Value to return from this function */ 2545b3bce662Sdanielk1977 AggregateInfo sAggInfo; 25469bb61fe7Sdrh 25476f8a503dSdanielk1977 if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1; 25484adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 2549daffd0e5Sdrh 2550b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 255182c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 255282c3d636Sdrh */ 255382c3d636Sdrh if( p->pPrior ){ 255484ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 255582c3d636Sdrh } 2556b7f9164eSdrh #endif 255782c3d636Sdrh 2558b3bce662Sdanielk1977 saveAggregateInfo(pParse, &sAggInfo); 2559b3bce662Sdanielk1977 pOrderBy = p->pOrderBy; 2560b3bce662Sdanielk1977 if( eDest==SRT_Union || eDest==SRT_Except || eDest==SRT_Discard ){ 2561b3bce662Sdanielk1977 p->pOrderBy = 0; 2562b3bce662Sdanielk1977 } 2563b3bce662Sdanielk1977 if( sqlite3SelectResolve(pParse, p, 0) ){ 2564b3bce662Sdanielk1977 goto select_end; 2565b3bce662Sdanielk1977 } 2566b3bce662Sdanielk1977 p->pOrderBy = pOrderBy; 2567b3bce662Sdanielk1977 256882c3d636Sdrh /* Make local copies of the parameters for this query. 256982c3d636Sdrh */ 25709bb61fe7Sdrh pTabList = p->pSrc; 25719bb61fe7Sdrh pWhere = p->pWhere; 25722282792aSdrh pGroupBy = p->pGroupBy; 25732282792aSdrh pHaving = p->pHaving; 2574b3bce662Sdanielk1977 isAgg = p->isAgg; 257519a775c2Sdrh isDistinct = p->isDistinct; 2576b3bce662Sdanielk1977 pEList = p->pEList; 2577b3bce662Sdanielk1977 if( pEList==0 ) goto select_end; 25789bb61fe7Sdrh 25799bb61fe7Sdrh /* 25809bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 25819bb61fe7Sdrh ** errors before this routine starts. 25829bb61fe7Sdrh */ 25831d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2584cce7d176Sdrh 25852282792aSdrh /* If writing to memory or generating a set 25862282792aSdrh ** only a single column may be output. 258719a775c2Sdrh */ 258851522cd3Sdrh assert( eDest!=SRT_Exists || pEList->nExpr==1 ); 258993758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2590fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 25914adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2592da93d238Sdrh "a SELECT that is part of an expression"); 25931d83f052Sdrh goto select_end; 259419a775c2Sdrh } 259593758c8dSdanielk1977 #endif 259619a775c2Sdrh 2597c926afbcSdrh /* ORDER BY is ignored for some destinations. 25982282792aSdrh */ 2599c926afbcSdrh switch( eDest ){ 2600c926afbcSdrh case SRT_Union: 2601c926afbcSdrh case SRT_Except: 2602c926afbcSdrh case SRT_Discard: 2603acd4c695Sdrh pOrderBy = 0; 2604c926afbcSdrh break; 2605c926afbcSdrh default: 2606c926afbcSdrh break; 26072282792aSdrh } 26082282792aSdrh 2609d820cb1bSdrh /* Begin generating code. 2610d820cb1bSdrh */ 26114adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2612d820cb1bSdrh if( v==0 ) goto select_end; 2613d820cb1bSdrh 2614e78e8284Sdrh /* Identify column names if we will be using them in a callback. This 2615e78e8284Sdrh ** step is skipped if the output is going to some other destination. 26160bb28106Sdrh */ 26170bb28106Sdrh if( eDest==SRT_Callback ){ 26186a3ea0e6Sdrh generateColumnNames(pParse, pTabList, pEList); 26190bb28106Sdrh } 26200bb28106Sdrh 2621d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2622d820cb1bSdrh */ 262351522cd3Sdrh #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) 2624ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 2625742f947bSdanielk1977 const char *zSavedAuthContext = 0; 2626c31c2eb8Sdrh int needRestoreContext; 2627c31c2eb8Sdrh 2628a76b5dfcSdrh if( pTabList->a[i].pSelect==0 ) continue; 26295cf590c1Sdrh if( pTabList->a[i].zName!=0 ){ 26305cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 26315cf590c1Sdrh pParse->zAuthContext = pTabList->a[i].zName; 2632c31c2eb8Sdrh needRestoreContext = 1; 2633c31c2eb8Sdrh }else{ 2634c31c2eb8Sdrh needRestoreContext = 0; 26355cf590c1Sdrh } 26364adee20fSdanielk1977 sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable, 2637b3bce662Sdanielk1977 pTabList->a[i].iCursor, p, i, &isAgg, 0); 2638c31c2eb8Sdrh if( needRestoreContext ){ 26395cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 26405cf590c1Sdrh } 2641832508b7Sdrh pTabList = p->pSrc; 2642832508b7Sdrh pWhere = p->pWhere; 2643c31c2eb8Sdrh if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ 2644832508b7Sdrh pOrderBy = p->pOrderBy; 2645acd4c695Sdrh } 2646832508b7Sdrh pGroupBy = p->pGroupBy; 2647832508b7Sdrh pHaving = p->pHaving; 2648832508b7Sdrh isDistinct = p->isDistinct; 26491b2e0329Sdrh } 265051522cd3Sdrh #endif 26511b2e0329Sdrh 26526e17529eSdrh /* Check for the special case of a min() or max() function by itself 26536e17529eSdrh ** in the result set. 26546e17529eSdrh */ 26556e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 26566e17529eSdrh rc = 0; 26576e17529eSdrh goto select_end; 26586e17529eSdrh } 26596e17529eSdrh 26601b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 26611b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 26621b2e0329Sdrh */ 2663b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 26641b2e0329Sdrh if( pParent && pParentAgg && 26658c74a8caSdrh flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ 26661b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 2667b3bce662Sdanielk1977 goto select_end; 26681b2e0329Sdrh } 2669b7f9164eSdrh #endif 2670832508b7Sdrh 26717cedc8d4Sdanielk1977 /* If there is an ORDER BY clause, resolve any collation sequences 26727cedc8d4Sdanielk1977 ** names that have been explicitly specified. 26737cedc8d4Sdanielk1977 */ 26747cedc8d4Sdanielk1977 if( pOrderBy ){ 26757cedc8d4Sdanielk1977 for(i=0; i<pOrderBy->nExpr; i++){ 26767cedc8d4Sdanielk1977 if( pOrderBy->a[i].zName ){ 26777cedc8d4Sdanielk1977 pOrderBy->a[i].pExpr->pColl = 26787cedc8d4Sdanielk1977 sqlite3LocateCollSeq(pParse, pOrderBy->a[i].zName, -1); 26797cedc8d4Sdanielk1977 } 26807cedc8d4Sdanielk1977 } 26817cedc8d4Sdanielk1977 if( pParse->nErr ){ 26827cedc8d4Sdanielk1977 goto select_end; 26837cedc8d4Sdanielk1977 } 26847cedc8d4Sdanielk1977 } 26857cedc8d4Sdanielk1977 26867b58daeaSdrh /* Set the limiter. 26877b58daeaSdrh */ 26887b58daeaSdrh computeLimitRegisters(pParse, p); 26897b58daeaSdrh 26902d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 26912d0794e3Sdrh */ 26922d0794e3Sdrh if( eDest==SRT_TempTable ){ 26934adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); 2694b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); 26952d0794e3Sdrh } 26962d0794e3Sdrh 26972282792aSdrh /* Do an analysis of aggregate expressions. 2698efb7251dSdrh */ 2699bb999ef6Sdrh if( isAgg || pGroupBy ){ 2700a58fdfb1Sdanielk1977 NameContext sNC; 2701a58fdfb1Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 2702a58fdfb1Sdanielk1977 sNC.pParse = pParse; 2703a58fdfb1Sdanielk1977 sNC.pSrcList = pTabList; 2704a58fdfb1Sdanielk1977 27050bce8354Sdrh assert( pParse->nAgg==0 ); 2706bb999ef6Sdrh isAgg = 1; 27072282792aSdrh for(i=0; i<pEList->nExpr; i++){ 2708a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pEList->a[i].pExpr) ){ 27091d83f052Sdrh goto select_end; 27102282792aSdrh } 27112282792aSdrh } 27122282792aSdrh if( pGroupBy ){ 27132282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 2714a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pGroupBy->a[i].pExpr) ){ 27151d83f052Sdrh goto select_end; 27162282792aSdrh } 27172282792aSdrh } 27182282792aSdrh } 2719a58fdfb1Sdanielk1977 if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ 27201d83f052Sdrh goto select_end; 27212282792aSdrh } 2722191b690eSdrh if( pOrderBy ){ 2723191b690eSdrh for(i=0; i<pOrderBy->nExpr; i++){ 2724a58fdfb1Sdanielk1977 if( sqlite3ExprAnalyzeAggregates(&sNC, pOrderBy->a[i].pExpr) ){ 27251d83f052Sdrh goto select_end; 2726191b690eSdrh } 2727191b690eSdrh } 2728191b690eSdrh } 2729efb7251dSdrh } 2730efb7251dSdrh 27312282792aSdrh /* Reset the aggregator 2732cce7d176Sdrh */ 2733cce7d176Sdrh if( isAgg ){ 2734e159fdf2Sdanielk1977 int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg); 2735e5095355Sdrh for(i=0; i<pParse->nAgg; i++){ 27360bce8354Sdrh FuncDef *pFunc; 27370bce8354Sdrh if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ 2738*5c2d9155Sdanielk1977 int nExpr = 0; 2739*5c2d9155Sdanielk1977 #ifdef SQLITE_SSE 2740*5c2d9155Sdanielk1977 Expr *pAggExpr = pParse->aAgg[i].pExpr; 2741*5c2d9155Sdanielk1977 if( pAggExpr && pAggExpr->pList ){ 2742*5c2d9155Sdanielk1977 nExpr = pAggExpr->pList->nExpr; 2743*5c2d9155Sdanielk1977 } 2744*5c2d9155Sdanielk1977 #endif 2745*5c2d9155Sdanielk1977 sqlite3VdbeOp3(v, OP_AggInit, nExpr, i, (char*)pFunc, P3_FUNCDEF); 2746e5095355Sdrh } 2747e5095355Sdrh } 2748e159fdf2Sdanielk1977 if( pGroupBy ){ 2749ce2663ccSdanielk1977 int sz = sizeof(KeyInfo) + pGroupBy->nExpr*sizeof(CollSeq*); 2750ce2663ccSdanielk1977 KeyInfo *pKey = (KeyInfo *)sqliteMalloc(sz); 2751ce2663ccSdanielk1977 if( 0==pKey ){ 2752ce2663ccSdanielk1977 goto select_end; 2753ce2663ccSdanielk1977 } 2754ce2663ccSdanielk1977 pKey->enc = pParse->db->enc; 2755ce2663ccSdanielk1977 pKey->nField = pGroupBy->nExpr; 2756ce2663ccSdanielk1977 for(i=0; i<pGroupBy->nExpr; i++){ 2757ce2663ccSdanielk1977 pKey->aColl[i] = sqlite3ExprCollSeq(pParse, pGroupBy->a[i].pExpr); 2758ce2663ccSdanielk1977 if( !pKey->aColl[i] ){ 2759ce2663ccSdanielk1977 pKey->aColl[i] = pParse->db->pDfltColl; 2760ce2663ccSdanielk1977 } 2761ce2663ccSdanielk1977 } 2762ce2663ccSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF); 27631bee3d7bSdrh } 2764cce7d176Sdrh } 2765cce7d176Sdrh 276651522cd3Sdrh /* Initialize the memory cell to NULL for SRT_Mem or 0 for SRT_Exists 276719a775c2Sdrh */ 276851522cd3Sdrh if( eDest==SRT_Mem || eDest==SRT_Exists ){ 276951522cd3Sdrh sqlite3VdbeAddOp(v, eDest==SRT_Mem ? OP_String8 : OP_Integer, 0, 0); 27704adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 277119a775c2Sdrh } 277219a775c2Sdrh 2773832508b7Sdrh /* Open a temporary table to use for the distinct set. 2774cce7d176Sdrh */ 277519a775c2Sdrh if( isDistinct ){ 2776832508b7Sdrh distinct = pParse->nTab++; 2777d3d39e93Sdrh openTempIndex(pParse, p, distinct, 0); 2778832508b7Sdrh }else{ 2779832508b7Sdrh distinct = -1; 2780efb7251dSdrh } 2781832508b7Sdrh 2782832508b7Sdrh /* Begin the database scan 2783832508b7Sdrh */ 2784e6f85e71Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 2785f8db1bc0Sdrh pGroupBy ? 0 : &pOrderBy); 27861d83f052Sdrh if( pWInfo==0 ) goto select_end; 2787cce7d176Sdrh 27882282792aSdrh /* Use the standard inner loop if we are not dealing with 27892282792aSdrh ** aggregates 2790cce7d176Sdrh */ 2791da9d6c45Sdrh if( !isAgg ){ 2792df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 279384ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 27941d83f052Sdrh goto select_end; 2795cce7d176Sdrh } 2796da9d6c45Sdrh } 2797cce7d176Sdrh 2798e3184744Sdrh /* If we are dealing with aggregates, then do the special aggregate 27992282792aSdrh ** processing. 2800efb7251dSdrh */ 28012282792aSdrh else{ 2802268380caSdrh AggExpr *pAgg; 2803a58fdfb1Sdanielk1977 int lbl1 = 0; 2804a58fdfb1Sdanielk1977 pParse->fillAgg = 1; 28052282792aSdrh if( pGroupBy ){ 28062282792aSdrh for(i=0; i<pGroupBy->nExpr; i++){ 28074adee20fSdanielk1977 sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr); 2808efb7251dSdrh } 2809ededfd5eSdanielk1977 /* No affinity string is attached to the following OP_MakeRecord 2810d3d39e93Sdrh ** because we do not need to do any coercion of datatypes. */ 2811ededfd5eSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0); 28124adee20fSdanielk1977 lbl1 = sqlite3VdbeMakeLabel(v); 28134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1); 2814a58fdfb1Sdanielk1977 } 2815268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 2816268380caSdrh if( pAgg->isAgg ) continue; 28174adee20fSdanielk1977 sqlite3ExprCode(pParse, pAgg->pExpr); 28184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggSet, 0, i); 28192282792aSdrh } 2820a58fdfb1Sdanielk1977 pParse->fillAgg = 0; 2821a58fdfb1Sdanielk1977 if( lbl1<0 ){ 28224adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, lbl1); 28232282792aSdrh } 2824268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 28252282792aSdrh Expr *pE; 2826268380caSdrh int nExpr; 2827268380caSdrh FuncDef *pDef; 2828268380caSdrh if( !pAgg->isAgg ) continue; 2829268380caSdrh assert( pAgg->pFunc!=0 ); 2830268380caSdrh assert( pAgg->pFunc->xStep!=0 ); 2831268380caSdrh pDef = pAgg->pFunc; 2832268380caSdrh pE = pAgg->pExpr; 2833268380caSdrh assert( pE!=0 ); 28342282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 2835f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pE->pList); 28364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 2837dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2838dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 2839dc1bdc4fSdanielk1977 int j; 2840dc1bdc4fSdanielk1977 for(j=0; !pColl && j<nExpr; j++){ 2841dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr); 2842dc1bdc4fSdanielk1977 } 2843dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 2844dc1bdc4fSdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 2845dc1bdc4fSdanielk1977 } 28461f55c056Sdanielk1977 sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_FUNCDEF); 28472282792aSdrh } 28482282792aSdrh } 28492282792aSdrh 2850cce7d176Sdrh /* End the database scan loop. 2851cce7d176Sdrh */ 28524adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 2853cce7d176Sdrh 28542282792aSdrh /* If we are processing aggregates, we need to set up a second loop 28552282792aSdrh ** over all of the aggregate values and process them. 28562282792aSdrh */ 28572282792aSdrh if( isAgg ){ 28584adee20fSdanielk1977 int endagg = sqlite3VdbeMakeLabel(v); 28592282792aSdrh int startagg; 28604adee20fSdanielk1977 startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg); 28612282792aSdrh if( pHaving ){ 28624adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pHaving, startagg, 1); 28632282792aSdrh } 2864df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 286584ac9d02Sdanielk1977 iParm, startagg, endagg, aff) ){ 28661d83f052Sdrh goto select_end; 28672282792aSdrh } 28684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, startagg); 28694adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endagg); 28704adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Noop, 0, 0); 28712282792aSdrh } 28722282792aSdrh 2873cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 2874cce7d176Sdrh ** and send them to the callback one by one. 2875cce7d176Sdrh */ 2876cce7d176Sdrh if( pOrderBy ){ 2877ffbc3088Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 2878cce7d176Sdrh } 28796a535340Sdrh 288093758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2881f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 2882f620b4e2Sdrh ** temporary table. So delete the subquery structure from the parent 2883f620b4e2Sdrh ** to prevent this subquery from being evaluated again and to force the 2884f620b4e2Sdrh ** the use of the temporary table. 2885f620b4e2Sdrh */ 2886f620b4e2Sdrh if( pParent ){ 2887f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 2888f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 28894adee20fSdanielk1977 sqlite3SelectDelete(p); 2890f620b4e2Sdrh pParent->pSrc->a[parentTab].pSelect = 0; 2891f620b4e2Sdrh } 289293758c8dSdanielk1977 #endif 2893f620b4e2Sdrh 28941d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 28951d83f052Sdrh ** to indicate no errors. 28961d83f052Sdrh */ 28971d83f052Sdrh rc = 0; 28981d83f052Sdrh 28991d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 29001d83f052Sdrh ** successful coding of the SELECT. 29011d83f052Sdrh */ 29021d83f052Sdrh select_end: 2903b3bce662Sdanielk1977 restoreAggregateInfo(pParse, &sAggInfo); 29041d83f052Sdrh return rc; 2905cce7d176Sdrh } 2906