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*d81bd4e2Sdrh ** $Id: select.c,v 1.260 2005/09/05 20:06:49 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19315555caSdrh 20cce7d176Sdrh /* 219bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that 229bb61fe7Sdrh ** structure. 23cce7d176Sdrh */ 244adee20fSdanielk1977 Select *sqlite3SelectNew( 25daffd0e5Sdrh ExprList *pEList, /* which columns to include in the result */ 26ad3cab52Sdrh SrcList *pSrc, /* the FROM clause -- which tables to scan */ 27daffd0e5Sdrh Expr *pWhere, /* the WHERE clause */ 28daffd0e5Sdrh ExprList *pGroupBy, /* the GROUP BY clause */ 29daffd0e5Sdrh Expr *pHaving, /* the HAVING clause */ 30daffd0e5Sdrh ExprList *pOrderBy, /* the ORDER BY clause */ 319bbca4c1Sdrh int isDistinct, /* true if the DISTINCT keyword is present */ 32a2dc3b1aSdanielk1977 Expr *pLimit, /* LIMIT value. NULL means not used */ 33a2dc3b1aSdanielk1977 Expr *pOffset /* OFFSET value. NULL means no offset */ 349bb61fe7Sdrh ){ 359bb61fe7Sdrh Select *pNew; 369bb61fe7Sdrh pNew = sqliteMalloc( sizeof(*pNew) ); 37a2dc3b1aSdanielk1977 assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */ 38daffd0e5Sdrh if( pNew==0 ){ 394adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 404adee20fSdanielk1977 sqlite3SrcListDelete(pSrc); 414adee20fSdanielk1977 sqlite3ExprDelete(pWhere); 424adee20fSdanielk1977 sqlite3ExprListDelete(pGroupBy); 434adee20fSdanielk1977 sqlite3ExprDelete(pHaving); 444adee20fSdanielk1977 sqlite3ExprListDelete(pOrderBy); 45a2dc3b1aSdanielk1977 sqlite3ExprDelete(pLimit); 46a2dc3b1aSdanielk1977 sqlite3ExprDelete(pOffset); 47daffd0e5Sdrh }else{ 48b733d037Sdrh if( pEList==0 ){ 494adee20fSdanielk1977 pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0); 50b733d037Sdrh } 519bb61fe7Sdrh pNew->pEList = pEList; 529bb61fe7Sdrh pNew->pSrc = pSrc; 539bb61fe7Sdrh pNew->pWhere = pWhere; 549bb61fe7Sdrh pNew->pGroupBy = pGroupBy; 559bb61fe7Sdrh pNew->pHaving = pHaving; 569bb61fe7Sdrh pNew->pOrderBy = pOrderBy; 579bb61fe7Sdrh pNew->isDistinct = isDistinct; 5882c3d636Sdrh pNew->op = TK_SELECT; 59a2dc3b1aSdanielk1977 pNew->pLimit = pLimit; 60a2dc3b1aSdanielk1977 pNew->pOffset = pOffset; 617b58daeaSdrh pNew->iLimit = -1; 627b58daeaSdrh pNew->iOffset = -1; 630342b1f5Sdrh pNew->addrOpenVirt[0] = -1; 640342b1f5Sdrh pNew->addrOpenVirt[1] = -1; 650342b1f5Sdrh pNew->addrOpenVirt[2] = -1; 66daffd0e5Sdrh } 679bb61fe7Sdrh return pNew; 689bb61fe7Sdrh } 699bb61fe7Sdrh 709bb61fe7Sdrh /* 7101f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the 7201f3f253Sdrh ** type of join. Return an integer constant that expresses that type 7301f3f253Sdrh ** in terms of the following bit values: 7401f3f253Sdrh ** 7501f3f253Sdrh ** JT_INNER 7601f3f253Sdrh ** JT_OUTER 7701f3f253Sdrh ** JT_NATURAL 7801f3f253Sdrh ** JT_LEFT 7901f3f253Sdrh ** JT_RIGHT 8001f3f253Sdrh ** 8101f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT. 8201f3f253Sdrh ** 8301f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return 8401f3f253Sdrh ** a join type, but put an error in the pParse structure. 8501f3f253Sdrh */ 864adee20fSdanielk1977 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ 8701f3f253Sdrh int jointype = 0; 8801f3f253Sdrh Token *apAll[3]; 8901f3f253Sdrh Token *p; 905719628aSdrh static const struct { 91c182d163Sdrh const char zKeyword[8]; 92290c1948Sdrh u8 nChar; 93290c1948Sdrh u8 code; 9401f3f253Sdrh } keywords[] = { 9501f3f253Sdrh { "natural", 7, JT_NATURAL }, 96195e6967Sdrh { "left", 4, JT_LEFT|JT_OUTER }, 97195e6967Sdrh { "right", 5, JT_RIGHT|JT_OUTER }, 98195e6967Sdrh { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, 9901f3f253Sdrh { "outer", 5, JT_OUTER }, 10001f3f253Sdrh { "inner", 5, JT_INNER }, 10101f3f253Sdrh { "cross", 5, JT_INNER }, 10201f3f253Sdrh }; 10301f3f253Sdrh int i, j; 10401f3f253Sdrh apAll[0] = pA; 10501f3f253Sdrh apAll[1] = pB; 10601f3f253Sdrh apAll[2] = pC; 107195e6967Sdrh for(i=0; i<3 && apAll[i]; i++){ 10801f3f253Sdrh p = apAll[i]; 10901f3f253Sdrh for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ 11001f3f253Sdrh if( p->n==keywords[j].nChar 1114adee20fSdanielk1977 && sqlite3StrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ 11201f3f253Sdrh jointype |= keywords[j].code; 11301f3f253Sdrh break; 11401f3f253Sdrh } 11501f3f253Sdrh } 11601f3f253Sdrh if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ 11701f3f253Sdrh jointype |= JT_ERROR; 11801f3f253Sdrh break; 11901f3f253Sdrh } 12001f3f253Sdrh } 121ad2d8307Sdrh if( 122ad2d8307Sdrh (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || 123195e6967Sdrh (jointype & JT_ERROR)!=0 124ad2d8307Sdrh ){ 125ae29ffbeSdrh const char *zSp1 = " "; 126ae29ffbeSdrh const char *zSp2 = " "; 127ae29ffbeSdrh if( pB==0 ){ zSp1++; } 128ae29ffbeSdrh if( pC==0 ){ zSp2++; } 129ae29ffbeSdrh sqlite3ErrorMsg(pParse, "unknown or unsupported join type: " 130ae29ffbeSdrh "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC); 13101f3f253Sdrh jointype = JT_INNER; 132195e6967Sdrh }else if( jointype & JT_RIGHT ){ 1334adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 134da93d238Sdrh "RIGHT and FULL OUTER JOINs are not currently supported"); 135195e6967Sdrh jointype = JT_INNER; 13601f3f253Sdrh } 13701f3f253Sdrh return jointype; 13801f3f253Sdrh } 13901f3f253Sdrh 14001f3f253Sdrh /* 141ad2d8307Sdrh ** Return the index of a column in a table. Return -1 if the column 142ad2d8307Sdrh ** is not contained in the table. 143ad2d8307Sdrh */ 144ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){ 145ad2d8307Sdrh int i; 146ad2d8307Sdrh for(i=0; i<pTab->nCol; i++){ 1474adee20fSdanielk1977 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; 148ad2d8307Sdrh } 149ad2d8307Sdrh return -1; 150ad2d8307Sdrh } 151ad2d8307Sdrh 152ad2d8307Sdrh /* 15391bb0eedSdrh ** Set the value of a token to a '\000'-terminated string. 15491bb0eedSdrh */ 15591bb0eedSdrh static void setToken(Token *p, const char *z){ 15691bb0eedSdrh p->z = z; 15791bb0eedSdrh p->n = strlen(z); 15891bb0eedSdrh p->dyn = 0; 15991bb0eedSdrh } 16091bb0eedSdrh 161c182d163Sdrh /* 162c182d163Sdrh ** Create an expression node for an identifier with the name of zName 163c182d163Sdrh */ 164c182d163Sdrh static Expr *createIdExpr(const char *zName){ 165c182d163Sdrh Token dummy; 166c182d163Sdrh setToken(&dummy, zName); 167c182d163Sdrh return sqlite3Expr(TK_ID, 0, 0, &dummy); 168c182d163Sdrh } 169c182d163Sdrh 17091bb0eedSdrh 17191bb0eedSdrh /* 172ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the 173ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2. 174ad2d8307Sdrh */ 175ad2d8307Sdrh static void addWhereTerm( 176ad2d8307Sdrh const char *zCol, /* Name of the column */ 177ad2d8307Sdrh const Table *pTab1, /* First table */ 178030530deSdrh const char *zAlias1, /* Alias for first table. May be NULL */ 179ad2d8307Sdrh const Table *pTab2, /* Second table */ 180030530deSdrh const char *zAlias2, /* Alias for second table. May be NULL */ 181ad2d8307Sdrh Expr **ppExpr /* Add the equality term to this expression */ 182ad2d8307Sdrh ){ 183ad2d8307Sdrh Expr *pE1a, *pE1b, *pE1c; 184ad2d8307Sdrh Expr *pE2a, *pE2b, *pE2c; 185ad2d8307Sdrh Expr *pE; 186ad2d8307Sdrh 187c182d163Sdrh pE1a = createIdExpr(zCol); 188c182d163Sdrh pE2a = createIdExpr(zCol); 189030530deSdrh if( zAlias1==0 ){ 190030530deSdrh zAlias1 = pTab1->zName; 191030530deSdrh } 192c182d163Sdrh pE1b = createIdExpr(zAlias1); 193030530deSdrh if( zAlias2==0 ){ 194030530deSdrh zAlias2 = pTab2->zName; 195030530deSdrh } 196c182d163Sdrh pE2b = createIdExpr(zAlias2); 1974adee20fSdanielk1977 pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0); 1984adee20fSdanielk1977 pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0); 1994adee20fSdanielk1977 pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0); 2001f16230bSdrh ExprSetProperty(pE, EP_FromJoin); 20191bb0eedSdrh *ppExpr = sqlite3ExprAnd(*ppExpr, pE); 202ad2d8307Sdrh } 203ad2d8307Sdrh 204ad2d8307Sdrh /* 2051f16230bSdrh ** Set the EP_FromJoin property on all terms of the given expression. 2061cc093c2Sdrh ** 207e78e8284Sdrh ** The EP_FromJoin property is used on terms of an expression to tell 2081cc093c2Sdrh ** the LEFT OUTER JOIN processing logic that this term is part of the 2091f16230bSdrh ** join restriction specified in the ON or USING clause and not a part 2101f16230bSdrh ** of the more general WHERE clause. These terms are moved over to the 2111f16230bSdrh ** WHERE clause during join processing but we need to remember that they 2121f16230bSdrh ** originated in the ON or USING clause. 2131cc093c2Sdrh */ 2141cc093c2Sdrh static void setJoinExpr(Expr *p){ 2151cc093c2Sdrh while( p ){ 2161f16230bSdrh ExprSetProperty(p, EP_FromJoin); 2171cc093c2Sdrh setJoinExpr(p->pLeft); 2181cc093c2Sdrh p = p->pRight; 2191cc093c2Sdrh } 2201cc093c2Sdrh } 2211cc093c2Sdrh 2221cc093c2Sdrh /* 223ad2d8307Sdrh ** This routine processes the join information for a SELECT statement. 224ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause. 225ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms. 226ad2d8307Sdrh ** 22791bb0eedSdrh ** The terms of a FROM clause are contained in the Select.pSrc structure. 22891bb0eedSdrh ** The left most table is the first entry in Select.pSrc. The right-most 22991bb0eedSdrh ** table is the last entry. The join operator is held in the entry to 23091bb0eedSdrh ** the left. Thus entry 0 contains the join operator for the join between 23191bb0eedSdrh ** entries 0 and 1. Any ON or USING clauses associated with the join are 23291bb0eedSdrh ** also attached to the left entry. 23391bb0eedSdrh ** 234ad2d8307Sdrh ** This routine returns the number of errors encountered. 235ad2d8307Sdrh */ 236ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){ 23791bb0eedSdrh SrcList *pSrc; /* All tables in the FROM clause */ 23891bb0eedSdrh int i, j; /* Loop counters */ 23991bb0eedSdrh struct SrcList_item *pLeft; /* Left table being joined */ 24091bb0eedSdrh struct SrcList_item *pRight; /* Right table being joined */ 241ad2d8307Sdrh 24291bb0eedSdrh pSrc = p->pSrc; 24391bb0eedSdrh pLeft = &pSrc->a[0]; 24491bb0eedSdrh pRight = &pLeft[1]; 24591bb0eedSdrh for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){ 24691bb0eedSdrh Table *pLeftTab = pLeft->pTab; 24791bb0eedSdrh Table *pRightTab = pRight->pTab; 24891bb0eedSdrh 24991bb0eedSdrh if( pLeftTab==0 || pRightTab==0 ) continue; 250ad2d8307Sdrh 251ad2d8307Sdrh /* When the NATURAL keyword is present, add WHERE clause terms for 252ad2d8307Sdrh ** every column that the two tables have in common. 253ad2d8307Sdrh */ 25491bb0eedSdrh if( pLeft->jointype & JT_NATURAL ){ 25591bb0eedSdrh if( pLeft->pOn || pLeft->pUsing ){ 2564adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "a NATURAL join may not have " 257ad2d8307Sdrh "an ON or USING clause", 0); 258ad2d8307Sdrh return 1; 259ad2d8307Sdrh } 26091bb0eedSdrh for(j=0; j<pLeftTab->nCol; j++){ 26191bb0eedSdrh char *zName = pLeftTab->aCol[j].zName; 26291bb0eedSdrh if( columnIndex(pRightTab, zName)>=0 ){ 263030530deSdrh addWhereTerm(zName, pLeftTab, pLeft->zAlias, 264030530deSdrh pRightTab, pRight->zAlias, &p->pWhere); 265ad2d8307Sdrh } 266ad2d8307Sdrh } 267ad2d8307Sdrh } 268ad2d8307Sdrh 269ad2d8307Sdrh /* Disallow both ON and USING clauses in the same join 270ad2d8307Sdrh */ 27191bb0eedSdrh if( pLeft->pOn && pLeft->pUsing ){ 2724adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot have both ON and USING " 273da93d238Sdrh "clauses in the same join"); 274ad2d8307Sdrh return 1; 275ad2d8307Sdrh } 276ad2d8307Sdrh 277ad2d8307Sdrh /* Add the ON clause to the end of the WHERE clause, connected by 27891bb0eedSdrh ** an AND operator. 279ad2d8307Sdrh */ 28091bb0eedSdrh if( pLeft->pOn ){ 28191bb0eedSdrh setJoinExpr(pLeft->pOn); 28291bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pLeft->pOn); 28391bb0eedSdrh pLeft->pOn = 0; 284ad2d8307Sdrh } 285ad2d8307Sdrh 286ad2d8307Sdrh /* Create extra terms on the WHERE clause for each column named 287ad2d8307Sdrh ** in the USING clause. Example: If the two tables to be joined are 288ad2d8307Sdrh ** A and B and the USING clause names X, Y, and Z, then add this 289ad2d8307Sdrh ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z 290ad2d8307Sdrh ** Report an error if any column mentioned in the USING clause is 291ad2d8307Sdrh ** not contained in both tables to be joined. 292ad2d8307Sdrh */ 29391bb0eedSdrh if( pLeft->pUsing ){ 29491bb0eedSdrh IdList *pList = pLeft->pUsing; 295ad2d8307Sdrh for(j=0; j<pList->nId; j++){ 29691bb0eedSdrh char *zName = pList->a[j].zName; 29791bb0eedSdrh if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){ 2984adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot join using column %s - column " 29991bb0eedSdrh "not present in both tables", zName); 300ad2d8307Sdrh return 1; 301ad2d8307Sdrh } 302030530deSdrh addWhereTerm(zName, pLeftTab, pLeft->zAlias, 303030530deSdrh pRightTab, pRight->zAlias, &p->pWhere); 304ad2d8307Sdrh } 305ad2d8307Sdrh } 306ad2d8307Sdrh } 307ad2d8307Sdrh return 0; 308ad2d8307Sdrh } 309ad2d8307Sdrh 310ad2d8307Sdrh /* 3119bb61fe7Sdrh ** Delete the given Select structure and all of its substructures. 3129bb61fe7Sdrh */ 3134adee20fSdanielk1977 void sqlite3SelectDelete(Select *p){ 31482c3d636Sdrh if( p==0 ) return; 3154adee20fSdanielk1977 sqlite3ExprListDelete(p->pEList); 3164adee20fSdanielk1977 sqlite3SrcListDelete(p->pSrc); 3174adee20fSdanielk1977 sqlite3ExprDelete(p->pWhere); 3184adee20fSdanielk1977 sqlite3ExprListDelete(p->pGroupBy); 3194adee20fSdanielk1977 sqlite3ExprDelete(p->pHaving); 3204adee20fSdanielk1977 sqlite3ExprListDelete(p->pOrderBy); 3214adee20fSdanielk1977 sqlite3SelectDelete(p->pPrior); 322a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 323a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pOffset); 3249bb61fe7Sdrh sqliteFree(p); 3259bb61fe7Sdrh } 3269bb61fe7Sdrh 3279bb61fe7Sdrh /* 328c926afbcSdrh ** Insert code into "v" that will push the record on the top of the 329c926afbcSdrh ** stack into the sorter. 330c926afbcSdrh */ 331c926afbcSdrh static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){ 332c182d163Sdrh sqlite3ExprCodeExprList(pParse, pOrderBy); 3334db38a70Sdrh sqlite3VdbeAddOp(v, OP_Sequence, pOrderBy->iTab, 0); 3344db38a70Sdrh sqlite3VdbeAddOp(v, OP_Pull, pOrderBy->nExpr + 1, 0); 3354db38a70Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr + 2, 0); 3360342b1f5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pOrderBy->iTab, 0); 337c926afbcSdrh } 338c926afbcSdrh 339c926afbcSdrh /* 340ea48eb2eSdrh ** Add code to implement the OFFSET and LIMIT 341ea48eb2eSdrh */ 342ea48eb2eSdrh static void codeLimiter( 343bab39e13Sdrh Vdbe *v, /* Generate code into this VM */ 344ea48eb2eSdrh Select *p, /* The SELECT statement being coded */ 345ea48eb2eSdrh int iContinue, /* Jump here to skip the current record */ 346ea48eb2eSdrh int iBreak, /* Jump here to end the loop */ 347ea48eb2eSdrh int nPop /* Number of times to pop stack when jumping */ 348ea48eb2eSdrh ){ 349ea48eb2eSdrh if( p->iOffset>=0 ){ 350a2dc3b1aSdanielk1977 int addr = sqlite3VdbeCurrentAddr(v) + 3; 351ea48eb2eSdrh if( nPop>0 ) addr++; 352a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, 0); 353a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_IfMemPos, p->iOffset, addr); 354ea48eb2eSdrh if( nPop>0 ){ 355ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_Pop, nPop, 0); 356ea48eb2eSdrh } 357ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 358ad6d9460Sdrh VdbeComment((v, "# skip OFFSET records")); 359ea48eb2eSdrh } 360ea48eb2eSdrh if( p->iLimit>=0 ){ 361ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak); 362ad6d9460Sdrh VdbeComment((v, "# exit when LIMIT reached")); 363ea48eb2eSdrh } 364ea48eb2eSdrh } 365ea48eb2eSdrh 366ea48eb2eSdrh /* 3672282792aSdrh ** This routine generates the code for the inside of the inner loop 3682282792aSdrh ** of a SELECT. 36982c3d636Sdrh ** 37038640e15Sdrh ** If srcTab and nColumn are both zero, then the pEList expressions 37138640e15Sdrh ** are evaluated in order to get the data for this row. If nColumn>0 37238640e15Sdrh ** then data is pulled from srcTab and pEList is used only to get the 37338640e15Sdrh ** datatypes for each column. 3742282792aSdrh */ 3752282792aSdrh static int selectInnerLoop( 3762282792aSdrh Parse *pParse, /* The parser context */ 377df199a25Sdrh Select *p, /* The complete select statement being coded */ 3782282792aSdrh ExprList *pEList, /* List of values being extracted */ 37982c3d636Sdrh int srcTab, /* Pull data from this table */ 380967e8b73Sdrh int nColumn, /* Number of columns in the source table */ 3812282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 3822282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 3832282792aSdrh int eDest, /* How to dispose of the results */ 3842282792aSdrh int iParm, /* An argument to the disposal method */ 3852282792aSdrh int iContinue, /* Jump here to continue with next row */ 38684ac9d02Sdanielk1977 int iBreak, /* Jump here to break out of the inner loop */ 38784ac9d02Sdanielk1977 char *aff /* affinity string if eDest is SRT_Union */ 3882282792aSdrh ){ 3892282792aSdrh Vdbe *v = pParse->pVdbe; 3902282792aSdrh int i; 391ea48eb2eSdrh int hasDistinct; /* True if the DISTINCT keyword is present */ 39238640e15Sdrh 393daffd0e5Sdrh if( v==0 ) return 0; 39438640e15Sdrh assert( pEList!=0 ); 3952282792aSdrh 396df199a25Sdrh /* If there was a LIMIT clause on the SELECT statement, then do the check 397df199a25Sdrh ** to see if this row should be output. 398df199a25Sdrh */ 399ea48eb2eSdrh hasDistinct = distinct>=0 && pEList && pEList->nExpr>0; 400ea48eb2eSdrh if( pOrderBy==0 && !hasDistinct ){ 401bab39e13Sdrh codeLimiter(v, p, iContinue, iBreak, 0); 402df199a25Sdrh } 403df199a25Sdrh 404967e8b73Sdrh /* Pull the requested columns. 4052282792aSdrh */ 40638640e15Sdrh if( nColumn>0 ){ 407967e8b73Sdrh for(i=0; i<nColumn; i++){ 4084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, i); 40982c3d636Sdrh } 41038640e15Sdrh }else{ 41138640e15Sdrh nColumn = pEList->nExpr; 412c182d163Sdrh sqlite3ExprCodeExprList(pParse, pEList); 41382c3d636Sdrh } 4142282792aSdrh 415daffd0e5Sdrh /* If the DISTINCT keyword was present on the SELECT statement 416daffd0e5Sdrh ** and this row has been seen before, then do not make this row 417daffd0e5Sdrh ** part of the result. 4182282792aSdrh */ 419ea48eb2eSdrh if( hasDistinct ){ 420c182d163Sdrh int n = pEList->nExpr; 4210bd1f4eaSdrh #if NULL_ALWAYS_DISTINCT 4224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7); 4230bd1f4eaSdrh #endif 424ededfd5eSdanielk1977 /* Deliberately leave the affinity string off of the following 425ededfd5eSdanielk1977 ** OP_MakeRecord */ 426c182d163Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, -n, 0); 4274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3); 428c182d163Sdrh sqlite3VdbeAddOp(v, OP_Pop, n+1, 0); 4294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 430ad6d9460Sdrh VdbeComment((v, "# skip indistinct records")); 431f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, distinct, 0); 432ea48eb2eSdrh if( pOrderBy==0 ){ 433bab39e13Sdrh codeLimiter(v, p, iContinue, iBreak, nColumn); 434ea48eb2eSdrh } 4352282792aSdrh } 43682c3d636Sdrh 437c926afbcSdrh switch( eDest ){ 4385338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 43982c3d636Sdrh /* In this mode, write each query result to the key of the temporary 44082c3d636Sdrh ** table iParm. 4412282792aSdrh */ 442c926afbcSdrh case SRT_Union: { 4434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 44484ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 445f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, iParm, 0); 446c926afbcSdrh break; 447c926afbcSdrh } 44882c3d636Sdrh 44982c3d636Sdrh /* Construct a record from the query result, but instead of 45082c3d636Sdrh ** saving that record, use it as a key to delete elements from 45182c3d636Sdrh ** the temporary table iParm. 45282c3d636Sdrh */ 453c926afbcSdrh case SRT_Except: { 4540bd1f4eaSdrh int addr; 4554adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); 45684ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 4574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3); 4584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Delete, iParm, 0); 459c926afbcSdrh break; 460c926afbcSdrh } 4615338a5f7Sdanielk1977 #endif 4625338a5f7Sdanielk1977 4635338a5f7Sdanielk1977 /* Store the result as data using a unique key. 4645338a5f7Sdanielk1977 */ 4655338a5f7Sdanielk1977 case SRT_Table: 4665338a5f7Sdanielk1977 case SRT_TempTable: { 4675338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 4685338a5f7Sdanielk1977 if( pOrderBy ){ 4695338a5f7Sdanielk1977 pushOntoSorter(pParse, v, pOrderBy); 4705338a5f7Sdanielk1977 }else{ 471f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0); 4725338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 473f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, iParm, 0); 4745338a5f7Sdanielk1977 } 4755338a5f7Sdanielk1977 break; 4765338a5f7Sdanielk1977 } 4772282792aSdrh 47893758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 4792282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 4802282792aSdrh ** then there should be a single item on the stack. Write this 4812282792aSdrh ** item into the set table with bogus data. 4822282792aSdrh */ 483c926afbcSdrh case SRT_Set: { 4844adee20fSdanielk1977 int addr1 = sqlite3VdbeCurrentAddr(v); 48552b36cabSdrh int addr2; 486e014a838Sdanielk1977 487967e8b73Sdrh assert( nColumn==1 ); 4884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3); 4894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 4904adee20fSdanielk1977 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 491c926afbcSdrh if( pOrderBy ){ 492de941c60Sdrh /* At first glance you would think we could optimize out the 493de941c60Sdrh ** ORDER BY in this case since the order of entries in the set 494de941c60Sdrh ** does not matter. But there might be a LIMIT clause, in which 495de941c60Sdrh ** case the order does matter */ 496c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 497c926afbcSdrh }else{ 498e014a838Sdanielk1977 char aff = (iParm>>16)&0xFF; 499e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff); 50094a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &aff, 1); 501f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0); 502c926afbcSdrh } 5034adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v)); 504c926afbcSdrh break; 505c926afbcSdrh } 50682c3d636Sdrh 5072282792aSdrh /* If this is a scalar select that is part of an expression, then 5082282792aSdrh ** store the results in the appropriate memory cell and break out 5092282792aSdrh ** of the scan loop. 5102282792aSdrh */ 51151522cd3Sdrh case SRT_Exists: 512c926afbcSdrh case SRT_Mem: { 513967e8b73Sdrh assert( nColumn==1 ); 514c926afbcSdrh if( pOrderBy ){ 515c926afbcSdrh pushOntoSorter(pParse, v, pOrderBy); 516c926afbcSdrh }else{ 5174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 5184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iBreak); 519c926afbcSdrh } 520c926afbcSdrh break; 521c926afbcSdrh } 52293758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ 5232282792aSdrh 524c182d163Sdrh /* Send the data to the callback function or to a subroutine. In the 525c182d163Sdrh ** case of a subroutine, the subroutine itself is responsible for 526c182d163Sdrh ** popping the data from the stack. 527f46f905aSdrh */ 528c182d163Sdrh case SRT_Subroutine: 529f46f905aSdrh case SRT_Callback: 530f46f905aSdrh case SRT_Sorter: { 531f46f905aSdrh if( pOrderBy ){ 532ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 533f46f905aSdrh pushOntoSorter(pParse, v, pOrderBy); 534c182d163Sdrh }else if( eDest==SRT_Subroutine ){ 5354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 536c182d163Sdrh }else{ 537c182d163Sdrh assert( eDest!=SRT_Sorter ); 538c182d163Sdrh sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 539ac82fcf5Sdrh } 540142e30dfSdrh break; 541142e30dfSdrh } 542142e30dfSdrh 5436a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_TRIGGER) 544d7489c39Sdrh /* Discard the results. This is used for SELECT statements inside 545d7489c39Sdrh ** the body of a TRIGGER. The purpose of such selects is to call 546d7489c39Sdrh ** user-defined functions that have side effects. We do not care 547d7489c39Sdrh ** about the actual results of the select. 548d7489c39Sdrh */ 549c926afbcSdrh default: { 550f46f905aSdrh assert( eDest==SRT_Discard ); 5514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 552c926afbcSdrh break; 553c926afbcSdrh } 55493758c8dSdanielk1977 #endif 555c926afbcSdrh } 55682c3d636Sdrh return 0; 55782c3d636Sdrh } 55882c3d636Sdrh 55982c3d636Sdrh /* 560dece1a84Sdrh ** Given an expression list, generate a KeyInfo structure that records 561dece1a84Sdrh ** the collating sequence for each expression in that expression list. 562dece1a84Sdrh ** 5630342b1f5Sdrh ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting 5640342b1f5Sdrh ** KeyInfo structure is appropriate for initializing a virtual index to 5650342b1f5Sdrh ** implement that clause. If the ExprList is the result set of a SELECT 5660342b1f5Sdrh ** then the KeyInfo structure is appropriate for initializing a virtual 5670342b1f5Sdrh ** index to implement a DISTINCT test. 5680342b1f5Sdrh ** 569dece1a84Sdrh ** Space to hold the KeyInfo structure is obtain from malloc. The calling 570dece1a84Sdrh ** function is responsible for seeing that this structure is eventually 571dece1a84Sdrh ** freed. Add the KeyInfo structure to the P3 field of an opcode using 572dece1a84Sdrh ** P3_KEYINFO_HANDOFF is the usual way of dealing with this. 573dece1a84Sdrh */ 574dece1a84Sdrh static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){ 575dece1a84Sdrh sqlite3 *db = pParse->db; 576dece1a84Sdrh int nExpr; 577dece1a84Sdrh KeyInfo *pInfo; 578dece1a84Sdrh struct ExprList_item *pItem; 579dece1a84Sdrh int i; 580dece1a84Sdrh 581dece1a84Sdrh nExpr = pList->nExpr; 582dece1a84Sdrh pInfo = sqliteMalloc( sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) ); 583dece1a84Sdrh if( pInfo ){ 584dece1a84Sdrh pInfo->aSortOrder = (char*)&pInfo->aColl[nExpr]; 585dece1a84Sdrh pInfo->nField = nExpr; 586dece1a84Sdrh pInfo->enc = db->enc; 587dece1a84Sdrh for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){ 588dece1a84Sdrh CollSeq *pColl; 589dece1a84Sdrh pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); 590dece1a84Sdrh if( !pColl ){ 591dece1a84Sdrh pColl = db->pDfltColl; 592dece1a84Sdrh } 593dece1a84Sdrh pInfo->aColl[i] = pColl; 594dece1a84Sdrh pInfo->aSortOrder[i] = pItem->sortOrder; 595dece1a84Sdrh } 596dece1a84Sdrh } 597dece1a84Sdrh return pInfo; 598dece1a84Sdrh } 599dece1a84Sdrh 600dece1a84Sdrh 601dece1a84Sdrh /* 602d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 603d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 604d8bc7086Sdrh ** we need to run the sorter and output the results. The following 605d8bc7086Sdrh ** routine generates the code needed to do that. 606d8bc7086Sdrh */ 607c926afbcSdrh static void generateSortTail( 608ffbc3088Sdrh Parse *pParse, /* The parsing context */ 609c926afbcSdrh Select *p, /* The SELECT statement */ 610c926afbcSdrh Vdbe *v, /* Generate code into this VDBE */ 611c926afbcSdrh int nColumn, /* Number of columns of data */ 612c926afbcSdrh int eDest, /* Write the sorted results here */ 613c926afbcSdrh int iParm /* Optional parameter associated with eDest */ 614c926afbcSdrh ){ 6150342b1f5Sdrh int brk = sqlite3VdbeMakeLabel(v); 6160342b1f5Sdrh int cont = sqlite3VdbeMakeLabel(v); 617d8bc7086Sdrh int addr; 6180342b1f5Sdrh int iTab; 6190342b1f5Sdrh ExprList *pOrderBy = p->pOrderBy; 620ffbc3088Sdrh 621f46f905aSdrh if( eDest==SRT_Sorter ) return; 6220342b1f5Sdrh iTab = pOrderBy->iTab; 6230342b1f5Sdrh addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk); 6240342b1f5Sdrh codeLimiter(v, p, cont, brk, 0); 6254db38a70Sdrh sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1); 626c926afbcSdrh switch( eDest ){ 627c926afbcSdrh case SRT_Table: 628c926afbcSdrh case SRT_TempTable: { 629f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0); 6304adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 631f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, iParm, 0); 632c926afbcSdrh break; 633c926afbcSdrh } 63493758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 635c926afbcSdrh case SRT_Set: { 636c926afbcSdrh assert( nColumn==1 ); 6374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 6384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 6394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3); 640ededfd5eSdanielk1977 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, "n", P3_STATIC); 641f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0); 642c926afbcSdrh break; 643c926afbcSdrh } 64451522cd3Sdrh case SRT_Exists: 645c926afbcSdrh case SRT_Mem: { 646c926afbcSdrh assert( nColumn==1 ); 6474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 6480342b1f5Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, brk); 649c926afbcSdrh break; 650c926afbcSdrh } 65193758c8dSdanielk1977 #endif 652ce665cf6Sdrh case SRT_Callback: 653ac82fcf5Sdrh case SRT_Subroutine: { 654ac82fcf5Sdrh int i; 65584ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0); 65684ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 657ac82fcf5Sdrh for(i=0; i<nColumn; i++){ 6584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, -1-i, i); 659ac82fcf5Sdrh } 660ce665cf6Sdrh if( eDest==SRT_Callback ){ 661ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 662ce665cf6Sdrh }else{ 6634adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 664ce665cf6Sdrh } 66584ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 666ac82fcf5Sdrh break; 667ac82fcf5Sdrh } 668c926afbcSdrh default: { 669f46f905aSdrh /* Do nothing */ 670c926afbcSdrh break; 671c926afbcSdrh } 672c926afbcSdrh } 6730342b1f5Sdrh sqlite3VdbeResolveLabel(v, cont); 6740342b1f5Sdrh sqlite3VdbeAddOp(v, OP_Next, iTab, addr); 6750342b1f5Sdrh sqlite3VdbeResolveLabel(v, brk); 676d8bc7086Sdrh } 677d8bc7086Sdrh 678d8bc7086Sdrh /* 679517eb646Sdanielk1977 ** Return a pointer to a string containing the 'declaration type' of the 680517eb646Sdanielk1977 ** expression pExpr. The string may be treated as static by the caller. 681e78e8284Sdrh ** 682517eb646Sdanielk1977 ** If the declaration type is the exact datatype definition extracted from 683517eb646Sdanielk1977 ** the original CREATE TABLE statement if the expression is a column. 684e78e8284Sdrh ** 685517eb646Sdanielk1977 ** The declaration type for an expression is either TEXT, NUMERIC or ANY. 686517eb646Sdanielk1977 ** The declaration type for a ROWID field is INTEGER. 687fcb78a49Sdrh */ 688b3bce662Sdanielk1977 static const char *columnType(NameContext *pNC, Expr *pExpr){ 68900e279d9Sdanielk1977 char const *zType; 690517eb646Sdanielk1977 int j; 691b3bce662Sdanielk1977 if( pExpr==0 || pNC->pSrcList==0 ) return 0; 6925338a5f7Sdanielk1977 6935338a5f7Sdanielk1977 /* The TK_AS operator can only occur in ORDER BY, GROUP BY, HAVING, 6945338a5f7Sdanielk1977 ** and LIMIT clauses. But pExpr originates in the result set of a 6955338a5f7Sdanielk1977 ** SELECT. So pExpr can never contain an AS operator. 6965338a5f7Sdanielk1977 */ 6975338a5f7Sdanielk1977 assert( pExpr->op!=TK_AS ); 6985338a5f7Sdanielk1977 69900e279d9Sdanielk1977 switch( pExpr->op ){ 70000e279d9Sdanielk1977 case TK_COLUMN: { 701b3bce662Sdanielk1977 Table *pTab = 0; 702517eb646Sdanielk1977 int iCol = pExpr->iColumn; 703b3bce662Sdanielk1977 while( pNC && !pTab ){ 704b3bce662Sdanielk1977 SrcList *pTabList = pNC->pSrcList; 705b3bce662Sdanielk1977 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); 706b3bce662Sdanielk1977 if( j<pTabList->nSrc ){ 7076a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 708b3bce662Sdanielk1977 }else{ 709b3bce662Sdanielk1977 pNC = pNC->pNext; 710b3bce662Sdanielk1977 } 711b3bce662Sdanielk1977 } 7127e62779aSdrh if( pTab==0 ){ 7137e62779aSdrh /* FIX ME: 7147e62779aSdrh ** This can occurs if you have something like "SELECT new.x;" inside 7157e62779aSdrh ** a trigger. In other words, if you reference the special "new" 7167e62779aSdrh ** table in the result set of a select. We do not have a good way 7177e62779aSdrh ** to find the actual table type, so call it "TEXT". This is really 7187e62779aSdrh ** something of a bug, but I do not know how to fix it. 7197e62779aSdrh ** 7207e62779aSdrh ** This code does not produce the correct answer - it just prevents 7217e62779aSdrh ** a segfault. See ticket #1229. 7227e62779aSdrh */ 7237e62779aSdrh zType = "TEXT"; 7247e62779aSdrh break; 7257e62779aSdrh } 726b3bce662Sdanielk1977 assert( pTab ); 727fcb78a49Sdrh if( iCol<0 ) iCol = pTab->iPKey; 728fcb78a49Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 729fcb78a49Sdrh if( iCol<0 ){ 730fcb78a49Sdrh zType = "INTEGER"; 731fcb78a49Sdrh }else{ 732fcb78a49Sdrh zType = pTab->aCol[iCol].zType; 733fcb78a49Sdrh } 73400e279d9Sdanielk1977 break; 735736c22b8Sdrh } 73693758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 73700e279d9Sdanielk1977 case TK_SELECT: { 738b3bce662Sdanielk1977 NameContext sNC; 73900e279d9Sdanielk1977 Select *pS = pExpr->pSelect; 740b3bce662Sdanielk1977 sNC.pSrcList = pExpr->pSelect->pSrc; 741b3bce662Sdanielk1977 sNC.pNext = pNC; 742b3bce662Sdanielk1977 zType = columnType(&sNC, pS->pEList->a[0].pExpr); 74300e279d9Sdanielk1977 break; 744fcb78a49Sdrh } 74593758c8dSdanielk1977 #endif 74600e279d9Sdanielk1977 default: 74700e279d9Sdanielk1977 zType = 0; 74800e279d9Sdanielk1977 } 74900e279d9Sdanielk1977 750517eb646Sdanielk1977 return zType; 751517eb646Sdanielk1977 } 752517eb646Sdanielk1977 753517eb646Sdanielk1977 /* 754517eb646Sdanielk1977 ** Generate code that will tell the VDBE the declaration types of columns 755517eb646Sdanielk1977 ** in the result set. 756517eb646Sdanielk1977 */ 757517eb646Sdanielk1977 static void generateColumnTypes( 758517eb646Sdanielk1977 Parse *pParse, /* Parser context */ 759517eb646Sdanielk1977 SrcList *pTabList, /* List of tables */ 760517eb646Sdanielk1977 ExprList *pEList /* Expressions defining the result set */ 761517eb646Sdanielk1977 ){ 762517eb646Sdanielk1977 Vdbe *v = pParse->pVdbe; 763517eb646Sdanielk1977 int i; 764b3bce662Sdanielk1977 NameContext sNC; 765b3bce662Sdanielk1977 sNC.pSrcList = pTabList; 766517eb646Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 767517eb646Sdanielk1977 Expr *p = pEList->a[i].pExpr; 768b3bce662Sdanielk1977 const char *zType = columnType(&sNC, p); 76900e279d9Sdanielk1977 if( zType==0 ) continue; 770fbcd585fSdanielk1977 /* The vdbe must make it's own copy of the column-type, in case the 771fbcd585fSdanielk1977 ** schema is reset before this virtual machine is deleted. 772fbcd585fSdanielk1977 */ 773fbcd585fSdanielk1977 sqlite3VdbeSetColName(v, i+pEList->nExpr, zType, strlen(zType)); 774fcb78a49Sdrh } 775fcb78a49Sdrh } 776fcb78a49Sdrh 777fcb78a49Sdrh /* 778fcb78a49Sdrh ** Generate code that will tell the VDBE the names of columns 779fcb78a49Sdrh ** in the result set. This information is used to provide the 780fcabd464Sdrh ** azCol[] values in the callback. 78182c3d636Sdrh */ 782832508b7Sdrh static void generateColumnNames( 783832508b7Sdrh Parse *pParse, /* Parser context */ 784ad3cab52Sdrh SrcList *pTabList, /* List of tables */ 785832508b7Sdrh ExprList *pEList /* Expressions defining the result set */ 786832508b7Sdrh ){ 787d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 7886a3ea0e6Sdrh int i, j; 7899bb575fdSdrh sqlite3 *db = pParse->db; 790fcabd464Sdrh int fullNames, shortNames; 791fcabd464Sdrh 792fe2093d7Sdrh #ifndef SQLITE_OMIT_EXPLAIN 7933cf86063Sdanielk1977 /* If this is an EXPLAIN, skip this step */ 7943cf86063Sdanielk1977 if( pParse->explain ){ 79561de0d1bSdanielk1977 return; 7963cf86063Sdanielk1977 } 7975338a5f7Sdanielk1977 #endif 7983cf86063Sdanielk1977 799d6502758Sdrh assert( v!=0 ); 8006f8a503dSdanielk1977 if( pParse->colNamesSet || v==0 || sqlite3_malloc_failed ) return; 801d8bc7086Sdrh pParse->colNamesSet = 1; 802fcabd464Sdrh fullNames = (db->flags & SQLITE_FullColNames)!=0; 803fcabd464Sdrh shortNames = (db->flags & SQLITE_ShortColNames)!=0; 80422322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, pEList->nExpr); 80582c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 80682c3d636Sdrh Expr *p; 8075a38705eSdrh p = pEList->a[i].pExpr; 8085a38705eSdrh if( p==0 ) continue; 80982c3d636Sdrh if( pEList->a[i].zName ){ 81082c3d636Sdrh char *zName = pEList->a[i].zName; 811d8123366Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, strlen(zName)); 81282c3d636Sdrh continue; 81382c3d636Sdrh } 814fa173a76Sdrh if( p->op==TK_COLUMN && pTabList ){ 8156a3ea0e6Sdrh Table *pTab; 81697665873Sdrh char *zCol; 8178aff1015Sdrh int iCol = p->iColumn; 8186a3ea0e6Sdrh for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} 8196a3ea0e6Sdrh assert( j<pTabList->nSrc ); 8206a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 8218aff1015Sdrh if( iCol<0 ) iCol = pTab->iPKey; 82297665873Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 823b1363206Sdrh if( iCol<0 ){ 82447a6db2bSdrh zCol = "rowid"; 825b1363206Sdrh }else{ 826b1363206Sdrh zCol = pTab->aCol[iCol].zName; 827b1363206Sdrh } 828fcabd464Sdrh if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ 8293cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); 830fcabd464Sdrh }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ 83182c3d636Sdrh char *zName = 0; 83282c3d636Sdrh char *zTab; 83382c3d636Sdrh 8346a3ea0e6Sdrh zTab = pTabList->a[j].zAlias; 835fcabd464Sdrh if( fullNames || zTab==0 ) zTab = pTab->zName; 8364adee20fSdanielk1977 sqlite3SetString(&zName, zTab, ".", zCol, 0); 8373cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, P3_DYNAMIC); 83882c3d636Sdrh }else{ 83947a6db2bSdrh sqlite3VdbeSetColName(v, i, zCol, strlen(zCol)); 84082c3d636Sdrh } 8416977fea8Sdrh }else if( p->span.z && p->span.z[0] ){ 8423cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); 8433cf86063Sdanielk1977 /* sqlite3VdbeCompressSpace(v, addr); */ 8441bee3d7bSdrh }else{ 8451bee3d7bSdrh char zName[30]; 8461bee3d7bSdrh assert( p->op!=TK_COLUMN || pTabList==0 ); 8471bee3d7bSdrh sprintf(zName, "column%d", i+1); 8483cf86063Sdanielk1977 sqlite3VdbeSetColName(v, i, zName, 0); 84982c3d636Sdrh } 85082c3d636Sdrh } 85176d505baSdanielk1977 generateColumnTypes(pParse, pTabList, pEList); 8525080aaa7Sdrh } 85382c3d636Sdrh 85493758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 85582c3d636Sdrh /* 856d8bc7086Sdrh ** Name of the connection operator, used for error messages. 857d8bc7086Sdrh */ 858d8bc7086Sdrh static const char *selectOpName(int id){ 859d8bc7086Sdrh char *z; 860d8bc7086Sdrh switch( id ){ 861d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 862d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 863d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 864d8bc7086Sdrh default: z = "UNION"; break; 865d8bc7086Sdrh } 866d8bc7086Sdrh return z; 867d8bc7086Sdrh } 86893758c8dSdanielk1977 #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 869d8bc7086Sdrh 870d8bc7086Sdrh /* 871315555caSdrh ** Forward declaration 872315555caSdrh */ 8739b3187e1Sdrh static int prepSelectStmt(Parse*, Select*); 874315555caSdrh 875315555caSdrh /* 87622f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes 87722f70c32Sdrh ** the result set of that SELECT. 87822f70c32Sdrh */ 8794adee20fSdanielk1977 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ 88022f70c32Sdrh Table *pTab; 881b733d037Sdrh int i, j; 88222f70c32Sdrh ExprList *pEList; 883290c1948Sdrh Column *aCol, *pCol; 88422f70c32Sdrh 8859b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 88622f70c32Sdrh return 0; 88722f70c32Sdrh } 888142bdf40Sdanielk1977 if( sqlite3SelectResolve(pParse, pSelect, 0) ){ 889142bdf40Sdanielk1977 return 0; 890142bdf40Sdanielk1977 } 89122f70c32Sdrh pTab = sqliteMalloc( sizeof(Table) ); 89222f70c32Sdrh if( pTab==0 ){ 89322f70c32Sdrh return 0; 89422f70c32Sdrh } 895ed8a3bb1Sdrh pTab->nRef = 1; 89622f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 89722f70c32Sdrh pEList = pSelect->pEList; 89822f70c32Sdrh pTab->nCol = pEList->nExpr; 899417be79cSdrh assert( pTab->nCol>0 ); 900b733d037Sdrh pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 901290c1948Sdrh for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){ 90279d5f63fSdrh Expr *p, *pR; 903517eb646Sdanielk1977 char *zType; 90491bb0eedSdrh char *zName; 90579d5f63fSdrh char *zBasename; 90679d5f63fSdrh int cnt; 907b3bce662Sdanielk1977 NameContext sNC; 90879d5f63fSdrh 90979d5f63fSdrh /* Get an appropriate name for the column 91079d5f63fSdrh */ 91179d5f63fSdrh p = pEList->a[i].pExpr; 912290c1948Sdrh assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 ); 91391bb0eedSdrh if( (zName = pEList->a[i].zName)!=0 ){ 91479d5f63fSdrh /* If the column contains an "AS <name>" phrase, use <name> as the name */ 91591bb0eedSdrh zName = sqliteStrDup(zName); 916517eb646Sdanielk1977 }else if( p->op==TK_DOT 917b733d037Sdrh && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ 91879d5f63fSdrh /* For columns of the from A.B use B as the name */ 91991bb0eedSdrh zName = sqlite3MPrintf("%T", &pR->token); 920b733d037Sdrh }else if( p->span.z && p->span.z[0] ){ 92179d5f63fSdrh /* Use the original text of the column expression as its name */ 92291bb0eedSdrh zName = sqlite3MPrintf("%T", &p->span); 92322f70c32Sdrh }else{ 92479d5f63fSdrh /* If all else fails, make up a name */ 92591bb0eedSdrh zName = sqlite3MPrintf("column%d", i+1); 92622f70c32Sdrh } 92791bb0eedSdrh sqlite3Dequote(zName); 928dd5b2fa5Sdrh if( sqlite3_malloc_failed ){ 929dd5b2fa5Sdrh sqliteFree(zName); 930dd5b2fa5Sdrh sqlite3DeleteTable(0, pTab); 931dd5b2fa5Sdrh return 0; 932dd5b2fa5Sdrh } 93379d5f63fSdrh 93479d5f63fSdrh /* Make sure the column name is unique. If the name is not unique, 93579d5f63fSdrh ** append a integer to the name so that it becomes unique. 93679d5f63fSdrh */ 93779d5f63fSdrh zBasename = zName; 93879d5f63fSdrh for(j=cnt=0; j<i; j++){ 93979d5f63fSdrh if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ 94079d5f63fSdrh zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt); 94179d5f63fSdrh j = -1; 942dd5b2fa5Sdrh if( zName==0 ) break; 94379d5f63fSdrh } 94479d5f63fSdrh } 94579d5f63fSdrh if( zBasename!=zName ){ 94679d5f63fSdrh sqliteFree(zBasename); 94779d5f63fSdrh } 94891bb0eedSdrh pCol->zName = zName; 949e014a838Sdanielk1977 95079d5f63fSdrh /* Get the typename, type affinity, and collating sequence for the 95179d5f63fSdrh ** column. 95279d5f63fSdrh */ 953c43e8be8Sdrh memset(&sNC, 0, sizeof(sNC)); 954b3bce662Sdanielk1977 sNC.pSrcList = pSelect->pSrc; 955b3bce662Sdanielk1977 zType = sqliteStrDup(columnType(&sNC, p)); 956290c1948Sdrh pCol->zType = zType; 957c60e9b82Sdanielk1977 pCol->affinity = sqlite3ExprAffinity(p); 958290c1948Sdrh pCol->pColl = sqlite3ExprCollSeq(pParse, p); 959290c1948Sdrh if( !pCol->pColl ){ 960290c1948Sdrh pCol->pColl = pParse->db->pDfltColl; 9610202b29eSdanielk1977 } 96222f70c32Sdrh } 96322f70c32Sdrh pTab->iPKey = -1; 96422f70c32Sdrh return pTab; 96522f70c32Sdrh } 96622f70c32Sdrh 96722f70c32Sdrh /* 9689b3187e1Sdrh ** Prepare a SELECT statement for processing by doing the following 9699b3187e1Sdrh ** things: 970d8bc7086Sdrh ** 9719b3187e1Sdrh ** (1) Make sure VDBE cursor numbers have been assigned to every 9729b3187e1Sdrh ** element of the FROM clause. 9739b3187e1Sdrh ** 9749b3187e1Sdrh ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that 9759b3187e1Sdrh ** defines FROM clause. When views appear in the FROM clause, 97663eb5f29Sdrh ** fill pTabList->a[].pSelect with a copy of the SELECT statement 97763eb5f29Sdrh ** that implements the view. A copy is made of the view's SELECT 97863eb5f29Sdrh ** statement so that we can freely modify or delete that statement 97963eb5f29Sdrh ** without worrying about messing up the presistent representation 98063eb5f29Sdrh ** of the view. 981d8bc7086Sdrh ** 9829b3187e1Sdrh ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword 983ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 984ad2d8307Sdrh ** 9859b3187e1Sdrh ** (4) Scan the list of columns in the result set (pEList) looking 98654473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 98754473229Sdrh ** If found, expand each "*" to be every column in every table 98854473229Sdrh ** and TABLE.* to be every column in TABLE. 989d8bc7086Sdrh ** 990d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 991d8bc7086Sdrh ** in pParse and return non-zero. 992d8bc7086Sdrh */ 9939b3187e1Sdrh static int prepSelectStmt(Parse *pParse, Select *p){ 99454473229Sdrh int i, j, k, rc; 995ad3cab52Sdrh SrcList *pTabList; 996daffd0e5Sdrh ExprList *pEList; 997a76b5dfcSdrh Table *pTab; 998290c1948Sdrh struct SrcList_item *pFrom; 999daffd0e5Sdrh 1000e94ddc9eSdanielk1977 if( p==0 || p->pSrc==0 || sqlite3_malloc_failed ) return 1; 1001daffd0e5Sdrh pTabList = p->pSrc; 1002daffd0e5Sdrh pEList = p->pEList; 1003d8bc7086Sdrh 10049b3187e1Sdrh /* Make sure cursor numbers have been assigned to all entries in 10059b3187e1Sdrh ** the FROM clause of the SELECT statement. 10069b3187e1Sdrh */ 10079b3187e1Sdrh sqlite3SrcListAssignCursors(pParse, p->pSrc); 10089b3187e1Sdrh 10099b3187e1Sdrh /* Look up every table named in the FROM clause of the select. If 10109b3187e1Sdrh ** an entry of the FROM clause is a subquery instead of a table or view, 10119b3187e1Sdrh ** then create a transient table structure to describe the subquery. 1012d8bc7086Sdrh */ 1013290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 10149b3187e1Sdrh if( pFrom->pTab!=0 ){ 10159b3187e1Sdrh /* This statement has already been prepared. There is no need 10169b3187e1Sdrh ** to go further. */ 10179b3187e1Sdrh assert( i==0 ); 1018d8bc7086Sdrh return 0; 1019d8bc7086Sdrh } 1020290c1948Sdrh if( pFrom->zName==0 ){ 102193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 102222f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 1023290c1948Sdrh assert( pFrom->pSelect!=0 ); 1024290c1948Sdrh if( pFrom->zAlias==0 ){ 102591bb0eedSdrh pFrom->zAlias = 102691bb0eedSdrh sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect); 1027ad2d8307Sdrh } 1028ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1029290c1948Sdrh pFrom->pTab = pTab = 1030290c1948Sdrh sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect); 103122f70c32Sdrh if( pTab==0 ){ 1032daffd0e5Sdrh return 1; 1033daffd0e5Sdrh } 10345cf590c1Sdrh /* The isTransient flag indicates that the Table structure has been 10355cf590c1Sdrh ** dynamically allocated and may be freed at any time. In other words, 10365cf590c1Sdrh ** pTab is not pointing to a persistent table structure that defines 10375cf590c1Sdrh ** part of the schema. */ 103822f70c32Sdrh pTab->isTransient = 1; 103993758c8dSdanielk1977 #endif 104022f70c32Sdrh }else{ 1041a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 1042ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1043290c1948Sdrh pFrom->pTab = pTab = 1044290c1948Sdrh sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); 1045a76b5dfcSdrh if( pTab==0 ){ 1046d8bc7086Sdrh return 1; 1047d8bc7086Sdrh } 1048ed8a3bb1Sdrh pTab->nRef++; 104993758c8dSdanielk1977 #ifndef SQLITE_OMIT_VIEW 1050a76b5dfcSdrh if( pTab->pSelect ){ 105163eb5f29Sdrh /* We reach here if the named table is a really a view */ 10524adee20fSdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 1053417be79cSdrh return 1; 1054417be79cSdrh } 1055290c1948Sdrh /* If pFrom->pSelect!=0 it means we are dealing with a 105663eb5f29Sdrh ** view within a view. The SELECT structure has already been 105763eb5f29Sdrh ** copied by the outer view so we can skip the copy step here 105863eb5f29Sdrh ** in the inner view. 105963eb5f29Sdrh */ 1060290c1948Sdrh if( pFrom->pSelect==0 ){ 1061290c1948Sdrh pFrom->pSelect = sqlite3SelectDup(pTab->pSelect); 1062a76b5dfcSdrh } 1063d8bc7086Sdrh } 106493758c8dSdanielk1977 #endif 106522f70c32Sdrh } 106663eb5f29Sdrh } 1067d8bc7086Sdrh 1068ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 1069ad2d8307Sdrh */ 1070ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 1071ad2d8307Sdrh 10727c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 107354473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 107454473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 10757c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 10767c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 10777c917d19Sdrh ** each one to the list of all columns in all tables. 107854473229Sdrh ** 107954473229Sdrh ** The first loop just checks to see if there are any "*" operators 108054473229Sdrh ** that need expanding. 1081d8bc7086Sdrh */ 10827c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 108354473229Sdrh Expr *pE = pEList->a[k].pExpr; 108454473229Sdrh if( pE->op==TK_ALL ) break; 108554473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 108654473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 10877c917d19Sdrh } 108854473229Sdrh rc = 0; 10897c917d19Sdrh if( k<pEList->nExpr ){ 109054473229Sdrh /* 109154473229Sdrh ** If we get here it means the result set contains one or more "*" 109254473229Sdrh ** operators that need to be expanded. Loop through each expression 109354473229Sdrh ** in the result set and expand them one by one. 109454473229Sdrh */ 10957c917d19Sdrh struct ExprList_item *a = pEList->a; 10967c917d19Sdrh ExprList *pNew = 0; 1097d70dc52dSdrh int flags = pParse->db->flags; 1098d70dc52dSdrh int longNames = (flags & SQLITE_FullColNames)!=0 && 1099d70dc52dSdrh (flags & SQLITE_ShortColNames)==0; 1100d70dc52dSdrh 11017c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 110254473229Sdrh Expr *pE = a[k].pExpr; 110354473229Sdrh if( pE->op!=TK_ALL && 110454473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 110554473229Sdrh /* This particular expression does not need to be expanded. 110654473229Sdrh */ 11074adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 11087c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 11097c917d19Sdrh a[k].pExpr = 0; 11107c917d19Sdrh a[k].zName = 0; 11117c917d19Sdrh }else{ 111254473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 111354473229Sdrh ** expanded. */ 111454473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 1115cf55b7aeSdrh char *zTName; /* text of name of TABLE */ 111654473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 1117cf55b7aeSdrh zTName = sqlite3NameFromToken(&pE->pLeft->token); 111854473229Sdrh }else{ 1119cf55b7aeSdrh zTName = 0; 112054473229Sdrh } 1121290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1122290c1948Sdrh Table *pTab = pFrom->pTab; 1123290c1948Sdrh char *zTabName = pFrom->zAlias; 112454473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 112554473229Sdrh zTabName = pTab->zName; 112654473229Sdrh } 1127cf55b7aeSdrh if( zTName && (zTabName==0 || zTabName[0]==0 || 1128cf55b7aeSdrh sqlite3StrICmp(zTName, zTabName)!=0) ){ 112954473229Sdrh continue; 113054473229Sdrh } 113154473229Sdrh tableSeen = 1; 1132d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 113322f70c32Sdrh Expr *pExpr, *pLeft, *pRight; 1134ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 1135ad2d8307Sdrh 113691bb0eedSdrh if( i>0 ){ 113791bb0eedSdrh struct SrcList_item *pLeft = &pTabList->a[i-1]; 113891bb0eedSdrh if( (pLeft->jointype & JT_NATURAL)!=0 && 113991bb0eedSdrh columnIndex(pLeft->pTab, zName)>=0 ){ 1140ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 1141ad2d8307Sdrh ** table on the right */ 1142ad2d8307Sdrh continue; 1143ad2d8307Sdrh } 114491bb0eedSdrh if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ 1145ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 1146ad2d8307Sdrh ** using clause from the table on the right. */ 1147ad2d8307Sdrh continue; 1148ad2d8307Sdrh } 114991bb0eedSdrh } 11504adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 115122f70c32Sdrh if( pRight==0 ) break; 115291bb0eedSdrh setToken(&pRight->token, zName); 1153d70dc52dSdrh if( zTabName && (longNames || pTabList->nSrc>1) ){ 11544adee20fSdanielk1977 pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 11554adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 115622f70c32Sdrh if( pExpr==0 ) break; 115791bb0eedSdrh setToken(&pLeft->token, zTabName); 115891bb0eedSdrh setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); 11596977fea8Sdrh pExpr->span.dyn = 1; 11606977fea8Sdrh pExpr->token.z = 0; 11616977fea8Sdrh pExpr->token.n = 0; 11626977fea8Sdrh pExpr->token.dyn = 0; 116322f70c32Sdrh }else{ 116422f70c32Sdrh pExpr = pRight; 11656977fea8Sdrh pExpr->span = pExpr->token; 116622f70c32Sdrh } 1167d70dc52dSdrh if( longNames ){ 1168d70dc52dSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); 1169d70dc52dSdrh }else{ 117079d5f63fSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); 1171d8bc7086Sdrh } 1172d8bc7086Sdrh } 1173d70dc52dSdrh } 117454473229Sdrh if( !tableSeen ){ 1175cf55b7aeSdrh if( zTName ){ 1176cf55b7aeSdrh sqlite3ErrorMsg(pParse, "no such table: %s", zTName); 1177f5db2d3eSdrh }else{ 11784adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1179f5db2d3eSdrh } 118054473229Sdrh rc = 1; 118154473229Sdrh } 1182cf55b7aeSdrh sqliteFree(zTName); 11837c917d19Sdrh } 11847c917d19Sdrh } 11854adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 11867c917d19Sdrh p->pEList = pNew; 1187d8bc7086Sdrh } 118854473229Sdrh return rc; 1189d8bc7086Sdrh } 1190d8bc7086Sdrh 119193758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 1192ff78bd2fSdrh /* 1193d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1194d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1195967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1196d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1197d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1198d8bc7086Sdrh ** 1199d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1200d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1201d8bc7086Sdrh ** 1202d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1203d8bc7086Sdrh ** of errors is returned. 1204d8bc7086Sdrh */ 1205d8bc7086Sdrh static int matchOrderbyToColumn( 1206d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1207d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1208d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1209e4de1febSdrh int iTable, /* Insert this value in iTable */ 1210d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1211d8bc7086Sdrh ){ 1212d8bc7086Sdrh int nErr = 0; 1213d8bc7086Sdrh int i, j; 1214d8bc7086Sdrh ExprList *pEList; 1215d8bc7086Sdrh 1216daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1217d8bc7086Sdrh if( mustComplete ){ 1218d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1219d8bc7086Sdrh } 12209b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 1221d8bc7086Sdrh return 1; 1222d8bc7086Sdrh } 1223d8bc7086Sdrh if( pSelect->pPrior ){ 122492cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 122592cd52f5Sdrh return 1; 122692cd52f5Sdrh } 1227d8bc7086Sdrh } 1228d8bc7086Sdrh pEList = pSelect->pEList; 1229d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1230d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1231e4de1febSdrh int iCol = -1; 1232d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 12334adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1234e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 12354adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1236da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1237e4de1febSdrh iCol, pEList->nExpr); 1238e4de1febSdrh nErr++; 1239e4de1febSdrh break; 1240e4de1febSdrh } 1241fcb78a49Sdrh if( !mustComplete ) continue; 1242e4de1febSdrh iCol--; 1243e4de1febSdrh } 1244e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 12454cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1246a76b5dfcSdrh char *zName, *zLabel; 1247a76b5dfcSdrh zName = pEList->a[j].zName; 1248a99db3b6Sdrh zLabel = sqlite3NameFromToken(&pE->token); 1249a99db3b6Sdrh assert( zLabel!=0 ); 12504adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1251e4de1febSdrh iCol = j; 1252d8bc7086Sdrh } 12536e142f54Sdrh sqliteFree(zLabel); 1254d8bc7086Sdrh } 12554adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1256e4de1febSdrh iCol = j; 1257d8bc7086Sdrh } 1258e4de1febSdrh } 1259e4de1febSdrh if( iCol>=0 ){ 1260967e8b73Sdrh pE->op = TK_COLUMN; 1261e4de1febSdrh pE->iColumn = iCol; 1262d8bc7086Sdrh pE->iTable = iTable; 1263a58fdfb1Sdanielk1977 pE->iAgg = -1; 1264d8bc7086Sdrh pOrderBy->a[i].done = 1; 1265d8bc7086Sdrh } 1266e4de1febSdrh if( iCol<0 && mustComplete ){ 12674adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1268da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1269d8bc7086Sdrh nErr++; 1270d8bc7086Sdrh break; 1271d8bc7086Sdrh } 1272d8bc7086Sdrh } 1273d8bc7086Sdrh return nErr; 1274d8bc7086Sdrh } 127593758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */ 1276d8bc7086Sdrh 1277d8bc7086Sdrh /* 1278d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1279d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1280d8bc7086Sdrh */ 12814adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1282d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1283d8bc7086Sdrh if( v==0 ){ 12844adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1285d8bc7086Sdrh } 1286d8bc7086Sdrh return v; 1287d8bc7086Sdrh } 1288d8bc7086Sdrh 1289d8bc7086Sdrh /* 12907b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 1291a2dc3b1aSdanielk1977 ** pLimit and pOffset expressions. nLimit and nOffset hold the expressions 12927b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 1293a2dc3b1aSdanielk1977 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset 1294a2dc3b1aSdanielk1977 ** are the integer memory register numbers for counters used to compute 1295a2dc3b1aSdanielk1977 ** the limit and offset. If there is no limit and/or offset, then 1296a2dc3b1aSdanielk1977 ** iLimit and iOffset are negative. 12977b58daeaSdrh ** 12987b58daeaSdrh ** This routine changes the values if iLimit and iOffset only if 12997b58daeaSdrh ** a limit or offset is defined by nLimit and nOffset. iLimit and 13007b58daeaSdrh ** iOffset should have been preset to appropriate default values 13017b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 13027b58daeaSdrh ** Only if nLimit>=0 or nOffset>0 do the limit registers get 13037b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 13047b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 13057b58daeaSdrh ** SELECT statements. 13067b58daeaSdrh */ 13077b58daeaSdrh static void computeLimitRegisters(Parse *pParse, Select *p){ 13087b58daeaSdrh /* 13097b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 13107b58daeaSdrh ** contraversy about what the correct behavior should be. 13117b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 13127b58daeaSdrh ** no rows. 13137b58daeaSdrh */ 1314a2dc3b1aSdanielk1977 if( p->pLimit ){ 13157b58daeaSdrh int iMem = pParse->nMem++; 13164adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13177b58daeaSdrh if( v==0 ) return; 1318a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pLimit); 1319a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1320a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1322ad6d9460Sdrh VdbeComment((v, "# LIMIT counter")); 13237b58daeaSdrh p->iLimit = iMem; 13247b58daeaSdrh } 1325a2dc3b1aSdanielk1977 if( p->pOffset ){ 13267b58daeaSdrh int iMem = pParse->nMem++; 13274adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 13287b58daeaSdrh if( v==0 ) return; 1329a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pOffset); 1330a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 1331a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 13324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); 1333ad6d9460Sdrh VdbeComment((v, "# OFFSET counter")); 13347b58daeaSdrh p->iOffset = iMem; 13357b58daeaSdrh } 13367b58daeaSdrh } 13377b58daeaSdrh 13387b58daeaSdrh /* 13390342b1f5Sdrh ** Allocate a virtual index to use for sorting. 1340d3d39e93Sdrh */ 13414db38a70Sdrh static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){ 13420342b1f5Sdrh if( pOrderBy ){ 1343dc1bdc4fSdanielk1977 int addr; 13440342b1f5Sdrh assert( pOrderBy->iTab==0 ); 13450342b1f5Sdrh pOrderBy->iTab = pParse->nTab++; 13460342b1f5Sdrh addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenVirtual, 13470342b1f5Sdrh pOrderBy->iTab, pOrderBy->nExpr+1); 13480342b1f5Sdrh assert( p->addrOpenVirt[2] == -1 ); 13490342b1f5Sdrh p->addrOpenVirt[2] = addr; 1350736c22b8Sdrh } 1351dc1bdc4fSdanielk1977 } 1352dc1bdc4fSdanielk1977 1353b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1354fbc4ee7bSdrh /* 1355fbc4ee7bSdrh ** Return the appropriate collating sequence for the iCol-th column of 1356fbc4ee7bSdrh ** the result set for the compound-select statement "p". Return NULL if 1357fbc4ee7bSdrh ** the column has no default collating sequence. 1358fbc4ee7bSdrh ** 1359fbc4ee7bSdrh ** The collating sequence for the compound select is taken from the 1360fbc4ee7bSdrh ** left-most term of the select that has a collating sequence. 1361fbc4ee7bSdrh */ 1362dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ 1363fbc4ee7bSdrh CollSeq *pRet; 1364dc1bdc4fSdanielk1977 if( p->pPrior ){ 1365dc1bdc4fSdanielk1977 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); 1366fbc4ee7bSdrh }else{ 1367fbc4ee7bSdrh pRet = 0; 1368dc1bdc4fSdanielk1977 } 1369fbc4ee7bSdrh if( pRet==0 ){ 1370dc1bdc4fSdanielk1977 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); 1371dc1bdc4fSdanielk1977 } 1372dc1bdc4fSdanielk1977 return pRet; 1373d3d39e93Sdrh } 1374b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1375d3d39e93Sdrh 1376b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1377d3d39e93Sdrh /* 137882c3d636Sdrh ** This routine is called to process a query that is really the union 137982c3d636Sdrh ** or intersection of two or more separate queries. 1380c926afbcSdrh ** 1381e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1382e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1383e78e8284Sdrh ** in which case this routine will be called recursively. 1384e78e8284Sdrh ** 1385e78e8284Sdrh ** The results of the total query are to be written into a destination 1386e78e8284Sdrh ** of type eDest with parameter iParm. 1387e78e8284Sdrh ** 1388e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1389e78e8284Sdrh ** 1390e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1391e78e8284Sdrh ** 1392e78e8284Sdrh ** This statement is parsed up as follows: 1393e78e8284Sdrh ** 1394e78e8284Sdrh ** SELECT c FROM t3 1395e78e8284Sdrh ** | 1396e78e8284Sdrh ** `-----> SELECT b FROM t2 1397e78e8284Sdrh ** | 13984b11c6d3Sjplyon ** `------> SELECT a FROM t1 1399e78e8284Sdrh ** 1400e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1401e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1402e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1403e78e8284Sdrh ** 1404e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1405e78e8284Sdrh ** individual selects always group from left to right. 140682c3d636Sdrh */ 140784ac9d02Sdanielk1977 static int multiSelect( 1408fbc4ee7bSdrh Parse *pParse, /* Parsing context */ 1409fbc4ee7bSdrh Select *p, /* The right-most of SELECTs to be coded */ 1410fbc4ee7bSdrh int eDest, /* \___ Store query results as specified */ 1411fbc4ee7bSdrh int iParm, /* / by these two parameters. */ 141284ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 141384ac9d02Sdanielk1977 ){ 141484ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 141510e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 141610e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 14178cdbf836Sdrh int nCol; /* Number of columns in the result set */ 14180342b1f5Sdrh ExprList *pOrderBy; /* The ORDER BY clause on p */ 14190342b1f5Sdrh int aSetP2[2]; /* Set P2 value of these op to number of columns */ 14200342b1f5Sdrh int nSetP2 = 0; /* Number of slots in aSetP2[] used */ 142182c3d636Sdrh 14227b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 1423fbc4ee7bSdrh ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. 142482c3d636Sdrh */ 142584ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 142684ac9d02Sdanielk1977 rc = 1; 142784ac9d02Sdanielk1977 goto multi_select_end; 142884ac9d02Sdanielk1977 } 1429d8bc7086Sdrh pPrior = p->pPrior; 14300342b1f5Sdrh assert( pPrior->pRightmost!=pPrior ); 14310342b1f5Sdrh assert( pPrior->pRightmost==p->pRightmost ); 1432d8bc7086Sdrh if( pPrior->pOrderBy ){ 14334adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1434da93d238Sdrh selectOpName(p->op)); 143584ac9d02Sdanielk1977 rc = 1; 143684ac9d02Sdanielk1977 goto multi_select_end; 143782c3d636Sdrh } 1438a2dc3b1aSdanielk1977 if( pPrior->pLimit ){ 14394adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 14407b58daeaSdrh selectOpName(p->op)); 144184ac9d02Sdanielk1977 rc = 1; 144284ac9d02Sdanielk1977 goto multi_select_end; 14437b58daeaSdrh } 144482c3d636Sdrh 1445d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1446d8bc7086Sdrh */ 14474adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 144884ac9d02Sdanielk1977 if( v==0 ){ 144984ac9d02Sdanielk1977 rc = 1; 145084ac9d02Sdanielk1977 goto multi_select_end; 145184ac9d02Sdanielk1977 } 1452d8bc7086Sdrh 14531cc3d75fSdrh /* Create the destination temporary table if necessary 14541cc3d75fSdrh */ 14551cc3d75fSdrh if( eDest==SRT_TempTable ){ 1456b4964b72Sdanielk1977 assert( p->pEList ); 14570342b1f5Sdrh assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); 14580342b1f5Sdrh aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenVirtual, iParm, 0); 14591cc3d75fSdrh eDest = SRT_Table; 14601cc3d75fSdrh } 14611cc3d75fSdrh 1462f46f905aSdrh /* Generate code for the left and right SELECT statements. 1463d8bc7086Sdrh */ 14640342b1f5Sdrh pOrderBy = p->pOrderBy; 146582c3d636Sdrh switch( p->op ){ 1466f46f905aSdrh case TK_ALL: { 14670342b1f5Sdrh if( pOrderBy==0 ){ 1468a2dc3b1aSdanielk1977 assert( !pPrior->pLimit ); 1469a2dc3b1aSdanielk1977 pPrior->pLimit = p->pLimit; 1470a2dc3b1aSdanielk1977 pPrior->pOffset = p->pOffset; 1471b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 147284ac9d02Sdanielk1977 if( rc ){ 147384ac9d02Sdanielk1977 goto multi_select_end; 147484ac9d02Sdanielk1977 } 1475f46f905aSdrh p->pPrior = 0; 14767b58daeaSdrh p->iLimit = pPrior->iLimit; 14777b58daeaSdrh p->iOffset = pPrior->iOffset; 1478a2dc3b1aSdanielk1977 p->pLimit = 0; 1479a2dc3b1aSdanielk1977 p->pOffset = 0; 1480b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1481f46f905aSdrh p->pPrior = pPrior; 148284ac9d02Sdanielk1977 if( rc ){ 148384ac9d02Sdanielk1977 goto multi_select_end; 148484ac9d02Sdanielk1977 } 1485f46f905aSdrh break; 1486f46f905aSdrh } 1487f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1488f46f905aSdrh } 148982c3d636Sdrh case TK_EXCEPT: 149082c3d636Sdrh case TK_UNION: { 1491d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1492742f947bSdanielk1977 int op = 0; /* One of the SRT_ operations to apply to self */ 1493d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1494a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ 1495dc1bdc4fSdanielk1977 int addr; 149682c3d636Sdrh 1497d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 14980342b1f5Sdrh if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){ 1499d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1500c926afbcSdrh ** right. 1501d8bc7086Sdrh */ 150282c3d636Sdrh unionTab = iParm; 150382c3d636Sdrh }else{ 1504d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1505d8bc7086Sdrh ** intermediate results. 1506d8bc7086Sdrh */ 150782c3d636Sdrh unionTab = pParse->nTab++; 15080342b1f5Sdrh if( pOrderBy && matchOrderbyToColumn(pParse, p, pOrderBy, unionTab,1) ){ 150984ac9d02Sdanielk1977 rc = 1; 151084ac9d02Sdanielk1977 goto multi_select_end; 1511d8bc7086Sdrh } 15129170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, unionTab, 0); 15130342b1f5Sdrh if( priorOp==SRT_Table ){ 15140342b1f5Sdrh assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); 15150342b1f5Sdrh aSetP2[nSetP2++] = addr; 15160342b1f5Sdrh }else{ 15170342b1f5Sdrh assert( p->addrOpenVirt[0] == -1 ); 15180342b1f5Sdrh p->addrOpenVirt[0] = addr; 15190342b1f5Sdrh p->pRightmost->usesVirt = 1; 1520dc1bdc4fSdanielk1977 } 15210342b1f5Sdrh createSortingIndex(pParse, p, pOrderBy); 152284ac9d02Sdanielk1977 assert( p->pEList ); 1523d8bc7086Sdrh } 1524d8bc7086Sdrh 1525d8bc7086Sdrh /* Code the SELECT statements to our left 1526d8bc7086Sdrh */ 1527b3bce662Sdanielk1977 assert( !pPrior->pOrderBy ); 1528b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 152984ac9d02Sdanielk1977 if( rc ){ 153084ac9d02Sdanielk1977 goto multi_select_end; 153184ac9d02Sdanielk1977 } 1532d8bc7086Sdrh 1533d8bc7086Sdrh /* Code the current SELECT statement 1534d8bc7086Sdrh */ 1535d8bc7086Sdrh switch( p->op ){ 1536d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1537d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1538d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1539d8bc7086Sdrh } 154082c3d636Sdrh p->pPrior = 0; 1541c926afbcSdrh p->pOrderBy = 0; 1542a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1543a2dc3b1aSdanielk1977 p->pLimit = 0; 1544a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1545a2dc3b1aSdanielk1977 p->pOffset = 0; 1546b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 154782c3d636Sdrh p->pPrior = pPrior; 1548c926afbcSdrh p->pOrderBy = pOrderBy; 1549a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1550a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1551a2dc3b1aSdanielk1977 p->pOffset = pOffset; 1552be5fd490Sdrh p->iLimit = -1; 1553be5fd490Sdrh p->iOffset = -1; 155484ac9d02Sdanielk1977 if( rc ){ 155584ac9d02Sdanielk1977 goto multi_select_end; 155684ac9d02Sdanielk1977 } 155784ac9d02Sdanielk1977 1558d8bc7086Sdrh 1559d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1560d8bc7086Sdrh ** it is that we currently need. 1561d8bc7086Sdrh */ 1562c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 15636b56344dSdrh int iCont, iBreak, iStart; 156482c3d636Sdrh assert( p->pEList ); 156541202ccaSdrh if( eDest==SRT_Callback ){ 15666a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 156741202ccaSdrh } 15684adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 15694adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 15704adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 15717b58daeaSdrh computeLimitRegisters(pParse, p); 15724adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 157338640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 15740342b1f5Sdrh pOrderBy, -1, eDest, iParm, 157584ac9d02Sdanielk1977 iCont, iBreak, 0); 157684ac9d02Sdanielk1977 if( rc ){ 157784ac9d02Sdanielk1977 rc = 1; 157884ac9d02Sdanielk1977 goto multi_select_end; 157984ac9d02Sdanielk1977 } 15804adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 15814adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 15824adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 15834adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 158482c3d636Sdrh } 158582c3d636Sdrh break; 158682c3d636Sdrh } 158782c3d636Sdrh case TK_INTERSECT: { 158882c3d636Sdrh int tab1, tab2; 15896b56344dSdrh int iCont, iBreak, iStart; 1590a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; 1591dc1bdc4fSdanielk1977 int addr; 159282c3d636Sdrh 1593d8bc7086Sdrh /* INTERSECT is different from the others since it requires 15946206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1595d8bc7086Sdrh ** by allocating the tables we will need. 1596d8bc7086Sdrh */ 159782c3d636Sdrh tab1 = pParse->nTab++; 159882c3d636Sdrh tab2 = pParse->nTab++; 15990342b1f5Sdrh if( pOrderBy && matchOrderbyToColumn(pParse,p,pOrderBy,tab1,1) ){ 160084ac9d02Sdanielk1977 rc = 1; 160184ac9d02Sdanielk1977 goto multi_select_end; 1602d8bc7086Sdrh } 16030342b1f5Sdrh createSortingIndex(pParse, p, pOrderBy); 1604dc1bdc4fSdanielk1977 16059170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, tab1, 0); 16060342b1f5Sdrh assert( p->addrOpenVirt[0] == -1 ); 16070342b1f5Sdrh p->addrOpenVirt[0] = addr; 16080342b1f5Sdrh p->pRightmost->usesVirt = 1; 160984ac9d02Sdanielk1977 assert( p->pEList ); 1610d8bc7086Sdrh 1611d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1612d8bc7086Sdrh */ 1613b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 161484ac9d02Sdanielk1977 if( rc ){ 161584ac9d02Sdanielk1977 goto multi_select_end; 161684ac9d02Sdanielk1977 } 1617d8bc7086Sdrh 1618d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1619d8bc7086Sdrh */ 16209170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, tab2, 0); 16210342b1f5Sdrh assert( p->addrOpenVirt[1] == -1 ); 16220342b1f5Sdrh p->addrOpenVirt[1] = addr; 162382c3d636Sdrh p->pPrior = 0; 1624a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1625a2dc3b1aSdanielk1977 p->pLimit = 0; 1626a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1627a2dc3b1aSdanielk1977 p->pOffset = 0; 1628b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 162982c3d636Sdrh p->pPrior = pPrior; 1630a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1631a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1632a2dc3b1aSdanielk1977 p->pOffset = pOffset; 163384ac9d02Sdanielk1977 if( rc ){ 163484ac9d02Sdanielk1977 goto multi_select_end; 163584ac9d02Sdanielk1977 } 1636d8bc7086Sdrh 1637d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1638d8bc7086Sdrh ** tables. 1639d8bc7086Sdrh */ 164082c3d636Sdrh assert( p->pEList ); 164141202ccaSdrh if( eDest==SRT_Callback ){ 16426a3ea0e6Sdrh generateColumnNames(pParse, 0, p->pEList); 164341202ccaSdrh } 16444adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 16454adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 16464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 16477b58daeaSdrh computeLimitRegisters(pParse, p); 16484a9f241cSdrh iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0); 16494adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 165038640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 16510342b1f5Sdrh pOrderBy, -1, eDest, iParm, 165284ac9d02Sdanielk1977 iCont, iBreak, 0); 165384ac9d02Sdanielk1977 if( rc ){ 165484ac9d02Sdanielk1977 rc = 1; 165584ac9d02Sdanielk1977 goto multi_select_end; 165684ac9d02Sdanielk1977 } 16574adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 16584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 16594adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 16604adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 16614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 166282c3d636Sdrh break; 166382c3d636Sdrh } 166482c3d636Sdrh } 16658cdbf836Sdrh 16668cdbf836Sdrh /* Make sure all SELECTs in the statement have the same number of elements 16678cdbf836Sdrh ** in their result sets. 16688cdbf836Sdrh */ 166982c3d636Sdrh assert( p->pEList && pPrior->pEList ); 167082c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 16714adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1672da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 167384ac9d02Sdanielk1977 rc = 1; 167484ac9d02Sdanielk1977 goto multi_select_end; 16752282792aSdrh } 167684ac9d02Sdanielk1977 16778cdbf836Sdrh /* Set the number of columns in temporary tables 16788cdbf836Sdrh */ 16798cdbf836Sdrh nCol = p->pEList->nExpr; 16800342b1f5Sdrh while( nSetP2 ){ 16810342b1f5Sdrh sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol); 16828cdbf836Sdrh } 16838cdbf836Sdrh 1684fbc4ee7bSdrh /* Compute collating sequences used by either the ORDER BY clause or 1685fbc4ee7bSdrh ** by any temporary tables needed to implement the compound select. 1686fbc4ee7bSdrh ** Attach the KeyInfo structure to all temporary tables. Invoke the 1687fbc4ee7bSdrh ** ORDER BY processing if there is an ORDER BY clause. 16888cdbf836Sdrh ** 16898cdbf836Sdrh ** This section is run by the right-most SELECT statement only. 16908cdbf836Sdrh ** SELECT statements to the left always skip this part. The right-most 16918cdbf836Sdrh ** SELECT might also skip this part if it has no ORDER BY clause and 16928cdbf836Sdrh ** no temp tables are required. 1693fbc4ee7bSdrh */ 16940342b1f5Sdrh if( pOrderBy || p->usesVirt ){ 1695fbc4ee7bSdrh int i; /* Loop counter */ 1696fbc4ee7bSdrh KeyInfo *pKeyInfo; /* Collating sequence for the result set */ 16970342b1f5Sdrh Select *pLoop; /* For looping through SELECT statements */ 16980342b1f5Sdrh CollSeq **apColl; 16990342b1f5Sdrh CollSeq **aCopy; 1700fbc4ee7bSdrh 17010342b1f5Sdrh assert( p->pRightmost==p ); 17024db38a70Sdrh pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*2*sizeof(CollSeq*) + nCol); 1703dc1bdc4fSdanielk1977 if( !pKeyInfo ){ 1704dc1bdc4fSdanielk1977 rc = SQLITE_NOMEM; 1705dc1bdc4fSdanielk1977 goto multi_select_end; 1706dc1bdc4fSdanielk1977 } 1707dc1bdc4fSdanielk1977 1708dc1bdc4fSdanielk1977 pKeyInfo->enc = pParse->db->enc; 1709dc1bdc4fSdanielk1977 pKeyInfo->nField = nCol; 1710dc1bdc4fSdanielk1977 17110342b1f5Sdrh for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){ 17120342b1f5Sdrh *apColl = multiSelectCollSeq(pParse, p, i); 17130342b1f5Sdrh if( 0==*apColl ){ 17140342b1f5Sdrh *apColl = pParse->db->pDfltColl; 1715dc1bdc4fSdanielk1977 } 1716dc1bdc4fSdanielk1977 } 1717dc1bdc4fSdanielk1977 17180342b1f5Sdrh for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ 17190342b1f5Sdrh for(i=0; i<2; i++){ 17200342b1f5Sdrh int addr = pLoop->addrOpenVirt[i]; 17210342b1f5Sdrh if( addr<0 ){ 17220342b1f5Sdrh /* If [0] is unused then [1] is also unused. So we can 17230342b1f5Sdrh ** always safely abort as soon as the first unused slot is found */ 17240342b1f5Sdrh assert( pLoop->addrOpenVirt[1]<0 ); 17250342b1f5Sdrh break; 17260342b1f5Sdrh } 17270342b1f5Sdrh sqlite3VdbeChangeP2(v, addr, nCol); 17280342b1f5Sdrh sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO); 17290342b1f5Sdrh } 1730dc1bdc4fSdanielk1977 } 1731dc1bdc4fSdanielk1977 17320342b1f5Sdrh if( pOrderBy ){ 17330342b1f5Sdrh struct ExprList_item *pOTerm = pOrderBy->a; 17340342b1f5Sdrh int nExpr = pOrderBy->nExpr; 17350342b1f5Sdrh int addr; 17364db38a70Sdrh u8 *pSortOrder; 17370342b1f5Sdrh 17380342b1f5Sdrh aCopy = (CollSeq**)&pKeyInfo[1]; 17394db38a70Sdrh pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nExpr]; 17400342b1f5Sdrh memcpy(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*)); 17410342b1f5Sdrh apColl = pKeyInfo->aColl; 17424db38a70Sdrh for(i=0; i<pOrderBy->nExpr; i++, pOTerm++, apColl++, pSortOrder++){ 17430342b1f5Sdrh Expr *pExpr = pOTerm->pExpr; 17440342b1f5Sdrh char *zName = pOTerm->zName; 1745dc1bdc4fSdanielk1977 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); 1746dc1bdc4fSdanielk1977 if( zName ){ 17470342b1f5Sdrh *apColl = sqlite3LocateCollSeq(pParse, zName, -1); 174884ac9d02Sdanielk1977 }else{ 17490342b1f5Sdrh *apColl = aCopy[pExpr->iColumn]; 175084ac9d02Sdanielk1977 } 17514db38a70Sdrh *pSortOrder = pOTerm->sortOrder; 175284ac9d02Sdanielk1977 } 17530342b1f5Sdrh assert( p->pRightmost==p ); 17540342b1f5Sdrh assert( p->addrOpenVirt[2]>=0 ); 17550342b1f5Sdrh addr = p->addrOpenVirt[2]; 17564db38a70Sdrh sqlite3VdbeChangeP2(v, addr, p->pEList->nExpr+2); 17574db38a70Sdrh sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 17584db38a70Sdrh pKeyInfo = 0; 1759dc1bdc4fSdanielk1977 generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1760dc1bdc4fSdanielk1977 } 1761dc1bdc4fSdanielk1977 1762dc1bdc4fSdanielk1977 sqliteFree(pKeyInfo); 1763dc1bdc4fSdanielk1977 } 1764dc1bdc4fSdanielk1977 1765dc1bdc4fSdanielk1977 multi_select_end: 176684ac9d02Sdanielk1977 return rc; 17672282792aSdrh } 1768b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 17692282792aSdrh 1770b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 17712282792aSdrh /* 1772832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 17736a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 177484e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 17756a3ea0e6Sdrh ** unchanged.) 1776832508b7Sdrh ** 1777832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1778832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1779832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1780832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1781832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1782832508b7Sdrh ** of the subquery rather the result set of the subquery. 1783832508b7Sdrh */ 17846a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 1785b3bce662Sdanielk1977 static void substSelect(Select *, int, ExprList *); /* Forward Decl */ 17866a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1787832508b7Sdrh if( pExpr==0 ) return; 178850350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 178950350a15Sdrh if( pExpr->iColumn<0 ){ 179050350a15Sdrh pExpr->op = TK_NULL; 179150350a15Sdrh }else{ 1792832508b7Sdrh Expr *pNew; 179384e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 1794832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 1795832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 1796832508b7Sdrh assert( pNew!=0 ); 1797832508b7Sdrh pExpr->op = pNew->op; 1798d94a6698Sdrh assert( pExpr->pLeft==0 ); 17994adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 1800d94a6698Sdrh assert( pExpr->pRight==0 ); 18014adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 1802d94a6698Sdrh assert( pExpr->pList==0 ); 18034adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 1804832508b7Sdrh pExpr->iTable = pNew->iTable; 1805832508b7Sdrh pExpr->iColumn = pNew->iColumn; 1806832508b7Sdrh pExpr->iAgg = pNew->iAgg; 18074adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 18084adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 1809a1cb183dSdanielk1977 pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); 1810a1cb183dSdanielk1977 pExpr->flags = pNew->flags; 181150350a15Sdrh } 1812832508b7Sdrh }else{ 18136a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 18146a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 1815b3bce662Sdanielk1977 substSelect(pExpr->pSelect, iTable, pEList); 18166a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 1817832508b7Sdrh } 1818832508b7Sdrh } 1819b3bce662Sdanielk1977 static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ 1820832508b7Sdrh int i; 1821832508b7Sdrh if( pList==0 ) return; 1822832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 18236a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 1824832508b7Sdrh } 1825832508b7Sdrh } 1826b3bce662Sdanielk1977 static void substSelect(Select *p, int iTable, ExprList *pEList){ 1827b3bce662Sdanielk1977 if( !p ) return; 1828b3bce662Sdanielk1977 substExprList(p->pEList, iTable, pEList); 1829b3bce662Sdanielk1977 substExprList(p->pGroupBy, iTable, pEList); 1830b3bce662Sdanielk1977 substExprList(p->pOrderBy, iTable, pEList); 1831b3bce662Sdanielk1977 substExpr(p->pHaving, iTable, pEList); 1832b3bce662Sdanielk1977 substExpr(p->pWhere, iTable, pEList); 1833b3bce662Sdanielk1977 } 1834b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_VIEW) */ 1835832508b7Sdrh 1836b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 1837832508b7Sdrh /* 18381350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 18391350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 18401350b030Sdrh ** occurs. 18411350b030Sdrh ** 18421350b030Sdrh ** To understand the concept of flattening, consider the following 18431350b030Sdrh ** query: 18441350b030Sdrh ** 18451350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 18461350b030Sdrh ** 18471350b030Sdrh ** The default way of implementing this query is to execute the 18481350b030Sdrh ** subquery first and store the results in a temporary table, then 18491350b030Sdrh ** run the outer query on that temporary table. This requires two 18501350b030Sdrh ** passes over the data. Furthermore, because the temporary table 18511350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 1852832508b7Sdrh ** optimized. 18531350b030Sdrh ** 1854832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 18551350b030Sdrh ** a single flat select, like this: 18561350b030Sdrh ** 18571350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 18581350b030Sdrh ** 18591350b030Sdrh ** The code generated for this simpification gives the same result 1860832508b7Sdrh ** but only has to scan the data once. And because indices might 1861832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 1862832508b7Sdrh ** avoided. 18631350b030Sdrh ** 1864832508b7Sdrh ** Flattening is only attempted if all of the following are true: 18651350b030Sdrh ** 1866832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 18671350b030Sdrh ** 1868832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 1869832508b7Sdrh ** 18708af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 18718af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 1872832508b7Sdrh ** 1873832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 1874832508b7Sdrh ** 1875832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 1876832508b7Sdrh ** aggregates. 1877832508b7Sdrh ** 1878832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 1879832508b7Sdrh ** DISTINCT. 1880832508b7Sdrh ** 188108192d5fSdrh ** (7) The subquery has a FROM clause. 188208192d5fSdrh ** 1883df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 1884df199a25Sdrh ** 1885df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 1886df199a25Sdrh ** aggregates. 1887df199a25Sdrh ** 1888df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 1889df199a25Sdrh ** use LIMIT. 1890df199a25Sdrh ** 1891174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 1892174b6195Sdrh ** 18933fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 18943fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 18953fc673e6Sdrh ** 1896832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 1897832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 1898832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 1899832508b7Sdrh ** 1900665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 1901832508b7Sdrh ** If flattening is attempted this routine returns 1. 1902832508b7Sdrh ** 1903832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 1904832508b7Sdrh ** the subquery before this routine runs. 19051350b030Sdrh */ 19068c74a8caSdrh static int flattenSubquery( 19078c74a8caSdrh Parse *pParse, /* The parsing context */ 19088c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 19098c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 19108c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 19118c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 19128c74a8caSdrh ){ 19130bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 1914ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 1915ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 19160bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 19176a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 191891bb0eedSdrh int i; /* Loop counter */ 191991bb0eedSdrh Expr *pWhere; /* The WHERE clause */ 192091bb0eedSdrh struct SrcList_item *pSubitem; /* The subquery */ 19211350b030Sdrh 1922832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 1923832508b7Sdrh */ 1924832508b7Sdrh if( p==0 ) return 0; 1925832508b7Sdrh pSrc = p->pSrc; 1926ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 192791bb0eedSdrh pSubitem = &pSrc->a[iFrom]; 192891bb0eedSdrh pSub = pSubitem->pSelect; 1929832508b7Sdrh assert( pSub!=0 ); 1930832508b7Sdrh if( isAgg && subqueryIsAgg ) return 0; 1931ad3cab52Sdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; 1932832508b7Sdrh pSubSrc = pSub->pSrc; 1933832508b7Sdrh assert( pSubSrc ); 1934a2dc3b1aSdanielk1977 if( (pSub->pLimit && p->pLimit) || pSub->pOffset || 1935a2dc3b1aSdanielk1977 (pSub->pLimit && isAgg) ) return 0; 1936c31c2eb8Sdrh if( pSubSrc->nSrc==0 ) return 0; 1937a2dc3b1aSdanielk1977 if( pSub->isDistinct && (pSrc->nSrc>1 || isAgg) ){ 1938df199a25Sdrh return 0; 1939df199a25Sdrh } 1940a2dc3b1aSdanielk1977 if( p->isDistinct && subqueryIsAgg ) return 0; 1941174b6195Sdrh if( p->pOrderBy && pSub->pOrderBy ) return 0; 1942832508b7Sdrh 19438af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 19448af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 19458af4d3acSdrh ** is not allowed: 19468af4d3acSdrh ** 19478af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 19488af4d3acSdrh ** 19498af4d3acSdrh ** If we flatten the above, we would get 19508af4d3acSdrh ** 19518af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 19528af4d3acSdrh ** 19538af4d3acSdrh ** which is not at all the same thing. 19548af4d3acSdrh */ 19558af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 19568af4d3acSdrh return 0; 19578af4d3acSdrh } 19588af4d3acSdrh 19593fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 19603fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 19613fc673e6Sdrh ** An examples of why this is not allowed: 19623fc673e6Sdrh ** 19633fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 19643fc673e6Sdrh ** 19653fc673e6Sdrh ** If we flatten the above, we would get 19663fc673e6Sdrh ** 19673fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 19683fc673e6Sdrh ** 19693fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 19703fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 19713fc673e6Sdrh */ 19723fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 19733fc673e6Sdrh && pSub->pWhere!=0 ){ 19743fc673e6Sdrh return 0; 19753fc673e6Sdrh } 19763fc673e6Sdrh 19770bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 197863eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 1979832508b7Sdrh */ 1980c31c2eb8Sdrh 1981c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 1982c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 1983c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 1984c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 1985c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 1986c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 1987c31c2eb8Sdrh ** elements we are now copying in. 1988c31c2eb8Sdrh */ 198991bb0eedSdrh iParent = pSubitem->iCursor; 1990c31c2eb8Sdrh { 1991c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 199291bb0eedSdrh int jointype = pSubitem->jointype; 1993c31c2eb8Sdrh 199491bb0eedSdrh sqlite3DeleteTable(0, pSubitem->pTab); 199591bb0eedSdrh sqliteFree(pSubitem->zDatabase); 199691bb0eedSdrh sqliteFree(pSubitem->zName); 199791bb0eedSdrh sqliteFree(pSubitem->zAlias); 1998c31c2eb8Sdrh if( nSubSrc>1 ){ 1999c31c2eb8Sdrh int extra = nSubSrc - 1; 2000c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 20014adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 2002c31c2eb8Sdrh } 2003c31c2eb8Sdrh p->pSrc = pSrc; 2004c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 2005c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 2006c31c2eb8Sdrh } 2007c31c2eb8Sdrh } 2008c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 2009c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 2010c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 2011c31c2eb8Sdrh } 20128af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 2013c31c2eb8Sdrh } 2014c31c2eb8Sdrh 2015c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 2016c31c2eb8Sdrh ** references to the iParent in the outer query. 2017c31c2eb8Sdrh ** 2018c31c2eb8Sdrh ** Example: 2019c31c2eb8Sdrh ** 2020c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 2021c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 2022c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 2023c31c2eb8Sdrh ** 2024c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 2025c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 2026c31c2eb8Sdrh */ 20276a3ea0e6Sdrh substExprList(p->pEList, iParent, pSub->pEList); 2028832508b7Sdrh pList = p->pEList; 2029832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 20306977fea8Sdrh Expr *pExpr; 20316977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 20326977fea8Sdrh pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); 2033832508b7Sdrh } 2034832508b7Sdrh } 20351b2e0329Sdrh if( isAgg ){ 20366a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 20376a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 20381b2e0329Sdrh } 2039174b6195Sdrh if( pSub->pOrderBy ){ 2040174b6195Sdrh assert( p->pOrderBy==0 ); 2041174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 2042174b6195Sdrh pSub->pOrderBy = 0; 2043174b6195Sdrh }else if( p->pOrderBy ){ 20446a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 2045174b6195Sdrh } 2046832508b7Sdrh if( pSub->pWhere ){ 20474adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 2048832508b7Sdrh }else{ 2049832508b7Sdrh pWhere = 0; 2050832508b7Sdrh } 2051832508b7Sdrh if( subqueryIsAgg ){ 2052832508b7Sdrh assert( p->pHaving==0 ); 20531b2e0329Sdrh p->pHaving = p->pWhere; 20541b2e0329Sdrh p->pWhere = pWhere; 20556a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 205691bb0eedSdrh p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); 20571b2e0329Sdrh assert( p->pGroupBy==0 ); 20584adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 2059832508b7Sdrh }else{ 20606a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 206191bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); 2062832508b7Sdrh } 2063c31c2eb8Sdrh 2064c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 2065c31c2eb8Sdrh ** outer query is distinct. 2066c31c2eb8Sdrh */ 2067832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 20688c74a8caSdrh 2069a58fdfb1Sdanielk1977 /* 2070a58fdfb1Sdanielk1977 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; 2071a58fdfb1Sdanielk1977 */ 2072a2dc3b1aSdanielk1977 if( pSub->pLimit ){ 2073a2dc3b1aSdanielk1977 p->pLimit = pSub->pLimit; 2074a2dc3b1aSdanielk1977 pSub->pLimit = 0; 2075df199a25Sdrh } 20768c74a8caSdrh 2077c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 2078c31c2eb8Sdrh ** success. 2079c31c2eb8Sdrh */ 20804adee20fSdanielk1977 sqlite3SelectDelete(pSub); 2081832508b7Sdrh return 1; 20821350b030Sdrh } 2083b7f9164eSdrh #endif /* SQLITE_OMIT_VIEW */ 20841350b030Sdrh 20851350b030Sdrh /* 20869562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 20879562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 20889562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 2089e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 20909562b551Sdrh ** simple min() or max() query, then return 0; 20919562b551Sdrh ** 20929562b551Sdrh ** A simply min() or max() query looks like this: 20939562b551Sdrh ** 20949562b551Sdrh ** SELECT min(a) FROM table; 20959562b551Sdrh ** SELECT max(a) FROM table; 20969562b551Sdrh ** 20979562b551Sdrh ** The query may have only a single table in its FROM argument. There 20989562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 20999562b551Sdrh ** be the min() or max() of a single column of the table. The column 21009562b551Sdrh ** in the min() or max() function must be indexed. 21019562b551Sdrh ** 21024adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 21039562b551Sdrh ** See the header comment on that routine for additional information. 21049562b551Sdrh */ 21059562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 21069562b551Sdrh Expr *pExpr; 21079562b551Sdrh int iCol; 21089562b551Sdrh Table *pTab; 21099562b551Sdrh Index *pIdx; 21109562b551Sdrh int base; 21119562b551Sdrh Vdbe *v; 21129562b551Sdrh int seekOp; 21139562b551Sdrh int cont; 21146e17529eSdrh ExprList *pEList, *pList, eList; 21159562b551Sdrh struct ExprList_item eListItem; 21166e17529eSdrh SrcList *pSrc; 21176e17529eSdrh 21189562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 21199562b551Sdrh ** zero if it is not. 21209562b551Sdrh */ 21219562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 21226e17529eSdrh pSrc = p->pSrc; 21236e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 21246e17529eSdrh pEList = p->pEList; 21256e17529eSdrh if( pEList->nExpr!=1 ) return 0; 21266e17529eSdrh pExpr = pEList->a[0].pExpr; 21279562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 21286e17529eSdrh pList = pExpr->pList; 21296e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 21306977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 21314adee20fSdanielk1977 if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){ 21320bce8354Sdrh seekOp = OP_Rewind; 21334adee20fSdanielk1977 }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){ 21340bce8354Sdrh seekOp = OP_Last; 21350bce8354Sdrh }else{ 21360bce8354Sdrh return 0; 21370bce8354Sdrh } 21386e17529eSdrh pExpr = pList->a[0].pExpr; 21399562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 21409562b551Sdrh iCol = pExpr->iColumn; 21416e17529eSdrh pTab = pSrc->a[0].pTab; 21429562b551Sdrh 21439562b551Sdrh /* If we get to here, it means the query is of the correct form. 214417f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 214517f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 214617f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 214717f71934Sdrh ** usable index is found, return 0. 21489562b551Sdrh */ 21499562b551Sdrh if( iCol<0 ){ 21509562b551Sdrh pIdx = 0; 21519562b551Sdrh }else{ 2152dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 21539562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 21549562b551Sdrh assert( pIdx->nColumn>=1 ); 2155dc1bdc4fSdanielk1977 if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break; 21569562b551Sdrh } 21579562b551Sdrh if( pIdx==0 ) return 0; 21589562b551Sdrh } 21599562b551Sdrh 2160e5f50722Sdrh /* Identify column types if we will be using the callback. This 21619562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 2162e5f50722Sdrh ** The column names have already been generated in the calling function. 21639562b551Sdrh */ 21644adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 21659562b551Sdrh if( v==0 ) return 0; 21669562b551Sdrh 21670c37e630Sdrh /* If the output is destined for a temporary table, open that table. 21680c37e630Sdrh */ 21690c37e630Sdrh if( eDest==SRT_TempTable ){ 2170*d81bd4e2Sdrh sqlite3VdbeAddOp(v, OP_OpenVirtual, iParm, 1); 21710c37e630Sdrh } 21720c37e630Sdrh 217317f71934Sdrh /* Generating code to find the min or the max. Basically all we have 217417f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 217517f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 217617f71934Sdrh ** or last entry in the main table. 21779562b551Sdrh */ 21784adee20fSdanielk1977 sqlite3CodeVerifySchema(pParse, pTab->iDb); 21796e17529eSdrh base = pSrc->a[0].iCursor; 21807b58daeaSdrh computeLimitRegisters(pParse, p); 21816e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 2182ad6d9460Sdrh sqlite3OpenTableForReading(v, base, pTab); 21836e17529eSdrh } 21844adee20fSdanielk1977 cont = sqlite3VdbeMakeLabel(v); 21859562b551Sdrh if( pIdx==0 ){ 21864adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 21879562b551Sdrh }else{ 21883719d7f9Sdanielk1977 /* Even though the cursor used to open the index here is closed 21893719d7f9Sdanielk1977 ** as soon as a single value has been read from it, allocate it 21903719d7f9Sdanielk1977 ** using (pParse->nTab++) to prevent the cursor id from being 21913719d7f9Sdanielk1977 ** reused. This is important for statements of the form 21923719d7f9Sdanielk1977 ** "INSERT INTO x SELECT max() FROM x". 21933719d7f9Sdanielk1977 */ 21943719d7f9Sdanielk1977 int iIdx; 21953719d7f9Sdanielk1977 iIdx = pParse->nTab++; 21964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 21973719d7f9Sdanielk1977 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 2198d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 21999eb516c0Sdrh if( seekOp==OP_Rewind ){ 2200f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 22011af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); 22021af3fdb4Sdrh seekOp = OP_MoveGt; 22039eb516c0Sdrh } 22043719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, seekOp, iIdx, 0); 2205f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0); 22063719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); 22077cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 22089562b551Sdrh } 22095cf8e8c7Sdrh eList.nExpr = 1; 22105cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 22115cf8e8c7Sdrh eList.a = &eListItem; 22125cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 221384ac9d02Sdanielk1977 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0); 22144adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, cont); 22154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 22166e17529eSdrh 22179562b551Sdrh return 1; 22189562b551Sdrh } 22199562b551Sdrh 22209562b551Sdrh /* 2221290c1948Sdrh ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return 2222290c1948Sdrh ** the number of errors seen. 2223290c1948Sdrh ** 2224290c1948Sdrh ** An ORDER BY or GROUP BY is a list of expressions. If any expression 2225290c1948Sdrh ** is an integer constant, then that expression is replaced by the 2226290c1948Sdrh ** corresponding entry in the result set. 2227290c1948Sdrh */ 2228290c1948Sdrh static int processOrderGroupBy( 2229b3bce662Sdanielk1977 NameContext *pNC, /* Name context of the SELECT statement. */ 2230290c1948Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 2231290c1948Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 2232290c1948Sdrh ){ 2233290c1948Sdrh int i; 2234b3bce662Sdanielk1977 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */ 2235b3bce662Sdanielk1977 Parse *pParse = pNC->pParse; /* The result set of the SELECT */ 2236b3bce662Sdanielk1977 assert( pEList ); 2237b3bce662Sdanielk1977 2238290c1948Sdrh if( pOrderBy==0 ) return 0; 2239290c1948Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2240290c1948Sdrh int iCol; 2241290c1948Sdrh Expr *pE = pOrderBy->a[i].pExpr; 2242b3bce662Sdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 2243b3bce662Sdanielk1977 if( iCol>0 && iCol<=pEList->nExpr ){ 2244290c1948Sdrh sqlite3ExprDelete(pE); 2245290c1948Sdrh pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 2246b3bce662Sdanielk1977 }else{ 2247290c1948Sdrh sqlite3ErrorMsg(pParse, 2248290c1948Sdrh "%s BY column number %d out of range - should be " 2249290c1948Sdrh "between 1 and %d", zType, iCol, pEList->nExpr); 2250290c1948Sdrh return 1; 2251290c1948Sdrh } 2252290c1948Sdrh } 2253b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(pNC, pE) ){ 2254b3bce662Sdanielk1977 return 1; 2255b3bce662Sdanielk1977 } 2256b3bce662Sdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 2257b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, 2258b3bce662Sdanielk1977 "%s BY terms must not be non-integer constants", zType); 2259b3bce662Sdanielk1977 return 1; 2260b3bce662Sdanielk1977 } 2261290c1948Sdrh } 2262290c1948Sdrh return 0; 2263290c1948Sdrh } 2264290c1948Sdrh 2265290c1948Sdrh /* 2266b3bce662Sdanielk1977 ** This routine resolves any names used in the result set of the 2267b3bce662Sdanielk1977 ** supplied SELECT statement. If the SELECT statement being resolved 2268b3bce662Sdanielk1977 ** is a sub-select, then pOuterNC is a pointer to the NameContext 2269b3bce662Sdanielk1977 ** of the parent SELECT. 2270b3bce662Sdanielk1977 */ 2271b3bce662Sdanielk1977 int sqlite3SelectResolve( 2272b3bce662Sdanielk1977 Parse *pParse, /* The parser context */ 2273b3bce662Sdanielk1977 Select *p, /* The SELECT statement being coded. */ 2274b3bce662Sdanielk1977 NameContext *pOuterNC /* The outer name context. May be NULL. */ 2275b3bce662Sdanielk1977 ){ 2276b3bce662Sdanielk1977 ExprList *pEList; /* Result set. */ 2277b3bce662Sdanielk1977 int i; /* For-loop variable used in multiple places */ 2278b3bce662Sdanielk1977 NameContext sNC; /* Local name-context */ 2279b3bce662Sdanielk1977 2280b3bce662Sdanielk1977 /* If this routine has run before, return immediately. */ 2281b3bce662Sdanielk1977 if( p->isResolved ){ 2282b3bce662Sdanielk1977 assert( !pOuterNC ); 2283b3bce662Sdanielk1977 return SQLITE_OK; 2284b3bce662Sdanielk1977 } 2285b3bce662Sdanielk1977 p->isResolved = 1; 2286b3bce662Sdanielk1977 2287b3bce662Sdanielk1977 /* If there have already been errors, do nothing. */ 2288b3bce662Sdanielk1977 if( pParse->nErr>0 ){ 2289b3bce662Sdanielk1977 return SQLITE_ERROR; 2290b3bce662Sdanielk1977 } 2291b3bce662Sdanielk1977 2292b3bce662Sdanielk1977 /* Prepare the select statement. This call will allocate all cursors 2293b3bce662Sdanielk1977 ** required to handle the tables and subqueries in the FROM clause. 2294b3bce662Sdanielk1977 */ 2295b3bce662Sdanielk1977 if( prepSelectStmt(pParse, p) ){ 2296b3bce662Sdanielk1977 return SQLITE_ERROR; 2297b3bce662Sdanielk1977 } 2298b3bce662Sdanielk1977 2299a2dc3b1aSdanielk1977 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 2300a2dc3b1aSdanielk1977 ** are not allowed to refer to any names, so pass an empty NameContext. 2301a2dc3b1aSdanielk1977 */ 2302b3bce662Sdanielk1977 sNC.pParse = pParse; 2303b3bce662Sdanielk1977 sNC.hasAgg = 0; 2304b3bce662Sdanielk1977 sNC.nErr = 0; 2305b3bce662Sdanielk1977 sNC.nRef = 0; 2306b3bce662Sdanielk1977 sNC.pEList = 0; 2307a2dc3b1aSdanielk1977 sNC.allowAgg = 0; 2308a2dc3b1aSdanielk1977 sNC.pSrcList = 0; 2309a2dc3b1aSdanielk1977 sNC.pNext = 0; 2310a2dc3b1aSdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pLimit) || 2311a2dc3b1aSdanielk1977 sqlite3ExprResolveNames(&sNC, p->pOffset) ){ 2312a2dc3b1aSdanielk1977 return SQLITE_ERROR; 2313a2dc3b1aSdanielk1977 } 2314a2dc3b1aSdanielk1977 2315a2dc3b1aSdanielk1977 /* Set up the local name-context to pass to ExprResolveNames() to 2316a2dc3b1aSdanielk1977 ** resolve the expression-list. 2317a2dc3b1aSdanielk1977 */ 2318a2dc3b1aSdanielk1977 sNC.allowAgg = 1; 2319a2dc3b1aSdanielk1977 sNC.pSrcList = p->pSrc; 2320a2dc3b1aSdanielk1977 sNC.pNext = pOuterNC; 2321b3bce662Sdanielk1977 2322b3bce662Sdanielk1977 /* NameContext.nDepth stores the depth of recursion for this query. For 2323b3bce662Sdanielk1977 ** an outer query (e.g. SELECT * FROM sqlite_master) this is 1. For 2324b3bce662Sdanielk1977 ** a subquery it is 2. For a subquery of a subquery, 3. And so on. 2325b3bce662Sdanielk1977 ** Parse.nMaxDepth is the maximum depth for any subquery resolved so 2326b3bce662Sdanielk1977 ** far. This is used to determine the number of aggregate contexts 2327b3bce662Sdanielk1977 ** required at runtime. 2328b3bce662Sdanielk1977 */ 2329b3bce662Sdanielk1977 sNC.nDepth = (pOuterNC?pOuterNC->nDepth+1:1); 2330b3bce662Sdanielk1977 if( sNC.nDepth>pParse->nMaxDepth ){ 2331b3bce662Sdanielk1977 pParse->nMaxDepth = sNC.nDepth; 2332b3bce662Sdanielk1977 } 2333b3bce662Sdanielk1977 2334b3bce662Sdanielk1977 /* Resolve names in the result set. */ 2335b3bce662Sdanielk1977 pEList = p->pEList; 2336b3bce662Sdanielk1977 if( !pEList ) return SQLITE_ERROR; 2337b3bce662Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 2338b3bce662Sdanielk1977 Expr *pX = pEList->a[i].pExpr; 2339b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pX) ){ 2340b3bce662Sdanielk1977 return SQLITE_ERROR; 2341b3bce662Sdanielk1977 } 2342b3bce662Sdanielk1977 } 2343b3bce662Sdanielk1977 2344b3bce662Sdanielk1977 /* If there are no aggregate functions in the result-set, and no GROUP BY 2345b3bce662Sdanielk1977 ** expression, do not allow aggregates in any of the other expressions. 2346b3bce662Sdanielk1977 */ 2347b3bce662Sdanielk1977 assert( !p->isAgg ); 2348b3bce662Sdanielk1977 if( p->pGroupBy || sNC.hasAgg ){ 2349b3bce662Sdanielk1977 p->isAgg = 1; 2350b3bce662Sdanielk1977 }else{ 2351b3bce662Sdanielk1977 sNC.allowAgg = 0; 2352b3bce662Sdanielk1977 } 2353b3bce662Sdanielk1977 2354b3bce662Sdanielk1977 /* If a HAVING clause is present, then there must be a GROUP BY clause. 2355b3bce662Sdanielk1977 */ 2356b3bce662Sdanielk1977 if( p->pHaving && !p->pGroupBy ){ 2357b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2358b3bce662Sdanielk1977 return SQLITE_ERROR; 2359b3bce662Sdanielk1977 } 2360b3bce662Sdanielk1977 2361b3bce662Sdanielk1977 /* Add the expression list to the name-context before parsing the 2362b3bce662Sdanielk1977 ** other expressions in the SELECT statement. This is so that 2363b3bce662Sdanielk1977 ** expressions in the WHERE clause (etc.) can refer to expressions by 2364b3bce662Sdanielk1977 ** aliases in the result set. 2365b3bce662Sdanielk1977 ** 2366b3bce662Sdanielk1977 ** Minor point: If this is the case, then the expression will be 2367b3bce662Sdanielk1977 ** re-evaluated for each reference to it. 2368b3bce662Sdanielk1977 */ 2369b3bce662Sdanielk1977 sNC.pEList = p->pEList; 2370b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pWhere) || 2371b3bce662Sdanielk1977 sqlite3ExprResolveNames(&sNC, p->pHaving) || 2372b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") || 2373b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pGroupBy, "GROUP") 2374b3bce662Sdanielk1977 ){ 2375b3bce662Sdanielk1977 return SQLITE_ERROR; 2376b3bce662Sdanielk1977 } 2377b3bce662Sdanielk1977 2378b3bce662Sdanielk1977 return SQLITE_OK; 2379b3bce662Sdanielk1977 } 2380b3bce662Sdanielk1977 2381b3bce662Sdanielk1977 /* 2382b3bce662Sdanielk1977 ** An instance of the following struct is used by sqlite3Select() 2383b3bce662Sdanielk1977 ** to save aggregate related information from the Parse object 2384b3bce662Sdanielk1977 ** at the start of each call and to restore it at the end. See 2385b3bce662Sdanielk1977 ** saveAggregateInfo() and restoreAggregateInfo(). 2386b3bce662Sdanielk1977 */ 2387b3bce662Sdanielk1977 struct AggregateInfo { 2388b3bce662Sdanielk1977 int nAgg; 2389b3bce662Sdanielk1977 AggExpr *aAgg; 2390b3bce662Sdanielk1977 }; 2391b3bce662Sdanielk1977 typedef struct AggregateInfo AggregateInfo; 2392b3bce662Sdanielk1977 2393b3bce662Sdanielk1977 /* 2394b3bce662Sdanielk1977 ** Copy aggregate related information from the Parse structure 2395b3bce662Sdanielk1977 ** into the AggregateInfo structure. Zero the aggregate related 2396b3bce662Sdanielk1977 ** values in the Parse struct. 2397b3bce662Sdanielk1977 */ 2398b3bce662Sdanielk1977 static void saveAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2399b3bce662Sdanielk1977 pInfo->aAgg = pParse->aAgg; 2400b3bce662Sdanielk1977 pInfo->nAgg = pParse->nAgg; 2401b3bce662Sdanielk1977 pParse->aAgg = 0; 2402b3bce662Sdanielk1977 pParse->nAgg = 0; 2403b3bce662Sdanielk1977 } 2404b3bce662Sdanielk1977 2405b3bce662Sdanielk1977 /* 2406b3bce662Sdanielk1977 ** Copy aggregate related information from the AggregateInfo struct 2407b3bce662Sdanielk1977 ** back into the Parse structure. The aggregate related information 2408b3bce662Sdanielk1977 ** currently stored in the Parse structure is deleted. 2409b3bce662Sdanielk1977 */ 2410b3bce662Sdanielk1977 static void restoreAggregateInfo(Parse *pParse, AggregateInfo *pInfo){ 2411b3bce662Sdanielk1977 sqliteFree(pParse->aAgg); 2412b3bce662Sdanielk1977 pParse->aAgg = pInfo->aAgg; 2413b3bce662Sdanielk1977 pParse->nAgg = pInfo->nAgg; 2414b3bce662Sdanielk1977 } 2415b3bce662Sdanielk1977 2416b3bce662Sdanielk1977 /* 24179bb61fe7Sdrh ** Generate code for the given SELECT statement. 24189bb61fe7Sdrh ** 2419fef5208cSdrh ** The results are distributed in various ways depending on the 2420fef5208cSdrh ** value of eDest and iParm. 2421fef5208cSdrh ** 2422fef5208cSdrh ** eDest Value Result 2423fef5208cSdrh ** ------------ ------------------------------------------- 2424fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2425fef5208cSdrh ** 2426fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2427fef5208cSdrh ** 2428e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2429fef5208cSdrh ** 243082c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 243182c3d636Sdrh ** 24324b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2433c4a3c779Sdrh ** 2434c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 24359bb61fe7Sdrh ** 2436e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2437e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2438e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2439e78e8284Sdrh ** 24409bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 24419bb61fe7Sdrh ** encountered, then an appropriate error message is left in 24429bb61fe7Sdrh ** pParse->zErrMsg. 24439bb61fe7Sdrh ** 24449bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 24459bb61fe7Sdrh ** calling function needs to do that. 24461b2e0329Sdrh ** 24471b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 24481b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 24491b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 24501b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 24511b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 24521b2e0329Sdrh ** can be changed. 2453e78e8284Sdrh ** 2454e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2455e78e8284Sdrh ** 2456e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2457e78e8284Sdrh ** \ \_______ subquery _______/ / 2458e78e8284Sdrh ** \ / 2459e78e8284Sdrh ** \____________________ outer query ___________________/ 2460e78e8284Sdrh ** 2461e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2462e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2463e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2464e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2465e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2466e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 24679bb61fe7Sdrh */ 24684adee20fSdanielk1977 int sqlite3Select( 2469cce7d176Sdrh Parse *pParse, /* The parser context */ 24709bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2471e78e8284Sdrh int eDest, /* How to dispose of the results */ 2472e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2473832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2474832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 247584ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 2476b3bce662Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2477cce7d176Sdrh ){ 2478d8bc7086Sdrh int i; 2479cce7d176Sdrh WhereInfo *pWInfo; 2480cce7d176Sdrh Vdbe *v; 2481b3bce662Sdanielk1977 int isAgg; /* True for select lists like "count(*)" */ 2482a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2483ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 24849bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 24859bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 24862282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 24872282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 248819a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 248919a775c2Sdrh int distinct; /* Table to use for the distinct set */ 24901d83f052Sdrh int rc = 1; /* Value to return from this function */ 2491b3bce662Sdanielk1977 AggregateInfo sAggInfo; 24929bb61fe7Sdrh 24936f8a503dSdanielk1977 if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1; 24944adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 2495daffd0e5Sdrh 2496b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 249782c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 249882c3d636Sdrh */ 249982c3d636Sdrh if( p->pPrior ){ 25000342b1f5Sdrh if( p->pRightmost==0 ){ 25010342b1f5Sdrh Select *pLoop; 25020342b1f5Sdrh for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ 25030342b1f5Sdrh pLoop->pRightmost = p; 25040342b1f5Sdrh } 25050342b1f5Sdrh } 250684ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 250782c3d636Sdrh } 2508b7f9164eSdrh #endif 250982c3d636Sdrh 2510b3bce662Sdanielk1977 saveAggregateInfo(pParse, &sAggInfo); 2511b3bce662Sdanielk1977 pOrderBy = p->pOrderBy; 2512b3bce662Sdanielk1977 if( eDest==SRT_Union || eDest==SRT_Except || eDest==SRT_Discard ){ 2513b3bce662Sdanielk1977 p->pOrderBy = 0; 2514b3bce662Sdanielk1977 } 2515b3bce662Sdanielk1977 if( sqlite3SelectResolve(pParse, p, 0) ){ 2516b3bce662Sdanielk1977 goto select_end; 2517b3bce662Sdanielk1977 } 2518b3bce662Sdanielk1977 p->pOrderBy = pOrderBy; 2519b3bce662Sdanielk1977 252082c3d636Sdrh /* Make local copies of the parameters for this query. 252182c3d636Sdrh */ 25229bb61fe7Sdrh pTabList = p->pSrc; 25239bb61fe7Sdrh pWhere = p->pWhere; 25242282792aSdrh pGroupBy = p->pGroupBy; 25252282792aSdrh pHaving = p->pHaving; 2526b3bce662Sdanielk1977 isAgg = p->isAgg; 252719a775c2Sdrh isDistinct = p->isDistinct; 2528b3bce662Sdanielk1977 pEList = p->pEList; 2529b3bce662Sdanielk1977 if( pEList==0 ) goto select_end; 25309bb61fe7Sdrh 25319bb61fe7Sdrh /* 25329bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 25339bb61fe7Sdrh ** errors before this routine starts. 25349bb61fe7Sdrh */ 25351d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2536cce7d176Sdrh 25372282792aSdrh /* If writing to memory or generating a set 25382282792aSdrh ** only a single column may be output. 253919a775c2Sdrh */ 254051522cd3Sdrh assert( eDest!=SRT_Exists || pEList->nExpr==1 ); 254193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2542fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 25434adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2544da93d238Sdrh "a SELECT that is part of an expression"); 25451d83f052Sdrh goto select_end; 254619a775c2Sdrh } 254793758c8dSdanielk1977 #endif 254819a775c2Sdrh 2549c926afbcSdrh /* ORDER BY is ignored for some destinations. 25502282792aSdrh */ 2551c926afbcSdrh switch( eDest ){ 2552c926afbcSdrh case SRT_Union: 2553c926afbcSdrh case SRT_Except: 2554c926afbcSdrh case SRT_Discard: 2555acd4c695Sdrh pOrderBy = 0; 2556c926afbcSdrh break; 2557c926afbcSdrh default: 2558c926afbcSdrh break; 25592282792aSdrh } 25602282792aSdrh 2561d820cb1bSdrh /* Begin generating code. 2562d820cb1bSdrh */ 25634adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2564d820cb1bSdrh if( v==0 ) goto select_end; 2565d820cb1bSdrh 2566e78e8284Sdrh /* Identify column names if we will be using them in a callback. This 2567e78e8284Sdrh ** step is skipped if the output is going to some other destination. 25680bb28106Sdrh */ 25690bb28106Sdrh if( eDest==SRT_Callback ){ 25706a3ea0e6Sdrh generateColumnNames(pParse, pTabList, pEList); 25710bb28106Sdrh } 25720bb28106Sdrh 2573d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2574d820cb1bSdrh */ 257551522cd3Sdrh #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) 2576ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 2577742f947bSdanielk1977 const char *zSavedAuthContext = 0; 2578c31c2eb8Sdrh int needRestoreContext; 2579c31c2eb8Sdrh 2580a76b5dfcSdrh if( pTabList->a[i].pSelect==0 ) continue; 25815cf590c1Sdrh if( pTabList->a[i].zName!=0 ){ 25825cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 25835cf590c1Sdrh pParse->zAuthContext = pTabList->a[i].zName; 2584c31c2eb8Sdrh needRestoreContext = 1; 2585c31c2eb8Sdrh }else{ 2586c31c2eb8Sdrh needRestoreContext = 0; 25875cf590c1Sdrh } 25884adee20fSdanielk1977 sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable, 2589b3bce662Sdanielk1977 pTabList->a[i].iCursor, p, i, &isAgg, 0); 2590c31c2eb8Sdrh if( needRestoreContext ){ 25915cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 25925cf590c1Sdrh } 2593832508b7Sdrh pTabList = p->pSrc; 2594832508b7Sdrh pWhere = p->pWhere; 2595c31c2eb8Sdrh if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ 2596832508b7Sdrh pOrderBy = p->pOrderBy; 2597acd4c695Sdrh } 2598832508b7Sdrh pGroupBy = p->pGroupBy; 2599832508b7Sdrh pHaving = p->pHaving; 2600832508b7Sdrh isDistinct = p->isDistinct; 26011b2e0329Sdrh } 260251522cd3Sdrh #endif 26031b2e0329Sdrh 26046e17529eSdrh /* Check for the special case of a min() or max() function by itself 26056e17529eSdrh ** in the result set. 26066e17529eSdrh */ 26076e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 26086e17529eSdrh rc = 0; 26096e17529eSdrh goto select_end; 26106e17529eSdrh } 26116e17529eSdrh 26121b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 26131b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 26141b2e0329Sdrh */ 2615b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 26161b2e0329Sdrh if( pParent && pParentAgg && 26178c74a8caSdrh flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ 26181b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 2619b3bce662Sdanielk1977 goto select_end; 26201b2e0329Sdrh } 2621b7f9164eSdrh #endif 2622832508b7Sdrh 26237cedc8d4Sdanielk1977 /* If there is an ORDER BY clause, resolve any collation sequences 26247cedc8d4Sdanielk1977 ** names that have been explicitly specified. 26257cedc8d4Sdanielk1977 */ 26267cedc8d4Sdanielk1977 if( pOrderBy ){ 26275d9a4af9Sdrh struct ExprList_item *pTerm; 26280342b1f5Sdrh KeyInfo *pKeyInfo; 26290342b1f5Sdrh int addr; 26305d9a4af9Sdrh for(i=0, pTerm=pOrderBy->a; i<pOrderBy->nExpr; i++, pTerm++){ 26315d9a4af9Sdrh if( pTerm->zName ){ 26325d9a4af9Sdrh pTerm->pExpr->pColl = sqlite3LocateCollSeq(pParse, pTerm->zName, -1); 26337cedc8d4Sdanielk1977 } 26347cedc8d4Sdanielk1977 } 26357cedc8d4Sdanielk1977 if( pParse->nErr ){ 26367cedc8d4Sdanielk1977 goto select_end; 26377cedc8d4Sdanielk1977 } 26380342b1f5Sdrh pKeyInfo = keyInfoFromExprList(pParse, pOrderBy); 26390342b1f5Sdrh pOrderBy->iTab = pParse->nTab++; 26404db38a70Sdrh addr = sqlite3VdbeOp3(v, OP_OpenVirtual, pOrderBy->iTab, pOrderBy->nExpr+2, 26410342b1f5Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 26420342b1f5Sdrh p->addrOpenVirt[2] = addr; 26437cedc8d4Sdanielk1977 } 26447cedc8d4Sdanielk1977 26457b58daeaSdrh /* Set the limiter. 26467b58daeaSdrh */ 26477b58daeaSdrh computeLimitRegisters(pParse, p); 26487b58daeaSdrh 26492d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 26502d0794e3Sdrh */ 26512d0794e3Sdrh if( eDest==SRT_TempTable ){ 26520342b1f5Sdrh sqlite3VdbeAddOp(v, OP_OpenVirtual, iParm, pEList->nExpr); 26532d0794e3Sdrh } 26542d0794e3Sdrh 26552282792aSdrh /* Do an analysis of aggregate expressions. 2656efb7251dSdrh */ 2657bb999ef6Sdrh if( isAgg || pGroupBy ){ 2658a58fdfb1Sdanielk1977 NameContext sNC; 2659a58fdfb1Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 2660a58fdfb1Sdanielk1977 sNC.pParse = pParse; 2661a58fdfb1Sdanielk1977 sNC.pSrcList = pTabList; 2662a58fdfb1Sdanielk1977 26630bce8354Sdrh assert( pParse->nAgg==0 ); 2664bb999ef6Sdrh isAgg = 1; 26655d9a4af9Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){ 26661d83f052Sdrh goto select_end; 26672282792aSdrh } 26685d9a4af9Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pGroupBy) ){ 26691d83f052Sdrh goto select_end; 26702282792aSdrh } 2671a58fdfb1Sdanielk1977 if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ 26721d83f052Sdrh goto select_end; 26732282792aSdrh } 26745d9a4af9Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){ 26751d83f052Sdrh goto select_end; 2676191b690eSdrh } 2677191b690eSdrh } 2678efb7251dSdrh 26792282792aSdrh /* Reset the aggregator 2680cce7d176Sdrh */ 2681cce7d176Sdrh if( isAgg ){ 2682e159fdf2Sdanielk1977 int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg); 2683e5095355Sdrh for(i=0; i<pParse->nAgg; i++){ 26840bce8354Sdrh FuncDef *pFunc; 26850bce8354Sdrh if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ 26865c2d9155Sdanielk1977 int nExpr = 0; 26875c2d9155Sdanielk1977 #ifdef SQLITE_SSE 26885c2d9155Sdanielk1977 Expr *pAggExpr = pParse->aAgg[i].pExpr; 26895c2d9155Sdanielk1977 if( pAggExpr && pAggExpr->pList ){ 26905c2d9155Sdanielk1977 nExpr = pAggExpr->pList->nExpr; 26915c2d9155Sdanielk1977 } 26925c2d9155Sdanielk1977 #endif 26935c2d9155Sdanielk1977 sqlite3VdbeOp3(v, OP_AggInit, nExpr, i, (char*)pFunc, P3_FUNCDEF); 2694e5095355Sdrh } 2695e5095355Sdrh } 2696e159fdf2Sdanielk1977 if( pGroupBy ){ 2697dece1a84Sdrh KeyInfo *pKey = keyInfoFromExprList(pParse, pGroupBy); 2698ce2663ccSdanielk1977 if( 0==pKey ){ 2699ce2663ccSdanielk1977 goto select_end; 2700ce2663ccSdanielk1977 } 2701ce2663ccSdanielk1977 sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF); 27021bee3d7bSdrh } 2703cce7d176Sdrh } 2704cce7d176Sdrh 270551522cd3Sdrh /* Initialize the memory cell to NULL for SRT_Mem or 0 for SRT_Exists 270619a775c2Sdrh */ 270751522cd3Sdrh if( eDest==SRT_Mem || eDest==SRT_Exists ){ 2708f0863fe5Sdrh sqlite3VdbeAddOp(v, eDest==SRT_Mem ? OP_Null : OP_Integer, 0, 0); 27094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 271019a775c2Sdrh } 271119a775c2Sdrh 2712dece1a84Sdrh /* Open a virtual index to use for the distinct set. 2713cce7d176Sdrh */ 271419a775c2Sdrh if( isDistinct ){ 27150342b1f5Sdrh KeyInfo *pKeyInfo; 2716832508b7Sdrh distinct = pParse->nTab++; 27170342b1f5Sdrh pKeyInfo = keyInfoFromExprList(pParse, p->pEList); 27180342b1f5Sdrh sqlite3VdbeOp3(v, OP_OpenVirtual, distinct, 0, 27190342b1f5Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 2720832508b7Sdrh }else{ 2721832508b7Sdrh distinct = -1; 2722efb7251dSdrh } 2723832508b7Sdrh 2724832508b7Sdrh /* Begin the database scan 2725832508b7Sdrh */ 2726e6f85e71Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 2727f8db1bc0Sdrh pGroupBy ? 0 : &pOrderBy); 27281d83f052Sdrh if( pWInfo==0 ) goto select_end; 2729cce7d176Sdrh 27302282792aSdrh /* Use the standard inner loop if we are not dealing with 27312282792aSdrh ** aggregates 2732cce7d176Sdrh */ 2733da9d6c45Sdrh if( !isAgg ){ 2734df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 273584ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 27361d83f052Sdrh goto select_end; 2737cce7d176Sdrh } 2738da9d6c45Sdrh } 2739cce7d176Sdrh 2740e3184744Sdrh /* If we are dealing with aggregates, then do the special aggregate 27412282792aSdrh ** processing. 2742efb7251dSdrh */ 27432282792aSdrh else{ 2744268380caSdrh AggExpr *pAgg; 2745a58fdfb1Sdanielk1977 int lbl1 = 0; 2746a58fdfb1Sdanielk1977 pParse->fillAgg = 1; 27472282792aSdrh if( pGroupBy ){ 2748c182d163Sdrh sqlite3ExprCodeExprList(pParse, pGroupBy); 2749ededfd5eSdanielk1977 /* No affinity string is attached to the following OP_MakeRecord 2750d3d39e93Sdrh ** because we do not need to do any coercion of datatypes. */ 2751ededfd5eSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0); 27524adee20fSdanielk1977 lbl1 = sqlite3VdbeMakeLabel(v); 27534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1); 2754a58fdfb1Sdanielk1977 } 2755268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 2756268380caSdrh if( pAgg->isAgg ) continue; 27574adee20fSdanielk1977 sqlite3ExprCode(pParse, pAgg->pExpr); 27584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggSet, 0, i); 27592282792aSdrh } 2760a58fdfb1Sdanielk1977 pParse->fillAgg = 0; 2761a58fdfb1Sdanielk1977 if( lbl1<0 ){ 27624adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, lbl1); 27632282792aSdrh } 2764268380caSdrh for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ 27652282792aSdrh Expr *pE; 2766268380caSdrh int nExpr; 2767268380caSdrh FuncDef *pDef; 2768268380caSdrh if( !pAgg->isAgg ) continue; 2769268380caSdrh assert( pAgg->pFunc!=0 ); 2770268380caSdrh assert( pAgg->pFunc->xStep!=0 ); 2771268380caSdrh pDef = pAgg->pFunc; 2772268380caSdrh pE = pAgg->pExpr; 2773268380caSdrh assert( pE!=0 ); 27742282792aSdrh assert( pE->op==TK_AGG_FUNCTION ); 2775f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pE->pList); 27764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 2777dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2778dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 2779dc1bdc4fSdanielk1977 int j; 2780dc1bdc4fSdanielk1977 for(j=0; !pColl && j<nExpr; j++){ 2781dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr); 2782dc1bdc4fSdanielk1977 } 2783dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 2784dc1bdc4fSdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 2785dc1bdc4fSdanielk1977 } 27861f55c056Sdanielk1977 sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_FUNCDEF); 27872282792aSdrh } 27882282792aSdrh } 27892282792aSdrh 2790cce7d176Sdrh /* End the database scan loop. 2791cce7d176Sdrh */ 27924adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 2793cce7d176Sdrh 27942282792aSdrh /* If we are processing aggregates, we need to set up a second loop 27952282792aSdrh ** over all of the aggregate values and process them. 27962282792aSdrh */ 27972282792aSdrh if( isAgg ){ 27984adee20fSdanielk1977 int endagg = sqlite3VdbeMakeLabel(v); 27992282792aSdrh int startagg; 28004adee20fSdanielk1977 startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg); 28012282792aSdrh if( pHaving ){ 28024adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pHaving, startagg, 1); 28032282792aSdrh } 2804df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 280584ac9d02Sdanielk1977 iParm, startagg, endagg, aff) ){ 28061d83f052Sdrh goto select_end; 28072282792aSdrh } 28084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, startagg); 28094adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endagg); 28104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Noop, 0, 0); 28112282792aSdrh } 28122282792aSdrh 2813cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 2814cce7d176Sdrh ** and send them to the callback one by one. 2815cce7d176Sdrh */ 2816cce7d176Sdrh if( pOrderBy ){ 2817ffbc3088Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 2818cce7d176Sdrh } 28196a535340Sdrh 282093758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2821f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 2822f620b4e2Sdrh ** temporary table. So delete the subquery structure from the parent 2823f620b4e2Sdrh ** to prevent this subquery from being evaluated again and to force the 2824f620b4e2Sdrh ** the use of the temporary table. 2825f620b4e2Sdrh */ 2826f620b4e2Sdrh if( pParent ){ 2827f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 2828f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 28294adee20fSdanielk1977 sqlite3SelectDelete(p); 2830f620b4e2Sdrh pParent->pSrc->a[parentTab].pSelect = 0; 2831f620b4e2Sdrh } 283293758c8dSdanielk1977 #endif 2833f620b4e2Sdrh 28341d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 28351d83f052Sdrh ** to indicate no errors. 28361d83f052Sdrh */ 28371d83f052Sdrh rc = 0; 28381d83f052Sdrh 28391d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 28401d83f052Sdrh ** successful coding of the SELECT. 28411d83f052Sdrh */ 28421d83f052Sdrh select_end: 2843b3bce662Sdanielk1977 restoreAggregateInfo(pParse, &sAggInfo); 28441d83f052Sdrh return rc; 2845cce7d176Sdrh } 2846