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*30bcf5dbSdrh ** $Id: select.c,v 1.311 2006/04/07 13:50:37 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19315555caSdrh 20cce7d176Sdrh /* 21eda639e1Sdrh ** Delete all the content of a Select structure but do not deallocate 22eda639e1Sdrh ** the select structure itself. 23eda639e1Sdrh */ 24f0113000Sdanielk1977 static void clearSelect(Select *p){ 25eda639e1Sdrh sqlite3ExprListDelete(p->pEList); 26eda639e1Sdrh sqlite3SrcListDelete(p->pSrc); 27eda639e1Sdrh sqlite3ExprDelete(p->pWhere); 28eda639e1Sdrh sqlite3ExprListDelete(p->pGroupBy); 29eda639e1Sdrh sqlite3ExprDelete(p->pHaving); 30eda639e1Sdrh sqlite3ExprListDelete(p->pOrderBy); 31eda639e1Sdrh sqlite3SelectDelete(p->pPrior); 32eda639e1Sdrh sqlite3ExprDelete(p->pLimit); 33eda639e1Sdrh sqlite3ExprDelete(p->pOffset); 34eda639e1Sdrh } 35eda639e1Sdrh 36eda639e1Sdrh 37eda639e1Sdrh /* 389bb61fe7Sdrh ** Allocate a new Select structure and return a pointer to that 399bb61fe7Sdrh ** structure. 40cce7d176Sdrh */ 414adee20fSdanielk1977 Select *sqlite3SelectNew( 42daffd0e5Sdrh ExprList *pEList, /* which columns to include in the result */ 43ad3cab52Sdrh SrcList *pSrc, /* the FROM clause -- which tables to scan */ 44daffd0e5Sdrh Expr *pWhere, /* the WHERE clause */ 45daffd0e5Sdrh ExprList *pGroupBy, /* the GROUP BY clause */ 46daffd0e5Sdrh Expr *pHaving, /* the HAVING clause */ 47daffd0e5Sdrh ExprList *pOrderBy, /* the ORDER BY clause */ 489bbca4c1Sdrh int isDistinct, /* true if the DISTINCT keyword is present */ 49a2dc3b1aSdanielk1977 Expr *pLimit, /* LIMIT value. NULL means not used */ 50a2dc3b1aSdanielk1977 Expr *pOffset /* OFFSET value. NULL means no offset */ 519bb61fe7Sdrh ){ 529bb61fe7Sdrh Select *pNew; 53eda639e1Sdrh Select standin; 549bb61fe7Sdrh pNew = sqliteMalloc( sizeof(*pNew) ); 55a2dc3b1aSdanielk1977 assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */ 56daffd0e5Sdrh if( pNew==0 ){ 57eda639e1Sdrh pNew = &standin; 58eda639e1Sdrh memset(pNew, 0, sizeof(*pNew)); 59eda639e1Sdrh } 60b733d037Sdrh if( pEList==0 ){ 614adee20fSdanielk1977 pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0); 62b733d037Sdrh } 639bb61fe7Sdrh pNew->pEList = pEList; 649bb61fe7Sdrh pNew->pSrc = pSrc; 659bb61fe7Sdrh pNew->pWhere = pWhere; 669bb61fe7Sdrh pNew->pGroupBy = pGroupBy; 679bb61fe7Sdrh pNew->pHaving = pHaving; 689bb61fe7Sdrh pNew->pOrderBy = pOrderBy; 699bb61fe7Sdrh pNew->isDistinct = isDistinct; 7082c3d636Sdrh pNew->op = TK_SELECT; 71a2dc3b1aSdanielk1977 pNew->pLimit = pLimit; 72a2dc3b1aSdanielk1977 pNew->pOffset = pOffset; 737b58daeaSdrh pNew->iLimit = -1; 747b58daeaSdrh pNew->iOffset = -1; 750342b1f5Sdrh pNew->addrOpenVirt[0] = -1; 760342b1f5Sdrh pNew->addrOpenVirt[1] = -1; 770342b1f5Sdrh pNew->addrOpenVirt[2] = -1; 78eda639e1Sdrh if( pNew==&standin) { 79eda639e1Sdrh clearSelect(pNew); 80eda639e1Sdrh pNew = 0; 81daffd0e5Sdrh } 829bb61fe7Sdrh return pNew; 839bb61fe7Sdrh } 849bb61fe7Sdrh 859bb61fe7Sdrh /* 86eda639e1Sdrh ** Delete the given Select structure and all of its substructures. 87eda639e1Sdrh */ 88eda639e1Sdrh void sqlite3SelectDelete(Select *p){ 89eda639e1Sdrh if( p ){ 90eda639e1Sdrh clearSelect(p); 91eda639e1Sdrh sqliteFree(p); 92eda639e1Sdrh } 93eda639e1Sdrh } 94eda639e1Sdrh 95eda639e1Sdrh /* 9601f3f253Sdrh ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the 9701f3f253Sdrh ** type of join. Return an integer constant that expresses that type 9801f3f253Sdrh ** in terms of the following bit values: 9901f3f253Sdrh ** 10001f3f253Sdrh ** JT_INNER 1013dec223cSdrh ** JT_CROSS 10201f3f253Sdrh ** JT_OUTER 10301f3f253Sdrh ** JT_NATURAL 10401f3f253Sdrh ** JT_LEFT 10501f3f253Sdrh ** JT_RIGHT 10601f3f253Sdrh ** 10701f3f253Sdrh ** A full outer join is the combination of JT_LEFT and JT_RIGHT. 10801f3f253Sdrh ** 10901f3f253Sdrh ** If an illegal or unsupported join type is seen, then still return 11001f3f253Sdrh ** a join type, but put an error in the pParse structure. 11101f3f253Sdrh */ 1124adee20fSdanielk1977 int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ 11301f3f253Sdrh int jointype = 0; 11401f3f253Sdrh Token *apAll[3]; 11501f3f253Sdrh Token *p; 1165719628aSdrh static const struct { 117c182d163Sdrh const char zKeyword[8]; 118290c1948Sdrh u8 nChar; 119290c1948Sdrh u8 code; 12001f3f253Sdrh } keywords[] = { 12101f3f253Sdrh { "natural", 7, JT_NATURAL }, 122195e6967Sdrh { "left", 4, JT_LEFT|JT_OUTER }, 123195e6967Sdrh { "right", 5, JT_RIGHT|JT_OUTER }, 124195e6967Sdrh { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, 12501f3f253Sdrh { "outer", 5, JT_OUTER }, 12601f3f253Sdrh { "inner", 5, JT_INNER }, 1273dec223cSdrh { "cross", 5, JT_INNER|JT_CROSS }, 12801f3f253Sdrh }; 12901f3f253Sdrh int i, j; 13001f3f253Sdrh apAll[0] = pA; 13101f3f253Sdrh apAll[1] = pB; 13201f3f253Sdrh apAll[2] = pC; 133195e6967Sdrh for(i=0; i<3 && apAll[i]; i++){ 13401f3f253Sdrh p = apAll[i]; 13501f3f253Sdrh for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ 13601f3f253Sdrh if( p->n==keywords[j].nChar 1372646da7eSdrh && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){ 13801f3f253Sdrh jointype |= keywords[j].code; 13901f3f253Sdrh break; 14001f3f253Sdrh } 14101f3f253Sdrh } 14201f3f253Sdrh if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ 14301f3f253Sdrh jointype |= JT_ERROR; 14401f3f253Sdrh break; 14501f3f253Sdrh } 14601f3f253Sdrh } 147ad2d8307Sdrh if( 148ad2d8307Sdrh (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || 149195e6967Sdrh (jointype & JT_ERROR)!=0 150ad2d8307Sdrh ){ 151ae29ffbeSdrh const char *zSp1 = " "; 152ae29ffbeSdrh const char *zSp2 = " "; 153ae29ffbeSdrh if( pB==0 ){ zSp1++; } 154ae29ffbeSdrh if( pC==0 ){ zSp2++; } 155ae29ffbeSdrh sqlite3ErrorMsg(pParse, "unknown or unsupported join type: " 156ae29ffbeSdrh "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC); 15701f3f253Sdrh jointype = JT_INNER; 158195e6967Sdrh }else if( jointype & JT_RIGHT ){ 1594adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 160da93d238Sdrh "RIGHT and FULL OUTER JOINs are not currently supported"); 161195e6967Sdrh jointype = JT_INNER; 16201f3f253Sdrh } 16301f3f253Sdrh return jointype; 16401f3f253Sdrh } 16501f3f253Sdrh 16601f3f253Sdrh /* 167ad2d8307Sdrh ** Return the index of a column in a table. Return -1 if the column 168ad2d8307Sdrh ** is not contained in the table. 169ad2d8307Sdrh */ 170ad2d8307Sdrh static int columnIndex(Table *pTab, const char *zCol){ 171ad2d8307Sdrh int i; 172ad2d8307Sdrh for(i=0; i<pTab->nCol; i++){ 1734adee20fSdanielk1977 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; 174ad2d8307Sdrh } 175ad2d8307Sdrh return -1; 176ad2d8307Sdrh } 177ad2d8307Sdrh 178ad2d8307Sdrh /* 17991bb0eedSdrh ** Set the value of a token to a '\000'-terminated string. 18091bb0eedSdrh */ 18191bb0eedSdrh static void setToken(Token *p, const char *z){ 1822646da7eSdrh p->z = (u8*)z; 183261919ccSdanielk1977 p->n = z ? strlen(z) : 0; 18491bb0eedSdrh p->dyn = 0; 18591bb0eedSdrh } 18691bb0eedSdrh 187c182d163Sdrh /* 188c182d163Sdrh ** Create an expression node for an identifier with the name of zName 189c182d163Sdrh */ 190c182d163Sdrh static Expr *createIdExpr(const char *zName){ 191c182d163Sdrh Token dummy; 192c182d163Sdrh setToken(&dummy, zName); 193c182d163Sdrh return sqlite3Expr(TK_ID, 0, 0, &dummy); 194c182d163Sdrh } 195c182d163Sdrh 19691bb0eedSdrh 19791bb0eedSdrh /* 198ad2d8307Sdrh ** Add a term to the WHERE expression in *ppExpr that requires the 199ad2d8307Sdrh ** zCol column to be equal in the two tables pTab1 and pTab2. 200ad2d8307Sdrh */ 201ad2d8307Sdrh static void addWhereTerm( 202ad2d8307Sdrh const char *zCol, /* Name of the column */ 203ad2d8307Sdrh const Table *pTab1, /* First table */ 204030530deSdrh const char *zAlias1, /* Alias for first table. May be NULL */ 205ad2d8307Sdrh const Table *pTab2, /* Second table */ 206030530deSdrh const char *zAlias2, /* Alias for second table. May be NULL */ 20722d6a53aSdrh int iRightJoinTable, /* VDBE cursor for the right table */ 208ad2d8307Sdrh Expr **ppExpr /* Add the equality term to this expression */ 209ad2d8307Sdrh ){ 210ad2d8307Sdrh Expr *pE1a, *pE1b, *pE1c; 211ad2d8307Sdrh Expr *pE2a, *pE2b, *pE2c; 212ad2d8307Sdrh Expr *pE; 213ad2d8307Sdrh 214c182d163Sdrh pE1a = createIdExpr(zCol); 215c182d163Sdrh pE2a = createIdExpr(zCol); 216030530deSdrh if( zAlias1==0 ){ 217030530deSdrh zAlias1 = pTab1->zName; 218030530deSdrh } 219c182d163Sdrh pE1b = createIdExpr(zAlias1); 220030530deSdrh if( zAlias2==0 ){ 221030530deSdrh zAlias2 = pTab2->zName; 222030530deSdrh } 223c182d163Sdrh pE2b = createIdExpr(zAlias2); 2244adee20fSdanielk1977 pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0); 2254adee20fSdanielk1977 pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0); 2264adee20fSdanielk1977 pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0); 2271f16230bSdrh ExprSetProperty(pE, EP_FromJoin); 22822d6a53aSdrh pE->iRightJoinTable = iRightJoinTable; 22991bb0eedSdrh *ppExpr = sqlite3ExprAnd(*ppExpr, pE); 230ad2d8307Sdrh } 231ad2d8307Sdrh 232ad2d8307Sdrh /* 2331f16230bSdrh ** Set the EP_FromJoin property on all terms of the given expression. 23422d6a53aSdrh ** And set the Expr.iRightJoinTable to iTable for every term in the 23522d6a53aSdrh ** expression. 2361cc093c2Sdrh ** 237e78e8284Sdrh ** The EP_FromJoin property is used on terms of an expression to tell 2381cc093c2Sdrh ** the LEFT OUTER JOIN processing logic that this term is part of the 2391f16230bSdrh ** join restriction specified in the ON or USING clause and not a part 2401f16230bSdrh ** of the more general WHERE clause. These terms are moved over to the 2411f16230bSdrh ** WHERE clause during join processing but we need to remember that they 2421f16230bSdrh ** originated in the ON or USING clause. 24322d6a53aSdrh ** 24422d6a53aSdrh ** The Expr.iRightJoinTable tells the WHERE clause processing that the 24522d6a53aSdrh ** expression depends on table iRightJoinTable even if that table is not 24622d6a53aSdrh ** explicitly mentioned in the expression. That information is needed 24722d6a53aSdrh ** for cases like this: 24822d6a53aSdrh ** 24922d6a53aSdrh ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5 25022d6a53aSdrh ** 25122d6a53aSdrh ** The where clause needs to defer the handling of the t1.x=5 25222d6a53aSdrh ** term until after the t2 loop of the join. In that way, a 25322d6a53aSdrh ** NULL t2 row will be inserted whenever t1.x!=5. If we do not 25422d6a53aSdrh ** defer the handling of t1.x=5, it will be processed immediately 25522d6a53aSdrh ** after the t1 loop and rows with t1.x!=5 will never appear in 25622d6a53aSdrh ** the output, which is incorrect. 2571cc093c2Sdrh */ 25822d6a53aSdrh static void setJoinExpr(Expr *p, int iTable){ 2591cc093c2Sdrh while( p ){ 2601f16230bSdrh ExprSetProperty(p, EP_FromJoin); 26122d6a53aSdrh p->iRightJoinTable = iTable; 26222d6a53aSdrh setJoinExpr(p->pLeft, iTable); 2631cc093c2Sdrh p = p->pRight; 2641cc093c2Sdrh } 2651cc093c2Sdrh } 2661cc093c2Sdrh 2671cc093c2Sdrh /* 268ad2d8307Sdrh ** This routine processes the join information for a SELECT statement. 269ad2d8307Sdrh ** ON and USING clauses are converted into extra terms of the WHERE clause. 270ad2d8307Sdrh ** NATURAL joins also create extra WHERE clause terms. 271ad2d8307Sdrh ** 27291bb0eedSdrh ** The terms of a FROM clause are contained in the Select.pSrc structure. 27391bb0eedSdrh ** The left most table is the first entry in Select.pSrc. The right-most 27491bb0eedSdrh ** table is the last entry. The join operator is held in the entry to 27591bb0eedSdrh ** the left. Thus entry 0 contains the join operator for the join between 27691bb0eedSdrh ** entries 0 and 1. Any ON or USING clauses associated with the join are 27791bb0eedSdrh ** also attached to the left entry. 27891bb0eedSdrh ** 279ad2d8307Sdrh ** This routine returns the number of errors encountered. 280ad2d8307Sdrh */ 281ad2d8307Sdrh static int sqliteProcessJoin(Parse *pParse, Select *p){ 28291bb0eedSdrh SrcList *pSrc; /* All tables in the FROM clause */ 28391bb0eedSdrh int i, j; /* Loop counters */ 28491bb0eedSdrh struct SrcList_item *pLeft; /* Left table being joined */ 28591bb0eedSdrh struct SrcList_item *pRight; /* Right table being joined */ 286ad2d8307Sdrh 28791bb0eedSdrh pSrc = p->pSrc; 28891bb0eedSdrh pLeft = &pSrc->a[0]; 28991bb0eedSdrh pRight = &pLeft[1]; 29091bb0eedSdrh for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){ 29191bb0eedSdrh Table *pLeftTab = pLeft->pTab; 29291bb0eedSdrh Table *pRightTab = pRight->pTab; 29391bb0eedSdrh 29491bb0eedSdrh if( pLeftTab==0 || pRightTab==0 ) continue; 295ad2d8307Sdrh 296ad2d8307Sdrh /* When the NATURAL keyword is present, add WHERE clause terms for 297ad2d8307Sdrh ** every column that the two tables have in common. 298ad2d8307Sdrh */ 29991bb0eedSdrh if( pLeft->jointype & JT_NATURAL ){ 30091bb0eedSdrh if( pLeft->pOn || pLeft->pUsing ){ 3014adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "a NATURAL join may not have " 302ad2d8307Sdrh "an ON or USING clause", 0); 303ad2d8307Sdrh return 1; 304ad2d8307Sdrh } 30591bb0eedSdrh for(j=0; j<pLeftTab->nCol; j++){ 30691bb0eedSdrh char *zName = pLeftTab->aCol[j].zName; 30791bb0eedSdrh if( columnIndex(pRightTab, zName)>=0 ){ 308030530deSdrh addWhereTerm(zName, pLeftTab, pLeft->zAlias, 30922d6a53aSdrh pRightTab, pRight->zAlias, 31022d6a53aSdrh pRight->iCursor, &p->pWhere); 31122d6a53aSdrh 312ad2d8307Sdrh } 313ad2d8307Sdrh } 314ad2d8307Sdrh } 315ad2d8307Sdrh 316ad2d8307Sdrh /* Disallow both ON and USING clauses in the same join 317ad2d8307Sdrh */ 31891bb0eedSdrh if( pLeft->pOn && pLeft->pUsing ){ 3194adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot have both ON and USING " 320da93d238Sdrh "clauses in the same join"); 321ad2d8307Sdrh return 1; 322ad2d8307Sdrh } 323ad2d8307Sdrh 324ad2d8307Sdrh /* Add the ON clause to the end of the WHERE clause, connected by 32591bb0eedSdrh ** an AND operator. 326ad2d8307Sdrh */ 32791bb0eedSdrh if( pLeft->pOn ){ 32822d6a53aSdrh setJoinExpr(pLeft->pOn, pRight->iCursor); 32991bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pLeft->pOn); 33091bb0eedSdrh pLeft->pOn = 0; 331ad2d8307Sdrh } 332ad2d8307Sdrh 333ad2d8307Sdrh /* Create extra terms on the WHERE clause for each column named 334ad2d8307Sdrh ** in the USING clause. Example: If the two tables to be joined are 335ad2d8307Sdrh ** A and B and the USING clause names X, Y, and Z, then add this 336ad2d8307Sdrh ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z 337ad2d8307Sdrh ** Report an error if any column mentioned in the USING clause is 338ad2d8307Sdrh ** not contained in both tables to be joined. 339ad2d8307Sdrh */ 34091bb0eedSdrh if( pLeft->pUsing ){ 34191bb0eedSdrh IdList *pList = pLeft->pUsing; 342ad2d8307Sdrh for(j=0; j<pList->nId; j++){ 34391bb0eedSdrh char *zName = pList->a[j].zName; 34491bb0eedSdrh if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){ 3454adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot join using column %s - column " 34691bb0eedSdrh "not present in both tables", zName); 347ad2d8307Sdrh return 1; 348ad2d8307Sdrh } 349030530deSdrh addWhereTerm(zName, pLeftTab, pLeft->zAlias, 35022d6a53aSdrh pRightTab, pRight->zAlias, 35122d6a53aSdrh pRight->iCursor, &p->pWhere); 352ad2d8307Sdrh } 353ad2d8307Sdrh } 354ad2d8307Sdrh } 355ad2d8307Sdrh return 0; 356ad2d8307Sdrh } 357ad2d8307Sdrh 358ad2d8307Sdrh /* 359c926afbcSdrh ** Insert code into "v" that will push the record on the top of the 360c926afbcSdrh ** stack into the sorter. 361c926afbcSdrh */ 362d59ba6ceSdrh static void pushOntoSorter( 363d59ba6ceSdrh Parse *pParse, /* Parser context */ 364d59ba6ceSdrh ExprList *pOrderBy, /* The ORDER BY clause */ 365d59ba6ceSdrh Select *pSelect /* The whole SELECT statement */ 366d59ba6ceSdrh ){ 367d59ba6ceSdrh Vdbe *v = pParse->pVdbe; 368c182d163Sdrh sqlite3ExprCodeExprList(pParse, pOrderBy); 3699d2985c7Sdrh sqlite3VdbeAddOp(v, OP_Sequence, pOrderBy->iECursor, 0); 3704db38a70Sdrh sqlite3VdbeAddOp(v, OP_Pull, pOrderBy->nExpr + 1, 0); 3714db38a70Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr + 2, 0); 3729d2985c7Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pOrderBy->iECursor, 0); 373d59ba6ceSdrh if( pSelect->iLimit>=0 ){ 37415007a99Sdrh int addr1, addr2; 37515007a99Sdrh addr1 = sqlite3VdbeAddOp(v, OP_IfMemZero, pSelect->iLimit+1, 0); 37615007a99Sdrh sqlite3VdbeAddOp(v, OP_MemIncr, -1, pSelect->iLimit+1); 37715007a99Sdrh addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 378d59ba6ceSdrh sqlite3VdbeJumpHere(v, addr1); 379d59ba6ceSdrh sqlite3VdbeAddOp(v, OP_Last, pOrderBy->iECursor, 0); 380d59ba6ceSdrh sqlite3VdbeAddOp(v, OP_Delete, pOrderBy->iECursor, 0); 38115007a99Sdrh sqlite3VdbeJumpHere(v, addr2); 382d59ba6ceSdrh pSelect->iLimit = -1; 383d59ba6ceSdrh } 384c926afbcSdrh } 385c926afbcSdrh 386c926afbcSdrh /* 387ec7429aeSdrh ** Add code to implement the OFFSET 388ea48eb2eSdrh */ 389ec7429aeSdrh static void codeOffset( 390bab39e13Sdrh Vdbe *v, /* Generate code into this VM */ 391ea48eb2eSdrh Select *p, /* The SELECT statement being coded */ 392ea48eb2eSdrh int iContinue, /* Jump here to skip the current record */ 393ea48eb2eSdrh int nPop /* Number of times to pop stack when jumping */ 394ea48eb2eSdrh ){ 39513449892Sdrh if( p->iOffset>=0 && iContinue!=0 ){ 39615007a99Sdrh int addr; 39715007a99Sdrh sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iOffset); 3983a129247Sdrh addr = sqlite3VdbeAddOp(v, OP_IfMemNeg, p->iOffset, 0); 399ea48eb2eSdrh if( nPop>0 ){ 400ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_Pop, nPop, 0); 401ea48eb2eSdrh } 402ea48eb2eSdrh sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); 403ad6d9460Sdrh VdbeComment((v, "# skip OFFSET records")); 40415007a99Sdrh sqlite3VdbeJumpHere(v, addr); 405ea48eb2eSdrh } 406ea48eb2eSdrh } 407ea48eb2eSdrh 408ea48eb2eSdrh /* 409c99130fdSdrh ** Add code that will check to make sure the top N elements of the 410c99130fdSdrh ** stack are distinct. iTab is a sorting index that holds previously 411c99130fdSdrh ** seen combinations of the N values. A new entry is made in iTab 412c99130fdSdrh ** if the current N values are new. 413c99130fdSdrh ** 414f8875400Sdrh ** A jump to addrRepeat is made and the N+1 values are popped from the 415c99130fdSdrh ** stack if the top N elements are not distinct. 416c99130fdSdrh */ 417c99130fdSdrh static void codeDistinct( 418c99130fdSdrh Vdbe *v, /* Generate code into this VM */ 419c99130fdSdrh int iTab, /* A sorting index used to test for distinctness */ 420c99130fdSdrh int addrRepeat, /* Jump to here if not distinct */ 421f8875400Sdrh int N /* The top N elements of the stack must be distinct */ 422c99130fdSdrh ){ 423c99130fdSdrh sqlite3VdbeAddOp(v, OP_MakeRecord, -N, 0); 424c99130fdSdrh sqlite3VdbeAddOp(v, OP_Distinct, iTab, sqlite3VdbeCurrentAddr(v)+3); 425f8875400Sdrh sqlite3VdbeAddOp(v, OP_Pop, N+1, 0); 426c99130fdSdrh sqlite3VdbeAddOp(v, OP_Goto, 0, addrRepeat); 427c99130fdSdrh VdbeComment((v, "# skip indistinct records")); 428c99130fdSdrh sqlite3VdbeAddOp(v, OP_IdxInsert, iTab, 0); 429c99130fdSdrh } 430c99130fdSdrh 431c99130fdSdrh 432c99130fdSdrh /* 4332282792aSdrh ** This routine generates the code for the inside of the inner loop 4342282792aSdrh ** of a SELECT. 43582c3d636Sdrh ** 43638640e15Sdrh ** If srcTab and nColumn are both zero, then the pEList expressions 43738640e15Sdrh ** are evaluated in order to get the data for this row. If nColumn>0 43838640e15Sdrh ** then data is pulled from srcTab and pEList is used only to get the 43938640e15Sdrh ** datatypes for each column. 4402282792aSdrh */ 4412282792aSdrh static int selectInnerLoop( 4422282792aSdrh Parse *pParse, /* The parser context */ 443df199a25Sdrh Select *p, /* The complete select statement being coded */ 4442282792aSdrh ExprList *pEList, /* List of values being extracted */ 44582c3d636Sdrh int srcTab, /* Pull data from this table */ 446967e8b73Sdrh int nColumn, /* Number of columns in the source table */ 4472282792aSdrh ExprList *pOrderBy, /* If not NULL, sort results using this key */ 4482282792aSdrh int distinct, /* If >=0, make sure results are distinct */ 4492282792aSdrh int eDest, /* How to dispose of the results */ 4502282792aSdrh int iParm, /* An argument to the disposal method */ 4512282792aSdrh int iContinue, /* Jump here to continue with next row */ 45284ac9d02Sdanielk1977 int iBreak, /* Jump here to break out of the inner loop */ 45384ac9d02Sdanielk1977 char *aff /* affinity string if eDest is SRT_Union */ 4542282792aSdrh ){ 4552282792aSdrh Vdbe *v = pParse->pVdbe; 4562282792aSdrh int i; 457ea48eb2eSdrh int hasDistinct; /* True if the DISTINCT keyword is present */ 45838640e15Sdrh 459daffd0e5Sdrh if( v==0 ) return 0; 46038640e15Sdrh assert( pEList!=0 ); 4612282792aSdrh 462df199a25Sdrh /* If there was a LIMIT clause on the SELECT statement, then do the check 463df199a25Sdrh ** to see if this row should be output. 464df199a25Sdrh */ 465eda639e1Sdrh hasDistinct = distinct>=0 && pEList->nExpr>0; 466ea48eb2eSdrh if( pOrderBy==0 && !hasDistinct ){ 467ec7429aeSdrh codeOffset(v, p, iContinue, 0); 468df199a25Sdrh } 469df199a25Sdrh 470967e8b73Sdrh /* Pull the requested columns. 4712282792aSdrh */ 47238640e15Sdrh if( nColumn>0 ){ 473967e8b73Sdrh for(i=0; i<nColumn; i++){ 4744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, i); 47582c3d636Sdrh } 47638640e15Sdrh }else{ 47738640e15Sdrh nColumn = pEList->nExpr; 478c182d163Sdrh sqlite3ExprCodeExprList(pParse, pEList); 47982c3d636Sdrh } 4802282792aSdrh 481daffd0e5Sdrh /* If the DISTINCT keyword was present on the SELECT statement 482daffd0e5Sdrh ** and this row has been seen before, then do not make this row 483daffd0e5Sdrh ** part of the result. 4842282792aSdrh */ 485ea48eb2eSdrh if( hasDistinct ){ 486f8875400Sdrh assert( pEList!=0 ); 487f8875400Sdrh assert( pEList->nExpr==nColumn ); 488f8875400Sdrh codeDistinct(v, distinct, iContinue, nColumn); 489ea48eb2eSdrh if( pOrderBy==0 ){ 490ec7429aeSdrh codeOffset(v, p, iContinue, nColumn); 491ea48eb2eSdrh } 4922282792aSdrh } 49382c3d636Sdrh 494c926afbcSdrh switch( eDest ){ 49582c3d636Sdrh /* In this mode, write each query result to the key of the temporary 49682c3d636Sdrh ** table iParm. 4972282792aSdrh */ 49813449892Sdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 499c926afbcSdrh case SRT_Union: { 500f8875400Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 50113449892Sdrh if( aff ){ 50284ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 50313449892Sdrh } 504f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, iParm, 0); 505c926afbcSdrh break; 506c926afbcSdrh } 50782c3d636Sdrh 50882c3d636Sdrh /* Construct a record from the query result, but instead of 50982c3d636Sdrh ** saving that record, use it as a key to delete elements from 51082c3d636Sdrh ** the temporary table iParm. 51182c3d636Sdrh */ 512c926afbcSdrh case SRT_Except: { 5130bd1f4eaSdrh int addr; 514f8875400Sdrh addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 51584ac9d02Sdanielk1977 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); 5164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3); 5174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Delete, iParm, 0); 518c926afbcSdrh break; 519c926afbcSdrh } 5205338a5f7Sdanielk1977 #endif 5215338a5f7Sdanielk1977 5225338a5f7Sdanielk1977 /* Store the result as data using a unique key. 5235338a5f7Sdanielk1977 */ 5245338a5f7Sdanielk1977 case SRT_Table: 5259d2985c7Sdrh case SRT_VirtualTab: { 5265338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 5275338a5f7Sdanielk1977 if( pOrderBy ){ 528d59ba6ceSdrh pushOntoSorter(pParse, pOrderBy, p); 5295338a5f7Sdanielk1977 }else{ 530f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0); 5315338a5f7Sdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 532f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, iParm, 0); 5335338a5f7Sdanielk1977 } 5345338a5f7Sdanielk1977 break; 5355338a5f7Sdanielk1977 } 5362282792aSdrh 53793758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 5382282792aSdrh /* If we are creating a set for an "expr IN (SELECT ...)" construct, 5392282792aSdrh ** then there should be a single item on the stack. Write this 5402282792aSdrh ** item into the set table with bogus data. 5412282792aSdrh */ 542c926afbcSdrh case SRT_Set: { 5434adee20fSdanielk1977 int addr1 = sqlite3VdbeCurrentAddr(v); 54452b36cabSdrh int addr2; 545e014a838Sdanielk1977 546967e8b73Sdrh assert( nColumn==1 ); 5474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3); 5484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 5494adee20fSdanielk1977 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 550c926afbcSdrh if( pOrderBy ){ 551de941c60Sdrh /* At first glance you would think we could optimize out the 552de941c60Sdrh ** ORDER BY in this case since the order of entries in the set 553de941c60Sdrh ** does not matter. But there might be a LIMIT clause, in which 554de941c60Sdrh ** case the order does matter */ 555d59ba6ceSdrh pushOntoSorter(pParse, pOrderBy, p); 556c926afbcSdrh }else{ 557f0113000Sdanielk1977 char affinity = (iParm>>16)&0xFF; 558f0113000Sdanielk1977 affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, affinity); 559f0113000Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 560f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0); 561c926afbcSdrh } 562d654be80Sdrh sqlite3VdbeJumpHere(v, addr2); 563c926afbcSdrh break; 564c926afbcSdrh } 56582c3d636Sdrh 566504b6989Sdrh /* If any row exist in the result set, record that fact and abort. 567ec7429aeSdrh */ 568ec7429aeSdrh case SRT_Exists: { 569ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, iParm); 570ec7429aeSdrh sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 571ec7429aeSdrh /* The LIMIT clause will terminate the loop for us */ 572ec7429aeSdrh break; 573ec7429aeSdrh } 574ec7429aeSdrh 5752282792aSdrh /* If this is a scalar select that is part of an expression, then 5762282792aSdrh ** store the results in the appropriate memory cell and break out 5772282792aSdrh ** of the scan loop. 5782282792aSdrh */ 579c926afbcSdrh case SRT_Mem: { 580967e8b73Sdrh assert( nColumn==1 ); 581c926afbcSdrh if( pOrderBy ){ 582d59ba6ceSdrh pushOntoSorter(pParse, pOrderBy, p); 583c926afbcSdrh }else{ 5844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 585ec7429aeSdrh /* The LIMIT clause will jump out of the loop for us */ 586c926afbcSdrh } 587c926afbcSdrh break; 588c926afbcSdrh } 58993758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ 5902282792aSdrh 591c182d163Sdrh /* Send the data to the callback function or to a subroutine. In the 592c182d163Sdrh ** case of a subroutine, the subroutine itself is responsible for 593c182d163Sdrh ** popping the data from the stack. 594f46f905aSdrh */ 595c182d163Sdrh case SRT_Subroutine: 5969d2985c7Sdrh case SRT_Callback: { 597f46f905aSdrh if( pOrderBy ){ 598ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 599d59ba6ceSdrh pushOntoSorter(pParse, pOrderBy, p); 600c182d163Sdrh }else if( eDest==SRT_Subroutine ){ 6014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 602c182d163Sdrh }else{ 603c182d163Sdrh sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 604ac82fcf5Sdrh } 605142e30dfSdrh break; 606142e30dfSdrh } 607142e30dfSdrh 6086a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_TRIGGER) 609d7489c39Sdrh /* Discard the results. This is used for SELECT statements inside 610d7489c39Sdrh ** the body of a TRIGGER. The purpose of such selects is to call 611d7489c39Sdrh ** user-defined functions that have side effects. We do not care 612d7489c39Sdrh ** about the actual results of the select. 613d7489c39Sdrh */ 614c926afbcSdrh default: { 615f46f905aSdrh assert( eDest==SRT_Discard ); 6164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 617c926afbcSdrh break; 618c926afbcSdrh } 61993758c8dSdanielk1977 #endif 620c926afbcSdrh } 621ec7429aeSdrh 622ec7429aeSdrh /* Jump to the end of the loop if the LIMIT is reached. 623ec7429aeSdrh */ 624ec7429aeSdrh if( p->iLimit>=0 && pOrderBy==0 ){ 62515007a99Sdrh sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit); 626ec7429aeSdrh sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, iBreak); 627ec7429aeSdrh } 62882c3d636Sdrh return 0; 62982c3d636Sdrh } 63082c3d636Sdrh 63182c3d636Sdrh /* 632dece1a84Sdrh ** Given an expression list, generate a KeyInfo structure that records 633dece1a84Sdrh ** the collating sequence for each expression in that expression list. 634dece1a84Sdrh ** 6350342b1f5Sdrh ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting 6360342b1f5Sdrh ** KeyInfo structure is appropriate for initializing a virtual index to 6370342b1f5Sdrh ** implement that clause. If the ExprList is the result set of a SELECT 6380342b1f5Sdrh ** then the KeyInfo structure is appropriate for initializing a virtual 6390342b1f5Sdrh ** index to implement a DISTINCT test. 6400342b1f5Sdrh ** 641dece1a84Sdrh ** Space to hold the KeyInfo structure is obtain from malloc. The calling 642dece1a84Sdrh ** function is responsible for seeing that this structure is eventually 643dece1a84Sdrh ** freed. Add the KeyInfo structure to the P3 field of an opcode using 644dece1a84Sdrh ** P3_KEYINFO_HANDOFF is the usual way of dealing with this. 645dece1a84Sdrh */ 646dece1a84Sdrh static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){ 647dece1a84Sdrh sqlite3 *db = pParse->db; 648dece1a84Sdrh int nExpr; 649dece1a84Sdrh KeyInfo *pInfo; 650dece1a84Sdrh struct ExprList_item *pItem; 651dece1a84Sdrh int i; 652dece1a84Sdrh 653dece1a84Sdrh nExpr = pList->nExpr; 654dece1a84Sdrh pInfo = sqliteMalloc( sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) ); 655dece1a84Sdrh if( pInfo ){ 6562646da7eSdrh pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr]; 657dece1a84Sdrh pInfo->nField = nExpr; 65814db2665Sdanielk1977 pInfo->enc = ENC(db); 659dece1a84Sdrh for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){ 660dece1a84Sdrh CollSeq *pColl; 661dece1a84Sdrh pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); 662dece1a84Sdrh if( !pColl ){ 663dece1a84Sdrh pColl = db->pDfltColl; 664dece1a84Sdrh } 665dece1a84Sdrh pInfo->aColl[i] = pColl; 666dece1a84Sdrh pInfo->aSortOrder[i] = pItem->sortOrder; 667dece1a84Sdrh } 668dece1a84Sdrh } 669dece1a84Sdrh return pInfo; 670dece1a84Sdrh } 671dece1a84Sdrh 672dece1a84Sdrh 673dece1a84Sdrh /* 674d8bc7086Sdrh ** If the inner loop was generated using a non-null pOrderBy argument, 675d8bc7086Sdrh ** then the results were placed in a sorter. After the loop is terminated 676d8bc7086Sdrh ** we need to run the sorter and output the results. The following 677d8bc7086Sdrh ** routine generates the code needed to do that. 678d8bc7086Sdrh */ 679c926afbcSdrh static void generateSortTail( 680cdd536f0Sdrh Parse *pParse, /* Parsing context */ 681c926afbcSdrh Select *p, /* The SELECT statement */ 682c926afbcSdrh Vdbe *v, /* Generate code into this VDBE */ 683c926afbcSdrh int nColumn, /* Number of columns of data */ 684c926afbcSdrh int eDest, /* Write the sorted results here */ 685c926afbcSdrh int iParm /* Optional parameter associated with eDest */ 686c926afbcSdrh ){ 6870342b1f5Sdrh int brk = sqlite3VdbeMakeLabel(v); 6880342b1f5Sdrh int cont = sqlite3VdbeMakeLabel(v); 689d8bc7086Sdrh int addr; 6900342b1f5Sdrh int iTab; 691cdd536f0Sdrh int pseudoTab; 6920342b1f5Sdrh ExprList *pOrderBy = p->pOrderBy; 693ffbc3088Sdrh 6949d2985c7Sdrh iTab = pOrderBy->iECursor; 695cdd536f0Sdrh if( eDest==SRT_Callback || eDest==SRT_Subroutine ){ 696cdd536f0Sdrh pseudoTab = pParse->nTab++; 697cdd536f0Sdrh sqlite3VdbeAddOp(v, OP_OpenPseudo, pseudoTab, 0); 698cdd536f0Sdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pseudoTab, nColumn); 699cdd536f0Sdrh } 7000342b1f5Sdrh addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk); 701ec7429aeSdrh codeOffset(v, p, cont, 0); 702cdd536f0Sdrh if( eDest==SRT_Callback || eDest==SRT_Subroutine ){ 703cdd536f0Sdrh sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 704cdd536f0Sdrh } 7054db38a70Sdrh sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1); 706c926afbcSdrh switch( eDest ){ 707c926afbcSdrh case SRT_Table: 7089d2985c7Sdrh case SRT_VirtualTab: { 709f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0); 7104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 711f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, iParm, 0); 712c926afbcSdrh break; 713c926afbcSdrh } 71493758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 715c926afbcSdrh case SRT_Set: { 716c926afbcSdrh assert( nColumn==1 ); 7174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 7184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 7194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3); 7208a51256cSdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, "c", P3_STATIC); 721f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0); 722c926afbcSdrh break; 723c926afbcSdrh } 724c926afbcSdrh case SRT_Mem: { 725c926afbcSdrh assert( nColumn==1 ); 7264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); 727ec7429aeSdrh /* The LIMIT clause will terminate the loop for us */ 728c926afbcSdrh break; 729c926afbcSdrh } 73093758c8dSdanielk1977 #endif 731ce665cf6Sdrh case SRT_Callback: 732ac82fcf5Sdrh case SRT_Subroutine: { 733ac82fcf5Sdrh int i; 734cdd536f0Sdrh sqlite3VdbeAddOp(v, OP_Insert, pseudoTab, 0); 735ac82fcf5Sdrh for(i=0; i<nColumn; i++){ 736cdd536f0Sdrh sqlite3VdbeAddOp(v, OP_Column, pseudoTab, i); 737ac82fcf5Sdrh } 738ce665cf6Sdrh if( eDest==SRT_Callback ){ 739ce665cf6Sdrh sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); 740ce665cf6Sdrh }else{ 7414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); 742ce665cf6Sdrh } 743ac82fcf5Sdrh break; 744ac82fcf5Sdrh } 745c926afbcSdrh default: { 746f46f905aSdrh /* Do nothing */ 747c926afbcSdrh break; 748c926afbcSdrh } 749c926afbcSdrh } 750ec7429aeSdrh 751ec7429aeSdrh /* Jump to the end of the loop when the LIMIT is reached 752ec7429aeSdrh */ 753ec7429aeSdrh if( p->iLimit>=0 ){ 75415007a99Sdrh sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit); 755ec7429aeSdrh sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, brk); 756ec7429aeSdrh } 757ec7429aeSdrh 758ec7429aeSdrh /* The bottom of the loop 759ec7429aeSdrh */ 7600342b1f5Sdrh sqlite3VdbeResolveLabel(v, cont); 7610342b1f5Sdrh sqlite3VdbeAddOp(v, OP_Next, iTab, addr); 7620342b1f5Sdrh sqlite3VdbeResolveLabel(v, brk); 763cdd536f0Sdrh if( eDest==SRT_Callback || eDest==SRT_Subroutine ){ 764cdd536f0Sdrh sqlite3VdbeAddOp(v, OP_Close, pseudoTab, 0); 765cdd536f0Sdrh } 766cdd536f0Sdrh 767d8bc7086Sdrh } 768d8bc7086Sdrh 769d8bc7086Sdrh /* 770517eb646Sdanielk1977 ** Return a pointer to a string containing the 'declaration type' of the 771517eb646Sdanielk1977 ** expression pExpr. The string may be treated as static by the caller. 772e78e8284Sdrh ** 773955de52cSdanielk1977 ** The declaration type is the exact datatype definition extracted from the 774955de52cSdanielk1977 ** original CREATE TABLE statement if the expression is a column. The 775955de52cSdanielk1977 ** declaration type for a ROWID field is INTEGER. Exactly when an expression 776955de52cSdanielk1977 ** is considered a column can be complex in the presence of subqueries. The 777955de52cSdanielk1977 ** result-set expression in all of the following SELECT statements is 778955de52cSdanielk1977 ** considered a column by this function. 779e78e8284Sdrh ** 780955de52cSdanielk1977 ** SELECT col FROM tbl; 781955de52cSdanielk1977 ** SELECT (SELECT col FROM tbl; 782955de52cSdanielk1977 ** SELECT (SELECT col FROM tbl); 783955de52cSdanielk1977 ** SELECT abc FROM (SELECT col AS abc FROM tbl); 784955de52cSdanielk1977 ** 785955de52cSdanielk1977 ** The declaration type for any expression other than a column is NULL. 786fcb78a49Sdrh */ 787955de52cSdanielk1977 static const char *columnType( 788955de52cSdanielk1977 NameContext *pNC, 789955de52cSdanielk1977 Expr *pExpr, 790955de52cSdanielk1977 const char **pzOriginDb, 791955de52cSdanielk1977 const char **pzOriginTab, 792955de52cSdanielk1977 const char **pzOriginCol 793955de52cSdanielk1977 ){ 794955de52cSdanielk1977 char const *zType = 0; 795955de52cSdanielk1977 char const *zOriginDb = 0; 796955de52cSdanielk1977 char const *zOriginTab = 0; 797955de52cSdanielk1977 char const *zOriginCol = 0; 798517eb646Sdanielk1977 int j; 799b3bce662Sdanielk1977 if( pExpr==0 || pNC->pSrcList==0 ) return 0; 8005338a5f7Sdanielk1977 8015338a5f7Sdanielk1977 /* The TK_AS operator can only occur in ORDER BY, GROUP BY, HAVING, 8025338a5f7Sdanielk1977 ** and LIMIT clauses. But pExpr originates in the result set of a 8035338a5f7Sdanielk1977 ** SELECT. So pExpr can never contain an AS operator. 8045338a5f7Sdanielk1977 */ 8055338a5f7Sdanielk1977 assert( pExpr->op!=TK_AS ); 8065338a5f7Sdanielk1977 80700e279d9Sdanielk1977 switch( pExpr->op ){ 808*30bcf5dbSdrh case TK_AGG_COLUMN: 80900e279d9Sdanielk1977 case TK_COLUMN: { 810955de52cSdanielk1977 /* The expression is a column. Locate the table the column is being 811955de52cSdanielk1977 ** extracted from in NameContext.pSrcList. This table may be real 812955de52cSdanielk1977 ** database table or a subquery. 813955de52cSdanielk1977 */ 814955de52cSdanielk1977 Table *pTab = 0; /* Table structure column is extracted from */ 815955de52cSdanielk1977 Select *pS = 0; /* Select the column is extracted from */ 816955de52cSdanielk1977 int iCol = pExpr->iColumn; /* Index of column in pTab */ 817b3bce662Sdanielk1977 while( pNC && !pTab ){ 818b3bce662Sdanielk1977 SrcList *pTabList = pNC->pSrcList; 819b3bce662Sdanielk1977 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); 820b3bce662Sdanielk1977 if( j<pTabList->nSrc ){ 8216a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 822955de52cSdanielk1977 pS = pTabList->a[j].pSelect; 823b3bce662Sdanielk1977 }else{ 824b3bce662Sdanielk1977 pNC = pNC->pNext; 825b3bce662Sdanielk1977 } 826b3bce662Sdanielk1977 } 827955de52cSdanielk1977 8287e62779aSdrh if( pTab==0 ){ 8297e62779aSdrh /* FIX ME: 8307e62779aSdrh ** This can occurs if you have something like "SELECT new.x;" inside 8317e62779aSdrh ** a trigger. In other words, if you reference the special "new" 8327e62779aSdrh ** table in the result set of a select. We do not have a good way 8337e62779aSdrh ** to find the actual table type, so call it "TEXT". This is really 8347e62779aSdrh ** something of a bug, but I do not know how to fix it. 8357e62779aSdrh ** 8367e62779aSdrh ** This code does not produce the correct answer - it just prevents 8377e62779aSdrh ** a segfault. See ticket #1229. 8387e62779aSdrh */ 8397e62779aSdrh zType = "TEXT"; 8407e62779aSdrh break; 8417e62779aSdrh } 842955de52cSdanielk1977 843b3bce662Sdanielk1977 assert( pTab ); 844955de52cSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 845955de52cSdanielk1977 if( pS ){ 846955de52cSdanielk1977 /* The "table" is actually a sub-select or a view in the FROM clause 847955de52cSdanielk1977 ** of the SELECT statement. Return the declaration type and origin 848955de52cSdanielk1977 ** data for the result-set column of the sub-select. 849955de52cSdanielk1977 */ 850955de52cSdanielk1977 if( iCol>=0 && iCol<pS->pEList->nExpr ){ 851955de52cSdanielk1977 /* If iCol is less than zero, then the expression requests the 852955de52cSdanielk1977 ** rowid of the sub-select or view. This expression is legal (see 853955de52cSdanielk1977 ** test case misc2.2.2) - it always evaluates to NULL. 854955de52cSdanielk1977 */ 855955de52cSdanielk1977 NameContext sNC; 856955de52cSdanielk1977 Expr *p = pS->pEList->a[iCol].pExpr; 857955de52cSdanielk1977 sNC.pSrcList = pS->pSrc; 858955de52cSdanielk1977 sNC.pNext = 0; 859955de52cSdanielk1977 sNC.pParse = pNC->pParse; 860955de52cSdanielk1977 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 861955de52cSdanielk1977 } 862955de52cSdanielk1977 }else 863955de52cSdanielk1977 #endif 864955de52cSdanielk1977 if( pTab->pSchema ){ 865955de52cSdanielk1977 /* A real table */ 866955de52cSdanielk1977 assert( !pS ); 867fcb78a49Sdrh if( iCol<0 ) iCol = pTab->iPKey; 868fcb78a49Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 869fcb78a49Sdrh if( iCol<0 ){ 870fcb78a49Sdrh zType = "INTEGER"; 871955de52cSdanielk1977 zOriginCol = "rowid"; 872fcb78a49Sdrh }else{ 873fcb78a49Sdrh zType = pTab->aCol[iCol].zType; 874955de52cSdanielk1977 zOriginCol = pTab->aCol[iCol].zName; 875955de52cSdanielk1977 } 876955de52cSdanielk1977 zOriginTab = pTab->zName; 877955de52cSdanielk1977 if( pNC->pParse ){ 878955de52cSdanielk1977 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); 879955de52cSdanielk1977 zOriginDb = pNC->pParse->db->aDb[iDb].zName; 880955de52cSdanielk1977 } 881fcb78a49Sdrh } 88200e279d9Sdanielk1977 break; 883736c22b8Sdrh } 88493758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 88500e279d9Sdanielk1977 case TK_SELECT: { 886955de52cSdanielk1977 /* The expression is a sub-select. Return the declaration type and 887955de52cSdanielk1977 ** origin info for the single column in the result set of the SELECT 888955de52cSdanielk1977 ** statement. 889955de52cSdanielk1977 */ 890b3bce662Sdanielk1977 NameContext sNC; 89100e279d9Sdanielk1977 Select *pS = pExpr->pSelect; 892955de52cSdanielk1977 Expr *p = pS->pEList->a[0].pExpr; 893955de52cSdanielk1977 sNC.pSrcList = pS->pSrc; 894b3bce662Sdanielk1977 sNC.pNext = pNC; 895955de52cSdanielk1977 sNC.pParse = pNC->pParse; 896955de52cSdanielk1977 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 89700e279d9Sdanielk1977 break; 898fcb78a49Sdrh } 89993758c8dSdanielk1977 #endif 90000e279d9Sdanielk1977 } 90100e279d9Sdanielk1977 902955de52cSdanielk1977 if( pzOriginDb ){ 903955de52cSdanielk1977 assert( pzOriginTab && pzOriginCol ); 904955de52cSdanielk1977 *pzOriginDb = zOriginDb; 905955de52cSdanielk1977 *pzOriginTab = zOriginTab; 906955de52cSdanielk1977 *pzOriginCol = zOriginCol; 907955de52cSdanielk1977 } 908517eb646Sdanielk1977 return zType; 909517eb646Sdanielk1977 } 910517eb646Sdanielk1977 911517eb646Sdanielk1977 /* 912517eb646Sdanielk1977 ** Generate code that will tell the VDBE the declaration types of columns 913517eb646Sdanielk1977 ** in the result set. 914517eb646Sdanielk1977 */ 915517eb646Sdanielk1977 static void generateColumnTypes( 916517eb646Sdanielk1977 Parse *pParse, /* Parser context */ 917517eb646Sdanielk1977 SrcList *pTabList, /* List of tables */ 918517eb646Sdanielk1977 ExprList *pEList /* Expressions defining the result set */ 919517eb646Sdanielk1977 ){ 920517eb646Sdanielk1977 Vdbe *v = pParse->pVdbe; 921517eb646Sdanielk1977 int i; 922b3bce662Sdanielk1977 NameContext sNC; 923b3bce662Sdanielk1977 sNC.pSrcList = pTabList; 924955de52cSdanielk1977 sNC.pParse = pParse; 925517eb646Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 926517eb646Sdanielk1977 Expr *p = pEList->a[i].pExpr; 927955de52cSdanielk1977 const char *zOrigDb = 0; 928955de52cSdanielk1977 const char *zOrigTab = 0; 929955de52cSdanielk1977 const char *zOrigCol = 0; 930955de52cSdanielk1977 const char *zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); 931955de52cSdanielk1977 9324b1ae99dSdanielk1977 /* The vdbe must make it's own copy of the column-type and other 9334b1ae99dSdanielk1977 ** column specific strings, in case the schema is reset before this 9344b1ae99dSdanielk1977 ** virtual machine is deleted. 935fbcd585fSdanielk1977 */ 9364b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P3_TRANSIENT); 9374b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P3_TRANSIENT); 9384b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P3_TRANSIENT); 9394b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P3_TRANSIENT); 940fcb78a49Sdrh } 941fcb78a49Sdrh } 942fcb78a49Sdrh 943fcb78a49Sdrh /* 944fcb78a49Sdrh ** Generate code that will tell the VDBE the names of columns 945fcb78a49Sdrh ** in the result set. This information is used to provide the 946fcabd464Sdrh ** azCol[] values in the callback. 94782c3d636Sdrh */ 948832508b7Sdrh static void generateColumnNames( 949832508b7Sdrh Parse *pParse, /* Parser context */ 950ad3cab52Sdrh SrcList *pTabList, /* List of tables */ 951832508b7Sdrh ExprList *pEList /* Expressions defining the result set */ 952832508b7Sdrh ){ 953d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 9546a3ea0e6Sdrh int i, j; 9559bb575fdSdrh sqlite3 *db = pParse->db; 956fcabd464Sdrh int fullNames, shortNames; 957fcabd464Sdrh 958fe2093d7Sdrh #ifndef SQLITE_OMIT_EXPLAIN 9593cf86063Sdanielk1977 /* If this is an EXPLAIN, skip this step */ 9603cf86063Sdanielk1977 if( pParse->explain ){ 96161de0d1bSdanielk1977 return; 9623cf86063Sdanielk1977 } 9635338a5f7Sdanielk1977 #endif 9643cf86063Sdanielk1977 965d6502758Sdrh assert( v!=0 ); 9669e12800dSdanielk1977 if( pParse->colNamesSet || v==0 || sqlite3MallocFailed() ) return; 967d8bc7086Sdrh pParse->colNamesSet = 1; 968fcabd464Sdrh fullNames = (db->flags & SQLITE_FullColNames)!=0; 969fcabd464Sdrh shortNames = (db->flags & SQLITE_ShortColNames)!=0; 97022322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, pEList->nExpr); 97182c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 97282c3d636Sdrh Expr *p; 9735a38705eSdrh p = pEList->a[i].pExpr; 9745a38705eSdrh if( p==0 ) continue; 97582c3d636Sdrh if( pEList->a[i].zName ){ 97682c3d636Sdrh char *zName = pEList->a[i].zName; 977955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, strlen(zName)); 97882c3d636Sdrh continue; 97982c3d636Sdrh } 980fa173a76Sdrh if( p->op==TK_COLUMN && pTabList ){ 9816a3ea0e6Sdrh Table *pTab; 98297665873Sdrh char *zCol; 9838aff1015Sdrh int iCol = p->iColumn; 9846a3ea0e6Sdrh for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} 9856a3ea0e6Sdrh assert( j<pTabList->nSrc ); 9866a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 9878aff1015Sdrh if( iCol<0 ) iCol = pTab->iPKey; 98897665873Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 989b1363206Sdrh if( iCol<0 ){ 99047a6db2bSdrh zCol = "rowid"; 991b1363206Sdrh }else{ 992b1363206Sdrh zCol = pTab->aCol[iCol].zName; 993b1363206Sdrh } 994fcabd464Sdrh if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ 995955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n); 996fcabd464Sdrh }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ 99782c3d636Sdrh char *zName = 0; 99882c3d636Sdrh char *zTab; 99982c3d636Sdrh 10006a3ea0e6Sdrh zTab = pTabList->a[j].zAlias; 1001fcabd464Sdrh if( fullNames || zTab==0 ) zTab = pTab->zName; 1002f93339deSdrh sqlite3SetString(&zName, zTab, ".", zCol, (char*)0); 1003955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P3_DYNAMIC); 100482c3d636Sdrh }else{ 1005955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol)); 100682c3d636Sdrh } 10076977fea8Sdrh }else if( p->span.z && p->span.z[0] ){ 1008955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n); 10093cf86063Sdanielk1977 /* sqlite3VdbeCompressSpace(v, addr); */ 10101bee3d7bSdrh }else{ 10111bee3d7bSdrh char zName[30]; 10121bee3d7bSdrh assert( p->op!=TK_COLUMN || pTabList==0 ); 10131bee3d7bSdrh sprintf(zName, "column%d", i+1); 1014955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, 0); 101582c3d636Sdrh } 101682c3d636Sdrh } 101776d505baSdanielk1977 generateColumnTypes(pParse, pTabList, pEList); 10185080aaa7Sdrh } 101982c3d636Sdrh 102093758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 102182c3d636Sdrh /* 1022d8bc7086Sdrh ** Name of the connection operator, used for error messages. 1023d8bc7086Sdrh */ 1024d8bc7086Sdrh static const char *selectOpName(int id){ 1025d8bc7086Sdrh char *z; 1026d8bc7086Sdrh switch( id ){ 1027d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 1028d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 1029d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 1030d8bc7086Sdrh default: z = "UNION"; break; 1031d8bc7086Sdrh } 1032d8bc7086Sdrh return z; 1033d8bc7086Sdrh } 103493758c8dSdanielk1977 #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1035d8bc7086Sdrh 1036d8bc7086Sdrh /* 1037315555caSdrh ** Forward declaration 1038315555caSdrh */ 10399b3187e1Sdrh static int prepSelectStmt(Parse*, Select*); 1040315555caSdrh 1041315555caSdrh /* 104222f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes 104322f70c32Sdrh ** the result set of that SELECT. 104422f70c32Sdrh */ 10454adee20fSdanielk1977 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ 104622f70c32Sdrh Table *pTab; 1047b733d037Sdrh int i, j; 104822f70c32Sdrh ExprList *pEList; 1049290c1948Sdrh Column *aCol, *pCol; 105022f70c32Sdrh 105192378253Sdrh while( pSelect->pPrior ) pSelect = pSelect->pPrior; 10529b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 105322f70c32Sdrh return 0; 105422f70c32Sdrh } 1055142bdf40Sdanielk1977 if( sqlite3SelectResolve(pParse, pSelect, 0) ){ 1056142bdf40Sdanielk1977 return 0; 1057142bdf40Sdanielk1977 } 105822f70c32Sdrh pTab = sqliteMalloc( sizeof(Table) ); 105922f70c32Sdrh if( pTab==0 ){ 106022f70c32Sdrh return 0; 106122f70c32Sdrh } 1062ed8a3bb1Sdrh pTab->nRef = 1; 106322f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 106422f70c32Sdrh pEList = pSelect->pEList; 106522f70c32Sdrh pTab->nCol = pEList->nExpr; 1066417be79cSdrh assert( pTab->nCol>0 ); 1067b733d037Sdrh pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 1068290c1948Sdrh for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){ 106979d5f63fSdrh Expr *p, *pR; 1070517eb646Sdanielk1977 char *zType; 107191bb0eedSdrh char *zName; 107279d5f63fSdrh char *zBasename; 1073b3bf556eSdanielk1977 CollSeq *pColl; 107479d5f63fSdrh int cnt; 1075b3bce662Sdanielk1977 NameContext sNC; 107679d5f63fSdrh 107779d5f63fSdrh /* Get an appropriate name for the column 107879d5f63fSdrh */ 107979d5f63fSdrh p = pEList->a[i].pExpr; 1080290c1948Sdrh assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 ); 108191bb0eedSdrh if( (zName = pEList->a[i].zName)!=0 ){ 108279d5f63fSdrh /* If the column contains an "AS <name>" phrase, use <name> as the name */ 108391bb0eedSdrh zName = sqliteStrDup(zName); 1084517eb646Sdanielk1977 }else if( p->op==TK_DOT 1085b733d037Sdrh && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ 108679d5f63fSdrh /* For columns of the from A.B use B as the name */ 108791bb0eedSdrh zName = sqlite3MPrintf("%T", &pR->token); 1088b733d037Sdrh }else if( p->span.z && p->span.z[0] ){ 108979d5f63fSdrh /* Use the original text of the column expression as its name */ 109091bb0eedSdrh zName = sqlite3MPrintf("%T", &p->span); 109122f70c32Sdrh }else{ 109279d5f63fSdrh /* If all else fails, make up a name */ 109391bb0eedSdrh zName = sqlite3MPrintf("column%d", i+1); 109422f70c32Sdrh } 109591bb0eedSdrh sqlite3Dequote(zName); 10969e12800dSdanielk1977 if( sqlite3MallocFailed() ){ 1097dd5b2fa5Sdrh sqliteFree(zName); 1098dd5b2fa5Sdrh sqlite3DeleteTable(0, pTab); 1099dd5b2fa5Sdrh return 0; 1100dd5b2fa5Sdrh } 110179d5f63fSdrh 110279d5f63fSdrh /* Make sure the column name is unique. If the name is not unique, 110379d5f63fSdrh ** append a integer to the name so that it becomes unique. 110479d5f63fSdrh */ 110579d5f63fSdrh zBasename = zName; 110679d5f63fSdrh for(j=cnt=0; j<i; j++){ 110779d5f63fSdrh if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ 110879d5f63fSdrh zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt); 110979d5f63fSdrh j = -1; 1110dd5b2fa5Sdrh if( zName==0 ) break; 111179d5f63fSdrh } 111279d5f63fSdrh } 111379d5f63fSdrh if( zBasename!=zName ){ 111479d5f63fSdrh sqliteFree(zBasename); 111579d5f63fSdrh } 111691bb0eedSdrh pCol->zName = zName; 1117e014a838Sdanielk1977 111879d5f63fSdrh /* Get the typename, type affinity, and collating sequence for the 111979d5f63fSdrh ** column. 112079d5f63fSdrh */ 1121c43e8be8Sdrh memset(&sNC, 0, sizeof(sNC)); 1122b3bce662Sdanielk1977 sNC.pSrcList = pSelect->pSrc; 1123955de52cSdanielk1977 zType = sqliteStrDup(columnType(&sNC, p, 0, 0, 0)); 1124290c1948Sdrh pCol->zType = zType; 1125c60e9b82Sdanielk1977 pCol->affinity = sqlite3ExprAffinity(p); 1126b3bf556eSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, p); 1127b3bf556eSdanielk1977 if( pColl ){ 1128e7259296Sdanielk1977 pCol->zColl = sqliteStrDup(pColl->zName); 11290202b29eSdanielk1977 } 113022f70c32Sdrh } 113122f70c32Sdrh pTab->iPKey = -1; 113222f70c32Sdrh return pTab; 113322f70c32Sdrh } 113422f70c32Sdrh 113522f70c32Sdrh /* 11369b3187e1Sdrh ** Prepare a SELECT statement for processing by doing the following 11379b3187e1Sdrh ** things: 1138d8bc7086Sdrh ** 11399b3187e1Sdrh ** (1) Make sure VDBE cursor numbers have been assigned to every 11409b3187e1Sdrh ** element of the FROM clause. 11419b3187e1Sdrh ** 11429b3187e1Sdrh ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that 11439b3187e1Sdrh ** defines FROM clause. When views appear in the FROM clause, 114463eb5f29Sdrh ** fill pTabList->a[].pSelect with a copy of the SELECT statement 114563eb5f29Sdrh ** that implements the view. A copy is made of the view's SELECT 114663eb5f29Sdrh ** statement so that we can freely modify or delete that statement 114763eb5f29Sdrh ** without worrying about messing up the presistent representation 114863eb5f29Sdrh ** of the view. 1149d8bc7086Sdrh ** 11509b3187e1Sdrh ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword 1151ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 1152ad2d8307Sdrh ** 11539b3187e1Sdrh ** (4) Scan the list of columns in the result set (pEList) looking 115454473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 115554473229Sdrh ** If found, expand each "*" to be every column in every table 115654473229Sdrh ** and TABLE.* to be every column in TABLE. 1157d8bc7086Sdrh ** 1158d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 1159d8bc7086Sdrh ** in pParse and return non-zero. 1160d8bc7086Sdrh */ 11619b3187e1Sdrh static int prepSelectStmt(Parse *pParse, Select *p){ 116254473229Sdrh int i, j, k, rc; 1163ad3cab52Sdrh SrcList *pTabList; 1164daffd0e5Sdrh ExprList *pEList; 1165290c1948Sdrh struct SrcList_item *pFrom; 1166daffd0e5Sdrh 11679e12800dSdanielk1977 if( p==0 || p->pSrc==0 || sqlite3MallocFailed() ){ 11686f7adc8aSdrh return 1; 11696f7adc8aSdrh } 1170daffd0e5Sdrh pTabList = p->pSrc; 1171daffd0e5Sdrh pEList = p->pEList; 1172d8bc7086Sdrh 11739b3187e1Sdrh /* Make sure cursor numbers have been assigned to all entries in 11749b3187e1Sdrh ** the FROM clause of the SELECT statement. 11759b3187e1Sdrh */ 11769b3187e1Sdrh sqlite3SrcListAssignCursors(pParse, p->pSrc); 11779b3187e1Sdrh 11789b3187e1Sdrh /* Look up every table named in the FROM clause of the select. If 11799b3187e1Sdrh ** an entry of the FROM clause is a subquery instead of a table or view, 11809b3187e1Sdrh ** then create a transient table structure to describe the subquery. 1181d8bc7086Sdrh */ 1182290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1183f0113000Sdanielk1977 Table *pTab; 11849b3187e1Sdrh if( pFrom->pTab!=0 ){ 11859b3187e1Sdrh /* This statement has already been prepared. There is no need 11869b3187e1Sdrh ** to go further. */ 11879b3187e1Sdrh assert( i==0 ); 1188d8bc7086Sdrh return 0; 1189d8bc7086Sdrh } 1190290c1948Sdrh if( pFrom->zName==0 ){ 119193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 119222f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 1193290c1948Sdrh assert( pFrom->pSelect!=0 ); 1194290c1948Sdrh if( pFrom->zAlias==0 ){ 119591bb0eedSdrh pFrom->zAlias = 119691bb0eedSdrh sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect); 1197ad2d8307Sdrh } 1198ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1199290c1948Sdrh pFrom->pTab = pTab = 1200290c1948Sdrh sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect); 120122f70c32Sdrh if( pTab==0 ){ 1202daffd0e5Sdrh return 1; 1203daffd0e5Sdrh } 12045cf590c1Sdrh /* The isTransient flag indicates that the Table structure has been 12055cf590c1Sdrh ** dynamically allocated and may be freed at any time. In other words, 12065cf590c1Sdrh ** pTab is not pointing to a persistent table structure that defines 12075cf590c1Sdrh ** part of the schema. */ 120822f70c32Sdrh pTab->isTransient = 1; 120993758c8dSdanielk1977 #endif 121022f70c32Sdrh }else{ 1211a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 1212ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1213290c1948Sdrh pFrom->pTab = pTab = 1214290c1948Sdrh sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); 1215a76b5dfcSdrh if( pTab==0 ){ 1216d8bc7086Sdrh return 1; 1217d8bc7086Sdrh } 1218ed8a3bb1Sdrh pTab->nRef++; 121993758c8dSdanielk1977 #ifndef SQLITE_OMIT_VIEW 1220a76b5dfcSdrh if( pTab->pSelect ){ 122163eb5f29Sdrh /* We reach here if the named table is a really a view */ 12224adee20fSdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 1223417be79cSdrh return 1; 1224417be79cSdrh } 1225290c1948Sdrh /* If pFrom->pSelect!=0 it means we are dealing with a 122663eb5f29Sdrh ** view within a view. The SELECT structure has already been 122763eb5f29Sdrh ** copied by the outer view so we can skip the copy step here 122863eb5f29Sdrh ** in the inner view. 122963eb5f29Sdrh */ 1230290c1948Sdrh if( pFrom->pSelect==0 ){ 1231290c1948Sdrh pFrom->pSelect = sqlite3SelectDup(pTab->pSelect); 1232a76b5dfcSdrh } 1233d8bc7086Sdrh } 123493758c8dSdanielk1977 #endif 123522f70c32Sdrh } 123663eb5f29Sdrh } 1237d8bc7086Sdrh 1238ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 1239ad2d8307Sdrh */ 1240ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 1241ad2d8307Sdrh 12427c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 124354473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 124454473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 12457c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 12467c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 12477c917d19Sdrh ** each one to the list of all columns in all tables. 124854473229Sdrh ** 124954473229Sdrh ** The first loop just checks to see if there are any "*" operators 125054473229Sdrh ** that need expanding. 1251d8bc7086Sdrh */ 12527c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 125354473229Sdrh Expr *pE = pEList->a[k].pExpr; 125454473229Sdrh if( pE->op==TK_ALL ) break; 125554473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 125654473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 12577c917d19Sdrh } 125854473229Sdrh rc = 0; 12597c917d19Sdrh if( k<pEList->nExpr ){ 126054473229Sdrh /* 126154473229Sdrh ** If we get here it means the result set contains one or more "*" 126254473229Sdrh ** operators that need to be expanded. Loop through each expression 126354473229Sdrh ** in the result set and expand them one by one. 126454473229Sdrh */ 12657c917d19Sdrh struct ExprList_item *a = pEList->a; 12667c917d19Sdrh ExprList *pNew = 0; 1267d70dc52dSdrh int flags = pParse->db->flags; 1268d70dc52dSdrh int longNames = (flags & SQLITE_FullColNames)!=0 && 1269d70dc52dSdrh (flags & SQLITE_ShortColNames)==0; 1270d70dc52dSdrh 12717c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 127254473229Sdrh Expr *pE = a[k].pExpr; 127354473229Sdrh if( pE->op!=TK_ALL && 127454473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 127554473229Sdrh /* This particular expression does not need to be expanded. 127654473229Sdrh */ 12774adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 1278261919ccSdanielk1977 if( pNew ){ 12797c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 1280261919ccSdanielk1977 }else{ 1281261919ccSdanielk1977 rc = 1; 1282261919ccSdanielk1977 } 12837c917d19Sdrh a[k].pExpr = 0; 12847c917d19Sdrh a[k].zName = 0; 12857c917d19Sdrh }else{ 128654473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 128754473229Sdrh ** expanded. */ 128854473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 1289cf55b7aeSdrh char *zTName; /* text of name of TABLE */ 129054473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 1291cf55b7aeSdrh zTName = sqlite3NameFromToken(&pE->pLeft->token); 129254473229Sdrh }else{ 1293cf55b7aeSdrh zTName = 0; 129454473229Sdrh } 1295290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1296290c1948Sdrh Table *pTab = pFrom->pTab; 1297290c1948Sdrh char *zTabName = pFrom->zAlias; 129854473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 129954473229Sdrh zTabName = pTab->zName; 130054473229Sdrh } 1301cf55b7aeSdrh if( zTName && (zTabName==0 || zTabName[0]==0 || 1302cf55b7aeSdrh sqlite3StrICmp(zTName, zTabName)!=0) ){ 130354473229Sdrh continue; 130454473229Sdrh } 130554473229Sdrh tableSeen = 1; 1306d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 1307f0113000Sdanielk1977 Expr *pExpr, *pRight; 1308ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 1309ad2d8307Sdrh 131091bb0eedSdrh if( i>0 ){ 131191bb0eedSdrh struct SrcList_item *pLeft = &pTabList->a[i-1]; 131291bb0eedSdrh if( (pLeft->jointype & JT_NATURAL)!=0 && 131391bb0eedSdrh columnIndex(pLeft->pTab, zName)>=0 ){ 1314ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 1315ad2d8307Sdrh ** table on the right */ 1316ad2d8307Sdrh continue; 1317ad2d8307Sdrh } 131891bb0eedSdrh if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ 1319ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 1320ad2d8307Sdrh ** using clause from the table on the right. */ 1321ad2d8307Sdrh continue; 1322ad2d8307Sdrh } 132391bb0eedSdrh } 13244adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 132522f70c32Sdrh if( pRight==0 ) break; 132691bb0eedSdrh setToken(&pRight->token, zName); 1327d70dc52dSdrh if( zTabName && (longNames || pTabList->nSrc>1) ){ 1328f0113000Sdanielk1977 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 13294adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 133022f70c32Sdrh if( pExpr==0 ) break; 133191bb0eedSdrh setToken(&pLeft->token, zTabName); 133291bb0eedSdrh setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); 13336977fea8Sdrh pExpr->span.dyn = 1; 13346977fea8Sdrh pExpr->token.z = 0; 13356977fea8Sdrh pExpr->token.n = 0; 13366977fea8Sdrh pExpr->token.dyn = 0; 133722f70c32Sdrh }else{ 133822f70c32Sdrh pExpr = pRight; 13396977fea8Sdrh pExpr->span = pExpr->token; 134022f70c32Sdrh } 1341d70dc52dSdrh if( longNames ){ 1342d70dc52dSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); 1343d70dc52dSdrh }else{ 134479d5f63fSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); 1345d8bc7086Sdrh } 1346d8bc7086Sdrh } 1347d70dc52dSdrh } 134854473229Sdrh if( !tableSeen ){ 1349cf55b7aeSdrh if( zTName ){ 1350cf55b7aeSdrh sqlite3ErrorMsg(pParse, "no such table: %s", zTName); 1351f5db2d3eSdrh }else{ 13524adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1353f5db2d3eSdrh } 135454473229Sdrh rc = 1; 135554473229Sdrh } 1356cf55b7aeSdrh sqliteFree(zTName); 13577c917d19Sdrh } 13587c917d19Sdrh } 13594adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 13607c917d19Sdrh p->pEList = pNew; 1361d8bc7086Sdrh } 136254473229Sdrh return rc; 1363d8bc7086Sdrh } 1364d8bc7086Sdrh 136593758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 1366ff78bd2fSdrh /* 1367d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1368d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1369967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1370d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1371d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1372d8bc7086Sdrh ** 1373d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1374d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1375d8bc7086Sdrh ** 1376d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1377d8bc7086Sdrh ** of errors is returned. 1378d8bc7086Sdrh */ 1379d8bc7086Sdrh static int matchOrderbyToColumn( 1380d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1381d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1382d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1383e4de1febSdrh int iTable, /* Insert this value in iTable */ 1384d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1385d8bc7086Sdrh ){ 1386d8bc7086Sdrh int nErr = 0; 1387d8bc7086Sdrh int i, j; 1388d8bc7086Sdrh ExprList *pEList; 1389d8bc7086Sdrh 1390daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1391d8bc7086Sdrh if( mustComplete ){ 1392d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1393d8bc7086Sdrh } 13949b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 1395d8bc7086Sdrh return 1; 1396d8bc7086Sdrh } 1397d8bc7086Sdrh if( pSelect->pPrior ){ 139892cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 139992cd52f5Sdrh return 1; 140092cd52f5Sdrh } 1401d8bc7086Sdrh } 1402d8bc7086Sdrh pEList = pSelect->pEList; 1403d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1404d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1405e4de1febSdrh int iCol = -1; 1406d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 14074adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1408e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 14094adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1410da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1411e4de1febSdrh iCol, pEList->nExpr); 1412e4de1febSdrh nErr++; 1413e4de1febSdrh break; 1414e4de1febSdrh } 1415fcb78a49Sdrh if( !mustComplete ) continue; 1416e4de1febSdrh iCol--; 1417e4de1febSdrh } 1418e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 14194cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1420a76b5dfcSdrh char *zName, *zLabel; 1421a76b5dfcSdrh zName = pEList->a[j].zName; 1422a99db3b6Sdrh zLabel = sqlite3NameFromToken(&pE->token); 1423a99db3b6Sdrh assert( zLabel!=0 ); 14244adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1425e4de1febSdrh iCol = j; 1426d8bc7086Sdrh } 14276e142f54Sdrh sqliteFree(zLabel); 1428d8bc7086Sdrh } 14294adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1430e4de1febSdrh iCol = j; 1431d8bc7086Sdrh } 1432e4de1febSdrh } 1433e4de1febSdrh if( iCol>=0 ){ 1434967e8b73Sdrh pE->op = TK_COLUMN; 1435e4de1febSdrh pE->iColumn = iCol; 1436d8bc7086Sdrh pE->iTable = iTable; 1437a58fdfb1Sdanielk1977 pE->iAgg = -1; 1438d8bc7086Sdrh pOrderBy->a[i].done = 1; 1439d8bc7086Sdrh } 1440e4de1febSdrh if( iCol<0 && mustComplete ){ 14414adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1442da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1443d8bc7086Sdrh nErr++; 1444d8bc7086Sdrh break; 1445d8bc7086Sdrh } 1446d8bc7086Sdrh } 1447d8bc7086Sdrh return nErr; 1448d8bc7086Sdrh } 144993758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */ 1450d8bc7086Sdrh 1451d8bc7086Sdrh /* 1452d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1453d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1454d8bc7086Sdrh */ 14554adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1456d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1457d8bc7086Sdrh if( v==0 ){ 14584adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1459d8bc7086Sdrh } 1460d8bc7086Sdrh return v; 1461d8bc7086Sdrh } 1462d8bc7086Sdrh 146315007a99Sdrh 1464d8bc7086Sdrh /* 14657b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 1466ec7429aeSdrh ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions 14677b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 1468a2dc3b1aSdanielk1977 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset 1469a2dc3b1aSdanielk1977 ** are the integer memory register numbers for counters used to compute 1470a2dc3b1aSdanielk1977 ** the limit and offset. If there is no limit and/or offset, then 1471a2dc3b1aSdanielk1977 ** iLimit and iOffset are negative. 14727b58daeaSdrh ** 1473d59ba6ceSdrh ** This routine changes the values of iLimit and iOffset only if 1474ec7429aeSdrh ** a limit or offset is defined by pLimit and pOffset. iLimit and 14757b58daeaSdrh ** iOffset should have been preset to appropriate default values 14767b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 1477ec7429aeSdrh ** Only if pLimit!=0 or pOffset!=0 do the limit registers get 14787b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 14797b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 14807b58daeaSdrh ** SELECT statements. 14817b58daeaSdrh */ 1482ec7429aeSdrh static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ 148302afc861Sdrh Vdbe *v = 0; 148402afc861Sdrh int iLimit = 0; 148515007a99Sdrh int iOffset; 148615007a99Sdrh int addr1, addr2; 148715007a99Sdrh 14887b58daeaSdrh /* 14897b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 14907b58daeaSdrh ** contraversy about what the correct behavior should be. 14917b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 14927b58daeaSdrh ** no rows. 14937b58daeaSdrh */ 1494a2dc3b1aSdanielk1977 if( p->pLimit ){ 149515007a99Sdrh p->iLimit = iLimit = pParse->nMem; 1496d59ba6ceSdrh pParse->nMem += 2; 149715007a99Sdrh v = sqlite3GetVdbe(pParse); 14987b58daeaSdrh if( v==0 ) return; 1499a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pLimit); 1500a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 150115007a99Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 0); 1502ad6d9460Sdrh VdbeComment((v, "# LIMIT counter")); 150315007a99Sdrh sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak); 15047b58daeaSdrh } 1505a2dc3b1aSdanielk1977 if( p->pOffset ){ 150615007a99Sdrh p->iOffset = iOffset = pParse->nMem++; 150715007a99Sdrh v = sqlite3GetVdbe(pParse); 15087b58daeaSdrh if( v==0 ) return; 1509a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pOffset); 1510a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 151115007a99Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iOffset, p->pLimit==0); 1512ad6d9460Sdrh VdbeComment((v, "# OFFSET counter")); 151315007a99Sdrh addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iOffset, 0); 151415007a99Sdrh sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 151515007a99Sdrh sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 151615007a99Sdrh sqlite3VdbeJumpHere(v, addr1); 1517d59ba6ceSdrh if( p->pLimit ){ 1518d59ba6ceSdrh sqlite3VdbeAddOp(v, OP_Add, 0, 0); 1519d59ba6ceSdrh } 15207b58daeaSdrh } 1521d59ba6ceSdrh if( p->pLimit ){ 152215007a99Sdrh addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iLimit, 0); 152315007a99Sdrh sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 152415007a99Sdrh sqlite3VdbeAddOp(v, OP_MemInt, -1, iLimit+1); 152515007a99Sdrh addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 152615007a99Sdrh sqlite3VdbeJumpHere(v, addr1); 152715007a99Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iLimit+1, 1); 1528d59ba6ceSdrh VdbeComment((v, "# LIMIT+OFFSET")); 152915007a99Sdrh sqlite3VdbeJumpHere(v, addr2); 1530d59ba6ceSdrh } 15317b58daeaSdrh } 15327b58daeaSdrh 15337b58daeaSdrh /* 15340342b1f5Sdrh ** Allocate a virtual index to use for sorting. 1535d3d39e93Sdrh */ 15364db38a70Sdrh static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){ 15370342b1f5Sdrh if( pOrderBy ){ 1538dc1bdc4fSdanielk1977 int addr; 15399d2985c7Sdrh assert( pOrderBy->iECursor==0 ); 15409d2985c7Sdrh pOrderBy->iECursor = pParse->nTab++; 15410342b1f5Sdrh addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenVirtual, 15429d2985c7Sdrh pOrderBy->iECursor, pOrderBy->nExpr+1); 15430342b1f5Sdrh assert( p->addrOpenVirt[2] == -1 ); 15440342b1f5Sdrh p->addrOpenVirt[2] = addr; 1545736c22b8Sdrh } 1546dc1bdc4fSdanielk1977 } 1547dc1bdc4fSdanielk1977 1548b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1549fbc4ee7bSdrh /* 1550fbc4ee7bSdrh ** Return the appropriate collating sequence for the iCol-th column of 1551fbc4ee7bSdrh ** the result set for the compound-select statement "p". Return NULL if 1552fbc4ee7bSdrh ** the column has no default collating sequence. 1553fbc4ee7bSdrh ** 1554fbc4ee7bSdrh ** The collating sequence for the compound select is taken from the 1555fbc4ee7bSdrh ** left-most term of the select that has a collating sequence. 1556fbc4ee7bSdrh */ 1557dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ 1558fbc4ee7bSdrh CollSeq *pRet; 1559dc1bdc4fSdanielk1977 if( p->pPrior ){ 1560dc1bdc4fSdanielk1977 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); 1561fbc4ee7bSdrh }else{ 1562fbc4ee7bSdrh pRet = 0; 1563dc1bdc4fSdanielk1977 } 1564fbc4ee7bSdrh if( pRet==0 ){ 1565dc1bdc4fSdanielk1977 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); 1566dc1bdc4fSdanielk1977 } 1567dc1bdc4fSdanielk1977 return pRet; 1568d3d39e93Sdrh } 1569b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1570d3d39e93Sdrh 1571b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1572d3d39e93Sdrh /* 157382c3d636Sdrh ** This routine is called to process a query that is really the union 157482c3d636Sdrh ** or intersection of two or more separate queries. 1575c926afbcSdrh ** 1576e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1577e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1578e78e8284Sdrh ** in which case this routine will be called recursively. 1579e78e8284Sdrh ** 1580e78e8284Sdrh ** The results of the total query are to be written into a destination 1581e78e8284Sdrh ** of type eDest with parameter iParm. 1582e78e8284Sdrh ** 1583e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1584e78e8284Sdrh ** 1585e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1586e78e8284Sdrh ** 1587e78e8284Sdrh ** This statement is parsed up as follows: 1588e78e8284Sdrh ** 1589e78e8284Sdrh ** SELECT c FROM t3 1590e78e8284Sdrh ** | 1591e78e8284Sdrh ** `-----> SELECT b FROM t2 1592e78e8284Sdrh ** | 15934b11c6d3Sjplyon ** `------> SELECT a FROM t1 1594e78e8284Sdrh ** 1595e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1596e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1597e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1598e78e8284Sdrh ** 1599e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1600e78e8284Sdrh ** individual selects always group from left to right. 160182c3d636Sdrh */ 160284ac9d02Sdanielk1977 static int multiSelect( 1603fbc4ee7bSdrh Parse *pParse, /* Parsing context */ 1604fbc4ee7bSdrh Select *p, /* The right-most of SELECTs to be coded */ 1605fbc4ee7bSdrh int eDest, /* \___ Store query results as specified */ 1606fbc4ee7bSdrh int iParm, /* / by these two parameters. */ 160784ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 160884ac9d02Sdanielk1977 ){ 160984ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 161010e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 161110e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 16128cdbf836Sdrh int nCol; /* Number of columns in the result set */ 16130342b1f5Sdrh ExprList *pOrderBy; /* The ORDER BY clause on p */ 16140342b1f5Sdrh int aSetP2[2]; /* Set P2 value of these op to number of columns */ 16150342b1f5Sdrh int nSetP2 = 0; /* Number of slots in aSetP2[] used */ 161682c3d636Sdrh 16177b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 1618fbc4ee7bSdrh ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. 161982c3d636Sdrh */ 162084ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 162184ac9d02Sdanielk1977 rc = 1; 162284ac9d02Sdanielk1977 goto multi_select_end; 162384ac9d02Sdanielk1977 } 1624d8bc7086Sdrh pPrior = p->pPrior; 16250342b1f5Sdrh assert( pPrior->pRightmost!=pPrior ); 16260342b1f5Sdrh assert( pPrior->pRightmost==p->pRightmost ); 1627d8bc7086Sdrh if( pPrior->pOrderBy ){ 16284adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1629da93d238Sdrh selectOpName(p->op)); 163084ac9d02Sdanielk1977 rc = 1; 163184ac9d02Sdanielk1977 goto multi_select_end; 163282c3d636Sdrh } 1633a2dc3b1aSdanielk1977 if( pPrior->pLimit ){ 16344adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 16357b58daeaSdrh selectOpName(p->op)); 163684ac9d02Sdanielk1977 rc = 1; 163784ac9d02Sdanielk1977 goto multi_select_end; 16387b58daeaSdrh } 163982c3d636Sdrh 1640d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1641d8bc7086Sdrh */ 16424adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 164384ac9d02Sdanielk1977 if( v==0 ){ 164484ac9d02Sdanielk1977 rc = 1; 164584ac9d02Sdanielk1977 goto multi_select_end; 164684ac9d02Sdanielk1977 } 1647d8bc7086Sdrh 16481cc3d75fSdrh /* Create the destination temporary table if necessary 16491cc3d75fSdrh */ 16509d2985c7Sdrh if( eDest==SRT_VirtualTab ){ 1651b4964b72Sdanielk1977 assert( p->pEList ); 16520342b1f5Sdrh assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); 16530342b1f5Sdrh aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenVirtual, iParm, 0); 16541cc3d75fSdrh eDest = SRT_Table; 16551cc3d75fSdrh } 16561cc3d75fSdrh 1657f46f905aSdrh /* Generate code for the left and right SELECT statements. 1658d8bc7086Sdrh */ 16590342b1f5Sdrh pOrderBy = p->pOrderBy; 166082c3d636Sdrh switch( p->op ){ 1661f46f905aSdrh case TK_ALL: { 16620342b1f5Sdrh if( pOrderBy==0 ){ 1663ec7429aeSdrh int addr = 0; 1664a2dc3b1aSdanielk1977 assert( !pPrior->pLimit ); 1665a2dc3b1aSdanielk1977 pPrior->pLimit = p->pLimit; 1666a2dc3b1aSdanielk1977 pPrior->pOffset = p->pOffset; 1667b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 1668ad68cb6bSdanielk1977 p->pLimit = 0; 1669ad68cb6bSdanielk1977 p->pOffset = 0; 167084ac9d02Sdanielk1977 if( rc ){ 167184ac9d02Sdanielk1977 goto multi_select_end; 167284ac9d02Sdanielk1977 } 1673f46f905aSdrh p->pPrior = 0; 16747b58daeaSdrh p->iLimit = pPrior->iLimit; 16757b58daeaSdrh p->iOffset = pPrior->iOffset; 1676ec7429aeSdrh if( p->iLimit>=0 ){ 1677ec7429aeSdrh addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0); 1678ec7429aeSdrh VdbeComment((v, "# Jump ahead if LIMIT reached")); 1679ec7429aeSdrh } 1680b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1681f46f905aSdrh p->pPrior = pPrior; 168284ac9d02Sdanielk1977 if( rc ){ 168384ac9d02Sdanielk1977 goto multi_select_end; 168484ac9d02Sdanielk1977 } 1685ec7429aeSdrh if( addr ){ 1686ec7429aeSdrh sqlite3VdbeJumpHere(v, addr); 1687ec7429aeSdrh } 1688f46f905aSdrh break; 1689f46f905aSdrh } 1690f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1691f46f905aSdrh } 169282c3d636Sdrh case TK_EXCEPT: 169382c3d636Sdrh case TK_UNION: { 1694d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1695742f947bSdanielk1977 int op = 0; /* One of the SRT_ operations to apply to self */ 1696d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1697a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ 1698dc1bdc4fSdanielk1977 int addr; 169982c3d636Sdrh 1700d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 17010342b1f5Sdrh if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){ 1702d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1703c926afbcSdrh ** right. 1704d8bc7086Sdrh */ 170582c3d636Sdrh unionTab = iParm; 170682c3d636Sdrh }else{ 1707d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1708d8bc7086Sdrh ** intermediate results. 1709d8bc7086Sdrh */ 171082c3d636Sdrh unionTab = pParse->nTab++; 17110342b1f5Sdrh if( pOrderBy && matchOrderbyToColumn(pParse, p, pOrderBy, unionTab,1) ){ 171284ac9d02Sdanielk1977 rc = 1; 171384ac9d02Sdanielk1977 goto multi_select_end; 1714d8bc7086Sdrh } 17159170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, unionTab, 0); 17160342b1f5Sdrh if( priorOp==SRT_Table ){ 17170342b1f5Sdrh assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); 17180342b1f5Sdrh aSetP2[nSetP2++] = addr; 17190342b1f5Sdrh }else{ 17200342b1f5Sdrh assert( p->addrOpenVirt[0] == -1 ); 17210342b1f5Sdrh p->addrOpenVirt[0] = addr; 17220342b1f5Sdrh p->pRightmost->usesVirt = 1; 1723dc1bdc4fSdanielk1977 } 17240342b1f5Sdrh createSortingIndex(pParse, p, pOrderBy); 172584ac9d02Sdanielk1977 assert( p->pEList ); 1726d8bc7086Sdrh } 1727d8bc7086Sdrh 1728d8bc7086Sdrh /* Code the SELECT statements to our left 1729d8bc7086Sdrh */ 1730b3bce662Sdanielk1977 assert( !pPrior->pOrderBy ); 1731b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 173284ac9d02Sdanielk1977 if( rc ){ 173384ac9d02Sdanielk1977 goto multi_select_end; 173484ac9d02Sdanielk1977 } 1735d8bc7086Sdrh 1736d8bc7086Sdrh /* Code the current SELECT statement 1737d8bc7086Sdrh */ 1738d8bc7086Sdrh switch( p->op ){ 1739d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1740d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1741d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1742d8bc7086Sdrh } 174382c3d636Sdrh p->pPrior = 0; 1744c926afbcSdrh p->pOrderBy = 0; 17454b14b4d7Sdrh p->disallowOrderBy = pOrderBy!=0; 1746a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1747a2dc3b1aSdanielk1977 p->pLimit = 0; 1748a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1749a2dc3b1aSdanielk1977 p->pOffset = 0; 1750b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 175182c3d636Sdrh p->pPrior = pPrior; 1752c926afbcSdrh p->pOrderBy = pOrderBy; 1753a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1754a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1755a2dc3b1aSdanielk1977 p->pOffset = pOffset; 1756be5fd490Sdrh p->iLimit = -1; 1757be5fd490Sdrh p->iOffset = -1; 175884ac9d02Sdanielk1977 if( rc ){ 175984ac9d02Sdanielk1977 goto multi_select_end; 176084ac9d02Sdanielk1977 } 176184ac9d02Sdanielk1977 1762d8bc7086Sdrh 1763d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1764d8bc7086Sdrh ** it is that we currently need. 1765d8bc7086Sdrh */ 1766c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 17676b56344dSdrh int iCont, iBreak, iStart; 176882c3d636Sdrh assert( p->pEList ); 176941202ccaSdrh if( eDest==SRT_Callback ){ 177092378253Sdrh Select *pFirst = p; 177192378253Sdrh while( pFirst->pPrior ) pFirst = pFirst->pPrior; 177292378253Sdrh generateColumnNames(pParse, 0, pFirst->pEList); 177341202ccaSdrh } 17744adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 17754adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 1776ec7429aeSdrh computeLimitRegisters(pParse, p, iBreak); 17774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 17784adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 177938640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 17800342b1f5Sdrh pOrderBy, -1, eDest, iParm, 178184ac9d02Sdanielk1977 iCont, iBreak, 0); 178284ac9d02Sdanielk1977 if( rc ){ 178384ac9d02Sdanielk1977 rc = 1; 178484ac9d02Sdanielk1977 goto multi_select_end; 178584ac9d02Sdanielk1977 } 17864adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 17874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 17884adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 17894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 179082c3d636Sdrh } 179182c3d636Sdrh break; 179282c3d636Sdrh } 179382c3d636Sdrh case TK_INTERSECT: { 179482c3d636Sdrh int tab1, tab2; 17956b56344dSdrh int iCont, iBreak, iStart; 1796a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; 1797dc1bdc4fSdanielk1977 int addr; 179882c3d636Sdrh 1799d8bc7086Sdrh /* INTERSECT is different from the others since it requires 18006206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1801d8bc7086Sdrh ** by allocating the tables we will need. 1802d8bc7086Sdrh */ 180382c3d636Sdrh tab1 = pParse->nTab++; 180482c3d636Sdrh tab2 = pParse->nTab++; 18050342b1f5Sdrh if( pOrderBy && matchOrderbyToColumn(pParse,p,pOrderBy,tab1,1) ){ 180684ac9d02Sdanielk1977 rc = 1; 180784ac9d02Sdanielk1977 goto multi_select_end; 1808d8bc7086Sdrh } 18090342b1f5Sdrh createSortingIndex(pParse, p, pOrderBy); 1810dc1bdc4fSdanielk1977 18119170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, tab1, 0); 18120342b1f5Sdrh assert( p->addrOpenVirt[0] == -1 ); 18130342b1f5Sdrh p->addrOpenVirt[0] = addr; 18140342b1f5Sdrh p->pRightmost->usesVirt = 1; 181584ac9d02Sdanielk1977 assert( p->pEList ); 1816d8bc7086Sdrh 1817d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1818d8bc7086Sdrh */ 1819b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 182084ac9d02Sdanielk1977 if( rc ){ 182184ac9d02Sdanielk1977 goto multi_select_end; 182284ac9d02Sdanielk1977 } 1823d8bc7086Sdrh 1824d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1825d8bc7086Sdrh */ 18269170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, tab2, 0); 18270342b1f5Sdrh assert( p->addrOpenVirt[1] == -1 ); 18280342b1f5Sdrh p->addrOpenVirt[1] = addr; 182982c3d636Sdrh p->pPrior = 0; 1830a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1831a2dc3b1aSdanielk1977 p->pLimit = 0; 1832a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1833a2dc3b1aSdanielk1977 p->pOffset = 0; 1834b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 183582c3d636Sdrh p->pPrior = pPrior; 1836a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1837a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1838a2dc3b1aSdanielk1977 p->pOffset = pOffset; 183984ac9d02Sdanielk1977 if( rc ){ 184084ac9d02Sdanielk1977 goto multi_select_end; 184184ac9d02Sdanielk1977 } 1842d8bc7086Sdrh 1843d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1844d8bc7086Sdrh ** tables. 1845d8bc7086Sdrh */ 184682c3d636Sdrh assert( p->pEList ); 184741202ccaSdrh if( eDest==SRT_Callback ){ 184892378253Sdrh Select *pFirst = p; 184992378253Sdrh while( pFirst->pPrior ) pFirst = pFirst->pPrior; 185092378253Sdrh generateColumnNames(pParse, 0, pFirst->pEList); 185141202ccaSdrh } 18524adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 18534adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 1854ec7429aeSdrh computeLimitRegisters(pParse, p, iBreak); 18554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 18564a9f241cSdrh iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0); 18574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 185838640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 18590342b1f5Sdrh pOrderBy, -1, eDest, iParm, 186084ac9d02Sdanielk1977 iCont, iBreak, 0); 186184ac9d02Sdanielk1977 if( rc ){ 186284ac9d02Sdanielk1977 rc = 1; 186384ac9d02Sdanielk1977 goto multi_select_end; 186484ac9d02Sdanielk1977 } 18654adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 18664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 18674adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 18684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 18694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 187082c3d636Sdrh break; 187182c3d636Sdrh } 187282c3d636Sdrh } 18738cdbf836Sdrh 18748cdbf836Sdrh /* Make sure all SELECTs in the statement have the same number of elements 18758cdbf836Sdrh ** in their result sets. 18768cdbf836Sdrh */ 187782c3d636Sdrh assert( p->pEList && pPrior->pEList ); 187882c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 18794adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1880da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 188184ac9d02Sdanielk1977 rc = 1; 188284ac9d02Sdanielk1977 goto multi_select_end; 18832282792aSdrh } 188484ac9d02Sdanielk1977 18858cdbf836Sdrh /* Set the number of columns in temporary tables 18868cdbf836Sdrh */ 18878cdbf836Sdrh nCol = p->pEList->nExpr; 18880342b1f5Sdrh while( nSetP2 ){ 18890342b1f5Sdrh sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol); 18908cdbf836Sdrh } 18918cdbf836Sdrh 1892fbc4ee7bSdrh /* Compute collating sequences used by either the ORDER BY clause or 1893fbc4ee7bSdrh ** by any temporary tables needed to implement the compound select. 1894fbc4ee7bSdrh ** Attach the KeyInfo structure to all temporary tables. Invoke the 1895fbc4ee7bSdrh ** ORDER BY processing if there is an ORDER BY clause. 18968cdbf836Sdrh ** 18978cdbf836Sdrh ** This section is run by the right-most SELECT statement only. 18988cdbf836Sdrh ** SELECT statements to the left always skip this part. The right-most 18998cdbf836Sdrh ** SELECT might also skip this part if it has no ORDER BY clause and 19008cdbf836Sdrh ** no temp tables are required. 1901fbc4ee7bSdrh */ 19020342b1f5Sdrh if( pOrderBy || p->usesVirt ){ 1903fbc4ee7bSdrh int i; /* Loop counter */ 1904fbc4ee7bSdrh KeyInfo *pKeyInfo; /* Collating sequence for the result set */ 19050342b1f5Sdrh Select *pLoop; /* For looping through SELECT statements */ 19060342b1f5Sdrh CollSeq **apColl; 19070342b1f5Sdrh CollSeq **aCopy; 1908fbc4ee7bSdrh 19090342b1f5Sdrh assert( p->pRightmost==p ); 19104db38a70Sdrh pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*2*sizeof(CollSeq*) + nCol); 1911dc1bdc4fSdanielk1977 if( !pKeyInfo ){ 1912dc1bdc4fSdanielk1977 rc = SQLITE_NOMEM; 1913dc1bdc4fSdanielk1977 goto multi_select_end; 1914dc1bdc4fSdanielk1977 } 1915dc1bdc4fSdanielk1977 191614db2665Sdanielk1977 pKeyInfo->enc = ENC(pParse->db); 1917dc1bdc4fSdanielk1977 pKeyInfo->nField = nCol; 1918dc1bdc4fSdanielk1977 19190342b1f5Sdrh for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){ 19200342b1f5Sdrh *apColl = multiSelectCollSeq(pParse, p, i); 19210342b1f5Sdrh if( 0==*apColl ){ 19220342b1f5Sdrh *apColl = pParse->db->pDfltColl; 1923dc1bdc4fSdanielk1977 } 1924dc1bdc4fSdanielk1977 } 1925dc1bdc4fSdanielk1977 19260342b1f5Sdrh for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ 19270342b1f5Sdrh for(i=0; i<2; i++){ 19280342b1f5Sdrh int addr = pLoop->addrOpenVirt[i]; 19290342b1f5Sdrh if( addr<0 ){ 19300342b1f5Sdrh /* If [0] is unused then [1] is also unused. So we can 19310342b1f5Sdrh ** always safely abort as soon as the first unused slot is found */ 19320342b1f5Sdrh assert( pLoop->addrOpenVirt[1]<0 ); 19330342b1f5Sdrh break; 19340342b1f5Sdrh } 19350342b1f5Sdrh sqlite3VdbeChangeP2(v, addr, nCol); 19360342b1f5Sdrh sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO); 19370342b1f5Sdrh } 1938dc1bdc4fSdanielk1977 } 1939dc1bdc4fSdanielk1977 19400342b1f5Sdrh if( pOrderBy ){ 19410342b1f5Sdrh struct ExprList_item *pOTerm = pOrderBy->a; 19424efc083fSdrh int nOrderByExpr = pOrderBy->nExpr; 19430342b1f5Sdrh int addr; 19444db38a70Sdrh u8 *pSortOrder; 19450342b1f5Sdrh 1946a86a5b6cSdrh aCopy = &pKeyInfo->aColl[nCol]; 19474efc083fSdrh pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol]; 19480342b1f5Sdrh memcpy(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*)); 19490342b1f5Sdrh apColl = pKeyInfo->aColl; 19504efc083fSdrh for(i=0; i<nOrderByExpr; i++, pOTerm++, apColl++, pSortOrder++){ 19510342b1f5Sdrh Expr *pExpr = pOTerm->pExpr; 19520342b1f5Sdrh char *zName = pOTerm->zName; 1953dc1bdc4fSdanielk1977 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); 1954dc1bdc4fSdanielk1977 if( zName ){ 19550342b1f5Sdrh *apColl = sqlite3LocateCollSeq(pParse, zName, -1); 195684ac9d02Sdanielk1977 }else{ 19570342b1f5Sdrh *apColl = aCopy[pExpr->iColumn]; 195884ac9d02Sdanielk1977 } 19594db38a70Sdrh *pSortOrder = pOTerm->sortOrder; 196084ac9d02Sdanielk1977 } 19610342b1f5Sdrh assert( p->pRightmost==p ); 19620342b1f5Sdrh assert( p->addrOpenVirt[2]>=0 ); 19630342b1f5Sdrh addr = p->addrOpenVirt[2]; 19644db38a70Sdrh sqlite3VdbeChangeP2(v, addr, p->pEList->nExpr+2); 19654efc083fSdrh pKeyInfo->nField = nOrderByExpr; 19664db38a70Sdrh sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 19674db38a70Sdrh pKeyInfo = 0; 1968cdd536f0Sdrh generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1969dc1bdc4fSdanielk1977 } 1970dc1bdc4fSdanielk1977 1971dc1bdc4fSdanielk1977 sqliteFree(pKeyInfo); 1972dc1bdc4fSdanielk1977 } 1973dc1bdc4fSdanielk1977 1974dc1bdc4fSdanielk1977 multi_select_end: 197584ac9d02Sdanielk1977 return rc; 19762282792aSdrh } 1977b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 19782282792aSdrh 1979b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 19802282792aSdrh /* 1981832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 19826a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 198384e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 19846a3ea0e6Sdrh ** unchanged.) 1985832508b7Sdrh ** 1986832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1987832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1988832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1989832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1990832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1991832508b7Sdrh ** of the subquery rather the result set of the subquery. 1992832508b7Sdrh */ 19936a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 1994b3bce662Sdanielk1977 static void substSelect(Select *, int, ExprList *); /* Forward Decl */ 19956a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1996832508b7Sdrh if( pExpr==0 ) return; 199750350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 199850350a15Sdrh if( pExpr->iColumn<0 ){ 199950350a15Sdrh pExpr->op = TK_NULL; 200050350a15Sdrh }else{ 2001832508b7Sdrh Expr *pNew; 200284e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 2003832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 2004832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 2005832508b7Sdrh assert( pNew!=0 ); 2006832508b7Sdrh pExpr->op = pNew->op; 2007d94a6698Sdrh assert( pExpr->pLeft==0 ); 20084adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 2009d94a6698Sdrh assert( pExpr->pRight==0 ); 20104adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 2011d94a6698Sdrh assert( pExpr->pList==0 ); 20124adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 2013832508b7Sdrh pExpr->iTable = pNew->iTable; 2014832508b7Sdrh pExpr->iColumn = pNew->iColumn; 2015832508b7Sdrh pExpr->iAgg = pNew->iAgg; 20164adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 20174adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 2018a1cb183dSdanielk1977 pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); 2019a1cb183dSdanielk1977 pExpr->flags = pNew->flags; 202050350a15Sdrh } 2021832508b7Sdrh }else{ 20226a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 20236a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 2024b3bce662Sdanielk1977 substSelect(pExpr->pSelect, iTable, pEList); 20256a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 2026832508b7Sdrh } 2027832508b7Sdrh } 2028b3bce662Sdanielk1977 static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ 2029832508b7Sdrh int i; 2030832508b7Sdrh if( pList==0 ) return; 2031832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 20326a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 2033832508b7Sdrh } 2034832508b7Sdrh } 2035b3bce662Sdanielk1977 static void substSelect(Select *p, int iTable, ExprList *pEList){ 2036b3bce662Sdanielk1977 if( !p ) return; 2037b3bce662Sdanielk1977 substExprList(p->pEList, iTable, pEList); 2038b3bce662Sdanielk1977 substExprList(p->pGroupBy, iTable, pEList); 2039b3bce662Sdanielk1977 substExprList(p->pOrderBy, iTable, pEList); 2040b3bce662Sdanielk1977 substExpr(p->pHaving, iTable, pEList); 2041b3bce662Sdanielk1977 substExpr(p->pWhere, iTable, pEList); 2042b3bce662Sdanielk1977 } 2043b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_VIEW) */ 2044832508b7Sdrh 2045b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 2046832508b7Sdrh /* 20471350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 20481350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 20491350b030Sdrh ** occurs. 20501350b030Sdrh ** 20511350b030Sdrh ** To understand the concept of flattening, consider the following 20521350b030Sdrh ** query: 20531350b030Sdrh ** 20541350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 20551350b030Sdrh ** 20561350b030Sdrh ** The default way of implementing this query is to execute the 20571350b030Sdrh ** subquery first and store the results in a temporary table, then 20581350b030Sdrh ** run the outer query on that temporary table. This requires two 20591350b030Sdrh ** passes over the data. Furthermore, because the temporary table 20601350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 2061832508b7Sdrh ** optimized. 20621350b030Sdrh ** 2063832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 20641350b030Sdrh ** a single flat select, like this: 20651350b030Sdrh ** 20661350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 20671350b030Sdrh ** 20681350b030Sdrh ** The code generated for this simpification gives the same result 2069832508b7Sdrh ** but only has to scan the data once. And because indices might 2070832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 2071832508b7Sdrh ** avoided. 20721350b030Sdrh ** 2073832508b7Sdrh ** Flattening is only attempted if all of the following are true: 20741350b030Sdrh ** 2075832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 20761350b030Sdrh ** 2077832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 2078832508b7Sdrh ** 20798af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 20808af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 2081832508b7Sdrh ** 2082832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 2083832508b7Sdrh ** 2084832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 2085832508b7Sdrh ** aggregates. 2086832508b7Sdrh ** 2087832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 2088832508b7Sdrh ** DISTINCT. 2089832508b7Sdrh ** 209008192d5fSdrh ** (7) The subquery has a FROM clause. 209108192d5fSdrh ** 2092df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 2093df199a25Sdrh ** 2094df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 2095df199a25Sdrh ** aggregates. 2096df199a25Sdrh ** 2097df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 2098df199a25Sdrh ** use LIMIT. 2099df199a25Sdrh ** 2100174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 2101174b6195Sdrh ** 21023fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 21033fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 21043fc673e6Sdrh ** 2105ac83963aSdrh ** (13) The subquery and outer query do not both use LIMIT 2106ac83963aSdrh ** 2107ac83963aSdrh ** (14) The subquery does not use OFFSET 2108ac83963aSdrh ** 2109832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 2110832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 2111832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 2112832508b7Sdrh ** 2113665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 2114832508b7Sdrh ** If flattening is attempted this routine returns 1. 2115832508b7Sdrh ** 2116832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 2117832508b7Sdrh ** the subquery before this routine runs. 21181350b030Sdrh */ 21198c74a8caSdrh static int flattenSubquery( 21208c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 21218c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 21228c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 21238c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 21248c74a8caSdrh ){ 21250bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 2126ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 2127ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 21280bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 21296a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 213091bb0eedSdrh int i; /* Loop counter */ 213191bb0eedSdrh Expr *pWhere; /* The WHERE clause */ 213291bb0eedSdrh struct SrcList_item *pSubitem; /* The subquery */ 21331350b030Sdrh 2134832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 2135832508b7Sdrh */ 2136832508b7Sdrh if( p==0 ) return 0; 2137832508b7Sdrh pSrc = p->pSrc; 2138ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 213991bb0eedSdrh pSubitem = &pSrc->a[iFrom]; 214091bb0eedSdrh pSub = pSubitem->pSelect; 2141832508b7Sdrh assert( pSub!=0 ); 2142ac83963aSdrh if( isAgg && subqueryIsAgg ) return 0; /* Restriction (1) */ 2143ac83963aSdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; /* Restriction (2) */ 2144832508b7Sdrh pSubSrc = pSub->pSrc; 2145832508b7Sdrh assert( pSubSrc ); 2146ac83963aSdrh /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, 2147ac83963aSdrh ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET 2148ac83963aSdrh ** because they could be computed at compile-time. But when LIMIT and OFFSET 2149ac83963aSdrh ** became arbitrary expressions, we were forced to add restrictions (13) 2150ac83963aSdrh ** and (14). */ 2151ac83963aSdrh if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ 2152ac83963aSdrh if( pSub->pOffset ) return 0; /* Restriction (14) */ 2153ac83963aSdrh if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ 2154ac83963aSdrh if( (pSub->isDistinct || pSub->pLimit) 2155ac83963aSdrh && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */ 2156df199a25Sdrh return 0; 2157df199a25Sdrh } 2158ac83963aSdrh if( p->isDistinct && subqueryIsAgg ) return 0; /* Restriction (6) */ 2159ac83963aSdrh if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){ 2160ac83963aSdrh return 0; /* Restriction (11) */ 2161ac83963aSdrh } 2162832508b7Sdrh 21638af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 21648af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 21658af4d3acSdrh ** is not allowed: 21668af4d3acSdrh ** 21678af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 21688af4d3acSdrh ** 21698af4d3acSdrh ** If we flatten the above, we would get 21708af4d3acSdrh ** 21718af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 21728af4d3acSdrh ** 21738af4d3acSdrh ** which is not at all the same thing. 21748af4d3acSdrh */ 21758af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 21768af4d3acSdrh return 0; 21778af4d3acSdrh } 21788af4d3acSdrh 21793fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 21803fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 21813fc673e6Sdrh ** An examples of why this is not allowed: 21823fc673e6Sdrh ** 21833fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 21843fc673e6Sdrh ** 21853fc673e6Sdrh ** If we flatten the above, we would get 21863fc673e6Sdrh ** 21873fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 21883fc673e6Sdrh ** 21893fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 21903fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 21913fc673e6Sdrh */ 21923fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 21933fc673e6Sdrh && pSub->pWhere!=0 ){ 21943fc673e6Sdrh return 0; 21953fc673e6Sdrh } 21963fc673e6Sdrh 21970bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 219863eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 2199832508b7Sdrh */ 2200c31c2eb8Sdrh 2201c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 2202c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 2203c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 2204c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 2205c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 2206c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 2207c31c2eb8Sdrh ** elements we are now copying in. 2208c31c2eb8Sdrh */ 220991bb0eedSdrh iParent = pSubitem->iCursor; 2210c31c2eb8Sdrh { 2211c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 221291bb0eedSdrh int jointype = pSubitem->jointype; 2213c31c2eb8Sdrh 221491bb0eedSdrh sqlite3DeleteTable(0, pSubitem->pTab); 221591bb0eedSdrh sqliteFree(pSubitem->zDatabase); 221691bb0eedSdrh sqliteFree(pSubitem->zName); 221791bb0eedSdrh sqliteFree(pSubitem->zAlias); 2218c31c2eb8Sdrh if( nSubSrc>1 ){ 2219c31c2eb8Sdrh int extra = nSubSrc - 1; 2220c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 22214adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 2222c31c2eb8Sdrh } 2223c31c2eb8Sdrh p->pSrc = pSrc; 2224c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 2225c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 2226c31c2eb8Sdrh } 2227c31c2eb8Sdrh } 2228c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 2229c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 2230c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 2231c31c2eb8Sdrh } 22328af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 2233c31c2eb8Sdrh } 2234c31c2eb8Sdrh 2235c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 2236c31c2eb8Sdrh ** references to the iParent in the outer query. 2237c31c2eb8Sdrh ** 2238c31c2eb8Sdrh ** Example: 2239c31c2eb8Sdrh ** 2240c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 2241c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 2242c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 2243c31c2eb8Sdrh ** 2244c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 2245c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 2246c31c2eb8Sdrh */ 2247832508b7Sdrh pList = p->pEList; 2248832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 22496977fea8Sdrh Expr *pExpr; 22506977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 22512646da7eSdrh pList->a[i].zName = sqliteStrNDup((char*)pExpr->span.z, pExpr->span.n); 2252832508b7Sdrh } 2253832508b7Sdrh } 2254643054c1Sdrh substExprList(p->pEList, iParent, pSub->pEList); 22551b2e0329Sdrh if( isAgg ){ 22566a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 22576a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 22581b2e0329Sdrh } 2259174b6195Sdrh if( pSub->pOrderBy ){ 2260174b6195Sdrh assert( p->pOrderBy==0 ); 2261174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 2262174b6195Sdrh pSub->pOrderBy = 0; 2263174b6195Sdrh }else if( p->pOrderBy ){ 22646a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 2265174b6195Sdrh } 2266832508b7Sdrh if( pSub->pWhere ){ 22674adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 2268832508b7Sdrh }else{ 2269832508b7Sdrh pWhere = 0; 2270832508b7Sdrh } 2271832508b7Sdrh if( subqueryIsAgg ){ 2272832508b7Sdrh assert( p->pHaving==0 ); 22731b2e0329Sdrh p->pHaving = p->pWhere; 22741b2e0329Sdrh p->pWhere = pWhere; 22756a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 227691bb0eedSdrh p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); 22771b2e0329Sdrh assert( p->pGroupBy==0 ); 22784adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 2279832508b7Sdrh }else{ 22806a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 228191bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); 2282832508b7Sdrh } 2283c31c2eb8Sdrh 2284c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 2285c31c2eb8Sdrh ** outer query is distinct. 2286c31c2eb8Sdrh */ 2287832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 22888c74a8caSdrh 2289a58fdfb1Sdanielk1977 /* 2290a58fdfb1Sdanielk1977 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; 2291ac83963aSdrh ** 2292ac83963aSdrh ** One is tempted to try to add a and b to combine the limits. But this 2293ac83963aSdrh ** does not work if either limit is negative. 2294a58fdfb1Sdanielk1977 */ 2295a2dc3b1aSdanielk1977 if( pSub->pLimit ){ 2296a2dc3b1aSdanielk1977 p->pLimit = pSub->pLimit; 2297a2dc3b1aSdanielk1977 pSub->pLimit = 0; 2298df199a25Sdrh } 22998c74a8caSdrh 2300c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 2301c31c2eb8Sdrh ** success. 2302c31c2eb8Sdrh */ 23034adee20fSdanielk1977 sqlite3SelectDelete(pSub); 2304832508b7Sdrh return 1; 23051350b030Sdrh } 2306b7f9164eSdrh #endif /* SQLITE_OMIT_VIEW */ 23071350b030Sdrh 23081350b030Sdrh /* 23099562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 23109562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 23119562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 2312e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 23139562b551Sdrh ** simple min() or max() query, then return 0; 23149562b551Sdrh ** 23159562b551Sdrh ** A simply min() or max() query looks like this: 23169562b551Sdrh ** 23179562b551Sdrh ** SELECT min(a) FROM table; 23189562b551Sdrh ** SELECT max(a) FROM table; 23199562b551Sdrh ** 23209562b551Sdrh ** The query may have only a single table in its FROM argument. There 23219562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 23229562b551Sdrh ** be the min() or max() of a single column of the table. The column 23239562b551Sdrh ** in the min() or max() function must be indexed. 23249562b551Sdrh ** 23254adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 23269562b551Sdrh ** See the header comment on that routine for additional information. 23279562b551Sdrh */ 23289562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 23299562b551Sdrh Expr *pExpr; 23309562b551Sdrh int iCol; 23319562b551Sdrh Table *pTab; 23329562b551Sdrh Index *pIdx; 23339562b551Sdrh int base; 23349562b551Sdrh Vdbe *v; 23359562b551Sdrh int seekOp; 23366e17529eSdrh ExprList *pEList, *pList, eList; 23379562b551Sdrh struct ExprList_item eListItem; 23386e17529eSdrh SrcList *pSrc; 2339ec7429aeSdrh int brk; 2340da184236Sdanielk1977 int iDb; 23416e17529eSdrh 23429562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 23439562b551Sdrh ** zero if it is not. 23449562b551Sdrh */ 23459562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 23466e17529eSdrh pSrc = p->pSrc; 23476e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 23486e17529eSdrh pEList = p->pEList; 23496e17529eSdrh if( pEList->nExpr!=1 ) return 0; 23506e17529eSdrh pExpr = pEList->a[0].pExpr; 23519562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 23526e17529eSdrh pList = pExpr->pList; 23536e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 23546977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 23552646da7eSdrh if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){ 23560bce8354Sdrh seekOp = OP_Rewind; 23572646da7eSdrh }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){ 23580bce8354Sdrh seekOp = OP_Last; 23590bce8354Sdrh }else{ 23600bce8354Sdrh return 0; 23610bce8354Sdrh } 23626e17529eSdrh pExpr = pList->a[0].pExpr; 23639562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 23649562b551Sdrh iCol = pExpr->iColumn; 23656e17529eSdrh pTab = pSrc->a[0].pTab; 23669562b551Sdrh 2367c00da105Sdanielk1977 23689562b551Sdrh /* If we get to here, it means the query is of the correct form. 236917f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 237017f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 237117f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 237217f71934Sdrh ** usable index is found, return 0. 23739562b551Sdrh */ 23749562b551Sdrh if( iCol<0 ){ 23759562b551Sdrh pIdx = 0; 23769562b551Sdrh }else{ 2377dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 23789562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 23799562b551Sdrh assert( pIdx->nColumn>=1 ); 2380b3bf556eSdanielk1977 if( pIdx->aiColumn[0]==iCol && 2381b3bf556eSdanielk1977 0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){ 2382b3bf556eSdanielk1977 break; 2383b3bf556eSdanielk1977 } 23849562b551Sdrh } 23859562b551Sdrh if( pIdx==0 ) return 0; 23869562b551Sdrh } 23879562b551Sdrh 2388e5f50722Sdrh /* Identify column types if we will be using the callback. This 23899562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 2390e5f50722Sdrh ** The column names have already been generated in the calling function. 23919562b551Sdrh */ 23924adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 23939562b551Sdrh if( v==0 ) return 0; 23949562b551Sdrh 23950c37e630Sdrh /* If the output is destined for a temporary table, open that table. 23960c37e630Sdrh */ 23979d2985c7Sdrh if( eDest==SRT_VirtualTab ){ 2398d81bd4e2Sdrh sqlite3VdbeAddOp(v, OP_OpenVirtual, iParm, 1); 23990c37e630Sdrh } 24000c37e630Sdrh 240117f71934Sdrh /* Generating code to find the min or the max. Basically all we have 240217f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 240317f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 240417f71934Sdrh ** or last entry in the main table. 24059562b551Sdrh */ 2406da184236Sdanielk1977 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 240714db2665Sdanielk1977 assert( iDb>=0 || pTab->isTransient ); 2408da184236Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 2409c00da105Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 24106e17529eSdrh base = pSrc->a[0].iCursor; 2411ec7429aeSdrh brk = sqlite3VdbeMakeLabel(v); 2412ec7429aeSdrh computeLimitRegisters(pParse, p, brk); 24136e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 2414c00da105Sdanielk1977 sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead); 24156e17529eSdrh } 24169562b551Sdrh if( pIdx==0 ){ 24174adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 24189562b551Sdrh }else{ 24193719d7f9Sdanielk1977 /* Even though the cursor used to open the index here is closed 24203719d7f9Sdanielk1977 ** as soon as a single value has been read from it, allocate it 24213719d7f9Sdanielk1977 ** using (pParse->nTab++) to prevent the cursor id from being 24223719d7f9Sdanielk1977 ** reused. This is important for statements of the form 24233719d7f9Sdanielk1977 ** "INSERT INTO x SELECT max() FROM x". 24243719d7f9Sdanielk1977 */ 24253719d7f9Sdanielk1977 int iIdx; 2426b3bf556eSdanielk1977 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 24273719d7f9Sdanielk1977 iIdx = pParse->nTab++; 2428da184236Sdanielk1977 assert( pIdx->pSchema==pTab->pSchema ); 2429da184236Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); 24303719d7f9Sdanielk1977 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 2431b3bf556eSdanielk1977 (char*)pKey, P3_KEYINFO_HANDOFF); 24329eb516c0Sdrh if( seekOp==OP_Rewind ){ 2433f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 24341af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); 24351af3fdb4Sdrh seekOp = OP_MoveGt; 24369eb516c0Sdrh } 24373719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, seekOp, iIdx, 0); 2438f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0); 24393719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); 24407cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 24419562b551Sdrh } 24425cf8e8c7Sdrh eList.nExpr = 1; 24435cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 24445cf8e8c7Sdrh eList.a = &eListItem; 24455cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 2446ec7429aeSdrh selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, brk, brk, 0); 2447ec7429aeSdrh sqlite3VdbeResolveLabel(v, brk); 24484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 24496e17529eSdrh 24509562b551Sdrh return 1; 24519562b551Sdrh } 24529562b551Sdrh 24539562b551Sdrh /* 2454290c1948Sdrh ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return 2455290c1948Sdrh ** the number of errors seen. 2456290c1948Sdrh ** 2457290c1948Sdrh ** An ORDER BY or GROUP BY is a list of expressions. If any expression 2458290c1948Sdrh ** is an integer constant, then that expression is replaced by the 2459290c1948Sdrh ** corresponding entry in the result set. 2460290c1948Sdrh */ 2461290c1948Sdrh static int processOrderGroupBy( 2462b3bce662Sdanielk1977 NameContext *pNC, /* Name context of the SELECT statement. */ 2463290c1948Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 2464290c1948Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 2465290c1948Sdrh ){ 2466290c1948Sdrh int i; 2467b3bce662Sdanielk1977 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */ 2468b3bce662Sdanielk1977 Parse *pParse = pNC->pParse; /* The result set of the SELECT */ 2469b3bce662Sdanielk1977 assert( pEList ); 2470b3bce662Sdanielk1977 2471290c1948Sdrh if( pOrderBy==0 ) return 0; 2472290c1948Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2473290c1948Sdrh int iCol; 2474290c1948Sdrh Expr *pE = pOrderBy->a[i].pExpr; 2475b3bce662Sdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 2476b3bce662Sdanielk1977 if( iCol>0 && iCol<=pEList->nExpr ){ 2477290c1948Sdrh sqlite3ExprDelete(pE); 2478290c1948Sdrh pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 2479b3bce662Sdanielk1977 }else{ 2480290c1948Sdrh sqlite3ErrorMsg(pParse, 2481290c1948Sdrh "%s BY column number %d out of range - should be " 2482290c1948Sdrh "between 1 and %d", zType, iCol, pEList->nExpr); 2483290c1948Sdrh return 1; 2484290c1948Sdrh } 2485290c1948Sdrh } 2486b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(pNC, pE) ){ 2487b3bce662Sdanielk1977 return 1; 2488b3bce662Sdanielk1977 } 2489b3bce662Sdanielk1977 if( sqlite3ExprIsConstant(pE) ){ 2490b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, 2491b3bce662Sdanielk1977 "%s BY terms must not be non-integer constants", zType); 2492b3bce662Sdanielk1977 return 1; 2493b3bce662Sdanielk1977 } 2494290c1948Sdrh } 2495290c1948Sdrh return 0; 2496290c1948Sdrh } 2497290c1948Sdrh 2498290c1948Sdrh /* 2499b3bce662Sdanielk1977 ** This routine resolves any names used in the result set of the 2500b3bce662Sdanielk1977 ** supplied SELECT statement. If the SELECT statement being resolved 2501b3bce662Sdanielk1977 ** is a sub-select, then pOuterNC is a pointer to the NameContext 2502b3bce662Sdanielk1977 ** of the parent SELECT. 2503b3bce662Sdanielk1977 */ 2504b3bce662Sdanielk1977 int sqlite3SelectResolve( 2505b3bce662Sdanielk1977 Parse *pParse, /* The parser context */ 2506b3bce662Sdanielk1977 Select *p, /* The SELECT statement being coded. */ 2507b3bce662Sdanielk1977 NameContext *pOuterNC /* The outer name context. May be NULL. */ 2508b3bce662Sdanielk1977 ){ 2509b3bce662Sdanielk1977 ExprList *pEList; /* Result set. */ 2510b3bce662Sdanielk1977 int i; /* For-loop variable used in multiple places */ 2511b3bce662Sdanielk1977 NameContext sNC; /* Local name-context */ 251213449892Sdrh ExprList *pGroupBy; /* The group by clause */ 2513b3bce662Sdanielk1977 2514b3bce662Sdanielk1977 /* If this routine has run before, return immediately. */ 2515b3bce662Sdanielk1977 if( p->isResolved ){ 2516b3bce662Sdanielk1977 assert( !pOuterNC ); 2517b3bce662Sdanielk1977 return SQLITE_OK; 2518b3bce662Sdanielk1977 } 2519b3bce662Sdanielk1977 p->isResolved = 1; 2520b3bce662Sdanielk1977 2521b3bce662Sdanielk1977 /* If there have already been errors, do nothing. */ 2522b3bce662Sdanielk1977 if( pParse->nErr>0 ){ 2523b3bce662Sdanielk1977 return SQLITE_ERROR; 2524b3bce662Sdanielk1977 } 2525b3bce662Sdanielk1977 2526b3bce662Sdanielk1977 /* Prepare the select statement. This call will allocate all cursors 2527b3bce662Sdanielk1977 ** required to handle the tables and subqueries in the FROM clause. 2528b3bce662Sdanielk1977 */ 2529b3bce662Sdanielk1977 if( prepSelectStmt(pParse, p) ){ 2530b3bce662Sdanielk1977 return SQLITE_ERROR; 2531b3bce662Sdanielk1977 } 2532b3bce662Sdanielk1977 2533a2dc3b1aSdanielk1977 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 2534a2dc3b1aSdanielk1977 ** are not allowed to refer to any names, so pass an empty NameContext. 2535a2dc3b1aSdanielk1977 */ 2536ffe07b2dSdrh memset(&sNC, 0, sizeof(sNC)); 2537b3bce662Sdanielk1977 sNC.pParse = pParse; 2538a2dc3b1aSdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pLimit) || 2539a2dc3b1aSdanielk1977 sqlite3ExprResolveNames(&sNC, p->pOffset) ){ 2540a2dc3b1aSdanielk1977 return SQLITE_ERROR; 2541a2dc3b1aSdanielk1977 } 2542a2dc3b1aSdanielk1977 2543a2dc3b1aSdanielk1977 /* Set up the local name-context to pass to ExprResolveNames() to 2544a2dc3b1aSdanielk1977 ** resolve the expression-list. 2545a2dc3b1aSdanielk1977 */ 2546a2dc3b1aSdanielk1977 sNC.allowAgg = 1; 2547a2dc3b1aSdanielk1977 sNC.pSrcList = p->pSrc; 2548a2dc3b1aSdanielk1977 sNC.pNext = pOuterNC; 2549b3bce662Sdanielk1977 2550b3bce662Sdanielk1977 /* Resolve names in the result set. */ 2551b3bce662Sdanielk1977 pEList = p->pEList; 2552b3bce662Sdanielk1977 if( !pEList ) return SQLITE_ERROR; 2553b3bce662Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 2554b3bce662Sdanielk1977 Expr *pX = pEList->a[i].pExpr; 2555b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pX) ){ 2556b3bce662Sdanielk1977 return SQLITE_ERROR; 2557b3bce662Sdanielk1977 } 2558b3bce662Sdanielk1977 } 2559b3bce662Sdanielk1977 2560b3bce662Sdanielk1977 /* If there are no aggregate functions in the result-set, and no GROUP BY 2561b3bce662Sdanielk1977 ** expression, do not allow aggregates in any of the other expressions. 2562b3bce662Sdanielk1977 */ 2563b3bce662Sdanielk1977 assert( !p->isAgg ); 256413449892Sdrh pGroupBy = p->pGroupBy; 256513449892Sdrh if( pGroupBy || sNC.hasAgg ){ 2566b3bce662Sdanielk1977 p->isAgg = 1; 2567b3bce662Sdanielk1977 }else{ 2568b3bce662Sdanielk1977 sNC.allowAgg = 0; 2569b3bce662Sdanielk1977 } 2570b3bce662Sdanielk1977 2571b3bce662Sdanielk1977 /* If a HAVING clause is present, then there must be a GROUP BY clause. 2572b3bce662Sdanielk1977 */ 257313449892Sdrh if( p->pHaving && !pGroupBy ){ 2574b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2575b3bce662Sdanielk1977 return SQLITE_ERROR; 2576b3bce662Sdanielk1977 } 2577b3bce662Sdanielk1977 2578b3bce662Sdanielk1977 /* Add the expression list to the name-context before parsing the 2579b3bce662Sdanielk1977 ** other expressions in the SELECT statement. This is so that 2580b3bce662Sdanielk1977 ** expressions in the WHERE clause (etc.) can refer to expressions by 2581b3bce662Sdanielk1977 ** aliases in the result set. 2582b3bce662Sdanielk1977 ** 2583b3bce662Sdanielk1977 ** Minor point: If this is the case, then the expression will be 2584b3bce662Sdanielk1977 ** re-evaluated for each reference to it. 2585b3bce662Sdanielk1977 */ 2586b3bce662Sdanielk1977 sNC.pEList = p->pEList; 2587b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pWhere) || 2588b3bce662Sdanielk1977 sqlite3ExprResolveNames(&sNC, p->pHaving) || 2589b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") || 259013449892Sdrh processOrderGroupBy(&sNC, pGroupBy, "GROUP") 2591b3bce662Sdanielk1977 ){ 2592b3bce662Sdanielk1977 return SQLITE_ERROR; 2593b3bce662Sdanielk1977 } 2594b3bce662Sdanielk1977 259513449892Sdrh /* Make sure the GROUP BY clause does not contain aggregate functions. 259613449892Sdrh */ 259713449892Sdrh if( pGroupBy ){ 259813449892Sdrh struct ExprList_item *pItem; 259913449892Sdrh 260013449892Sdrh for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ 260113449892Sdrh if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ 260213449892Sdrh sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " 260313449892Sdrh "the GROUP BY clause"); 260413449892Sdrh return SQLITE_ERROR; 260513449892Sdrh } 260613449892Sdrh } 260713449892Sdrh } 260813449892Sdrh 2609b3bce662Sdanielk1977 return SQLITE_OK; 2610b3bce662Sdanielk1977 } 2611b3bce662Sdanielk1977 2612b3bce662Sdanielk1977 /* 261313449892Sdrh ** Reset the aggregate accumulator. 261413449892Sdrh ** 261513449892Sdrh ** The aggregate accumulator is a set of memory cells that hold 261613449892Sdrh ** intermediate results while calculating an aggregate. This 261713449892Sdrh ** routine simply stores NULLs in all of those memory cells. 2618b3bce662Sdanielk1977 */ 261913449892Sdrh static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ 262013449892Sdrh Vdbe *v = pParse->pVdbe; 262113449892Sdrh int i; 2622c99130fdSdrh struct AggInfo_func *pFunc; 262313449892Sdrh if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){ 262413449892Sdrh return; 262513449892Sdrh } 262613449892Sdrh for(i=0; i<pAggInfo->nColumn; i++){ 2627d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemNull, pAggInfo->aCol[i].iMem, 0); 262813449892Sdrh } 2629c99130fdSdrh for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){ 2630d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemNull, pFunc->iMem, 0); 2631c99130fdSdrh if( pFunc->iDistinct>=0 ){ 2632c99130fdSdrh Expr *pE = pFunc->pExpr; 2633c99130fdSdrh if( pE->pList==0 || pE->pList->nExpr!=1 ){ 2634c99130fdSdrh sqlite3ErrorMsg(pParse, "DISTINCT in aggregate must be followed " 2635c99130fdSdrh "by an expression"); 2636c99130fdSdrh pFunc->iDistinct = -1; 2637c99130fdSdrh }else{ 2638c99130fdSdrh KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->pList); 2639c99130fdSdrh sqlite3VdbeOp3(v, OP_OpenVirtual, pFunc->iDistinct, 0, 2640c99130fdSdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 2641c99130fdSdrh } 2642c99130fdSdrh } 264313449892Sdrh } 2644b3bce662Sdanielk1977 } 2645b3bce662Sdanielk1977 2646b3bce662Sdanielk1977 /* 264713449892Sdrh ** Invoke the OP_AggFinalize opcode for every aggregate function 264813449892Sdrh ** in the AggInfo structure. 2649b3bce662Sdanielk1977 */ 265013449892Sdrh static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ 265113449892Sdrh Vdbe *v = pParse->pVdbe; 265213449892Sdrh int i; 265313449892Sdrh struct AggInfo_func *pF; 265413449892Sdrh for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ 2655a10a34b8Sdrh ExprList *pList = pF->pExpr->pList; 2656a10a34b8Sdrh sqlite3VdbeOp3(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 2657a10a34b8Sdrh (void*)pF->pFunc, P3_FUNCDEF); 2658b3bce662Sdanielk1977 } 265913449892Sdrh } 266013449892Sdrh 266113449892Sdrh /* 266213449892Sdrh ** Update the accumulator memory cells for an aggregate based on 266313449892Sdrh ** the current cursor position. 266413449892Sdrh */ 266513449892Sdrh static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ 266613449892Sdrh Vdbe *v = pParse->pVdbe; 266713449892Sdrh int i; 266813449892Sdrh struct AggInfo_func *pF; 266913449892Sdrh struct AggInfo_col *pC; 267013449892Sdrh 267113449892Sdrh pAggInfo->directMode = 1; 267213449892Sdrh for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ 267313449892Sdrh int nArg; 2674c99130fdSdrh int addrNext = 0; 267513449892Sdrh ExprList *pList = pF->pExpr->pList; 267613449892Sdrh if( pList ){ 267713449892Sdrh nArg = pList->nExpr; 267813449892Sdrh sqlite3ExprCodeExprList(pParse, pList); 267913449892Sdrh }else{ 268013449892Sdrh nArg = 0; 268113449892Sdrh } 2682c99130fdSdrh if( pF->iDistinct>=0 ){ 2683c99130fdSdrh addrNext = sqlite3VdbeMakeLabel(v); 2684c99130fdSdrh assert( nArg==1 ); 2685f8875400Sdrh codeDistinct(v, pF->iDistinct, addrNext, 1); 2686c99130fdSdrh } 268713449892Sdrh if( pF->pFunc->needCollSeq ){ 268813449892Sdrh CollSeq *pColl = 0; 268913449892Sdrh struct ExprList_item *pItem; 269013449892Sdrh int j; 269143617e9aSdrh assert( pList!=0 ); /* pList!=0 if pF->pFunc->needCollSeq is true */ 269243617e9aSdrh for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){ 269313449892Sdrh pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); 269413449892Sdrh } 269513449892Sdrh if( !pColl ){ 269613449892Sdrh pColl = pParse->db->pDfltColl; 269713449892Sdrh } 269813449892Sdrh sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 269913449892Sdrh } 270013449892Sdrh sqlite3VdbeOp3(v, OP_AggStep, pF->iMem, nArg, (void*)pF->pFunc, P3_FUNCDEF); 2701c99130fdSdrh if( addrNext ){ 2702c99130fdSdrh sqlite3VdbeResolveLabel(v, addrNext); 2703c99130fdSdrh } 270413449892Sdrh } 270513449892Sdrh for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){ 27065774b806Sdrh sqlite3ExprCode(pParse, pC->pExpr); 270713449892Sdrh sqlite3VdbeAddOp(v, OP_MemStore, pC->iMem, 1); 270813449892Sdrh } 270913449892Sdrh pAggInfo->directMode = 0; 271013449892Sdrh } 271113449892Sdrh 2712b3bce662Sdanielk1977 2713b3bce662Sdanielk1977 /* 27149bb61fe7Sdrh ** Generate code for the given SELECT statement. 27159bb61fe7Sdrh ** 2716fef5208cSdrh ** The results are distributed in various ways depending on the 2717fef5208cSdrh ** value of eDest and iParm. 2718fef5208cSdrh ** 2719fef5208cSdrh ** eDest Value Result 2720fef5208cSdrh ** ------------ ------------------------------------------- 2721fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2722fef5208cSdrh ** 2723fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2724fef5208cSdrh ** 2725e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2726fef5208cSdrh ** 272782c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 272882c3d636Sdrh ** 27294b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2730c4a3c779Sdrh ** 2731c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 27329bb61fe7Sdrh ** 2733e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2734e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2735e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2736e78e8284Sdrh ** 27379bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 27389bb61fe7Sdrh ** encountered, then an appropriate error message is left in 27399bb61fe7Sdrh ** pParse->zErrMsg. 27409bb61fe7Sdrh ** 27419bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 27429bb61fe7Sdrh ** calling function needs to do that. 27431b2e0329Sdrh ** 27441b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 27451b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 27461b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 27471b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 27481b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 27491b2e0329Sdrh ** can be changed. 2750e78e8284Sdrh ** 2751e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2752e78e8284Sdrh ** 2753e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2754e78e8284Sdrh ** \ \_______ subquery _______/ / 2755e78e8284Sdrh ** \ / 2756e78e8284Sdrh ** \____________________ outer query ___________________/ 2757e78e8284Sdrh ** 2758e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2759e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2760e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2761e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2762e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2763e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 27649bb61fe7Sdrh */ 27654adee20fSdanielk1977 int sqlite3Select( 2766cce7d176Sdrh Parse *pParse, /* The parser context */ 27679bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2768e78e8284Sdrh int eDest, /* How to dispose of the results */ 2769e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2770832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2771832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 277284ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 2773b3bce662Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2774cce7d176Sdrh ){ 277513449892Sdrh int i, j; /* Loop counters */ 277613449892Sdrh WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */ 277713449892Sdrh Vdbe *v; /* The virtual machine under construction */ 2778b3bce662Sdanielk1977 int isAgg; /* True for select lists like "count(*)" */ 2779a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2780ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 27819bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 27829bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 27832282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 27842282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 278519a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 278619a775c2Sdrh int distinct; /* Table to use for the distinct set */ 27871d83f052Sdrh int rc = 1; /* Value to return from this function */ 27889d2985c7Sdrh int addrSortIndex; /* Address of an OP_OpenVirtual instruction */ 278913449892Sdrh AggInfo sAggInfo; /* Information used by aggregate queries */ 2790ec7429aeSdrh int iEnd; /* Address of the end of the query */ 27919bb61fe7Sdrh 27929e12800dSdanielk1977 if( p==0 || sqlite3MallocFailed() || pParse->nErr ){ 27936f7adc8aSdrh return 1; 27946f7adc8aSdrh } 27954adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 279613449892Sdrh memset(&sAggInfo, 0, sizeof(sAggInfo)); 2797daffd0e5Sdrh 2798b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 279982c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 280082c3d636Sdrh */ 280182c3d636Sdrh if( p->pPrior ){ 28020342b1f5Sdrh if( p->pRightmost==0 ){ 28030342b1f5Sdrh Select *pLoop; 28040342b1f5Sdrh for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ 28050342b1f5Sdrh pLoop->pRightmost = p; 28060342b1f5Sdrh } 28070342b1f5Sdrh } 280884ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 280982c3d636Sdrh } 2810b7f9164eSdrh #endif 281182c3d636Sdrh 2812b3bce662Sdanielk1977 pOrderBy = p->pOrderBy; 281313449892Sdrh if( IgnorableOrderby(eDest) ){ 2814b3bce662Sdanielk1977 p->pOrderBy = 0; 2815b3bce662Sdanielk1977 } 2816b3bce662Sdanielk1977 if( sqlite3SelectResolve(pParse, p, 0) ){ 2817b3bce662Sdanielk1977 goto select_end; 2818b3bce662Sdanielk1977 } 2819b3bce662Sdanielk1977 p->pOrderBy = pOrderBy; 2820b3bce662Sdanielk1977 282182c3d636Sdrh /* Make local copies of the parameters for this query. 282282c3d636Sdrh */ 28239bb61fe7Sdrh pTabList = p->pSrc; 28249bb61fe7Sdrh pWhere = p->pWhere; 28252282792aSdrh pGroupBy = p->pGroupBy; 28262282792aSdrh pHaving = p->pHaving; 2827b3bce662Sdanielk1977 isAgg = p->isAgg; 282819a775c2Sdrh isDistinct = p->isDistinct; 2829b3bce662Sdanielk1977 pEList = p->pEList; 2830b3bce662Sdanielk1977 if( pEList==0 ) goto select_end; 28319bb61fe7Sdrh 28329bb61fe7Sdrh /* 28339bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 28349bb61fe7Sdrh ** errors before this routine starts. 28359bb61fe7Sdrh */ 28361d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2837cce7d176Sdrh 28382282792aSdrh /* If writing to memory or generating a set 28392282792aSdrh ** only a single column may be output. 284019a775c2Sdrh */ 284193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2842fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 28434adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2844da93d238Sdrh "a SELECT that is part of an expression"); 28451d83f052Sdrh goto select_end; 284619a775c2Sdrh } 284793758c8dSdanielk1977 #endif 284819a775c2Sdrh 2849c926afbcSdrh /* ORDER BY is ignored for some destinations. 28502282792aSdrh */ 285113449892Sdrh if( IgnorableOrderby(eDest) ){ 2852acd4c695Sdrh pOrderBy = 0; 28532282792aSdrh } 28542282792aSdrh 2855d820cb1bSdrh /* Begin generating code. 2856d820cb1bSdrh */ 28574adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2858d820cb1bSdrh if( v==0 ) goto select_end; 2859d820cb1bSdrh 2860d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2861d820cb1bSdrh */ 286251522cd3Sdrh #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) 2863ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 2864742f947bSdanielk1977 const char *zSavedAuthContext = 0; 2865c31c2eb8Sdrh int needRestoreContext; 286613449892Sdrh struct SrcList_item *pItem = &pTabList->a[i]; 2867c31c2eb8Sdrh 28681787ccabSdanielk1977 if( pItem->pSelect==0 || pItem->isPopulated ) continue; 286913449892Sdrh if( pItem->zName!=0 ){ 28705cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 287113449892Sdrh pParse->zAuthContext = pItem->zName; 2872c31c2eb8Sdrh needRestoreContext = 1; 2873c31c2eb8Sdrh }else{ 2874c31c2eb8Sdrh needRestoreContext = 0; 28755cf590c1Sdrh } 28769d2985c7Sdrh sqlite3Select(pParse, pItem->pSelect, SRT_VirtualTab, 287713449892Sdrh pItem->iCursor, p, i, &isAgg, 0); 2878c31c2eb8Sdrh if( needRestoreContext ){ 28795cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 28805cf590c1Sdrh } 2881832508b7Sdrh pTabList = p->pSrc; 2882832508b7Sdrh pWhere = p->pWhere; 288313449892Sdrh if( !IgnorableOrderby(eDest) ){ 2884832508b7Sdrh pOrderBy = p->pOrderBy; 2885acd4c695Sdrh } 2886832508b7Sdrh pGroupBy = p->pGroupBy; 2887832508b7Sdrh pHaving = p->pHaving; 2888832508b7Sdrh isDistinct = p->isDistinct; 28891b2e0329Sdrh } 289051522cd3Sdrh #endif 28911b2e0329Sdrh 28926e17529eSdrh /* Check for the special case of a min() or max() function by itself 28936e17529eSdrh ** in the result set. 28946e17529eSdrh */ 28956e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 28966e17529eSdrh rc = 0; 28976e17529eSdrh goto select_end; 28986e17529eSdrh } 28996e17529eSdrh 29001b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 29011b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 29021b2e0329Sdrh */ 2903b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 29041b2e0329Sdrh if( pParent && pParentAgg && 290574161705Sdrh flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){ 29061b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 2907b3bce662Sdanielk1977 goto select_end; 29081b2e0329Sdrh } 2909b7f9164eSdrh #endif 2910832508b7Sdrh 29117cedc8d4Sdanielk1977 /* If there is an ORDER BY clause, resolve any collation sequences 29129d2985c7Sdrh ** names that have been explicitly specified and create a sorting index. 29139d2985c7Sdrh ** 29149d2985c7Sdrh ** This sorting index might end up being unused if the data can be 29159d2985c7Sdrh ** extracted in pre-sorted order. If that is the case, then the 29169d2985c7Sdrh ** OP_OpenVirtual instruction will be changed to an OP_Noop once 29179d2985c7Sdrh ** we figure out that the sorting index is not needed. The addrSortIndex 29189d2985c7Sdrh ** variable is used to facilitate that change. 29197cedc8d4Sdanielk1977 */ 29207cedc8d4Sdanielk1977 if( pOrderBy ){ 29215d9a4af9Sdrh struct ExprList_item *pTerm; 29220342b1f5Sdrh KeyInfo *pKeyInfo; 29235d9a4af9Sdrh for(i=0, pTerm=pOrderBy->a; i<pOrderBy->nExpr; i++, pTerm++){ 29245d9a4af9Sdrh if( pTerm->zName ){ 29255d9a4af9Sdrh pTerm->pExpr->pColl = sqlite3LocateCollSeq(pParse, pTerm->zName, -1); 29267cedc8d4Sdanielk1977 } 29277cedc8d4Sdanielk1977 } 29287cedc8d4Sdanielk1977 if( pParse->nErr ){ 29297cedc8d4Sdanielk1977 goto select_end; 29307cedc8d4Sdanielk1977 } 29310342b1f5Sdrh pKeyInfo = keyInfoFromExprList(pParse, pOrderBy); 29329d2985c7Sdrh pOrderBy->iECursor = pParse->nTab++; 29339d2985c7Sdrh p->addrOpenVirt[2] = addrSortIndex = 29349d2985c7Sdrh sqlite3VdbeOp3(v, OP_OpenVirtual, pOrderBy->iECursor, pOrderBy->nExpr+2, 29350342b1f5Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 29369d2985c7Sdrh }else{ 29379d2985c7Sdrh addrSortIndex = -1; 29387cedc8d4Sdanielk1977 } 29397cedc8d4Sdanielk1977 29407b58daeaSdrh /* Set the limiter. 29417b58daeaSdrh */ 2942ec7429aeSdrh iEnd = sqlite3VdbeMakeLabel(v); 2943ec7429aeSdrh computeLimitRegisters(pParse, p, iEnd); 29447b58daeaSdrh 29452d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 29462d0794e3Sdrh */ 29479d2985c7Sdrh if( eDest==SRT_VirtualTab ){ 29480342b1f5Sdrh sqlite3VdbeAddOp(v, OP_OpenVirtual, iParm, pEList->nExpr); 29492d0794e3Sdrh } 29502d0794e3Sdrh 2951dece1a84Sdrh /* Open a virtual index to use for the distinct set. 2952cce7d176Sdrh */ 295319a775c2Sdrh if( isDistinct ){ 29540342b1f5Sdrh KeyInfo *pKeyInfo; 2955832508b7Sdrh distinct = pParse->nTab++; 29560342b1f5Sdrh pKeyInfo = keyInfoFromExprList(pParse, p->pEList); 29570342b1f5Sdrh sqlite3VdbeOp3(v, OP_OpenVirtual, distinct, 0, 29580342b1f5Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 2959832508b7Sdrh }else{ 2960832508b7Sdrh distinct = -1; 2961efb7251dSdrh } 2962832508b7Sdrh 296313449892Sdrh /* Aggregate and non-aggregate queries are handled differently */ 296413449892Sdrh if( !isAgg && pGroupBy==0 ){ 296513449892Sdrh /* This case is for non-aggregate queries 296613449892Sdrh ** Begin the database scan 2967832508b7Sdrh */ 296813449892Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy); 29691d83f052Sdrh if( pWInfo==0 ) goto select_end; 2970cce7d176Sdrh 29719d2985c7Sdrh /* If sorting index that was created by a prior OP_OpenVirtual 29729d2985c7Sdrh ** instruction ended up not being needed, then change the OP_OpenVirtual 29739d2985c7Sdrh ** into an OP_Noop. 29749d2985c7Sdrh */ 29759d2985c7Sdrh if( addrSortIndex>=0 && pOrderBy==0 ){ 2976f8875400Sdrh sqlite3VdbeChangeToNoop(v, addrSortIndex, 1); 29779d2985c7Sdrh p->addrOpenVirt[2] = -1; 29789d2985c7Sdrh } 29799d2985c7Sdrh 298013449892Sdrh /* Use the standard inner loop 2981cce7d176Sdrh */ 2982df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 298384ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 29841d83f052Sdrh goto select_end; 2985cce7d176Sdrh } 29862282792aSdrh 2987cce7d176Sdrh /* End the database scan loop. 2988cce7d176Sdrh */ 29894adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 299013449892Sdrh }else{ 299113449892Sdrh /* This is the processing for aggregate queries */ 299213449892Sdrh NameContext sNC; /* Name context for processing aggregate information */ 299313449892Sdrh int iAMem; /* First Mem address for storing current GROUP BY */ 299413449892Sdrh int iBMem; /* First Mem address for previous GROUP BY */ 299513449892Sdrh int iUseFlag; /* Mem address holding flag indicating that at least 299613449892Sdrh ** one row of the input to the aggregator has been 299713449892Sdrh ** processed */ 299813449892Sdrh int iAbortFlag; /* Mem address which causes query abort if positive */ 299913449892Sdrh int groupBySort; /* Rows come from source in GROUP BY order */ 3000cce7d176Sdrh 300113449892Sdrh 300213449892Sdrh /* The following variables hold addresses or labels for parts of the 300313449892Sdrh ** virtual machine program we are putting together */ 300413449892Sdrh int addrOutputRow; /* Start of subroutine that outputs a result row */ 300513449892Sdrh int addrSetAbort; /* Set the abort flag and return */ 300613449892Sdrh int addrInitializeLoop; /* Start of code that initializes the input loop */ 300713449892Sdrh int addrTopOfLoop; /* Top of the input loop */ 300813449892Sdrh int addrGroupByChange; /* Code that runs when any GROUP BY term changes */ 300913449892Sdrh int addrProcessRow; /* Code to process a single input row */ 301013449892Sdrh int addrEnd; /* End of all processing */ 301113449892Sdrh int addrSortingIdx; /* The OP_OpenVirtual for the sorting index */ 3012e313382eSdrh int addrReset; /* Subroutine for resetting the accumulator */ 301313449892Sdrh 301413449892Sdrh addrEnd = sqlite3VdbeMakeLabel(v); 301513449892Sdrh 301613449892Sdrh /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in 301713449892Sdrh ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the 301813449892Sdrh ** SELECT statement. 30192282792aSdrh */ 302013449892Sdrh memset(&sNC, 0, sizeof(sNC)); 302113449892Sdrh sNC.pParse = pParse; 302213449892Sdrh sNC.pSrcList = pTabList; 302313449892Sdrh sNC.pAggInfo = &sAggInfo; 302413449892Sdrh sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0; 30259d2985c7Sdrh sAggInfo.pGroupBy = pGroupBy; 302613449892Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){ 30271d83f052Sdrh goto select_end; 30282282792aSdrh } 302913449892Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){ 303013449892Sdrh goto select_end; 30312282792aSdrh } 303213449892Sdrh if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ 303313449892Sdrh goto select_end; 303413449892Sdrh } 303513449892Sdrh sAggInfo.nAccumulator = sAggInfo.nColumn; 303613449892Sdrh for(i=0; i<sAggInfo.nFunc; i++){ 303713449892Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){ 303813449892Sdrh goto select_end; 303913449892Sdrh } 304013449892Sdrh } 30419e12800dSdanielk1977 if( sqlite3MallocFailed() ) goto select_end; 304213449892Sdrh 304313449892Sdrh /* Processing for aggregates with GROUP BY is very different and 304413449892Sdrh ** much more complex tha aggregates without a GROUP BY. 304513449892Sdrh */ 304613449892Sdrh if( pGroupBy ){ 304713449892Sdrh KeyInfo *pKeyInfo; /* Keying information for the group by clause */ 304813449892Sdrh 304913449892Sdrh /* Create labels that we will be needing 305013449892Sdrh */ 305113449892Sdrh 305213449892Sdrh addrInitializeLoop = sqlite3VdbeMakeLabel(v); 305313449892Sdrh addrGroupByChange = sqlite3VdbeMakeLabel(v); 305413449892Sdrh addrProcessRow = sqlite3VdbeMakeLabel(v); 305513449892Sdrh 305613449892Sdrh /* If there is a GROUP BY clause we might need a sorting index to 305713449892Sdrh ** implement it. Allocate that sorting index now. If it turns out 305813449892Sdrh ** that we do not need it after all, the OpenVirtual instruction 305913449892Sdrh ** will be converted into a Noop. 306013449892Sdrh */ 306113449892Sdrh sAggInfo.sortingIdx = pParse->nTab++; 306213449892Sdrh pKeyInfo = keyInfoFromExprList(pParse, pGroupBy); 306313449892Sdrh addrSortingIdx = 306413449892Sdrh sqlite3VdbeOp3(v, OP_OpenVirtual, sAggInfo.sortingIdx, 306513449892Sdrh sAggInfo.nSortingColumn, 306613449892Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 306713449892Sdrh 306813449892Sdrh /* Initialize memory locations used by GROUP BY aggregate processing 306913449892Sdrh */ 307013449892Sdrh iUseFlag = pParse->nMem++; 307113449892Sdrh iAbortFlag = pParse->nMem++; 307213449892Sdrh iAMem = pParse->nMem; 307313449892Sdrh pParse->nMem += pGroupBy->nExpr; 307413449892Sdrh iBMem = pParse->nMem; 307513449892Sdrh pParse->nMem += pGroupBy->nExpr; 3076d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iAbortFlag); 3077de29e3e9Sdrh VdbeComment((v, "# clear abort flag")); 3078d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iUseFlag); 3079de29e3e9Sdrh VdbeComment((v, "# indicate accumulator empty")); 308013449892Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, addrInitializeLoop); 308113449892Sdrh 308213449892Sdrh /* Generate a subroutine that outputs a single row of the result 308313449892Sdrh ** set. This subroutine first looks at the iUseFlag. If iUseFlag 308413449892Sdrh ** is less than or equal to zero, the subroutine is a no-op. If 308513449892Sdrh ** the processing calls for the query to abort, this subroutine 308613449892Sdrh ** increments the iAbortFlag memory location before returning in 308713449892Sdrh ** order to signal the caller to abort. 308813449892Sdrh */ 308913449892Sdrh addrSetAbort = sqlite3VdbeCurrentAddr(v); 3090de29e3e9Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, iAbortFlag); 3091de29e3e9Sdrh VdbeComment((v, "# set abort flag")); 309213449892Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 309313449892Sdrh addrOutputRow = sqlite3VdbeCurrentAddr(v); 309413449892Sdrh sqlite3VdbeAddOp(v, OP_IfMemPos, iUseFlag, addrOutputRow+2); 3095de29e3e9Sdrh VdbeComment((v, "# Groupby result generator entry point")); 309613449892Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 309713449892Sdrh finalizeAggFunctions(pParse, &sAggInfo); 309813449892Sdrh if( pHaving ){ 309913449892Sdrh sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, 1); 310013449892Sdrh } 310113449892Sdrh rc = selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy, 310213449892Sdrh distinct, eDest, iParm, 310313449892Sdrh addrOutputRow+1, addrSetAbort, aff); 310413449892Sdrh if( rc ){ 310513449892Sdrh goto select_end; 310613449892Sdrh } 310713449892Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 3108de29e3e9Sdrh VdbeComment((v, "# end groupby result generator")); 310913449892Sdrh 3110e313382eSdrh /* Generate a subroutine that will reset the group-by accumulator 3111e313382eSdrh */ 3112e313382eSdrh addrReset = sqlite3VdbeCurrentAddr(v); 3113e313382eSdrh resetAccumulator(pParse, &sAggInfo); 3114e313382eSdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 3115e313382eSdrh 311613449892Sdrh /* Begin a loop that will extract all source rows in GROUP BY order. 311713449892Sdrh ** This might involve two separate loops with an OP_Sort in between, or 311813449892Sdrh ** it might be a single loop that uses an index to extract information 311913449892Sdrh ** in the right order to begin with. 312013449892Sdrh */ 312113449892Sdrh sqlite3VdbeResolveLabel(v, addrInitializeLoop); 3122e313382eSdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset); 312313449892Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy); 31245360ad34Sdrh if( pWInfo==0 ) goto select_end; 312513449892Sdrh if( pGroupBy==0 ){ 312613449892Sdrh /* The optimizer is able to deliver rows in group by order so 312713449892Sdrh ** we do not have to sort. The OP_OpenVirtual table will be 312813449892Sdrh ** cancelled later because we still need to use the pKeyInfo 312913449892Sdrh */ 313013449892Sdrh pGroupBy = p->pGroupBy; 313113449892Sdrh groupBySort = 0; 313213449892Sdrh }else{ 313313449892Sdrh /* Rows are coming out in undetermined order. We have to push 313413449892Sdrh ** each row into a sorting index, terminate the first loop, 313513449892Sdrh ** then loop over the sorting index in order to get the output 313613449892Sdrh ** in sorted order 313713449892Sdrh */ 313813449892Sdrh groupBySort = 1; 313913449892Sdrh sqlite3ExprCodeExprList(pParse, pGroupBy); 314013449892Sdrh sqlite3VdbeAddOp(v, OP_Sequence, sAggInfo.sortingIdx, 0); 314113449892Sdrh j = pGroupBy->nExpr+1; 314213449892Sdrh for(i=0; i<sAggInfo.nColumn; i++){ 314313449892Sdrh struct AggInfo_col *pCol = &sAggInfo.aCol[i]; 314413449892Sdrh if( pCol->iSorterColumn<j ) continue; 314513449892Sdrh if( pCol->iColumn<0 ){ 314613449892Sdrh sqlite3VdbeAddOp(v, OP_Rowid, pCol->iTable, 0); 314713449892Sdrh }else{ 314813449892Sdrh sqlite3VdbeAddOp(v, OP_Column, pCol->iTable, pCol->iColumn); 314913449892Sdrh } 315013449892Sdrh j++; 315113449892Sdrh } 315213449892Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, j, 0); 315313449892Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, sAggInfo.sortingIdx, 0); 315413449892Sdrh sqlite3WhereEnd(pWInfo); 315597571957Sdrh sqlite3VdbeAddOp(v, OP_Sort, sAggInfo.sortingIdx, addrEnd); 3156de29e3e9Sdrh VdbeComment((v, "# GROUP BY sort")); 315713449892Sdrh sAggInfo.useSortingIdx = 1; 315813449892Sdrh } 315913449892Sdrh 316013449892Sdrh /* Evaluate the current GROUP BY terms and store in b0, b1, b2... 316113449892Sdrh ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth) 316213449892Sdrh ** Then compare the current GROUP BY terms against the GROUP BY terms 316313449892Sdrh ** from the previous row currently stored in a0, a1, a2... 316413449892Sdrh */ 316513449892Sdrh addrTopOfLoop = sqlite3VdbeCurrentAddr(v); 316613449892Sdrh for(j=0; j<pGroupBy->nExpr; j++){ 316713449892Sdrh if( groupBySort ){ 316813449892Sdrh sqlite3VdbeAddOp(v, OP_Column, sAggInfo.sortingIdx, j); 316913449892Sdrh }else{ 317013449892Sdrh sAggInfo.directMode = 1; 317113449892Sdrh sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr); 317213449892Sdrh } 317313449892Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iBMem+j, j<pGroupBy->nExpr-1); 317413449892Sdrh } 317513449892Sdrh for(j=pGroupBy->nExpr-1; j>=0; j--){ 317613449892Sdrh if( j<pGroupBy->nExpr-1 ){ 317713449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, iBMem+j, 0); 317813449892Sdrh } 317913449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, iAMem+j, 0); 318013449892Sdrh if( j==0 ){ 3181e313382eSdrh sqlite3VdbeAddOp(v, OP_Eq, 0x200, addrProcessRow); 318213449892Sdrh }else{ 31834f686238Sdrh sqlite3VdbeAddOp(v, OP_Ne, 0x200, addrGroupByChange); 318413449892Sdrh } 318513449892Sdrh sqlite3VdbeChangeP3(v, -1, (void*)pKeyInfo->aColl[j], P3_COLLSEQ); 318613449892Sdrh } 318713449892Sdrh 318813449892Sdrh /* Generate code that runs whenever the GROUP BY changes. 318913449892Sdrh ** Change in the GROUP BY are detected by the previous code 319013449892Sdrh ** block. If there were no changes, this block is skipped. 319113449892Sdrh ** 319213449892Sdrh ** This code copies current group by terms in b0,b1,b2,... 319313449892Sdrh ** over to a0,a1,a2. It then calls the output subroutine 319413449892Sdrh ** and resets the aggregate accumulator registers in preparation 319513449892Sdrh ** for the next GROUP BY batch. 319613449892Sdrh */ 319713449892Sdrh sqlite3VdbeResolveLabel(v, addrGroupByChange); 319813449892Sdrh for(j=0; j<pGroupBy->nExpr; j++){ 3199d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemMove, iAMem+j, iBMem+j); 320013449892Sdrh } 320113449892Sdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow); 3202de29e3e9Sdrh VdbeComment((v, "# output one row")); 320313449892Sdrh sqlite3VdbeAddOp(v, OP_IfMemPos, iAbortFlag, addrEnd); 3204de29e3e9Sdrh VdbeComment((v, "# check abort flag")); 3205e313382eSdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset); 3206de29e3e9Sdrh VdbeComment((v, "# reset accumulator")); 320713449892Sdrh 320813449892Sdrh /* Update the aggregate accumulators based on the content of 320913449892Sdrh ** the current row 321013449892Sdrh */ 321113449892Sdrh sqlite3VdbeResolveLabel(v, addrProcessRow); 321213449892Sdrh updateAccumulator(pParse, &sAggInfo); 3213de29e3e9Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, iUseFlag); 3214de29e3e9Sdrh VdbeComment((v, "# indicate data in accumulator")); 321513449892Sdrh 321613449892Sdrh /* End of the loop 321713449892Sdrh */ 321813449892Sdrh if( groupBySort ){ 321913449892Sdrh sqlite3VdbeAddOp(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop); 322013449892Sdrh }else{ 322113449892Sdrh sqlite3WhereEnd(pWInfo); 3222f8875400Sdrh sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1); 322313449892Sdrh } 322413449892Sdrh 322513449892Sdrh /* Output the final row of result 322613449892Sdrh */ 322713449892Sdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow); 3228de29e3e9Sdrh VdbeComment((v, "# output final row")); 322913449892Sdrh 323013449892Sdrh } /* endif pGroupBy */ 323113449892Sdrh else { 323213449892Sdrh /* This case runs if the aggregate has no GROUP BY clause. The 323313449892Sdrh ** processing is much simpler since there is only a single row 323413449892Sdrh ** of output. 323513449892Sdrh */ 323613449892Sdrh resetAccumulator(pParse, &sAggInfo); 323713449892Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); 32385360ad34Sdrh if( pWInfo==0 ) goto select_end; 323913449892Sdrh updateAccumulator(pParse, &sAggInfo); 324013449892Sdrh sqlite3WhereEnd(pWInfo); 324113449892Sdrh finalizeAggFunctions(pParse, &sAggInfo); 324213449892Sdrh pOrderBy = 0; 32435774b806Sdrh if( pHaving ){ 32445774b806Sdrh sqlite3ExprIfFalse(pParse, pHaving, addrEnd, 1); 32455774b806Sdrh } 324613449892Sdrh selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 324713449892Sdrh eDest, iParm, addrEnd, addrEnd, aff); 324813449892Sdrh } 324913449892Sdrh sqlite3VdbeResolveLabel(v, addrEnd); 325013449892Sdrh 325113449892Sdrh } /* endif aggregate query */ 32522282792aSdrh 3253cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 3254cce7d176Sdrh ** and send them to the callback one by one. 3255cce7d176Sdrh */ 3256cce7d176Sdrh if( pOrderBy ){ 3257cdd536f0Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 3258cce7d176Sdrh } 32596a535340Sdrh 326093758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 3261f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 32621787ccabSdanielk1977 ** temporary table. So set the SrcList_item.isPopulated flag to prevent 32631787ccabSdanielk1977 ** this subquery from being evaluated again and to force the use of 32641787ccabSdanielk1977 ** the temporary table. 3265f620b4e2Sdrh */ 3266f620b4e2Sdrh if( pParent ){ 3267f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 3268f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 32691787ccabSdanielk1977 pParent->pSrc->a[parentTab].isPopulated = 1; 3270f620b4e2Sdrh } 327193758c8dSdanielk1977 #endif 3272f620b4e2Sdrh 3273ec7429aeSdrh /* Jump here to skip this query 3274ec7429aeSdrh */ 3275ec7429aeSdrh sqlite3VdbeResolveLabel(v, iEnd); 3276ec7429aeSdrh 32771d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 32781d83f052Sdrh ** to indicate no errors. 32791d83f052Sdrh */ 32801d83f052Sdrh rc = 0; 32811d83f052Sdrh 32821d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 32831d83f052Sdrh ** successful coding of the SELECT. 32841d83f052Sdrh */ 32851d83f052Sdrh select_end: 3286955de52cSdanielk1977 3287955de52cSdanielk1977 /* Identify column names if we will be using them in a callback. This 3288955de52cSdanielk1977 ** step is skipped if the output is going to some other destination. 3289955de52cSdanielk1977 */ 3290955de52cSdanielk1977 if( rc==SQLITE_OK && eDest==SRT_Callback ){ 3291955de52cSdanielk1977 generateColumnNames(pParse, pTabList, pEList); 3292955de52cSdanielk1977 } 3293955de52cSdanielk1977 329413449892Sdrh sqliteFree(sAggInfo.aCol); 329513449892Sdrh sqliteFree(sAggInfo.aFunc); 32961d83f052Sdrh return rc; 3297cce7d176Sdrh } 3298